26f0fd2cfa1fbf47789133cc1466ea2e73c77cfb
[libradsec.git] / list.c
1 /* Copyright (c) 2006-2009, Stig Venaas, UNINETT AS.
2  * Copyright (c) 2010, UNINETT AS, NORDUnet A/S.
3  * Copyright (c) 2010-2012, NORDUnet A/S. */
4 /* See LICENSE for licensing information. */
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include "list.h"
9
10 /* allocates and initialises list structure; returns NULL if malloc fails */
11 struct list *list_create() {
12     struct list *list = malloc(sizeof(struct list));
13     if (list)
14         memset(list, 0, sizeof(struct list));
15     return list;
16 }
17
18 /* frees all memory associated with the list */
19 void list_destroy(struct list *list) {
20     struct list_node *node, *next;
21
22     if (!list)
23         return;
24
25     for (node = list->first; node; node = next) {
26         free(node->data);
27         next = node->next;
28         free(node);
29     }
30     free(list);
31 }
32
33 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
34 int list_push(struct list *list, void *data) {
35     struct list_node *node;
36
37     node = malloc(sizeof(struct list_node));
38     if (!node)
39         return 0;
40
41     node->next = NULL;
42     node->data = data;
43
44     if (list->first)
45         list->last->next = node;
46     else
47         list->first = node;
48     list->last = node;
49
50     list->count++;
51     return 1;
52 }
53
54 /* removes first entry from list and returns data */
55 void *list_shift(struct list *list) {
56     struct list_node *node;
57     void *data;
58
59     if (!list || !list->first)
60         return NULL;
61
62     node = list->first;
63     list->first = node->next;
64     if (!list->first)
65         list->last = NULL;
66     data = node->data;
67     free(node);
68     list->count--;
69     return data;
70 }
71
72 /* removes all entries with matching data pointer */
73 void list_removedata(struct list *list, void *data) {
74     struct list_node *node, *t;
75
76     if (!list || !list->first)
77         return;
78
79     node = list->first;
80     while (node->data == data) {
81         list->first = node->next;
82         free(node);
83         list->count--;
84         node = list->first;
85         if (!node) {
86             list->last = NULL;
87             return;
88         }
89     }
90     for (; node->next; node = node->next)
91         if (node->next->data == data) {
92             t = node->next;
93             node->next = t->next;
94             free(t);
95             list->count--;
96             if (!node->next) { /* we removed the last one */
97                 list->last = node;
98                 return;
99             }
100         }
101 }
102
103 /* returns first node */
104 struct list_node *list_first(struct list *list) {
105     return list ? list->first : NULL;
106 }
107
108 /* returns the next node after the argument */
109 struct list_node *list_next(struct list_node *node) {
110     return node->next;
111 }
112
113 /* returns number of nodes */
114 uint32_t list_count(struct list *list) {
115     return list->count;
116 }
117
118 /* Local Variables: */
119 /* c-file-style: "stroustrup" */
120 /* End: */