19.3 General Memory Management Concepts 893
// Remove a block from the free list.
HeaderBlock* header = freeList.RemoveFront();
header->Used = true;
// Subdivide the blocks until you find one of the
// minimal size to satisfy the request.
while (k > kmin)
{
// The block needs to be split.
k=k-1;
HeaderBlock* buddy = header + pow(2,k);
buddy->Used = false;
buddy->Size = k;
// The list m_freeList[k] is empty to have gotten
// here. Add buddy to this list (easy).
m_freeList[k] = buddy;
buddy->Prev = buddy;
buddy->Next = buddy;
}
// At this point we know k == kmin.
char* allocated = header + m_hsize;
return allocated;
}
Memory deallocation is described by the following pseudocode. Attempts are
made to coalesce buddies.
void MemoryManager::Deallocate (char* toDeallocate) ...