The Block Heap

A block heap is a memory space that is tracked and controlled through the use of a set of block structures. The block structures are kept separate from the memory space they control. The number of allocations that can be performed on the heap is limited by the number of block structures that are given to it.


Figure 5-1 Block heap logical structure

A block heap has the advantage that it does not use any of the memory it is managing. This makes it ideal for use in situations where you are tracking read-only areas, as well as for areas where the contents of memory change rapidly or change over the boundaries between subsequent allocations. A block heap has the disadvantage that the tracking blocks must be kept somewhere, and due to the nature of heap usage the number of blocks can almost never realistically shrink.

Creating Heaps

To use a Block Heap, usually the space for one is instantiated statically in your program. It is then initialized when the program runs, and tracking blocks are added to it to let it manage a memory range.

Functions to Create and Destroy Block Heaps
FunctionsFeatures
MEMInitBlockHeapInitializes a block heap.
MEMDestroyBlockHeapDestroys a block heap.

Adding Blocks Used to Track Allocations

The following functions help add blocks to a block heap so that more allocations can take place.

Functions for controlling tracking blocks in a block heap
FunctionsFeatures
MEMAddBlockHeapTrackingAdds more blocks to a block heap, so that more allocations may be tracked.
MEMGetTrackingLeftInBlockHeapReturns the number of free blocks left that are used to track allocations. More than one block may be used to track an allocation if an allocation to a specific address or at a specific alignment splits a free area into three pieces.

Allocating Memory Areas

The following functions allocate and free memory areas in a block heap.

Functions for Allocating and Freeing Memory Blocks
FunctionsFeatures
MEMAllocFromBlockHeapAllocates a memory area from the block heap using the default alignment.
MEMAllocFromBlockHeapAtAllocates a memory area from the block heap attempting to use a specified address. The size of the memory area is also specified.
MEMAllocFromBlockHeapExAllocates a memory area from the block heap. The alignment can be specified (described in the next section).
MEMFreeToExpHeapDeallocates a memory area to a block heap.

Determining How Many Blocks You Need

How many blocks you need to track allocations is strictly based on your use of the memory area. Occasionally, you may need relatively few blocks ( < 32 ). But you may need many more. You may also need to add blocks if you run out.

Defines to help determine memory needed for tracking in a block heap
FunctionsFeatures
MEM_BLKHEAP_BYTES_PER_BLOCK_TRACKThis #define determines how many bytes are needed per tracking block
MEM_BLKHEAP_TRACK_GROUP_OVERHEADThis #define determines how much extra overhead is taken when a group of blocks are added to track the heap
MEM_BLKHEAP_TRACKING_BYTES(numTrack)This macro determines how many bytes you need for a group of tracking blocks, taking into account the overhead for a group

Allocating Memory Blocks from the Top of the Heap Region

Generally, the Block Heap searches for free regions from the lowest to highest address in the heap region and allocates memory blocks to the free regions starting from the bottom. As an alternative, you can search for free regions from the highest to lowest address in the heap. Using this feature, you can allocate longer-term memory blocks from the beginning of the heap region, and temporary memory blocks from the end of the heap region to help minimize heap fragmentation. For example, you can use this feature to load compressed data into memory, expand the compressed data, and then delete the compressed data source. In this case, if the expansion process is performed by allocating the memory block from the bottom of the heap region as usual, the free region will be divided into two.


Figure 2-2 Mechanism for Segmenting Memory Blocks

In contrast, if you temporarily load the compressed data into a memory block allocated from the top of the heap region, the free region is not divided.


Figure 2-3 Method for Segmenting Expanded Heap Memory Blocks

To allocate memory blocks from the top of the heap region, use the memory block allocation function, MEMAllocFromBlockHeapEx, and pass a negative value to the alignment argument (described below).

Specifying Alignment

The Block Heap specifies alignment during memory block allocation. In the MEMAllocFromBlockHeapEx function, you can specify any number as an alignment values. If negative values (i.e., -4, -8, ...) are specified, memory blocks are allocated from the top of the heap. The MEMAllocFromBlockHeap function does not specify an alignment; the alignment value is always 4.

Revision History

2013/05/08 Automated cleanup pass.
2010/11/01 Initial version.


CONFIDENTIAL