-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-mempool.h Memory pools
*
* Copyright (C) 2002, 2003 Red Hat, Inc.
*
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+#include <config.h>
#include "dbus-mempool.h"
+#include "dbus-internals.h"
+#include "dbus-valgrind-internal.h"
/**
* @defgroup DBusMemPool memory pools
* when we free the mem pool.
*/
- int used_so_far; /**< bytes of this block already allocated as elements. */
+ /* this is a long so that "elements" is aligned */
+ 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 */
};
DBusFreedElement *free_elements; /**< a free list of elements to recycle */
DBusMemBlock *blocks; /**< blocks of memory from malloc() */
+ int allocated_elements; /**< Count of outstanding allocated elements */
};
/** @} */
pool->zero_elements = zero_elements != FALSE;
+ pool->allocated_elements = 0;
+
/* pick a size for the first block; it increases
* for each block we need to allocate. This is
* actually half the initial block size
_dbus_assert ((pool->block_size %
pool->element_size) == 0);
-
+
+ VALGRIND_CREATE_MEMPOOL (pool, 0, zero_elements);
+
return pool;
}
{
DBusMemBlock *block;
+ VALGRIND_DESTROY_MEMPOOL (pool);
+
block = pool->blocks;
while (block != NULL)
{
void*
_dbus_mem_pool_alloc (DBusMemPool *pool)
{
- if (_dbus_decrement_fail_alloc_counter ())
- return NULL;
-
- if (pool->free_elements)
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ if (_dbus_disable_mem_pools ())
{
- DBusFreedElement *element = pool->free_elements;
+ DBusMemBlock *block;
+ int alloc_size;
+
+ /* This is obviously really silly, but it's
+ * debug-mode-only code that is compiled out
+ * when tests are disabled (_dbus_disable_mem_pools()
+ * is a constant expression FALSE so this block
+ * should vanish)
+ */
+
+ alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING +
+ pool->element_size;
+
+ if (pool->zero_elements)
+ block = dbus_malloc0 (alloc_size);
+ else
+ block = dbus_malloc (alloc_size);
- pool->free_elements = pool->free_elements->next;
+ if (block != NULL)
+ {
+ block->next = pool->blocks;
+ pool->blocks = block;
+ pool->allocated_elements += 1;
- if (pool->zero_elements)
- memset (element, '\0', pool->element_size);
-
- return element;
+ VALGRIND_MEMPOOL_ALLOC (pool, (void *) &block->elements[0],
+ pool->element_size);
+ return (void*) &block->elements[0];
+ }
+ else
+ return NULL;
}
else
+#endif
{
- void *element;
-
- if (pool->blocks == NULL ||
- pool->blocks->used_so_far == pool->block_size)
+ if (_dbus_decrement_fail_alloc_counter ())
+ {
+ _dbus_verbose (" FAILING mempool alloc\n");
+ return NULL;
+ }
+ else if (pool->free_elements)
{
- /* Need a new block */
- DBusMemBlock *block;
- int alloc_size;
-#ifdef DBUS_BUILD_TESTS
- int saved_counter;
+ DBusFreedElement *element = pool->free_elements;
+
+ pool->free_elements = pool->free_elements->next;
+
+ VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
+
+ if (pool->zero_elements)
+ memset (element, '\0', pool->element_size);
+
+ pool->allocated_elements += 1;
+
+ return element;
+ }
+ else
+ {
+ void *element;
+
+ if (pool->blocks == NULL ||
+ pool->blocks->used_so_far == pool->block_size)
+ {
+ /* Need a new block */
+ DBusMemBlock *block;
+ int alloc_size;
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ int saved_counter;
#endif
- if (pool->block_size <= _DBUS_INT_MAX / 4) /* avoid overflow */
- {
- /* use a larger block size for our next block */
- pool->block_size *= 2;
- _dbus_assert ((pool->block_size %
- pool->element_size) == 0);
- }
-
- alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING + pool->block_size;
-
-#ifdef DBUS_BUILD_TESTS
- /* We save/restore the counter, so that memory pools won't
- * cause a given function to have different number of
- * allocations on different invocations. i.e. when testing
- * we want consistent alloc patterns. So we skip our
- * malloc here for purposes of failed alloc simulation.
- */
- saved_counter = _dbus_get_fail_alloc_counter ();
- _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
+ if (pool->block_size <= _DBUS_INT_MAX / 4) /* avoid overflow */
+ {
+ /* use a larger block size for our next block */
+ pool->block_size *= 2;
+ _dbus_assert ((pool->block_size %
+ pool->element_size) == 0);
+ }
+
+ alloc_size = sizeof (DBusMemBlock) - ELEMENT_PADDING + pool->block_size;
+
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ /* We save/restore the counter, so that memory pools won't
+ * cause a given function to have different number of
+ * allocations on different invocations. i.e. when testing
+ * we want consistent alloc patterns. So we skip our
+ * malloc here for purposes of failed alloc simulation.
+ */
+ saved_counter = _dbus_get_fail_alloc_counter ();
+ _dbus_set_fail_alloc_counter (_DBUS_INT_MAX);
#endif
- if (pool->zero_elements)
- block = dbus_malloc0 (alloc_size);
- else
- block = dbus_malloc (alloc_size);
-
-#ifdef DBUS_BUILD_TESTS
- _dbus_set_fail_alloc_counter (saved_counter);
+ if (pool->zero_elements)
+ block = dbus_malloc0 (alloc_size);
+ else
+ block = dbus_malloc (alloc_size);
+
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ _dbus_set_fail_alloc_counter (saved_counter);
+ _dbus_assert (saved_counter == _dbus_get_fail_alloc_counter ());
#endif
- if (block == NULL)
- return NULL;
+ if (block == NULL)
+ return NULL;
- block->used_so_far = 0;
- block->next = pool->blocks;
- pool->blocks = block;
- }
+ block->used_so_far = 0;
+ block->next = pool->blocks;
+ pool->blocks = block;
+ }
- element = &pool->blocks->elements[pool->blocks->used_so_far];
+ element = &pool->blocks->elements[pool->blocks->used_so_far];
+
+ pool->blocks->used_so_far += pool->element_size;
- pool->blocks->used_so_far += pool->element_size;
+ pool->allocated_elements += 1;
- return element;
+ VALGRIND_MEMPOOL_ALLOC (pool, element, pool->element_size);
+ return element;
+ }
}
}
* must have come from this same pool.
* @param pool the memory pool
* @param element the element earlier allocated.
+ * @returns #TRUE if there are no remaining allocated elements
*/
-void
+dbus_bool_t
_dbus_mem_pool_dealloc (DBusMemPool *pool,
void *element)
{
+ VALGRIND_MEMPOOL_FREE (pool, element);
+
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ if (_dbus_disable_mem_pools ())
+ {
+ DBusMemBlock *block;
+ DBusMemBlock *prev;
+
+ /* mmm, fast. ;-) debug-only code, so doesn't matter. */
+
+ prev = NULL;
+ block = pool->blocks;
+
+ while (block != NULL)
+ {
+ if (block->elements == (unsigned char*) element)
+ {
+ if (prev)
+ prev->next = block->next;
+ else
+ pool->blocks = block->next;
+
+ dbus_free (block);
+
+ _dbus_assert (pool->allocated_elements > 0);
+ pool->allocated_elements -= 1;
+
+ if (pool->allocated_elements == 0)
+ _dbus_assert (pool->blocks == NULL);
+
+ return pool->blocks == NULL;
+ }
+ prev = block;
+ block = block->next;
+ }
+
+ _dbus_assert_not_reached ("freed nonexistent block");
+ return FALSE;
+ }
+ else
+#endif
+ {
+ DBusFreedElement *freed;
+
+ freed = element;
+ /* used for internal mempool administration */
+ VALGRIND_MAKE_MEM_UNDEFINED (freed, sizeof (*freed));
+
+ freed->next = pool->free_elements;
+ pool->free_elements = freed;
+
+ _dbus_assert (pool->allocated_elements > 0);
+ pool->allocated_elements -= 1;
+
+ return pool->allocated_elements == 0;
+ }
+}
+
+#ifdef DBUS_ENABLE_STATS
+void
+_dbus_mem_pool_get_stats (DBusMemPool *pool,
+ dbus_uint32_t *in_use_p,
+ dbus_uint32_t *in_free_list_p,
+ dbus_uint32_t *allocated_p)
+{
+ DBusMemBlock *block;
DBusFreedElement *freed;
+ dbus_uint32_t in_use = 0;
+ dbus_uint32_t in_free_list = 0;
+ dbus_uint32_t allocated = 0;
+
+ if (pool != NULL)
+ {
+ in_use = pool->element_size * pool->allocated_elements;
+
+ for (freed = pool->free_elements; freed != NULL; freed = freed->next)
+ {
+ in_free_list += pool->element_size;
+ }
+
+ for (block = pool->blocks; block != NULL; block = block->next)
+ {
+ if (block == pool->blocks)
+ allocated += pool->block_size;
+ else
+ allocated += block->used_so_far;
+ }
+ }
+
+ if (in_use_p != NULL)
+ *in_use_p = in_use;
+
+ if (in_free_list_p != NULL)
+ *in_free_list_p = in_free_list;
- freed = element;
- freed->next = pool->free_elements;
- pool->free_elements = freed;
+ if (allocated_p != NULL)
+ *allocated_p = allocated;
}
+#endif /* DBUS_ENABLE_STATS */
/** @} */
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
#include "dbus-test.h"
#include <stdio.h>
#include <time.h>
{
int i;
int j;
+#ifdef DBUS_ENABLE_VERBOSE_MODE
clock_t start;
clock_t end;
+#endif
#define FREE_ARRAY_SIZE 512
#define N_ITERATIONS FREE_ARRAY_SIZE * 512
void *to_free[FREE_ARRAY_SIZE];
_dbus_verbose (" malloc\n");
+#ifdef DBUS_ENABLE_VERBOSE_MODE
start = clock ();
-
+#endif
+
i = 0;
j = 0;
while (i < N_ITERATIONS)
++i;
}
+#ifdef DBUS_ENABLE_VERBOSE_MODE
end = clock ();
_dbus_verbose (" created/destroyed %d elements in %g seconds\n",
_dbus_verbose (" mempools\n");
start = clock ();
+#endif
pool = _dbus_mem_pool_new (size, FALSE);
_dbus_mem_pool_free (pool);
+#ifdef DBUS_ENABLE_VERBOSE_MODE
end = clock ();
_dbus_verbose (" created/destroyed %d elements in %g seconds\n",
_dbus_verbose (" zeroed malloc\n");
start = clock ();
+#endif
i = 0;
j = 0;
++i;
}
+#ifdef DBUS_ENABLE_VERBOSE_MODE
end = clock ();
_dbus_verbose (" created/destroyed %d elements in %g seconds\n",
_dbus_verbose (" zeroed mempools\n");
start = clock ();
+#endif
pool = _dbus_mem_pool_new (size, TRUE);
_dbus_mem_pool_free (pool);
+#ifdef DBUS_ENABLE_VERBOSE_MODE
end = clock ();
_dbus_verbose (" created/destroyed %d elements in %g seconds\n",
N_ITERATIONS, (end - start) / (double) CLOCKS_PER_SEC);
+#endif
}
/**
return TRUE;
}
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */