diff options
author | Anders Carlsson <andersca@codefactory.se> | 2003-02-15 17:15:40 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@codefactory.se> | 2003-02-15 17:15:40 +0000 |
commit | 0c502d5bc3c1858acb78d57439767f8ef24f90fe (patch) | |
tree | 9be6f0a05bd25c9c1113e9da21da4898b60058a5 | |
parent | fe4018941190f8bf020e4a8ed2999c212e0e113d (diff) |
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.
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | dbus/dbus-internals.h | 2 | ||||
-rw-r--r-- | dbus/dbus-list.c | 3 | ||||
-rw-r--r-- | dbus/dbus-memory.c | 72 |
4 files changed, 84 insertions, 5 deletions
@@ -1,3 +1,15 @@ +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: diff --git a/dbus/dbus-internals.h b/dbus/dbus-internals.h index 29dd41ce..26661564 100644 --- a/dbus/dbus-internals.h +++ b/dbus/dbus-internals.h @@ -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; diff --git a/dbus/dbus-list.c b/dbus/dbus-list.c index 7f12e3db..8c4e0c3a 100644 --- a/dbus/dbus-list.c +++ b/dbus/dbus-list.c @@ -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); 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); |