]>
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_sort(struct list
*list
, void *val
)
84 for (n
= list
->head
; n
; n
= n
->next
) {
85 if ((*list
->cmp
)(val
, n
->data
) < 0) {
100 new->prev
= list
->tail
;
103 list
->tail
->next
= new;
111 struct listnode
*listnode_add_after(struct list
*list
, struct listnode
*pp
,
123 list
->head
->prev
= nn
;
127 nn
->next
= list
->head
;
146 struct listnode
*listnode_add_before(struct list
*list
, struct listnode
*pp
,
158 list
->tail
->next
= nn
;
162 nn
->prev
= list
->tail
;
181 void listnode_move_to_tail(struct list
*l
, struct listnode
*n
)
183 LISTNODE_DETACH(l
, n
);
184 LISTNODE_ATTACH(l
, n
);
187 void listnode_delete(struct list
*list
, void *val
)
189 struct listnode
*node
;
192 for (node
= list
->head
; node
; node
= node
->next
) {
193 if (node
->data
== val
) {
195 node
->prev
->next
= node
->next
;
197 list
->head
= node
->next
;
200 node
->next
->prev
= node
->prev
;
202 list
->tail
= node
->prev
;
211 void *listnode_head(struct list
*list
)
213 struct listnode
*node
;
223 void list_delete_all_node(struct list
*list
)
225 struct listnode
*node
;
226 struct listnode
*next
;
229 for (node
= list
->head
; node
; node
= next
) {
232 (*list
->del
)(node
->data
);
235 list
->head
= list
->tail
= NULL
;
239 void list_delete_and_null(struct list
**list
)
242 list_delete_all_node(*list
);
243 list_free_internal(*list
);
247 void list_delete_original(struct list
*list
)
249 list_delete_and_null(&list
);
252 struct listnode
*listnode_lookup(struct list
*list
, void *data
)
254 struct listnode
*node
;
257 for (node
= listhead(list
); node
; node
= listnextnode(node
))
258 if (data
== listgetdata(node
))
263 void list_delete_node(struct list
*list
, struct listnode
*node
)
266 node
->prev
->next
= node
->next
;
268 list
->head
= node
->next
;
270 node
->next
->prev
= node
->prev
;
272 list
->tail
= node
->prev
;
277 void list_add_list(struct list
*list
, struct list
*add
)
281 for (n
= listhead(add
); n
; n
= listnextnode(n
))
282 listnode_add(list
, n
->data
);
285 struct list
*list_dup(struct list
*list
)
287 struct list
*new = list_new();
291 new->cmp
= list
->cmp
;
292 new->del
= list
->del
;
294 for (ALL_LIST_ELEMENTS_RO(list
, ln
, data
))
295 listnode_add(new, data
);
300 void list_sort(struct list
*list
, int (*cmp
)(const void **, const void **))
302 struct listnode
*ln
, *nn
;
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
;
310 for (ALL_LIST_ELEMENTS(list
, ln
, nn
, data
)) {
312 list_delete_node(list
, ln
);
315 qsort(items
, n
, sizeof(void *), realcmp
);
317 for (unsigned int i
= 0; i
< n
; ++i
)
318 listnode_add(list
, items
[i
]);
320 XFREE(MTYPE_TMP
, items
);