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
27 DEFINE_MTYPE_STATIC(LIB
, LINK_LIST
, "Link List")
28 DEFINE_MTYPE_STATIC(LIB
, LINK_NODE
, "Link Node")
30 struct list
*list_new(void)
32 return XCALLOC(MTYPE_LINK_LIST
, sizeof(struct list
));
36 static void list_free_internal(struct list
*l
)
38 XFREE(MTYPE_LINK_LIST
, l
);
42 /* Allocate new listnode. Internal use only. */
43 static struct listnode
*listnode_new(struct list
*list
, void *val
)
45 struct listnode
*node
;
47 /* if listnode memory is managed by the app then the val
48 * passed in is the listnode
50 if (list
->flags
& LINKLIST_FLAG_NODE_MEM_BY_APP
) {
52 node
->prev
= node
->next
= NULL
;
54 node
= XCALLOC(MTYPE_LINK_NODE
, sizeof(struct listnode
));
61 static void listnode_free(struct list
*list
, struct listnode
*node
)
63 if (!(list
->flags
& LINKLIST_FLAG_NODE_MEM_BY_APP
))
64 XFREE(MTYPE_LINK_NODE
, node
);
67 struct listnode
*listnode_add(struct list
*list
, void *val
)
69 struct listnode
*node
;
73 node
= listnode_new(list
, val
);
75 node
->prev
= list
->tail
;
77 if (list
->head
== NULL
)
80 list
->tail
->next
= node
;
88 void listnode_add_head(struct list
*list
, void *val
)
90 struct listnode
*node
;
94 node
= listnode_new(list
, val
);
96 node
->next
= list
->head
;
98 if (list
->head
== NULL
)
101 list
->head
->prev
= node
;
107 bool listnode_add_sort_nodup(struct list
*list
, void *val
)
110 struct listnode
*new;
116 if (list
->flags
& LINKLIST_FLAG_NODE_MEM_BY_APP
) {
124 for (n
= list
->head
; n
; n
= n
->next
) {
125 ret
= (*list
->cmp
)(data
, n
->data
);
127 new = listnode_new(list
, val
);
140 /* found duplicate return false */
146 new = listnode_new(list
, val
);
148 LISTNODE_ATTACH(list
, new);
153 struct list
*list_dup(struct list
*list
)
156 struct listnode
*node
;
162 dup
->cmp
= list
->cmp
;
163 dup
->del
= list
->del
;
164 for (ALL_LIST_ELEMENTS_RO(list
, node
, data
))
165 listnode_add(dup
, data
);
170 void listnode_add_sort(struct list
*list
, void *val
)
173 struct listnode
*new;
177 new = listnode_new(list
, val
);
181 for (n
= list
->head
; n
; n
= n
->next
) {
182 if ((*list
->cmp
)(val
, n
->data
) < 0) {
197 new->prev
= list
->tail
;
200 list
->tail
->next
= new;
208 struct listnode
*listnode_add_after(struct list
*list
, struct listnode
*pp
,
215 nn
= listnode_new(list
, val
);
219 list
->head
->prev
= nn
;
223 nn
->next
= list
->head
;
242 struct listnode
*listnode_add_before(struct list
*list
, struct listnode
*pp
,
249 nn
= listnode_new(list
, val
);
253 list
->tail
->next
= nn
;
257 nn
->prev
= list
->tail
;
276 void listnode_move_to_tail(struct list
*l
, struct listnode
*n
)
278 LISTNODE_DETACH(l
, n
);
279 LISTNODE_ATTACH(l
, n
);
282 void listnode_delete(struct list
*list
, const void *val
)
284 struct listnode
*node
= listnode_lookup(list
, val
);
287 list_delete_node(list
, node
);
290 void *listnode_head(struct list
*list
)
292 struct listnode
*node
;
302 void list_delete_all_node(struct list
*list
)
304 struct listnode
*node
;
305 struct listnode
*next
;
308 for (node
= list
->head
; node
; node
= next
) {
311 (*list
->del
)(node
->data
);
312 listnode_free(list
, node
);
314 list
->head
= list
->tail
= NULL
;
318 void list_filter_out_nodes(struct list
*list
, bool (*cond
)(void *data
))
320 struct listnode
*node
;
321 struct listnode
*next
;
326 for (ALL_LIST_ELEMENTS(list
, node
, next
, data
)) {
327 if ((cond
&& cond(data
)) || (!cond
)) {
330 list_delete_node(list
, node
);
335 void list_delete(struct list
**list
)
338 list_delete_all_node(*list
);
339 list_free_internal(*list
);
343 struct listnode
*listnode_lookup(struct list
*list
, const void *data
)
345 struct listnode
*node
;
348 for (node
= listhead(list
); node
; node
= listnextnode(node
))
349 if (data
== listgetdata(node
))
354 struct listnode
*listnode_lookup_nocheck(struct list
*list
, void *data
)
358 return listnode_lookup(list
, data
);
361 void list_delete_node(struct list
*list
, struct listnode
*node
)
364 node
->prev
->next
= node
->next
;
366 list
->head
= node
->next
;
368 node
->next
->prev
= node
->prev
;
370 list
->tail
= node
->prev
;
372 listnode_free(list
, node
);
375 void list_sort(struct list
*list
, int (*cmp
)(const void **, const void **))
377 struct listnode
*ln
, *nn
;
380 size_t n
= list
->count
;
381 void **items
= XCALLOC(MTYPE_TMP
, (sizeof(void *)) * n
);
382 int (*realcmp
)(const void *, const void *) =
383 (int (*)(const void *, const void *))cmp
;
385 for (ALL_LIST_ELEMENTS(list
, ln
, nn
, data
)) {
387 list_delete_node(list
, ln
);
390 qsort(items
, n
, sizeof(void *), realcmp
);
392 for (unsigned int j
= 0; j
< n
; ++j
)
393 listnode_add(list
, items
[j
]);
395 XFREE(MTYPE_TMP
, items
);
398 struct listnode
*listnode_add_force(struct list
**list
, void *val
)
402 return listnode_add(*list
, val
);
405 void **list_to_array(struct list
*list
, void **arr
, size_t arrlen
)
411 for (ALL_LIST_ELEMENTS_RO(list
, ln
, vp
)) {