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 | |
| 25 | typedef struct list *list; |
| 26 | typedef struct listnode *listnode; |
| 27 | |
| 28 | struct listnode |
| 29 | { |
| 30 | struct listnode *next; |
| 31 | struct listnode *prev; |
| 32 | void *data; |
| 33 | }; |
| 34 | |
| 35 | struct list |
| 36 | { |
| 37 | struct listnode *head; |
| 38 | struct listnode *tail; |
gdt | a7a9990 | 2003-12-22 16:07:52 +0000 | [diff] [blame] | 39 | /* invariant: count is the number of listnodes in the list */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | unsigned int count; |
gdt | a7a9990 | 2003-12-22 16:07:52 +0000 | [diff] [blame] | 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 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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) |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame^] | 51 | #define listtail(X) ((X)->tail) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | #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. */ |
| 57 | struct list *list_new(); |
| 58 | void list_free (struct list *); |
| 59 | |
| 60 | void listnode_add (struct list *, void *); |
| 61 | void listnode_add_sort (struct list *, void *); |
| 62 | void listnode_add_after (struct list *, struct listnode *, void *); |
| 63 | void listnode_delete (struct list *, void *); |
| 64 | struct listnode *listnode_lookup (struct list *, void *); |
| 65 | void *listnode_head (struct list *); |
| 66 | |
| 67 | void list_delete (struct list *); |
| 68 | void list_delete_all_node (struct list *); |
| 69 | |
| 70 | /* For ospfd and ospf6d. */ |
| 71 | void list_delete_node (list, listnode); |
| 72 | |
| 73 | /* For ospf_spf.c */ |
| 74 | void list_add_node_prev (list, listnode, void *); |
| 75 | void list_add_node_next (list, listnode, void *); |
| 76 | void 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); \ |
paul | 071fced | 2003-08-12 05:40:28 +0000 | [diff] [blame] | 92 | (L)->count++; \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 93 | } 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; \ |
paul | 071fced | 2003-08-12 05:40:28 +0000 | [diff] [blame] | 106 | (L)->count--; \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | } while (0) |
| 108 | |
| 109 | #endif /* _ZEBRA_LINKLIST_H */ |