Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / dbus / dbus-memory.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-memory.c  D-Bus memory handling
3  *
4  * Copyright (C) 2002, 2003  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-memory.h"
26 #include "dbus-internals.h"
27 #include "dbus-sysdeps.h"
28 #include "dbus-list.h"
29 #include <stdlib.h>
30
31 /**
32  * @defgroup DBusMemory Memory Allocation
33  * @ingroup  DBus
34  * @brief dbus_malloc(), dbus_free(), etc.
35  *
36  * Functions and macros related to allocating and releasing
37  * blocks of memory.
38  *
39  */
40
41 /**
42  * @defgroup DBusMemoryInternals Memory allocation implementation details
43  * @ingroup  DBusInternals
44  * @brief internals of dbus_malloc() etc.
45  *
46  * Implementation details related to allocating and releasing blocks
47  * of memory.
48  */
49
50 /**
51  * @addtogroup DBusMemory
52  *
53  * @{
54  */
55
56 /**
57  * @def dbus_new
58  *
59  * Safe macro for using dbus_malloc(). Accepts the type
60  * to allocate and the number of type instances to
61  * allocate as arguments, and returns a memory block
62  * cast to the desired type, instead of as a void*.
63  *
64  * @param type type name to allocate
65  * @param count number of instances in the allocated array
66  * @returns the new memory block or #NULL on failure
67  */
68
69 /**
70  * @def dbus_new0
71  *
72  * Safe macro for using dbus_malloc0(). Accepts the type
73  * to allocate and the number of type instances to
74  * allocate as arguments, and returns a memory block
75  * cast to the desired type, instead of as a void*.
76  * The allocated array is initialized to all-bits-zero.
77  *
78  * @param type type name to allocate
79  * @param count number of instances in the allocated array
80  * @returns the new memory block or #NULL on failure
81  */
82
83 /**
84  * @typedef DBusFreeFunction
85  *
86  * The type of a function which frees a block of memory.
87  *
88  * @param memory the memory to free
89  */
90
91 /** @} */ /* end of public API docs */
92
93 /**
94  * @addtogroup DBusMemoryInternals
95  *
96  * @{
97  */
98
99 #ifdef DBUS_BUILD_TESTS
100 static dbus_bool_t debug_initialized = FALSE;
101 static int fail_nth = -1;
102 static size_t fail_size = 0;
103 static int fail_alloc_counter = _DBUS_INT_MAX;
104 static int n_failures_per_failure = 1;
105 static int n_failures_this_failure = 0;
106 static dbus_bool_t guards = FALSE;
107 static dbus_bool_t disable_mem_pools = FALSE;
108 static dbus_bool_t backtrace_on_fail_alloc = FALSE;
109 static DBusAtomic n_blocks_outstanding = {0};
110
111 /** value stored in guard padding for debugging buffer overrun */
112 #define GUARD_VALUE 0xdeadbeef
113 /** size of the information about the block stored in guard mode */
114 #define GUARD_INFO_SIZE 8
115 /** size of the GUARD_VALUE-filled padding after the header info  */
116 #define GUARD_START_PAD 16
117 /** size of the GUARD_VALUE-filled padding at the end of the block */
118 #define GUARD_END_PAD 16
119 /** size of stuff at start of block */
120 #define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)
121 /** total extra size over the requested allocation for guard stuff */
122 #define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)
123
124 static void
125 _dbus_initialize_malloc_debug (void)
126 {
127   if (!debug_initialized)
128     {
129       debug_initialized = TRUE;
130       
131       if (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH") != NULL)
132         {
133           fail_nth = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_NTH"));
134           fail_alloc_counter = fail_nth;
135           _dbus_verbose ("Will fail malloc every %d times\n", fail_nth);
136         }
137       
138       if (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN") != NULL)
139         {
140           fail_size = atoi (_dbus_getenv ("DBUS_MALLOC_FAIL_GREATER_THAN"));
141           _dbus_verbose ("Will fail mallocs over %ld bytes\n",
142                          (long) fail_size);
143         }
144
145       if (_dbus_getenv ("DBUS_MALLOC_GUARDS") != NULL)
146         {
147           guards = TRUE;
148           _dbus_verbose ("Will use malloc guards\n");
149         }
150
151       if (_dbus_getenv ("DBUS_DISABLE_MEM_POOLS") != NULL)
152         {
153           disable_mem_pools = TRUE;
154           _dbus_verbose ("Will disable memory pools\n");
155         }
156
157       if (_dbus_getenv ("DBUS_MALLOC_BACKTRACES") != NULL)
158         {
159           backtrace_on_fail_alloc = TRUE;
160           _dbus_verbose ("Will backtrace on failing a malloc\n");
161         }
162     }
163 }
164
165 /**
166  * Whether to turn off mem pools, useful for leak checking.
167  *
168  * @returns #TRUE if mempools should not be used.
169  */
170 dbus_bool_t
171 _dbus_disable_mem_pools (void)
172 {
173   _dbus_initialize_malloc_debug ();
174   return disable_mem_pools;
175 }
176
177 /**
178  * Sets the number of allocations until we simulate a failed
179  * allocation. If set to 0, the next allocation to run
180  * fails; if set to 1, one succeeds then the next fails; etc.
181  * Set to _DBUS_INT_MAX to not fail anything. 
182  *
183  * @param until_next_fail number of successful allocs before one fails
184  */
185 void
186 _dbus_set_fail_alloc_counter (int until_next_fail)
187 {
188   _dbus_initialize_malloc_debug ();
189
190   fail_alloc_counter = until_next_fail;
191
192 #if 0
193   _dbus_verbose ("Set fail alloc counter = %d\n", fail_alloc_counter);
194 #endif
195 }
196
197 /**
198  * Gets the number of successful allocs until we'll simulate
199  * a failed alloc.
200  *
201  * @returns current counter value
202  */
203 int
204 _dbus_get_fail_alloc_counter (void)
205 {
206   _dbus_initialize_malloc_debug ();
207
208   return fail_alloc_counter;
209 }
210
211 /**
212  * Sets how many mallocs to fail when the fail alloc counter reaches
213  * 0.
214  *
215  * @param failures_per_failure number to fail
216  */
217 void
218 _dbus_set_fail_alloc_failures (int failures_per_failure)
219 {
220   n_failures_per_failure = failures_per_failure;
221 }
222
223 /**
224  * Gets the number of failures we'll have when the fail malloc
225  * counter reaches 0.
226  *
227  * @returns number of failures planned
228  */
229 int
230 _dbus_get_fail_alloc_failures (void)
231 {
232   return n_failures_per_failure;
233 }
234
235 #ifdef DBUS_BUILD_TESTS
236 /**
237  * Called when about to alloc some memory; if
238  * it returns #TRUE, then the allocation should
239  * fail. If it returns #FALSE, then the allocation
240  * should not fail.
241  *
242  * @returns #TRUE if this alloc should fail
243  */
244 dbus_bool_t
245 _dbus_decrement_fail_alloc_counter (void)
246 {
247   _dbus_initialize_malloc_debug ();
248 #ifdef DBUS_WIN_FIXME
249   {
250     static dbus_bool_t called = 0;
251
252     if (!called)
253       {
254         _dbus_verbose("TODO: memory allocation testing errors disabled for now\n");
255         called = 1;
256       }
257     return FALSE;
258   }
259 #endif
260
261   if (fail_alloc_counter <= 0)
262     {
263       if (backtrace_on_fail_alloc)
264         _dbus_print_backtrace ();
265
266       _dbus_verbose ("failure %d\n", n_failures_this_failure);
267       
268       n_failures_this_failure += 1;
269       if (n_failures_this_failure >= n_failures_per_failure)
270         {
271           if (fail_nth >= 0)
272             fail_alloc_counter = fail_nth;
273           else
274             fail_alloc_counter = _DBUS_INT_MAX;
275
276           n_failures_this_failure = 0;
277
278           _dbus_verbose ("reset fail alloc counter to %d\n", fail_alloc_counter);
279         }
280       
281       return TRUE;
282     }
283   else
284     {
285       fail_alloc_counter -= 1;
286       return FALSE;
287     }
288 }
289 #endif /* DBUS_BUILD_TESTS */
290
291 /**
292  * Get the number of outstanding malloc()'d blocks.
293  *
294  * @returns number of blocks
295  */
296 int
297 _dbus_get_malloc_blocks_outstanding (void)
298 {
299   return _dbus_atomic_get (&n_blocks_outstanding);
300 }
301
302 /**
303  * Where the block came from.
304  */
305 typedef enum
306 {
307   SOURCE_UNKNOWN,
308   SOURCE_MALLOC,
309   SOURCE_REALLOC,
310   SOURCE_MALLOC_ZERO,
311   SOURCE_REALLOC_NULL
312 } BlockSource;
313
314 static const char*
315 source_string (BlockSource source)
316 {
317   switch (source)
318     {
319     case SOURCE_UNKNOWN:
320       return "unknown";
321     case SOURCE_MALLOC:
322       return "malloc";
323     case SOURCE_REALLOC:
324       return "realloc";
325     case SOURCE_MALLOC_ZERO:
326       return "malloc0";
327     case SOURCE_REALLOC_NULL:
328       return "realloc(NULL)";
329     }
330   _dbus_assert_not_reached ("Invalid malloc block source ID");
331   return "invalid!";
332 }
333
334 static void
335 check_guards (void       *free_block,
336               dbus_bool_t overwrite)
337 {
338   if (free_block != NULL)
339     {
340       unsigned char *block = ((unsigned char*)free_block) - GUARD_START_OFFSET;
341       size_t requested_bytes = *(dbus_uint32_t*)block;
342       BlockSource source = *(dbus_uint32_t*)(block + 4);
343       unsigned int i;
344       dbus_bool_t failed;
345
346       failed = FALSE;
347
348 #if 0
349       _dbus_verbose ("Checking %d bytes request from source %s\n",
350                      requested_bytes, source_string (source));
351 #endif
352       
353       i = GUARD_INFO_SIZE;
354       while (i < GUARD_START_OFFSET)
355         {
356           dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
357           if (value != GUARD_VALUE)
358             {
359               _dbus_warn ("Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x\n",
360                           (long) requested_bytes, source_string (source),
361                           value, i, GUARD_VALUE);
362               failed = TRUE;
363             }
364           
365           i += 4;
366         }
367
368       i = GUARD_START_OFFSET + requested_bytes;
369       while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
370         {
371           dbus_uint32_t value = *(dbus_uint32_t*) &block[i];
372           if (value != GUARD_VALUE)
373             {
374               _dbus_warn ("Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x\n",
375                           (long) requested_bytes, source_string (source),
376                           value, i, GUARD_VALUE);
377               failed = TRUE;
378             }
379           
380           i += 4;
381         }
382
383       /* set memory to anything but nul bytes */
384       if (overwrite)
385         memset (free_block, 'g', requested_bytes);
386       
387       if (failed)
388         _dbus_assert_not_reached ("guard value corruption");
389     }
390 }
391
392 static void*
393 set_guards (void       *real_block,
394             size_t      requested_bytes,
395             BlockSource source)
396 {
397   unsigned char *block = real_block;
398   unsigned int i;
399   
400   if (block == NULL)
401     return NULL;
402
403   _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
404   
405   *((dbus_uint32_t*)block) = requested_bytes;
406   *((dbus_uint32_t*)(block + 4)) = source;
407
408   i = GUARD_INFO_SIZE;
409   while (i < GUARD_START_OFFSET)
410     {
411       (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
412       
413       i += 4;
414     }
415
416   i = GUARD_START_OFFSET + requested_bytes;
417   while (i < (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
418     {
419       (*(dbus_uint32_t*) &block[i]) = GUARD_VALUE;
420       
421       i += 4;
422     }
423   
424   check_guards (block + GUARD_START_OFFSET, FALSE);
425   
426   return block + GUARD_START_OFFSET;
427 }
428
429 #endif
430
431 /** @} */ /* End of internals docs */
432
433
434 /**
435  * @addtogroup DBusMemory
436  *
437  * @{
438  */
439
440 /**
441  * Allocates the given number of bytes, as with standard
442  * malloc(). Guaranteed to return #NULL if bytes is zero
443  * on all platforms. Returns #NULL if the allocation fails.
444  * The memory must be released with dbus_free().
445  *
446  * dbus_malloc() memory is NOT safe to free with regular free() from
447  * the C library. Free it with dbus_free() only.
448  *
449  * @param bytes number of bytes to allocate
450  * @return allocated memory, or #NULL if the allocation fails.
451  */
452 void*
453 dbus_malloc (size_t bytes)
454 {
455 #ifdef DBUS_BUILD_TESTS
456   _dbus_initialize_malloc_debug ();
457   
458   if (_dbus_decrement_fail_alloc_counter ())
459     {
460       _dbus_verbose (" FAILING malloc of %ld bytes\n", (long) bytes);
461       return NULL;
462     }
463 #endif
464
465   if (bytes == 0) /* some system mallocs handle this, some don't */
466     return NULL;
467 #ifdef DBUS_BUILD_TESTS
468   else if (fail_size != 0 && bytes > fail_size)
469     return NULL;
470   else if (guards)
471     {
472       void *block;
473
474       block = malloc (bytes + GUARD_EXTRA_SIZE);
475       if (block)
476         _dbus_atomic_inc (&n_blocks_outstanding);
477       
478       return set_guards (block, bytes, SOURCE_MALLOC);
479     }
480 #endif
481   else
482     {
483       void *mem;
484       mem = malloc (bytes);
485 #ifdef DBUS_BUILD_TESTS
486       if (mem)
487         _dbus_atomic_inc (&n_blocks_outstanding);
488 #endif
489       return mem;
490     }
491 }
492
493 /**
494  * Allocates the given number of bytes, as with standard malloc(), but
495  * all bytes are initialized to zero as with calloc(). Guaranteed to
496  * return #NULL if bytes is zero on all platforms. Returns #NULL if the
497  * allocation fails.  The memory must be released with dbus_free().
498  *
499  * dbus_malloc0() memory is NOT safe to free with regular free() from
500  * the C library. Free it with dbus_free() only.
501  *
502  * @param bytes number of bytes to allocate
503  * @return allocated memory, or #NULL if the allocation fails.
504  */
505 void*
506 dbus_malloc0 (size_t bytes)
507 {
508 #ifdef DBUS_BUILD_TESTS
509   _dbus_initialize_malloc_debug ();
510   
511   if (_dbus_decrement_fail_alloc_counter ())
512     {
513       _dbus_verbose (" FAILING malloc0 of %ld bytes\n", (long) bytes);
514       
515       return NULL;
516     }
517 #endif
518   
519   if (bytes == 0)
520     return NULL;
521 #ifdef DBUS_BUILD_TESTS
522   else if (fail_size != 0 && bytes > fail_size)
523     return NULL;
524   else if (guards)
525     {
526       void *block;
527
528       block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
529       if (block)
530         _dbus_atomic_inc (&n_blocks_outstanding);
531       return set_guards (block, bytes, SOURCE_MALLOC_ZERO);
532     }
533 #endif
534   else
535     {
536       void *mem;
537       mem = calloc (bytes, 1);
538 #ifdef DBUS_BUILD_TESTS
539       if (mem)
540         _dbus_atomic_inc (&n_blocks_outstanding);
541 #endif
542       return mem;
543     }
544 }
545
546 /**
547  * Resizes a block of memory previously allocated by dbus_malloc() or
548  * dbus_malloc0(). Guaranteed to free the memory and return #NULL if bytes
549  * is zero on all platforms. Returns #NULL if the resize fails.
550  * If the resize fails, the memory is not freed.
551  *
552  * @param memory block to be resized
553  * @param bytes new size of the memory block
554  * @return allocated memory, or #NULL if the resize fails.
555  */
556 void*
557 dbus_realloc (void  *memory,
558               size_t bytes)
559 {
560 #ifdef DBUS_BUILD_TESTS
561   _dbus_initialize_malloc_debug ();
562   
563   if (_dbus_decrement_fail_alloc_counter ())
564     {
565       _dbus_verbose (" FAILING realloc of %ld bytes\n", (long) bytes);
566       
567       return NULL;
568     }
569 #endif
570   
571   if (bytes == 0) /* guarantee this is safe */
572     {
573       dbus_free (memory);
574       return NULL;
575     }
576 #ifdef DBUS_BUILD_TESTS
577   else if (fail_size != 0 && bytes > fail_size)
578     return NULL;
579   else if (guards)
580     {
581       if (memory)
582         {
583           size_t old_bytes;
584           void *block;
585           
586           check_guards (memory, FALSE);
587           
588           block = realloc (((unsigned char*)memory) - GUARD_START_OFFSET,
589                            bytes + GUARD_EXTRA_SIZE);
590
591           old_bytes = *(dbus_uint32_t*)block;
592           if (block && bytes >= old_bytes)
593             /* old guards shouldn't have moved */
594             check_guards (((unsigned char*)block) + GUARD_START_OFFSET, FALSE);
595           
596           return set_guards (block, bytes, SOURCE_REALLOC);
597         }
598       else
599         {
600           void *block;
601           
602           block = malloc (bytes + GUARD_EXTRA_SIZE);
603
604           if (block)
605             _dbus_atomic_inc (&n_blocks_outstanding);
606           
607           return set_guards (block, bytes, SOURCE_REALLOC_NULL);   
608         }
609     }
610 #endif
611   else
612     {
613       void *mem;
614       mem = realloc (memory, bytes);
615 #ifdef DBUS_BUILD_TESTS
616       if (memory == NULL && mem != NULL)
617             _dbus_atomic_inc (&n_blocks_outstanding);
618 #endif
619       return mem;
620     }
621 }
622
623 /**
624  * Frees a block of memory previously allocated by dbus_malloc() or
625  * dbus_malloc0(). If passed #NULL, does nothing.
626  * 
627  * @param memory block to be freed
628  */
629 void
630 dbus_free (void  *memory)
631 {
632 #ifdef DBUS_BUILD_TESTS
633   if (guards)
634     {
635       check_guards (memory, TRUE);
636       if (memory)
637         {
638 #ifdef DBUS_DISABLE_ASSERT
639           _dbus_atomic_dec (&n_blocks_outstanding);
640 #else
641           dbus_int32_t old_value;
642
643           old_value = _dbus_atomic_dec (&n_blocks_outstanding);
644           _dbus_assert (old_value >= 1);
645 #endif
646
647           free (((unsigned char*)memory) - GUARD_START_OFFSET);
648         }
649       
650       return;
651     }
652 #endif
653     
654   if (memory) /* we guarantee it's safe to free (NULL) */
655     {
656 #ifdef DBUS_BUILD_TESTS
657 #ifdef DBUS_DISABLE_ASSERT
658       _dbus_atomic_dec (&n_blocks_outstanding);
659 #else
660       dbus_int32_t old_value;
661
662       old_value = _dbus_atomic_dec (&n_blocks_outstanding);
663       _dbus_assert (old_value >= 1);
664 #endif
665 #endif
666
667       free (memory);
668     }
669 }
670
671 /**
672  * Frees a #NULL-terminated array of strings.
673  * If passed #NULL, does nothing.
674  *
675  * @param str_array the array to be freed
676  */
677 void
678 dbus_free_string_array (char **str_array)
679 {
680   if (str_array)
681     {
682       int i;
683
684       i = 0;
685       while (str_array[i])
686         {
687           dbus_free (str_array[i]);
688           i++;
689         }
690
691       dbus_free (str_array);
692     }
693 }
694
695 /** @} */ /* End of public API docs block */
696
697
698 /**
699  * @addtogroup DBusMemoryInternals
700  *
701  * @{
702  */
703
704 /**
705  * _dbus_current_generation is used to track each
706  * time that dbus_shutdown() is called, so we can
707  * reinit things after it's been called. It is simply
708  * incremented each time we shut down.
709  */
710 int _dbus_current_generation = 1;
711
712 /**
713  * Represents a function to be called on shutdown.
714  */
715 typedef struct ShutdownClosure ShutdownClosure;
716
717 /**
718  * This struct represents a function to be called on shutdown.
719  */
720 struct ShutdownClosure
721 {
722   ShutdownClosure *next;     /**< Next ShutdownClosure */
723   DBusShutdownFunction func; /**< Function to call */
724   void *data;                /**< Data for function */
725 };
726
727 _DBUS_DEFINE_GLOBAL_LOCK (shutdown_funcs);
728 static ShutdownClosure *registered_globals = NULL;
729
730 /**
731  * Register a cleanup function to be called exactly once
732  * the next time dbus_shutdown() is called.
733  *
734  * @param func the function
735  * @param data data to pass to the function
736  * @returns #FALSE on not enough memory
737  */
738 dbus_bool_t
739 _dbus_register_shutdown_func (DBusShutdownFunction  func,
740                               void                 *data)
741 {
742   ShutdownClosure *c;
743
744   c = dbus_new (ShutdownClosure, 1);
745
746   if (c == NULL)
747     return FALSE;
748
749   c->func = func;
750   c->data = data;
751
752   _DBUS_LOCK (shutdown_funcs);
753   
754   c->next = registered_globals;
755   registered_globals = c;
756
757   _DBUS_UNLOCK (shutdown_funcs);
758   
759   return TRUE;
760 }
761
762 /** @} */ /* End of private API docs block */
763
764
765 /**
766  * @addtogroup DBusMemory
767  *
768  * @{
769  */
770
771 /**
772  * Frees all memory allocated internally by libdbus and
773  * reverses the effects of dbus_threads_init(). libdbus keeps internal
774  * global variables, for example caches and thread locks, and it
775  * can be useful to free these internal data structures.
776  *
777  * dbus_shutdown() does NOT free memory that was returned
778  * to the application. It only returns libdbus-internal
779  * data structures.
780  *
781  * You MUST free all memory and release all reference counts
782  * returned to you by libdbus prior to calling dbus_shutdown().
783  *
784  * You can't continue to use any D-Bus objects, such as connections,
785  * that were allocated prior to dbus_shutdown(). You can, however,
786  * start over; call dbus_threads_init() again, create new connections,
787  * and so forth.
788  *
789  * WARNING: dbus_shutdown() is NOT thread safe, it must be called
790  * while NO other threads are using D-Bus. (Remember, you have to free
791  * all D-Bus objects and memory before you call dbus_shutdown(), so no
792  * thread can be using libdbus.)
793  *
794  * The purpose of dbus_shutdown() is to allow applications to get
795  * clean output from memory leak checkers. dbus_shutdown() may also be
796  * useful if you want to dlopen() libdbus instead of linking to it,
797  * and want to be able to unload the library again.
798  *
799  * There is absolutely no requirement to call dbus_shutdown() - in fact,
800  * most applications won't bother and should not feel guilty.
801  * 
802  * You have to know that nobody is using libdbus in your application's
803  * process before you can call dbus_shutdown(). One implication of this
804  * is that calling dbus_shutdown() from a library is almost certainly
805  * wrong, since you don't know what the rest of the app is up to.
806  * 
807  */
808 void
809 dbus_shutdown (void)
810 {
811   while (registered_globals != NULL)
812     {
813       ShutdownClosure *c;
814
815       c = registered_globals;
816       registered_globals = c->next;
817       
818       (* c->func) (c->data);
819       
820       dbus_free (c);
821     }
822
823   _dbus_current_generation += 1;
824 }
825
826 /** @} */ /** End of public API docs block */
827
828 #ifdef DBUS_BUILD_TESTS
829 #include "dbus-test.h"
830
831 /**
832  * @ingroup DBusMemoryInternals
833  * Unit test for DBusMemory
834  * @returns #TRUE on success.
835  */
836 dbus_bool_t
837 _dbus_memory_test (void)
838 {
839   dbus_bool_t old_guards;
840   void *p;
841   size_t size;
842
843   old_guards = guards;
844   guards = TRUE;
845   p = dbus_malloc (4);
846   if (p == NULL)
847     _dbus_assert_not_reached ("no memory");
848   for (size = 4; size < 256; size += 4)
849     {
850       p = dbus_realloc (p, size);
851       if (p == NULL)
852         _dbus_assert_not_reached ("no memory");
853     }
854   for (size = 256; size != 0; size -= 4)
855     {
856       p = dbus_realloc (p, size);
857       if (p == NULL)
858         _dbus_assert_not_reached ("no memory");
859     }
860   dbus_free (p);
861   guards = old_guards;
862   return TRUE;
863 }
864
865 #endif