]> git.proxmox.com Git - mirror_frr.git/blame - lib/linklist.h
2004-07-23 Paul Jakma <paul@dishone.st>
[mirror_frr.git] / lib / linklist.h
CommitLineData
718e3744 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
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;
a7a9990f 39 /* invariant: count is the number of listnodes in the list */
718e3744 40 unsigned int count;
a7a9990f 41 /*
42 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
43 * Used as definition of sorted for listnode_add_sort
44 */
718e3744 45 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)
51#define listcount(X) ((X)->count)
52#define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
53#define getdata(X) ((X)->data)
54
55/* Prototypes. */
56struct list *list_new();
57void list_free (struct list *);
58
59void listnode_add (struct list *, void *);
60void listnode_add_sort (struct list *, void *);
61void listnode_add_after (struct list *, struct listnode *, void *);
62void listnode_delete (struct list *, void *);
63struct listnode *listnode_lookup (struct list *, void *);
64void *listnode_head (struct list *);
65
66void list_delete (struct list *);
67void list_delete_all_node (struct list *);
68
69/* For ospfd and ospf6d. */
70void list_delete_node (list, listnode);
71
72/* For ospf_spf.c */
73void list_add_node_prev (list, listnode, void *);
74void list_add_node_next (list, listnode, void *);
75void list_add_list (list, list);
76
77/* List iteration macro. */
78#define LIST_LOOP(L,V,N) \
79 for ((N) = (L)->head; (N); (N) = (N)->next) \
80 if (((V) = (N)->data) != NULL)
81
82/* List node add macro. */
83#define LISTNODE_ADD(L,N) \
84 do { \
85 (N)->prev = (L)->tail; \
86 if ((L)->head == NULL) \
87 (L)->head = (N); \
88 else \
89 (L)->tail->next = (N); \
90 (L)->tail = (N); \
071fcedb 91 (L)->count++; \
718e3744 92 } while (0)
93
94/* List node delete macro. */
95#define LISTNODE_DELETE(L,N) \
96 do { \
97 if ((N)->prev) \
98 (N)->prev->next = (N)->next; \
99 else \
100 (L)->head = (N)->next; \
101 if ((N)->next) \
102 (N)->next->prev = (N)->prev; \
103 else \
104 (L)->tail = (N)->prev; \
071fcedb 105 (L)->count--; \
718e3744 106 } while (0)
107
108#endif /* _ZEBRA_LINKLIST_H */