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