Listing 1
template <class SuperHeap> class SizeHeap { union freeObject { size_t sz; double _dummy; // for alignment. }; public: void * malloc(const size_t sz) { // Add room for a size field. freeObject * ptr = (freeObject *) SuperHeap::malloc(sz + sizeof(freeObject)); // Store the requested size. ptr->sz = sz; return ptr + 1; } void free(void * ptr) { SuperHeap::free((freeObject *) ptr - 1); } static size_t getSize (const void * ptr) { return ((freeObject *)ptr - 1)->sz; } };