17#include "kzoneallocator_p.h"
19#include <kcompletion_debug.h>
25class KZoneAllocator::MemBlock
40 MemBlock(
const MemBlock &) =
delete;
41 MemBlock &operator=(
const MemBlock &) =
delete;
42 bool is_in(
void *ptr)
const
44 return !(begin > (
char *)ptr || (begin + size) <= (
char *)ptr);
53class KZoneAllocator::Private
57 : currentBlock(nullptr)
69 MemBlock *currentBlock;
77 unsigned int num_blocks;
81 unsigned int hashSize;
86KZoneAllocator::KZoneAllocator(
unsigned long _blockSize)
89 while (d->blockSize < _blockSize) {
96 d->blockOffset = d->blockSize + 1;
99KZoneAllocator::~KZoneAllocator()
101 unsigned int count = 0;
105 for (
unsigned int i = 0; i < d->hashSize; i++) {
106 delete d->hashList[i];
108 delete[] d->hashList;
109 d->hashList =
nullptr;
112 for (; d->currentBlock; d->currentBlock =
next) {
113 next = d->currentBlock->older;
114 delete d->currentBlock;
120 fprintf(stderr,
"zone still contained %u blocks", count);
126void KZoneAllocator::insertHash(MemBlock *b)
128 quintptr adr = ((quintptr)b->begin) & (~(d->blockSize - 1));
129 quintptr
end = ((quintptr)b->begin) + d->blockSize;
131 quintptr key = adr >> d->log2;
132 key = key & (d->hashSize - 1);
133 if (!d->hashList[key]) {
136 d->hashList[key]->append(b);
146void KZoneAllocator::addBlock(MemBlock *b)
149 b->older = d->currentBlock;
150 if (d->currentBlock) {
158 if (d->hashList && ((d->num_blocks / 4) > d->hashSize && d->hashSize < 64 * 1024)) {
163 if (d->hashList && !d->hashDirty) {
169void KZoneAllocator::initHash()
172 for (
unsigned int i = 0; i < d->hashSize; i++) {
173 delete d->hashList[i];
175 delete[] d->hashList;
176 d->hashList =
nullptr;
179 while (d->hashSize < d->num_blocks) {
182 if (d->hashSize < 1024) {
185 if (d->hashSize > 64 * 1024) {
186 d->hashSize = 64 * 1024;
190 d->hashDirty =
false;
191 for (MemBlock *b = d->currentBlock; b; b = b->older) {
200void KZoneAllocator::delBlock(MemBlock *b)
204 if (d->hashList && !d->hashDirty) {
205 quintptr adr = ((quintptr)b->begin) & (~(d->blockSize - 1));
206 quintptr
end = ((quintptr)b->begin) + d->blockSize;
208 quintptr key = adr >> d->log2;
209 key = key & (d->hashSize - 1);
210 if (d->hashList[key]) {
214 for (; it != endit; ++it) {
225 b->older->newer = b->newer;
228 b->newer->older = b->older;
230 if (b == d->currentBlock) {
231 d->currentBlock =
nullptr;
232 d->blockOffset = d->blockSize;
238void *KZoneAllocator::allocate(
size_t _size)
241 const size_t alignment =
sizeof(
void *) - 1;
242 _size = (_size + alignment) & ~alignment;
244 if ((
unsigned long)_size + d->blockOffset > d->blockSize) {
245 if (_size > d->blockSize) {
246 qCDebug(KCOMPLETION_LOG,
"KZoneAllocator: allocating more than %zu bytes", (
size_t)d->blockSize);
249 addBlock(
new MemBlock(d->blockSize));
253 void *result = (
void *)(d->currentBlock->begin + d->blockOffset);
254 d->currentBlock->ref++;
255 d->blockOffset += _size;
259void KZoneAllocator::deallocate(
void *ptr)
265 quintptr key = (((quintptr)ptr) >> d->log2) & (d->hashSize - 1);
275 for (; it != endit; ++it) {
277 if (cur->is_in(ptr)) {
279 if (cur != d->currentBlock) {
293void KZoneAllocator::free_since(
void *ptr)
298 if (d->hashList && !d->hashDirty) {
300 unsigned int removed = 0;
301 for (b = d->currentBlock; b; b = b->older, removed++) {
306 if (d->hashSize >= 4 * (d->num_blocks - removed)) {
310 while (d->currentBlock && !d->currentBlock->is_in(ptr)) {
311 d->currentBlock = d->currentBlock->older;
312 delBlock(d->currentBlock->newer);
314 d->blockOffset = ((
char *)ptr) - d->currentBlock->begin;
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & next()
const QList< QKeySequence > & end()
iterator erase(const_iterator begin, const_iterator end)