Debian Codegraph @

vendor/Heap-Layers/legacy/reap/regionheap.h

raw
1 // -*- C++ -*-
2
3 /*
4
5 Heap Layers: An Extensible Memory Allocation Infrastructure
6
7 Copyright (C) 2000-2020 by Emery Berger
8 http://www.emeryberger.com
9 [email protected]
10
11 Heap Layers is distributed under the terms of the Apache 2.0 license.
12
13 You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 */
17
18 #ifndef _REGIONHEAP_H_
19 #define _REGIONHEAP_H_
20
21 /**
22 * @file regionheap.h
23 * @brief The support classes for reap.
24 * @author Emery Berger <http://www.cs.umass.edu/~emery>
25 */
26
27 #include <assert.h>
28
29 #include "heaplayers.h"
30 #include "chunkheap.h"
31 #include "nestedheap.h"
32 #include "slopheap.h"
33 #include "threads/cpuinfo.h"
34
35 // Reap-specific.
36 #include "addheader.h"
37 #include "clearoptimizeheap.h"
38
39 /**
40 * @class LeaHeap2
41 * @brief An implementation of the Lea heap, using only an sbrk heap.
42 */
43
44 #include "dlheap.h"
45
46 using namespace HL;
47
48 template <class Sbrk>
49 class LeaHeap2 :
50 public
51 Threshold<HL::CPUInfo::PageSize,
52 DLSmallHeapType<DLBigHeapType<CoalesceableHeap<Sbrk> > > >
53 {};
54
55
56
57 #if 0 // USE stack for non-touching deletion stuff (for drag measurements)
58
59 // NB: The magic MaxGlobalElements here supports 1GB worth of 8K chunks.
60
61 template <class TYPE, int MaxGlobalElements = 131072>
62 class StaticStack
63 {
64 public:
65
66 StaticStack (void)
67 : head (NULL, NULL)
68 {
69 }
70
71 /**
72 * @brief Push an item onto the stack.
73 * @return 0 iff the stack is full.
74 */
75
76 inline int push (TYPE v) {
77 Entry * e = new Entry (v, head.next);
78 head.next = e;
79 return 1;
80 }
81
82 /**
83 * @brief Pops an item off of the stack.
84 * @return 0 iff the stack is empty.
85 */
86
87 inline int pop (TYPE& t) {
88 Entry * e = head.next;
89 if (e == NULL) {
90 return 0;
91 }
92 head.next = head.next->next;
93 TYPE r = e->datum;
94 delete e;
95 t = r;
96 return 1;
97 }
98
99 private:
100
101 // class Entry : public PerClassHeap<FreelistHeap<StaticHeap<MaxGlobalElements * (sizeof(TYPE) + sizeof(Entry *))> > > {
102 class Entry : public PerClassHeap<FreelistHeap<ZoneHeap<SbrkHeap, 8192> > > {
103 public:
104 explicit Entry (TYPE d, Entry * n)
105 : datum (d), next (n)
106 {}
107 TYPE datum;
108 Entry * next;
109 };
110
111 Entry head;
112 };
113
114
115 template <class SuperHeap>
116 class RegionHeap : public SuperHeap {
117 public:
118
119 RegionHeap (void) {}
120
121 ~RegionHeap (void)
122 {
123 clear();
124 }
125
126 inline void * malloc (const size_t sz) {
127 void * ptr = SuperHeap::malloc (sz);
128 if (!stk.push (ptr)) {
129 return NULL;
130 } else {
131 return ptr;
132 }
133 }
134
135 inline void clear (void) {
136 void * ptr;
137 while (stk.pop(ptr)) {
138 SuperHeap::free (ptr);
139 }
140 }
141
142 private:
143
144 StaticStack<void *> stk;
145
146 };
147
148 #else
149
150 /**
151 * @class RegionHeap
152 * @brief A heap layer that provides region semantics.
153 */
154
155 template <class SuperHeap>
156 class RegionHeap : public SuperHeap {
157 public:
158
159 RegionHeap (void)
160 : prev (NULL)
161 {}
162
163 ~RegionHeap (void)
164 {
165 clear();
166 }
167
168 inline void * malloc (const size_t sz) {
169 char * ch = (char *) SuperHeap::malloc (sz + sizeof(Header));
170
171 if (ch == NULL) {
172 return NULL;
173 }
174
175 // Put the "header" at the end of the object.
176 // This is just so we can overwrite the start of the object
177 // with a boundary tag (metadata) that will let us free it
178 // into a coalescing heap.
179
180 // The datum member points to the actual start of the object,
181 // while the prev member is our linked-list pointer.
182
183 Header * ptr = (Header *) (ch + sz);
184 ptr->datum = ch;
185 ptr->prev = prev;
186 prev = ptr;
187
188 return (void *) ch;
189 }
190
191 /**
192 * @brief Checks to see if an object was allocated from this heap.
193 * @return 1 iff the obj was in one of our chunks.
194 */
195 int find (void * obj) const {
196 // Search backwards through the header links.
197 Header * curr = prev;
198 while (curr) {
199 if ((curr->datum <= obj) && (curr > obj)) {
200 return 1;
201 }
202 curr = curr->prev;
203 }
204 return 0;
205 }
206
207 /// Delete everything.
208 inline void clear (void) {
209 while (prev) {
210 Header * ptr = prev->prev;
211 SuperHeap::free (prev->datum);
212 prev = ptr;
213 }
214 }
215
216 // Just a NOP.
217 inline void free (void *) {}
218
219 private:
220
221
222 /// The header for allocated objects.
223 class Header {
224 public:
225 /// Points to the start of the allocated object.
226 void * datum;
227
228 /// The previous header.
229 Header * prev;
230 };
231
232 Header * prev;
233 };
234
235 #endif
236
237 class NoHeap {};
238
239 /**
240 * @class ReapTopHeap
241 * @brief Grab 8K chunks, leaving room for the header to be added below.
242 *
243 */
244
245 template <class MainStore>
246 class ReapTopHeap :
247 public ChunkHeap<8192 - sizeof(typename AddHeader<NoHeap>::Header),
248 SlopHeap<RegionHeap<MainStore>, 16> > {};
249
250 /**
251 * @class ReapBaseType
252 * @brief The base implementation for reap.
253 *
254 */
255
256 template <class MainStore>
257 class ReapBaseType :
258 public ClearOptimizeHeap<AddHeader<ReapTopHeap<MainStore> >,
259 LeaHeap2<ReapTopHeap<MainStore> > >
260 {};
261
262 /**
263 * @class Reap
264 * @brief A hybrid region-heap.
265 *
266 * This class uses a per-class freelist heap to optimize reap
267 * allocation, and adds nested (hierarchical) heaps and ANSI
268 * compliance.
269 */
270
271 template <class MainStore>
272 class Reap :
273 public PerClassHeap<FreelistHeap<MainStore> >,
274 public ANSIWrapper<NestedHeap<ReapBaseType<MainStore> > >
275 {};
276
277 #endif