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
This commit is contained in:
Alexander Larsson 2017-12-19 11:17:09 +01:00
parent bf0be21208
commit f49a93b207
4 changed files with 135 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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 ();
}