Main Page | Modules | Data Structures | Directories | File List | Data Fields | Related Pages

dbus-memory.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-memory.c  D-BUS memory handling
00003  *
00004  * Copyright (C) 2002, 2003  Red Hat Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include "dbus-memory.h"
00025 #include "dbus-internals.h"
00026 #include "dbus-sysdeps.h"
00027 #include "dbus-list.h"
00028 #include <stdlib.h>
00029  /* end of public API docs */
00091 
00098 #ifdef DBUS_BUILD_TESTS
00099 static dbus_bool_t debug_initialized = FALSE;
00100 static int fail_nth = -1;
00101 static size_t fail_size = 0;
00102 static int fail_alloc_counter = _DBUS_INT_MAX;
00103 static int n_failures_per_failure = 1;
00104 static int n_failures_this_failure = 0;
00105 static dbus_bool_t guards = FALSE;
00106 static dbus_bool_t disable_mem_pools = FALSE;
00107 static dbus_bool_t backtrace_on_fail_alloc = FALSE;
00108 static int n_blocks_outstanding = 0;
00109 
00111 #define GUARD_VALUE 0xdeadbeef
00112 
00113 #define GUARD_INFO_SIZE 8
00114 
00115 #define GUARD_START_PAD 16
00116 
00117 #define GUARD_END_PAD 16
00118 
00119 #define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)
00120 
00121 #define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)
00122 
00123 static void
00124 _dbus_initialize_malloc_debug (void)
00125 {
00126   if (!debug_initialized)
00127     {
00128       debug_initialized = TRUE;
00129       
00130       if (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH") != NULL)
00131         {
00132           fail_nth = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH"));
00133           fail_alloc_counter = fail_nth;
00134           _dbus_verbose ("Will fail malloc every %d times\n", fail_nth);
00135         }
00136       
00137       if (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN") != NULL)
00138         {
00139           fail_size = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN"));
00140           _dbus_verbose ("Will fail mallocs over %ld bytes\n",
00141                          (long) fail_size);
00142         }
00143 
00144       if (_dbus_getenv ("DBUS_MALLOC_GUARDS") != NULL)
00145         {
00146           guards = TRUE;
00147           _dbus_verbose ("Will use malloc guards\n");
00148         }
00149 
00150       if (_dbus_getenv ("DBUS_DISABLE_MEM_POOLS") != NULL)
00151         {
00152           disable_mem_pools = TRUE;
00153           _dbus_verbose ("Will disable memory pools\n");
00154         }
00155 
00156       if (_dbus_getenv ("DBUS_MALLOC_BACKTRACES") != NULL)
00157         {
00158           backtrace_on_fail_alloc = TRUE;
00159           _dbus_verbose ("Will backtrace on failing a malloc\n");
00160         }
00161     }
00162 }
00163 
00169 dbus_bool_t
00170 _dbus_disable_mem_pools (void)
00171 {
00172   _dbus_initialize_malloc_debug ();
00173   return disable_mem_pools;
00174 }
00175 
00184 void
00185 _dbus_set_fail_alloc_counter (int until_next_fail)
00186 {
00187   _dbus_initialize_malloc_debug ();
00188 
00189   fail_alloc_counter = until_next_fail;
00190 
00191 #if 0
00192   _dbus_verbose ("Set fail alloc counter = %d\n", fail_alloc_counter);
00193 #endif
00194 }
00195 
00202 int
00203 _dbus_get_fail_alloc_counter (void)
00204 {
00205   _dbus_initialize_malloc_debug ();
00206 
00207   return fail_alloc_counter;
00208 }
00209 
00216 void
00217 _dbus_set_fail_alloc_failures (int failures_per_failure)
00218 {
00219   n_failures_per_failure = failures_per_failure;
00220 }
00221 
00228 int
00229 _dbus_get_fail_alloc_failures (void)
00230 {
00231   return n_failures_per_failure;
00232 }
00233 
00234 #ifdef DBUS_BUILD_TESTS
00235 
00243 dbus_bool_t
00244 _dbus_decrement_fail_alloc_counter (void)
00245 {
00246   _dbus_initialize_malloc_debug ();
00247   
00248   if (fail_alloc_counter <= 0)
00249     {
00250       if (backtrace_on_fail_alloc)
00251         _dbus_print_backtrace ();
00252 
00253       _dbus_verbose ("failure %d\n", n_failures_this_failure);
00254       
00255       n_failures_this_failure += 1;
00256       if (n_failures_this_failure >= n_failures_per_failure)
00257         {
00258           if (fail_nth >= 0)
00259             fail_alloc_counter = fail_nth;
00260           else
00261             fail_alloc_counter = _DBUS_INT_MAX;
00262 
00263           n_failures_this_failure = 0;
00264 
00265           _dbus_verbose ("reset fail alloc counter to %d\n", fail_alloc_counter);
00266         }
00267       
00268       return TRUE;
00269     }
00270   else
00271     {
00272       fail_alloc_counter -= 1;
00273       return FALSE;
00274     }
00275 }
00276 #endif /* DBUS_BUILD_TESTS */
00277 
00283 int
00284 _dbus_get_malloc_blocks_outstanding (void)
00285 {
00286   return n_blocks_outstanding;
00287 }
00288 
00292 typedef enum
00293 {
00294   SOURCE_UNKNOWN,
00295   SOURCE_MALLOC,
00296   SOURCE_REALLOC,
00297   SOURCE_MALLOC_ZERO,
00298   SOURCE_REALLOC_NULL
00299 } BlockSource;
00300 
00301 static const char*
00302 source_string (BlockSource source)
00303 {
00304   switch (source)
00305     {
00306     case SOURCE_UNKNOWN:
00307       return "unknown";
00308     case SOURCE_MALLOC:
00309       return "malloc";
00310     case SOURCE_REALLOC:
00311       return "realloc";
00312     case SOURCE_MALLOC_ZERO:
00313       return "malloc0";
00314     case SOURCE_REALLOC_NULL:
00315       return "realloc(NULL)";
00316     }
00317   _dbus_assert_not_reached ("Invalid malloc block source ID");
00318   return "invalid!";
00319 }
00320 
00321 static void
00322 check_guards (void       *free_block,
00323               dbus_bool_t overwrite)
00324 {
00325   if (free_block != NULL)
00326     {
00327       unsigned char *block = ((unsigned char*)free_block) - GUARD_START_OFFSET;
00328       size_t requested_bytes = *(dbus_uint32_t*)block;
00329       BlockSource source = *(dbus_uint32_t*)(block + 4);
00330       unsigned int i;
00331       dbus_bool_t failed;
00332 
00333       failed = FALSE;
00334 
00335 #if 0
00336       _dbus_verbose ("Checking %d bytes request from source %s\n",
00337                      requested_bytes, source_string (source));
00338 #endif
00339       
00340       i = GUARD_INFO_SIZE;
00341       while (i < GUARD_START_OFFSET)
00342         {
00343           dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
00344           if (value != GUARD_VALUE)
00345             {
00346               _dbus_warn ("Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x\n",
00347                           (long) requested_bytes, source_string (source),
00348                           value, i, GUARD_VALUE);
00349               failed = TRUE;
00350             }
00351           
00352           i += 4;
00353         }
00354 
00355       i = GUARD_START_OFFSET + requested_bytes;
00356       while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
00357         {
00358           dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
00359           if (value != GUARD_VALUE)
00360             {
00361               _dbus_warn ("Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x\n",
00362                           (long) requested_bytes, source_string (source),
00363                           value, i, GUARD_VALUE);
00364               failed = TRUE;
00365             }
00366           
00367           i += 4;
00368         }
00369 
00370       /* set memory to anything but nul bytes */
00371       if (overwrite)
00372         memset (free_block, 'g', requested_bytes);
00373       
00374       if (failed)
00375         _dbus_assert_not_reached ("guard value corruption");
00376     }
00377 }
00378 
00379 static void*
00380 set_guards (void       *real_block,
00381             size_t      requested_bytes,
00382             BlockSource source)
00383 {
00384   unsigned char *block = real_block;
00385   unsigned int i;
00386   
00387   if (block == NULL)
00388     return NULL;
00389 
00390   _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
00391   
00392   *((dbus_uint32_t*)block) = requested_bytes;
00393   *((dbus_uint32_t*)(block + 4)) = source;
00394 
00395   i = GUARD_INFO_SIZE;
00396   while (i < GUARD_START_OFFSET)
00397     {
00398       (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
00399       
00400       i += 4;
00401     }
00402 
00403   i = GUARD_START_OFFSET + requested_bytes;
00404   while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
00405     {
00406       (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
00407       
00408       i += 4;
00409     }
00410   
00411   check_guards (block + GUARD_START_OFFSET, FALSE);
00412   
00413   return block + GUARD_START_OFFSET;
00414 }
00415 
00416 #endif
00417  /* End of internals docs */
00419 
00420 
00436 void*
00437 dbus_malloc (size_t bytes)
00438 {
00439 #ifdef DBUS_BUILD_TESTS
00440   _dbus_initialize_malloc_debug ();
00441   
00442   if (_dbus_decrement_fail_alloc_counter ())
00443     {
00444       _dbus_verbose (" FAILING malloc of %ld bytes\n", (long) bytes);
00445       
00446       return NULL;
00447     }
00448 #endif
00449   
00450   if (bytes == 0) /* some system mallocs handle this, some don't */
00451     return NULL;
00452 #ifdef DBUS_BUILD_TESTS
00453   else if (fail_size != 0 && bytes > fail_size)
00454     return NULL;
00455   else if (guards)
00456     {
00457       void *block;
00458 
00459       block = malloc (bytes + GUARD_EXTRA_SIZE);
00460       if (block)
00461         n_blocks_outstanding += 1;
00462       
00463       return set_guards (block, bytes, SOURCE_MALLOC);
00464     }
00465 #endif
00466   else
00467     {
00468       void *mem;
00469       mem = malloc (bytes);
00470 #ifdef DBUS_BUILD_TESTS
00471       if (mem)
00472         n_blocks_outstanding += 1;
00473 #endif
00474       return mem;
00475     }
00476 }
00477 
00487 void*
00488 dbus_malloc0 (size_t bytes)
00489 {
00490 #ifdef DBUS_BUILD_TESTS
00491   _dbus_initialize_malloc_debug ();
00492   
00493   if (_dbus_decrement_fail_alloc_counter ())
00494     {
00495       _dbus_verbose (" FAILING malloc0 of %ld bytes\n", (long) bytes);
00496       
00497       return NULL;
00498     }
00499 #endif
00500 
00501   if (bytes == 0)
00502     return NULL;
00503 #ifdef DBUS_BUILD_TESTS
00504   else if (fail_size != 0 && bytes > fail_size)
00505     return NULL;
00506   else if (guards)
00507     {
00508       void *block;
00509 
00510       block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
00511       if (block)
00512         n_blocks_outstanding += 1;
00513       return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
00514     }
00515 #endif
00516   else
00517     {
00518       void *mem;
00519       mem = calloc (bytes, 1);
00520 #ifdef DBUS_BUILD_TESTS
00521       if (mem)
00522         n_blocks_outstanding += 1;
00523 #endif
00524       return mem;
00525     }
00526 }
00527 
00538 void*
00539 dbus_realloc (void  *memory,
00540               size_t bytes)
00541 {
00542 #ifdef DBUS_BUILD_TESTS
00543   _dbus_initialize_malloc_debug ();
00544   
00545   if (_dbus_decrement_fail_alloc_counter ())
00546     {
00547       _dbus_verbose (" FAILING realloc of %ld bytes\n", (long) bytes);
00548       
00549       return NULL;
00550     }
00551 #endif
00552   
00553   if (bytes == 0) /* guarantee this is safe */
00554     {
00555       dbus_free (memory);
00556       return NULL;
00557     }
00558 #ifdef DBUS_BUILD_TESTS
00559   else if (fail_size != 0 && bytes > fail_size)
00560     return NULL;
00561   else if (guards)
00562     {
00563       if (memory)
00564         {
00565           size_t old_bytes;
00566           void *block;
00567           
00568           check_guards (memory, FALSE);
00569           
00570           block = realloc (((unsigned char*)memory) - GUARD_START_OFFSET,
00571                            bytes + GUARD_EXTRA_SIZE);
00572 
00573           old_bytes = *(dbus_uint32_t*)block;
00574           if (block && bytes >= old_bytes)
00575             /* old guards shouldn't have moved */
00576             check_guards (((unsigned char*)block) + GUARD_START_OFFSET, FALSE);
00577           
00578           return set_guards (block, bytes, SOURCE_REALLOC);
00579         }
00580       else
00581         {
00582           void *block;
00583           
00584           block = malloc (bytes + GUARD_EXTRA_SIZE);
00585 
00586           if (block)
00587             n_blocks_outstanding += 1;
00588           
00589           return set_guards (block, bytes, SOURCE_REALLOC_NULL);   
00590         }
00591     }
00592 #endif
00593   else
00594     {
00595       void *mem;
00596       mem = realloc (memory, bytes);
00597 #ifdef DBUS_BUILD_TESTS
00598       if (memory == NULL && mem != NULL)
00599         n_blocks_outstanding += 1;
00600 #endif
00601       return mem;
00602     }
00603 }
00604 
00611 void
00612 dbus_free (void  *memory)
00613 {
00614 #ifdef DBUS_BUILD_TESTS
00615   if (guards)
00616     {
00617       check_guards (memory, TRUE);
00618       if (memory)
00619         {
00620           n_blocks_outstanding -= 1;
00621           
00622           _dbus_assert (n_blocks_outstanding >= 0);
00623           
00624           free (((unsigned char*)memory) - GUARD_START_OFFSET);
00625         }
00626       
00627       return;
00628     }
00629 #endif
00630     
00631   if (memory) /* we guarantee it's safe to free (NULL) */
00632     {
00633 #ifdef DBUS_BUILD_TESTS
00634       n_blocks_outstanding -= 1;
00635       
00636       _dbus_assert (n_blocks_outstanding >= 0);
00637 #endif
00638 
00639       free (memory);
00640     }
00641 }
00642 
00649 void
00650 dbus_free_string_array (char **str_array)
00651 {
00652   if (str_array)
00653     {
00654       int i;
00655 
00656       i = 0;
00657       while (str_array[i])
00658         {
00659           dbus_free (str_array[i]);
00660           i++;
00661         }
00662 
00663       dbus_free (str_array);
00664     }
00665 }
00666  /* End of public API docs block */
00668 
00669 
00682 int _dbus_current_generation = 1;
00683 
00687 typedef struct ShutdownClosure ShutdownClosure;
00688 
00692 struct ShutdownClosure
00693 {
00694   ShutdownClosure *next;     
00695   DBusShutdownFunction func; 
00696   void *data;                
00697 };
00698 
00699 _DBUS_DEFINE_GLOBAL_LOCK (shutdown_funcs);
00700 static ShutdownClosure *registered_globals = NULL;
00701 
00710 dbus_bool_t
00711 _dbus_register_shutdown_func (DBusShutdownFunction  func,
00712                               void                 *data)
00713 {
00714   ShutdownClosure *c;
00715 
00716   c = dbus_new (ShutdownClosure, 1);
00717 
00718   if (c == NULL)
00719     return FALSE;
00720 
00721   c->func = func;
00722   c->data = data;
00723 
00724   _DBUS_LOCK (shutdown_funcs);
00725   
00726   c->next = registered_globals;
00727   registered_globals = c;
00728 
00729   _DBUS_UNLOCK (shutdown_funcs);
00730   
00731   return TRUE;
00732 }
00733  /* End of private API docs block */
00735 
00736 
00755 void
00756 dbus_shutdown (void)
00757 {
00758   while (registered_globals != NULL)
00759     {
00760       ShutdownClosure *c;
00761 
00762       c = registered_globals;
00763       registered_globals = c->next;
00764       
00765       (* c->func) (c->data);
00766       
00767       dbus_free (c);
00768     }
00769 
00770   _dbus_current_generation += 1;
00771 }
00772  
00775 #ifdef DBUS_BUILD_TESTS
00776 #include "dbus-test.h"
00777 
00783 dbus_bool_t
00784 _dbus_memory_test (void)
00785 {
00786   dbus_bool_t old_guards;
00787   void *p;
00788   size_t size;
00789 
00790   old_guards = guards;
00791   guards = TRUE;
00792   p = dbus_malloc (4);
00793   if (p == NULL)
00794     _dbus_assert_not_reached ("no memory");
00795   for (size = 4; size < 256; size += 4)
00796     {
00797       p = dbus_realloc (p, size);
00798       if (p == NULL)
00799         _dbus_assert_not_reached ("no memory");
00800     }
00801   for (size = 256; size != 0; size -= 4)
00802     {
00803       p = dbus_realloc (p, size);
00804       if (p == NULL)
00805         _dbus_assert_not_reached ("no memory");
00806     }
00807   dbus_free (p);
00808   guards = old_guards;
00809   return TRUE;
00810 }
00811 
00812 #endif

Generated on Tue Aug 30 16:35:51 2005 for D-BUS by  doxygen 1.4.3