| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | #ifndef HL_ADDHEAP_H |
| 4 | #define HL_ADDHEAP_H |
| 5 | |
| 6 | /* |
| 7 | |
| 8 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 9 | |
| 10 | Copyright (C) 2000-2020 by Emery Berger |
| 11 | http://www.emeryberger.com |
| 12 | [email protected] |
| 13 | |
| 14 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 15 | |
| 16 | You may obtain a copy of the License at |
| 17 | http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | |
| 19 | */ |
| 20 | |
| 21 | // Reserve space for a class in the head of every allocated object. |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include "utility/lcm.h" |
| 25 | |
| 26 | namespace HL { |
| 27 | |
| 28 | template <class Add, class SuperHeap> |
| 29 | class AddHeap : public SuperHeap { |
| 30 | public: |
| 31 | |
| 32 | inline void * malloc (size_t sz) { |
| 33 | void * ptr = SuperHeap::malloc (sz + HeaderSize); |
| 34 | void * newPtr = (char *) ptr + HeaderSize; |
| 35 | return newPtr; |
| 36 | } |
| 37 | |
| 38 | inline void free (void * ptr) { |
| 39 | SuperHeap::free (getOriginal(ptr)); |
| 40 | } |
| 41 | |
| 42 | inline size_t getSize (void * ptr) { |
| 43 | return SuperHeap::getSize (getOriginal(ptr)); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | |
| 48 | inline void * getOriginal (void * ptr) { |
| 49 | void * origPtr = (void *) ((char *) ptr - HeaderSize); |
| 50 | return origPtr; |
| 51 | } |
| 52 | |
| 53 | // A size that preserves existing alignment restrictions. |
| 54 | // Beware: can seriously increase size requirements. |
| 55 | enum { HeaderSize = lcm<(int) SuperHeap::Alignment, sizeof(Add)>::value }; |
| 56 | |
| 57 | }; |
| 58 | |
| 59 | } |
| 60 | #endif |