Advertisement Jump to content
Sign in to follow this  
luasitdown

How does the snippet work?

This topic is 4917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!