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.
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.
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.
|Initializes a block heap.|
|Destroys a block heap.|
The following functions help add blocks to a block heap so that more allocations can take place.
|Adds more blocks to a block heap, so that more allocations may be tracked.|
|Returns 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.|
The following functions allocate and free memory areas in a block heap.
|Allocates a memory area from the block heap using the default alignment.|
|Allocates a memory area from the block heap attempting to use a specified address. The size of the memory area is also specified.|
|Allocates a memory area from the block heap. The alignment can be specified (described in the next section).|
|Deallocates a memory area to a block heap.|
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.
|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|
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.
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.
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).
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
function does not specify an alignment; the alignment value is always 4.
2013/05/08 Automated cleanup pass.
2010/11/01 Initial version.