]> git.proxmox.com Git - mirror_frr.git/blame - lib/linklist.c
Merge pull request #4155 from pguibert6WIND/bfd_increase_config
[mirror_frr.git] / lib / linklist.c
CommitLineData
718e3744 1/* Generic linked list routine.
2 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
3a5c3bcb 22#include <stdlib.h>
718e3744 23
24#include "linklist.h"
25#include "memory.h"
6b0655a2 26
4a1ab8e4
DL
27DEFINE_MTYPE_STATIC(LIB, LINK_LIST, "Link List")
28DEFINE_MTYPE_STATIC(LIB, LINK_NODE, "Link Node")
29
d62a17ae 30struct list *list_new(void)
718e3744 31{
d62a17ae 32 return XCALLOC(MTYPE_LINK_LIST, sizeof(struct list));
718e3744 33}
34
35/* Free list. */
acdf5e25 36static void list_free_internal(struct list *l)
718e3744 37{
d62a17ae 38 XFREE(MTYPE_LINK_LIST, l);
718e3744 39}
40
41/* Allocate new listnode. Internal use only. */
d62a17ae 42static struct listnode *listnode_new(void)
718e3744 43{
d62a17ae 44 return XCALLOC(MTYPE_LINK_NODE, sizeof(struct listnode));
718e3744 45}
46
47/* Free listnode. */
d62a17ae 48static void listnode_free(struct listnode *node)
718e3744 49{
d62a17ae 50 XFREE(MTYPE_LINK_NODE, node);
718e3744 51}
6b0655a2 52
315999e9 53struct listnode *listnode_add(struct list *list, void *val)
718e3744 54{
d62a17ae 55 struct listnode *node;
56
57 assert(val != NULL);
58
59 node = listnode_new();
60
61 node->prev = list->tail;
62 node->data = val;
63
64 if (list->head == NULL)
65 list->head = node;
66 else
67 list->tail->next = node;
68 list->tail = node;
69
70 list->count++;
315999e9
AK
71
72 return node;
718e3744 73}
74
9ea82f28
RW
75void listnode_add_head(struct list *list, void *val)
76{
77 struct listnode *node;
78
79 assert(val != NULL);
80
81 node = listnode_new();
82
83 node->next = list->head;
84 node->data = val;
85
86 if (list->head == NULL)
87 list->head = node;
88 else
89 list->head->prev = node;
90 list->head = node;
91
92 list->count++;
93}
94
d62a17ae 95void listnode_add_sort(struct list *list, void *val)
718e3744 96{
d62a17ae 97 struct listnode *n;
98 struct listnode *new;
99
100 assert(val != NULL);
101
102 new = listnode_new();
103 new->data = val;
104
105 if (list->cmp) {
106 for (n = list->head; n; n = n->next) {
107 if ((*list->cmp)(val, n->data) < 0) {
108 new->next = n;
109 new->prev = n->prev;
110
111 if (n->prev)
112 n->prev->next = new;
113 else
114 list->head = new;
115 n->prev = new;
116 list->count++;
117 return;
118 }
119 }
718e3744 120 }
718e3744 121
d62a17ae 122 new->prev = list->tail;
718e3744 123
d62a17ae 124 if (list->tail)
125 list->tail->next = new;
126 else
127 list->head = new;
718e3744 128
d62a17ae 129 list->tail = new;
130 list->count++;
718e3744 131}
132
d62a17ae 133struct listnode *listnode_add_after(struct list *list, struct listnode *pp,
134 void *val)
718e3744 135{
d62a17ae 136 struct listnode *nn;
137
138 assert(val != NULL);
139
140 nn = listnode_new();
141 nn->data = val;
142
143 if (pp == NULL) {
144 if (list->head)
145 list->head->prev = nn;
146 else
147 list->tail = nn;
148
149 nn->next = list->head;
150 nn->prev = pp;
151
152 list->head = nn;
153 } else {
154 if (pp->next)
155 pp->next->prev = nn;
156 else
157 list->tail = nn;
158
159 nn->next = pp->next;
160 nn->prev = pp;
161
162 pp->next = nn;
163 }
164 list->count++;
165 return nn;
718e3744 166}
167
d62a17ae 168struct listnode *listnode_add_before(struct list *list, struct listnode *pp,
169 void *val)
b21e9619 170{
d62a17ae 171 struct listnode *nn;
172
173 assert(val != NULL);
174
175 nn = listnode_new();
176 nn->data = val;
177
178 if (pp == NULL) {
179 if (list->tail)
180 list->tail->next = nn;
181 else
182 list->head = nn;
183
184 nn->prev = list->tail;
185 nn->next = pp;
186
187 list->tail = nn;
188 } else {
189 if (pp->prev)
190 pp->prev->next = nn;
191 else
192 list->head = nn;
193
194 nn->prev = pp->prev;
195 nn->next = pp;
196
197 pp->prev = nn;
198 }
199 list->count++;
200 return nn;
b21e9619
DL
201}
202
d62a17ae 203void listnode_move_to_tail(struct list *l, struct listnode *n)
93559b99 204{
d62a17ae 205 LISTNODE_DETACH(l, n);
206 LISTNODE_ATTACH(l, n);
93559b99 207}
718e3744 208
d62a17ae 209void listnode_delete(struct list *list, void *val)
718e3744 210{
0866cdaf 211 struct listnode *node = listnode_lookup(list, val);
d62a17ae 212
0866cdaf
A
213 if (node)
214 list_delete_node(list, node);
718e3744 215}
216
d62a17ae 217void *listnode_head(struct list *list)
718e3744 218{
d62a17ae 219 struct listnode *node;
718e3744 220
d62a17ae 221 assert(list);
222 node = list->head;
718e3744 223
d62a17ae 224 if (node)
225 return node->data;
226 return NULL;
718e3744 227}
228
d62a17ae 229void list_delete_all_node(struct list *list)
718e3744 230{
d62a17ae 231 struct listnode *node;
232 struct listnode *next;
233
234 assert(list);
235 for (node = list->head; node; node = next) {
236 next = node->next;
affe9e99 237 if (*list->del)
d62a17ae 238 (*list->del)(node->data);
239 listnode_free(node);
240 }
241 list->head = list->tail = NULL;
242 list->count = 0;
718e3744 243}
244
6a154c88 245void list_delete(struct list **list)
718e3744 246{
affe9e99
DS
247 assert(*list);
248 list_delete_all_node(*list);
acdf5e25 249 list_free_internal(*list);
affe9e99
DS
250 *list = NULL;
251}
252
d62a17ae 253struct listnode *listnode_lookup(struct list *list, void *data)
718e3744 254{
d62a17ae 255 struct listnode *node;
718e3744 256
d62a17ae 257 assert(list);
258 for (node = listhead(list); node; node = listnextnode(node))
259 if (data == listgetdata(node))
260 return node;
261 return NULL;
718e3744 262}
6b0655a2 263
2fe55afe
PG
264struct listnode *listnode_lookup_nocheck(struct list *list, void *data)
265{
266 if (!list)
267 return NULL;
268 return listnode_lookup(list, data);
269}
270
d62a17ae 271void list_delete_node(struct list *list, struct listnode *node)
718e3744 272{
d62a17ae 273 if (node->prev)
274 node->prev->next = node->next;
275 else
276 list->head = node->next;
277 if (node->next)
278 node->next->prev = node->prev;
279 else
280 list->tail = node->prev;
281 list->count--;
282 listnode_free(node);
718e3744 283}
6b0655a2 284
6fd8c487 285void list_add_list(struct list *list, struct list *add)
718e3744 286{
d62a17ae 287 struct listnode *n;
718e3744 288
6fd8c487
QY
289 for (n = listhead(add); n; n = listnextnode(n))
290 listnode_add(list, n->data);
718e3744 291}
3a5c3bcb 292
6fd8c487 293struct list *list_dup(struct list *list)
3a5c3bcb
QY
294{
295 struct list *new = list_new();
296 struct listnode *ln;
297 void *data;
298
6fd8c487
QY
299 new->cmp = list->cmp;
300 new->del = list->del;
3a5c3bcb 301
6fd8c487 302 for (ALL_LIST_ELEMENTS_RO(list, ln, data))
3a5c3bcb
QY
303 listnode_add(new, data);
304
305 return new;
306}
307
308void list_sort(struct list *list, int (*cmp)(const void **, const void **))
309{
310 struct listnode *ln, *nn;
311 int i = -1;
312 void *data;
313 size_t n = list->count;
314 void **items = XCALLOC(MTYPE_TMP, (sizeof(void *)) * n);
315 int (*realcmp)(const void *, const void *) =
316 (int (*)(const void *, const void *))cmp;
317
318 for (ALL_LIST_ELEMENTS(list, ln, nn, data)) {
319 items[++i] = data;
320 list_delete_node(list, ln);
321 }
322
323 qsort(items, n, sizeof(void *), realcmp);
324
c683bd44
A
325 for (unsigned int j = 0; j < n; ++j)
326 listnode_add(list, items[j]);
3a5c3bcb
QY
327
328 XFREE(MTYPE_TMP, items);
329}
33bca8a1 330
75839aab 331struct listnode *listnode_add_force(struct list **list, void *val)
33bca8a1
PG
332{
333 if (*list == NULL)
334 *list = list_new();
335 return listnode_add(*list, val);
336}