Debian Codegraph @

vendor/Heap-Layers/heaps/general/dlheap.h

raw symbols SCIP
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 HL_DLHEAP_H
19 #define HL_DLHEAP_H
20
21 /**
22 * @file dlheap.h
23 * @brief Contains all the classes required to approximate DLmalloc 2.7.0.
24 * @author Emery Berger
25 */
26
27 #include <assert.h>
28
29 #include "heaps/buildingblock/adaptheap.h"
30 #include "utility/dllist.h"
31 #include "utility/sllist.h"
32 #include "heaps/objectrep/coalesceableheap.h"
33 #include "heaps/buildingblock/coalesceheap.h"
34
35 #ifndef TRUE
36 #define TRUE 1
37 #define FALSE 0
38 #endif
39
40
41 /**
42 * @class CoalesceableMmapHeap
43 * @brief Adds headers to mmapped objects to allow coalescing.
44 * @author Emery Berger
45 */
46
47 namespace HL {
48
49 template <class Mmap>
50 class CoalesceableMmapHeap : public RequireCoalesceable<Mmap> {
51 public:
52 typedef RequireCoalesceable<Mmap> super;
53 typedef typename RequireCoalesceable<Mmap>::Header Header;
54
55 inline void * malloc (const size_t sz) {
56 void * buf = super::malloc (sz + sizeof(Header));
57 void * ptr = Header::makeObject (buf, 0, sz);
58 super::markMmapped (ptr);
59 super::markInUse (ptr);
60 return ptr;
61 }
62 inline void free (void * ptr) {
63 super::free (Header::getHeader (ptr));
64 }
65 inline int remove (void * ptr) {
66 return super::remove (Header::getHeader (ptr));
67 }
68 };
69
70 /**
71 * @class SelectMmapHeap
72 * @brief Use Mmap (here the superheap) for objects above a certain size.
73 * @author Emery Berger
74 *
75 * @param ThresholdBytes The maximum number of bytes managed by SmallHeap.
76 * @param SmallHeap The heap for "small" objects.
77 * @param super The heap for "large" objects.
78 */
79
80 template <int ThresholdBytes, class SmallHeap, class super>
81 class SelectMmapHeap : public super {
82 public:
83 inline void * malloc (const size_t sz) {
84 void * ptr = NULL;
85 if (sz <= ThresholdBytes) {
86 ptr = sm.malloc (sz);
87 }
88
89 // Fall-through: go ahead and try mmap if the small heap is out of memory.
90
91 if (ptr == NULL) {
92 ptr = super::malloc (sz);
93 super::markMmapped (ptr);
94 }
95 return ptr;
96 }
97 inline void free (void * ptr) {
98 if (super::isMmapped(ptr)) {
99 super::free (ptr);
100 } else {
101 sm.free (ptr);
102 }
103 }
104 inline int remove (void * ptr) {
105 if (super::isMmapped(ptr)) {
106 return super::remove (ptr);
107 } else {
108 return sm.remove (ptr);
109 }
110 }
111 inline void clear (void) {
112 sm.clear();
113 super::clear();
114 }
115
116 private:
117 SmallHeap sm;
118 };
119
120
121 // LeaHeap 2.7.0-like threshold scheme
122 // for managing a small superheap.
123
124
125 // NOTE: THIS HAS BEEN CHANGED NOW!
126
127 template <int ThresholdBytes, class super>
128 class Threshold : public super {
129 public:
130
131 enum { MIN_LARGE_SIZE = 64 };
132
133 #if 1
134 Threshold (void)
135 : freeAllNextMalloc (FALSE),
136 inUse (0),
137 maxInUse (0),
138 threshold (0)
139 {}
140
141 inline void * malloc (const size_t sz) {
142 void * ptr = super::malloc (sz);
143 if (ptr) {
144 const size_t actualSize = super::getSize(ptr);
145 inUse += actualSize;
146 if (inUse > maxInUse) {
147 maxInUse = inUse;
148 threshold = 16384 + maxInUse / 2;
149 }
150 #if 0
151 if (freed < 0) {
152 freed = 0;
153 }
154 #endif
155 }
156 return ptr;
157 }
158
159
160 #if 0
161 void * internalMalloc (const size_t sz) {
162 if (freeAllNextMalloc || (freed > 0)) {
163 freed = 0;
164 super::freeAll();
165 freeAllNextMalloc = FALSE;
166 }
167 void * ptr = super::malloc (sz);
168 if (ptr != NULL) {
169 if (sz < MIN_LARGE_SIZE) {
170 freed -= getSize(ptr);
171 if (freed < 0) {
172 freed = 0;
173 }
174 }
175 }
176 return ptr;
177 }
178 #endif
179
180
181 inline void free (void * ptr) {
182 const size_t sz = super::getSize(ptr);
183 inUse -= sz;
184 super::free (ptr);
185 if (super::getMemoryHeld() > threshold) {
186 super::freeAll();
187 }
188 }
189
190 private:
191
192 /// The number of bytes in use.
193 size_t inUse;
194
195 /// The high-water mark of bytes in use.
196 size_t maxInUse;
197
198 size_t threshold;
199
200 // How many bytes have been freed (whose requests were below MIN_LARGE_SIZE).
201 // int freed;
202
203 /// Should we free all in the superheap on the next malloc?
204 bool freeAllNextMalloc;
205
206 #else
207 inline Threshold (void)
208 {}
209
210 inline void * malloc (const size_t sz) {
211 if ((getMemoryHeld() > ThresholdBytes) ||
212 ((sz >= MIN_LARGE_SIZE) && (getMemoryHeld() >= sz))) {
213 super::freeAll();
214 }
215 return super::malloc (sz);
216 }
217 #endif
218 };
219
220
221 /**
222 * @namespace DLBigHeapNS
223 * @brief All of the bins & size functions for the "big heap".
224 */
225
226 namespace DLBigHeapNS
227 {
228 const size_t bins[] = {8U, 16U, 24U, 32U, 40U, 48U, 56U, 64U, 72U, 80U, 88U,
229 96U, 104U, 112U, 120U, 128U, 136U, 144U, 152U, 160U,
230 168U, 176U, 184U, 192U, 200U, 208U, 216U, 224U, 232U,
231 240U, 248U, 256U, 264U, 272U, 280U, 288U, 296U, 304U,
232 312U, 320U, 328U, 336U, 344U, 352U, 360U, 368U, 376U,
233 384U, 392U, 400U, 408U, 416U, 424U, 432U, 440U, 448U,
234 456U, 464U, 472U, 480U, 488U, 496U, 504U, 512U, 576U,
235 640U, 704U, 768U, 832U, 896U, 960U, 1024U, 1088U, 1152U,
236 1216U, 1280U, 1344U, 1408U, 1472U, 1536U, 1600U, 1664U,
237 1728U, 1792U, 1856U, 1920U, 1984U, 2048U, 2112U, 2560U,
238 3072U, 3584U,
239 4096U, 4608U, 5120U, 5632U, 6144U, 6656U, 7168U, 7680U,
240 8192U, 8704U, 9216U, 9728U, 10240U, 10752U, 12288U,
241 16384U, 20480U, 24576U, 28672U, 32768U, 36864U, 40960U,
242 65536U, 98304U, 131072U, 163840U, 262144U, 524288U,
243 1048576U, 2097152U, 4194304U, 8388608U, 16777216U,
244 33554432U, 67108864U, 134217728U, 268435456U, 536870912U,
245 1073741824U, 2147483648U };
246
247 enum { NUMBINS = sizeof(bins) / sizeof(size_t) };
248 enum { BIG_OBJECT = 2147483648U };
249
250 /**
251 * @brief Compute the log base two.
252 * @param sz The value we want the log of.
253 */
254 inline int log2 (const size_t sz) {
255 int c = 0;
256 size_t sz1 = sz;
257 while (sz1 > 1) {
258 sz1 >>= 1;
259 c++;
260 }
261 return c;
262 }
263
264 inline int getSizeClass (const size_t sz);
265
266 inline size_t getClassSize (const int i) {
267 assert (i >= 0);
268 assert (i < NUMBINS);
269 #if 0
270 if (i < 64) {
271 return ((size_t) ((i+1) << 3));
272 } else {
273 return
274 (i < 89) ? ((size_t) ((i - 55) << 6)) :
275 (i < 106) ? ((size_t) ((i - 84) << 9)) :
276 (i < 114) ? ((size_t) ((i - 103) << 12)) :
277 (i < 118) ? ((size_t) ((i - 112) << 15)) :
278 (i < 120) ? ((size_t) ((i - 117) * 262144)) :
279 (i < 122) ? ((size_t) ((i - 119) * 1048576)) :
280 (i < 124) ? ((size_t) ((i - 121) * 4 * 1048576)) :
281 (i < 126) ? ((size_t) ((i - 123) * 16 * 1048576)) :
282 (i < 128) ? ((size_t) ((i - 125) * 64 * 1048576)) :
283 (i < 130) ? ((size_t) ((i - 127) * 256 * 1048576)) :
284 ((size_t) ((i - 129) * 1024 * 1048576));
285 }
286 #else
287 #if 0
288 if (i < 64) {
289 return (size_t) ((i+1) << 3);
290 }
291 #endif
292 return bins[i];
293 #endif
294 }
295
296 inline int getSizeClass (const size_t sz) {
297 size_t sz1 = sz - 1;
298 if (sz1 <= 513) {
299 return (int) (sz1 >> 3);
300 } else {
301 return (int) ((((sz1 >> 6) <= 32)? 56 + (sz1 >> 6):
302 ((sz1 >> 9) <= 20)? 91 + (sz1 >> 9):
303 ((sz1 >> 12) <= 10)? 110 - 6 + (sz1 >> 12):
304 ((sz1 >> 15) <= 4)? 119 - 6 + (sz1 >> 15):
305 ((sz1 >> 18) <= 2)? 124 - 6 + (sz1 >> 18):
306 126 - 6 + (size_t) log2(sz1>>19)));
307 }
308 }
309
310 }
311
312
313 /**
314 * @namespace DLSmallHeapNS
315 * @brief The size functions for the "small" heap (fastbins).
316 */
317
318 namespace DLSmallHeapNS {
319 enum { NUMBINS = 8 };
320 inline int getSizeClass (const size_t sz) {
321 return (int) ((sz-1) >> 3);
322 }
323 inline size_t getClassSize (const int i) {
324 assert (i >= 0);
325 assert (i < NUMBINS);
326 return (size_t) (((long) i+1) << 3);
327 }
328 }
329
330 #if 0
331
332 #include "kingsleyheap.h"
333
334 /**
335 * @class DLBigHeapType
336 * @brief The "big heap" -- a coalescing segregated-fits allocator.
337 * @author Emery Berger
338 */
339
340 template <class super>
341 class DLBigHeapType :
342 public
343 CoalesceHeap<RequireCoalesceable<
344 SegHeap<Kingsley::NUMBINS,
345 Kingsley::size2Class,
346 Kingsley::class2Size,
347 AdaptHeap<DLList, NullHeap<super> >,
348 super> > >
349 {};
350
351 #else
352
353 template <class super>
354 class DLBigHeapType :
355 public
356 CoalesceHeap<RequireCoalesceable<
357 SegHeap<DLBigHeapNS::NUMBINS,
358 DLBigHeapNS::getSizeClass,
359 DLBigHeapNS::getClassSize,
360 AdaptHeap<DLList, NullHeap<super> >,
361 super> > >
362 {};
363
364 #endif
365
366 /**
367 * @class DLSmallHeapType
368 * @brief The "small heap" -- non-coalescing "fastbins" (quicklists).
369 * @author Emery Berger
370 */
371
372 template <class super>
373 class DLSmallHeapType :
374 public RequireCoalesceable<
375 StrictSegHeap<DLSmallHeapNS::NUMBINS,
376 DLSmallHeapNS::getSizeClass,
377 DLSmallHeapNS::getClassSize,
378 AdaptHeap<HL::SLList, NullHeap<super> >,
379 super> > {};
380
381
382 /**
383 * @class LeaHeap
384 * @brief This heap approximates the algorithms used by DLmalloc 2.7.0.
385 *
386 * The whole thing. Big objects are allocated via mmap.
387 * Other objects are first allocated from the special thresholded quicklists,
388 * or if they're too big, they're allocated from the coalescing big heap.
389 *
390 * @param Sbrk An sbrk-like heap, for small object allocation.
391 * @param Mmap An mmap-like heap, for large object allocation.
392 */
393
394 template <class Sbrk, class Mmap>
395 class LeaHeap :
396 public
397 SelectMmapHeap<128 * 1024,
398 Threshold<4096,
399 DLSmallHeapType<DLBigHeapType<CoalesceableHeap<Sbrk> > > >,
400 CoalesceableMmapHeap<Mmap> >
401 {};
402
403 }
404
405 #endif