core/containers/queue: Add _q_clear() function

This is used to clean up the items on a queue, and should be called to
prevent memory leaks.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-01-25 08:08:28 -05:00
parent 73ea514c27
commit b586461104
2 changed files with 16 additions and 2 deletions

View File

@ -89,6 +89,12 @@ static inline guint _q_add_tail(struct _queue *queue, gpointer data)
/* Called to add an item to a sorted queue. */
guint _q_add_sorted(struct _queue *, gpointer, GCompareDataFunc, gpointer);
/* Called to remove all items from a queue */
static inline void _q_clear(struct _queue *queue)
{
g_queue_clear(&queue->_queue);
}
/* Called to remove an item by iterator. */
gpointer _q_remove_it(struct _queue *, struct _q_iter *);

View File

@ -88,13 +88,21 @@ static void test_stress(unsigned int N)
} test_loop_passed();
test_equal(_q_size(&queue), N);
/* _q_remove_it() (remaining items) */
for (i = 0; i < N; i++) {
/* _q_remove_it() (half of remaining items) */
for (i = 0; i < (N / 2); i++) {
_q_iter_set(&queue, &it, 0);
test_loop_not_equal((void *)it.it_iter, NULL, i);
test_loop_equal(it.it_pos, 0, i);
test_loop_equal(GPOINTER_TO_INT(_q_remove_it(&queue, &it)), N - (i + 1), i);
} test_loop_passed();
test_equal(_q_size(&queue), N / 2);
/* _q_clear() (remaining items) */
_q_clear(&queue);
test_equal(_q_size(&queue), 0);
test_equal((void *)queue._queue.head, NULL);
test_equal((void *)queue._queue.tail, NULL);
test_equal(queue._queue.length, 0);
}
static void test_basics() { test_stress(10); }