Merged own patch for bug #390 (rewrite zebra/zebra_rib.c:nexthop_active_update())
[jleu-quagga.git] / lib / linklist.h
blobcc6867cd4881356b474f8e33c70cd38cd25269d5
1 /* Generic linked list
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 #ifndef _ZEBRA_LINKLIST_H
23 #define _ZEBRA_LINKLIST_H
25 /* listnodes must always contain data to be valid. Adding an empty node
26 * to a list is invalid
28 struct listnode
30 struct listnode *next;
31 struct listnode *prev;
33 /* private member, use getdata() to retrieve, do not access directly */
34 void *data;
37 struct list
39 struct listnode *head;
40 struct listnode *tail;
42 /* invariant: count is the number of listnodes in the list */
43 unsigned int count;
46 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
47 * Used as definition of sorted for listnode_add_sort
49 int (*cmp) (void *val1, void *val2);
51 /* callback to free user-owned data when listnode is deleted. supplying
52 * this callback is very much encouraged!
54 void (*del) (void *val);
57 #define listnextnode(X) ((X)->next)
58 #define listhead(X) ((X)->head)
59 #define listtail(X) ((X)->tail)
60 #define listcount(X) ((X)->count)
61 #define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
62 #define listgetdata(X) (assert((X)->data != NULL), (X)->data)
64 /* Prototypes. */
65 extern struct list *list_new(void); /* encouraged: set list.del callback on new lists */
66 extern void list_free (struct list *);
68 extern void listnode_add (struct list *, void *);
69 extern void listnode_add_sort (struct list *, void *);
70 extern void listnode_add_after (struct list *, struct listnode *, void *);
71 extern void listnode_delete (struct list *, void *);
72 extern struct listnode *listnode_lookup (struct list *, void *);
73 extern void *listnode_head (struct list *);
75 extern void list_delete (struct list *);
76 extern void list_delete_all_node (struct list *);
78 /* For ospfd and ospf6d. */
79 extern void list_delete_node (struct list *, struct listnode *);
81 /* For ospf_spf.c */
82 extern void list_add_node_prev (struct list *, struct listnode *, void *);
83 extern void list_add_node_next (struct list *, struct listnode *, void *);
84 extern void list_add_list (struct list *, struct list *);
86 /* List iteration macro.
87 * Usage: for (ALL_LIST_ELEMENTS (...) { ... }
88 * It is safe to delete the listnode using this macro.
90 #define ALL_LIST_ELEMENTS(list,node,nextnode,data) \
91 (node) = listhead(list); \
92 (node) != NULL && \
93 ((data) = listgetdata(node),(nextnode) = listnextnode(node), 1); \
94 (node) = (nextnode)
96 /* read-only list iteration macro.
97 * Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
98 * use this macro when it is *immediately obvious* the listnode is not
99 * deleted in the body of the loop. Does not have forward-reference overhead
100 * of previous macro.
102 #define ALL_LIST_ELEMENTS_RO(list,node,data) \
103 (node) = listhead(list); \
104 (node) != NULL && ((data) = listgetdata(node), 1); \
105 (node) = listnextnode(node)
107 /* these *do not* cleanup list nodes and referenced data, as the functions
108 * do - these macros simply {de,at}tach a listnode from/to a list.
111 /* List node attach macro. */
112 #define LISTNODE_ATTACH(L,N) \
113 do { \
114 (N)->prev = (L)->tail; \
115 if ((L)->head == NULL) \
116 (L)->head = (N); \
117 else \
118 (L)->tail->next = (N); \
119 (L)->tail = (N); \
120 (L)->count++; \
121 } while (0)
123 /* List node detach macro. */
124 #define LISTNODE_DETACH(L,N) \
125 do { \
126 if ((N)->prev) \
127 (N)->prev->next = (N)->next; \
128 else \
129 (L)->head = (N)->next; \
130 if ((N)->next) \
131 (N)->next->prev = (N)->prev; \
132 else \
133 (L)->tail = (N)->prev; \
134 (L)->count--; \
135 } while (0)
137 /* Deprecated: 20050406 */
138 #if !defined(QUAGGA_NO_DEPRECATED_INTERFACES)
139 #warning "Using deprecated libzebra interfaces"
140 #define LISTNODE_ADD(L,N) LISTNODE_ATTACH(L,N)
141 #define LISTNODE_DELETE(L,N) LISTNODE_DETACH(L,N)
142 #define nextnode(X) ((X) = (X)->next)
143 #define getdata(X) listgetdata(X)
144 #define LIST_LOOP(L,V,N) \
145 for (ALL_LIST_ELEMENTS_RO (L,N,V))
146 #endif /* QUAGGA_NO_DEPRECATED_INTERFACES */
148 #endif /* _ZEBRA_LINKLIST_H */