blob: 303b0bced507a34fa2b0e315e64075615a0ab655 [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
25typedef struct list *list;
26typedef struct listnode *listnode;
27
28struct listnode
29{
30 struct listnode *next;
31 struct listnode *prev;
32 void *data;
33};
34
35struct list
36{
37 struct listnode *head;
38 struct listnode *tail;
gdta7a99902003-12-22 16:07:52 +000039 /* invariant: count is the number of listnodes in the list */
paul718e3742002-12-13 20:15:29 +000040 unsigned int count;
gdta7a99902003-12-22 16:07:52 +000041 /*
42 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
43 * Used as definition of sorted for listnode_add_sort
44 */
paul718e3742002-12-13 20:15:29 +000045 int (*cmp) (void *val1, void *val2);
46 void (*del) (void *val);
47};
48
49#define nextnode(X) ((X) = (X)->next)
50#define listhead(X) ((X)->head)
gdt630e4802004-08-31 17:28:41 +000051#define listtail(X) ((X)->tail)
paul718e3742002-12-13 20:15:29 +000052#define listcount(X) ((X)->count)
53#define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
54#define getdata(X) ((X)->data)
55
56/* Prototypes. */
57struct list *list_new();
58void list_free (struct list *);
59
60void listnode_add (struct list *, void *);
61void listnode_add_sort (struct list *, void *);
62void listnode_add_after (struct list *, struct listnode *, void *);
63void listnode_delete (struct list *, void *);
64struct listnode *listnode_lookup (struct list *, void *);
65void *listnode_head (struct list *);
66
67void list_delete (struct list *);
68void list_delete_all_node (struct list *);
69
70/* For ospfd and ospf6d. */
71void list_delete_node (list, listnode);
72
73/* For ospf_spf.c */
74void list_add_node_prev (list, listnode, void *);
75void list_add_node_next (list, listnode, void *);
76void list_add_list (list, list);
77
78/* List iteration macro. */
79#define LIST_LOOP(L,V,N) \
80 for ((N) = (L)->head; (N); (N) = (N)->next) \
81 if (((V) = (N)->data) != NULL)
82
83/* List node add macro. */
84#define LISTNODE_ADD(L,N) \
85 do { \
86 (N)->prev = (L)->tail; \
87 if ((L)->head == NULL) \
88 (L)->head = (N); \
89 else \
90 (L)->tail->next = (N); \
91 (L)->tail = (N); \
paul071fced2003-08-12 05:40:28 +000092 (L)->count++; \
paul718e3742002-12-13 20:15:29 +000093 } while (0)
94
95/* List node delete macro. */
96#define LISTNODE_DELETE(L,N) \
97 do { \
98 if ((N)->prev) \
99 (N)->prev->next = (N)->next; \
100 else \
101 (L)->head = (N)->next; \
102 if ((N)->next) \
103 (N)->next->prev = (N)->prev; \
104 else \
105 (L)->tail = (N)->prev; \
paul071fced2003-08-12 05:40:28 +0000106 (L)->count--; \
paul718e3742002-12-13 20:15:29 +0000107 } while (0)
108
109#endif /* _ZEBRA_LINKLIST_H */