[176] | 1 | // -*- C++ -*- |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | |
---|
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
| 6 | |
---|
| 7 | Copyright (C) 2000-2003 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 | /* |
---|
| 28 | |
---|
| 29 | StaticHeap: manage a fixed range of memory. |
---|
| 30 | |
---|
| 31 | */ |
---|
| 32 | |
---|
| 33 | #ifndef _STATICHEAP_H_ |
---|
| 34 | #define _STATICHEAP_H_ |
---|
| 35 | |
---|
| 36 | namespace HL { |
---|
| 37 | |
---|
| 38 | template <int MemorySize> |
---|
| 39 | class StaticHeap { |
---|
| 40 | public: |
---|
| 41 | |
---|
| 42 | StaticHeap (void) |
---|
| 43 | : ptr (&buf[0]), |
---|
| 44 | remaining (MemorySize) |
---|
| 45 | {} |
---|
| 46 | |
---|
| 47 | enum { Alignment = 1 }; |
---|
| 48 | |
---|
| 49 | inline void * malloc (size_t sz) { |
---|
| 50 | if (remaining < sz) { |
---|
| 51 | return NULL; |
---|
| 52 | } |
---|
| 53 | void * p = ptr; |
---|
| 54 | ptr += sz; |
---|
| 55 | remaining -= sz; |
---|
| 56 | return p; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void free (void *) {} |
---|
| 60 | int remove (void *) { return 0; } |
---|
| 61 | |
---|
| 62 | int isValid (void * ptr) { |
---|
| 63 | return (((size_t) ptr >= (size_t) buf) && |
---|
| 64 | ((size_t) ptr < (size_t) buf)); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | private: |
---|
| 68 | |
---|
| 69 | // Disable copying and assignment. |
---|
| 70 | StaticHeap (const StaticHeap& treap); |
---|
| 71 | StaticHeap& operator= (const StaticHeap& treap); |
---|
| 72 | |
---|
| 73 | char buf[MemorySize]; |
---|
| 74 | char * ptr; |
---|
| 75 | size_t remaining; |
---|
| 76 | }; |
---|
| 77 | |
---|
| 78 | }; |
---|
| 79 | |
---|
| 80 | #endif |
---|