Merge branch 'more-auto-macros' into 'master'

Add g_autoqueue

See merge request GNOME/glib!474
This commit is contained in:
Philip Withnall 2019-05-08 20:19:43 +00:00
commit ae47f0d156
5 changed files with 100 additions and 0 deletions

View File

@ -419,6 +419,7 @@ g_autoptr
g_autofree
g_autolist
g_autoslist
g_autoqueue
G_DEFINE_AUTOPTR_CLEANUP_FUNC
G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC
G_DEFINE_AUTO_CLEANUP_FREE_FUNC

View File

@ -2407,6 +2407,35 @@
* Since: 2.56
*/
/**
* g_autoqueue:
* @TypeName: a supported variable type
*
* Helper to declare a double-ended queue variable with automatic deep cleanup.
*
* The queue 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 queues of a type with a cleanup
* function. The type of the variable is a `GQueue *`. 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.62
*/
/**
* G_DEFINE_AUTOPTR_CLEANUP_FUNC:
* @TypeName: a type name to define a g_autoptr() cleanup function for

View File

@ -957,12 +957,15 @@
#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_AUTOPTR_QUEUE_FUNC_NAME(TypeName) glib_queueautoptr_cleanup_##TypeName
#define _GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName) TypeName##_queueautoptr
#define _GLIB_AUTO_FUNC_NAME(TypeName) glib_auto_cleanup_##TypeName
#define _GLIB_CLEANUP(func) __attribute__((cleanup(func)))
#define _GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, ParentName, cleanup) \
typedef TypeName *_GLIB_AUTOPTR_TYPENAME(TypeName); \
typedef GList *_GLIB_AUTOPTR_LIST_TYPENAME(TypeName); \
typedef GSList *_GLIB_AUTOPTR_SLIST_TYPENAME(TypeName); \
typedef GQueue *_GLIB_AUTOPTR_QUEUE_TYPENAME(TypeName); \
G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_CLEAR_FUNC_NAME(TypeName) (TypeName *_ptr) \
{ if (_ptr) (cleanup) ((ParentName *) _ptr); } \
@ -972,6 +975,8 @@
{ g_list_free_full (*_l, (GDestroyNotify) (void(*)(void)) cleanup); } \
static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_SLIST_FUNC_NAME(TypeName) (GSList **_l) \
{ g_slist_free_full (*_l, (GDestroyNotify) (void(*)(void)) cleanup); } \
static G_GNUC_UNUSED inline void _GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName) (GQueue **_q) \
{ if (*_q) g_queue_free_full (*_q, (GDestroyNotify) (void(*)(void)) cleanup); } \
G_GNUC_END_IGNORE_DEPRECATIONS
#define _GLIB_DEFINE_AUTOPTR_CHAINUP(ModuleObjName, ParentName) \
_GLIB_DEFINE_AUTOPTR_CLEANUP_FUNCS(ModuleObjName, ParentName, _GLIB_AUTOPTR_CLEAR_FUNC_NAME(ParentName))
@ -991,6 +996,7 @@
#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_autoqueue(TypeName) _GLIB_CLEANUP(_GLIB_AUTOPTR_QUEUE_FUNC_NAME(TypeName)) _GLIB_AUTOPTR_QUEUE_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

@ -564,6 +564,35 @@ test_autoslist (void)
g_assert (freed2);
}
static void
test_autoqueue (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_autoqueue(GBytes) q = g_queue_new ();
g_queue_push_head (q, b1);
g_queue_push_tail (q, 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[])
{
@ -620,6 +649,7 @@ main (int argc, gchar *argv[])
g_test_add_func ("/autoptr/refstring", test_refstring);
g_test_add_func ("/autoptr/autolist", test_autolist);
g_test_add_func ("/autoptr/autoslist", test_autoslist);
g_test_add_func ("/autoptr/autoqueue", test_autoqueue);
return g_test_run ();
}

View File

@ -159,6 +159,39 @@ test_autoslist (void)
g_assert_null (tac3);
}
/* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
* autoqueue cleanup functions (defined using the ones of the base type declared
* with G_DECLARE_DERIVABLE_TYPE) and so that can be used with g_autoqueue, and
* that freeing the queue correctly unrefs the object too */
static void
test_autoqueue (void)
{
TestAutoCleanup *tac1 = test_auto_cleanup_new ();
TestAutoCleanup *tac2 = test_auto_cleanup_new ();
g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
{
g_autoqueue (TestAutoCleanup) q = g_queue_new ();
g_queue_push_head (q, tac1);
g_queue_push_tail (q, tac2);
}
/* Only assert if autoptr works */
#ifdef __GNUC__
g_assert_null (tac1);
g_assert_null (tac2);
#endif
g_assert_nonnull (tac3);
g_clear_object (&tac3);
g_assert_null (tac3);
}
int
main (int argc, gchar *argv[])
{
@ -168,6 +201,7 @@ main (int argc, gchar *argv[])
g_test_add_func ("/autoptr/autoptr_steal", test_autoptr_steal);
g_test_add_func ("/autoptr/autolist", test_autolist);
g_test_add_func ("/autoptr/autoslist", test_autoslist);
g_test_add_func ("/autoptr/autoqueue", test_autoqueue);
return g_test_run ();
}