Debian Codegraph @

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

raw symbols SCIP
1 /* -*- C++ -*- */cxx . . $ `<file>/vendor/Heap-Layers/heaps/general/dlheap.h`/
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_Hcxx . . $ `vendor/Heap-Layers/heaps/general/dlheap.h:19:9`!
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"cxx . . $ `<file>/vendor/Heap-Layers/heaps/buildingblock/adaptheap.h`/
30 #include "utility/dllist.h"cxx . . $ `<file>/vendor/Heap-Layers/utility/dllist.h`/
31 #include "utility/sllist.h"cxx . . $ `<file>/vendor/Heap-Layers/utility/sllist.h`/
32 #include "heaps/objectrep/coalesceableheap.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/objectrep/coalesceableheap.h`/
33 #include "heaps/buildingblock/coalesceheap.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/buildingblock/coalesceheap.h`/
34
35 #ifndef TRUE
36 #define TRUEcxx . . $ `vendor/Heap-Layers/heaps/general/dlheap.h:36:9`! 1
37 #define FALSEcxx . . $ `vendor/Heap-Layers/heaps/general/dlheap.h:37:9`! 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 HLcxx . . $ HL/ {
48
49 template <class Mmap>
50 class CoalesceableMmapHeapcxx . . $ HL/CoalesceableMmapHeap# : public RequireCoalesceablecxx . . $ HL/RequireCoalesceable#<Mmap> {
51 public:
52 typedef RequireCoalesceablecxx . . $ HL/RequireCoalesceable#<Mmap> supercxx . . $ HL/CoalesceableMmapHeap#super#;
53 typedef typename RequireCoalesceablecxx . . $ HL/RequireCoalesceable#<Mmap>::Header Headercxx . . $ HL/CoalesceableMmapHeap#Header#;
54
55 inline void * malloccxx . . $ HL/CoalesceableMmapHeap#malloc(bfcfa4d6b7f7ef64). (const size_t sz) {
56 void * buf = supercxx . . $ HL/CoalesceableMmapHeap#super#::malloc (sz + sizeof(Headercxx . . $ HL/CoalesceableMmapHeap#Header#));
57 void * ptr = Headercxx . . $ HL/CoalesceableMmapHeap#Header#::makeObject (buf, 0, sz);
58 supercxx . . $ HL/CoalesceableMmapHeap#super#::markMmapped (ptr);
59 supercxx . . $ HL/CoalesceableMmapHeap#super#::markInUse (ptr);
60 return ptr;
61 }
62 inline void freecxx . . $ HL/CoalesceableMmapHeap#free(5d22bdacc48458e8). (void * ptr) {
63 supercxx . . $ HL/CoalesceableMmapHeap#super#::free (Headercxx . . $ HL/CoalesceableMmapHeap#Header#::getHeader (ptr));
64 }
65 inline int removecxx . . $ HL/CoalesceableMmapHeap#remove(dedf1e9b1c614a07). (void * ptr) {
66 return supercxx . . $ HL/CoalesceableMmapHeap#super#::remove (Headercxx . . $ HL/CoalesceableMmapHeap#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 SelectMmapHeapcxx . . $ HL/SelectMmapHeap# : public super {
82 public:
83 inline void * malloccxx . . $ HL/SelectMmapHeap#malloc(bfcfa4d6b7f7ef64). (const size_t sz) {
84 void * ptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
85 if (sz <= ThresholdBytes) {
86 ptr = smcxx . . $ HL/SelectMmapHeap#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 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
92 ptr = super::malloc (sz);
93 super::markMmapped (ptr);
94 }
95 return ptr;
96 }
97 inline void freecxx . . $ HL/SelectMmapHeap#free(5d22bdacc48458e8). (void * ptr) {
98 if (super::isMmapped(ptr)) {
99 super::free (ptr);
100 } else {
101 smcxx . . $ HL/SelectMmapHeap#sm..free (ptr);
102 }
103 }
104 inline int removecxx . . $ HL/SelectMmapHeap#remove(dedf1e9b1c614a07). (void * ptr) {
105 if (super::isMmapped(ptr)) {
106 return super::remove (ptr);
107 } else {
108 return smcxx . . $ HL/SelectMmapHeap#sm..remove (ptr);
109 }
110 }
111 inline void clearcxx . . $ HL/SelectMmapHeap#clear(49f6e7a06ebc5aa8). (void) {
112 smcxx . . $ HL/SelectMmapHeap#sm..clear();
113 super::clear();
114 }
115
116 private:
117 SmallHeap smcxx . . $ HL/SelectMmapHeap#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 Thresholdcxx . . $ HL/Threshold# : public super {
129 public:
130
131 enumcxx . . $ HL/Threshold#$anonymous_type_0# { MIN_LARGE_SIZEcxx . . $ HL/Threshold#MIN_LARGE_SIZE. = 64 };
132
133 #if 1
134 Thresholdcxx . . $ HL/Threshold#`Threshold<ThresholdBytes, super>`(49f6e7a06ebc5aa8). (void)
135 : freeAllNextMalloccxx . . $ HL/Threshold#freeAllNextMalloc. (FALSEcxx . . $ `vendor/Heap-Layers/heaps/general/dlheap.h:37:9`!),
136 inUsecxx . . $ HL/Threshold#inUse. (0),
137 maxInUsecxx . . $ HL/Threshold#maxInUse. (0),
138 thresholdcxx . . $ HL/Threshold#threshold. (0)
139 {}
140
141 inline void * malloccxx . . $ HL/Threshold#malloc(bfcfa4d6b7f7ef64). (const size_t sz) {
142 void * ptr = super::malloc (sz);
143 if (ptr) {
144 const size_t actualSize = super::getSize(ptr);
145 inUsecxx . . $ HL/Threshold#inUse. += actualSize;
146 if (inUsecxx . . $ HL/Threshold#inUse. > maxInUsecxx . . $ HL/Threshold#maxInUse.) {
147 maxInUsecxx . . $ HL/Threshold#maxInUse. = inUsecxx . . $ HL/Threshold#inUse.;
148 thresholdcxx . . $ HL/Threshold#threshold. = 16384 + maxInUsecxx . . $ HL/Threshold#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 freecxx . . $ HL/Threshold#free(5d22bdacc48458e8). (void * ptr) {
182 const size_t sz = super::getSize(ptr);
183 inUsecxx . . $ HL/Threshold#inUse. -= sz;
184 super::free (ptr);
185 if (super::getMemoryHeld() >cxx . . $ std/`operator>`(39b98f4fe21ae589). thresholdcxx . . $ HL/Threshold#threshold.) {
186 super::freeAll();
187 }
188 }
189
190 private:
191
192 /// The number of bytes in use.
193 size_t inUsecxx . . $ HL/Threshold#inUse.;
194
195 /// The high-water mark of bytes in use.
196 size_t maxInUsecxx . . $ HL/Threshold#maxInUse.;
197
198 size_t thresholdcxx . . $ HL/Threshold#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 freeAllNextMalloccxx . . $ HL/Threshold#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 DLBigHeapNScxx . . $ HL/DLBigHeapNS/
227 {
228 const size_t binscxx . . $ HL/DLBigHeapNS/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 enumcxx . . $ HL/DLBigHeapNS/$anonymous_type_146a10f9b07f24b7_1# { NUMBINScxx . . $ HL/DLBigHeapNS/NUMBINS. = sizeof(binscxx . . $ HL/DLBigHeapNS/bins.) / sizeof(size_t) };
248 enumcxx . . $ HL/DLBigHeapNS/$anonymous_type_146a10f9b07f24b7_2# { BIG_OBJECTcxx . . $ HL/DLBigHeapNS/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 log2cxx . . $ HL/DLBigHeapNS/log2(15f694ef3c96d230). (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 getSizeClasscxx . . $ HL/DLBigHeapNS/getSizeClass(15f694ef3c96d230). (const size_t sz);
265
266 inline size_t getClassSizecxx . . $ HL/DLBigHeapNS/getClassSize(c104ebd7d68b2193). (const int i) {
267 assertcxx . . $ `/usr/include/assert.h:50:10`! (i >= 0);
268 assertcxx . . $ `/usr/include/assert.h:50:10`! (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 binscxx . . $ HL/DLBigHeapNS/bins.[i];
293 #endif
294 }
295
296 inline int getSizeClasscxx . . $ HL/DLBigHeapNS/getSizeClass(15f694ef3c96d230). (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) log2cxx . . $ HL/DLBigHeapNS/log2(15f694ef3c96d230).(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 DLSmallHeapNScxx . . $ HL/DLSmallHeapNS/ {
319 enumcxx . . $ HL/DLSmallHeapNS/$anonymous_type_146a10f9b07f24b7_3# { NUMBINScxx . . $ HL/DLSmallHeapNS/NUMBINS. = 8 };
320 inline int getSizeClasscxx . . $ HL/DLSmallHeapNS/getSizeClass(15f694ef3c96d230). (const size_t sz) {
321 return (int) ((sz-1) >> 3);
322 }
323 inline size_t getClassSizecxx . . $ HL/DLSmallHeapNS/getClassSize(c104ebd7d68b2193). (const int i) {
324 assertcxx . . $ `/usr/include/assert.h:50:10`! (i >= 0);
325 assertcxx . . $ `/usr/include/assert.h:50:10`! (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 DLBigHeapTypecxx . . $ HL/DLBigHeapType# :
355 public
356 CoalesceHeapcxx . . $ HL/CoalesceHeap#<RequireCoalesceablecxx . . $ HL/RequireCoalesceable#<
357 SegHeapcxx . . $ HL/SegHeap#<DLBigHeapNScxx . . $ HL/DLBigHeapNS/::NUMBINScxx . . $ HL/DLBigHeapNS/NUMBINS.,
358 DLBigHeapNScxx . . $ HL/DLBigHeapNS/::getSizeClasscxx . . $ HL/DLBigHeapNS/getSizeClass(15f694ef3c96d230).,
359 DLBigHeapNScxx . . $ HL/DLBigHeapNS/::getClassSizecxx . . $ HL/DLBigHeapNS/getClassSize(c104ebd7d68b2193).,
360 AdaptHeapcxx . . $ HL/AdaptHeap#<DLListcxx . . $ HL/DLList#, NullHeapcxx . . $ HL/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 DLSmallHeapTypecxx . . $ HL/DLSmallHeapType# :
374 public RequireCoalesceablecxx . . $ HL/RequireCoalesceable#<
375 StrictSegHeapcxx . . $ HL/StrictSegHeap#<DLSmallHeapNScxx . . $ HL/DLSmallHeapNS/::NUMBINScxx . . $ HL/DLSmallHeapNS/NUMBINS.,
376 DLSmallHeapNScxx . . $ HL/DLSmallHeapNS/::getSizeClasscxx . . $ HL/DLSmallHeapNS/getSizeClass(15f694ef3c96d230).,
377 DLSmallHeapNScxx . . $ HL/DLSmallHeapNS/::getClassSizecxx . . $ HL/DLSmallHeapNS/getClassSize(c104ebd7d68b2193).,
378 AdaptHeapcxx . . $ HL/AdaptHeap#<HLcxx . . $ HL/::SLListcxx . . $ HL/SLList#, NullHeapcxx . . $ HL/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 LeaHeapcxx . . $ HL/LeaHeap# :
396 public
397 SelectMmapHeapcxx . . $ HL/SelectMmapHeap#<128 * 1024,
398 Thresholdcxx . . $ HL/Threshold#<4096,
399 DLSmallHeapTypecxx . . $ HL/DLSmallHeapType#<DLBigHeapTypecxx . . $ HL/DLBigHeapType#<CoalesceableHeapcxx . . $ HL/CoalesceableHeap#<Sbrk> > > >,
400 CoalesceableMmapHeapcxx . . $ HL/CoalesceableMmapHeap#<Mmap> >
401 {};
402
403 }
404
405 #endif