Debian Codegraph @

vendor/Heap-Layers/heaps/combining/hybridheap.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_HYBRIDHEAP_H
19 #define HL_HYBRIDHEAP_H
20
21 #include <assert.h>
22
23 #include "heaplayers.h"
24
25 /**
26 * @class HybridHeap
27 * Objects no bigger than BigSize are allocated and freed to SmallHeap.
28 * Bigger objects are passed on to the super heap.
29 */
30
31 namespace HL {
32
33 template <int BigSize, class SmallHeap, class BigHeap>
34 class HybridHeap : public SmallHeap {
35 public:
36
37 HybridHeap (void)
38 {
39 static_assert(BigSize > 0, "Large object size must be positive.");
40 }
41
42 enum { Alignment = gcd<(int) SmallHeap::Alignment, (int) BigHeap::Alignment>::value };
43
44 MALLOC_FUNCTION INLINE void * malloc (size_t sz) {
45 void * ptr;
46 if (sz <= BigSize) {
47 ptr = SmallHeap::malloc (sz);
48 } else {
49 ptr = slowPath (sz);
50 }
51 if (SmallHeap::getSize(ptr) < sz) {
52 assert(bm.getSize(ptr) >= sz);
53 }
54 assert (SmallHeap::getSize(ptr) >= sz);
55 assert ((size_t) ptr % Alignment == 0);
56 return ptr;
57 }
58
59 inline void free (void * ptr) {
60 if (SmallHeap::getSize(ptr) <= BigSize) {
61 SmallHeap::free (ptr);
62 } else {
63 bm.free (ptr);
64 }
65 }
66
67 inline void clear (void) {
68 bm.clear();
69 SmallHeap::clear();
70 }
71
72
73 private:
74
75 MALLOC_FUNCTION NO_INLINE
76 void * slowPath (size_t sz) {
77 return bm.malloc (sz);
78 }
79
80
81 BigHeap bm;
82 };
83
84 }
85
86 #endif