Debian Codegraph @

vendor/Heap-Layers/heaps/utility/uniqueheap.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_UNIQUEHEAP_H
19 #define HL_UNIQUEHEAP_H
20
21 #include <cstdlib>
22 #include <new>
23
24 /**
25 *
26 * @class UniqueHeap
27 * @brief Instantiates one instance of a class used for every malloc & free.
28 * @author Emery Berger
29 *
30 */
31
32 namespace HL {
33
34 template <class SuperHeap, class Child = int>
35 class UniqueHeap {
36 public:
37
38 enum { Alignment = SuperHeap::Alignment };
39
40 /**
41 * Ensure that the super heap gets created,
42 * and add a reference for every instance of unique heap.
43 */
44 UniqueHeap()
45 {
46 volatile SuperHeap * forceCreationOfSuperHeap __attribute__((unused)) = getSuperHeap();
47 addRef();
48 }
49
50 /**
51 * @brief Delete one reference to the unique heap.
52 * When the number of references goes to zero,
53 * delete the super heap.
54 */
55 ~UniqueHeap() {
56 int r = delRef();
57 if (r <= 0) {
58 getSuperHeap()->SuperHeap::~SuperHeap();
59 }
60 }
61
62 // The remaining public methods are just
63 // thin wrappers that route calls to the static object.
64
65 inline void * malloc (size_t sz) {
66 return getSuperHeap()->malloc (sz);
67 }
68
69 inline void free (void * ptr) {
70 getSuperHeap()->free (ptr);
71 }
72
73 inline size_t getSize (void * ptr) {
74 return getSuperHeap()->getSize (ptr);
75 }
76
77 inline int remove (void * ptr) {
78 return getSuperHeap()->remove (ptr);
79 }
80
81 inline void clear() {
82 getSuperHeap()->clear();
83 }
84
85 #if 0
86 inline int getAllocated() {
87 return getSuperHeap()->getAllocated();
88 }
89
90 inline int getMaxAllocated() {
91 return getSuperHeap()->getMaxAllocated();
92 }
93
94 inline int getMaxInUse() {
95 return getSuperHeap()->getMaxInUse();
96 }
97 #endif
98
99 private:
100
101 /// Add one reference.
102 void addRef() {
103 getRefs() += 1;
104 }
105
106 /// Delete one reference count.
107 int delRef() {
108 getRefs() -= 1;
109 return getRefs();
110 }
111
112 /// Internal accessor for reference count.
113 int& getRefs() {
114 static int numRefs = 0;
115 return numRefs;
116 }
117
118 SuperHeap * getSuperHeap() {
119 static char superHeapBuffer[sizeof(SuperHeap)];
120 static SuperHeap * aSuperHeap = (SuperHeap *) (new ((char *) &superHeapBuffer) SuperHeap);
121 return aSuperHeap;
122 }
123
124 void doNothing (Child *) {}
125 };
126
127 }
128 #endif // _UNIQUE_H_