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; |
| 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. */ |
| 51 | struct list *list_new(); |
| 52 | void list_free (struct list *); |
| 53 | |
| 54 | void listnode_add (struct list *, void *); |
| 55 | void listnode_add_sort (struct list *, void *); |
| 56 | void listnode_add_after (struct list *, struct listnode *, void *); |
| 57 | void listnode_delete (struct list *, void *); |
| 58 | struct listnode *listnode_lookup (struct list *, void *); |
| 59 | void *listnode_head (struct list *); |
| 60 | |
| 61 | void list_delete (struct list *); |
| 62 | void list_delete_all_node (struct list *); |
| 63 | |
| 64 | /* For ospfd and ospf6d. */ |
| 65 | void list_delete_node (list, listnode); |
| 66 | |
| 67 | /* For ospf_spf.c */ |
| 68 | void list_add_node_prev (list, listnode, void *); |
| 69 | void list_add_node_next (list, listnode, void *); |
| 70 | void 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); \ |
paul | 071fced | 2003-08-12 05:40:28 +0000 | [diff] [blame^] | 86 | (L)->count++; \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | } while (0) |
| 88 | |
| 89 | /* List node delete macro. */ |
| 90 | #define LISTNODE_DELETE(L,N) \ |
| 91 | do { \ |
| 92 | if ((N)->prev) \ |
| 93 | (N)->prev->next = (N)->next; \ |
| 94 | else \ |
| 95 | (L)->head = (N)->next; \ |
| 96 | if ((N)->next) \ |
| 97 | (N)->next->prev = (N)->prev; \ |
| 98 | else \ |
| 99 | (L)->tail = (N)->prev; \ |
paul | 071fced | 2003-08-12 05:40:28 +0000 | [diff] [blame^] | 100 | (L)->count--; \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | } while (0) |
| 102 | |
| 103 | #endif /* _ZEBRA_LINKLIST_H */ |