#ifndef _BLOCK_H_
#define _BLOCK_H_
#include "config.h"
#include "os/support/Debug.h"
namespace BPrivate {
class superblock;
class block {
public:
block(superblock * sb)
:
#if HEAP_DEBUG
_magic(FREE_BLOCK_MAGIC),
#endif
_next(NULL), _mySuperblock(sb)
{
}
block &
operator=(const block & b)
{
#if HEAP_DEBUG
_magic = b._magic;
#endif
_next = b._next;
_mySuperblock = b._mySuperblock;
#if HEAP_FRAG_STATS
_requestedSize = b._requestedSize;
#endif
return *this;
}
enum {
ALLOCATED_BLOCK_MAGIC = 0xcafecafe,
FREE_BLOCK_MAGIC = 0xbabebabe
};
inline void markFree(void);
inline void markAllocated(void);
inline const int isValid(void) const;
inline superblock *getSuperblock(void);
#if HEAP_FRAG_STATS
void
setRequestedSize(size_t s)
{
_requestedSize = s;
}
size_t
getRequestedSize(void)
{
return _requestedSize;
}
#endif
#if USE_PRIVATE_HEAPS
void
setActualSize(size_t s)
{
_actualSize = s;
}
size_t
getActualSize(void)
{
return _actualSize;
}
#endif
void
setNext(block * b)
{
_next = b;
}
block *
getNext(void)
{
return _next;
}
#if HEAP_LEAK_CHECK
void
setCallStack(int index, void *address)
{
_callStack[index] = address;
}
void *
getCallStack(int index)
{
return _callStack[index];
}
void
setAllocatedSize(size_t size)
{
_allocatedSize = size;
}
size_t
getAllocatedSize()
{
return _allocatedSize;
}
#endif
private:
#if USE_PRIVATE_HEAPS
#if HEAP_DEBUG
union {
unsigned long _magic;
double _d1;
};
#endif
block *_next;
size_t _actualSize;
union {
double _d2;
superblock *_mySuperblock;
};
#else
#if HEAP_DEBUG
union {
unsigned long _magic;
double _d3;
};
#endif
block *_next;
superblock *_mySuperblock;
#if defined(__i386__) && (__GNUC__ > 2)
double _d5;
#endif
#endif
#if HEAP_LEAK_CHECK
void *_callStack[HEAP_CALL_STACK_SIZE];
size_t _allocatedSize;
#endif
#if HEAP_FRAG_STATS
union {
double _d4;
size_t _requestedSize;
};
#endif
block(const block &);
};
#if __GNUC__ > 2
STATIC_ASSERT(sizeof(block) % HAIKU_MEMORY_ALIGNMENT == 0);
#endif
superblock *
block::getSuperblock(void)
{
#if HEAP_DEBUG
assert(isValid());
#endif
return _mySuperblock;
}
void
block::markFree(void)
{
#if HEAP_DEBUG
assert(_magic == ALLOCATED_BLOCK_MAGIC);
_magic = FREE_BLOCK_MAGIC;
#endif
}
void
block::markAllocated(void)
{
#if HEAP_DEBUG
assert(_magic == FREE_BLOCK_MAGIC);
_magic = ALLOCATED_BLOCK_MAGIC;
#endif
}
const int
block::isValid(void) const
{
#if HEAP_DEBUG
return _magic == FREE_BLOCK_MAGIC
|| _magic == ALLOCATED_BLOCK_MAGIC;
#else
return 1;
#endif
}
}
#endif