]>
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 /* Allocate new list. */
31 return XCALLOC (MTYPE_LINK_LIST
, sizeof (struct list
));
36 list_free (struct list
*l
)
38 XFREE (MTYPE_LINK_LIST
, l
);
41 /* Allocate new listnode. Internal use only. */
42 static struct listnode
*
45 return XCALLOC (MTYPE_LINK_NODE
, sizeof (struct listnode
));
50 listnode_free (struct listnode
*node
)
52 XFREE (MTYPE_LINK_NODE
, node
);
55 /* Add new data to the list. */
57 listnode_add (struct list
*list
, void *val
)
59 struct listnode
*node
;
63 node
= listnode_new ();
65 node
->prev
= list
->tail
;
68 if (list
->head
== NULL
)
71 list
->tail
->next
= node
;
78 * Add a node to the list. If the list was sorted according to the
79 * cmp function, insert a new node with the given val such that the
80 * list remains sorted. The new node is always inserted; there is no
81 * notion of omitting duplicates.
84 listnode_add_sort (struct list
*list
, void *val
)
91 new = listnode_new ();
96 for (n
= list
->head
; n
; n
= n
->next
)
98 if ((*list
->cmp
) (val
, n
->data
) < 0)
114 new->prev
= list
->tail
;
117 list
->tail
->next
= new;
126 listnode_add_after (struct list
*list
, struct listnode
*pp
, void *val
)
130 assert (val
!= NULL
);
132 nn
= listnode_new ();
138 list
->head
->prev
= nn
;
142 nn
->next
= list
->head
;
162 /* Move given listnode to tail of the list */
164 listnode_move_to_tail (struct list
*l
, struct listnode
*n
)
166 LISTNODE_DETACH(l
,n
);
167 LISTNODE_ATTACH(l
,n
);
170 /* Delete specific date pointer from the list. */
172 listnode_delete (struct list
*list
, void *val
)
174 struct listnode
*node
;
177 for (node
= list
->head
; node
; node
= node
->next
)
179 if (node
->data
== val
)
182 node
->prev
->next
= node
->next
;
184 list
->head
= node
->next
;
187 node
->next
->prev
= node
->prev
;
189 list
->tail
= node
->prev
;
192 listnode_free (node
);
198 /* Return first node's data if it is there. */
200 listnode_head (struct list
*list
)
202 struct listnode
*node
;
212 /* Delete all listnode from the list. */
214 list_delete_all_node (struct list
*list
)
216 struct listnode
*node
;
217 struct listnode
*next
;
220 for (node
= list
->head
; node
; node
= next
)
224 (*list
->del
) (node
->data
);
225 listnode_free (node
);
227 list
->head
= list
->tail
= NULL
;
231 /* Delete all listnode then free list itself. */
233 list_delete (struct list
*list
)
236 list_delete_all_node (list
);
240 /* Lookup the node which has given data. */
242 listnode_lookup (struct list
*list
, void *data
)
244 struct listnode
*node
;
247 for (node
= listhead(list
); node
; node
= listnextnode (node
))
248 if (data
== listgetdata (node
))
253 /* Delete the node from list. For ospfd and ospf6d. */
255 list_delete_node (struct list
*list
, struct listnode
*node
)
258 node
->prev
->next
= node
->next
;
260 list
->head
= node
->next
;
262 node
->next
->prev
= node
->prev
;
264 list
->tail
= node
->prev
;
266 listnode_free (node
);
271 list_add_node_prev (struct list
*list
, struct listnode
*current
, void *val
)
273 struct listnode
*node
;
275 assert (val
!= NULL
);
277 node
= listnode_new ();
278 node
->next
= current
;
281 if (current
->prev
== NULL
)
284 current
->prev
->next
= node
;
286 node
->prev
= current
->prev
;
287 current
->prev
= node
;
294 list_add_node_next (struct list
*list
, struct listnode
*current
, void *val
)
296 struct listnode
*node
;
298 assert (val
!= NULL
);
300 node
= listnode_new ();
301 node
->prev
= current
;
304 if (current
->next
== NULL
)
307 current
->next
->prev
= node
;
309 node
->next
= current
->next
;
310 current
->next
= node
;
317 list_add_list (struct list
*l
, struct list
*m
)
321 for (n
= listhead (m
); n
; n
= listnextnode (n
))
322 listnode_add (l
, n
->data
);