| 1 | /* -*- C++ -*- */cxx . . $ `<file>/vendor/Heap-Layers/heaps/special/xallocheap.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_XALLOCHEAP_H |
| 19 | #define HL_XALLOCHEAP_Hcxx . . $ `vendor/Heap-Layers/heaps/special/xallocheap.h:19:9`! |
| 20 | |
| 21 | #include <cstddef> |
| 22 | |
| 23 | #include "utility/align.h"cxx . . $ `<file>/vendor/Heap-Layers/utility/align.h`/ |
| 24 | |
| 25 | namespace HLcxx . . $ HL/ { |
| 26 | |
| 27 | template <int ArenaSize, class SuperHeap> |
| 28 | class XallocHeapcxx . . $ HL/XallocHeap# : public SuperHeap { |
| 29 | |
| 30 | public: |
| 31 | |
| 32 | inline XallocHeapcxx . . $ HL/XallocHeap#`XallocHeap<ArenaSize, SuperHeap>`(49f6e7a06ebc5aa8).() { |
| 33 | start_of_arraycxx . . $ HL/XallocHeap#start_of_array. = (char *) SuperHeap::malloc (ArenaSize); |
| 34 | end_of_arraycxx . . $ HL/XallocHeap#end_of_array. = start_of_arraycxx . . $ HL/XallocHeap#start_of_array. + HLcxx . . $ HL/::aligncxx . . $ HL/align(ecdaa1632f49f591).<sizeof(double)>(sizeof(Nuggiecxx . . $ HL/XallocHeap#Nuggie#)); |
| 35 | size_lvalcxx . . $ HL/XallocHeap#size_lval(b086cf61ceba9bfd).(end_of_arraycxx . . $ HL/XallocHeap#end_of_array.) = 0; |
| 36 | last_blockcxx . . $ HL/XallocHeap#last_block. = nullptr; |
| 37 | } |
| 38 | |
| 39 | inline ~cxx . . $ HL/XallocHeap#`~XallocHeap<ArenaSize, SuperHeap>`(49f6e7a06ebc5aa8).XallocHeap() { |
| 40 | SuperHeap::free (start_of_arraycxx . . $ HL/XallocHeap#start_of_array.); |
| 41 | } |
| 42 | |
| 43 | inline void * malloccxx . . $ HL/XallocHeap#malloc(bfcfa4d6b7f7ef64). (size_t size) { |
| 44 | char * old_end_of_array = end_of_arraycxx . . $ HL/XallocHeap#end_of_array.; |
| 45 | end_of_arraycxx . . $ HL/XallocHeap#end_of_array. += HLcxx . . $ HL/::aligncxx . . $ HL/align(ecdaa1632f49f591).<sizeof(double)>(size + sizeof(Nuggiecxx . . $ HL/XallocHeap#Nuggie#)); |
| 46 | if (old_end_of_array + size >= start_of_arraycxx . . $ HL/XallocHeap#start_of_array. + ArenaSize) { |
| 47 | // We're out of memory. |
| 48 | return nullptr; |
| 49 | } |
| 50 | size_lvalcxx . . $ HL/XallocHeap#size_lval(b086cf61ceba9bfd).(end_of_arraycxx . . $ HL/XallocHeap#end_of_array.) = end_of_arraycxx . . $ HL/XallocHeap#end_of_array. - old_end_of_array; |
| 51 | clear_usecxx . . $ HL/XallocHeap#clear_use(fda57c8427c60377).(end_of_arraycxx . . $ HL/XallocHeap#end_of_array.); /* this is not necessary, cause it will be zero */ |
| 52 | set_usecxx . . $ HL/XallocHeap#set_use(fda57c8427c60377).(old_end_of_array); |
| 53 | last_blockcxx . . $ HL/XallocHeap#last_block. = old_end_of_array; |
| 54 | return old_end_of_array; |
| 55 | } |
| 56 | |
| 57 | inline void freecxx . . $ HL/XallocHeap#free(5d22bdacc48458e8). (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_usecxx . . $ HL/XallocHeap#clear_use(fda57c8427c60377).(p); /* mark this block as unused */ |
| 63 | if (p == last_blockcxx . . $ HL/XallocHeap#last_block.) { |
| 64 | while (1) { |
| 65 | q = prev_blockcxx . . $ HL/XallocHeap#prev_block(7a9722d75b371431).(p); |
| 66 | if (q == p) { |
| 67 | last_blockcxx . . $ HL/XallocHeap#last_block. = nullptr; |
| 68 | end_of_arraycxx . . $ HL/XallocHeap#end_of_array. = p; |
| 69 | break; /* only happens when we get to the beginning */ |
| 70 | } |
| 71 | if (in_usecxx . . $ HL/XallocHeap#in_use(1ef8e6ec083aabb9).(q)) { |
| 72 | last_blockcxx . . $ HL/XallocHeap#last_block. = q; |
| 73 | end_of_arraycxx . . $ HL/XallocHeap#end_of_array. = p; |
| 74 | break; |
| 75 | } |
| 76 | p = q; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | |
| 83 | static inline size_t& size_lvalcxx . . $ HL/XallocHeap#size_lval(b086cf61ceba9bfd). (char * x) { |
| 84 | return (((Nuggiecxx . . $ HL/XallocHeap#Nuggie# *)(((char *)x) - sizeof(Nuggiecxx . . $ HL/XallocHeap#Nuggie#)))->sizecxx . . $ HL/XallocHeap#Nuggie#size.); |
| 85 | } |
| 86 | |
| 87 | static inline char * prev_blockcxx . . $ HL/XallocHeap#prev_block(7a9722d75b371431). (char * x) { |
| 88 | return (((char *) x) - (size_lvalcxx . . $ HL/XallocHeap#size_lval(b086cf61ceba9bfd).(x) & (~1))); |
| 89 | } |
| 90 | |
| 91 | static inline int in_usecxx . . $ HL/XallocHeap#in_use(1ef8e6ec083aabb9). (char * x) { |
| 92 | return (size_lvalcxx . . $ HL/XallocHeap#size_lval(b086cf61ceba9bfd).(x) & (1)); |
| 93 | } |
| 94 | |
| 95 | static inline void set_usecxx . . $ HL/XallocHeap#set_use(fda57c8427c60377). (char * x) { |
| 96 | (size_lvalcxx . . $ HL/XallocHeap#size_lval(b086cf61ceba9bfd).(x) |= (1)); |
| 97 | } |
| 98 | |
| 99 | static inline void clear_usecxx . . $ HL/XallocHeap#clear_use(fda57c8427c60377). (char * x) { |
| 100 | (size_lvalcxx . . $ HL/XallocHeap#size_lval(b086cf61ceba9bfd).(x) &= (~1)); |
| 101 | } |
| 102 | |
| 103 | class Nuggiecxx . . $ HL/XallocHeap#Nuggie# { |
| 104 | public: |
| 105 | size_t sizecxx . . $ HL/XallocHeap#Nuggie#size.; |
| 106 | }; |
| 107 | |
| 108 | char * end_of_arraycxx . . $ HL/XallocHeap#end_of_array.; |
| 109 | char * start_of_arraycxx . . $ HL/XallocHeap#start_of_array.; |
| 110 | char * last_blockcxx . . $ HL/XallocHeap#last_block.; |
| 111 | }; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | #endif |