diff options
Diffstat (limited to 'src/adlist.c')
-rw-r--r-- | src/adlist.c | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/adlist.c b/src/adlist.c index 2e811e02e..6bde46132 100644 --- a/src/adlist.c +++ b/src/adlist.c @@ -93,6 +93,14 @@ list *listAddNodeHead(list *list, void *value) if ((node = zmalloc(sizeof(*node))) == NULL) return NULL; node->value = value; + listLinkNodeHead(list, node); + return list; +} + +/* + * Add a node that has already been allocated to the head of list + */ +void listLinkNodeHead(list* list, listNode *node) { if (list->len == 0) { list->head = list->tail = node; node->prev = node->next = NULL; @@ -103,7 +111,6 @@ list *listAddNodeHead(list *list, void *value) list->head = node; } list->len++; - return list; } /* Add a new node to the list, to tail, containing the specified 'value' @@ -162,11 +169,20 @@ list *listInsertNode(list *list, listNode *old_node, void *value, int after) { } /* Remove the specified node from the specified list. - * It's up to the caller to free the private value of the node. + * The node is freed. If free callback is provided the value is freed as well. * * This function can't fail. */ void listDelNode(list *list, listNode *node) { + listUnlinkNode(list, node); + if (list->free) list->free(node->value); + zfree(node); +} + +/* + * Remove the specified node from the list without freeing it. + */ +void listUnlinkNode(list *list, listNode *node) { if (node->prev) node->prev->next = node->next; else @@ -175,8 +191,10 @@ void listDelNode(list *list, listNode *node) node->next->prev = node->prev; else list->tail = node->prev; - if (list->free) list->free(node->value); - zfree(node); + + node->next = NULL; + node->prev = NULL; + list->len--; } @@ -381,3 +399,12 @@ void listJoin(list *l, list *o) { o->head = o->tail = NULL; o->len = 0; } + +/* Initializes the node's value and sets its pointers + * so that it is initially not a member of any list. + */ +void listInitNode(listNode *node, void *value) { + node->prev = NULL; + node->next = NULL; + node->value = value; +} |