]>
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 struct listnode
*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
;
75 void listnode_add_head(struct list
*list
, void *val
)
77 struct listnode
*node
;
81 node
= listnode_new();
83 node
->next
= list
->head
;
86 if (list
->head
== NULL
)
89 list
->head
->prev
= node
;
95 void listnode_add_sort(struct list
*list
, void *val
)
102 new = listnode_new();
106 for (n
= list
->head
; n
; n
= n
->next
) {
107 if ((*list
->cmp
)(val
, n
->data
) < 0) {
122 new->prev
= list
->tail
;
125 list
->tail
->next
= new;
133 struct listnode
*listnode_add_after(struct list
*list
, struct listnode
*pp
,
145 list
->head
->prev
= nn
;
149 nn
->next
= list
->head
;
168 struct listnode
*listnode_add_before(struct list
*list
, struct listnode
*pp
,
180 list
->tail
->next
= nn
;
184 nn
->prev
= list
->tail
;
203 void listnode_move_to_tail(struct list
*l
, struct listnode
*n
)
205 LISTNODE_DETACH(l
, n
);
206 LISTNODE_ATTACH(l
, n
);
209 void listnode_delete(struct list
*list
, void *val
)
211 struct listnode
*node
= listnode_lookup(list
, val
);
214 list_delete_node(list
, node
);
217 void *listnode_head(struct list
*list
)
219 struct listnode
*node
;
229 void list_delete_all_node(struct list
*list
)
231 struct listnode
*node
;
232 struct listnode
*next
;
235 for (node
= list
->head
; node
; node
= next
) {
238 (*list
->del
)(node
->data
);
241 list
->head
= list
->tail
= NULL
;
245 void list_delete(struct list
**list
)
248 list_delete_all_node(*list
);
249 list_free_internal(*list
);
253 struct listnode
*listnode_lookup(struct list
*list
, void *data
)
255 struct listnode
*node
;
258 for (node
= listhead(list
); node
; node
= listnextnode(node
))
259 if (data
== listgetdata(node
))
264 struct listnode
*listnode_lookup_nocheck(struct list
*list
, void *data
)
268 return listnode_lookup(list
, data
);
271 void list_delete_node(struct list
*list
, struct listnode
*node
)
274 node
->prev
->next
= node
->next
;
276 list
->head
= node
->next
;
278 node
->next
->prev
= node
->prev
;
280 list
->tail
= node
->prev
;
285 void list_add_list(struct list
*list
, struct list
*add
)
289 for (n
= listhead(add
); n
; n
= listnextnode(n
))
290 listnode_add(list
, n
->data
);
293 struct list
*list_dup(struct list
*list
)
295 struct list
*new = list_new();
299 new->cmp
= list
->cmp
;
300 new->del
= list
->del
;
302 for (ALL_LIST_ELEMENTS_RO(list
, ln
, data
))
303 listnode_add(new, data
);
308 void list_sort(struct list
*list
, int (*cmp
)(const void **, const void **))
310 struct listnode
*ln
, *nn
;
313 size_t n
= list
->count
;
314 void **items
= XCALLOC(MTYPE_TMP
, (sizeof(void *)) * n
);
315 int (*realcmp
)(const void *, const void *) =
316 (int (*)(const void *, const void *))cmp
;
318 for (ALL_LIST_ELEMENTS(list
, ln
, nn
, data
)) {
320 list_delete_node(list
, ln
);
323 qsort(items
, n
, sizeof(void *), realcmp
);
325 for (unsigned int j
= 0; j
< n
; ++j
)
326 listnode_add(list
, items
[j
]);
328 XFREE(MTYPE_TMP
, items
);
331 struct listnode
*listnode_add_force(struct list
**list
, void *val
)
335 return listnode_add(*list
, val
);