/* -*- C++ -*- */ #ifndef _TOPSLOTHEAP_H_ #define _TOPSLOTHEAP_H_ #include #include "bigchunk.h" /* TopSlotHeap. malloc returns objects of size chunkSize. free returns objects of size chunkSize. */ template class TopSlotHeap : public Super { public: TopSlotHeap (void) : myChunks (NULL) {} // Get a chunkSize object. inline void * malloc (size_t sz); // Free a chunkSize object. inline void free (void * ptr); protected: virtual inline void localFree (void * ptr); private: BigChunk * myChunks; }; template void * TopSlotHeap::malloc (size_t sz) { assert (sz <= chunkSize); if (myChunks == NULL) { return new (Super::malloc (chunkSize)) BigChunk; } else { printf ("Recycled a chunk.\n"); BigChunk * ch = myChunks; myChunks = myChunks->getNext(); ch->setNext (NULL); ch->setHeap (NULL); return ch; } } template void TopSlotHeap::free (void * ptr) { printf ("Freed a chunk.\n"); BigChunk * ch = (BigChunk *) ptr; ch->setNext (myChunks); ch->setHeap (this); myChunks = ch; } template void TopSlotHeap::localFree (void * ptr) { free (ptr); } #endif