]> git.proxmox.com Git - mirror_frr.git/blob - lib/linklist.h
Merge branch 'frr/pull/550'
[mirror_frr.git] / lib / linklist.h
1 /* 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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #ifndef _ZEBRA_LINKLIST_H
22 #define _ZEBRA_LINKLIST_H
23
24 /* listnodes must always contain data to be valid. Adding an empty node
25 * to a list is invalid
26 */
27 struct listnode
28 {
29 struct listnode *next;
30 struct listnode *prev;
31
32 /* private member, use getdata() to retrieve, do not access directly */
33 void *data;
34 };
35
36 struct list
37 {
38 struct listnode *head;
39 struct listnode *tail;
40
41 /* invariant: count is the number of listnodes in the list */
42 unsigned int count;
43
44 /*
45 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
46 * Used as definition of sorted for listnode_add_sort
47 */
48 int (*cmp) (void *val1, void *val2);
49
50 /* callback to free user-owned data when listnode is deleted. supplying
51 * this callback is very much encouraged!
52 */
53 void (*del) (void *val);
54 };
55
56 #define listnextnode(X) ((X) ? ((X)->next) : NULL)
57 #define listhead(X) ((X) ? ((X)->head) : NULL)
58 #define listtail(X) ((X) ? ((X)->tail) : NULL)
59 #define listcount(X) ((X)->count)
60 #define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
61 #define listgetdata(X) (assert((X)->data != NULL), (X)->data)
62
63 /* Prototypes. */
64 extern struct list *list_new(void); /* encouraged: set list.del callback on new lists */
65 extern void list_free (struct list *);
66
67 extern void listnode_add (struct list *, void *);
68 extern void listnode_add_sort (struct list *, void *);
69 extern struct listnode *listnode_add_after (struct list *, struct listnode *, void *);
70 extern struct listnode *listnode_add_before (struct list *, struct listnode *, void *);
71 extern void listnode_move_to_tail (struct list *, struct listnode *);
72 extern void listnode_delete (struct list *, void *);
73 extern struct listnode *listnode_lookup (struct list *, void *);
74 extern void *listnode_head (struct list *);
75
76 extern void list_delete (struct list *);
77 extern void list_delete_all_node (struct list *);
78
79 /* For ospfd and ospf6d. */
80 extern void list_delete_node (struct list *, struct listnode *);
81
82 /* For ospf_spf.c */
83 extern void list_add_list (struct list *, struct list *);
84
85 /* List iteration macro.
86 * Usage: for (ALL_LIST_ELEMENTS (...) { ... }
87 * It is safe to delete the listnode using this macro.
88 */
89 #define ALL_LIST_ELEMENTS(list,node,nextnode,data) \
90 (node) = listhead(list), ((data) = NULL); \
91 (node) != NULL && \
92 ((data) = listgetdata(node),(nextnode) = node->next, 1); \
93 (node) = (nextnode), ((data) = NULL)
94
95 /* read-only list iteration macro.
96 * Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
97 * use this macro when it is *immediately obvious* the listnode is not
98 * deleted in the body of the loop. Does not have forward-reference overhead
99 * of previous macro.
100 */
101 #define ALL_LIST_ELEMENTS_RO(list,node,data) \
102 (node) = listhead(list), ((data) = NULL);\
103 (node) != NULL && ((data) = listgetdata(node), 1); \
104 (node) = listnextnode(node), ((data) = NULL)
105
106 /* these *do not* cleanup list nodes and referenced data, as the functions
107 * do - these macros simply {de,at}tach a listnode from/to a list.
108 */
109
110 /* List node attach macro. */
111 #define LISTNODE_ATTACH(L,N) \
112 do { \
113 (N)->prev = (L)->tail; \
114 (N)->next = NULL; \
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)
122
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)
136
137 #endif /* _ZEBRA_LINKLIST_H */