// ============================================================ // // subHeap.h // // Copyright 2002, Dennis Meilicke and Rene Peralta // // ============================================================ // // Description: // // Implementation of the sub-heap allocator. // // ============================================================ #ifndef __ln3_subHeap__ #define __ln3_subHeap__ #include using namespace std; template class SubHeapAllocator { private: OBJ *block; OBJ *endOfBlock; OBJ *nextBase; OBJ *freeBase; size_t quanta; public: SubHeapAllocator( ) { block = 0; endOfBlock = 0; nextBase = (OBJ *)1; freeBase = 0; quanta = 10000; } OBJ *allocate( void ) { OBJ *ret; if( freeBase ) { ret = freeBase; freeBase = *( (OBJ **)freeBase ); } else { if( nextBase > endOfBlock ) { // // allocate enough for 'quanta' Bases, plus one void pointer // for the block list. // OBJ *newBlock = (OBJ *)(new char[ sizeof( void * ) + ( sizeof(OBJ) * quanta ) ]); *( (OBJ **)newBlock ) = block; block = newBlock; nextBase = (OBJ *)( (char *)block + sizeof( void * ) ); endOfBlock = block + quanta; } ret = nextBase; nextBase++; } return ret; } void deallocate( OBJ *x ) { *((OBJ **)x) = freeBase; freeBase = x; } inline size_t getQuanta( void ) { return quanta; } inline void setQuanta( size_t q ) { quanta = q; } }; #endif