bhp_util - Block Heap Allocation Utilities

The BHP_UTIL package implements a generic, block allocation facility.

Programs that allocate and free many of a single kind of structure require a call to malloc(3) for each structure allocated and a corresponding call to free(3) for each structure deallocated. This engenders two system calls and the malloc(3) storage overhead for each structure allocated. The common solution to this problem is to allocate structures in blocks (i.e., N structures at a time) and to dole out the structures individually as needed.

The BHP_UTIL package provides a generic means of managing heaps of structure blocks. To use the package, you must first create a block heap for the size structure you will be allocating and freeing. The following code fragment creates a heap for SomeStructures; the structures are allocated from the system heap in groups of 100:

    #include  "bhp_util.h"		-- Block heap utilities.
    BlockHeap  heap ;
    bhpCreate (sizeof (SomeStructure), 100, NULL, &heap) ;

To allocate memory for an individual structure from that structure's heap, call bhpAlloc():

    SomeStructure  *this ;
    this = bhpAlloc (heap) ;

(In the source code, the individual structures are known as units of memory. Furthermore, a heap is not tied to a particular structure, but to the structure's size; i.e., the unit size.)

Freeing an allocated structure is done as follows:

    bhpFree (heap, this) ;

Finally, an entire heap can be deleted with a call to bhpDestroy():

    bhpDestroy (heap) ;

bhpCreate() accepts an argument that determines from which memory pool to allocate blocks. By default, blocks are allocated from the system heap using malloc(3). Blocks can be allocated from other pools (e.g., global memory) if the appropriate memory pool is created and specified in the call to bhpCreate(); see the Memory Pool Utilities (mpl_util.c) for more information.

Public Procedures

bhpAlloc() - allocates a unit of memory from a block heap.
bhpCreate() - creates a block heap for a specific unit size.
bhpDestroy() - deletes a block heap.
bhpFree() - returns a unit of memory to its block heap.
bhpInfo() - returns information about a block heap.

Source Files


Alex Measday  /  E-mail