| 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 | #pragma once |
| 19 | |
| 20 | #ifndef HL_STATICBUFFERHEAP_H |
| 21 | #define HL_STATICBUFFERHEAP_H |
| 22 | |
| 23 | namespace HL { |
| 24 | |
| 25 | /** |
| 26 | * Heap that satisfies all requests out of static buffer. |
| 27 | */ |
| 28 | template <int BufferSize> |
| 29 | class StaticBufferHeap { |
| 30 | public: |
| 31 | StaticBufferHeap() {} |
| 32 | |
| 33 | enum { Alignment = alignof(std::max_align_t) }; |
| 34 | |
| 35 | void *malloc(size_t sz) { |
| 36 | auto oldAllocated = allocated(); |
| 37 | auto prevPtr = _bufPtr; |
| 38 | if (sz == 0) { |
| 39 | sz = Alignment; |
| 40 | } |
| 41 | sz = sz + (Alignment - (sz % Alignment)); |
| 42 | if (allocated() + sizeof(Header) + sz > BufferSize) { |
| 43 | return nullptr; |
| 44 | } |
| 45 | _bufPtr += sz + sizeof(Header); |
| 46 | new (prevPtr) Header(sz); |
| 47 | auto ptr = (Header *)prevPtr + 1; |
| 48 | assert(sz <= getSize(ptr)); |
| 49 | assert(isValid(ptr)); |
| 50 | assert(allocated() == oldAllocated + sz + sizeof(Header)); |
| 51 | #if 0 |
| 52 | tprintf::tprintf("allocated @ sz = @\n", |
| 53 | sz, |
| 54 | (void *) ptr); |
| 55 | #endif |
| 56 | return ptr; |
| 57 | } |
| 58 | |
| 59 | void *memalign(size_t alignment, size_t sz) { |
| 60 | if (sz == 0 || !isPowerOf2(alignment)) { |
| 61 | return nullptr; |
| 62 | } |
| 63 | |
| 64 | // Ensure we're not breaking this heap's 'Alignment'. |
| 65 | alignment = std::max(alignment, (size_t)Alignment); |
| 66 | |
| 67 | // Note that if we're already aligned, we'll still skip 'alignment' bytes: |
| 68 | // we can't skip 0 bytes as we need space for 'Header' |
| 69 | uintptr_t skip = alignment - ((uintptr_t)_bufPtr % alignment); |
| 70 | while (skip < sizeof(Header)) { |
| 71 | skip += alignment; |
| 72 | } |
| 73 | skip -= sizeof(Header); // requires 'sizeof(Header) % Alignment == 0' |
| 74 | |
| 75 | assert((skip % Alignment) == 0); |
| 76 | |
| 77 | _bufPtr += skip; |
| 78 | |
| 79 | if (void* p = malloc(sz)) { |
| 80 | return p; |
| 81 | } |
| 82 | |
| 83 | _bufPtr -= skip; |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | void free(void *) {} |
| 88 | |
| 89 | size_t getSize(void *ptr) { |
| 90 | if (isValid(ptr)) { |
| 91 | auto sz = ((Header *)ptr - 1)->size; |
| 92 | // tprintf::tprintf("size of @ = @\n", ptr, sz); |
| 93 | return sz; |
| 94 | } else { |
| 95 | return 0; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | bool isValid(void *ptr) { |
| 100 | if ((uintptr_t)ptr >= (uintptr_t)_buf) { |
| 101 | if ((uintptr_t)ptr < (uintptr_t)_buf + BufferSize) { |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | size_t allocated() const { return (uintptr_t)_bufPtr - (uintptr_t)_buf; } |
| 109 | |
| 110 | private: |
| 111 | class Header { |
| 112 | public: |
| 113 | Header(size_t sz) : size(sz) {} |
| 114 | alignas(Alignment) size_t size; |
| 115 | }; |
| 116 | |
| 117 | inline bool isPowerOf2(size_t n) { |
| 118 | return n && !(n & (n-1)); |
| 119 | } |
| 120 | |
| 121 | alignas(Alignment) char _buf[BufferSize]; |
| 122 | char *_bufPtr{_buf}; |
| 123 | }; |
| 124 | |
| 125 | } // namespace |
| 126 | |
| 127 | #endif |