1 /* Generic linked list routine.
2 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
26 #include "libfrr_trace.h"
28 DEFINE_MTYPE_STATIC(LIB
, LINK_LIST
, "Link List");
29 DEFINE_MTYPE_STATIC(LIB
, LINK_NODE
, "Link Node");
31 struct list
*list_new(void)
33 return XCALLOC(MTYPE_LINK_LIST
, sizeof(struct list
));
37 static void list_free_internal(struct list
*l
)
39 XFREE(MTYPE_LINK_LIST
, l
);
43 /* Allocate new listnode. Internal use only. */
44 static struct listnode
*listnode_new(struct list
*list
, void *val
)
46 struct listnode
*node
;
48 /* if listnode memory is managed by the app then the val
49 * passed in is the listnode
51 if (list
->flags
& LINKLIST_FLAG_NODE_MEM_BY_APP
) {
53 node
->prev
= node
->next
= NULL
;
55 node
= XCALLOC(MTYPE_LINK_NODE
, sizeof(struct listnode
));
62 static void listnode_free(struct list
*list
, struct listnode
*node
)
64 if (!(list
->flags
& LINKLIST_FLAG_NODE_MEM_BY_APP
))
65 XFREE(MTYPE_LINK_NODE
, node
);
68 struct listnode
*listnode_add(struct list
*list
, void *val
)
70 frrtrace(2, frr_libfrr
, list_add
, list
, val
);
72 struct listnode
*node
;
76 node
= listnode_new(list
, val
);
78 node
->prev
= list
->tail
;
80 if (list
->head
== NULL
)
83 list
->tail
->next
= node
;
91 void listnode_add_head(struct list
*list
, void *val
)
93 struct listnode
*node
;
97 node
= listnode_new(list
, val
);
99 node
->next
= list
->head
;
101 if (list
->head
== NULL
)
104 list
->head
->prev
= node
;
110 bool listnode_add_sort_nodup(struct list
*list
, void *val
)
113 struct listnode
*new;
119 if (list
->flags
& LINKLIST_FLAG_NODE_MEM_BY_APP
) {
127 for (n
= list
->head
; n
; n
= n
->next
) {
128 ret
= (*list
->cmp
)(data
, n
->data
);
130 new = listnode_new(list
, val
);
143 /* found duplicate return false */
149 new = listnode_new(list
, val
);
151 LISTNODE_ATTACH(list
, new);
156 struct list
*list_dup(struct list
*list
)
159 struct listnode
*node
;
165 dup
->cmp
= list
->cmp
;
166 dup
->del
= list
->del
;
167 for (ALL_LIST_ELEMENTS_RO(list
, node
, data
))
168 listnode_add(dup
, data
);
173 void listnode_add_sort(struct list
*list
, void *val
)
176 struct listnode
*new;
180 new = listnode_new(list
, val
);
184 for (n
= list
->head
; n
; n
= n
->next
) {
185 if ((*list
->cmp
)(val
, n
->data
) < 0) {
200 new->prev
= list
->tail
;
203 list
->tail
->next
= new;
211 struct listnode
*listnode_add_after(struct list
*list
, struct listnode
*pp
,
218 nn
= listnode_new(list
, val
);
222 list
->head
->prev
= nn
;
226 nn
->next
= list
->head
;
245 struct listnode
*listnode_add_before(struct list
*list
, struct listnode
*pp
,
252 nn
= listnode_new(list
, val
);
256 list
->tail
->next
= nn
;
260 nn
->prev
= list
->tail
;
279 void listnode_move_to_tail(struct list
*l
, struct listnode
*n
)
281 LISTNODE_DETACH(l
, n
);
282 LISTNODE_ATTACH(l
, n
);
285 void listnode_delete(struct list
*list
, const void *val
)
287 frrtrace(2, frr_libfrr
, list_remove
, list
, val
);
289 struct listnode
*node
= listnode_lookup(list
, val
);
292 list_delete_node(list
, node
);
295 void *listnode_head(struct list
*list
)
297 struct listnode
*node
;
307 void list_delete_all_node(struct list
*list
)
309 struct listnode
*node
;
310 struct listnode
*next
;
313 for (node
= list
->head
; node
; node
= next
) {
316 (*list
->del
)(node
->data
);
317 listnode_free(list
, node
);
319 list
->head
= list
->tail
= NULL
;
323 void list_delete(struct list
**list
)
326 list_delete_all_node(*list
);
327 list_free_internal(*list
);
331 struct listnode
*listnode_lookup(struct list
*list
, const void *data
)
333 struct listnode
*node
;
336 for (node
= listhead(list
); node
; node
= listnextnode(node
))
337 if (data
== listgetdata(node
))
342 struct listnode
*listnode_lookup_nocheck(struct list
*list
, void *data
)
346 return listnode_lookup(list
, data
);
349 void list_delete_node(struct list
*list
, struct listnode
*node
)
351 frrtrace(2, frr_libfrr
, list_delete_node
, list
, node
);
354 node
->prev
->next
= node
->next
;
356 list
->head
= node
->next
;
358 node
->next
->prev
= node
->prev
;
360 list
->tail
= node
->prev
;
362 listnode_free(list
, node
);
365 void list_sort(struct list
*list
, int (*cmp
)(const void **, const void **))
367 frrtrace(1, frr_libfrr
, list_sort
, list
);
369 struct listnode
*ln
, *nn
;
372 size_t n
= list
->count
;
373 void **items
= XCALLOC(MTYPE_TMP
, (sizeof(void *)) * n
);
374 int (*realcmp
)(const void *, const void *) =
375 (int (*)(const void *, const void *))cmp
;
377 for (ALL_LIST_ELEMENTS(list
, ln
, nn
, data
)) {
379 list_delete_node(list
, ln
);
382 qsort(items
, n
, sizeof(void *), realcmp
);
384 for (unsigned int j
= 0; j
< n
; ++j
)
385 listnode_add(list
, items
[j
]);
387 XFREE(MTYPE_TMP
, items
);
390 struct listnode
*listnode_add_force(struct list
**list
, void *val
)
394 return listnode_add(*list
, val
);
397 void **list_to_array(struct list
*list
, void **arr
, size_t arrlen
)
403 for (ALL_LIST_ELEMENTS_RO(list
, ln
, vp
)) {