[glib] Add support for g_auto[s]list(Type)



commit f49a93b20761a0be51b22c481503b4cda0f7264f
Author: Alexander Larsson <alexl redhat com>
Date:   Tue Dec 19 11:17:09 2017 +0100

    Add support for g_auto[s]list(Type)
    
    This lets you do g_autoptr style cleanup of GList that does deep freeing.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791342

 docs/reference/glib/glib-sections.txt |    2 +
 glib/docs.c                           |   56 +++++++++++++++++++++++++++
 glib/gmacros.h                        |   10 +++++
 glib/tests/autoptr.c                  |   67 +++++++++++++++++++++++++++++++++
 4 files changed, 135 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index 1aaaf60..dd1d5de 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -401,6 +401,8 @@ G_INLINE_FUNC
 g_auto
 g_autoptr
 g_autofree
+g_autolist
+g_autoslist
 G_DEFINE_AUTOPTR_CLEANUP_FUNC
 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC
 G_DEFINE_AUTO_CLEANUP_FREE_FUNC
diff --git a/glib/docs.c b/glib/docs.c
index d6ac4b3..b9c4245 100644
--- a/glib/docs.c
+++ b/glib/docs.c
@@ -2608,6 +2608,62 @@
  */
 
 /**
+ * g_autolist:
+ * @TypeName: a supported variable type
+ *
+ * Helper to declare a list variable with automatic deep cleanup.
+ *
+ * The list is deeply freed, in a way appropriate to the specified type, when the
+ * variable goes out of scope.  The type must support this.
+ *
+ * This feature is only supported on GCC and clang.  This macro is not
+ * defined on other compilers and should not be used in programs that
+ * are intended to be portable to those compilers.
+ *
+ * This is meant to be used to declare lists of a type with a cleanup
+ * function.  The type of the variable is a GList *.  You
+ * must not add your own '*'.
+ *
+ * This macro can be used to avoid having to do explicit cleanups of
+ * local variables when exiting functions.  It often vastly simplifies
+ * handling of error conditions, removing the need for various tricks
+ * such as 'goto out' or repeating of cleanup code.  It is also helpful
+ * for non-error cases.
+ *
+ * See also g_autoslist(), g_autoptr() and g_steal_pointer().
+ *
+ * Since: 2.56
+ */
+
+/**
+ * g_autoslist:
+ * @TypeName: a supported variable type
+ *
+ * Helper to declare a singly linked list variable with automatic deep cleanup.
+ *
+ * The list is deeply freed, in a way appropriate to the specified type, when the
+ * variable goes out of scope.  The type must support this.
+ *
+ * This feature is only supported on GCC and clang.  This macro is not
+ * defined on other compilers and should not be used in programs that
+ * are intended to be portable to those compilers.
+ *
+ * This is meant to be used to declare lists of a type with a cleanup
+ * function.  The type of the variable is a GSList *.  You
+ * must not add your own '*'.
+ *
+ * This macro can be used to avoid having to do explicit cleanups of
+ * local variables when exiting functions.  It often vastly simplifies
+ * handling of error conditions, removing the need for various tricks
+ * such as 'goto out' or repeating of cleanup code.  It is also helpful
+ * for non-error cases.
+ *
+ * See also g_autolist(), g_autoptr() and g_steal_pointer().
+ *
+ * Since: 2.56
+ */
+
+/**
  * G_DEFINE_AUTOPTR_CLEANUP_FUNC:
  * @TypeName: a type name to define a g_autoptr() cleanup function for
  * @func: the cleanup function
diff --git a/glib/gmacros.h b/glib/gmacros.h
index 4ef1436..3533677 100644
--- a/glib/gmacros.h
+++ b/glib/gmacros.h
@@ -438,6 +438,10 @@
 /* these macros are private */
 #define _GLIB_AUTOPTR_FUNC_NAME(TypeName) glib_autoptr_cleanup_##TypeName
 #define _GLIB_AUTOPTR_TYPENAME(TypeName)  TypeName##_autoptr
+#define _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) glib_listautoptr_cleanup_##TypeName
+#define _GLIB_AUTOPTR_LIST_TYPENAME(TypeName)  TypeName##_listautoptr
+#define _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) glib_slistautoptr_cleanup_##TypeName
+#define _GLIB_AUTOPTR_SLIST_TYPENAME(TypeName)  TypeName##_slistautoptr
 #define _GLIB_AUTO_FUNC_NAME(TypeName)    glib_auto_cleanup_##TypeName
 #define _GLIB_CLEANUP(func)               __attribute__((cleanup(func)))
 #define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \
@@ -449,8 +453,12 @@
 /* these macros are API */
 #define G_DEFINE_AUTOPTR_CLEANUP_FUNC(TypeName, func) \
   typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName);                                                        
   \
+  typedef GList *_GLIB_AUTOPTR_LIST_TYPENAME(TypeName);                                                      
   \
+  typedef GSList *_GLIB_AUTOPTR_SLIST_TYPENAME(TypeName);                                                    
     \
   G_GNUC_BEGIN_IGNORE_DEPRECATIONS                                                                           
   \
   static inline void _GLIB_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) { if (*_ptr) (func) (*_ptr); }      
   \
+  static inline void _GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName) (GList **_l) { g_list_free_full (*_l, 
(GDestroyNotify) func); } \
+  static inline void _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) (GSList **_l) { g_slist_free_full (*_l, 
(GDestroyNotify) func); } \
   G_GNUC_END_IGNORE_DEPRECATIONS
 #define G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(TypeName, func) \
   G_GNUC_BEGIN_IGNORE_DEPRECATIONS                                                                           
   \
@@ -461,6 +469,8 @@
   static inline void _GLIB_AUTO_FUNC_NAME(TypeName) (TypeName *_ptr) { if (*_ptr != none) (func) (*_ptr); }  
   \
   G_GNUC_END_IGNORE_DEPRECATIONS
 #define g_autoptr(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_TYPENAME(TypeName)
+#define g_autolist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_LIST_FUNC_NAME(TypeName)) 
_GLIB_AUTOPTR_LIST_TYPENAME(TypeName)
+#define g_autoslist(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName)) 
_GLIB_AUTOPTR_SLIST_TYPENAME(TypeName)
 #define g_auto(TypeName) _GLIB_CLEANUP(_GLIB_AUTO_FUNC_NAME(TypeName)) TypeName
 #define g_autofree _GLIB_CLEANUP(g_autoptr_cleanup_generic_gfree)
 
diff --git a/glib/tests/autoptr.c b/glib/tests/autoptr.c
index 754999c..ca6ae34 100644
--- a/glib/tests/autoptr.c
+++ b/glib/tests/autoptr.c
@@ -410,6 +410,71 @@ test_strv (void)
   g_assert (val != NULL);
 }
 
+static void
+mark_freed (gpointer ptr)
+{
+  gboolean *freed = ptr;
+  *freed = TRUE;
+}
+
+static void
+test_autolist (void)
+{
+  char data[1] = {0};
+  gboolean freed1 = FALSE;
+  gboolean freed2 = FALSE;
+  gboolean freed3 = FALSE;
+  GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
+  GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
+  GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
+
+  {
+    g_autolist(GBytes) l = NULL;
+
+    l = g_list_prepend (l, b1);
+    l = g_list_prepend (l, b3);
+  }
+
+  /* Only assert if autoptr works */
+#ifdef __GNUC__
+  g_assert (freed1);
+  g_assert (freed3);
+#endif
+  g_assert (!freed2);
+
+  g_bytes_unref (b2);
+  g_assert (freed2);
+}
+
+static void
+test_autoslist (void)
+{
+  char data[1] = {0};
+  gboolean freed1 = FALSE;
+  gboolean freed2 = FALSE;
+  gboolean freed3 = FALSE;
+  GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
+  GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
+  GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
+
+  {
+    g_autoslist(GBytes) l = NULL;
+
+    l = g_slist_prepend (l, b1);
+    l = g_slist_prepend (l, b3);
+  }
+
+  /* Only assert if autoptr works */
+#ifdef __GNUC__
+  g_assert (freed1);
+  g_assert (freed3);
+#endif
+  g_assert (!freed2);
+
+  g_bytes_unref (b2);
+  g_assert (freed2);
+}
+
 int
 main (int argc, gchar *argv[])
 {
@@ -462,6 +527,8 @@ main (int argc, gchar *argv[])
   g_test_add_func ("/autoptr/g_variant_dict", test_g_variant_dict);
   g_test_add_func ("/autoptr/g_variant_type", test_g_variant_type);
   g_test_add_func ("/autoptr/strv", test_strv);
+  g_test_add_func ("/autoptr/autolist", test_autolist);
+  g_test_add_func ("/autoptr/autoslist", test_autoslist);
 
   return g_test_run ();
 }


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