Sign in to follow this  
luasitdown

How does the snippet work?

Recommended Posts

// setup the free list m_pHeadOfFreeList = pBlob->m_Data; Assert (m_pHeadOfFreeList); void **newBlob = (void**)m_pHeadOfFreeList; for (int j = 0; j < nElements-1; j++) { newBlob[0] = (char*)newBlob + m_BlockSize; newBlob = (void**)newBlob[0]; } // null terminate list newBlob[0] = NULL; m_NumBlobs++; The pointer confuse me.my god. it is from mempool.cpp

Share this post


Link to post
Share on other sites
It looks like there's a memory layout such that the first word of each block is a pointer to the next (free) block. So at the beginning of block x it assigns x + m_BlockSize to that word, then follows the pointer by assigning x to itself.

A more readable (but still ugly) way of writing that would be:



for(int j = 0; j < nElements-1; j++)
{
newBlob[0] = (char *)newBlob + m_BlockSize;
(char *)newBlob += m_BlockSize;
}


(Note: I could be wrong, that is fairly ugly stuff :)

--ajas

Share this post


Link to post
Share on other sites
Without going to lengths to explain how it does it let me tell you what it does ;)
There's a fairly well known technique when building memorpools and chunked allocators where you in an empty block save the pointer to the next (empty) block instead overlaid the object storage. That way you don't need to keep the pointer around as overhead, so empty blocks really hold a pointer to a new empty block and then you have the "head" pointer pointing to one such block.

When freeing a block you simply have to store the head pointer in the just freed block and store a pointer to that block as head, allocating a block simply follows the pointer from the headblock to the next block marks that as the new head and returns the block previously pointed to.

Easy huh? ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by DigitalDelusion
Without going to lengths to explain how it does it let me tell you what it does ;)
There's a fairly well known technique when building memorpools and chunked allocators where you in an empty block save the pointer to the next (empty) block instead overlaid the object storage. That way you don't need to keep the pointer around as overhead, so empty blocks really hold a pointer to a new empty block and then you have the "head" pointer pointing to one such block.

When freeing a block you simply have to store the head pointer in the just freed block and store a pointer to that block as head, allocating a block simply follows the pointer from the headblock to the next block marks that as the new head and returns the block previously pointed to.

Easy huh? ;)


That seem linked list.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this