| 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 | * @file segheap.h |
| 20 | * @brief Definition of SegHeap. |
| 21 | */ |
| 22 | |
| 23 | #ifndef HL_SEGHEAP_H |
| 24 | #define HL_SEGHEAP_H |
| 25 | |
| 26 | /** |
| 27 | * @class SegHeap |
| 28 | * @brief A segregated-fits collection of (homogeneous) heaps. |
| 29 | * @author Emery Berger |
| 30 | * |
| 31 | * Note that one extra heap is used for objects that are "too big". |
| 32 | * |
| 33 | * @param NumBins The number of bins (subheaps). |
| 34 | * @param getSizeClass Function to compute size class from size. |
| 35 | * @param getClassMaxSize Function to compute the largest size for a given size class. |
| 36 | * @param LittleHeap The subheap class. |
| 37 | * @param BigHeap The parent class, used for "big" objects. |
| 38 | * |
| 39 | * Example:<BR> |
| 40 | * <TT> |
| 41 | * int myFunc (size_t sz); // The size-to-class function.<BR> |
| 42 | * size_t myFunc2 (int); // The class-to-size function.<P> |
| 43 | * // The heap. Use freelists for these small objects,<BR> |
| 44 | * // but defer to malloc for large objects.<P> |
| 45 | * |
| 46 | * SegHeap<4, myFunc, myFunc2, freelistHeap<MallocHeap>, MallocHeap> mySegHeap; |
| 47 | * </TT> |
| 48 | **/ |
| 49 | |
| 50 | #include <assert.h> |
| 51 | |
| 52 | #include "utility/gcd.h" |
| 53 | |
| 54 | namespace HL { |
| 55 | |
| 56 | template <int NumBins, |
| 57 | int (*getSizeClass) (const size_t), |
| 58 | size_t (*getClassMaxSize) (const int), |
| 59 | class LittleHeap, |
| 60 | class BigHeap> |
| 61 | class SegHeap : public LittleHeap { |
| 62 | public: |
| 63 | |
| 64 | enum { Alignment = gcd<LittleHeap::Alignment, BigHeap::Alignment>::VALUE }; |
| 65 | |
| 66 | static constexpr size_t num_size_classes = NumBins; |
| 67 | int get_size_class(const size_t sz) const { return getSizeClass(sz); } |
| 68 | size_t get_class_size(int i) const { return getClassMaxSize(i); } |
| 69 | |
| 70 | inline SegHeap() |
| 71 | : _memoryHeld (0), |
| 72 | _maxObjectSize (getClassMaxSize(NumBins - 1)) |
| 73 | { |
| 74 | for (int i = 0; i < NUM_ULONGS; i++) { |
| 75 | binmap[i] = 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | inline size_t getMemoryHeld() const { |
| 80 | return _memoryHeld; |
| 81 | } |
| 82 | |
| 83 | size_t getSize (void * ptr) { |
| 84 | return LittleHeap::getSize (ptr); |
| 85 | } |
| 86 | |
| 87 | inline void * malloc (const size_t sz) { |
| 88 | void * ptr = nullptr; |
| 89 | if (sz > _maxObjectSize) { |
| 90 | goto GET_MEMORY; |
| 91 | } |
| 92 | |
| 93 | { |
| 94 | const auto sc = getSizeClass(sz); |
| 95 | assert (sc >= 0); |
| 96 | assert (sc < NumBins); |
| 97 | auto idx = sc; |
| 98 | auto block = idx2block (idx); |
| 99 | unsigned long map = binmap[block]; |
| 100 | unsigned long bit = idx2bit (idx); |
| 101 | |
| 102 | for (;;) { |
| 103 | if (bit > map || bit == 0) { |
| 104 | do { |
| 105 | if (++block >= NUM_ULONGS) { |
| 106 | goto GET_MEMORY; |
| 107 | // return bigheap.malloc (sz); |
| 108 | } |
| 109 | } while ( (map = binmap[block]) == 0); |
| 110 | |
| 111 | idx = block << SHIFTS_PER_ULONG; |
| 112 | bit = 1; |
| 113 | } |
| 114 | |
| 115 | while ((bit & map) == 0) { |
| 116 | bit <<= 1; |
| 117 | assert(bit != 0); |
| 118 | idx++; |
| 119 | } |
| 120 | |
| 121 | assert (idx < NumBins); |
| 122 | ptr = myLittleHeap[idx].malloc (sz); |
| 123 | |
| 124 | if (ptr == NULL) { |
| 125 | binmap[block] = map &= ~bit; // Write through |
| 126 | idx++; |
| 127 | bit <<= 1; |
| 128 | } else { |
| 129 | _memoryHeld -= sz; |
| 130 | return ptr; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | GET_MEMORY: |
| 136 | if (ptr == NULL) { |
| 137 | // There was no free memory in any of the bins. |
| 138 | // Get some memory. |
| 139 | ptr = bigheap.malloc (sz); |
| 140 | } |
| 141 | |
| 142 | return ptr; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | inline void free (void * ptr) { |
| 147 | // printf ("Free: %x (%d bytes)\n", ptr, getSize(ptr)); |
| 148 | const auto objectSize = getSize(ptr); // was bigheap.getSize(ptr) |
| 149 | if (objectSize > _maxObjectSize) { |
| 150 | bigheap.free (ptr); |
| 151 | } else { |
| 152 | auto objectSizeClass = getSizeClass(objectSize); |
| 153 | assert (objectSizeClass >= 0); |
| 154 | assert (objectSizeClass < NumBins); |
| 155 | // Put the freed object into the right sizeclass heap. |
| 156 | assert (getClassMaxSize(objectSizeClass) >= objectSize); |
| 157 | #if 1 |
| 158 | while (getClassMaxSize(objectSizeClass) > objectSize) { |
| 159 | objectSizeClass--; |
| 160 | } |
| 161 | #endif |
| 162 | assert (getClassMaxSize(objectSizeClass) <= objectSize); |
| 163 | if (objectSizeClass > 0) { |
| 164 | assert (objectSize >= getClassMaxSize(objectSizeClass - 1)); |
| 165 | } |
| 166 | |
| 167 | myLittleHeap[objectSizeClass].free (ptr); |
| 168 | mark_bin (objectSizeClass); |
| 169 | _memoryHeld += objectSize; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void clear() { |
| 175 | for (auto i = 0; i < NumBins; i++) { |
| 176 | myLittleHeap[i].clear(); |
| 177 | } |
| 178 | for (auto j = 0; j < NUM_ULONGS; j++) { |
| 179 | binmap[j] = 0; |
| 180 | } |
| 181 | bigheap.clear(); |
| 182 | _memoryHeld = 0; |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | |
| 187 | enum { BITS_PER_ULONG = sizeof(unsigned long) * 8 }; |
| 188 | enum { SHIFTS_PER_ULONG = (BITS_PER_ULONG == 32) ? 5 : 6 }; |
| 189 | enum { MAX_BITS = (NumBins + BITS_PER_ULONG - 1) & ~(BITS_PER_ULONG - 1) }; |
| 190 | |
| 191 | |
| 192 | private: |
| 193 | |
| 194 | static inline int idx2block (int i) { |
| 195 | int blk = i >> SHIFTS_PER_ULONG; |
| 196 | assert (blk < NUM_ULONGS); |
| 197 | assert (blk >= 0); |
| 198 | return blk; |
| 199 | } |
| 200 | |
| 201 | static inline unsigned long idx2bit (int i) { |
| 202 | unsigned long bit = ((1U << ((i) & ((1U << SHIFTS_PER_ULONG)-1)))); |
| 203 | return bit; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | protected: |
| 208 | |
| 209 | BigHeap bigheap; |
| 210 | |
| 211 | enum { NUM_ULONGS = MAX_BITS / BITS_PER_ULONG }; |
| 212 | unsigned long binmap[NUM_ULONGS]; |
| 213 | |
| 214 | inline int get_binmap (int i) const { |
| 215 | return binmap[i >> SHIFTS_PER_ULONG] & idx2bit(i); |
| 216 | } |
| 217 | |
| 218 | inline void mark_bin (int i) { |
| 219 | binmap[i >> SHIFTS_PER_ULONG] |= idx2bit(i); |
| 220 | } |
| 221 | |
| 222 | inline void unmark_bin (int i) { |
| 223 | binmap[i >> SHIFTS_PER_ULONG] &= ~(idx2bit(i)); |
| 224 | } |
| 225 | |
| 226 | size_t _memoryHeld; |
| 227 | |
| 228 | const size_t _maxObjectSize; |
| 229 | |
| 230 | // The little heaps. |
| 231 | LittleHeap myLittleHeap[NumBins]; |
| 232 | |
| 233 | }; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | |
| 238 | #endif |