25 #include "dbus-memory.h"
26 #include "dbus-internals.h"
27 #include "dbus-sysdeps.h"
28 #include "dbus-list.h"
99 #ifdef DBUS_BUILD_TESTS
101 static int fail_nth = -1;
102 static size_t fail_size = 0;
104 static int n_failures_per_failure = 1;
105 static int n_failures_this_failure = 0;
113 #define GUARD_VALUE 0xdeadbeef
115 #define GUARD_INFO_SIZE 8
117 #define GUARD_START_PAD 16
119 #define GUARD_END_PAD 16
121 #define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)
123 #define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)
126 _dbus_initialize_malloc_debug (
void)
128 if (!debug_initialized)
130 debug_initialized =
TRUE;
134 fail_nth = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_NTH"));
135 fail_alloc_counter = fail_nth;
136 _dbus_verbose (
"Will fail dbus_malloc every %d times\n", fail_nth);
141 fail_size = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_GREATER_THAN"));
142 _dbus_verbose (
"Will fail mallocs over %ld bytes\n",
149 _dbus_verbose (
"Will use dbus_malloc guards\n");
154 disable_mem_pools =
TRUE;
155 _dbus_verbose (
"Will disable memory pools\n");
160 backtrace_on_fail_alloc =
TRUE;
161 _dbus_verbose (
"Will backtrace on failing a dbus_malloc\n");
166 malloc_cannot_fail =
TRUE;
167 _dbus_verbose (
"Will abort if system malloc() and friends fail\n");
178 _dbus_disable_mem_pools (
void)
180 _dbus_initialize_malloc_debug ();
181 return disable_mem_pools;
193 _dbus_set_fail_alloc_counter (
int until_next_fail)
195 _dbus_initialize_malloc_debug ();
197 fail_alloc_counter = until_next_fail;
200 _dbus_verbose (
"Set fail alloc counter = %d\n", fail_alloc_counter);
211 _dbus_get_fail_alloc_counter (
void)
213 _dbus_initialize_malloc_debug ();
215 return fail_alloc_counter;
225 _dbus_set_fail_alloc_failures (
int failures_per_failure)
227 n_failures_per_failure = failures_per_failure;
237 _dbus_get_fail_alloc_failures (
void)
239 return n_failures_per_failure;
242 #ifdef DBUS_BUILD_TESTS
252 _dbus_decrement_fail_alloc_counter (
void)
254 _dbus_initialize_malloc_debug ();
255 #ifdef DBUS_WIN_FIXME
261 _dbus_verbose(
"TODO: memory allocation testing errors disabled for now\n");
268 if (fail_alloc_counter <= 0)
270 if (backtrace_on_fail_alloc)
273 _dbus_verbose (
"failure %d\n", n_failures_this_failure);
275 n_failures_this_failure += 1;
276 if (n_failures_this_failure >= n_failures_per_failure)
279 fail_alloc_counter = fail_nth;
283 n_failures_this_failure = 0;
285 _dbus_verbose (
"reset fail alloc counter to %d\n", fail_alloc_counter);
292 fail_alloc_counter -= 1;
304 _dbus_get_malloc_blocks_outstanding (
void)
322 source_string (BlockSource source)
332 case SOURCE_MALLOC_ZERO:
334 case SOURCE_REALLOC_NULL:
335 return "realloc(NULL)";
342 check_guards (
void *free_block,
345 if (free_block !=
NULL)
347 unsigned char *block = ((
unsigned char*)free_block) - GUARD_START_OFFSET;
356 _dbus_verbose (
"Checking %d bytes request from source %s\n",
357 requested_bytes, source_string (source));
361 while (i < GUARD_START_OFFSET)
364 if (value != GUARD_VALUE)
366 _dbus_warn (
"Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x\n",
367 (
long) requested_bytes, source_string (source),
368 value, i, GUARD_VALUE);
375 i = GUARD_START_OFFSET + requested_bytes;
376 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
379 if (value != GUARD_VALUE)
381 _dbus_warn (
"Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x\n",
382 (
long) requested_bytes, source_string (source),
383 value, i, GUARD_VALUE);
392 memset (free_block,
'g', requested_bytes);
400 set_guards (
void *real_block,
401 size_t requested_bytes,
404 unsigned char *block = real_block;
410 _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
416 while (i < GUARD_START_OFFSET)
423 i = GUARD_START_OFFSET + requested_bytes;
424 while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
431 check_guards (block + GUARD_START_OFFSET,
FALSE);
433 return block + GUARD_START_OFFSET;
462 #ifdef DBUS_BUILD_TESTS
463 _dbus_initialize_malloc_debug ();
465 if (_dbus_decrement_fail_alloc_counter ())
467 _dbus_verbose (
" FAILING malloc of %ld bytes\n", (
long) bytes);
474 #ifdef DBUS_BUILD_TESTS
475 else if (fail_size != 0 && bytes > fail_size)
481 block = malloc (bytes + GUARD_EXTRA_SIZE);
486 else if (malloc_cannot_fail)
488 _dbus_warn (
"out of memory: malloc (%ld + %ld)\n",
489 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
493 return set_guards (block, bytes, SOURCE_MALLOC);
499 mem = malloc (bytes);
501 #ifdef DBUS_BUILD_TESTS
506 else if (malloc_cannot_fail)
508 _dbus_warn (
"out of memory: malloc (%ld)\n", (
long) bytes);
532 #ifdef DBUS_BUILD_TESTS
533 _dbus_initialize_malloc_debug ();
535 if (_dbus_decrement_fail_alloc_counter ())
537 _dbus_verbose (
" FAILING malloc0 of %ld bytes\n", (
long) bytes);
545 #ifdef DBUS_BUILD_TESTS
546 else if (fail_size != 0 && bytes > fail_size)
552 block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
558 else if (malloc_cannot_fail)
560 _dbus_warn (
"out of memory: calloc (%ld + %ld, 1)\n",
561 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
565 return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
571 mem = calloc (bytes, 1);
573 #ifdef DBUS_BUILD_TESTS
578 else if (malloc_cannot_fail)
580 _dbus_warn (
"out of memory: calloc (%ld)\n", (
long) bytes);
603 #ifdef DBUS_BUILD_TESTS
604 _dbus_initialize_malloc_debug ();
606 if (_dbus_decrement_fail_alloc_counter ())
608 _dbus_verbose (
" FAILING realloc of %ld bytes\n", (
long) bytes);
619 #ifdef DBUS_BUILD_TESTS
620 else if (fail_size != 0 && bytes > fail_size)
629 check_guards (memory,
FALSE);
631 block = realloc (((
unsigned char*)memory) - GUARD_START_OFFSET,
632 bytes + GUARD_EXTRA_SIZE);
636 if (malloc_cannot_fail)
638 _dbus_warn (
"out of memory: realloc (%p, %ld + %ld)\n",
639 memory, (
long) bytes, (
long) GUARD_EXTRA_SIZE);
647 if (bytes >= old_bytes)
649 check_guards (((
unsigned char*)block) + GUARD_START_OFFSET,
FALSE);
651 return set_guards (block, bytes, SOURCE_REALLOC);
657 block = malloc (bytes + GUARD_EXTRA_SIZE);
663 else if (malloc_cannot_fail)
665 _dbus_warn (
"out of memory: malloc (%ld + %ld)\n",
666 (
long) bytes, (
long) GUARD_EXTRA_SIZE);
670 return set_guards (block, bytes, SOURCE_REALLOC_NULL);
677 mem = realloc (memory, bytes);
679 #ifdef DBUS_BUILD_TESTS
680 if (mem ==
NULL && malloc_cannot_fail)
682 _dbus_warn (
"out of memory: malloc (%ld)\n", (
long) bytes);
702 #ifdef DBUS_BUILD_TESTS
705 check_guards (memory,
TRUE);
708 #ifdef DBUS_DISABLE_ASSERT
717 free (((
unsigned char*)memory) - GUARD_START_OFFSET);
726 #ifdef DBUS_BUILD_TESTS
727 #ifdef DBUS_DISABLE_ASSERT
824 c->
next = registered_globals;
825 registered_globals = c;
881 while (registered_globals !=
NULL)
885 c = registered_globals;
886 registered_globals = c->
next;
893 _dbus_current_generation += 1;
898 #ifdef DBUS_BUILD_TESTS
899 #include "dbus-test.h"
907 _dbus_memory_test (
void)
918 for (size = 4; size < 256; size += 4)
924 for (size = 256; size != 0; size -= 4)
This struct represents a function to be called on shutdown.
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
An atomic integer safe to increment or decrement from multiple threads.
dbus_int32_t _dbus_atomic_get(DBusAtomic *atomic)
Atomically get the value of an integer.
#define NULL
A null pointer, defined appropriately for C or C++.
void * dbus_realloc(void *memory, size_t bytes)
Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void * data
Data for function.
#define _DBUS_INT_MAX
Maximum value of type "int".
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core).
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
ShutdownClosure * next
Next ShutdownClosure.
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.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called, so we can reinit things after it's been called.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
DBusShutdownFunction func
Function to call.
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
#define TRUE
Expands to "1".
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _DBUS_DEFINE_GLOBAL_LOCK(name)
Defines a global lock variable with the given name.
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
void dbus_shutdown(void)
Frees all memory allocated internally by libdbus and reverses the effects of dbus_threads_init().
#define FALSE
Expands to "0".
void _dbus_print_backtrace(void)
On GNU libc systems, print a crude backtrace to stderr.
dbus_bool_t _dbus_register_shutdown_func(DBusShutdownFunction function, void *data)
Register a cleanup function to be called exactly once the next time dbus_shutdown() is called...
#define _DBUS_LOCK(name)
Locks a global lock.
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
int dbus_int32_t
A 32-bit signed integer on all platforms.