| 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 | #ifndef HL_BUMPALLOC_H |
| 19 | #define HL_BUMPALLOC_H |
| 20 | |
| 21 | #include <cstddef> |
| 22 | |
| 23 | #include "utility/gcd.h" |
| 24 | |
| 25 | #if defined(__clang__) |
| 26 | #pragma clang diagnostic push |
| 27 | #pragma clang diagnostic ignored "-Wunused-variable" |
| 28 | #endif |
| 29 | |
| 30 | /** |
| 31 | * @class BumpAlloc |
| 32 | * @brief Obtains memory in chunks and bumps a pointer through the chunks. |
| 33 | * @author Emery Berger <http://www.cs.umass.edu/~emery> |
| 34 | */ |
| 35 | |
| 36 | namespace HL { |
| 37 | |
| 38 | template <size_t ChunkSize, |
| 39 | class SuperHeap, |
| 40 | size_t Alignment_ = 1UL> |
| 41 | class BumpAlloc : public SuperHeap { |
| 42 | public: |
| 43 | |
| 44 | enum { Alignment = Alignment_ }; |
| 45 | |
| 46 | BumpAlloc() |
| 47 | : _bump (nullptr), |
| 48 | _remaining (0) |
| 49 | { |
| 50 | static_assert((int) gcd<ChunkSize, Alignment>::VALUE == Alignment, |
| 51 | "Alignment must be satisfiable."); |
| 52 | static_assert((int) gcd<SuperHeap::Alignment, Alignment>::VALUE == Alignment, |
| 53 | "Alignment must be compatible with the SuperHeap's alignment."); |
| 54 | static_assert((Alignment & (Alignment-1)) == 0, |
| 55 | "Alignment must be a power of two."); |
| 56 | } |
| 57 | |
| 58 | inline void * malloc (size_t sz) { |
| 59 | // Round up the size if necessary. |
| 60 | size_t newSize = (sz + Alignment - 1UL) & ~(Alignment - 1UL); |
| 61 | |
| 62 | // If there's not enough space left to fulfill this request, get |
| 63 | // another chunk. |
| 64 | if (_remaining < newSize) { |
| 65 | refill(newSize); |
| 66 | } |
| 67 | // Bump that pointer. |
| 68 | char * old = _bump; |
| 69 | _bump += newSize; |
| 70 | _remaining -= newSize; |
| 71 | |
| 72 | assert ((size_t) old % Alignment == 0); |
| 73 | return old; |
| 74 | } |
| 75 | |
| 76 | /// Free is disabled (we only bump, never reclaim). |
| 77 | inline bool free (void *) { return false; } |
| 78 | |
| 79 | private: |
| 80 | |
| 81 | /// The bump pointer. |
| 82 | char * _bump; |
| 83 | |
| 84 | /// How much space remains in the current chunk. |
| 85 | size_t _remaining; |
| 86 | |
| 87 | // Get another chunk. |
| 88 | void refill (size_t sz) { |
| 89 | if (sz < ChunkSize) { |
| 90 | sz = ChunkSize; |
| 91 | } |
| 92 | _bump = (char *) SuperHeap::malloc (sz); |
| 93 | assert ((size_t) _bump % Alignment == 0); |
| 94 | _remaining = sz; |
| 95 | } |
| 96 | |
| 97 | }; |
| 98 | |
| 99 | // We are going to rename this layer to BumpHeap and deprecate BumpAlloc eventually. |
| 100 | template <size_t Size, class X> |
| 101 | using BumpHeap = BumpAlloc<Size, X>; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | #if defined(__clang__) |
| 106 | #pragma clang diagnostic pop |
| 107 | #endif |
| 108 | |
| 109 | #endif |