27 #include "dbus-mempool.h"
28 #include "dbus-internals.h"
29 #include "dbus-valgrind-internal.h"
72 #define ELEMENT_PADDING 4
150 if (element_size < 8)
152 if (element_size < (
int)
sizeof (
void *))
153 element_size =
sizeof (
void *);
164 pool->
element_size = _DBUS_ALIGN_VALUE (element_size,
sizeof (
void *));
180 VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
195 VALGRIND_DESTROY_MEMPOOL (pool);
198 while (block !=
NULL)
220 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
221 if (_dbus_disable_mem_pools ())
247 VALGRIND_MEMPOOL_ALLOC (pool, (
void *) &block->
elements[0],
257 if (_dbus_decrement_fail_alloc_counter ())
259 _dbus_verbose (
" FAILING mempool alloc\n");
268 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->
element_size);
287 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
301 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
308 saved_counter = _dbus_get_fail_alloc_counter ();
317 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
318 _dbus_set_fail_alloc_counter (saved_counter);
319 _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
336 VALGRIND_MEMPOOL_ALLOC (pool, element, pool->
element_size);
354 VALGRIND_MEMPOOL_FREE (pool, element);
356 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
357 if (_dbus_disable_mem_pools ())
367 while (block !=
NULL)
369 if (block->
elements == (
unsigned char*) element)
400 VALGRIND_MAKE_MEM_UNDEFINED (freed,
sizeof (*freed));
412 #ifdef DBUS_ENABLE_STATS
436 if (block == pool->
blocks)
443 if (in_use_p !=
NULL)
446 if (in_free_list_p !=
NULL)
447 *in_free_list_p = in_free_list;
449 if (allocated_p !=
NULL)
450 *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".
#define ELEMENT_PADDING
The dummy size of the variable-length "elements" field in DBusMemBlock.
struct DBusMemBlock DBusMemBlock
Typedef for DBusMemBlock so the struct can recursively point to itself.
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().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
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 ...
DBusMemBlock * next
next block in the list, which is already used up; only saved so we can free all the blocks when we fr...
long used_so_far
bytes of this block already allocated as elements.
unsigned char elements[ELEMENT_PADDING]
the block data, actually allocated to required size
Internals fields of DBusMemPool.
int allocated_elements
Count of outstanding allocated elements.
unsigned int zero_elements
whether to zero-init allocated elements
int block_size
size of most recently allocated block
DBusMemBlock * blocks
blocks of memory from malloc()
int element_size
size of a single object in the pool
DBusFreedElement * free_elements
a free list of elements to recycle