static void * list_node_destroy(List l, ListNode *pp) { /* Removes the node pointed to by (*pp) from from list (l), * where (pp) is the address of the previous node's "next" ptr. * Returns the data ptr associated with list item being removed, * or NULL if (*pp) points to the NULL element. * This routine assumes the list is already locked upon entry. */ void *v; ListNode p; ListIterator i; assert(l != NULL); assert(l->magic == LIST_MAGIC); assert(pp != NULL); if (!(p = *pp)) return(NULL); v = p->data; if (!(*pp = p->next)) l->tail = pp; l->count--; for (i=l->iNext; i; i=i->iNext) { assert(i->magic == LIST_MAGIC); if (i->pos == p) i->pos = p->next, i->prev = pp; else if (i->prev == &p->next) i->prev = pp; assert((i->pos == *i->prev) || (i->pos == (*i->prev)->next)); } list_node_free(p); return(v); }
list_node_t* list_remove(list_t* list, list_node_t* node) { if (!list || list->size == 0 || !node || node == list->head) { return NULL; } list_node_t* curr, *prev; for (prev = list->head, curr = list->head->next; curr != list->head; prev = curr, curr = curr->next) { if (curr == node) { break; } } if (curr == list->head) { return NULL; } // curr == node at this point prev->next = curr->next; list->size--; list_node_free(curr); return prev->next; }
void list_destroy(List l) { ListIterator i, iTmp; ListNode p, pTmp; assert(l != NULL); list_mutex_lock(&l->mutex); assert(l->magic == LIST_MAGIC); i = l->iNext; while (i) { assert(i->magic == LIST_MAGIC); iTmp = i->iNext; assert(i->magic = 1); /* clear magic via assert abuse */ list_iterator_free(i); i = iTmp; } p = l->head; while (p) { pTmp = p->next; if (p->data && l->fDel) l->fDel(p->data); list_node_free(p); p = pTmp; } assert(l->magic = 1); /* clear magic via assert abuse */ list_mutex_unlock(&l->mutex); list_mutex_destroy(&l->mutex); list_free(l); return; }
/* list_node_destroy() * * Removes the node pointed to by [*pp] from from list [l], * where [pp] is the address of the previous node's "next" ptr. * Returns the data ptr associated with list item being removed, * or NULL if [*pp] points to the NULL element. * This routine assumes the list is already locked upon entry. */ static void * list_node_destroy (List l, ListNode *pp) { void *v; ListNode p; ListIterator i; assert(l != NULL); assert(l->magic == LIST_MAGIC); assert(_list_mutex_is_locked(&l->mutex)); assert(pp != NULL); if (!(p = *pp)) return NULL; v = p->data; if (!(*pp = p->next)) l->tail = pp; l->count--; for (i = l->iNext; i; i = i->iNext) { assert(i->magic == LIST_MAGIC); if (i->pos == p) i->pos = p->next, i->prev = pp; else if (i->prev == &p->next) i->prev = pp; assert((i->pos == *i->prev) || ((*i->prev) && (i->pos == (*i->prev)->next))); } list_node_free(p); return v; }
int main(void) { list_node_t *node = NULL; node = list_node_alloc(); node->data = "Wohoo!"; printf("node->data: \"%s\"\n", (char *)node->data); node = list_node_free(node); /* Allocate again, should be the same struct but zeroed. */ node = list_node_alloc(); printf("node->data: \"%s\"\n", (char *)node->data); /* Should segfault! */ node = list_node_free(node); return 0; }
void list_node_free(list_node_t *node_ptr) { if (node_ptr != NULL) { free(node_ptr->dataPtr); list_node_free(node_ptr->next); node_ptr->next = NULL; free(node_ptr); } }
void list_free(list_t *list_ptr) { list_node_t *node_ptr = list_ptr->head; list_node_free(node_ptr); list_ptr->head = NULL; list_ptr->size = 0; free(list_ptr); }
/* * Free a list structure */ void list_free(list * l) { assert(l); while (l->first != NULL) list_node_free(l, l->first); l->last = NULL; free(l); l = NULL; }
void list_free(list_t* list) { if (!list) { return; } int size = list->size, i = 0; list_node_t* curr = list->head, *next = list->head->next; for (; i < size; curr = next, next = next->next) { list_node_free(curr); } }