]>
git.proxmox.com Git - mirror_frr.git/blob - lib/linklist.c
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
);
41 /* Allocate new listnode. Internal use only. */
42 static struct listnode
*listnode_new(void)
44 return XCALLOC(MTYPE_LINK_NODE
, sizeof(struct listnode
));
48 static void listnode_free(struct listnode
*node
)
50 XFREE(MTYPE_LINK_NODE
, node
);
53 void listnode_add(struct list
*list
, void *val
)
55 struct listnode
*node
;
59 node
= listnode_new();
61 node
->prev
= list
->tail
;
64 if (list
->head
== NULL
)
67 list
->tail
->next
= node
;
73 void listnode_add_head(struct list
*list
, void *val
)
75 struct listnode
*node
;
79 node
= listnode_new();
81 node
->next
= list
->head
;
84 if (list
->head
== NULL
)
87 list
->head
->prev
= node
;
93 void listnode_add_sort(struct list
*list
, void *val
)
100 new = listnode_new();
104 for (n
= list
->head
; n
; n
= n
->next
) {
105 if ((*list
->cmp
)(val
, n
->data
) < 0) {
120 new->prev
= list
->tail
;
123 list
->tail
->next
= new;
131 struct listnode
*listnode_add_after(struct list
*list
, struct listnode
*pp
,
143 list
->head
->prev
= nn
;
147 nn
->next
= list
->head
;
166 struct listnode
*listnode_add_before(struct list
*list
, struct listnode
*pp
,
178 list
->tail
->next
= nn
;
182 nn
->prev
= list
->tail
;
201 void listnode_move_to_tail(struct list
*l
, struct listnode
*n
)
203 LISTNODE_DETACH(l
, n
);
204 LISTNODE_ATTACH(l
, n
);
207 void listnode_delete(struct list
*list
, void *val
)
209 struct listnode
*node
= listnode_lookup(list
, val
);
212 list_delete_node(list
, node
);
215 void *listnode_head(struct list
*list
)
217 struct listnode
*node
;
227 void list_delete_all_node(struct list
*list
)
229 struct listnode
*node
;
230 struct listnode
*next
;
233 for (node
= list
->head
; node
; node
= next
) {
236 (*list
->del
)(node
->data
);
239 list
->head
= list
->tail
= NULL
;
243 void list_delete(struct list
**list
)
246 list_delete_all_node(*list
);
247 list_free_internal(*list
);
251 struct listnode
*listnode_lookup(struct list
*list
, void *data
)
253 struct listnode
*node
;
256 for (node
= listhead(list
); node
; node
= listnextnode(node
))
257 if (data
== listgetdata(node
))
262 struct listnode
*listnode_lookup_nocheck(struct list
*list
, void *data
)
266 return listnode_lookup(list
, data
);
269 void list_delete_node(struct list
*list
, struct listnode
*node
)
272 node
->prev
->next
= node
->next
;
274 list
->head
= node
->next
;
276 node
->next
->prev
= node
->prev
;
278 list
->tail
= node
->prev
;
283 void list_add_list(struct list
*list
, struct list
*add
)
287 for (n
= listhead(add
); n
; n
= listnextnode(n
))
288 listnode_add(list
, n
->data
);
291 struct list
*list_dup(struct list
*list
)
293 struct list
*new = list_new();
297 new->cmp
= list
->cmp
;
298 new->del
= list
->del
;
300 for (ALL_LIST_ELEMENTS_RO(list
, ln
, data
))
301 listnode_add(new, data
);
306 void list_sort(struct list
*list
, int (*cmp
)(const void **, const void **))
308 struct listnode
*ln
, *nn
;
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
;
316 for (ALL_LIST_ELEMENTS(list
, ln
, nn
, data
)) {
318 list_delete_node(list
, ln
);
321 qsort(items
, n
, sizeof(void *), realcmp
);
323 for (unsigned int j
= 0; j
< n
; ++j
)
324 listnode_add(list
, items
[j
]);
326 XFREE(MTYPE_TMP
, items
);
329 void listnode_add_force(struct list
**list
, void *val
)
333 return listnode_add(*list
, val
);