]>
Commit | Line | Data |
---|---|---|
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 | ||
718e3744 | 25 | struct listnode |
26 | { | |
27 | struct listnode *next; | |
28 | struct listnode *prev; | |
29 | void *data; | |
30 | }; | |
31 | ||
32 | struct list | |
33 | { | |
34 | struct listnode *head; | |
35 | struct listnode *tail; | |
a7a9990f | 36 | /* invariant: count is the number of listnodes in the list */ |
718e3744 | 37 | unsigned int count; |
a7a9990f | 38 | /* |
39 | * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2. | |
40 | * Used as definition of sorted for listnode_add_sort | |
41 | */ | |
718e3744 | 42 | int (*cmp) (void *val1, void *val2); |
43 | void (*del) (void *val); | |
44 | }; | |
45 | ||
46 | #define nextnode(X) ((X) = (X)->next) | |
47 | #define listhead(X) ((X)->head) | |
630e4807 | 48 | #define listtail(X) ((X)->tail) |
718e3744 | 49 | #define listcount(X) ((X)->count) |
50 | #define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL) | |
51 | #define getdata(X) ((X)->data) | |
52 | ||
53 | /* Prototypes. */ | |
54 | struct list *list_new(); | |
55 | void list_free (struct list *); | |
56 | ||
57 | void listnode_add (struct list *, void *); | |
58 | void listnode_add_sort (struct list *, void *); | |
59 | void listnode_add_after (struct list *, struct listnode *, void *); | |
60 | void listnode_delete (struct list *, void *); | |
61 | struct listnode *listnode_lookup (struct list *, void *); | |
62 | void *listnode_head (struct list *); | |
63 | ||
64 | void list_delete (struct list *); | |
65 | void list_delete_all_node (struct list *); | |
66 | ||
67 | /* For ospfd and ospf6d. */ | |
52dc7ee6 | 68 | void list_delete_node (struct list *, struct listnode *); |
718e3744 | 69 | |
70 | /* For ospf_spf.c */ | |
52dc7ee6 | 71 | void list_add_node_prev (struct list *, struct listnode *, void *); |
72 | void list_add_node_next (struct list *, struct listnode *, void *); | |
73 | void list_add_list (struct list *, struct list *); | |
718e3744 | 74 | |
75 | /* List iteration macro. */ | |
76 | #define LIST_LOOP(L,V,N) \ | |
77 | for ((N) = (L)->head; (N); (N) = (N)->next) \ | |
78 | if (((V) = (N)->data) != NULL) | |
79 | ||
80 | /* List node add macro. */ | |
81 | #define LISTNODE_ADD(L,N) \ | |
82 | do { \ | |
83 | (N)->prev = (L)->tail; \ | |
84 | if ((L)->head == NULL) \ | |
85 | (L)->head = (N); \ | |
86 | else \ | |
87 | (L)->tail->next = (N); \ | |
88 | (L)->tail = (N); \ | |
071fcedb | 89 | (L)->count++; \ |
718e3744 | 90 | } while (0) |
91 | ||
92 | /* List node delete macro. */ | |
93 | #define LISTNODE_DELETE(L,N) \ | |
94 | do { \ | |
95 | if ((N)->prev) \ | |
96 | (N)->prev->next = (N)->next; \ | |
97 | else \ | |
98 | (L)->head = (N)->next; \ | |
99 | if ((N)->next) \ | |
100 | (N)->next->prev = (N)->prev; \ | |
101 | else \ | |
102 | (L)->tail = (N)->prev; \ | |
071fcedb | 103 | (L)->count--; \ |
718e3744 | 104 | } while (0) |
105 | ||
106 | #endif /* _ZEBRA_LINKLIST_H */ |