1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
These routines form a robust, block allocation mechanism that is somewhat
tweakable for specific applications.
BlockHeap * BlockHeapCreate (size_t elemsize, int elemsperblock)
Creates a new block heap, from which elements can be allocated. The
size of each block of memory can be tuned by changing elemsperblock.
elemsize should contain the size of elements you will be allocating
from this block.
void * BlockHeapAlloc (BlockHeap *bh)
Allocates a single element from the passed block heap. Be sure to
cast the returned pointer to the type you expect. Alternately,
you can use the #defined macro BlockHeapALLOC (BlockHeap *bh, type)
where "type" is your element type (int, double, struct, etc.)
int BlockHeapGarbageCollect (BlockHeap *bh)
Cleans up all blocks that are completely free in the blockheap.
Used as a general-purpose garbage collection routine.
int BlockHeapFree (BlockHeap *bh, void *ptr)
Frees the element pointed to by *ptr. The memory is not returned to
the system -- just the block heap itself. Returns 0 if successful, or
1 if the element is not contained within the given blockheap.
int BlockHeapDestroy (BlockHeap *bh)
Destroys the entire block heap, including free()ing all associated
memory. Use as a cleanup function; all pointers to memory allocated
using BlockHeapAlloc() will become invalid.
------------------------------------------------------------------------
Note: do not use this system for anything new. The authors was
braindamaged; block allocation is stupid. It appears faster by sheer
coincidence.
- asuffield
|