00001
00002
00003 #ifndef _SLOTHEAP_H_
00004 #define _SLOTHEAP_H_
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <assert.h>
00023
00024 #include "chunkheap.h"
00025
00026
00027
00028
00029
00030
00031
00032 template <int chunkSize, int slotSize, class Super>
00033 class SlotInterface;
00034
00035 template <int chunkSize, int slotSize, class Super>
00036 class SlotHeap : public SlotInterface<chunkSize, slotSize, ChunkHeap<chunkSize, slotSize, Super> >{};
00037
00038 template <int chunkSize, int slotSize, class Super>
00039 class SlotInterface : public Super {
00040 public:
00041
00042 SlotInterface (void)
00043 : currentChunk (new (Super::malloc(chunkSize)) Chunk<chunkSize, slotSize>)
00044 {}
00045
00046 inline void * malloc (size_t sz) {
00047 assert (sz == slotSize);
00048
00049
00050 void * ptr = currentChunk->getSlot();
00051 if (ptr == NULL) {
00052
00053 currentChunk = new (Super::malloc(chunkSize)) Chunk<chunkSize, slotSize>;
00054 ptr = currentChunk->getSlot();
00055 }
00056 assert (ptr != NULL);
00057 return ptr;
00058 }
00059
00060 inline void free (void * ptr) {
00061
00062
00063 if (getChunk(ptr) == currentChunk) {
00064 currentChunk->putSlot (ptr);
00065 } else {
00066 Super::free (ptr);
00067 }
00068 }
00069
00070 private:
00071
00072 Chunk<chunkSize, slotSize> * currentChunk;
00073
00074 };
00075
00076 #endif