]>
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
= listnode_lookup(list
, val
);
192 list_delete_node(list
, node
);
195 void *listnode_head(struct list
*list
)
197 struct listnode
*node
;
207 void list_delete_all_node(struct list
*list
)
209 struct listnode
*node
;
210 struct listnode
*next
;
213 for (node
= list
->head
; node
; node
= next
) {
216 (*list
->del
)(node
->data
);
219 list
->head
= list
->tail
= NULL
;
223 void list_delete_and_null(struct list
**list
)
226 list_delete_all_node(*list
);
227 list_free_internal(*list
);
231 void list_delete_original(struct list
*list
)
233 list_delete_and_null(&list
);
236 struct listnode
*listnode_lookup(struct list
*list
, void *data
)
238 struct listnode
*node
;
241 for (node
= listhead(list
); node
; node
= listnextnode(node
))
242 if (data
== listgetdata(node
))
247 void list_delete_node(struct list
*list
, struct listnode
*node
)
250 node
->prev
->next
= node
->next
;
252 list
->head
= node
->next
;
254 node
->next
->prev
= node
->prev
;
256 list
->tail
= node
->prev
;
261 void list_add_list(struct list
*list
, struct list
*add
)
265 for (n
= listhead(add
); n
; n
= listnextnode(n
))
266 listnode_add(list
, n
->data
);
269 struct list
*list_dup(struct list
*list
)
271 struct list
*new = list_new();
275 new->cmp
= list
->cmp
;
276 new->del
= list
->del
;
278 for (ALL_LIST_ELEMENTS_RO(list
, ln
, data
))
279 listnode_add(new, data
);
284 void list_sort(struct list
*list
, int (*cmp
)(const void **, const void **))
286 struct listnode
*ln
, *nn
;
289 size_t n
= list
->count
;
290 void **items
= XCALLOC(MTYPE_TMP
, (sizeof(void *)) * n
);
291 int (*realcmp
)(const void *, const void *) =
292 (int (*)(const void *, const void *))cmp
;
294 for (ALL_LIST_ELEMENTS(list
, ln
, nn
, data
)) {
296 list_delete_node(list
, ln
);
299 qsort(items
, n
, sizeof(void *), realcmp
);
301 for (unsigned int i
= 0; i
< n
; ++i
)
302 listnode_add(list
, items
[i
]);
304 XFREE(MTYPE_TMP
, items
);