From 077e20664c8a1982fda3b21a3fb58e16fac325c5 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Sat, 7 Jan 2017 07:14:43 -0500 Subject: Improve the implementation of a couple linked list routines. ll_insert_next() and ll_remove_next() can be done more elegantly by adding a level of indirection to reference the 'next' pointer. Change-Id: If3ab2bc2a659b517c793749cfa9088938ae08d0d --- firmware/common/linked_list.c | 49 ++++++++++++------------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) (limited to 'firmware') diff --git a/firmware/common/linked_list.c b/firmware/common/linked_list.c index 006caacc91..a5b3de3cf7 100644 --- a/firmware/common/linked_list.c +++ b/firmware/common/linked_list.c @@ -60,26 +60,14 @@ void ll_init(struct ll_head *list) void ll_insert_next(struct ll_head *list, struct ll_node *node, struct ll_node *newnode) { - if (node == NULL) - { - node = list->head; - - newnode->next = node; - list->head = newnode; - - if (node == NULL) - list->tail = node; - } - else - { - struct ll_node *next = node->next; + struct ll_node **nodep = node != NULL ? &node->next : &list->head; + struct ll_node *next = *nodep; - newnode->next = next; - node->next = newnode; + newnode->next = next; + *nodep = newnode; - if (next == NULL) - list->tail = newnode; - } + if (next == NULL) + list->tail = newnode; } /** @@ -117,27 +105,16 @@ void ll_insert_last(struct ll_head *list, struct ll_node *node) */ void ll_remove_next(struct ll_head *list, struct ll_node *node) { - if (node == NULL) - { - node = list->head->next; - - list->head = node; + struct ll_node **nodep = node != NULL ? &node->next : &list->head; + struct ll_node *next = *nodep; - if (node == NULL) - list->tail = NULL; - } - else + if (next != NULL) { - struct ll_node *next = node->next; - - if (next != NULL) - { - next = next->next; - node->next = next; + next = next->next; + *nodep = next; - if (!next) - list->tail = node; - } + if (next == NULL) + list->tail = node; } } -- cgit v1.2.3