headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / malloc / block.h
blob4d60cc1254bbe5c691ccd44913a79be13541de26
1 ///-*-C++-*-//////////////////////////////////////////////////////////////////
2 //
3 // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
4 // for Shared-Memory Multiprocessors
5 // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
6 //
7 // Copyright (c) 1998-2000, The University of Texas at Austin.
8 //
9 // This library is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU Library General Public License as
11 // published by the Free Software Foundation, http://www.fsf.org.
13 // This library is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 //////////////////////////////////////////////////////////////////////////////
20 #ifndef _BLOCK_H_
21 #define _BLOCK_H_
23 #include "config.h"
25 //#include <assert.h>
27 namespace BPrivate {
29 class superblock;
31 class block {
32 public:
33 block(superblock * sb)
35 #if HEAP_DEBUG
36 _magic(FREE_BLOCK_MAGIC),
37 #endif
38 _next(NULL), _mySuperblock(sb)
42 block &
43 operator=(const block & b)
45 #if HEAP_DEBUG
46 _magic = b._magic;
47 #endif
48 _next = b._next;
49 _mySuperblock = b._mySuperblock;
50 #if HEAP_FRAG_STATS
51 _requestedSize = b._requestedSize;
52 #endif
53 return *this;
56 enum {
57 ALLOCATED_BLOCK_MAGIC = 0xcafecafe,
58 FREE_BLOCK_MAGIC = 0xbabebabe
61 // Mark this block as free.
62 inline void markFree(void);
64 // Mark this block as allocated.
65 inline void markAllocated(void);
67 // Is this block valid? (i.e.,
68 // does it have the right magic number?)
69 inline const int isValid(void) const;
71 // Return the block's superblock pointer.
72 inline superblock *getSuperblock(void);
74 #if HEAP_FRAG_STATS
75 void
76 setRequestedSize(size_t s)
78 _requestedSize = s;
81 size_t
82 getRequestedSize(void)
84 return _requestedSize;
86 #endif
88 #if USE_PRIVATE_HEAPS
89 void
90 setActualSize(size_t s)
92 _actualSize = s;
95 size_t
96 getActualSize(void)
98 return _actualSize;
100 #endif
101 void
102 setNext(block * b)
104 _next = b;
107 block *
108 getNext(void)
110 return _next;
113 #if HEAP_LEAK_CHECK
114 void
115 setCallStack(int index, void *address)
117 _callStack[index] = address;
120 void *
121 getCallStack(int index)
123 return _callStack[index];
126 void
127 setAllocatedSize(size_t size)
129 _allocatedSize = size;
132 size_t
133 getAllocatedSize()
135 return _allocatedSize;
137 #endif
139 private:
140 #if USE_PRIVATE_HEAPS
141 #if HEAP_DEBUG
142 union {
143 unsigned long _magic;
144 double _d1; // For alignment.
146 #endif
148 block *_next; // The next block in a linked-list of blocks.
149 size_t _actualSize; // The actual size of the block.
151 union {
152 double _d2; // For alignment.
153 superblock *_mySuperblock; // A pointer to my superblock.
155 #else // ! USE_PRIVATE_HEAPS
157 #if HEAP_DEBUG
158 union {
159 unsigned long _magic;
160 double _d3; // For alignment.
162 #endif
164 block *_next; // The next block in a linked-list of blocks.
165 superblock *_mySuperblock; // A pointer to my superblock.
166 #endif // USE_PRIVATE_HEAPS
168 #if HEAP_LEAK_CHECK
169 void *_callStack[HEAP_CALL_STACK_SIZE];
170 size_t _allocatedSize;
171 #endif
173 #if HEAP_FRAG_STATS
174 union {
175 double _d4; // This is just for alignment purposes.
176 size_t _requestedSize; // The amount of space requested (vs. allocated).
178 #endif
180 // Disable copying.
181 block(const block &);
185 superblock *
186 block::getSuperblock(void)
188 #if HEAP_DEBUG
189 assert(isValid());
190 #endif
192 return _mySuperblock;
196 void
197 block::markFree(void)
199 #if HEAP_DEBUG
200 assert(_magic == ALLOCATED_BLOCK_MAGIC);
201 _magic = FREE_BLOCK_MAGIC;
202 #endif
206 void
207 block::markAllocated(void)
209 #if HEAP_DEBUG
210 assert(_magic == FREE_BLOCK_MAGIC);
211 _magic = ALLOCATED_BLOCK_MAGIC;
212 #endif
216 const int
217 block::isValid(void) const
219 #if HEAP_DEBUG
220 return _magic == FREE_BLOCK_MAGIC
221 || _magic == ALLOCATED_BLOCK_MAGIC;
222 #else
223 return 1;
224 #endif
227 } // namespace BPrivate
229 #endif // _BLOCK_H_