| 1 | // -*- C++ -*-cxx . . $ `<file>/vendor/Heap-Layers/heaps/special/bumpalloc.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 | #ifndef HL_BUMPALLOC_H |
| 19 | #define HL_BUMPALLOC_Hcxx . . $ `vendor/Heap-Layers/heaps/special/bumpalloc.h:19:9`! |
| 20 | |
| 21 | #include <cstddef> |
| 22 | |
| 23 | #include "utility/gcd.h"cxx . . $ `<file>/vendor/Heap-Layers/utility/gcd.h`/ |
| 24 | |
| 25 | #if defined(__clang__cxx . . $ `<built-in>:3:9`!) |
| 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 HLcxx . . $ HL/ { |
| 37 | |
| 38 | template <size_t ChunkSize, |
| 39 | class SuperHeap, |
| 40 | size_t Alignment_ = 1UL> |
| 41 | class BumpAlloccxx . . $ HL/BumpAlloc# : public SuperHeap { |
| 42 | public: |
| 43 | |
| 44 | enumcxx . . $ HL/BumpAlloc#$anonymous_type_0# { Alignmentcxx . . $ HL/BumpAlloc#Alignment. = Alignment_ }; |
| 45 | |
| 46 | BumpAlloccxx . . $ HL/BumpAlloc#`BumpAlloc<ChunkSize, SuperHeap, Alignment_>`(49f6e7a06ebc5aa8).() |
| 47 | : _bumpcxx . . $ HL/BumpAlloc#_bump. (nullptr), |
| 48 | _remainingcxx . . $ HL/BumpAlloc#_remaining. (0) |
| 49 | { |
| 50 | static_assert((int) gcdcxx . . $ HL/gcd#<ChunkSize, Alignment>::VALUE ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). Alignmentcxx . . $ HL/BumpAlloc#Alignment., |
| 51 | "Alignment must be satisfiable."); |
| 52 | static_assert((int) gcdcxx . . $ HL/gcd#<SuperHeap::Alignment, Alignment>::VALUE ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). Alignmentcxx . . $ HL/BumpAlloc#Alignment., |
| 53 | "Alignment must be compatible with the SuperHeap's alignment."); |
| 54 | static_assert((Alignmentcxx . . $ HL/BumpAlloc#Alignment. &cxx . . $ std/`operator&`(3fbb01592cd53f2a). (Alignmentcxx . . $ HL/BumpAlloc#Alignment.-cxx . . $ std/operator-(a6e85813984128c7).1)) ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). 0, |
| 55 | "Alignment must be a power of two."); |
| 56 | } |
| 57 | |
| 58 | inline void * malloccxx . . $ HL/BumpAlloc#malloc(bfcfa4d6b7f7ef64). (size_t sz) { |
| 59 | // Round up the size if necessary. |
| 60 | size_t newSize = (sz +cxx . . $ std/operator+(933aeee3a62e2e30). Alignmentcxx . . $ HL/BumpAlloc#Alignment. -cxx . . $ std/operator-(a6e85813984128c7). 1UL) &cxx . . $ std/`operator&`(3fbb01592cd53f2a). ~(Alignmentcxx . . $ HL/BumpAlloc#Alignment. -cxx . . $ std/operator-(a6e85813984128c7). 1UL); |
| 61 | |
| 62 | // If there's not enough space left to fulfill this request, get |
| 63 | // another chunk. |
| 64 | if (_remainingcxx . . $ HL/BumpAlloc#_remaining. < newSize) { |
| 65 | refillcxx . . $ HL/BumpAlloc#refill(2c44cd906ae4e829).(newSize); |
| 66 | } |
| 67 | // Bump that pointer. |
| 68 | char * old = _bumpcxx . . $ HL/BumpAlloc#_bump.; |
| 69 | _bumpcxx . . $ HL/BumpAlloc#_bump. += newSize; |
| 70 | _remainingcxx . . $ HL/BumpAlloc#_remaining. -= newSize; |
| 71 | |
| 72 | assertcxx . . $ `/usr/include/assert.h:50:10`! ((size_t) old % Alignment == 0); |
| 73 | return old; |
| 74 | } |
| 75 | |
| 76 | /// Free is disabled (we only bump, never reclaim). |
| 77 | inline bool freecxx . . $ HL/BumpAlloc#free(db00c6a08f501612). (void *) { return false; } |
| 78 | |
| 79 | private: |
| 80 | |
| 81 | /// The bump pointer. |
| 82 | char * _bumpcxx . . $ HL/BumpAlloc#_bump.; |
| 83 | |
| 84 | /// How much space remains in the current chunk. |
| 85 | size_t _remainingcxx . . $ HL/BumpAlloc#_remaining.; |
| 86 | |
| 87 | // Get another chunk. |
| 88 | void refillcxx . . $ HL/BumpAlloc#refill(2c44cd906ae4e829). (size_t sz) { |
| 89 | if (sz < ChunkSize) { |
| 90 | sz = ChunkSize; |
| 91 | } |
| 92 | _bumpcxx . . $ HL/BumpAlloc#_bump. = (char *) SuperHeap::malloc (sz); |
| 93 | assertcxx . . $ `/usr/include/assert.h:50:10`! ((size_t) _bump % Alignment == 0); |
| 94 | _remainingcxx . . $ HL/BumpAlloc#_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 BumpHeapcxx . . $ HL/BumpHeap# = BumpAlloccxx . . $ HL/BumpAlloc#<Size, X>; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | #if defined(__clang__cxx . . $ `<built-in>:3:9`!) |
| 106 | #pragma clang diagnostic pop |
| 107 | #endif |
| 108 | |
| 109 | #endif |