1 | // -*- C++ -*- |
---|
2 | |
---|
3 | #ifndef _HOARDSUPERBLOCK_H_ |
---|
4 | #define _HOARDSUPERBLOCK_H_ |
---|
5 | |
---|
6 | #include <assert.h> |
---|
7 | #include <stdlib.h> |
---|
8 | |
---|
9 | #include "hldefines.h" |
---|
10 | #include "freesllist.h" |
---|
11 | #include "hoardsuperblockheader.h" |
---|
12 | |
---|
13 | namespace Hoard { |
---|
14 | |
---|
15 | template <class LockType, |
---|
16 | int SuperblockSize, |
---|
17 | typename HeapType> |
---|
18 | class HoardSuperblock { |
---|
19 | public: |
---|
20 | |
---|
21 | HoardSuperblock (size_t sz) |
---|
22 | : _header (sz, BufferSize) |
---|
23 | { |
---|
24 | assert (_header.isValid()); |
---|
25 | } |
---|
26 | |
---|
27 | /// @brief Find the start of the superblock by bitmasking. |
---|
28 | /// @note All superblocks <em>must</em> be naturally aligned, and powers of two. |
---|
29 | static inline HoardSuperblock * getSuperblock (void * ptr) { |
---|
30 | return (HoardSuperblock *) |
---|
31 | (((size_t) ptr) & ~((size_t) SuperblockSize-1)); |
---|
32 | } |
---|
33 | |
---|
34 | INLINE size_t getSize (void * ptr) const { |
---|
35 | assert (_header.isValid()); |
---|
36 | if (inRange (ptr)) { |
---|
37 | return _header.getSize (ptr); |
---|
38 | } else { |
---|
39 | return 0; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | INLINE size_t getObjectSize (void) const { |
---|
45 | return _header.getObjectSize(); |
---|
46 | } |
---|
47 | |
---|
48 | MALLOC_FUNCTION INLINE void * malloc (size_t sz) { |
---|
49 | sz = sz; // avoid warning |
---|
50 | assert (_header.isValid()); |
---|
51 | void * ptr = _header.malloc(); |
---|
52 | if (ptr) { |
---|
53 | assert (inRange (ptr)); |
---|
54 | } |
---|
55 | return ptr; |
---|
56 | } |
---|
57 | |
---|
58 | INLINE void free (void * ptr) { |
---|
59 | assert (_header.isValid()); |
---|
60 | if (inRange (ptr)) { |
---|
61 | // Pointer is in range. |
---|
62 | _header.free (ptr); |
---|
63 | } else { |
---|
64 | // Invalid free. |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | void clear (void) { |
---|
69 | _header.clear(); |
---|
70 | } |
---|
71 | |
---|
72 | // ----- below here are non-conventional heap methods ----- // |
---|
73 | |
---|
74 | INLINE bool isValidSuperblock (void) const { |
---|
75 | assert (_header.isValid()); |
---|
76 | bool b = _header.isValid(); |
---|
77 | return b; |
---|
78 | } |
---|
79 | |
---|
80 | INLINE int getTotalObjects (void) const { |
---|
81 | assert (_header.isValid()); |
---|
82 | return _header.getTotalObjects(); |
---|
83 | } |
---|
84 | |
---|
85 | /// Return the number of free objects in this superblock. |
---|
86 | INLINE int getObjectsFree (void) const { |
---|
87 | assert (_header.isValid()); |
---|
88 | assert (_header.getObjectsFree() >= 0); |
---|
89 | assert (_header.getObjectsFree() <= _header.getTotalObjects()); |
---|
90 | return _header.getObjectsFree(); |
---|
91 | } |
---|
92 | |
---|
93 | inline void lock (void) { |
---|
94 | assert (_header.isValid()); |
---|
95 | _header.lock(); |
---|
96 | } |
---|
97 | |
---|
98 | inline void unlock (void) { |
---|
99 | assert (_header.isValid()); |
---|
100 | _header.unlock(); |
---|
101 | } |
---|
102 | |
---|
103 | inline HeapType * getOwner (void) const { |
---|
104 | assert (_header.isValid()); |
---|
105 | return _header.getOwner(); |
---|
106 | } |
---|
107 | |
---|
108 | inline void setOwner (HeapType * o) { |
---|
109 | assert (_header.isValid()); |
---|
110 | assert (o != NULL); |
---|
111 | _header.setOwner (o); |
---|
112 | } |
---|
113 | |
---|
114 | inline HoardSuperblock * getNext (void) const { |
---|
115 | assert (_header.isValid()); |
---|
116 | return _header.getNext(); |
---|
117 | } |
---|
118 | |
---|
119 | inline HoardSuperblock * getPrev (void) const { |
---|
120 | assert (_header.isValid()); |
---|
121 | return _header.getPrev(); |
---|
122 | } |
---|
123 | |
---|
124 | inline void setNext (HoardSuperblock * f) { |
---|
125 | assert (_header.isValid()); |
---|
126 | assert (f != this); |
---|
127 | _header.setNext (f); |
---|
128 | } |
---|
129 | |
---|
130 | inline void setPrev (HoardSuperblock * f) { |
---|
131 | assert (_header.isValid()); |
---|
132 | assert (f != this); |
---|
133 | _header.setPrev (f); |
---|
134 | } |
---|
135 | |
---|
136 | INLINE bool inRange (void * ptr) const { |
---|
137 | // Returns true iff the pointer is valid. |
---|
138 | const size_t ptrValue = (size_t) ptr; |
---|
139 | return ((ptrValue >= (size_t) _buf) && |
---|
140 | (ptrValue <= (size_t) &_buf[BufferSize])); |
---|
141 | } |
---|
142 | |
---|
143 | INLINE void * normalize (void * ptr) const { |
---|
144 | void * ptr2 = _header.normalize (ptr); |
---|
145 | assert (inRange (ptr)); |
---|
146 | assert (inRange (ptr2)); |
---|
147 | return ptr2; |
---|
148 | } |
---|
149 | |
---|
150 | typedef Hoard::HoardSuperblockHeader<LockType, SuperblockSize, HeapType> Header; |
---|
151 | |
---|
152 | private: |
---|
153 | |
---|
154 | |
---|
155 | // Disable copying and assignment. |
---|
156 | |
---|
157 | HoardSuperblock (const HoardSuperblock&); |
---|
158 | HoardSuperblock& operator=(const HoardSuperblock&); |
---|
159 | |
---|
160 | enum { BufferSize = SuperblockSize - sizeof(Header) }; |
---|
161 | |
---|
162 | /// The metadata. |
---|
163 | Header _header; |
---|
164 | |
---|
165 | |
---|
166 | /// The actual buffer. MUST immediately follow the header! |
---|
167 | char _buf[BufferSize]; |
---|
168 | }; |
---|
169 | |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | #endif |
---|