Could someone explain to me what this does? I would be really greatful!Code:#define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
Could someone explain to me what this does? I would be really greatful!Code:#define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
Last edited by Xpl0ReRChR; 04-18-2013 at 01:44 AM.
It defines a macro called "SLOB_UNITS" that calculates a certain value on the basis of a parameter "size" via the formula ((size) + SLOB_UNIT - 1) / SLOB_UNIT.
SLOB_UNIT seems to be another macro representing a constant value. Should also be defined somewhere in the file.
Thank you Dennis! SLOB_UNIT is defined as sizeof(slob_t), which is a struct describing a block.
I should have been more specific. What I meant to ask is, what is it that the formula represents?
Given that information:
I'd suggest the formula determines how many slob_t elements the value of the parameter size contains. Say the struct slob_t has 2 kB. Now you pass your macro a size of, say, 16 kB:SLOB_UNIT is defined as sizeof(slob_t), which is a struct describing a block.
Go through the formula: (size (16) + SLOB_UNIT (2) - 1) / SLOB_UNIT (2))Code:SLOB_UNITS(16)
16 + 2 - 1 / 2 = 8 (integer division!), which would be exactly the number of slob_ts in a size of 16.
The name of the macro makes sense that way, too.
It makes perfect sense, I should have tried using an example myself. Good point on the integer division!
Thank you for taking the time!