blob: 96aaf4319602e0aea3ab7ee6da065f09cee6e7f7 [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
paul1eb8ef22005-04-07 07:30:20 +000025/* listnodes must always contain data to be valid. Adding an empty node
26 * to a list is invalid
27 */
paul718e3742002-12-13 20:15:29 +000028struct listnode
29{
30 struct listnode *next;
31 struct listnode *prev;
paul1eb8ef22005-04-07 07:30:20 +000032
33 /* private member, use getdata() to retrieve, do not access directly */
paul718e3742002-12-13 20:15:29 +000034 void *data;
35};
36
37struct list
38{
39 struct listnode *head;
40 struct listnode *tail;
paul1eb8ef22005-04-07 07:30:20 +000041
gdta7a99902003-12-22 16:07:52 +000042 /* invariant: count is the number of listnodes in the list */
paul718e3742002-12-13 20:15:29 +000043 unsigned int count;
paul1eb8ef22005-04-07 07:30:20 +000044
gdta7a99902003-12-22 16:07:52 +000045 /*
46 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
47 * Used as definition of sorted for listnode_add_sort
48 */
paul718e3742002-12-13 20:15:29 +000049 int (*cmp) (void *val1, void *val2);
paul1eb8ef22005-04-07 07:30:20 +000050
51 /* callback to free user-owned data when listnode is deleted. supplying
52 * this callback is very much encouraged!
53 */
paul718e3742002-12-13 20:15:29 +000054 void (*del) (void *val);
55};
56
Josh Bailey54dd6122012-03-21 10:00:07 -070057#define listnextnode(X) ((X) ? ((X)->next) : NULL)
58#define listhead(X) ((X) ? ((X)->head) : NULL)
59#define listtail(X) ((X) ? ((X)->tail) : NULL)
paul718e3742002-12-13 20:15:29 +000060#define listcount(X) ((X)->count)
61#define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
paul1eb8ef22005-04-07 07:30:20 +000062#define listgetdata(X) (assert((X)->data != NULL), (X)->data)
paul718e3742002-12-13 20:15:29 +000063
64/* Prototypes. */
paul8cc41982005-05-06 21:25:49 +000065extern struct list *list_new(void); /* encouraged: set list.del callback on new lists */
66extern void list_free (struct list *);
paul718e3742002-12-13 20:15:29 +000067
paul8cc41982005-05-06 21:25:49 +000068extern void listnode_add (struct list *, void *);
69extern void listnode_add_sort (struct list *, void *);
70extern void listnode_add_after (struct list *, struct listnode *, void *);
David Lamparter449b29e2016-06-13 17:29:12 +020071extern struct listnode *listnode_add_before (struct list *, struct listnode *, void *);
Paul Jakma6d831132014-10-09 16:05:15 +010072extern void listnode_move_to_tail (struct list *, struct listnode *);
paul8cc41982005-05-06 21:25:49 +000073extern void listnode_delete (struct list *, void *);
74extern struct listnode *listnode_lookup (struct list *, void *);
75extern void *listnode_head (struct list *);
paul718e3742002-12-13 20:15:29 +000076
paul8cc41982005-05-06 21:25:49 +000077extern void list_delete (struct list *);
78extern void list_delete_all_node (struct list *);
paul718e3742002-12-13 20:15:29 +000079
80/* For ospfd and ospf6d. */
paul8cc41982005-05-06 21:25:49 +000081extern void list_delete_node (struct list *, struct listnode *);
paul718e3742002-12-13 20:15:29 +000082
83/* For ospf_spf.c */
paul8cc41982005-05-06 21:25:49 +000084extern void list_add_node_prev (struct list *, struct listnode *, void *);
85extern void list_add_node_next (struct list *, struct listnode *, void *);
86extern void list_add_list (struct list *, struct list *);
paul718e3742002-12-13 20:15:29 +000087
paul1eb8ef22005-04-07 07:30:20 +000088/* List iteration macro.
89 * Usage: for (ALL_LIST_ELEMENTS (...) { ... }
90 * It is safe to delete the listnode using this macro.
91 */
92#define ALL_LIST_ELEMENTS(list,node,nextnode,data) \
Josh Bailey54dd6122012-03-21 10:00:07 -070093 (node) = listhead(list), ((data) = NULL); \
paul1eb8ef22005-04-07 07:30:20 +000094 (node) != NULL && \
David Lampartera5c851c2012-11-27 03:21:44 +010095 ((data) = listgetdata(node),(nextnode) = node->next, 1); \
Josh Bailey54dd6122012-03-21 10:00:07 -070096 (node) = (nextnode), ((data) = NULL)
paul718e3742002-12-13 20:15:29 +000097
paul1eb8ef22005-04-07 07:30:20 +000098/* read-only list iteration macro.
99 * Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
100 * use this macro when it is *immediately obvious* the listnode is not
101 * deleted in the body of the loop. Does not have forward-reference overhead
102 * of previous macro.
103 */
104#define ALL_LIST_ELEMENTS_RO(list,node,data) \
Josh Bailey54dd6122012-03-21 10:00:07 -0700105 (node) = listhead(list), ((data) = NULL);\
paul1eb8ef22005-04-07 07:30:20 +0000106 (node) != NULL && ((data) = listgetdata(node), 1); \
Josh Bailey54dd6122012-03-21 10:00:07 -0700107 (node) = listnextnode(node), ((data) = NULL)
paul1eb8ef22005-04-07 07:30:20 +0000108
109/* these *do not* cleanup list nodes and referenced data, as the functions
110 * do - these macros simply {de,at}tach a listnode from/to a list.
111 */
112
113/* List node attach macro. */
114#define LISTNODE_ATTACH(L,N) \
paul718e3742002-12-13 20:15:29 +0000115 do { \
116 (N)->prev = (L)->tail; \
David Lamparter1c6db0d2014-12-12 21:35:28 +0100117 (N)->next = NULL; \
paul718e3742002-12-13 20:15:29 +0000118 if ((L)->head == NULL) \
119 (L)->head = (N); \
120 else \
121 (L)->tail->next = (N); \
122 (L)->tail = (N); \
paul071fced2003-08-12 05:40:28 +0000123 (L)->count++; \
paul718e3742002-12-13 20:15:29 +0000124 } while (0)
125
paul1eb8ef22005-04-07 07:30:20 +0000126/* List node detach macro. */
127#define LISTNODE_DETACH(L,N) \
paul718e3742002-12-13 20:15:29 +0000128 do { \
129 if ((N)->prev) \
130 (N)->prev->next = (N)->next; \
131 else \
132 (L)->head = (N)->next; \
133 if ((N)->next) \
134 (N)->next->prev = (N)->prev; \
135 else \
136 (L)->tail = (N)->prev; \
paul071fced2003-08-12 05:40:28 +0000137 (L)->count--; \
paul718e3742002-12-13 20:15:29 +0000138 } while (0)
139
paul1eb8ef22005-04-07 07:30:20 +0000140/* Deprecated: 20050406 */
141#if !defined(QUAGGA_NO_DEPRECATED_INTERFACES)
142#warning "Using deprecated libzebra interfaces"
143#define LISTNODE_ADD(L,N) LISTNODE_ATTACH(L,N)
144#define LISTNODE_DELETE(L,N) LISTNODE_DETACH(L,N)
145#define nextnode(X) ((X) = (X)->next)
146#define getdata(X) listgetdata(X)
147#define LIST_LOOP(L,V,N) \
148 for (ALL_LIST_ELEMENTS_RO (L,N,V))
149#endif /* QUAGGA_NO_DEPRECATED_INTERFACES */
150
paul718e3742002-12-13 20:15:29 +0000151#endif /* _ZEBRA_LINKLIST_H */