diff --git a/cunit/test_list.c b/cunit/test_list.c index 13942917a..c0b375c84 100644 --- a/cunit/test_list.c +++ b/cunit/test_list.c @@ -62,6 +62,8 @@ void test_list(void) { struct my_list* list; struct my_list_item* item; + struct my_list_item* item1; + struct my_list_item* item2; int i; list = my_list_new(); @@ -81,5 +83,16 @@ void test_list(void) /*printf("%d %d\n", item->a, item->b);*/ } + item1 = my_list_item_new(); + my_list_add(list, item1); + item2 = my_list_item_new(); + my_list_add(list, item2); + + CU_ASSERT(my_list_remove(list, item1) == item1); + my_list_item_free(item1); + CU_ASSERT(my_list_remove(list, item2) == item2); + CU_ASSERT(my_list_remove(list, item2) == NULL); + my_list_item_free(item2); + my_list_free(list); } diff --git a/include/freerdp/utils/list.h b/include/freerdp/utils/list.h index c08f4566d..cdc33666f 100644 --- a/include/freerdp/utils/list.h +++ b/include/freerdp/utils/list.h @@ -94,6 +94,32 @@ static struct _item_type* _list_type##_dequeue(struct _list_type* list) \ return item; \ } \ \ +static void _list_type##_add(struct _list_type* list, struct _item_type* item) \ +{ \ + _list_type##_enqueue(list, item); \ +} \ +\ +static struct _item_type* _list_type##_remove(struct _list_type* list, struct _item_type* item) \ +{ \ + struct _item_type* prev; \ + struct _item_type* curr; \ +\ + for (prev = NULL, curr = (struct _item_type*)list->head; curr; prev = curr, curr = ((struct _item_type##_full*)curr)->next) \ + { \ + if (curr == item) \ + { \ + if (prev) \ + ((struct _item_type##_full*)prev)->next = ((struct _item_type##_full*)curr)->next; \ + if (list->head == item) \ + list->head = ((struct _item_type##_full*)curr)->next; \ + if (list->tail == item) \ + list->tail = prev; \ + return item; \ + } \ + } \ + return NULL; \ +} \ +\ void _list_type##_free(struct _list_type* list) \ { \ struct _item_type* item; \