| 1 | // -*- C++ -*-cxx . . $ `<file>/vendor/Heap-Layers/heaps/combining/segheap.h`/ |
| 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_Hcxx . . $ `vendor/Heap-Layers/heaps/combining/segheap.h:24:9`! |
| 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"cxx . . $ `<file>/vendor/Heap-Layers/utility/gcd.h`/ |
| 53 | |
| 54 | namespace HLcxx . . $ 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 SegHeapcxx . . $ HL/SegHeap# : public LittleHeap { |
| 62 | public: |
| 63 | |
| 64 | enumcxx . . $ HL/SegHeap#$anonymous_type_0# { Alignmentcxx . . $ HL/SegHeap#Alignment. = gcdcxx . . $ HL/gcd#<LittleHeap::Alignment, BigHeap::Alignment>::VALUE }; |
| 65 | |
| 66 | static constexpr size_t num_size_classescxx . . $ HL/SegHeap#num_size_classes. = NumBins; |
| 67 | int get_size_classcxx . . $ HL/SegHeap#get_size_class(e3044d25979e9615).(const size_t sz) const { return getSizeClass(sz); } |
| 68 | size_t get_class_sizecxx . . $ HL/SegHeap#get_class_size(79dc3568750208f6).(int i) const { return getClassMaxSize(i); } |
| 69 | |
| 70 | inline SegHeapcxx . . $ HL/SegHeap#`SegHeap<NumBins, getSizeClass, getClassMaxSize, LittleHeap, BigHeap>`(49f6e7a06ebc5aa8).() |
| 71 | : _memoryHeldcxx . . $ HL/SegHeap#_memoryHeld. (0), |
| 72 | _maxObjectSizecxx . . $ HL/SegHeap#_maxObjectSize. (getClassMaxSize(NumBins - 1)) |
| 73 | { |
| 74 | for (int i = 0; i <cxx . . $ std/`operator<`(39b98f4fe21ae589). NUM_ULONGScxx . . $ HL/SegHeap#NUM_ULONGS.; i++) { |
| 75 | binmapcxx . . $ HL/SegHeap#binmap.[i] = 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | inline size_t getMemoryHeldcxx . . $ HL/SegHeap#getMemoryHeld(fe4d2d262d4164a8).() const { |
| 80 | return _memoryHeldcxx . . $ HL/SegHeap#_memoryHeld.; |
| 81 | } |
| 82 | |
| 83 | size_t getSizecxx . . $ HL/SegHeap#getSize(751e5d26f8574c32). (void * ptr) { |
| 84 | return LittleHeap::getSize (ptr); |
| 85 | } |
| 86 | |
| 87 | inline void * malloccxx . . $ HL/SegHeap#malloc(bfcfa4d6b7f7ef64). (const size_t sz) { |
| 88 | void * ptr = nullptr; |
| 89 | if (sz > _maxObjectSizecxx . . $ HL/SegHeap#_maxObjectSize.) { |
| 90 | goto GET_MEMORY; |
| 91 | } |
| 92 | |
| 93 | { |
| 94 | const auto sc = getSizeClass(sz); |
| 95 | assertcxx . . $ `/usr/include/assert.h:50:10`! (sc >= 0); |
| 96 | assertcxx . . $ `/usr/include/assert.h:50:10`! (sc < NumBins); |
| 97 | auto idx = sc; |
| 98 | auto block = idx2blockcxx . . $ HL/SegHeap#idx2block(7864480464b09eea). (idx); |
| 99 | unsigned long map = binmapcxx . . $ HL/SegHeap#binmap.[block]; |
| 100 | unsigned long bit = idx2bitcxx . . $ HL/SegHeap#idx2bit(c104ebd7d68b2193). (idx); |
| 101 | |
| 102 | for (;;) { |
| 103 | if (bit > map || bit == 0) { |
| 104 | do { |
| 105 | if (++block >=cxx . . $ std/`operator>=`(39b98f4fe21ae589). NUM_ULONGScxx . . $ HL/SegHeap#NUM_ULONGS.) { |
| 106 | goto GET_MEMORY; |
| 107 | // return bigheap.malloc (sz); |
| 108 | } |
| 109 | } while ( (map = binmapcxx . . $ HL/SegHeap#binmap.[block]) ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). 0); |
| 110 | |
| 111 | idx = block <<cxx . . $ std/`operator<<`(f00c5788794c253e). SHIFTS_PER_ULONGcxx . . $ HL/SegHeap#SHIFTS_PER_ULONG.; |
| 112 | bit = 1; |
| 113 | } |
| 114 | |
| 115 | while ((bit & map) == 0) { |
| 116 | bit <<= 1; |
| 117 | assertcxx . . $ `/usr/include/assert.h:50:10`!(bit != 0); |
| 118 | idx++; |
| 119 | } |
| 120 | |
| 121 | assertcxx . . $ `/usr/include/assert.h:50:10`! (idx < NumBins); |
| 122 | ptr = myLittleHeapcxx . . $ HL/SegHeap#myLittleHeap.[idx].malloc (sz); |
| 123 | |
| 124 | if (ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 125 | binmapcxx . . $ HL/SegHeap#binmap.[block] = map &= ~bit; // Write through |
| 126 | idx++; |
| 127 | bit <<= 1; |
| 128 | } else { |
| 129 | _memoryHeldcxx . . $ HL/SegHeap#_memoryHeld. -= sz; |
| 130 | return ptr; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | GET_MEMORY: |
| 136 | if (ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 137 | // There was no free memory in any of the bins. |
| 138 | // Get some memory. |
| 139 | ptr = bigheapcxx . . $ HL/SegHeap#bigheap..malloc (sz); |
| 140 | } |
| 141 | |
| 142 | return ptr; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | inline void freecxx . . $ HL/SegHeap#free(5d22bdacc48458e8). (void * ptr) { |
| 147 | // printf ("Free: %x (%d bytes)\n", ptr, getSize(ptr)); |
| 148 | const auto objectSize = getSizecxx . . $ HL/SegHeap#getSize(751e5d26f8574c32).(ptr); // was bigheap.getSize(ptr) |
| 149 | if (objectSize >cxx . . $ std/`operator>`(39b98f4fe21ae589). _maxObjectSizecxx . . $ HL/SegHeap#_maxObjectSize.) { |
| 150 | bigheapcxx . . $ HL/SegHeap#bigheap..free (ptr); |
| 151 | } else { |
| 152 | auto objectSizeClass = getSizeClass(objectSize); |
| 153 | assertcxx . . $ `/usr/include/assert.h:50:10`! (objectSizeClass >= 0); |
| 154 | assertcxx . . $ `/usr/include/assert.h:50:10`! (objectSizeClass < NumBins); |
| 155 | // Put the freed object into the right sizeclass heap. |
| 156 | assertcxx . . $ `/usr/include/assert.h:50:10`! (getClassMaxSize(objectSizeClass) >= objectSize); |
| 157 | #if 1 |
| 158 | while (getClassMaxSize(objectSizeClass) >cxx . . $ std/`operator>`(39b98f4fe21ae589). objectSize) { |
| 159 | objectSizeClass--; |
| 160 | } |
| 161 | #endif |
| 162 | assertcxx . . $ `/usr/include/assert.h:50:10`! (getClassMaxSize(objectSizeClass) <= objectSize); |
| 163 | if (objectSizeClass >cxx . . $ std/`operator>`(39b98f4fe21ae589). 0) { |
| 164 | assertcxx . . $ `/usr/include/assert.h:50:10`! (objectSize >= getClassMaxSize(objectSizeClass - 1)); |
| 165 | } |
| 166 | |
| 167 | myLittleHeapcxx . . $ HL/SegHeap#myLittleHeap.[objectSizeClass].free (ptr); |
| 168 | mark_bincxx . . $ HL/SegHeap#mark_bin(d4f767463ce0a6b3). (objectSizeClass); |
| 169 | _memoryHeldcxx . . $ HL/SegHeap#_memoryHeld. += objectSize; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void clearcxx . . $ HL/SegHeap#clear(49f6e7a06ebc5aa8).() { |
| 175 | for (auto i = 0; i < NumBins; i++) { |
| 176 | myLittleHeapcxx . . $ HL/SegHeap#myLittleHeap.[i].clear(); |
| 177 | } |
| 178 | for (auto j = 0; j <cxx . . $ std/`operator<`(39b98f4fe21ae589). NUM_ULONGScxx . . $ HL/SegHeap#NUM_ULONGS.; j++) { |
| 179 | binmapcxx . . $ HL/SegHeap#binmap.[j] = 0; |
| 180 | } |
| 181 | bigheapcxx . . $ HL/SegHeap#bigheap..clear(); |
| 182 | _memoryHeldcxx . . $ HL/SegHeap#_memoryHeld. = 0; |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | |
| 187 | enumcxx . . $ HL/SegHeap#$anonymous_type_1# { BITS_PER_ULONGcxx . . $ HL/SegHeap#BITS_PER_ULONG. = sizeof(unsigned long) * 8 }; |
| 188 | enumcxx . . $ HL/SegHeap#$anonymous_type_2# { SHIFTS_PER_ULONGcxx . . $ HL/SegHeap#SHIFTS_PER_ULONG. = (BITS_PER_ULONGcxx . . $ HL/SegHeap#BITS_PER_ULONG. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). 32) ? 5 : 6 }; |
| 189 | enumcxx . . $ HL/SegHeap#$anonymous_type_3# { MAX_BITScxx . . $ HL/SegHeap#MAX_BITS. = (NumBins +cxx . . $ std/operator+(933aeee3a62e2e30). BITS_PER_ULONGcxx . . $ HL/SegHeap#BITS_PER_ULONG. -cxx . . $ std/operator-(a6e85813984128c7). 1) &cxx . . $ std/`operator&`(3fbb01592cd53f2a). ~(BITS_PER_ULONGcxx . . $ HL/SegHeap#BITS_PER_ULONG. -cxx . . $ std/operator-(a6e85813984128c7). 1) }; |
| 190 | |
| 191 | |
| 192 | private: |
| 193 | |
| 194 | static inline int idx2blockcxx . . $ HL/SegHeap#idx2block(7864480464b09eea). (int i) { |
| 195 | int blk = i >> SHIFTS_PER_ULONGcxx . . $ HL/SegHeap#SHIFTS_PER_ULONG.; |
| 196 | assertcxx . . $ `/usr/include/assert.h:50:10`! (blk < NUM_ULONGS); |
| 197 | assertcxx . . $ `/usr/include/assert.h:50:10`! (blk >= 0); |
| 198 | return blk; |
| 199 | } |
| 200 | |
| 201 | static inline unsigned long idx2bitcxx . . $ HL/SegHeap#idx2bit(c104ebd7d68b2193). (int i) { |
| 202 | unsigned long bit = ((1U <<cxx . . $ std/`operator<<`(f00c5788794c253e). ((i) &cxx . . $ std/`operator&`(3fbb01592cd53f2a). ((1U <<cxx . . $ std/`operator<<`(f00c5788794c253e). SHIFTS_PER_ULONGcxx . . $ HL/SegHeap#SHIFTS_PER_ULONG.)-cxx . . $ std/operator-(a6e85813984128c7).1)))); |
| 203 | return bit; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | protected: |
| 208 | |
| 209 | BigHeap bigheapcxx . . $ HL/SegHeap#bigheap.; |
| 210 | |
| 211 | enumcxx . . $ HL/SegHeap#$anonymous_type_4# { NUM_ULONGScxx . . $ HL/SegHeap#NUM_ULONGS. = MAX_BITScxx . . $ HL/SegHeap#MAX_BITS. / BITS_PER_ULONGcxx . . $ HL/SegHeap#BITS_PER_ULONG. }; |
| 212 | unsigned long binmapcxx . . $ HL/SegHeap#binmap.[NUM_ULONGScxx . . $ HL/SegHeap#NUM_ULONGS.]; |
| 213 | |
| 214 | inline int get_binmapcxx . . $ HL/SegHeap#get_binmap(7ccb52b1bfe01045). (int i) const { |
| 215 | return binmapcxx . . $ HL/SegHeap#binmap.[i >> SHIFTS_PER_ULONGcxx . . $ HL/SegHeap#SHIFTS_PER_ULONG.] &cxx . . $ std/`operator&`(3fbb01592cd53f2a). idx2bitcxx . . $ HL/SegHeap#idx2bit(c104ebd7d68b2193).(i); |
| 216 | } |
| 217 | |
| 218 | inline void mark_bincxx . . $ HL/SegHeap#mark_bin(d4f767463ce0a6b3). (int i) { |
| 219 | binmapcxx . . $ HL/SegHeap#binmap.[i >> SHIFTS_PER_ULONGcxx . . $ HL/SegHeap#SHIFTS_PER_ULONG.] |= idx2bitcxx . . $ HL/SegHeap#idx2bit(c104ebd7d68b2193).(i); |
| 220 | } |
| 221 | |
| 222 | inline void unmark_bincxx . . $ HL/SegHeap#unmark_bin(d4f767463ce0a6b3). (int i) { |
| 223 | binmapcxx . . $ HL/SegHeap#binmap.[i >> SHIFTS_PER_ULONGcxx . . $ HL/SegHeap#SHIFTS_PER_ULONG.] &= ~(idx2bitcxx . . $ HL/SegHeap#idx2bit(c104ebd7d68b2193).(i)); |
| 224 | } |
| 225 | |
| 226 | size_t _memoryHeldcxx . . $ HL/SegHeap#_memoryHeld.; |
| 227 | |
| 228 | const size_t _maxObjectSizecxx . . $ HL/SegHeap#_maxObjectSize.; |
| 229 | |
| 230 | // The little heaps. |
| 231 | LittleHeap myLittleHeapcxx . . $ HL/SegHeap#myLittleHeap.[NumBins]; |
| 232 | |
| 233 | }; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | |
| 238 | #endif |