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;
39  unsigned int count;
40  int (*cmp) (void *val1, void *val2);
41  void (*del) (void *val);
42};
43
44#define nextnode(X) ((X) = (X)->next)
45#define listhead(X) ((X)->head)
46#define listcount(X) ((X)->count)
47#define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
48#define getdata(X) ((X)->data)
49
50/* Prototypes. */
51struct list *list_new();
52void list_free (struct list *);
53
54void listnode_add (struct list *, void *);
55void listnode_add_sort (struct list *, void *);
56void listnode_add_after (struct list *, struct listnode *, void *);
57void listnode_delete (struct list *, void *);
58struct listnode *listnode_lookup (struct list *, void *);
59void *listnode_head (struct list *);
60
61void list_delete (struct list *);
62void list_delete_all_node (struct list *);
63
64/* For ospfd and ospf6d. */
65void list_delete_node (list, listnode);
66
67/* For ospf_spf.c */
68void list_add_node_prev (list, listnode, void *);
69void list_add_node_next (list, listnode, void *);
70void list_add_list (list, list);
71
72/* List iteration macro. */
73#define LIST_LOOP(L,V,N) \
74  for ((N) = (L)->head; (N); (N) = (N)->next) \
75    if (((V) = (N)->data) != NULL)
76
77/* List node add macro.  */
78#define LISTNODE_ADD(L,N) \
79  do { \
80    (N)->prev = (L)->tail; \
81    if ((L)->head == NULL) \
82      (L)->head = (N); \
83    else \
84      (L)->tail->next = (N); \
85    (L)->tail = (N); \
86  } while (0)
87
88/* List node delete macro.  */
89#define LISTNODE_DELETE(L,N) \
90  do { \
91    if ((N)->prev) \
92      (N)->prev->next = (N)->next; \
93    else \
94      (L)->head = (N)->next; \
95    if ((N)->next) \
96      (N)->next->prev = (N)->prev; \
97    else \
98      (L)->tail = (N)->prev; \
99  } while (0)
100
101#endif /* _ZEBRA_LINKLIST_H */
102