Multi-Heap Management

This discusses game programs when they create and use multiple heaps.

Multi-Heaps

Various types of data are used in a game, such as graphics, music, and system data. Using multiple heaps (for example, a game, sound, system heap) makes data management easier. Using multiple heaps in this way is referred to as multi-heaps.

Freeing Multi-Heap Memory

A programmer should know from which heap to allocate memory blocks. Therefore, the programmer can specify and allocate memory block from the heap.

What about freeing memory blocks? If you allocated these memory blocks, you will know which heap you should return the memory blocks to. So which heap should memory blocks be returned to when they are freed by another programmer? Even in this case, determining which heap the memory blocks should be returned to is possible if the intended purpose of each heap among the multiple heaps is clear. However, what do you do if the memory block can be returned to multiple heaps?

Managing Heaps Using a Tree Structure

When you can’t determine where the memory block that is being freed was allocated, a method to search for the heap where the memory block was allocated would be convenient. To implement this method, you can manage the heaps with a tree structure. You can use the memory blocks allocated from a heap like heap memory (a hierarchical heap structure).

Using a tree structure to manage heaps, you can recursively check the heap memory region to check which heap the memory block was allocated from. The MEM library internally creates a hierarchical structure for each heap that it creates. There is also a function that searches for the heap that a memory block was allocated from. These functions are shown in the following table.

Functions for Searching for Heaps which Memory Blocks Were Allocated From
FunctionDescription
MEMFindContainHeapSearches for the heap which the specified memory block was allocated from and returns a handle to this heap.
MEMFindParentHeapReturns the parent heap handle if the parent heap containing the specified heap exists. Returns MME_HEAP_INVALID_HANDLE (a NULL value) if the heap does not exist.

Revision History

03/01/2006 Initial version.