1 /* Copyright (c) 2007-2009, UNINETT AS */
2 /* See LICENSE for licensing information. */
8 /* Private helper functions. */
9 static void list_free_helper_(struct list *list, int free_data_flag) {
10 struct list_node *node, *next;
15 for (node = list->first; node; node = next) {
24 /* Public functions. */
26 /* allocates and initialises list structure; returns NULL if malloc fails */
27 struct list *list_create() {
28 struct list *list = malloc(sizeof(struct list));
30 memset(list, 0, sizeof(struct list));
34 /* frees all memory associated with the list
35 note that the data pointed at from each node is also freed
36 use list_free() to free only the memory used by the list itself */
37 void list_destroy(struct list *list) {
38 list_free_helper_(list, 1);
41 /* frees the meory used by the list itself
42 note that the data pointed at from each node is not freed
43 use list_destroy() to free all the data associated with the list */
44 void list_free(struct list *list) {
45 list_free_helper_(list, 0);
48 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
49 int list_push(struct list *list, void *data) {
50 struct list_node *node;
52 node = malloc(sizeof(struct list_node));
60 list->last->next = node;
69 /* removes first entry from list and returns data */
70 void *list_shift(struct list *list) {
71 struct list_node *node;
74 if (!list || !list->first)
78 list->first = node->next;
87 /* removes all entries with matching data pointer */
88 void list_removedata(struct list *list, void *data) {
89 struct list_node *node, *t;
91 if (!list || !list->first)
95 while (node->data == data) {
96 list->first = node->next;
105 for (; node->next; node = node->next)
106 if (node->next->data == data) {
108 node->next = t->next;
111 if (!node->next) { /* we removed the last one */
118 /* returns first node */
119 struct list_node *list_first(struct list *list) {
120 return list ? list->first : NULL;
123 /* returns the next node after the argument */
124 struct list_node *list_next(struct list_node *node) {
128 /* returns number of nodes */
129 uint32_t list_count(struct list *list) {
133 /* Local Variables: */
134 /* c-file-style: "stroustrup" */