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_ENABLE_EMBEDDED_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_ENABLE_EMBEDDED_TESTS
297 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
304 saved_counter = _dbus_get_fail_alloc_counter ();
313 #ifdef DBUS_ENABLE_EMBEDDED_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_ENABLE_EMBEDDED_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
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_ENABLE_EMBEDDED_TESTS
453 #include "dbus-test.h"
458 time_for_size (
int size)
462 #ifdef DBUS_ENABLE_VERBOSE_MODE
466 #define FREE_ARRAY_SIZE 512
467 #define N_ITERATIONS FREE_ARRAY_SIZE * 512
468 void *to_free[FREE_ARRAY_SIZE];
471 _dbus_verbose (
"Timings for size %d\n", size);
473 _dbus_verbose (
" malloc\n");
475 #ifdef DBUS_ENABLE_VERBOSE_MODE
481 while (i < N_ITERATIONS)
488 if (j == FREE_ARRAY_SIZE)
491 while (j < FREE_ARRAY_SIZE)
503 #ifdef DBUS_ENABLE_VERBOSE_MODE
506 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
507 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
511 _dbus_verbose (
" mempools\n");
520 while (i < N_ITERATIONS)
527 if (j == FREE_ARRAY_SIZE)
530 while (j < FREE_ARRAY_SIZE)
544 #ifdef DBUS_ENABLE_VERBOSE_MODE
547 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
548 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
550 _dbus_verbose (
" zeroed malloc\n");
557 while (i < N_ITERATIONS)
564 if (j == FREE_ARRAY_SIZE)
567 while (j < FREE_ARRAY_SIZE)
579 #ifdef DBUS_ENABLE_VERBOSE_MODE
582 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
583 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
585 _dbus_verbose (
" zeroed mempools\n");
594 while (i < N_ITERATIONS)
601 if (j == FREE_ARRAY_SIZE)
604 while (j < FREE_ARRAY_SIZE)
618 #ifdef DBUS_ENABLE_VERBOSE_MODE
621 _dbus_verbose (
" created/destroyed %d elements in %g seconds\n",
622 N_ITERATIONS, (end - start) / (
double) CLOCKS_PER_SEC);
632 _dbus_mem_pool_test (
void)
635 int element_sizes[] = { 4, 8, 16, 50, 124 };
640 time_for_size (element_sizes[i]);
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
DBusMemBlock * next
next block in the list, which is already used up; only saved so we can free all the blocks when we fr...
#define NULL
A null pointer, defined appropriately for C or C++.
void * _dbus_mem_pool_alloc(DBusMemPool *pool)
Allocates an object from the memory pool.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
struct DBusMemBlock DBusMemBlock
Typedef for DBusMemBlock so the struct can recursively point to itself.
DBusFreedElement * free_elements
a free list of elements to recycle
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
unsigned char elements[ELEMENT_PADDING]
the block data, actually allocated to required size
dbus_bool_t _dbus_mem_pool_dealloc(DBusMemPool *pool, void *element)
Deallocates an object previously created with _dbus_mem_pool_alloc().
DBusMemBlock object represents a single malloc()-returned block that gets chunked up into objects in ...
#define _DBUS_INT_MAX
Maximum value of type "int".
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
int allocated_elements
Count of outstanding allocated elements.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
int block_size
size of most recently allocated block
DBusMemBlock * blocks
blocks of memory from malloc()
void _dbus_mem_pool_free(DBusMemPool *pool)
Frees a memory pool (and all elements allocated from it).
Internals fields of DBusMemPool.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
DBusFreedElement * next
next element of the free list
long used_so_far
bytes of this block already allocated as elements.
struct representing an element on the free list.
unsigned int zero_elements
whether to zero-init allocated elements
#define FALSE
Expands to "0".
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
int element_size
size of a single object in the pool
DBusMemPool * _dbus_mem_pool_new(int element_size, dbus_bool_t zero_elements)
Creates a new memory pool, or returns NULL on failure.
#define ELEMENT_PADDING
The dummy size of the variable-length "elements" field in DBusMemBlock.