Listing 3
template <class Super, size_t S1, size_t S2> struct FLHeap { ~FLHeap() { while (myFreeList) { freeObject* next = myFreeList->next; Super::free(myFreeList); myFreeList = next; } } void * malloc(const size_t s) { if (s < S1 || s > S2)) { return Super::malloc(s); } if (!myFreeList) { return Super::malloc(S2); } void * ptr = myFreeList; myFreeList = myFreeList->next; return ptr; } void free(void * p) { const size_t s = getSize(p); if (s < S1 || s > S2) { return Super::free(p); } freeObject p = reinterpret_cast<freeObject *>(ptr); p->next = myFreeList; myFreeList = p; } private: /// The linked list pointer we embed in the freed objects. class freeObject { public: freeObject * next; }; /// The head of the linked list of freed objects. freeObject * myFreeList; };