| 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 | /** |
| 19 | * @class ZoneHeap |
| 20 | * @brief A zone (or arena, or region) based allocator. |
| 21 | * @author Emery Berger |
| 22 | * |
| 23 | * Uses the superclass to obtain large chunks of memory that are only |
| 24 | * returned when the heap itself is destroyed. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #ifndef HL_ZONEHEAP_H |
| 29 | #define HL_ZONEHEAP_H |
| 30 | |
| 31 | #include <assert.h> |
| 32 | |
| 33 | #include "utility/align.h" |
| 34 | #include "wrappers/mallocinfo.h" |
| 35 | |
| 36 | namespace HL { |
| 37 | |
| 38 | template <class SuperHeap, size_t ChunkSize> |
| 39 | class ZoneHeap : public SuperHeap { |
| 40 | public: |
| 41 | |
| 42 | enum { Alignment = SuperHeap::Alignment }; |
| 43 | |
| 44 | ZoneHeap() |
| 45 | : _sizeRemaining (0), |
| 46 | _currentArena (nullptr), |
| 47 | _pastArenas (nullptr) |
| 48 | {} |
| 49 | |
| 50 | ~ZoneHeap() |
| 51 | { |
| 52 | clear(); |
| 53 | } |
| 54 | |
| 55 | inline void * malloc (size_t sz) { |
| 56 | void * ptr = zoneMalloc (sz); |
| 57 | // assert ((size_t) ptr % Alignment == 0); |
| 58 | return ptr; |
| 59 | } |
| 60 | |
| 61 | /// Free in a zone allocator is a no-op. |
| 62 | inline void free (void *) {} |
| 63 | |
| 64 | /// Remove in a zone allocator is a no-op. |
| 65 | inline int remove (void *) { return 0; } |
| 66 | |
| 67 | void clear() { |
| 68 | // printf ("deleting arenas!\n"); |
| 69 | // Delete all of our arenas. |
| 70 | Arena * ptr = _pastArenas; |
| 71 | while (ptr != nullptr) { |
| 72 | auto oldPtr = ptr; |
| 73 | ptr = ptr->nextArena; |
| 74 | SuperHeap::free (oldPtr, oldPtr->arenaSize); |
| 75 | } |
| 76 | if (_currentArena != nullptr) { |
| 77 | //printf ("deleting %x\n", _currentArena); |
| 78 | SuperHeap::free ((void *) _currentArena, _currentArena->arenaSize); |
| 79 | } |
| 80 | _currentArena = nullptr; |
| 81 | _sizeRemaining = 0; |
| 82 | _pastArenas = nullptr; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | |
| 87 | ZoneHeap (const ZoneHeap&); |
| 88 | ZoneHeap& operator=(const ZoneHeap&); |
| 89 | |
| 90 | inline void * zoneMalloc (size_t sz) { |
| 91 | void * ptr; |
| 92 | // Round up size to an aligned value. |
| 93 | sz = HL::align<HL::MallocInfo::Alignment>(sz); |
| 94 | // Get more space in our arena if there's not enough room in this one. |
| 95 | if ((_currentArena == nullptr) || (_sizeRemaining < sz)) { |
| 96 | // First, add this arena to our past arena list. |
| 97 | if (_currentArena != nullptr) { |
| 98 | _currentArena->nextArena = _pastArenas; |
| 99 | _pastArenas = _currentArena; |
| 100 | } |
| 101 | // Now get more memory. |
| 102 | size_t allocSize = ChunkSize; |
| 103 | if (allocSize < sz) { |
| 104 | allocSize = sz; |
| 105 | } |
| 106 | _currentArena = |
| 107 | (Arena *) SuperHeap::malloc (allocSize + sizeof(Arena)); |
| 108 | if (_currentArena == nullptr) { |
| 109 | return nullptr; |
| 110 | } |
| 111 | _currentArena->arenaSpace = (char *) (_currentArena + 1); |
| 112 | _currentArena->nextArena = nullptr; |
| 113 | _currentArena->arenaSize = allocSize + sizeof(Arena); |
| 114 | _sizeRemaining = allocSize; |
| 115 | } |
| 116 | // Bump the pointer and update the amount of memory remaining. |
| 117 | _sizeRemaining -= sz; |
| 118 | ptr = _currentArena->arenaSpace; |
| 119 | _currentArena->arenaSpace += sz; |
| 120 | assert (ptr != nullptr); |
| 121 | // assert ((size_t) ptr % SuperHeap::Alignment == 0); |
| 122 | return ptr; |
| 123 | } |
| 124 | |
| 125 | class Arena { |
| 126 | public: |
| 127 | Arena() { |
| 128 | static_assert((sizeof(Arena) % HL::MallocInfo::Alignment == 0), |
| 129 | "Alignment must match Arena size."); |
| 130 | } |
| 131 | |
| 132 | Arena * nextArena; |
| 133 | char * arenaSpace; |
| 134 | size_t arenaSize; |
| 135 | }; |
| 136 | |
| 137 | /// Space left in the current arena. |
| 138 | size_t _sizeRemaining; |
| 139 | |
| 140 | /// The current arena. |
| 141 | Arena * _currentArena; |
| 142 | |
| 143 | /// A linked list of past arenas. |
| 144 | Arena * _pastArenas; |
| 145 | }; |
| 146 | |
| 147 | } |
| 148 | |
| 149 | #endif |