26 #define CHUNK_SIZE 100000
30 struct SpaceChunk* next;
45 struct SpaceChunk* chunk = _first, *next;
53 if (0) qDebug(
"~FixPool: Had %d objects with total size %d\n",
59 if (!ensureSpace(size))
return 0;
62 void* result = _last->space + _last->used;
73 if (!ensureSpace(size))
return 0;
76 return _last->space + _last->used;
82 if (_reservation < size)
return false;
93 bool FixPool::ensureSpace(
unsigned int size)
95 if (_last && _last->used + size <=
CHUNK_SIZE)
return true;
97 struct SpaceChunk* newChunk;
102 newChunk = (
struct SpaceChunk*) malloc(
sizeof(
struct SpaceChunk) +
105 qFatal(
"ERROR: Out of memory. Sorry. KCachegrind has to terminate.\n\n"
106 "You probably tried to load a profile data file too huge for"
107 "this system. You could try loading this file on a 64-bit OS.");
114 _last = _first = newChunk;
117 _last->next = newChunk;
146 size = (size+3) & ~3;
153 if (!ensureSpace(size + 12))
return false;
155 char** obj = (
char**) (_data+_used);
156 obj[0] = (
char*)(_data + _used + size + 8);
158 *(
int*)(_data+_used+size+8) = 0;
159 *ptr = _data+_used+8;
170 (*(
char**)(*ptr - 4)) != (
char*)ptr )
171 qFatal(
"Chaining error in DynPool::free");
173 (*(
char**)(*ptr - 4)) = 0;
177 bool DynPool::ensureSpace(
unsigned int size)
179 if (_used + size <= _size)
return true;
181 unsigned int newsize = _size *3/2 +
CHUNK_SIZE;
182 char* newdata = (
char*) malloc(newsize);
184 unsigned int freed = 0, len;
185 char **p, **pnext, **pnew;
187 qDebug(
"DynPool::ensureSpace size: %d => %d, used %d. %p => %p",
188 _size, newsize, _used, _data, newdata);
190 pnew = (
char**) newdata;
194 len = (
char*)pnext - (
char*)p;
196 if (0) qDebug(
" [%8p] Len %d (ptr %p), freed %d (=> %p)",
197 p, len, p[1], freed, pnew);
213 pnew[0] = (
char*)pnew + len;
215 memcpy((
char*)pnew + 8, (
char*)p + 8, len-8);
218 char** ptr = (
char**) p[1];
219 if (*ptr != ((
char*)p)+8)
220 qFatal(
"Chaining error in DynPool::ensureSpace");
221 *ptr = ((
char*)pnew)+8;
223 pnew = (
char**) pnew[0];
228 unsigned int newused = (
char*)pnew - (
char*)newdata;
229 qDebug(
"DynPool::ensureSpace size: %d => %d, used %d => %d (%d freed)",
230 _size, newsize, _used, newused, freed);
bool allocateReserved(unsigned int size)
Before calling this, you have to reserve at least bytes with reserveSpace().
void * reserve(unsigned int size)
Reserve space.
bool allocate(char **ptr, unsigned int size)
Take bytes from the pool, changing <*ptr> to point to this allocated space.
void free(char **ptr)
To resize, first allocate new space, and free old afterwards.
void * allocate(unsigned int size)
Take bytes from the pool.