Thread: Question about something defined in slob.c

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    69

    Question about something defined in slob.c

    Code:
    #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
    Could someone explain to me what this does? I would be really greatful!
    Last edited by Xpl0ReRChR; 04-18-2013 at 01:44 AM.

  2. #2
    spaghetticode
    Guest
    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.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    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?

  4. #4
    spaghetticode
    Guest
    Given that information:

    SLOB_UNIT is defined as sizeof(slob_t), which is a struct describing a block.
    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:

    Code:
    SLOB_UNITS(16)
    Go through the formula: (size (16) + SLOB_UNIT (2) - 1) / SLOB_UNIT (2))
    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.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    69
    It makes perfect sense, I should have tried using an example myself. Good point on the integer division!

    Thank you for taking the time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user defined template question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2006, 05:01 AM
  2. how to use a self-defined lib in VC6
    By wu7up in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 03:04 AM
  3. Replies: 7
    Last Post: 04-13-2003, 10:53 PM
  4. Static but never defined?
    By electrolove in forum C Programming
    Replies: 3
    Last Post: 03-25-2003, 07:11 AM
  5. #if defined...
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2002, 08:08 PM