Like Tree1Likes
  • 1 Post By dennis.cpp

Question about something defined in slob.c

This is a discussion on Question about something defined in slob.c within the Linux Programming forums, part of the Platform Specific Boards category; Code: #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT) Could someone explain to me what this does? I would be really ...

  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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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.
    Xpl0ReRChR likes this.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Fedora 19 (64-Bit) / Linux 3.11.6-200.fc19 / KDE SC 4.11.2
    Compiler: gcc 4.8.2 with Netbeans IDE 7.4

  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, 06:11 AM
  5. #if defined...
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-06-2002, 07:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21