30#include "kallocator.h"
33class TDEZoneAllocator::MemBlock
36 MemBlock(
size_t s) : size(s), ref(0), older(0), newer(0)
37 { begin =
new char[s]; }
38 ~MemBlock() {
delete [] begin; }
39 bool is_in(
void *ptr)
const {
return !(begin > (
char *)ptr
40 || (begin + size) <= (
char *)ptr); }
49: currentBlock(0), blockSize(1), blockOffset(0), log2(0), num_blocks(0),
50 hashList(0), hashSize(0), hashDirty(true)
61 unsigned int count = 0;
65 for (
unsigned int i = 0; i <
hashSize; i++)
79 tqDebug(
"zone still contained %d blocks", count);
83void TDEZoneAllocator::insertHash(MemBlock *b)
85 unsigned long adr = ((
unsigned long)b->begin) & (~(
blockSize - 1));
86 unsigned long end = ((
unsigned long)b->begin) +
blockSize;
88 unsigned long key = adr >>
log2;
91 hashList[key] =
new TQValueList<MemBlock *>;
125 for (
unsigned int i = 0; i <
hashSize; i++)
153 unsigned long adr = ((
unsigned long)b->begin) & (~(
blockSize - 1));
154 unsigned long end = ((
unsigned long)b->begin) +
blockSize;
156 unsigned long key = adr >>
log2;
159 TQValueList<MemBlock *> *list =
hashList[key];
160 TQValueList<MemBlock *>::Iterator it = list->begin();
161 TQValueList<MemBlock *>::Iterator endit = list->end();
162 for (; it != endit; ++it)
172 b->older->newer = b->newer;
174 b->newer->older = b->older;
187 const size_t alignment =
sizeof(
void *) - 1;
188 _size = (_size + alignment) & ~alignment;
193 tqDebug(
"TDEZoneAllocator: allocating more than %lu bytes",
blockSize);
212 unsigned long key = (((
unsigned long)ptr) >>
log2) & (
hashSize - 1);
213 TQValueList<MemBlock *> *list =
hashList[key];
220 TQValueList<MemBlock*>::ConstIterator it = list->begin();
221 TQValueList<MemBlock*>::ConstIterator endit = list->end();
222 for (; it != endit; ++it) {
224 if (cur->is_in(ptr)) {
248 unsigned int removed = 0;
void deallocate(void *ptr)
Gives back a block returned by allocate() to the zone allocator, and possibly deallocates the block h...
void addBlock(MemBlock *b)
Add a new memory block to the pool of blocks, and reorganize the hash lists if needed.
unsigned int log2
base-2 log of the block size.
MemList ** hashList
Collection of lists of blocks, for lookups.
unsigned long blockSize
Store block size from constructor.
unsigned int num_blocks
Count total number of allocated blocks.
unsigned long blockOffset
Store offset into current block; size-offset is free.
TDEZoneAllocator(unsigned long _blockSize=8 *1024)
Creates a TDEZoneAllocator object.
void free_since(void *ptr)
Deallocate many objects at once.
MemBlock * currentBlock
One block is 'current' to satisfy requests.
bool hashDirty
Flag the hashes as in need of reorganization.
unsigned int hashSize
Count of hashes.
void delBlock(MemBlock *b)
Delete a memory block.
void initHash()
Reinitialize hash list.
~TDEZoneAllocator()
Destructs the ZoneAllocator and free all memory allocated by it.
void * allocate(size_t _size)
Allocates a memory block.