| 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_XALLOCHEAP_H |
| 19 | #define HL_XALLOCHEAP_H |
| 20 | |
| 21 | #include <cstddef> |
| 22 | |
| 23 | #include "utility/align.h" |
| 24 | |
| 25 | namespace HL { |
| 26 | |
| 27 | template <int ArenaSize, class SuperHeap> |
| 28 | class XallocHeap : public SuperHeap { |
| 29 | |
| 30 | public: |
| 31 | |
| 32 | inline XallocHeap() { |
| 33 | start_of_array = (char *) SuperHeap::malloc (ArenaSize); |
| 34 | end_of_array = start_of_array + HL::align<sizeof(double)>(sizeof(Nuggie)); |
| 35 | size_lval(end_of_array) = 0; |
| 36 | last_block = nullptr; |
| 37 | } |
| 38 | |
| 39 | inline ~XallocHeap() { |
| 40 | SuperHeap::free (start_of_array); |
| 41 | } |
| 42 | |
| 43 | inline void * malloc (size_t size) { |
| 44 | char * old_end_of_array = end_of_array; |
| 45 | end_of_array += HL::align<sizeof(double)>(size + sizeof(Nuggie)); |
| 46 | if (old_end_of_array + size >= start_of_array + ArenaSize) { |
| 47 | // We're out of memory. |
| 48 | return nullptr; |
| 49 | } |
| 50 | size_lval(end_of_array) = end_of_array - old_end_of_array; |
| 51 | clear_use(end_of_array); /* this is not necessary, cause it will be zero */ |
| 52 | set_use(old_end_of_array); |
| 53 | last_block = old_end_of_array; |
| 54 | return old_end_of_array; |
| 55 | } |
| 56 | |
| 57 | inline void free (void * ptr) { |
| 58 | char * p = (char *) ptr; |
| 59 | char * q; |
| 60 | /* At this point we could check that the in_use bit for this block |
| 61 | is set, and also check that the size is right */ |
| 62 | clear_use(p); /* mark this block as unused */ |
| 63 | if (p == last_block) { |
| 64 | while (1) { |
| 65 | q = prev_block(p); |
| 66 | if (q == p) { |
| 67 | last_block = nullptr; |
| 68 | end_of_array = p; |
| 69 | break; /* only happens when we get to the beginning */ |
| 70 | } |
| 71 | if (in_use(q)) { |
| 72 | last_block = q; |
| 73 | end_of_array = p; |
| 74 | break; |
| 75 | } |
| 76 | p = q; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | |
| 83 | static inline size_t& size_lval (char * x) { |
| 84 | return (((Nuggie *)(((char *)x) - sizeof(Nuggie)))->size); |
| 85 | } |
| 86 | |
| 87 | static inline char * prev_block (char * x) { |
| 88 | return (((char *) x) - (size_lval(x) & (~1))); |
| 89 | } |
| 90 | |
| 91 | static inline int in_use (char * x) { |
| 92 | return (size_lval(x) & (1)); |
| 93 | } |
| 94 | |
| 95 | static inline void set_use (char * x) { |
| 96 | (size_lval(x) |= (1)); |
| 97 | } |
| 98 | |
| 99 | static inline void clear_use (char * x) { |
| 100 | (size_lval(x) &= (~1)); |
| 101 | } |
| 102 | |
| 103 | class Nuggie { |
| 104 | public: |
| 105 | size_t size; |
| 106 | }; |
| 107 | |
| 108 | char * end_of_array; |
| 109 | char * start_of_array; |
| 110 | char * last_block; |
| 111 | }; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | #endif |