]> git.proxmox.com Git - mirror_frr.git/blame - lib/linklist.c
lib: move SG prefix2str APIs from pimd to lib
[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
d62a17ae 53void 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++;
718e3744 71}
72
9ea82f28
RW
73void listnode_add_head(struct list *list, void *val)
74{
75 struct listnode *node;
76
77 assert(val != NULL);
78
79 node = listnode_new();
80
81 node->next = list->head;
82 node->data = val;
83
84 if (list->head == NULL)
85 list->head = node;
86 else
87 list->head->prev = node;
88 list->head = node;
89
90 list->count++;
91}
92
d62a17ae 93void listnode_add_sort(struct list *list, void *val)
718e3744 94{
d62a17ae 95 struct listnode *n;
96 struct listnode *new;
97
98 assert(val != NULL);
99
100 new = listnode_new();
101 new->data = val;
102
103 if (list->cmp) {
104 for (n = list->head; n; n = n->next) {
105 if ((*list->cmp)(val, n->data) < 0) {
106 new->next = n;
107 new->prev = n->prev;
108
109 if (n->prev)
110 n->prev->next = new;
111 else
112 list->head = new;
113 n->prev = new;
114 list->count++;
115 return;
116 }
117 }
718e3744 118 }
718e3744 119
d62a17ae 120 new->prev = list->tail;
718e3744 121
d62a17ae 122 if (list->tail)
123 list->tail->next = new;
124 else
125 list->head = new;
718e3744 126
d62a17ae 127 list->tail = new;
128 list->count++;
718e3744 129}
130
d62a17ae 131struct listnode *listnode_add_after(struct list *list, struct listnode *pp,
132 void *val)
718e3744 133{
d62a17ae 134 struct listnode *nn;
135
136 assert(val != NULL);
137
138 nn = listnode_new();
139 nn->data = val;
140
141 if (pp == NULL) {
142 if (list->head)
143 list->head->prev = nn;
144 else
145 list->tail = nn;
146
147 nn->next = list->head;
148 nn->prev = pp;
149
150 list->head = nn;
151 } else {
152 if (pp->next)
153 pp->next->prev = nn;
154 else
155 list->tail = nn;
156
157 nn->next = pp->next;
158 nn->prev = pp;
159
160 pp->next = nn;
161 }
162 list->count++;
163 return nn;
718e3744 164}
165
d62a17ae 166struct listnode *listnode_add_before(struct list *list, struct listnode *pp,
167 void *val)
b21e9619 168{
d62a17ae 169 struct listnode *nn;
170
171 assert(val != NULL);
172
173 nn = listnode_new();
174 nn->data = val;
175
176 if (pp == NULL) {
177 if (list->tail)
178 list->tail->next = nn;
179 else
180 list->head = nn;
181
182 nn->prev = list->tail;
183 nn->next = pp;
184
185 list->tail = nn;
186 } else {
187 if (pp->prev)
188 pp->prev->next = nn;
189 else
190 list->head = nn;
191
192 nn->prev = pp->prev;
193 nn->next = pp;
194
195 pp->prev = nn;
196 }
197 list->count++;
198 return nn;
b21e9619
DL
199}
200
d62a17ae 201void listnode_move_to_tail(struct list *l, struct listnode *n)
93559b99 202{
d62a17ae 203 LISTNODE_DETACH(l, n);
204 LISTNODE_ATTACH(l, n);
93559b99 205}
718e3744 206
d62a17ae 207void listnode_delete(struct list *list, void *val)
718e3744 208{
0866cdaf 209 struct listnode *node = listnode_lookup(list, val);
d62a17ae 210
0866cdaf
A
211 if (node)
212 list_delete_node(list, node);
718e3744 213}
214
d62a17ae 215void *listnode_head(struct list *list)
718e3744 216{
d62a17ae 217 struct listnode *node;
718e3744 218
d62a17ae 219 assert(list);
220 node = list->head;
718e3744 221
d62a17ae 222 if (node)
223 return node->data;
224 return NULL;
718e3744 225}
226
d62a17ae 227void list_delete_all_node(struct list *list)
718e3744 228{
d62a17ae 229 struct listnode *node;
230 struct listnode *next;
231
232 assert(list);
233 for (node = list->head; node; node = next) {
234 next = node->next;
affe9e99 235 if (*list->del)
d62a17ae 236 (*list->del)(node->data);
237 listnode_free(node);
238 }
239 list->head = list->tail = NULL;
240 list->count = 0;
718e3744 241}
242
6a154c88 243void list_delete(struct list **list)
718e3744 244{
affe9e99
DS
245 assert(*list);
246 list_delete_all_node(*list);
acdf5e25 247 list_free_internal(*list);
affe9e99
DS
248 *list = NULL;
249}
250
d62a17ae 251struct listnode *listnode_lookup(struct list *list, void *data)
718e3744 252{
d62a17ae 253 struct listnode *node;
718e3744 254
d62a17ae 255 assert(list);
256 for (node = listhead(list); node; node = listnextnode(node))
257 if (data == listgetdata(node))
258 return node;
259 return NULL;
718e3744 260}
6b0655a2 261
2fe55afe
PG
262struct listnode *listnode_lookup_nocheck(struct list *list, void *data)
263{
264 if (!list)
265 return NULL;
266 return listnode_lookup(list, data);
267}
268
d62a17ae 269void list_delete_node(struct list *list, struct listnode *node)
718e3744 270{
d62a17ae 271 if (node->prev)
272 node->prev->next = node->next;
273 else
274 list->head = node->next;
275 if (node->next)
276 node->next->prev = node->prev;
277 else
278 list->tail = node->prev;
279 list->count--;
280 listnode_free(node);
718e3744 281}
6b0655a2 282
6fd8c487 283void list_add_list(struct list *list, struct list *add)
718e3744 284{
d62a17ae 285 struct listnode *n;
718e3744 286
6fd8c487
QY
287 for (n = listhead(add); n; n = listnextnode(n))
288 listnode_add(list, n->data);
718e3744 289}
3a5c3bcb 290
6fd8c487 291struct list *list_dup(struct list *list)
3a5c3bcb
QY
292{
293 struct list *new = list_new();
294 struct listnode *ln;
295 void *data;
296
6fd8c487
QY
297 new->cmp = list->cmp;
298 new->del = list->del;
3a5c3bcb 299
6fd8c487 300 for (ALL_LIST_ELEMENTS_RO(list, ln, data))
3a5c3bcb
QY
301 listnode_add(new, data);
302
303 return new;
304}
305
306void list_sort(struct list *list, int (*cmp)(const void **, const void **))
307{
308 struct listnode *ln, *nn;
309 int i = -1;
310 void *data;
311 size_t n = list->count;
312 void **items = XCALLOC(MTYPE_TMP, (sizeof(void *)) * n);
313 int (*realcmp)(const void *, const void *) =
314 (int (*)(const void *, const void *))cmp;
315
316 for (ALL_LIST_ELEMENTS(list, ln, nn, data)) {
317 items[++i] = data;
318 list_delete_node(list, ln);
319 }
320
321 qsort(items, n, sizeof(void *), realcmp);
322
c683bd44
A
323 for (unsigned int j = 0; j < n; ++j)
324 listnode_add(list, items[j]);
3a5c3bcb
QY
325
326 XFREE(MTYPE_TMP, items);
327}
33bca8a1
PG
328
329void listnode_add_force(struct list **list, void *val)
330{
331 if (*list == NULL)
332 *list = list_new();
333 return listnode_add(*list, val);
334}