blob: b766420fadb862e9bbf164cac70a914187a55665 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Generic linked list
2 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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.
10 *
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.
15 *
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.
20 */
21
22#ifndef _ZEBRA_LINKLIST_H
23#define _ZEBRA_LINKLIST_H
24
paul718e3742002-12-13 20:15:29 +000025struct listnode
26{
27 struct listnode *next;
28 struct listnode *prev;
29 void *data;
30};
31
32struct list
33{
34 struct listnode *head;
35 struct listnode *tail;
gdta7a99902003-12-22 16:07:52 +000036 /* invariant: count is the number of listnodes in the list */
paul718e3742002-12-13 20:15:29 +000037 unsigned int count;
gdta7a99902003-12-22 16:07:52 +000038 /*
39 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
40 * Used as definition of sorted for listnode_add_sort
41 */
paul718e3742002-12-13 20:15:29 +000042 int (*cmp) (void *val1, void *val2);
43 void (*del) (void *val);
44};
45
46#define nextnode(X) ((X) = (X)->next)
47#define listhead(X) ((X)->head)
gdt630e4802004-08-31 17:28:41 +000048#define listtail(X) ((X)->tail)
paul718e3742002-12-13 20:15:29 +000049#define listcount(X) ((X)->count)
50#define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
51#define getdata(X) ((X)->data)
52
53/* Prototypes. */
54struct list *list_new();
55void list_free (struct list *);
56
57void listnode_add (struct list *, void *);
58void listnode_add_sort (struct list *, void *);
59void listnode_add_after (struct list *, struct listnode *, void *);
60void listnode_delete (struct list *, void *);
61struct listnode *listnode_lookup (struct list *, void *);
62void *listnode_head (struct list *);
63
64void list_delete (struct list *);
65void list_delete_all_node (struct list *);
66
67/* For ospfd and ospf6d. */
hasso52dc7ee2004-09-23 19:18:23 +000068void list_delete_node (struct list *, struct listnode *);
paul718e3742002-12-13 20:15:29 +000069
70/* For ospf_spf.c */
hasso52dc7ee2004-09-23 19:18:23 +000071void list_add_node_prev (struct list *, struct listnode *, void *);
72void list_add_node_next (struct list *, struct listnode *, void *);
73void list_add_list (struct list *, struct list *);
paul718e3742002-12-13 20:15:29 +000074
75/* List iteration macro. */
76#define LIST_LOOP(L,V,N) \
77 for ((N) = (L)->head; (N); (N) = (N)->next) \
78 if (((V) = (N)->data) != NULL)
79
80/* List node add macro. */
81#define LISTNODE_ADD(L,N) \
82 do { \
83 (N)->prev = (L)->tail; \
84 if ((L)->head == NULL) \
85 (L)->head = (N); \
86 else \
87 (L)->tail->next = (N); \
88 (L)->tail = (N); \
paul071fced2003-08-12 05:40:28 +000089 (L)->count++; \
paul718e3742002-12-13 20:15:29 +000090 } while (0)
91
92/* List node delete macro. */
93#define LISTNODE_DELETE(L,N) \
94 do { \
95 if ((N)->prev) \
96 (N)->prev->next = (N)->next; \
97 else \
98 (L)->head = (N)->next; \
99 if ((N)->next) \
100 (N)->next->prev = (N)->prev; \
101 else \
102 (L)->tail = (N)->prev; \
paul071fced2003-08-12 05:40:28 +0000103 (L)->count--; \
paul718e3742002-12-13 20:15:29 +0000104 } while (0)
105
106#endif /* _ZEBRA_LINKLIST_H */