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 | #ifndef _SIZEOWNERHEAP_H_ |
---|
28 | #define _SIZEOWNERHEAP_H_ |
---|
29 | |
---|
30 | #include <assert.h> |
---|
31 | |
---|
32 | #include "addheap.h" |
---|
33 | |
---|
34 | /** |
---|
35 | * @class SizeOwnerHeap |
---|
36 | * @brief Adds object size and owner heap information. |
---|
37 | */ |
---|
38 | |
---|
39 | namespace HL { |
---|
40 | |
---|
41 | template <class Heap> |
---|
42 | class SizeOwner { |
---|
43 | public: |
---|
44 | union { |
---|
45 | struct { |
---|
46 | size_t size; |
---|
47 | Heap * owner; |
---|
48 | } s; |
---|
49 | double dummy; |
---|
50 | }; |
---|
51 | }; |
---|
52 | |
---|
53 | template <class Super> |
---|
54 | class SizeOwnerHeap : public AddHeap<SizeOwner<Super>, Super> { |
---|
55 | private: |
---|
56 | |
---|
57 | typedef AddHeap<SizeOwner<Super>, Super> SuperHeap; |
---|
58 | |
---|
59 | public: |
---|
60 | |
---|
61 | inline void * malloc (size_t sz) { |
---|
62 | void * ptr = SuperHeap::malloc (sz); |
---|
63 | // Store the requested size. |
---|
64 | SizeOwner<Super> * so = (SizeOwner<Super> *) ptr; |
---|
65 | so->s.size = sz; |
---|
66 | so->s.owner = this; |
---|
67 | // Store the owner. |
---|
68 | return (void *) (so + 1); |
---|
69 | } |
---|
70 | |
---|
71 | inline void free (void * ptr) { |
---|
72 | void * origPtr = (void *) ((SizeOwner<Super> *) ptr - 1); |
---|
73 | SuperHeap::free (origPtr); |
---|
74 | } |
---|
75 | |
---|
76 | static inline Super * owner (void * ptr) { |
---|
77 | SizeOwner<Super> * so = (SizeOwner<Super> *) ptr - 1; |
---|
78 | return so->s.owner; |
---|
79 | } |
---|
80 | |
---|
81 | static inline size_t size (void * ptr) { |
---|
82 | SizeOwner<Super> * so = (SizeOwner<Super> *) ptr - 1; |
---|
83 | return so->s.size; |
---|
84 | } |
---|
85 | }; |
---|
86 | |
---|
87 | }; |
---|
88 | |
---|
89 | #endif |
---|