From f49a93b20761a0be51b22c481503b4cda0f7264f Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Tue, 19 Dec 2017 11:17:09 +0100 Subject: [PATCH] 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(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 1aaaf6043..dd1d5dea6 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 d6ac4b392..b9c42454a 100644 --- a/glib/docs.c +++ b/glib/docs.c @@ -2607,6 +2607,62 @@ * Since: 2.44 */ +/** + * 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 diff --git a/glib/gmacros.h b/glib/gmacros.h index 4ef143626..353367764 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 754999cf1..ca6ae3464 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 (); }