X-Git-Url: https://tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=src%2Flist.c;h=27494c898ae5ed2a31fa5e488aa844e3a4aa8ddc;hp=0bbc5d42aeb23563f947cfe10c4c3d5654556ff9;hb=f6e87ab476a0faf8b124ecaaa27f967d825e6457;hpb=3a316823b971396a428f020f401b9fe41252d98d diff --git a/src/list.c b/src/list.c index 0bbc5d42..27494c89 100644 --- a/src/list.c +++ b/src/list.c @@ -41,8 +41,9 @@ list_node_t *list_alloc_node(void) { } void list_free_node(list_t *list, list_node_t *node) { - if(node->data && list->delete) + if(node->data && list->delete) { list->delete(node->data); + } free(node); } @@ -57,10 +58,11 @@ list_node_t *list_insert_head(list_t *list, void *data) { node->next = list->head; list->head = node; - if(node->next) + if(node->next) { node->next->prev = node; - else + } else { list->tail = node; + } list->count++; @@ -75,10 +77,11 @@ list_node_t *list_insert_tail(list_t *list, void *data) { node->prev = list->tail; list->tail = node; - if(node->prev) + if(node->prev) { node->prev->next = node; - else + } else { list->head = node; + } list->count++; @@ -93,10 +96,11 @@ list_node_t *list_insert_after(list_t *list, list_node_t *after, void *data) { node->prev = after; after->next = node; - if(node->next) + if(node->next) { node->next->prev = node; - else + } else { list->tail = node; + } list->count++; @@ -113,10 +117,11 @@ list_node_t *list_insert_before(list_t *list, list_node_t *before, void *data) { node->prev = before->prev; before->prev = node; - if(node->prev) + if(node->prev) { node->prev->next = node; - else + } else { list->head = node; + } list->count++; @@ -124,15 +129,17 @@ list_node_t *list_insert_before(list_t *list, list_node_t *before, void *data) { } void list_unlink_node(list_t *list, list_node_t *node) { - if(node->prev) + if(node->prev) { node->prev->next = node->next; - else + } else { list->head = node->next; + } - if(node->next) + if(node->next) { node->next->prev = node->prev; - else + } else { list->tail = node->prev; + } list->count--; } @@ -152,31 +159,35 @@ void list_delete_tail(list_t *list) { void list_delete(list_t *list, const void *data) { for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) - if(node->data == data) + if(node->data == data) { list_delete_node(list, node); + } } /* Head/tail lookup */ void *list_get_head(list_t *list) { - if(list->head) + if(list->head) { return list->head->data; - else + } else { return NULL; + } } void *list_get_tail(list_t *list) { - if(list->tail) + if(list->tail) { return list->tail->data; - else + } else { return NULL; + } } /* Fast list deletion */ void list_delete_list(list_t *list) { - for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) + for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) { list_free_node(list, node); + } list_free(list); } @@ -184,12 +195,14 @@ void list_delete_list(list_t *list) { /* Traversing */ void list_foreach_node(list_t *list, list_action_node_t action) { - for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) + for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) { action(node); + } } void list_foreach(list_t *list, list_action_t action) { for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) - if(node->data) + if(node->data) { action(node->data); + } }