Commit 0c502d5b authored by Anders Carlsson's avatar Anders Carlsson

2003-02-15 Anders Carlsson <andersca@codefactory.se>

	* dbus/dbus-internals.h:
	Fix build without tests.

	* dbus/dbus-list.c: (alloc_link):
	Fix a segfault when a malloc fails.

	* dbus/dbus-memory.c: (initialize_malloc_debug), (dbus_malloc),
	(dbus_malloc0), (dbus_realloc):
	Add support for malloc debugging.
parent fe401894
2003-02-15 Anders Carlsson <andersca@codefactory.se>
* dbus/dbus-internals.h:
Fix build without tests.
* dbus/dbus-list.c: (alloc_link):
Fix a segfault when a malloc fails.
* dbus/dbus-memory.c: (initialize_malloc_debug), (dbus_malloc),
(dbus_malloc0), (dbus_realloc):
Add support for malloc debugging.
2003-02-15 Alexander Larsson <alexl@redhat.com>
* dbus/dbus-threads.c:
......
......@@ -150,7 +150,7 @@ dbus_bool_t _dbus_decrement_fail_alloc_counter (void);
#else
#define _dbus_set_fail_alloc_counter(n)
#define _dbus_get_fail_alloc_counter _DBUS_INT_MAX
#define _dbus_decrement_fail_alloc_counter FALSE
#define _dbus_decrement_fail_alloc_counter() FALSE
#endif /* !DBUS_BUILD_TESTS */
DBUS_END_DECLS;
......
......@@ -78,7 +78,8 @@ alloc_link (void *data)
}
link = _dbus_mem_pool_alloc (list_pool);
link->data = data;
if (link)
link->data = data;
dbus_mutex_unlock (list_pool_lock);
......
......@@ -25,6 +25,7 @@
#include "dbus-internals.h"
#include <stdlib.h>
/**
* @defgroup DBusMemory Memory Allocation
* @ingroup DBus
......@@ -71,6 +72,32 @@
* @param memory the memory to free
*/
#ifdef DBUS_BUILD_TESTS
static dbus_bool_t inited = FALSE;
static int fail_counts = -1;
static int fail_size = -1;
#endif
#ifdef DBUS_BUILD_TESTS
static void
initialize_malloc_debug (void)
{
if (!inited)
{
if (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH") != NULL)
{
fail_counts = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH"));
_dbus_set_fail_alloc_counter (fail_counts);
}
if (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN") != NULL)
fail_size = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN"));
inited = TRUE;
}
}
#endif
/**
* Allocates the given number of bytes, as with standard
* malloc(). Guaranteed to return #NULL if bytes is zero
......@@ -83,11 +110,24 @@
void*
dbus_malloc (size_t bytes)
{
#ifdef DBUS_BUILD_TESTS
initialize_malloc_debug ();
if (_dbus_decrement_fail_alloc_counter ())
return NULL;
{
if (fail_counts != -1)
_dbus_set_fail_alloc_counter (fail_counts);
return NULL;
}
#endif
if (bytes == 0) /* some system mallocs handle this, some don't */
return NULL;
#if DBUS_BUILD_TESTS
else if (fail_size != -1 && bytes > fail_size)
return NULL;
#endif
else
return malloc (bytes);
}
......@@ -104,11 +144,24 @@ dbus_malloc (size_t bytes)
void*
dbus_malloc0 (size_t bytes)
{
#ifdef DBUS_BUILD_TESTS
initialize_malloc_debug ();
if (_dbus_decrement_fail_alloc_counter ())
return NULL;
{
if (fail_counts != -1)
_dbus_set_fail_alloc_counter (fail_counts);
return NULL;
}
#endif
if (bytes == 0)
return NULL;
#if DBUS_BUILD_TESTS
else if (fail_size != -1 && bytes > fail_size)
return NULL;
#endif
else
return calloc (bytes, 1);
}
......@@ -127,14 +180,27 @@ void*
dbus_realloc (void *memory,
size_t bytes)
{
#ifdef DBUS_BUILD_TESTS
initialize_malloc_debug ();
if (_dbus_decrement_fail_alloc_counter ())
return NULL;
{
if (fail_counts != -1)
_dbus_set_fail_alloc_counter (fail_counts);
return NULL;
}
#endif
if (bytes == 0) /* guarantee this is safe */
{
dbus_free (memory);
return NULL;
}
#if DBUS_BUILD_TESTS
else if (fail_size != -1 && bytes > fail_size)
return NULL;
#endif
else
{
return realloc (memory, bytes);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment