How does the snippet work?
// 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
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:
(Note: I could be wrong, that is fairly ugly stuff :)
--ajas
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
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? ;)
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? ;)
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.
Quote:Original post by luasitdown
That seem linked list.
That's exactly what it is, overlaid the pooled memory.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement