Help understand the macro

I had a problem understanding a piece of code in the MTD driver

#define ROUNDUP(x, y)       ((((x)+((y)-1))/(y))*(y))
...
static struct mtd_partition my_parts[] =
{
   {
      .name = "boot",
      .size = 0,
      .offset = 0,
      .mask_flags = MTD_WRITEABLE
   },
   {
      .name = "linux",
      .size = 0,
      .offset = 0
   },
   {
       .name = "rootfs",
       .size = 0,
       .offset = 0,
       .mask_flags = MTD_WRITEABLE
   },
   {
       .name = "nvram",
       .size = 0,
       .offset = 0
   },
   {
       .name = 0,
       .size = 0,
       .offset = 0
   }
}
...

i = (sizeof(bcm947xx_parts)/sizeof(struct mtd_partition)) - 2;

bcm947xx_parts[i].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
bcm947xx_parts[i].offset = size - bcm947xx_parts[i].size;

So, here are my searches: 1) why is it necessary to round the size of a partition? 2) Could you help me understand how rounding works? 3) the flash driver in the bootloader on the same platform does not round up for this particular partition, so the flash layout has different offsets in the kernel and in the bootloader. What is the reason for this?

Thanks in advance for your valuable comments!

+5
source share
1 answer

(1) - . (-. , , .) , NVRAM . . - . , - , , ( slow-ish), . ( , .)

(2) :

((((x)+((y)-1))/(y))*(y))

1, , , , , - .

(((x+(y-1))/y)*y)

2, , .

(x+y-1)/y*y

3, C, . x y ( , ), , C .

 floor((x+y-1)/y)*y

4, . x y, , y-1 , y, x. x , y, y-1 y, - y, , x. , x 1 y-1 , y, "+ y-1" y, - y .

, , ROUNDUP (x, y) x y, x. , : , , . ( int = 3; ROUNDUP (6, ++) , i.)

(3) . -, NVRAM ?

+6

All Articles