[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | |
---|
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
| 6 | |
---|
| 7 | Copyright (C) 2000-2005 by Emery Berger |
---|
| 8 | http://www.cs.umass.edu/~emery |
---|
| 9 | emery@cs.umass.edu |
---|
| 10 | |
---|
| 11 | This program is free software; you can redistribute it and/or modify |
---|
| 12 | it under the terms of the GNU General Public License as published by |
---|
| 13 | the Free Software Foundation; either version 2 of the License, or |
---|
| 14 | (at your option) any later version. |
---|
| 15 | |
---|
| 16 | This program is distributed in the hope that it will be useful, |
---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 19 | GNU General Public License for more details. |
---|
| 20 | |
---|
| 21 | You should have received a copy of the GNU General Public License |
---|
| 22 | along with this program; if not, write to the Free Software |
---|
| 23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #ifndef _XALLOCHEAP_H_ |
---|
| 28 | #define _XALLOCHEAP_H_ |
---|
| 29 | |
---|
| 30 | namespace HL { |
---|
| 31 | |
---|
| 32 | template <int ArenaSize, class SuperHeap> |
---|
| 33 | class XallocHeap : public SuperHeap { |
---|
| 34 | |
---|
| 35 | public: |
---|
| 36 | |
---|
| 37 | inline XallocHeap (void) { |
---|
| 38 | start_of_array = (char *) SuperHeap::malloc (ArenaSize); |
---|
| 39 | end_of_array = start_of_array + align(sizeof(Nuggie)); |
---|
| 40 | size_lval(end_of_array) = 0; |
---|
| 41 | last_block = NULL; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | inline ~XallocHeap (void) { |
---|
| 45 | SuperHeap::free (start_of_array); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | inline void * malloc (size_t size) { |
---|
| 49 | char * old_end_of_array = end_of_array; |
---|
| 50 | end_of_array += align(size + sizeof(Nuggie)); |
---|
| 51 | if (old_end_of_array + size >= start_of_array + ArenaSize) { |
---|
| 52 | // We're out of memory. |
---|
| 53 | return NULL; |
---|
| 54 | } |
---|
| 55 | size_lval(end_of_array) = end_of_array - old_end_of_array; |
---|
| 56 | clear_use(end_of_array); /* this is not necessary, cause it will be zero */ |
---|
| 57 | set_use(old_end_of_array); |
---|
| 58 | last_block = old_end_of_array; |
---|
| 59 | return old_end_of_array; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | inline void free (void * ptr) { |
---|
| 63 | char * p = (char *) ptr; |
---|
| 64 | char * q; |
---|
| 65 | /* At this point we could check that the in_use bit for this block |
---|
| 66 | is set, and also check that the size is right */ |
---|
| 67 | clear_use(p); /* mark this block as unused */ |
---|
| 68 | if (p == last_block) { |
---|
| 69 | while (1) { |
---|
| 70 | q = prev_block(p); |
---|
| 71 | if (q == p) { |
---|
| 72 | last_block = NULL; |
---|
| 73 | end_of_array = p; |
---|
| 74 | break; /* only happens when we get to the beginning */ |
---|
| 75 | } |
---|
| 76 | if (in_use(q)) { |
---|
| 77 | last_block = q; |
---|
| 78 | end_of_array = p; |
---|
| 79 | break; |
---|
| 80 | } |
---|
| 81 | p = q; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | private: |
---|
| 87 | |
---|
| 88 | static inline size_t& size_lval (char * x) { |
---|
| 89 | return (((Nuggie *)(((char *)x) - sizeof(Nuggie)))->size); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | static inline char * prev_block (char * x) { |
---|
| 93 | return (((char *) x) - (size_lval(x) & (~1))); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | static inline int in_use (char * x) { |
---|
| 97 | return (size_lval(x) & (1)); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | static inline void set_use (char * x) { |
---|
| 101 | (size_lval(x) |= (1)); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | static inline void clear_use (char * x) { |
---|
| 105 | (size_lval(x) &= (~1)); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | class Nuggie { |
---|
| 109 | public: |
---|
| 110 | size_t size; |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | inline static size_t align (int sz) { |
---|
| 114 | return (sz + (sizeof(double) - 1)) & ~(sizeof(double) - 1); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | char * end_of_array; |
---|
| 118 | char * start_of_array; |
---|
| 119 | char * last_block; |
---|
| 120 | }; |
---|
| 121 | |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | #endif |
---|