29#include "dbus-mempool.h"
30#include "dbus-internals.h"
31#include "dbus-valgrind-internal.h"
88#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
99_DBUS_STATIC_ASSERT (_DBUS_IS_ALIGNED (
sizeof (
struct DBusMemBlock),
101_DBUS_STATIC_ASSERT (_DBUS_IS_ALIGNED (offsetof (
struct DBusMemBlock,
149 dbus_bool_t zero_elements)
158 if (element_size < 8)
160 if (element_size < (
int)
sizeof (
void *))
161 element_size =
sizeof (
void *);
189 VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
204 VALGRIND_DESTROY_MEMPOOL (pool);
207 while (block !=
NULL)
229#ifdef DBUS_ENABLE_EMBEDDED_TESTS
230 if (_dbus_disable_mem_pools ())
255 VALGRIND_MEMPOOL_ALLOC (pool, (
void *) &block->
elements[0],
267 if (_dbus_decrement_fail_alloc_counter ())
269 _dbus_verbose (
" FAILING mempool alloc\n");
278 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->
element_size);
298#ifdef DBUS_ENABLE_EMBEDDED_TESTS
312#ifdef DBUS_ENABLE_EMBEDDED_TESTS
319 saved_counter = _dbus_get_fail_alloc_counter ();
330#ifdef DBUS_ENABLE_EMBEDDED_TESTS
331 _dbus_set_fail_alloc_counter (saved_counter);
332 _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
349 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->
element_size);
369 VALGRIND_MEMPOOL_FREE (pool, element);
371#ifdef DBUS_ENABLE_EMBEDDED_TESTS
372 if (_dbus_disable_mem_pools ())
382 while (block !=
NULL)
384 if (block->
elements == (
unsigned char*) element)
415 VALGRIND_MAKE_MEM_UNDEFINED (freed,
sizeof (*freed));
427#ifdef DBUS_ENABLE_STATS
430 dbus_uint32_t *in_use_p,
431 dbus_uint32_t *in_free_list_p,
432 dbus_uint32_t *allocated_p)
436 dbus_uint32_t in_use = 0;
437 dbus_uint32_t in_free_list = 0;
438 dbus_uint32_t allocated = 0;
451 if (block == pool->
blocks)
458 if (in_use_p !=
NULL)
461 if (in_free_list_p !=
NULL)
462 *in_free_list_p = in_free_list;
464 if (allocated_p !=
NULL)
465 *allocated_p = allocated;
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_INT_MAX
Maximum value of type "int".
#define NULL
A null pointer, defined appropriately for C or C++.
#define FALSE
Expands to "0".
void * _dbus_mem_pool_alloc(DBusMemPool *pool)
Allocates an object from the memory pool.
dbus_bool_t _dbus_mem_pool_dealloc(DBusMemPool *pool, void *element)
Deallocates an object previously created with _dbus_mem_pool_alloc().
void _dbus_mem_pool_free(DBusMemPool *pool)
Frees a memory pool (and all elements allocated from it).
DBusMemPool * _dbus_mem_pool_new(int element_size, dbus_bool_t zero_elements)
Creates a new memory pool, or returns NULL on failure.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
struct representing an element on the free list.
DBusFreedElement * next
next element of the free list
DBusMemBlock object represents a single malloc()-returned block that gets chunked up into objects in ...
size_t used_so_far
bytes of this block already allocated as elements.
unsigned char elements[]
the block data, actually allocated to required size
DBusMemBlock * next
next block in the list, which is already used up; only saved so we can free all the blocks when we fr...
Internals fields of DBusMemPool.
int allocated_elements
Count of outstanding allocated elements.
unsigned int zero_elements
whether to zero-init allocated elements
size_t block_size
size of most recently allocated block
DBusMemBlock * blocks
blocks of memory from malloc()
size_t element_size
size of a single object in the pool
DBusFreedElement * free_elements
a free list of elements to recycle
A simple value union that lets you access bytes as if they were various types; useful when dealing wi...