/* $Id: alloc.h,v 1.4 1996/11/06 11:37:44 roitzsch Exp $ */ #ifndef ALLOC_H #define ALLOC_H #include using namespace std; #include "stack.h" //------------------------------------------------------------------------- // the class StaticAllocator is designed only for use as a static object, // thus its destructor takes no action // for the derived class Allocator, the objects must supply a reset-function // (as the constructor is not called if an object is returned from the stack) //------------------------------------------------------------------------- template class StaticAllocator { protected: struct MemoryBlock { T* space; MemoryBlock* next; }; MemoryBlock* lastBlock; Stack returnedElements; int maxElementsInBlock; int noOfElementsInBlock; int noOfBlocksSinceIncrement; int noOfBlocks, memSpace; T* freeElement; T* getFromBlock(); void allocationError(); public: StaticAllocator(int maxElementsInBlock); virtual ~StaticAllocator() { } virtual T* Get(); void Return(T* elem) { if (elem) returnedElements.push(elem); } void Info() const; int MemSpace() const; int NoOfBlocks() const { return noOfBlocks; } }; //------------------------------------------------------------------------- //------------------------------------------------------------------------- template class Allocator : public StaticAllocator { public: Allocator(int maxElementsInBlock0) : StaticAllocator(maxElementsInBlock0) {} virtual ~Allocator(); // virtual T* Allocator:: Get(); virtual T* Get(); }; //------------------------------------------------------------------------- #if INCLUDE_TEMPLATE_DEFS #include "alloc.c" #endif #endif