| 1 |
extern void abort (void); |
| 2 |
|
| 3 |
typedef struct foo_t |
| 4 |
{ |
| 5 |
unsigned int blksz; |
| 6 |
unsigned int bf_cnt; |
| 7 |
} foo_t; |
| 8 |
|
| 9 |
#define _RNDUP(x, unit) ((((x) + (unit) - 1) / (unit)) * (unit)) |
| 10 |
#define _RNDDOWN(x, unit) ((x) - ((x)%(unit))) |
| 11 |
|
| 12 |
long long |
| 13 |
foo (foo_t *const pxp, long long offset, unsigned int extent) |
| 14 |
{ |
| 15 |
long long blkoffset = _RNDDOWN(offset, (long long )pxp->blksz); |
| 16 |
unsigned int diff = (unsigned int)(offset - blkoffset); |
| 17 |
unsigned int blkextent = _RNDUP(diff + extent, pxp->blksz); |
| 18 |
|
| 19 |
if (pxp->blksz < blkextent) |
| 20 |
return -1LL; |
| 21 |
|
| 22 |
if (pxp->bf_cnt > pxp->blksz) |
| 23 |
pxp->bf_cnt = pxp->blksz; |
| 24 |
|
| 25 |
return blkoffset; |
| 26 |
} |
| 27 |
|
| 28 |
int |
| 29 |
main () |
| 30 |
{ |
| 31 |
foo_t x; |
| 32 |
long long xx; |
| 33 |
|
| 34 |
x.blksz = 8192; |
| 35 |
x.bf_cnt = 0; |
| 36 |
xx = foo (&x, 0, 4096); |
| 37 |
if (xx != 0LL) |
| 38 |
abort (); |
| 39 |
return 0; |
| 40 |
} |