Thread: Flagbits

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Flagbits

    Hi all - hope this makes sense!

    Just wanted to ask a question regarding the 12 bytes or so that is prepended to a block of memory storing a structure.

    How do I go about walking the block pointer to get the information on a structure that is in memory?

    Any tuts - or docs that I can read on this?


    Cheers!

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Well, this sounds OS dependant, but if you want to get a pointer to the start of a 12-byte block in front of your structure, try this:
    Code:
    char *ptr;
    
    ptr = (char *) &my_structure;
    
    ptr -= 12; /* go back 12 bytes */
    Is that what you wanted?
    Jason Deckard

  3. #3
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    well, this looks like a lot with a recent post of mine
    about passing a memeory allocation to a function without
    passing its allocation size as an argument...

    Any suggestions on where to find more info on this subject
    would be appreciated

    many thanks

  4. #4
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Is that what you wanted?
    Yes and no.

    From what I have read in a couple of other posts there appears to be almost like a header for the block which is comprised of 12 bytes of data. This data also appears to contain the size of the data block.. which would be useful as Trekker says in determining the size of a datablock that is passed to a function without passing its allocation size as an argument.

    I did actually wonder if this is implentation or OS dependant, or if there is a standard structure to this data?

  5. #5
    Sayeh
    Guest
    An O/S isn't magical-- the very same questions you're asking, it has to have answers to in order to maintain itself.

    And yes, this is O/S dependent. Some O/S's use more bytes or fewer, depending on what information they want to store with each block.

    Some O/S's don't use blockheaders at all, they retain that information in the master pointer list which instead of just pointers contains structures.

    Locate Memory Manager information from the O/S builder.

    As for accessing block headers (if your O/S handles allocations this way), yes it's just a negative offset from the base pointer. For example:

    Code:
    /* defines */
    
    #define  0x00000001   flagLOCKED;
    #define  0x00000002   flagRESIZE;
    
    /* typedefs */
    
    typedef struct                              /* block header format (12 bytes) */
       {
       long     blockSize;                      /* size of ramblock + blockheader size */
       long     flagBits;                         /* block 'state' */
       int        whichZone;                   /* what heap is ramblock allocated from */
       int        elemSize;                      /* smallest incrementing step */
       }blockheader;
    
    
    /* prototypes */
    
    void foo(void);                              /* our test function */
    
    
    /* functions & procs */
    
    void foo(void)
       {
       blockheader     *bhPtr;
       char                  *rawData;
       long                  blockSize;
    
       bhPtr = 0L;                                /* init vars */
       rawData = (char*)malloc(1024L);
       if(rawData)
          {
          /* assume, from our research, that a windows blockheader appears as above typedef'd */
    
          bhPtr = (blockheader*)(rawData - sizeof(blockheader));
    
          blockSize = bhPtr->blockSize;      /* get block size */
          blockSize -= sizeof(blockheader);   /* account for header size */
    
          free(rawData);
          rawData = 0L;
          bhPtr = 0L;
          };
       }
    I hope this helps.

  6. #6
    Unregistered
    Guest
    Oooh yes! Thanks Sayeh..

Popular pages Recent additions subscribe to a feed