ospfd: Only refresh external default route once.
[jleu-quagga.git] / lib / linklist.c
blob485a80bee1fe8eede7f3aaa0e74a71533e71c943
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
9 * later version.
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
19 * 02111-1307, USA.
22 #include <zebra.h>
24 #include "linklist.h"
25 #include "memory.h"
27 /* Allocate new list. */
28 struct list *
29 list_new (void)
31 return XCALLOC (MTYPE_LINK_LIST, sizeof (struct list));
34 /* Free list. */
35 void
36 list_free (struct list *l)
38 XFREE (MTYPE_LINK_LIST, l);
41 /* Allocate new listnode. Internal use only. */
42 static struct listnode *
43 listnode_new (void)
45 return XCALLOC (MTYPE_LINK_NODE, sizeof (struct listnode));
48 /* Free listnode. */
49 static void
50 listnode_free (struct listnode *node)
52 XFREE (MTYPE_LINK_NODE, node);
55 /* Add new data to the list. */
56 void
57 listnode_add (struct list *list, void *val)
59 struct listnode *node;
61 assert (val != NULL);
63 node = listnode_new ();
65 node->prev = list->tail;
66 node->data = val;
68 if (list->head == NULL)
69 list->head = node;
70 else
71 list->tail->next = node;
72 list->tail = node;
74 list->count++;
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.
83 void
84 listnode_add_sort (struct list *list, void *val)
86 struct listnode *n;
87 struct listnode *new;
89 assert (val != NULL);
91 new = listnode_new ();
92 new->data = val;
94 if (list->cmp)
96 for (n = list->head; n; n = n->next)
98 if ((*list->cmp) (val, n->data) < 0)
100 new->next = n;
101 new->prev = n->prev;
103 if (n->prev)
104 n->prev->next = new;
105 else
106 list->head = new;
107 n->prev = new;
108 list->count++;
109 return;
114 new->prev = list->tail;
116 if (list->tail)
117 list->tail->next = new;
118 else
119 list->head = new;
121 list->tail = new;
122 list->count++;
125 void
126 listnode_add_after (struct list *list, struct listnode *pp, void *val)
128 struct listnode *nn;
130 assert (val != NULL);
132 nn = listnode_new ();
133 nn->data = val;
135 if (pp == NULL)
137 if (list->head)
138 list->head->prev = nn;
139 else
140 list->tail = nn;
142 nn->next = list->head;
143 nn->prev = pp;
145 list->head = nn;
147 else
149 if (pp->next)
150 pp->next->prev = nn;
151 else
152 list->tail = nn;
154 nn->next = pp->next;
155 nn->prev = pp;
157 pp->next = nn;
159 list->count++;
163 /* Delete specific date pointer from the list. */
164 void
165 listnode_delete (struct list *list, void *val)
167 struct listnode *node;
169 assert(list);
170 for (node = list->head; node; node = node->next)
172 if (node->data == val)
174 if (node->prev)
175 node->prev->next = node->next;
176 else
177 list->head = node->next;
179 if (node->next)
180 node->next->prev = node->prev;
181 else
182 list->tail = node->prev;
184 list->count--;
185 listnode_free (node);
186 return;
191 /* Return first node's data if it is there. */
192 void *
193 listnode_head (struct list *list)
195 struct listnode *node;
197 assert(list);
198 node = list->head;
200 if (node)
201 return node->data;
202 return NULL;
205 /* Delete all listnode from the list. */
206 void
207 list_delete_all_node (struct list *list)
209 struct listnode *node;
210 struct listnode *next;
212 assert(list);
213 for (node = list->head; node; node = next)
215 next = node->next;
216 if (list->del)
217 (*list->del) (node->data);
218 listnode_free (node);
220 list->head = list->tail = NULL;
221 list->count = 0;
224 /* Delete all listnode then free list itself. */
225 void
226 list_delete (struct list *list)
228 assert(list);
229 list_delete_all_node (list);
230 list_free (list);
233 /* Lookup the node which has given data. */
234 struct listnode *
235 listnode_lookup (struct list *list, void *data)
237 struct listnode *node;
239 assert(list);
240 for (node = listhead(list); node; node = listnextnode (node))
241 if (data == listgetdata (node))
242 return node;
243 return NULL;
246 /* Delete the node from list. For ospfd and ospf6d. */
247 void
248 list_delete_node (struct list *list, struct listnode *node)
250 if (node->prev)
251 node->prev->next = node->next;
252 else
253 list->head = node->next;
254 if (node->next)
255 node->next->prev = node->prev;
256 else
257 list->tail = node->prev;
258 list->count--;
259 listnode_free (node);
262 /* ospf_spf.c */
263 void
264 list_add_node_prev (struct list *list, struct listnode *current, void *val)
266 struct listnode *node;
268 assert (val != NULL);
270 node = listnode_new ();
271 node->next = current;
272 node->data = val;
274 if (current->prev == NULL)
275 list->head = node;
276 else
277 current->prev->next = node;
279 node->prev = current->prev;
280 current->prev = node;
282 list->count++;
285 /* ospf_spf.c */
286 void
287 list_add_node_next (struct list *list, struct listnode *current, void *val)
289 struct listnode *node;
291 assert (val != NULL);
293 node = listnode_new ();
294 node->prev = current;
295 node->data = val;
297 if (current->next == NULL)
298 list->tail = node;
299 else
300 current->next->prev = node;
302 node->next = current->next;
303 current->next = node;
305 list->count++;
308 /* ospf_spf.c */
309 void
310 list_add_list (struct list *l, struct list *m)
312 struct listnode *n;
314 for (n = listhead (m); n; n = listnextnode (n))
315 listnode_add (l, n->data);