[libgsystem] localalloc: Declare the cleanup functions inline in the header



commit aaa4e8623c578a1cf385f615ebd9713b5c18afa0
Author: Christian Persch <chpe gnome org>
Date:   Wed Oct 23 13:16:48 2013 +0200

    localalloc: Declare the cleanup functions inline in the header
    
    This enables gcc with -Wuninitialized to warn if the cleanup function is used
    on an uninitialised variable.

 gsystem-local-alloc.c |   84 -------------------------------------------------
 gsystem-local-alloc.h |   38 +++++++++++++++-------
 2 files changed, 26 insertions(+), 96 deletions(-)
---
diff --git a/gsystem-local-alloc.c b/gsystem-local-alloc.c
index 3879b00..add3fcb 100644
--- a/gsystem-local-alloc.c
+++ b/gsystem-local-alloc.c
@@ -70,87 +70,3 @@
  * </example>
  *
  */
-
-void
-gs_local_free (void *loc)
-{
-  void **location = loc;
-  if (location)
-    g_free (*location);
-}
-
-#define _gs_local_free(type, function) do {           \
-    void **location = loc;                            \
-    if (location)                                     \
-      {                                               \
-        type value = *location;                      \
-        if (value)                                    \
-          function (value);                           \
-      }                                               \
-  } while (0)
-
-void
-gs_local_obj_unref (void *loc)
-{
-  _gs_local_free(GObject*, g_object_unref);
-}
-
-void
-gs_local_variant_unref (void *loc)
-{
-  _gs_local_free(GVariant*, g_variant_unref);
-}
-
-void
-gs_local_variant_iter_free (void *loc)
-{
-  _gs_local_free(GVariantIter*, g_variant_iter_free);
-}
-
-void
-gs_local_variant_builder_unref (void *loc)
-{
-  _gs_local_free(GVariantBuilder*, g_variant_builder_unref);
-}
-
-void
-gs_local_ptrarray_unref (void *loc)
-{
-  _gs_local_free(GPtrArray*, g_ptr_array_unref);
-}
-
-void
-gs_local_array_unref (void *loc)
-{
-  _gs_local_free(GArray*, g_array_unref);
-}
-
-void
-gs_local_hashtable_unref (void *loc)
-{
-  _gs_local_free(GHashTable*, g_hash_table_unref);
-}
-
-void
-gs_local_checksum_free (void *loc)
-{
-  _gs_local_free(GChecksum*, g_checksum_free);
-}
-
-void
-gs_local_bytes_unref (void *loc)
-{
-  _gs_local_free(GBytes*, g_bytes_unref);
-}
-
-void
-gs_local_strfreev (void *loc)
-{
-  _gs_local_free(char **, g_strfreev);
-}
-
-void
-gs_local_free_error (void *loc)
-{
-  _gs_local_free(GError*, g_error_free);
-}
diff --git a/gsystem-local-alloc.h b/gsystem-local-alloc.h
index 150748c..34db297 100644
--- a/gsystem-local-alloc.h
+++ b/gsystem-local-alloc.h
@@ -25,23 +25,37 @@
 
 G_BEGIN_DECLS
 
+#define GS_DEFINE_CLEANUP_FUNCTION(Type, name, func) \
+  static inline void name (void *v) \
+  { \
+    func (*(Type*)v); \
+  }
+
+#define GS_DEFINE_CLEANUP_FUNCTION0(Type, name, func) \
+  static inline void name (void *v) \
+  { \
+    if (*(Type*)v) \
+      func (*(Type*)v); \
+  }
+
 /* These functions shouldn't be invoked directly;
  * they are stubs that:
  * 1) Take a pointer to the location (typically itself a pointer).
  * 2) Provide %NULL-safety where it doesn't exist already (e.g. g_object_unref)
  */
-void gs_local_free (void *loc);
-void gs_local_obj_unref (void *loc);
-void gs_local_variant_unref (void *loc);
-void gs_local_variant_iter_free (void *loc);
-void gs_local_variant_builder_unref (void *loc);
-void gs_local_array_unref (void *loc);
-void gs_local_ptrarray_unref (void *loc);
-void gs_local_hashtable_unref (void *loc);
-void gs_local_checksum_free (void *loc);
-void gs_local_bytes_unref (void *loc);
-void gs_local_strfreev (void *loc);
-void gs_local_free_error (void *loc);
+GS_DEFINE_CLEANUP_FUNCTION0(GArray*, gs_local_array_unref, g_array_unref)
+GS_DEFINE_CLEANUP_FUNCTION0(GBytes*, gs_local_bytes_unref, g_bytes_unref)
+GS_DEFINE_CLEANUP_FUNCTION0(GChecksum*, gs_local_checksum_free, g_checksum_free)
+GS_DEFINE_CLEANUP_FUNCTION0(GError*, gs_local_free_error, g_error_free)
+GS_DEFINE_CLEANUP_FUNCTION0(GHashTable*, gs_local_hashtable_unref, g_hash_table_unref)
+GS_DEFINE_CLEANUP_FUNCTION0(GObject*, gs_local_obj_unref, g_object_unref)
+GS_DEFINE_CLEANUP_FUNCTION0(GPtrArray*, gs_local_ptrarray_unref, g_ptr_array_unref)
+GS_DEFINE_CLEANUP_FUNCTION0(GVariant*, gs_local_variant_unref, g_variant_unref)
+GS_DEFINE_CLEANUP_FUNCTION0(GVariantBuilder*, gs_local_variant_builder_unref, g_variant_builder_unref)
+GS_DEFINE_CLEANUP_FUNCTION0(GVariantIter*, gs_local_variant_iter_free, g_variant_iter_free)
+
+GS_DEFINE_CLEANUP_FUNCTION(char**, gs_local_strfreev, g_strfreev)
+GS_DEFINE_CLEANUP_FUNCTION(void*, gs_local_free, g_free)
 
 /**
  * gs_free:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]