]>
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. */
33 new = XMALLOC (MTYPE_LINK_LIST
, sizeof (struct list
));
34 memset (new, 0, sizeof (struct list
));
40 list_free (struct list
*l
)
42 XFREE (MTYPE_LINK_LIST
, l
);
45 /* Allocate new listnode. Internal use only. */
46 static struct listnode
*
49 struct listnode
*node
;
51 node
= XMALLOC (MTYPE_LINK_NODE
, sizeof (struct listnode
));
52 memset (node
, 0, sizeof (struct listnode
));
58 listnode_free (struct listnode
*node
)
60 XFREE (MTYPE_LINK_NODE
, node
);
63 /* Add new data to the list. */
65 listnode_add (struct list
*list
, void *val
)
67 struct listnode
*node
;
69 node
= listnode_new ();
71 node
->prev
= list
->tail
;
74 if (list
->head
== NULL
)
77 list
->tail
->next
= node
;
84 * Add a node to the list. If the list was sorted according to the
85 * cmp function, insert a new node with the given val such that the
86 * list remains sorted. The new node is always inserted; there is no
87 * notion of omitting duplicates.
90 listnode_add_sort (struct list
*list
, void *val
)
95 new = listnode_new ();
100 for (n
= list
->head
; n
; n
= n
->next
)
102 if ((*list
->cmp
) (val
, n
->data
) < 0)
118 new->prev
= list
->tail
;
121 list
->tail
->next
= new;
130 listnode_add_after (struct list
*list
, struct listnode
*pp
, void *val
)
134 nn
= listnode_new ();
140 list
->head
->prev
= nn
;
144 nn
->next
= list
->head
;
164 /* Delete specific date pointer from the list. */
166 listnode_delete (struct list
*list
, void *val
)
168 struct listnode
*node
;
171 for (node
= list
->head
; node
; node
= node
->next
)
173 if (node
->data
== val
)
176 node
->prev
->next
= node
->next
;
178 list
->head
= node
->next
;
181 node
->next
->prev
= node
->prev
;
183 list
->tail
= node
->prev
;
186 listnode_free (node
);
192 /* Return first node's data if it is there. */
194 listnode_head (struct list
*list
)
196 struct listnode
*node
;
206 /* Delete all listnode from the list. */
208 list_delete_all_node (struct list
*list
)
210 struct listnode
*node
;
211 struct listnode
*next
;
214 for (node
= list
->head
; node
; node
= next
)
218 (*list
->del
) (node
->data
);
219 listnode_free (node
);
221 list
->head
= list
->tail
= NULL
;
225 /* Delete all listnode then free list itself. */
227 list_delete (struct list
*list
)
229 struct listnode
*node
;
230 struct listnode
*next
;
233 for (node
= list
->head
; node
; node
= next
)
237 (*list
->del
) (node
->data
);
238 listnode_free (node
);
243 /* Lookup the node which has given data. */
245 listnode_lookup (struct list
*list
, void *data
)
247 struct listnode
*node
;
250 for (node
= listhead(list
); node
; node
= listnextnode (node
))
251 if (data
== listgetdata (node
))
256 /* Delete the node from list. For ospfd and ospf6d. */
258 list_delete_node (struct list
*list
, struct listnode
*node
)
261 node
->prev
->next
= node
->next
;
263 list
->head
= node
->next
;
265 node
->next
->prev
= node
->prev
;
267 list
->tail
= node
->prev
;
269 listnode_free (node
);
274 list_add_node_prev (struct list
*list
, struct listnode
*current
, void *val
)
276 struct listnode
*node
;
278 node
= listnode_new ();
279 node
->next
= current
;
282 if (current
->prev
== NULL
)
285 current
->prev
->next
= node
;
287 node
->prev
= current
->prev
;
288 current
->prev
= node
;
295 list_add_node_next (struct list
*list
, struct listnode
*current
, void *val
)
297 struct listnode
*node
;
299 node
= listnode_new ();
300 node
->prev
= current
;
303 if (current
->next
== NULL
)
306 current
->next
->prev
= node
;
308 node
->next
= current
->next
;
309 current
->next
= node
;
316 list_add_list (struct list
*l
, struct list
*m
)
320 for (n
= listhead (m
); n
; n
= listnextnode (n
))
321 listnode_add (l
, n
->data
);