paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Generic linked list routine. |
| 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 | #include <zebra.h> |
| 23 | |
| 24 | #include "linklist.h" |
| 25 | #include "memory.h" |
| 26 | |
| 27 | /* Allocate new list. */ |
| 28 | struct list * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 29 | list_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | { |
| 31 | struct list *new; |
| 32 | |
| 33 | new = XMALLOC (MTYPE_LINK_LIST, sizeof (struct list)); |
| 34 | memset (new, 0, sizeof (struct list)); |
| 35 | return new; |
| 36 | } |
| 37 | |
| 38 | /* Free list. */ |
| 39 | void |
| 40 | list_free (struct list *l) |
| 41 | { |
| 42 | XFREE (MTYPE_LINK_LIST, l); |
| 43 | } |
| 44 | |
| 45 | /* Allocate new listnode. Internal use only. */ |
| 46 | static struct listnode * |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 47 | listnode_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | { |
| 49 | struct listnode *node; |
| 50 | |
| 51 | node = XMALLOC (MTYPE_LINK_NODE, sizeof (struct listnode)); |
| 52 | memset (node, 0, sizeof (struct listnode)); |
| 53 | return node; |
| 54 | } |
| 55 | |
| 56 | /* Free listnode. */ |
| 57 | static void |
| 58 | listnode_free (struct listnode *node) |
| 59 | { |
| 60 | XFREE (MTYPE_LINK_NODE, node); |
| 61 | } |
| 62 | |
| 63 | /* Add new data to the list. */ |
| 64 | void |
| 65 | listnode_add (struct list *list, void *val) |
| 66 | { |
| 67 | struct listnode *node; |
| 68 | |
| 69 | node = listnode_new (); |
| 70 | |
| 71 | node->prev = list->tail; |
| 72 | node->data = val; |
| 73 | |
| 74 | if (list->head == NULL) |
| 75 | list->head = node; |
| 76 | else |
| 77 | list->tail->next = node; |
| 78 | list->tail = node; |
| 79 | |
| 80 | list->count++; |
| 81 | } |
| 82 | |
gdt | 2976021 | 2003-12-22 15:56:00 +0000 | [diff] [blame] | 83 | /* |
| 84 | * Add a node to the list. If the list was sorted according to the |
| 85 | * cmp function, insert a new node with the given val such that the |
| 86 | * list remains sorted. The new node is always inserted; there is no |
| 87 | * notion of omitting duplicates. |
| 88 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | void |
| 90 | listnode_add_sort (struct list *list, void *val) |
| 91 | { |
| 92 | struct listnode *n; |
| 93 | struct listnode *new; |
| 94 | |
hasso | e90fbab | 2003-12-21 09:51:42 +0000 | [diff] [blame] | 95 | new = listnode_new (); |
| 96 | new->data = val; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | |
| 98 | if (list->cmp) |
| 99 | { |
| 100 | for (n = list->head; n; n = n->next) |
| 101 | { |
hasso | 47ce02a | 2003-12-22 16:49:15 +0000 | [diff] [blame] | 102 | if ((*list->cmp) (val, n->data) < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | { |
| 104 | new->next = n; |
| 105 | new->prev = n->prev; |
| 106 | |
| 107 | if (n->prev) |
| 108 | n->prev->next = new; |
| 109 | else |
| 110 | list->head = new; |
| 111 | n->prev = new; |
| 112 | list->count++; |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | new->prev = list->tail; |
| 119 | |
| 120 | if (list->tail) |
| 121 | list->tail->next = new; |
| 122 | else |
| 123 | list->head = new; |
| 124 | |
| 125 | list->tail = new; |
| 126 | list->count++; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | listnode_add_after (struct list *list, struct listnode *pp, void *val) |
| 131 | { |
| 132 | struct listnode *nn; |
| 133 | |
| 134 | nn = listnode_new (); |
| 135 | nn->data = val; |
| 136 | |
| 137 | if (pp == NULL) |
| 138 | { |
| 139 | if (list->head) |
| 140 | list->head->prev = nn; |
| 141 | else |
| 142 | list->tail = nn; |
| 143 | |
| 144 | nn->next = list->head; |
| 145 | nn->prev = pp; |
| 146 | |
| 147 | list->head = nn; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | if (pp->next) |
| 152 | pp->next->prev = nn; |
| 153 | else |
| 154 | list->tail = nn; |
| 155 | |
| 156 | nn->next = pp->next; |
| 157 | nn->prev = pp; |
| 158 | |
| 159 | pp->next = nn; |
| 160 | } |
Denis Ovsienko | 6ce80bd | 2007-11-12 14:55:01 +0000 | [diff] [blame] | 161 | list->count++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | /* Delete specific date pointer from the list. */ |
| 166 | void |
| 167 | listnode_delete (struct list *list, void *val) |
| 168 | { |
| 169 | struct listnode *node; |
| 170 | |
paul | f2c8065 | 2002-12-13 21:44:27 +0000 | [diff] [blame] | 171 | assert(list); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | for (node = list->head; node; node = node->next) |
| 173 | { |
| 174 | if (node->data == val) |
| 175 | { |
| 176 | if (node->prev) |
| 177 | node->prev->next = node->next; |
| 178 | else |
| 179 | list->head = node->next; |
| 180 | |
| 181 | if (node->next) |
| 182 | node->next->prev = node->prev; |
| 183 | else |
| 184 | list->tail = node->prev; |
| 185 | |
| 186 | list->count--; |
| 187 | listnode_free (node); |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* Return first node's data if it is there. */ |
| 194 | void * |
| 195 | listnode_head (struct list *list) |
| 196 | { |
| 197 | struct listnode *node; |
| 198 | |
paul | f2c8065 | 2002-12-13 21:44:27 +0000 | [diff] [blame] | 199 | assert(list); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | node = list->head; |
| 201 | |
| 202 | if (node) |
| 203 | return node->data; |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | /* Delete all listnode from the list. */ |
| 208 | void |
| 209 | list_delete_all_node (struct list *list) |
| 210 | { |
| 211 | struct listnode *node; |
| 212 | struct listnode *next; |
| 213 | |
paul | f2c8065 | 2002-12-13 21:44:27 +0000 | [diff] [blame] | 214 | assert(list); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | for (node = list->head; node; node = next) |
| 216 | { |
| 217 | next = node->next; |
| 218 | if (list->del) |
| 219 | (*list->del) (node->data); |
| 220 | listnode_free (node); |
| 221 | } |
| 222 | list->head = list->tail = NULL; |
| 223 | list->count = 0; |
| 224 | } |
| 225 | |
| 226 | /* Delete all listnode then free list itself. */ |
| 227 | void |
| 228 | list_delete (struct list *list) |
| 229 | { |
paul | f2c8065 | 2002-12-13 21:44:27 +0000 | [diff] [blame] | 230 | assert(list); |
Paul Jakma | c024fd0 | 2006-06-15 12:43:09 +0000 | [diff] [blame] | 231 | list_delete_all_node (list); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 232 | list_free (list); |
| 233 | } |
| 234 | |
| 235 | /* Lookup the node which has given data. */ |
| 236 | struct listnode * |
| 237 | listnode_lookup (struct list *list, void *data) |
| 238 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 239 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 240 | |
paul | f2c8065 | 2002-12-13 21:44:27 +0000 | [diff] [blame] | 241 | assert(list); |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 242 | for (node = listhead(list); node; node = listnextnode (node)) |
| 243 | if (data == listgetdata (node)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 244 | return node; |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | /* Delete the node from list. For ospfd and ospf6d. */ |
| 249 | void |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 250 | list_delete_node (struct list *list, struct listnode *node) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | { |
| 252 | if (node->prev) |
| 253 | node->prev->next = node->next; |
| 254 | else |
| 255 | list->head = node->next; |
| 256 | if (node->next) |
| 257 | node->next->prev = node->prev; |
| 258 | else |
| 259 | list->tail = node->prev; |
| 260 | list->count--; |
| 261 | listnode_free (node); |
| 262 | } |
| 263 | |
| 264 | /* ospf_spf.c */ |
| 265 | void |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 266 | list_add_node_prev (struct list *list, struct listnode *current, void *val) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | { |
| 268 | struct listnode *node; |
| 269 | |
| 270 | node = listnode_new (); |
| 271 | node->next = current; |
| 272 | node->data = val; |
| 273 | |
| 274 | if (current->prev == NULL) |
| 275 | list->head = node; |
| 276 | else |
| 277 | current->prev->next = node; |
| 278 | |
| 279 | node->prev = current->prev; |
| 280 | current->prev = node; |
| 281 | |
| 282 | list->count++; |
| 283 | } |
| 284 | |
| 285 | /* ospf_spf.c */ |
| 286 | void |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 287 | list_add_node_next (struct list *list, struct listnode *current, void *val) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 288 | { |
| 289 | struct listnode *node; |
| 290 | |
| 291 | node = listnode_new (); |
| 292 | node->prev = current; |
| 293 | node->data = val; |
| 294 | |
| 295 | if (current->next == NULL) |
| 296 | list->tail = node; |
| 297 | else |
| 298 | current->next->prev = node; |
| 299 | |
| 300 | node->next = current->next; |
| 301 | current->next = node; |
| 302 | |
| 303 | list->count++; |
| 304 | } |
| 305 | |
| 306 | /* ospf_spf.c */ |
| 307 | void |
| 308 | list_add_list (struct list *l, struct list *m) |
| 309 | { |
| 310 | struct listnode *n; |
| 311 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 312 | for (n = listhead (m); n; n = listnextnode (n)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 313 | listnode_add (l, n->data); |
| 314 | } |