]>
git.proxmox.com Git - mirror_frr.git/blob - lib/linklist.h
2 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef _ZEBRA_LINKLIST_H
22 #define _ZEBRA_LINKLIST_H
24 /* listnodes must always contain data to be valid. Adding an empty node
25 * to a list is invalid
28 struct listnode
*next
;
29 struct listnode
*prev
;
31 /* private member, use getdata() to retrieve, do not access directly */
36 struct listnode
*head
;
37 struct listnode
*tail
;
39 /* invariant: count is the number of listnodes in the list */
43 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
44 * Used as definition of sorted for listnode_add_sort
46 int (*cmp
)(void *val1
, void *val2
);
48 /* callback to free user-owned data when listnode is deleted. supplying
49 * this callback is very much encouraged!
51 void (*del
)(void *val
);
54 #define listnextnode(X) ((X) ? ((X)->next) : NULL)
55 #define listnextnode_unchecked(X) ((X)->next)
56 #define listhead(X) ((X) ? ((X)->head) : NULL)
57 #define listhead_unchecked(X) ((X)->head)
58 #define listtail(X) ((X) ? ((X)->tail) : NULL)
59 #define listcount(X) ((X)->count)
60 #define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
61 /* return X->data only if X and X->data are not NULL */
62 #define listgetdata(X) (assert(X), assert((X)->data != NULL), (X)->data)
65 * Create a new linked list.
68 * the created linked list
70 extern struct list
*list_new(void);
73 * Add a new element to the tail of a list.
83 extern void listnode_add(struct list
*list
, void *data
);
86 * Add a new element to the beginning of a list.
96 extern void listnode_add_head(struct list
*list
, void *data
);
99 * Insert a new element into a list with insertion sort.
101 * If list->cmp is set, this function is used to determine the position to
102 * insert the new element. If it is not set, this function is equivalent to
113 extern void listnode_add_sort(struct list
*list
, void *val
);
116 * Insert a new element into a list after another element.
124 * listnode to insert after
130 * pointer to newly created listnode that contains the inserted data
132 extern struct listnode
*listnode_add_after(struct list
*list
,
133 struct listnode
*pp
, void *data
);
136 * Insert a new element into a list before another element.
144 * listnode to insert before
150 * pointer to newly created listnode that contains the inserted data
152 extern struct listnode
*listnode_add_before(struct list
*list
,
153 struct listnode
*pp
, void *data
);
156 * Move a node to the tail of a list.
164 * node to move to tail
166 extern void listnode_move_to_tail(struct list
*list
, struct listnode
*node
);
169 * Delete an element from a list.
177 * data to insert into list
179 extern void listnode_delete(struct list
*list
, void *data
);
182 * Find the listnode corresponding to an element in a list.
191 * pointer to listnode storing the given data if found, NULL otherwise
193 extern struct listnode
*listnode_lookup(struct list
*list
, void *data
);
196 * Retrieve the element at the head of a list.
202 * data at head of list, or NULL if list is empty
204 extern void *listnode_head(struct list
*list
);
215 extern struct list
*list_dup(struct list
*l
);
218 * Sort a list in place.
220 * The sorting algorithm used is quicksort. Runtimes are equivalent to those of
221 * quicksort plus N. The sort is not stable.
223 * For portability reasons, the comparison function takes a pointer to pointer
224 * to void. This pointer should be dereferenced to get the actual data pointer.
225 * It is always safe to do this.
231 * comparison function for quicksort. Should return less than, equal to or
232 * greater than zero if the first argument is less than, equal to or greater
233 * than the second argument.
235 extern void list_sort(struct list
*list
,
236 int (*cmp
)(const void **, const void **));
239 * Delete a list and NULL its pointer.
241 * If non-null, list->del is called with each data element.
244 * pointer to list pointer; this will be set to NULL after the list has been
247 extern void list_delete(struct list
**plist
);
250 * Delete all nodes from a list without deleting the list itself.
252 * If non-null, list->del is called with each data element.
257 extern void list_delete_all_node(struct list
*list
);
260 * Delete a node from a list.
262 * list->del is not called with the data associated with the node.
272 extern void list_delete_node(struct list
*list
, struct listnode
*node
);
275 * Append a list to an existing list.
277 * Runtime is O(N) where N = listcount(add).
285 extern void list_add_list(struct list
*list
, struct list
*add
);
287 /* List iteration macro.
288 * Usage: for (ALL_LIST_ELEMENTS (...) { ... }
289 * It is safe to delete the listnode using this macro.
291 #define ALL_LIST_ELEMENTS(list, node, nextnode, data) \
292 (node) = listhead(list), ((data) = NULL); \
294 && ((data) = listgetdata(node), (nextnode) = node->next, 1); \
295 (node) = (nextnode), ((data) = NULL)
297 /* read-only list iteration macro.
298 * Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
299 * use this macro when it is *immediately obvious* the listnode is not
300 * deleted in the body of the loop. Does not have forward-reference overhead
303 #define ALL_LIST_ELEMENTS_RO(list, node, data) \
304 (node) = listhead(list), ((data) = NULL); \
305 (node) != NULL && ((data) = listgetdata(node), 1); \
306 (node) = listnextnode(node), ((data) = NULL)
308 /* these *do not* cleanup list nodes and referenced data, as the functions
309 * do - these macros simply {de,at}tach a listnode from/to a list.
312 /* List node attach macro. */
313 #define LISTNODE_ATTACH(L, N) \
315 (N)->prev = (L)->tail; \
317 if ((L)->head == NULL) \
320 (L)->tail->next = (N); \
325 /* List node detach macro. */
326 #define LISTNODE_DETACH(L, N) \
329 (N)->prev->next = (N)->next; \
331 (L)->head = (N)->next; \
333 (N)->next->prev = (N)->prev; \
335 (L)->tail = (N)->prev; \
339 #endif /* _ZEBRA_LINKLIST_H */