2 * Copyright (C) 2006-2009 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
13 /* Private helper functions. */
14 static void list_free_helper_(struct list *list, int free_data_flag) {
15 struct list_node *node, *next;
20 for (node = list->first; node; node = next) {
29 /* Public functions. */
31 /* allocates and initialises list structure; returns NULL if malloc fails */
32 struct list *list_create() {
33 struct list *list = malloc(sizeof(struct list));
35 memset(list, 0, sizeof(struct list));
39 /* frees all memory associated with the list
40 note that the data pointed at from each node is also freed
41 use list_free() to free only the memory used by the list itself */
42 void list_destroy(struct list *list) {
43 list_free_helper_(list, 1);
46 /* frees the meory used by the list itself
47 note that the data pointed at from each node is not freed
48 use list_destroy() to free all the data associated with the list */
49 void list_free(struct list *list) {
50 list_free_helper_(list, 0);
53 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
54 int list_push(struct list *list, void *data) {
55 struct list_node *node;
57 node = malloc(sizeof(struct list_node));
65 list->last->next = node;
74 /* removes first entry from list and returns data */
75 void *list_shift(struct list *list) {
76 struct list_node *node;
79 if (!list || !list->first)
83 list->first = node->next;
92 /* removes all entries with matching data pointer */
93 void list_removedata(struct list *list, void *data) {
94 struct list_node *node, *t;
96 if (!list || !list->first)
100 while (node->data == data) {
101 list->first = node->next;
110 for (; node->next; node = node->next)
111 if (node->next->data == data) {
113 node->next = t->next;
116 if (!node->next) { /* we removed the last one */
123 /* returns first node */
124 struct list_node *list_first(struct list *list) {
125 return list ? list->first : NULL;
128 /* returns the next node after the argument */
129 struct list_node *list_next(struct list_node *node) {
133 /* returns number of nodes */
134 uint32_t list_count(struct list *list) {
138 /* Local Variables: */
139 /* c-file-style: "stroustrup" */