00001
00002
00003 #ifndef _TOPSLOTHEAP_H_
00004 #define _TOPSLOTHEAP_H_
00005
00006 #include <assert.h>
00007
00008 #include "bigchunk.h"
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 template <int chunkSize, int slotSize, class Super>
00020 class TopSlotHeap : public Super {
00021 public:
00022
00023 TopSlotHeap (void)
00024 : myChunks (NULL)
00025 {}
00026
00027
00028 inline void * malloc (size_t sz);
00029
00030
00031 inline void free (void * ptr);
00032
00033 protected:
00034
00035 virtual inline void localFree (void * ptr);
00036
00037
00038 private:
00039
00040 BigChunk<chunkSize, slotSize, TopSlotHeap> * myChunks;
00041
00042 };
00043
00044
00045 template <int chunkSize, int slotSize, class Super>
00046 void * TopSlotHeap<chunkSize, slotSize, Super>::malloc (size_t sz)
00047 {
00048 assert (sz <= chunkSize);
00049 if (myChunks == NULL) {
00050 return new (Super::malloc (chunkSize)) BigChunk<chunkSize, slotSize, TopSlotHeap>;
00051 } else {
00052 printf ("Recycled a chunk.\n");
00053 BigChunk<chunkSize, slotSize, TopSlotHeap> * ch = myChunks;
00054 myChunks = myChunks->getNext();
00055 ch->setNext (NULL);
00056 ch->setHeap (NULL);
00057 return ch;
00058 }
00059 }
00060
00061
00062 template <int chunkSize, int slotSize, class Super>
00063 void TopSlotHeap<chunkSize, slotSize, Super>::free (void * ptr) {
00064 printf ("Freed a chunk.\n");
00065 BigChunk<chunkSize, slotSize, TopSlotHeap> * ch = (BigChunk<chunkSize, slotSize, TopSlotHeap> *) ptr;
00066 ch->setNext (myChunks);
00067 ch->setHeap (this);
00068 myChunks = ch;
00069 }
00070
00071
00072 template <int chunkSize, int slotSize, class Super>
00073 void TopSlotHeap<chunkSize, slotSize, Super>::localFree (void * ptr) {
00074 free (ptr);
00075 }
00076
00077
00078 #endif