1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _ADDHEAP_H_ |
---|
4 | #define _ADDHEAP_H_ |
---|
5 | |
---|
6 | /* |
---|
7 | |
---|
8 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
9 | |
---|
10 | Copyright (C) 2000-2003 by Emery Berger |
---|
11 | http://www.cs.umass.edu/~emery |
---|
12 | emery@cs.umass.edu |
---|
13 | |
---|
14 | This program is free software; you can redistribute it and/or modify |
---|
15 | it under the terms of the GNU General Public License as published by |
---|
16 | the Free Software Foundation; either version 2 of the License, or |
---|
17 | (at your option) any later version. |
---|
18 | |
---|
19 | This program is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | GNU General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
27 | |
---|
28 | */ |
---|
29 | |
---|
30 | // Reserve space for a class in the head of every allocated object. |
---|
31 | |
---|
32 | #include <assert.h> |
---|
33 | |
---|
34 | namespace HL { |
---|
35 | |
---|
36 | template <class Add, class Super> |
---|
37 | class AddHeap : public Super { |
---|
38 | public: |
---|
39 | |
---|
40 | inline void * malloc (size_t sz) { |
---|
41 | void * ptr = Super::malloc (sz + align(sizeof(Add))); |
---|
42 | void * newPtr = (void *) align ((size_t) ((Add *) ptr + 1)); |
---|
43 | return ptr; |
---|
44 | } |
---|
45 | |
---|
46 | inline void free (void * ptr) { |
---|
47 | void * origPtr = (void *) ((Add *) ptr - 1); |
---|
48 | Super::free (origPtr); |
---|
49 | } |
---|
50 | |
---|
51 | private: |
---|
52 | static inline size_t align (size_t sz) { |
---|
53 | return (sz + (sizeof(double) - 1)) & ~(sizeof(double) - 1); |
---|
54 | } |
---|
55 | |
---|
56 | }; |
---|
57 | |
---|
58 | }; |
---|
59 | #endif |
---|