Contracting a binary tree heap
Posted: Wed Nov 25, 2009 2:26 pm
I've been redesigning my memory manager lately, and have come up with two binary trees, sorted by size. The free and used blocks are each represented in one of these trees, making deallocation a simple matter of removing a node from one tree and adding it to another. I can also bring it closer to best fit, by inspecting the left and right nodes.
I've run into a small roadblock when I try to contract the 'heap' of available pages. I simply can't figure out how to practically remove the nodes making up a page from the free list. The only safe way to do this that I can think of is to start at the smallest free address, and look for nodes with a start address of previousNode->Address+previousNode->Size. When the total size of that continuous block is 0x1000 blocks, I remove all of them from the free list and release the pages they use. I do this as many times as I need to.
But in practice, this concept seems to elude me. More precisely, I don't know how to make it run quickly. Something that I've also been considering is adding an extra field to the memory block descriptor which provides a pointer to the next block in the page. But adding an extra integer to every block of memory seems wasteful, and it seems as though it would slow down the addition of nodes to the tree. This would change the contraction to just looking through the nodes until the 'chain' that it creates has a total size of 0x1000.
Is there any other way of contracting a binary tree heap that I can't think of, or any way to make this faster?
I've run into a small roadblock when I try to contract the 'heap' of available pages. I simply can't figure out how to practically remove the nodes making up a page from the free list. The only safe way to do this that I can think of is to start at the smallest free address, and look for nodes with a start address of previousNode->Address+previousNode->Size. When the total size of that continuous block is 0x1000 blocks, I remove all of them from the free list and release the pages they use. I do this as many times as I need to.
But in practice, this concept seems to elude me. More precisely, I don't know how to make it run quickly. Something that I've also been considering is adding an extra field to the memory block descriptor which provides a pointer to the next block in the page. But adding an extra integer to every block of memory seems wasteful, and it seems as though it would slow down the addition of nodes to the tree. This would change the contraction to just looking through the nodes until the 'chain' that it creates has a total size of 0x1000.
Is there any other way of contracting a binary tree heap that I can't think of, or any way to make this faster?