diff --git a/glib/garraylist.c b/glib/garraylist.c index 945666f8c..e177c9a29 100644 --- a/glib/garraylist.c +++ b/glib/garraylist.c @@ -500,3 +500,30 @@ g_array_list_last_link (GArrayList *self) return &items [self->len - 1]; } + +void +g_array_list_clear (GArrayList *self) +{ + GArrayListAny *any = (GArrayListAny *)self; + GArrayListEmbed *embed = (GArrayListEmbed *)self; + GArrayListAlloc *alloc = (GArrayListAlloc *)self; + + g_return_if_fail (self != NULL); + + if (any->destroy != NULL) + { + GList *items; + gsize i; + + items = (any->mode == MODE_EMBED) ? embed->items : alloc->items; + + for (i = 0; i < any->len; i++) + any->destroy (items [i].data); + } + + if (any->mode == MODE_ALLOC) + g_free (alloc->items); + + any->len = 0; + any->mode = MODE_EMBED; +} diff --git a/glib/garraylist.h b/glib/garraylist.h index cd2b5f6c6..af0b234ea 100644 --- a/glib/garraylist.h +++ b/glib/garraylist.h @@ -78,6 +78,10 @@ GLIB_AVAILABLE_IN_2_46 void g_array_list_prepend (GArrayList *list, gpointer data); +GLIB_AVAILABLE_IN_2_46 +void g_array_list_clear (GArrayList *list); + +#define g_array_list_empty(list) ((list)->len == 0) #define g_array_list_first(list) (((list)->len == 0) ? NULL : g_array_list_index((list),0)) #define g_array_list_last(list) (((list)->len == 0) ? NULL : g_array_list_index((list),(list)->len-1)) diff --git a/glib/tests/arraylist.c b/glib/tests/arraylist.c index 328088d07..d8ef39add 100644 --- a/glib/tests/arraylist.c +++ b/glib/tests/arraylist.c @@ -77,9 +77,12 @@ test_basic (GArrayList *al) g_array_list_prepend (al, GSIZE_TO_POINTER (191919)); g_assert_cmpint (GPOINTER_TO_SIZE (g_array_list_index (al, 0)), ==, 191919); - g_array_list_destroy (al); + g_array_list_clear (al); + g_assert_cmpint (al->len, ==, 0); g_assert_cmpint (test_basic_counter, ==, 1000); + g_array_list_destroy (al); + test_basic_counter = 0; }