[rename] renamed kdbus related macros
[platform/upstream/dbus.git] / dbus / dbus-memory.c
index a033b54..d59e8f8 100644 (file)
@@ -26,6 +26,7 @@
 #include "dbus-internals.h"
 #include "dbus-sysdeps.h"
 #include "dbus-list.h"
+#include "dbus-threads.h"
 #include <stdlib.h>
 
 /**
@@ -96,7 +97,7 @@
  * @{
  */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 static dbus_bool_t debug_initialized = FALSE;
 static int fail_nth = -1;
 static size_t fail_size = 0;
@@ -239,7 +240,7 @@ _dbus_get_fail_alloc_failures (void)
   return n_failures_per_failure;
 }
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 /**
  * Called when about to alloc some memory; if
  * it returns #TRUE, then the allocation should
@@ -293,7 +294,7 @@ _dbus_decrement_fail_alloc_counter (void)
       return FALSE;
     }
 }
-#endif /* DBUS_BUILD_TESTS */
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
 
 /**
  * Get the number of outstanding malloc()'d blocks.
@@ -459,7 +460,7 @@ set_guards (void       *real_block,
 void*
 dbus_malloc (size_t bytes)
 {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   _dbus_initialize_malloc_debug ();
   
   if (_dbus_decrement_fail_alloc_counter ())
@@ -471,7 +472,7 @@ dbus_malloc (size_t bytes)
 
   if (bytes == 0) /* some system mallocs handle this, some don't */
     return NULL;
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   else if (fail_size != 0 && bytes > fail_size)
     return NULL;
   else if (guards)
@@ -498,7 +499,7 @@ dbus_malloc (size_t bytes)
       void *mem;
       mem = malloc (bytes);
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (mem)
         {
           _dbus_atomic_inc (&n_blocks_outstanding);
@@ -529,7 +530,7 @@ dbus_malloc (size_t bytes)
 void*
 dbus_malloc0 (size_t bytes)
 {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   _dbus_initialize_malloc_debug ();
   
   if (_dbus_decrement_fail_alloc_counter ())
@@ -542,7 +543,7 @@ dbus_malloc0 (size_t bytes)
   
   if (bytes == 0)
     return NULL;
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   else if (fail_size != 0 && bytes > fail_size)
     return NULL;
   else if (guards)
@@ -570,7 +571,7 @@ dbus_malloc0 (size_t bytes)
       void *mem;
       mem = calloc (bytes, 1);
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (mem)
         {
           _dbus_atomic_inc (&n_blocks_outstanding);
@@ -600,7 +601,7 @@ void*
 dbus_realloc (void  *memory,
               size_t bytes)
 {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   _dbus_initialize_malloc_debug ();
   
   if (_dbus_decrement_fail_alloc_counter ())
@@ -616,7 +617,7 @@ dbus_realloc (void  *memory,
       dbus_free (memory);
       return NULL;
     }
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   else if (fail_size != 0 && bytes > fail_size)
     return NULL;
   else if (guards)
@@ -676,7 +677,7 @@ dbus_realloc (void  *memory,
       void *mem;
       mem = realloc (memory, bytes);
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
       if (mem == NULL && malloc_cannot_fail)
         {
           _dbus_warn ("out of memory: malloc (%ld)\n", (long) bytes);
@@ -699,7 +700,7 @@ dbus_realloc (void  *memory,
 void
 dbus_free (void  *memory)
 {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
   if (guards)
     {
       check_guards (memory, TRUE);
@@ -723,7 +724,7 @@ dbus_free (void  *memory)
     
   if (memory) /* we guarantee it's safe to free (NULL) */
     {
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #ifdef DBUS_DISABLE_ASSERT
       _dbus_atomic_dec (&n_blocks_outstanding);
 #else
@@ -794,7 +795,7 @@ struct ShutdownClosure
   void *data;                /**< Data for function */
 };
 
-_DBUS_DEFINE_GLOBAL_LOCK (shutdown_funcs);
+/* Protected by _DBUS_LOCK (shutdown_funcs) */
 static ShutdownClosure *registered_globals = NULL;
 
 /**
@@ -809,6 +810,20 @@ dbus_bool_t
 _dbus_register_shutdown_func (DBusShutdownFunction  func,
                               void                 *data)
 {
+  dbus_bool_t ok;
+
+  if (!_DBUS_LOCK (shutdown_funcs))
+    return FALSE;
+
+  ok = _dbus_register_shutdown_func_unlocked (func, data);
+  _DBUS_UNLOCK (shutdown_funcs);
+  return ok;
+}
+
+dbus_bool_t
+_dbus_register_shutdown_func_unlocked (DBusShutdownFunction  func,
+                                       void                 *data)
+{
   ShutdownClosure *c;
 
   c = dbus_new (ShutdownClosure, 1);
@@ -819,13 +834,9 @@ _dbus_register_shutdown_func (DBusShutdownFunction  func,
   c->func = func;
   c->data = data;
 
-  _DBUS_LOCK (shutdown_funcs);
-  
   c->next = registered_globals;
   registered_globals = c;
 
-  _DBUS_UNLOCK (shutdown_funcs);
-  
   return TRUE;
 }
 
@@ -890,12 +901,18 @@ dbus_shutdown (void)
       dbus_free (c);
     }
 
+  /* We wrap this in the thread-initialization lock because
+   * dbus_threads_init() uses the current generation to tell whether
+   * we're initialized, so we need to make sure that un-initializing
+   * propagates into all threads. */
+  _dbus_threads_lock_platform_specific ();
   _dbus_current_generation += 1;
+  _dbus_threads_unlock_platform_specific ();
 }
 
 /** @} */ /** End of public API docs block */
 
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
 #include "dbus-test.h"
 
 /**