summaryrefslogtreecommitdiff
path: root/dbus/dbus-memory.c
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@codefactory.se>2003-02-15 17:15:40 +0000
committerAnders Carlsson <andersca@codefactory.se>2003-02-15 17:15:40 +0000
commit0c502d5bc3c1858acb78d57439767f8ef24f90fe (patch)
tree9be6f0a05bd25c9c1113e9da21da4898b60058a5 /dbus/dbus-memory.c
parentfe4018941190f8bf020e4a8ed2999c212e0e113d (diff)
downloaddbus-0c502d5bc3c1858acb78d57439767f8ef24f90fe.tar.gz
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.
Diffstat (limited to 'dbus/dbus-memory.c')
-rw-r--r--dbus/dbus-memory.c72
1 files changed, 69 insertions, 3 deletions
diff --git a/dbus/dbus-memory.c b/dbus/dbus-memory.c
index 357e8be6..74c97c26 100644
--- a/dbus/dbus-memory.c
+++ b/dbus/dbus-memory.c
@@ -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);