Debian Codegraph @

vendor/Heap-Layers/heaps/special/obstack.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 // An "Obstack".
19
20 #ifndef HL_OBSTACK_H
21 #define HL_OBSTACK_H
22
23
24 #include <assert.h>
25 #include <new>
26
27 #include "utility/align.h"
28 #include "wrappers/mallocinfo.h"
29
30 /**
31 * @class ObstackHeap
32 * @brief Implements obstack functionality (as in the GNU obstack library).
33 */
34
35 namespace HL {
36
37 template <int ChunkSize, class SuperHeap>
38 class ObstackHeap : public SuperHeap {
39 public:
40
41 ObstackHeap()
42 {
43 // Get one chunk and set the current position marker.
44 currentChunk = makeChunk (NULL, ChunkSize);
45 currentBase = nextPos = (char *) (currentChunk + 1);
46 assert (isValid());
47 }
48
49 ~ObstackHeap() {
50 // Free every chunk.
51 assert (isValid());
52 ChunkHeader * ch = currentChunk;
53 while (ch != NULL) {
54 ChunkHeader * pch = ch->getPrevChunk();
55 // cout << "Freeing chunk " << ch << endl;
56 SuperHeap::free (ch);
57 ch = pch;
58 }
59 }
60
61 // "Grow" the current object.
62 // Returns a point in the object just before the current "grow".
63 inline void * grow (size_t sz) {
64 // If we've grown beyond the confines of this chunk,
65 // get a new one.
66 assert (isValid());
67 if ((int) ((char *) currentChunk->getLimit() - (char *) nextPos) < sz) {
68 ChunkHeader * newCurrent = copyToNew(sz);
69 if (newCurrent == NULL)
70 return NULL;
71 #if 0
72 // Now delete the previous chunk if this was the only object in it.
73 if (deleteChunk != NULL) {
74 SuperHeap::free (deleteChunk);
75 }
76 #endif
77 assert (isValid());
78 }
79 assert (((int) ((char *) currentChunk->getLimit() - (char *) nextPos) >= sz));
80 assert ((char *) (sz + nextPos) <= currentChunk->getLimit());
81 // Bump the pointer for the next object.
82 void * prevNextPos = nextPos;
83 nextPos += sz;
84 assert (isValid());
85 return prevNextPos;
86 }
87
88
89 inline void * malloc (size_t sz) {
90 assert (isValid());
91 if (currentChunk == NULL) {
92 return NULL;
93 }
94 //sz = align(sz > 0 ? sz : 1);
95 // If this object can't fit in the current chunk,
96 // get another one.
97 if ((int) ((char *) currentChunk->getLimit() - (char *) nextPos) < sz) {
98 // Allocate a chunk that's large enough to hold the requested size.
99 currentChunk = makeChunk (currentChunk, sz);
100 if (currentChunk == NULL) {
101 return NULL;
102 }
103 currentBase = nextPos = (char *) (currentChunk + 1);
104 assert (isValid());
105 }
106 assert (((int) ((char *) currentChunk->getLimit() - (char *) nextPos) >= sz));
107 assert ((char *) (sz + nextPos) <= currentChunk->getLimit());
108 // Bump the pointers forward.
109 currentBase = nextPos;
110 nextPos += sz;
111 void * ptr = currentBase;
112 finalize();
113 assert (isValid());
114 return (void *) ptr;
115 }
116
117 // Free everything allocated "after" ptr.
118 // NB: Freeing NULL safely empties the entire obstack.
119 inline void free (void * ptr) {
120 assert (isValid());
121 // Free every chunk until we find the one that this pointer is in.
122 // Then reset the current position to ptr.
123 while (currentChunk != NULL &&
124 (((char *) currentChunk > (char *) ptr) ||
125 ((char *) currentChunk->getLimit() < (char *) ptr))) {
126 ChunkHeader * pch = currentChunk;
127 currentChunk = currentChunk->getPrevChunk();
128 SuperHeap::free (pch);
129 }
130 if (currentChunk != NULL) {
131 currentBase = nextPos = (char *) ptr;
132 assert (isValid());
133 } else {
134 if (ptr != NULL) {
135 // Something bad has happened -- we tried to free an item that
136 // wasn't in any chunk.
137 abort();
138 } else {
139 // Get one chunk.
140 currentChunk = makeChunk (NULL, ChunkSize);
141 currentBase = nextPos = (char *) (currentChunk + 1);
142 assert (isValid());
143 }
144 }
145 }
146
147
148 inline void * getObjectBase() {
149 assert (isValid());
150 return currentBase;
151 }
152
153
154 inline void finalize() {
155 assert (isValid());
156 nextPos = (char *) HL::align<HL::MallocInfo::Alignment>((int) nextPos);
157 currentBase = nextPos;
158 assert (isValid());
159 }
160
161
162 private:
163
164
165 inline int objectSize() {
166 int diff = (int) (nextPos - currentBase);
167 assert (diff >= 0);
168 return diff;
169 }
170
171
172 int isValid() {
173 // Verify class invariants.
174 #ifndef NDEBUG
175 bool c1 = (currentBase <= nextPos);
176 assert (c1);
177 bool c2 = (nextPos <= currentChunk->getLimit());
178 assert (c2);
179 bool c3 = ((char *) currentChunk <= currentChunk->getLimit());
180 assert (c3);
181 bool c4 = ((char *) currentChunk <= currentBase);
182 assert (c4);
183 bool c5 = (currentChunk != currentChunk->getPrevChunk());
184 assert (c5);
185 bool c6 = (objectSize() >= 0);
186 assert (c6);
187 return (c1 && c2 && c3 && c4 && c5 && c6);
188 #else
189 return 1;
190 #endif
191 }
192
193 // The header for every chunk.
194 class ChunkHeader {
195 public:
196 inline ChunkHeader (ChunkHeader * prev, size_t sz)
197 : _pastEnd ((char *) (this + 1) + sz),
198 _prevChunk (prev)
199 {}
200
201 // Return the end of the current chunk.
202 inline char * getLimit() { return _pastEnd; }
203
204 // Return the previous chunk.
205 inline ChunkHeader * getPrevChunk() { return _prevChunk; }
206
207 private:
208 // Just past the end of this chunk.
209 char * _pastEnd;
210
211 // Address of prior chunk.
212 ChunkHeader * _prevChunk;
213 };
214
215
216 // Make a new chunk of at least sz bytes.
217 inline ChunkHeader * makeChunk (ChunkHeader * ch, size_t sz) {
218 // Round up the allocation size to at least one chunk.
219 size_t allocSize
220 = HL::align<sizeof(double)>((sz > ChunkSize - sizeof(ChunkHeader)) ? sz : ChunkSize - sizeof(ChunkHeader));
221 // Make a new chunk.
222 ChunkHeader * newChunk
223 = new (SuperHeap::malloc (sizeof(ChunkHeader) + allocSize)) ChunkHeader (ch, allocSize);
224 return newChunk;
225 }
226
227
228 // Copy the current object to a new chunk.
229 // sz = the minimum amount of extra space we need.
230 inline ChunkHeader * copyToNew (size_t sz) {
231 size_t obj_size = objectSize();
232 size_t new_size = obj_size + sz + (obj_size >> 3) + 100;
233 //size_t new_size = 2 * (obj_size + sz);
234 ChunkHeader * newChunk;
235 #if 0
236 // This variable will hold the chunk to be deleted (if any).
237 ChunkHeader * deleteChunk = NULL;
238 // If this object was the only one in the chunk,
239 // link back past this chunk.
240 if (currentBase == (char *) (currentChunk + 1)) {
241 newChunk = makeChunk (currentChunk->getPrevChunk(), new_size);
242 deleteChunk = currentChunk;
243 } else {
244 newChunk = makeChunk (currentChunk, new_size);
245 }
246 #else
247 newChunk = makeChunk (currentChunk, new_size);
248 #endif
249 if (newChunk == NULL) {
250 currentChunk = NULL;
251 return NULL;
252 }
253 // Copy the current object to the new chunk.
254 memcpy ((char *) (newChunk + 1), currentBase, obj_size);
255 currentChunk = newChunk;
256 currentBase = (char *) (currentChunk + 1);
257 nextPos = currentBase + obj_size;
258 #if 0
259 if (deleteChunk != NULL) {
260 SuperHeap::free (deleteChunk);
261 }
262 #endif
263 return currentChunk;
264 }
265
266
267 // Current position in the current chunk.
268 char * currentBase;
269
270 // Where to add the next character to the current object.
271 char * nextPos;
272
273 // My current chunk.
274 ChunkHeader * currentChunk;
275
276 };
277
278 #if 0
279 // The C version of the above (just large enough to hold all of the data structures).
280 struct obstack {
281 void * _dummy[3];
282 };
283 #endif
284
285 }
286
287 #endif // HL_OBSTACK_H