paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; |
gdt | a7a9990 | 2003-12-22 16:07:52 +0000 | [diff] [blame] | 36 | /* invariant: count is the number of listnodes in the list */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | unsigned int count; |
gdt | a7a9990 | 2003-12-22 16:07:52 +0000 | [diff] [blame] | 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 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 48 | #define listtail(X) ((X)->tail) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 68 | void list_delete_node (struct list *, struct listnode *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | |
| 70 | /* For ospf_spf.c */ |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 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 *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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); \ |
paul | 071fced | 2003-08-12 05:40:28 +0000 | [diff] [blame] | 89 | (L)->count++; \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; \ |
paul | 071fced | 2003-08-12 05:40:28 +0000 | [diff] [blame] | 103 | (L)->count--; \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | } while (0) |
| 105 | |
| 106 | #endif /* _ZEBRA_LINKLIST_H */ |