| 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 strictsegheap.h |
| 20 | * @brief Definition of StrictSegHeap. |
| 21 | */ |
| 22 | |
| 23 | #ifndef HL_STRICTSEGHEAP_H |
| 24 | #define HL_STRICTSEGHEAP_H |
| 25 | |
| 26 | #include "segheap.h" |
| 27 | |
| 28 | /** |
| 29 | * @class StrictSegHeap |
| 30 | * @brief A "strict" segregated-fits collection of (homogeneous) heaps. |
| 31 | * |
| 32 | * One extra heap is used for objects that are "too big". Unlike |
| 33 | * SegHeap, StrictSegHeap does not perform splitting to satisfy memory |
| 34 | * requests. If no memory is available from the appropriate bin, |
| 35 | * malloc returns NULL. |
| 36 | * |
| 37 | * @sa SegHeap |
| 38 | * |
| 39 | * @param NumBins The number of bins (subheaps). |
| 40 | * @param getSizeClass Function to compute size class from size. |
| 41 | * @param getClassMaxSize Function to compute the largest size for a given size class. |
| 42 | * @param LittleHeap The subheap class. |
| 43 | * @param BigHeap The parent class, used for "big" objects. |
| 44 | */ |
| 45 | |
| 46 | namespace HL { |
| 47 | |
| 48 | template <int NumBins, |
| 49 | int (*size2class) (const size_t), |
| 50 | size_t (*class2size) (const int), |
| 51 | class LittleHeap, |
| 52 | class BigHeap> |
| 53 | class StrictSegHeap : |
| 54 | public SegHeap<NumBins, size2class, class2size, LittleHeap, BigHeap> |
| 55 | { |
| 56 | private: |
| 57 | |
| 58 | typedef SegHeap<NumBins, size2class, class2size, LittleHeap, BigHeap> SuperHeap; |
| 59 | |
| 60 | public: |
| 61 | |
| 62 | static constexpr size_t num_size_classes = NumBins; |
| 63 | int get_size_class(const size_t sz) const { return size2class(sz); } |
| 64 | size_t get_class_size(int i) const { return class2size(i); } |
| 65 | |
| 66 | void clear () { |
| 67 | for (auto i = 0; i < NumBins; i++) { |
| 68 | const size_t sz = class2size(i); |
| 69 | void * ptr; |
| 70 | while ((ptr = SuperHeap::myLittleHeap[i].malloc (sz)) != NULL) { |
| 71 | SuperHeap::bigheap.free (ptr); |
| 72 | } |
| 73 | } |
| 74 | for (auto j = 0; j < SuperHeap::NUM_ULONGS; j++) { |
| 75 | SuperHeap::binmap[j] = 0; |
| 76 | } |
| 77 | SuperHeap::_memoryHeld = 0; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Malloc from exactly one available size. |
| 82 | * (don't look in every small heap, as in SegHeap). |
| 83 | */ |
| 84 | |
| 85 | inline void * malloc (const size_t sz) { |
| 86 | void * ptr = nullptr; |
| 87 | const auto sizeClass = size2class(sz); |
| 88 | const auto realSize = class2size(sizeClass); |
| 89 | |
| 90 | assert (realSize >= sz); |
| 91 | |
| 92 | if (realSize <= SuperHeap::_maxObjectSize) { |
| 93 | assert (sizeClass >= 0); |
| 94 | assert (sizeClass < NumBins); |
| 95 | ptr = SuperHeap::myLittleHeap[sizeClass].malloc (realSize); |
| 96 | } |
| 97 | if (!ptr) { |
| 98 | ptr = SuperHeap::bigheap.malloc (realSize); |
| 99 | } |
| 100 | return ptr; |
| 101 | } |
| 102 | |
| 103 | inline void free (void * ptr) { |
| 104 | const auto objectSize = SuperHeap::getSize(ptr); |
| 105 | free(ptr, objectSize); |
| 106 | } |
| 107 | |
| 108 | inline void free(void * ptr, size_t objectSize) { |
| 109 | if (objectSize > SuperHeap::_maxObjectSize) { |
| 110 | SuperHeap::bigheap.free (ptr); |
| 111 | } else { |
| 112 | auto objectSizeClass = size2class(objectSize); |
| 113 | assert (objectSizeClass >= 0); |
| 114 | assert (objectSizeClass < NumBins); |
| 115 | assert (class2size(objectSizeClass) >= objectSize); |
| 116 | |
| 117 | // Put the freed object into the right sizeclass heap. |
| 118 | |
| 119 | // Ensure that the bin that we are going to put it in is for |
| 120 | // objects that are no bigger than the actual size of the |
| 121 | // object. Ensure we don't run forever. |
| 122 | |
| 123 | while ((objectSizeClass > 0) && |
| 124 | (class2size(objectSizeClass) > objectSize)) { |
| 125 | objectSizeClass--; |
| 126 | } |
| 127 | // Leak objects if something went wrong. |
| 128 | if (class2size(objectSizeClass) >= objectSize) { |
| 129 | SuperHeap::myLittleHeap[objectSizeClass].free (ptr); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | }; |
| 135 | |
| 136 | } |
| 137 | |
| 138 | #endif |
| 139 |