previousupnext

4.6 Heap Identifier Tagging

The common programming style is to match allocations with frees, so that each heap allocation is explicitly freed by the application when it is no longer needed. An alternative style, when the operating system provides suitable facilities such as the NT HeapCreate and HeapDestroy routines, is to create a heap dedicated to each subtask or phase of the application. When the subtask is complete, the whole heap is destroyed in one go, saving the programmer from having to keep track of the heap entries. The latter style is perfectly respectable, and indeed can have performance advantages, but it is incompatible with LeapHeap; a NT process only ever has one LeapHeap.

To allow LeapHeap to work with existing applications that are heavy users of the HeapDestroy routine, a tagged mode of operation is provided, selectable through mechanisms described in section 6 The Registry Interface. When operating in tagged mode, LeapHeap maintains a cell tagmap in parallel with the tier-1 cell bitmap. The tagmap records which heap (as viewed by the application) each memory cell is allocated on. The LeapHeap implementation of HeapDestroy is then able to scan the tagmap and free all allocations associated with the heap it is meant to be destroying. A separate scheme is used to add heap identifier tags to big-block descriptors.

The size of the tag is selectable as either 8-bit or 16-bit, depending on the maximum number of concurrent heaps (256 or 65536) the application manages; eight bits will almost always be sufficient. Using tagged mode increases LeapHeap memory requirements by 8 MB or 16 MB of RAM (for the default LeapHeap size) and is to be avoided unless necessary. When discussing algorithms and performance issues this guide often assumes that tagged mode is not in force.

If LeapHeap is used in untagged mode with an application that habitually calls HeapDestroy to remove non-empty heaps, a memory leak will result. LeapHeap records calls of HeapCreate and HeapDestroy so that is is possible to judge whether an application is of this type.