MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / zebra / lib / linklist.c
blob5a2b696930458672fe72f6ceca0d37bc089717c9
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 ()
31 struct list *new;
33 new = XMALLOC (MTYPE_LINK_LIST, sizeof (struct list));
34 memset (new, 0, sizeof (struct list));
35 return new;
38 /* Free list. */
39 void
40 list_free (struct list *l)
42 XFREE (MTYPE_LINK_LIST, l);
45 /* Allocate new listnode. Internal use only. */
46 static struct listnode *
47 listnode_new ()
49 struct listnode *node;
51 node = XMALLOC (MTYPE_LINK_NODE, sizeof (struct listnode));
52 memset (node, 0, sizeof (struct listnode));
53 return node;
56 /* Free listnode. */
57 static void
58 listnode_free (struct listnode *node)
60 XFREE (MTYPE_LINK_NODE, node);
63 /* Add new data to the list. */
64 void
65 listnode_add (struct list *list, void *val)
67 struct listnode *node;
69 node = listnode_new ();
71 node->prev = list->tail;
72 node->data = val;
74 if (list->head == NULL)
75 list->head = node;
76 else
77 list->tail->next = node;
78 list->tail = node;
80 list->count++;
83 /* Add new node with sort function. */
84 void
85 listnode_add_sort (struct list *list, void *val)
87 struct listnode *n;
88 struct listnode *new;
90 new = listnode_new ();
91 new->data = val;
93 if (list->cmp)
95 for (n = list->head; n; n = n->next)
97 if ((*list->cmp) (val, n->data) < 0)
99 new->next = n;
100 new->prev = n->prev;
102 if (n->prev)
103 n->prev->next = new;
104 else
105 list->head = new;
106 n->prev = new;
107 list->count++;
108 return;
113 new->prev = list->tail;
115 if (list->tail)
116 list->tail->next = new;
117 else
118 list->head = new;
120 list->tail = new;
121 list->count++;
124 void
125 listnode_add_after (struct list *list, struct listnode *pp, void *val)
127 struct listnode *nn;
129 nn = listnode_new ();
130 nn->data = val;
132 if (pp == NULL)
134 if (list->head)
135 list->head->prev = nn;
136 else
137 list->tail = nn;
139 nn->next = list->head;
140 nn->prev = pp;
142 list->head = nn;
144 else
146 if (pp->next)
147 pp->next->prev = nn;
148 else
149 list->tail = nn;
151 nn->next = pp->next;
152 nn->prev = pp;
154 pp->next = nn;
159 /* Delete specific date pointer from the list. */
160 void
161 listnode_delete (struct list *list, void *val)
163 struct listnode *node;
165 for (node = list->head; node; node = node->next)
167 if (node->data == val)
169 if (node->prev)
170 node->prev->next = node->next;
171 else
172 list->head = node->next;
174 if (node->next)
175 node->next->prev = node->prev;
176 else
177 list->tail = node->prev;
179 list->count--;
180 listnode_free (node);
181 return;
186 /* Return first node's data if it is there. */
187 void *
188 listnode_head (struct list *list)
190 struct listnode *node;
192 node = list->head;
194 if (node)
195 return node->data;
196 return NULL;
199 /* Delete all listnode from the list. */
200 void
201 list_delete_all_node (struct list *list)
203 struct listnode *node;
204 struct listnode *next;
206 for (node = list->head; node; node = next)
208 next = node->next;
209 if (list->del)
210 (*list->del) (node->data);
211 listnode_free (node);
213 list->head = list->tail = NULL;
214 list->count = 0;
217 /* Delete all listnode then free list itself. */
218 void
219 list_delete (struct list *list)
221 struct listnode *node;
222 struct listnode *next;
224 for (node = list->head; node; node = next)
226 next = node->next;
227 if (list->del)
228 (*list->del) (node->data);
229 listnode_free (node);
231 list_free (list);
234 /* Lookup the node which has given data. */
235 struct listnode *
236 listnode_lookup (struct list *list, void *data)
238 listnode node;
240 for (node = list->head; node; nextnode (node))
241 if (data == getdata (node))
242 return node;
243 return NULL;
246 /* Delete the node from list. For ospfd and ospf6d. */
247 void
248 list_delete_node (list list, 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 (list list, listnode current, void *val)
266 struct listnode *node;
268 node = listnode_new ();
269 node->next = current;
270 node->data = val;
272 if (current->prev == NULL)
273 list->head = node;
274 else
275 current->prev->next = node;
277 node->prev = current->prev;
278 current->prev = node;
280 list->count++;
283 /* ospf_spf.c */
284 void
285 list_add_node_next (list list, listnode current, void *val)
287 struct listnode *node;
289 node = listnode_new ();
290 node->prev = current;
291 node->data = val;
293 if (current->next == NULL)
294 list->tail = node;
295 else
296 current->next->prev = node;
298 node->next = current->next;
299 current->next = node;
301 list->count++;
304 /* ospf_spf.c */
305 void
306 list_add_list (struct list *l, struct list *m)
308 struct listnode *n;
310 for (n = listhead (m); n; nextnode (n))
311 listnode_add (l, n->data);