1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _LAZYSLOTHEAP_H_ |
---|
4 | #define _LAZYSLOTHEAP_H_ |
---|
5 | |
---|
6 | /* |
---|
7 | This heap manages memory in units of Chunks. |
---|
8 | malloc returns a slot within a chunk, |
---|
9 | while free returns slots back to a chunk. |
---|
10 | |
---|
11 | We completely exhaust the first chunk before we ever get another one. |
---|
12 | Once a chunk (except for our current one) is COMPLETELY empty, it is returned to the superheap. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <assert.h> |
---|
16 | #include <new.h> |
---|
17 | |
---|
18 | #include "chunk.h" |
---|
19 | |
---|
20 | |
---|
21 | template <int chunkSize, int slotSize, class Super> |
---|
22 | class LazySlotHeap : public Super { |
---|
23 | public: |
---|
24 | |
---|
25 | LazySlotHeap (void) |
---|
26 | : myChunk (new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>()) |
---|
27 | {} |
---|
28 | |
---|
29 | ~LazySlotHeap (void) |
---|
30 | { |
---|
31 | // Give up our chunk. |
---|
32 | Super::free (myChunk); |
---|
33 | } |
---|
34 | |
---|
35 | inline void * malloc (size_t sz) { |
---|
36 | assert (sz <= slotSize); |
---|
37 | void * ptr = myChunk->getSlot(); |
---|
38 | if (ptr == NULL) { |
---|
39 | myChunk = new (Super::malloc (chunkSize)) Chunk<chunkSize, slotSize>(); |
---|
40 | ptr = myChunk->getSlot(); |
---|
41 | assert (ptr != NULL); |
---|
42 | } |
---|
43 | return ptr; |
---|
44 | } |
---|
45 | |
---|
46 | inline void free (void * ptr) { |
---|
47 | /// Return a slot to its chunk. |
---|
48 | Chunk<chunkSize, slotSize> * ch = Chunk<chunkSize, slotSize>::getChunk (ptr); |
---|
49 | ch->putSlot (ptr); |
---|
50 | // check if it's completely empty. If so, free it. |
---|
51 | if (ch != myChunk) { |
---|
52 | // Once the chunk is completely empty, free it. |
---|
53 | if (ch->getNumSlotsAvailable() == ch->getNumSlots()) { |
---|
54 | Super::free (ch); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | #if 0 |
---|
59 | // If this chunk isn't the one we're currently holding, |
---|
60 | // free it. NB: It is NOT guaranteed to be empty! |
---|
61 | if (ch != myChunk) { |
---|
62 | Super::free (ch); |
---|
63 | } |
---|
64 | #endif |
---|
65 | #if 0 |
---|
66 | template <int chunkSize, int slotSize, class Super> |
---|
67 | class StrictSlotHeap : public LazySlotHeap { |
---|
68 | public: |
---|
69 | inline void free (void * ptr) { |
---|
70 | /// Return a slot to its chunk. |
---|
71 | Chunk<chunkSize, slotSize> * ch = Chunk<chunkSize, slotSize>::getChunk (ptr); |
---|
72 | ch->putSlot (ptr); |
---|
73 | // check if it's completely empty. If so, free it. |
---|
74 | if (ch != myChunk) { |
---|
75 | // Once the chunk is completely empty, free it. |
---|
76 | if (ch->getNumSlotsAvailable() == ch->getNumSlots()) { |
---|
77 | Super::free (ch); |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | }; |
---|
83 | #endif |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | inline static size_t size (void * ptr) |
---|
88 | { |
---|
89 | return slotSize; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | protected: |
---|
94 | |
---|
95 | Chunk<chunkSize, slotSize> * myChunk; |
---|
96 | |
---|
97 | }; |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | #endif |
---|