Commit 0d3fe5c3 authored by Philip Withnall's avatar Philip Withnall

Merge branch 'queue-clear' into 'master'

Add g_queue_clear_full API

Closes #1464

See merge request !537
parents 615f8b60 2dc82840
Pipeline #49979 failed with stages
in 10 minutes and 51 seconds
......@@ -2363,6 +2363,7 @@ g_queue_free_full
G_QUEUE_INIT
g_queue_init
g_queue_clear
g_queue_clear_full
g_queue_is_empty
g_queue_get_length
g_queue_reverse
......
......@@ -149,6 +149,28 @@ g_queue_clear (GQueue *queue)
g_queue_init (queue);
}
/**
* g_queue_clear_full:
* @queue: a pointer to a #GQueue
* @free_func: (nullable): the function to be called to free memory allocated
*
* Convenience method, which frees all the memory used by a #GQueue,
* and calls the provided @free_func on each item in the #GQueue.
*
* Since: 2.60
*/
void
g_queue_clear_full (GQueue *queue,
GDestroyNotify free_func)
{
g_return_if_fail (queue != NULL);
if (free_func != NULL)
g_queue_foreach (queue, (GFunc) free_func, NULL);
g_queue_clear (queue);
}
/**
* g_queue_is_empty:
* @queue: a #GQueue.
......
......@@ -82,6 +82,9 @@ GLIB_AVAILABLE_IN_ALL
void g_queue_clear (GQueue *queue);
GLIB_AVAILABLE_IN_ALL
gboolean g_queue_is_empty (GQueue *queue);
GLIB_AVAILABLE_IN_2_60
void g_queue_clear_full (GQueue *queue,
GDestroyNotify free_func);
GLIB_AVAILABLE_IN_ALL
guint g_queue_get_length (GQueue *queue);
GLIB_AVAILABLE_IN_ALL
......
......@@ -1058,6 +1058,43 @@ new_item (int x)
return item;
}
static void
test_clear_full (void)
{
QueueItem *one, *two, *three, *four;
GQueue *queue;
queue = g_queue_new ();
g_queue_push_tail (queue, one = new_item (1));
g_queue_push_tail (queue, two = new_item (2));
g_queue_push_tail (queue, three = new_item (3));
g_queue_push_tail (queue, four = new_item (4));
g_assert_cmpint (g_queue_get_length (queue), ==, 4);
g_assert_false (one->freed);
g_assert_false (two->freed);
g_assert_false (three->freed);
g_assert_false (four->freed);
g_queue_clear_full (queue, free_func);
g_assert_true (one->freed);
g_assert_true (two->freed);
g_assert_true (three->freed);
g_assert_true (four->freed);
check_integrity (queue);
g_slice_free (QueueItem, one);
g_slice_free (QueueItem, two);
g_slice_free (QueueItem, three);
g_slice_free (QueueItem, four);
g_assert_true (g_queue_is_empty (queue));
g_queue_free (queue);
}
static void
test_free_full (void)
{
......@@ -1095,6 +1132,7 @@ int main (int argc, char *argv[])
g_test_add_func ("/queue/static", test_static);
g_test_add_func ("/queue/clear", test_clear);
g_test_add_func ("/queue/free-full", test_free_full);
g_test_add_func ("/queue/clear-full", test_clear_full);
seed = g_test_rand_int_range (0, G_MAXINT);
path = g_strdup_printf ("/queue/random/seed:%u", seed);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment