2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #ifndef PLATFORM_WINDOWS
26 #include "pbd/reallocpool.h"
28 #if defined RAP_WITH_SEGMENT_STATS || defined RAP_WITH_CALL_STATS
32 #ifdef RAP_WITH_SEGMENT_STATS
33 #define STATS_segment collect_segment_stats();
38 #ifdef RAP_WITH_CALL_STATS
39 #define STATS_inc(VAR) ++VAR;
40 #define STATS_if(COND, VAR) if (COND) {++VAR;}
41 #define STATS_used(DELTA) { _cur_used += (DELTA); if (_cur_used > _max_used) { _max_used = _cur_used; } }
43 #define STATS_inc(VAR)
44 #define STATS_if(COND, VAR)
45 #define STATS_used(DELTA)
48 #ifdef RAP_WITH_HISTOGRAM
49 #define STATS_hist(VAR, SIZE) ++VAR[hist_bin(SIZE)];
51 #define STATS_hist(VAR, SIZE)
56 typedef int poolsize_t;
58 ReallocPool::ReallocPool (std::string name, size_t bytes)
62 #ifdef RAP_WITH_SEGMENT_STATS
71 #ifdef RAP_WITH_CALL_STATS
82 _pool = (char*) ::malloc (bytes);
84 memset (_pool, 0, bytes); // make resident
85 #ifndef PLATFORM_WINDOWS
89 poolsize_t *in = (poolsize_t*) _pool;
90 *in = - (bytes - sizeof (poolsize_t));
93 #ifdef RAP_WITH_HISTOGRAM
94 for (int i = 0; i < RAP_WITH_HISTOGRAM; ++i) {
95 _hist_alloc[i] = _hist_free[i] = _hist_grow[i] = _hist_shrink[i] = 0;
100 ReallocPool::~ReallocPool ()
108 // realloc() does it all, malloc(), realloc() and free()
110 ReallocPool::_realloc (void *ptr, size_t oldsize, size_t newsize) {
113 if (ptr == 0 && newsize == 0) {
119 rv = _malloc (newsize);
120 STATS_if (!rv, _n_oom);
122 STATS_hist(_hist_alloc, newsize);
128 STATS_hist(_hist_free, _asize(ptr));
135 if (newsize == oldsize) {
140 if (newsize > oldsize) {
142 const size_t ns = (newsize + RAP_BLOCKSIZE) & (~RAP_BLOCKSIZE);
143 if (ns <= _asize(ptr)) {
148 if ((rv = _malloc (newsize))) {
149 memcpy (rv, ptr, oldsize);
152 STATS_if(!rv, _n_oom);
154 STATS_hist(_hist_grow, newsize);
159 if (newsize < oldsize) {
161 if ((rv = _malloc (newsize))) {
162 memccpy (rv, ptr, newsize);
164 STATS_if(!rv, _n_oom);
166 #elif 1 // shrink current segment
167 const size_t ns = (newsize + RAP_BLOCKSIZE) & (~RAP_BLOCKSIZE);
173 STATS_inc(_n_shrink);
174 STATS_hist (_hist_shrink, newsize);
178 return NULL; // not reached
181 #define SEGSIZ (*((poolsize_t*) p))
184 ReallocPool::consolidate_ptr (char *p) {
185 const poolsize_t sop = sizeof(poolsize_t);
186 if (p - SEGSIZ + sop >= _pool + _poolsize) {
187 return; // reached end
189 poolsize_t *next = (poolsize_t*)(p - SEGSIZ + sop);
191 SEGSIZ = SEGSIZ + (*next) - sop;
192 if (p - SEGSIZ + sop >= _pool + _poolsize) {
195 next = (poolsize_t*)(p -SEGSIZ + sop);
201 ReallocPool::_malloc (size_t s) {
202 const poolsize_t sop = sizeof(poolsize_t);
203 size_t traversed = 0;
207 s = (s + RAP_BLOCKSIZE) & (~RAP_BLOCKSIZE); // optional, helps to reduce fragmentation
210 while (1) { // iterates at most once over the available pool
212 traversed += SEGSIZ + sop;
213 if (traversed >= _poolsize) {
214 return NULL; // reached last segment. OOM.
217 if (p == _pool + _poolsize) {
222 // found free segment.
223 const poolsize_t avail = -SEGSIZ;
224 const poolsize_t sp = (poolsize_t)s;
225 const poolsize_t ss = sop + s;
235 // segment is larger than required space,
236 // (we need to fit data + two non-zero poolsize_t)
237 SEGSIZ = sp; // mark area as used.
238 *((poolsize_t*)(p + ss)) = ss - avail; // mark free space after.
239 consolidate_ptr (p + ss);
245 // segment is not large enough
246 consolidate_ptr (p); // try to consolidate with next segment (if any)
248 // check segment (again) and skip over too small free ones
249 while (SEGSIZ < 0 && (-SEGSIZ) <= ss && (-SEGSIZ) != sp) {
250 traversed += -SEGSIZ + sop;
251 if (traversed >= _poolsize) {
252 return NULL; // reached last segment. OOM.
254 p += (-SEGSIZ) + sop;
255 if (p >= _pool + _poolsize) {
257 if (SEGSIZ < 0) consolidate_ptr (p);
265 ReallocPool::_free (void *ptr) {
266 poolsize_t *in = (poolsize_t*) ptr;
268 *in = -*in; // mark as free
274 ReallocPool::_shrink (void *ptr, size_t newsize) {
275 poolsize_t *in = (poolsize_t*) ptr;
277 const poolsize_t avail = *in;
278 const poolsize_t ss = newsize + sizeof(poolsize_t);
280 return; // can't shrink
282 const poolsize_t sp = (poolsize_t)newsize;
283 STATS_used (newsize - avail);
284 *in = sp; // set new size
285 char *p = (char*) in;
286 *((poolsize_t*)(p + ss)) = ss - avail; // mark free space after.
291 ReallocPool::_asize (void *ptr) {
292 if (ptr == 0) return 0;
293 poolsize_t *in = (poolsize_t*) ptr;
302 ReallocPool::printstats ()
304 #ifdef RAP_WITH_SEGMENT_STATS
305 printf ("ReallocPool '%s': used: %ld (%.1f%%) (max: %ld), free: %ld [bytes]\n"
306 "|| segments: cur: %ld (max: %ld), largest-used: %ld, largest-free: %ld\n",
308 _cur_allocated, _cur_allocated * 100.f / _poolsize, _max_allocated, _cur_avail,
309 _seg_cur_count, _seg_max_count, _seg_max_used, _seg_max_avail);
310 #elif defined RAP_WITH_CALL_STATS
311 printf ("ReallocPool '%s':\n", _name.c_str());
313 #ifdef RAP_WITH_CALL_STATS
314 printf("|| malloc(): %ld, free(): %ld, realloc()+:%ld, realloc()-: %ld NOOP:%ld OOM:%ld\n",
315 _n_alloc, _n_free, _n_grow, _n_shrink, _n_noop, _n_oom);
316 printf("|| used: %ld / %ld, max: %ld (%.1f%%)\n",
317 _cur_used, _poolsize,
318 _max_used, 100.f *_max_used / _poolsize);
320 #ifdef RAP_WITH_HISTOGRAM
321 printf("--- malloc()\n");
322 print_histogram (_hist_alloc);
323 printf("--- realloc()/grow-to\n");
324 print_histogram (_hist_grow);
325 printf("--- realloc()/shrink-to\n");
326 print_histogram (_hist_shrink);
327 printf("--- free() histogram\n");
328 print_histogram (_hist_free);
329 printf("--------------------\n");
334 ReallocPool::dumpsegments ()
337 const poolsize_t sop = sizeof(poolsize_t);
338 poolsize_t *in = (poolsize_t*) p;
339 unsigned int traversed = 0;
340 printf ("<<<<< %s\n", _name.c_str());
343 printf ("0x%08x used %4d\n", traversed, *in);
344 printf ("0x%08x data %p\n", traversed + sop , p + sop);
345 traversed += *in + sop;
347 } else if ((*in) < 0) {
348 printf ("0x%08x free %4d [+%d]\n", traversed, -*in, sop);
349 traversed += -*in + sop;
352 printf ("0x%08x Corrupt!\n", traversed);
355 in = (poolsize_t*) p;
356 if (p == _pool + _poolsize) {
357 printf ("%08x end\n", traversed);
360 if (p > _pool + _poolsize) {
361 printf ("%08x Beyond End!\n", traversed);
368 #ifdef RAP_WITH_SEGMENT_STATS
370 ReallocPool::collect_segment_stats ()
373 poolsize_t *in = (poolsize_t*) p;
375 _cur_allocated = _cur_avail = 0;
376 _seg_cur_count = _seg_max_avail = _seg_max_used = 0;
381 _cur_allocated += *in;
383 if (*in > (poolsize_t)_seg_max_used) {
389 if (-*in > (poolsize_t)_seg_max_avail) {
390 _seg_max_avail = -*in;
393 p += sizeof(poolsize_t);
394 in = (poolsize_t*) p;
395 if (p == _pool + _poolsize) {
399 _seg_cur_count = _seg_cur_count;
401 if (_cur_allocated > _max_allocated) {
402 _max_allocated = _cur_allocated;
404 if (_seg_cur_count > _seg_max_count) {
405 _seg_max_count = _seg_cur_count;
410 #ifdef RAP_WITH_HISTOGRAM
412 ReallocPool::print_histogram (size_t const * const histogram) const
415 for (int i = 0; i < RAP_WITH_HISTOGRAM; ++i) {
416 if (histogram[i] > maxhist) maxhist = histogram[i];
418 const int termwidth = 50;
420 const int fact = RAP_BLOCKSIZE + 1;
422 if (maxhist > 0) for (int i = 0; i < RAP_WITH_HISTOGRAM; ++i) {
423 if (histogram[i] == 0) { continue; }
424 if (i == RAP_WITH_HISTOGRAM -1 ) {
426 printf(" > %4d: %7lu ", i * fact, histogram[i]);
428 printf(">%4d:%7lu ", i * fact, histogram[i]);
432 printf("%4d .. %4d: %7lu ", i * fact, (i + 1) * fact -1, histogram[i]);
434 printf("%4d: %7lu ", i * fact, (i + 1) * fact -1 , histogram[i]);
437 int bar_width = (histogram[i] * termwidth ) / maxhist;
438 if (bar_width == 0 && histogram[i] > 0) bar_width = 1;
439 for (int j = 0; j < bar_width; ++j) printf("#");
445 ReallocPool::hist_bin (size_t s) const {
447 s = (s + RAP_BLOCKSIZE) & (~RAP_BLOCKSIZE);
448 s /= (RAP_BLOCKSIZE + 1);
450 if (s > RAP_WITH_HISTOGRAM - 1) s = RAP_WITH_HISTOGRAM - 1;