25 #include "dbus-mempool.h"
26 #include "dbus-internals.h"
27 #include "dbus-valgrind-internal.h"
70 #define ELEMENT_PADDING 4
148 if (element_size < 8)
160 pool->
element_size = _DBUS_ALIGN_VALUE (element_size,
sizeof (
void *));
176 VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
191 VALGRIND_DESTROY_MEMPOOL (pool);
194 while (block !=
NULL)
216 #ifdef DBUS_BUILD_TESTS
217 if (_dbus_disable_mem_pools ())
243 VALGRIND_MEMPOOL_ALLOC (pool, (
void *) &block->
elements[0],
253 if (_dbus_decrement_fail_alloc_counter ())
255 _dbus_verbose (
" FAILING mempool alloc\n");
264 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->
element_size);
283 #ifdef DBUS_BUILD_TESTS
297 #ifdef DBUS_BUILD_TESTS
304 saved_counter = _dbus_get_fail_alloc_counter ();
313 #ifdef DBUS_BUILD_TESTS
314 _dbus_set_fail_alloc_counter (saved_counter);
315 _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
332 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->
element_size);
350 VALGRIND_MEMPOOL_FREE (pool, element);
352 #ifdef DBUS_BUILD_TESTS
353 if (_dbus_disable_mem_pools ())
363 while (block !=
NULL)
365 if (block->
elements == (
unsigned char*) element)
396 VALGRIND_MAKE_MEM_UNDEFINED (freed,
sizeof (freed));
408 #ifdef DBUS_ENABLE_STATS
411 dbus_uint32_t *in_use_p,
412 dbus_uint32_t *in_free_list_p,
413 dbus_uint32_t *allocated_p)
417 dbus_uint32_t in_use = 0;
418 dbus_uint32_t in_free_list = 0;
419 dbus_uint32_t allocated = 0;
432 if (block == pool->
blocks)
439 if (in_use_p !=
NULL)
442 if (in_free_list_p !=
NULL)
443 *in_free_list_p = in_free_list;
445 if (allocated_p !=
NULL)
446 *allocated_p = allocated;
452 #ifdef DBUS_BUILD_TESTS
453 #include "dbus-test.h"
458 time_for_size (
int size)
464 #define FREE_ARRAY_SIZE 512
465 #define N_ITERATIONS FREE_ARRAY_SIZE * 512
466 void *to_free[FREE_ARRAY_SIZE];
469 _dbus_verbose (
"Timings for size %d\n", size);
471 _dbus_verbose (
" malloc\n");
477 while (i < N_ITERATIONS)
484 if (j == FREE_ARRAY_SIZE)
487 while (j < FREE_ARRAY_SIZE)
501 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
502 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
506 _dbus_verbose (
" mempools\n");
514 while (i < N_ITERATIONS)
521 if (j == FREE_ARRAY_SIZE)
524 while (j < FREE_ARRAY_SIZE)
540 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
541 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
543 _dbus_verbose (
" zeroed malloc\n");
549 while (i < N_ITERATIONS)
556 if (j == FREE_ARRAY_SIZE)
559 while (j < FREE_ARRAY_SIZE)
573 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
574 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
576 _dbus_verbose (
" zeroed mempools\n");
584 while (i < N_ITERATIONS)
591 if (j == FREE_ARRAY_SIZE)
594 while (j < FREE_ARRAY_SIZE)
610 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
611 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
620 _dbus_mem_pool_test (
void)
623 int element_sizes[] = { 4, 8, 16, 50, 124 };
628 time_for_size (element_sizes[i]);