]>
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 /* Allocate new list. */
31 struct list
*list_new(void)
33 return XCALLOC(MTYPE_LINK_LIST
, sizeof(struct list
));
37 static void list_free_internal(struct list
*l
)
39 XFREE(MTYPE_LINK_LIST
, l
);
42 /* Allocate new listnode. Internal use only. */
43 static struct listnode
*listnode_new(void)
45 return XCALLOC(MTYPE_LINK_NODE
, sizeof(struct listnode
));
49 static void listnode_free(struct listnode
*node
)
51 XFREE(MTYPE_LINK_NODE
, node
);
54 /* Add new data to the list. */
55 void listnode_add(struct list
*list
, void *val
)
57 struct listnode
*node
;
61 node
= listnode_new();
63 node
->prev
= list
->tail
;
66 if (list
->head
== NULL
)
69 list
->tail
->next
= node
;
76 * Add a node to the list. If the list was sorted according to the
77 * cmp function, insert a new node with the given val such that the
78 * list remains sorted. The new node is always inserted; there is no
79 * notion of omitting duplicates.
81 void listnode_add_sort(struct list
*list
, void *val
)
92 for (n
= list
->head
; n
; n
= n
->next
) {
93 if ((*list
->cmp
)(val
, n
->data
) < 0) {
108 new->prev
= list
->tail
;
111 list
->tail
->next
= new;
119 struct listnode
*listnode_add_after(struct list
*list
, struct listnode
*pp
,
131 list
->head
->prev
= nn
;
135 nn
->next
= list
->head
;
154 struct listnode
*listnode_add_before(struct list
*list
, struct listnode
*pp
,
166 list
->tail
->next
= nn
;
170 nn
->prev
= list
->tail
;
189 /* Move given listnode to tail of the list */
190 void listnode_move_to_tail(struct list
*l
, struct listnode
*n
)
192 LISTNODE_DETACH(l
, n
);
193 LISTNODE_ATTACH(l
, n
);
196 /* Delete specific date pointer from the list. */
197 void listnode_delete(struct list
*list
, void *val
)
199 struct listnode
*node
;
202 for (node
= list
->head
; node
; node
= node
->next
) {
203 if (node
->data
== val
) {
205 node
->prev
->next
= node
->next
;
207 list
->head
= node
->next
;
210 node
->next
->prev
= node
->prev
;
212 list
->tail
= node
->prev
;
221 /* Return first node's data if it is there. */
222 void *listnode_head(struct list
*list
)
224 struct listnode
*node
;
234 /* Delete all listnode from the list. */
235 void list_delete_all_node(struct list
*list
)
237 struct listnode
*node
;
238 struct listnode
*next
;
241 for (node
= list
->head
; node
; node
= next
) {
244 (*list
->del
)(node
->data
);
247 list
->head
= list
->tail
= NULL
;
251 /* Delete all listnode then free list itself. */
252 void list_delete_and_null(struct list
**list
)
255 list_delete_all_node(*list
);
256 list_free_internal(*list
);
260 void list_delete_original(struct list
*list
)
262 list_delete_and_null(&list
);
265 /* Lookup the node which has given data. */
266 struct listnode
*listnode_lookup(struct list
*list
, void *data
)
268 struct listnode
*node
;
271 for (node
= listhead(list
); node
; node
= listnextnode(node
))
272 if (data
== listgetdata(node
))
277 /* Delete the node from list. For ospfd and ospf6d. */
278 void list_delete_node(struct list
*list
, struct listnode
*node
)
281 node
->prev
->next
= node
->next
;
283 list
->head
= node
->next
;
285 node
->next
->prev
= node
->prev
;
287 list
->tail
= node
->prev
;
293 void list_add_list(struct list
*l
, struct list
*m
)
297 for (n
= listhead(m
); n
; n
= listnextnode(n
))
298 listnode_add(l
, n
->data
);
301 struct list
*list_dup(struct list
*l
)
303 struct list
*new = list_new();
310 for (ALL_LIST_ELEMENTS_RO(l
, ln
, data
))
311 listnode_add(new, data
);
316 void list_sort(struct list
*list
, int (*cmp
)(const void **, const void **))
318 struct listnode
*ln
, *nn
;
321 size_t n
= list
->count
;
322 void **items
= XCALLOC(MTYPE_TMP
, (sizeof(void *)) * n
);
323 int (*realcmp
)(const void *, const void *) =
324 (int (*)(const void *, const void *))cmp
;
326 for (ALL_LIST_ELEMENTS(list
, ln
, nn
, data
)) {
328 list_delete_node(list
, ln
);
331 qsort(items
, n
, sizeof(void *), realcmp
);
333 for (unsigned int i
= 0; i
< n
; ++i
)
334 listnode_add(list
, items
[i
]);
336 XFREE(MTYPE_TMP
, items
);