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