25 #include "dbus-memory.h"    26 #include "dbus-internals.h"    27 #include "dbus-sysdeps.h"    28 #include "dbus-list.h"    29 #include "dbus-threads.h"   100 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   102 static int fail_nth = -1;
   103 static size_t fail_size = 0;
   105 static int n_failures_per_failure = 1;
   106 static int n_failures_this_failure = 0;
   114 #define GUARD_VALUE 0xdeadbeef   116 #define GUARD_INFO_SIZE 8   118 #define GUARD_START_PAD 16   120 #define GUARD_END_PAD 16   122 #define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)   124 #define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)   127 _dbus_initialize_malloc_debug (
void)
   129   if (!debug_initialized)
   131       debug_initialized = 
TRUE;
   135           fail_nth = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_NTH"));
   136           fail_alloc_counter = fail_nth;
   137           _dbus_verbose (
"Will fail dbus_malloc every %d times\n", fail_nth);
   142           fail_size = atoi (
_dbus_getenv (
"DBUS_MALLOC_FAIL_GREATER_THAN"));
   143           _dbus_verbose (
"Will fail mallocs over %ld bytes\n",
   150           _dbus_verbose (
"Will use dbus_malloc guards\n");
   155           disable_mem_pools = 
TRUE;
   156           _dbus_verbose (
"Will disable memory pools\n");
   161           backtrace_on_fail_alloc = 
TRUE;
   162           _dbus_verbose (
"Will backtrace on failing a dbus_malloc\n");
   167           malloc_cannot_fail = 
TRUE;
   168           _dbus_verbose (
"Will abort if system malloc() and friends fail\n");
   179 _dbus_disable_mem_pools (
void)
   181   _dbus_initialize_malloc_debug ();
   182   return disable_mem_pools;
   194 _dbus_set_fail_alloc_counter (
int until_next_fail)
   196   _dbus_initialize_malloc_debug ();
   198   fail_alloc_counter = until_next_fail;
   201   _dbus_verbose (
"Set fail alloc counter = %d\n", fail_alloc_counter);
   212 _dbus_get_fail_alloc_counter (
void)
   214   _dbus_initialize_malloc_debug ();
   216   return fail_alloc_counter;
   226 _dbus_set_fail_alloc_failures (
int failures_per_failure)
   228   n_failures_per_failure = failures_per_failure;
   238 _dbus_get_fail_alloc_failures (
void)
   240   return n_failures_per_failure;
   243 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   253 _dbus_decrement_fail_alloc_counter (
void)
   255   _dbus_initialize_malloc_debug ();
   256 #ifdef DBUS_WIN_FIXME   262         _dbus_verbose(
"TODO: memory allocation testing errors disabled for now\n");
   269   if (fail_alloc_counter <= 0)
   271       if (backtrace_on_fail_alloc)
   274       _dbus_verbose (
"failure %d\n", n_failures_this_failure);
   276       n_failures_this_failure += 1;
   277       if (n_failures_this_failure >= n_failures_per_failure)
   280             fail_alloc_counter = fail_nth;
   284           n_failures_this_failure = 0;
   286           _dbus_verbose (
"reset fail alloc counter to %d\n", fail_alloc_counter);
   293       fail_alloc_counter -= 1;
   305 _dbus_get_malloc_blocks_outstanding (
void)
   323 source_string (BlockSource source)
   333     case SOURCE_MALLOC_ZERO:
   335     case SOURCE_REALLOC_NULL:
   336       return "realloc(NULL)";
   343 check_guards (
void       *free_block,
   346   if (free_block != 
NULL)
   348       unsigned char *block = ((
unsigned char*)free_block) - GUARD_START_OFFSET;
   349       size_t requested_bytes = *(dbus_uint32_t*)block;
   350       BlockSource source = *(dbus_uint32_t*)(block + 4);
   357       _dbus_verbose (
"Checking %d bytes request from source %s\n",
   358                      requested_bytes, source_string (source));
   362       while (i < GUARD_START_OFFSET)
   364           dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
   365           if (value != GUARD_VALUE)
   367               _dbus_warn (
"Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x\n",
   368                           (
long) requested_bytes, source_string (source),
   369                           value, i, GUARD_VALUE);
   376       i = GUARD_START_OFFSET + requested_bytes;
   377       while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
   379           dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
   380           if (value != GUARD_VALUE)
   382               _dbus_warn (
"Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x\n",
   383                           (
long) requested_bytes, source_string (source),
   384                           value, i, GUARD_VALUE);
   393         memset (free_block, 
'g', requested_bytes);
   401 set_guards (
void       *real_block,
   402             size_t      requested_bytes,
   405   unsigned char *block = real_block;
   411   _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
   413   *((dbus_uint32_t*)block) = requested_bytes;
   414   *((dbus_uint32_t*)(block + 4)) = source;
   417   while (i < GUARD_START_OFFSET)
   419       (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
   424   i = GUARD_START_OFFSET + requested_bytes;
   425   while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
   427       (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
   432   check_guards (block + GUARD_START_OFFSET, 
FALSE);
   434   return block + GUARD_START_OFFSET;
   463 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   464   _dbus_initialize_malloc_debug ();
   466   if (_dbus_decrement_fail_alloc_counter ())
   468       _dbus_verbose (
" FAILING malloc of %ld bytes\n", (
long) bytes);
   475 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   476   else if (fail_size != 0 && bytes > fail_size)
   482       block = malloc (bytes + GUARD_EXTRA_SIZE);
   487       else if (malloc_cannot_fail)
   489           _dbus_warn (
"out of memory: malloc (%ld + %ld)\n",
   490               (
long) bytes, (
long) GUARD_EXTRA_SIZE);
   494       return set_guards (block, bytes, SOURCE_MALLOC);
   500       mem = malloc (bytes);
   502 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   507       else if (malloc_cannot_fail)
   509           _dbus_warn (
"out of memory: malloc (%ld)\n", (
long) bytes);
   533 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   534   _dbus_initialize_malloc_debug ();
   536   if (_dbus_decrement_fail_alloc_counter ())
   538       _dbus_verbose (
" FAILING malloc0 of %ld bytes\n", (
long) bytes);
   546 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   547   else if (fail_size != 0 && bytes > fail_size)
   553       block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
   559       else if (malloc_cannot_fail)
   561           _dbus_warn (
"out of memory: calloc (%ld + %ld, 1)\n",
   562               (
long) bytes, (
long) GUARD_EXTRA_SIZE);
   566       return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
   572       mem = calloc (bytes, 1);
   574 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   579       else if (malloc_cannot_fail)
   581           _dbus_warn (
"out of memory: calloc (%ld)\n", (
long) bytes);
   604 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   605   _dbus_initialize_malloc_debug ();
   607   if (_dbus_decrement_fail_alloc_counter ())
   609       _dbus_verbose (
" FAILING realloc of %ld bytes\n", (
long) bytes);
   620 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   621   else if (fail_size != 0 && bytes > fail_size)
   630           check_guards (memory, 
FALSE);
   632           block = realloc (((
unsigned char*)memory) - GUARD_START_OFFSET,
   633                            bytes + GUARD_EXTRA_SIZE);
   637               if (malloc_cannot_fail)
   639                   _dbus_warn (
"out of memory: realloc (%p, %ld + %ld)\n",
   640                       memory, (
long) bytes, (
long) GUARD_EXTRA_SIZE);
   647           old_bytes = *(dbus_uint32_t*)block;
   648           if (bytes >= old_bytes)
   650             check_guards (((
unsigned char*)block) + GUARD_START_OFFSET, 
FALSE);
   652           return set_guards (block, bytes, SOURCE_REALLOC);
   658           block = malloc (bytes + GUARD_EXTRA_SIZE);
   664           else if (malloc_cannot_fail)
   666               _dbus_warn (
"out of memory: malloc (%ld + %ld)\n",
   667                   (
long) bytes, (
long) GUARD_EXTRA_SIZE);
   671           return set_guards (block, bytes, SOURCE_REALLOC_NULL);   
   678       mem = realloc (memory, bytes);
   680 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   681       if (mem == 
NULL && malloc_cannot_fail)
   683           _dbus_warn (
"out of memory: malloc (%ld)\n", (
long) bytes);
   703 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   706       check_guards (memory, 
TRUE);
   709 #ifdef DBUS_DISABLE_ASSERT   712           dbus_int32_t old_value;
   718           free (((
unsigned char*)memory) - GUARD_START_OFFSET);
   727 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   728 #ifdef DBUS_DISABLE_ASSERT   731       dbus_int32_t old_value;
   818   ok = _dbus_register_shutdown_func_unlocked (func, data);
   824 _dbus_register_shutdown_func_unlocked (DBusShutdownFunction  
func,
   837   c->
next = registered_globals;
   838   registered_globals = c;
   899   while (registered_globals != 
NULL)
   903       c = registered_globals;
   904       registered_globals = c->
next;
   916   _dbus_current_generation += 1;
   922 #ifdef DBUS_ENABLE_EMBEDDED_TESTS   923 #include "dbus-test.h"   931 _dbus_memory_test (
void)
   942   for (size = 4; size < 256; size += 4)
   948   for (size = 256; size != 0; size -= 4)
 This struct represents a function to be called on shutdown. 
An atomic integer safe to increment or decrement from multiple threads. 
#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". 
void _dbus_abort(void)
Aborts the program with SIGABRT (dumping core). 
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific(). 
ShutdownClosure * next
Next ShutdownClosure. 
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc(). 
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific(). 
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. 
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer. 
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. 
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. 
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer. 
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 func, 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, initializing it first if necessary. 
void * dbus_malloc0(size_t bytes)
Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...
dbus_int32_t _dbus_atomic_get(DBusAtomic *atomic)
Atomically get the value of an integer. 
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().