]>
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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 DEFINE_MTYPE_STATIC(LIB
, LINK_LIST
, "Link List")
28 DEFINE_MTYPE_STATIC(LIB
, LINK_NODE
, "Link Node")
30 /* Allocate new list. */
34 return XCALLOC (MTYPE_LINK_LIST
, sizeof (struct list
));
39 list_free (struct list
*l
)
41 XFREE (MTYPE_LINK_LIST
, l
);
44 /* Allocate new listnode. Internal use only. */
45 static struct listnode
*
48 return XCALLOC (MTYPE_LINK_NODE
, sizeof (struct listnode
));
53 listnode_free (struct listnode
*node
)
55 XFREE (MTYPE_LINK_NODE
, node
);
58 /* Add new data to the list. */
60 listnode_add (struct list
*list
, void *val
)
62 struct listnode
*node
;
66 node
= listnode_new ();
68 node
->prev
= list
->tail
;
71 if (list
->head
== NULL
)
74 list
->tail
->next
= node
;
81 * Add a node to the list. If the list was sorted according to the
82 * cmp function, insert a new node with the given val such that the
83 * list remains sorted. The new node is always inserted; there is no
84 * notion of omitting duplicates.
87 listnode_add_sort (struct list
*list
, void *val
)
94 new = listnode_new ();
99 for (n
= list
->head
; n
; n
= n
->next
)
101 if ((*list
->cmp
) (val
, n
->data
) < 0)
117 new->prev
= list
->tail
;
120 list
->tail
->next
= new;
129 listnode_add_after (struct list
*list
, struct listnode
*pp
, void *val
)
133 assert (val
!= NULL
);
135 nn
= listnode_new ();
141 list
->head
->prev
= nn
;
145 nn
->next
= list
->head
;
167 listnode_add_before (struct list
*list
, struct listnode
*pp
, void *val
)
171 assert (val
!= NULL
);
173 nn
= listnode_new ();
179 list
->tail
->next
= nn
;
183 nn
->prev
= list
->tail
;
204 /* Move given listnode to tail of the list */
206 listnode_move_to_tail (struct list
*l
, struct listnode
*n
)
208 LISTNODE_DETACH(l
,n
);
209 LISTNODE_ATTACH(l
,n
);
212 /* Delete specific date pointer from the list. */
214 listnode_delete (struct list
*list
, void *val
)
216 struct listnode
*node
;
219 for (node
= list
->head
; node
; node
= node
->next
)
221 if (node
->data
== val
)
224 node
->prev
->next
= node
->next
;
226 list
->head
= node
->next
;
229 node
->next
->prev
= node
->prev
;
231 list
->tail
= node
->prev
;
234 listnode_free (node
);
240 /* Return first node's data if it is there. */
242 listnode_head (struct list
*list
)
244 struct listnode
*node
;
254 /* Delete all listnode from the list. */
256 list_delete_all_node (struct list
*list
)
258 struct listnode
*node
;
259 struct listnode
*next
;
262 for (node
= list
->head
; node
; node
= next
)
266 (*list
->del
) (node
->data
);
267 listnode_free (node
);
269 list
->head
= list
->tail
= NULL
;
273 /* Delete all listnode then free list itself. */
275 list_delete (struct list
*list
)
278 list_delete_all_node (list
);
282 /* Lookup the node which has given data. */
284 listnode_lookup (struct list
*list
, void *data
)
286 struct listnode
*node
;
289 for (node
= listhead(list
); node
; node
= listnextnode (node
))
290 if (data
== listgetdata (node
))
295 /* Delete the node from list. For ospfd and ospf6d. */
297 list_delete_node (struct list
*list
, struct listnode
*node
)
300 node
->prev
->next
= node
->next
;
302 list
->head
= node
->next
;
304 node
->next
->prev
= node
->prev
;
306 list
->tail
= node
->prev
;
308 listnode_free (node
);
313 list_add_list (struct list
*l
, struct list
*m
)
317 for (n
= listhead (m
); n
; n
= listnextnode (n
))
318 listnode_add (l
, n
->data
);