]>
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
28 /* listnodes must always contain data to be valid. Adding an empty node
29 * to a list is invalid
32 struct listnode
*next
;
33 struct listnode
*prev
;
35 /* private member, use getdata() to retrieve, do not access directly */
40 struct listnode
*head
;
41 struct listnode
*tail
;
43 /* invariant: count is the number of listnodes in the list */
47 /* Indicates that listnode memory is managed by the application and
48 * doesn't need to be freed by this library via listnode_delete etc.
50 #define LINKLIST_FLAG_NODE_MEM_BY_APP (1 << 0)
53 * Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
54 * Used as definition of sorted for listnode_add_sort
56 int (*cmp
)(void *val1
, void *val2
);
58 /* callback to free user-owned data when listnode is deleted. supplying
59 * this callback is very much encouraged!
61 void (*del
)(void *val
);
64 #define listnextnode(X) ((X) ? ((X)->next) : NULL)
65 #define listnextnode_unchecked(X) ((X)->next)
66 #define listhead(X) ((X) ? ((X)->head) : NULL)
67 #define listhead_unchecked(X) ((X)->head)
68 #define listtail(X) ((X) ? ((X)->tail) : NULL)
69 #define listtail_unchecked(X) ((X)->tail)
70 #define listcount(X) ((X)->count)
71 #define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
72 /* return X->data only if X and X->data are not NULL */
73 #define listgetdata(X) (assert(X), assert((X)->data != NULL), (X)->data)
74 /* App is going to manage listnode memory */
75 #define listset_app_node_mem(X) ((X)->flags |= LINKLIST_FLAG_NODE_MEM_BY_APP)
76 #define listnode_init(X, val) ((X)->data = (val))
79 * Create a new linked list.
82 * the created linked list
84 extern struct list
*list_new(void);
87 * Add a new element to the tail of a list.
97 extern struct listnode
*listnode_add(struct list
*list
, void *data
);
100 * Add a new element to the beginning of a list.
108 * If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
110 extern void listnode_add_head(struct list
*list
, void *data
);
113 * Insert a new element into a list with insertion sort.
115 * If list->cmp is set, this function is used to determine the position to
116 * insert the new element. If it is not set, this function is equivalent to
125 * If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
127 extern void listnode_add_sort(struct list
*list
, void *val
);
130 * Insert a new element into a list after another element.
138 * listnode to insert after
141 * If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
144 * pointer to newly created listnode that contains the inserted data
146 extern struct listnode
*listnode_add_after(struct list
*list
,
147 struct listnode
*pp
, void *data
);
150 * Insert a new element into a list before another element.
158 * listnode to insert before
161 * If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
164 * pointer to newly created listnode that contains the inserted data
166 extern struct listnode
*listnode_add_before(struct list
*list
,
167 struct listnode
*pp
, void *data
);
170 * Move a node to the tail of a list.
178 * node to move to tail
180 extern void listnode_move_to_tail(struct list
*list
, struct listnode
*node
);
183 * Delete an element from a list.
191 * data to insert into list
193 extern void listnode_delete(struct list
*list
, const void *data
);
196 * Find the listnode corresponding to an element in a list.
205 * pointer to listnode storing the given data if found, NULL otherwise
207 extern struct listnode
*listnode_lookup(struct list
*list
, const void *data
);
210 * Retrieve the element at the head of a list.
216 * data at head of list, or NULL if list is empty
218 extern void *listnode_head(struct list
*list
);
221 * Sort a list in place.
223 * The sorting algorithm used is quicksort. Runtimes are equivalent to those of
224 * quicksort plus N. The sort is not stable.
226 * For portability reasons, the comparison function takes a pointer to pointer
227 * to void. This pointer should be dereferenced to get the actual data pointer.
228 * It is always safe to do this.
234 * comparison function for quicksort. Should return less than, equal to or
235 * greater than zero if the first argument is less than, equal to or greater
236 * than the second argument.
238 extern void list_sort(struct list
*list
,
239 int (*cmp
)(const void **, const void **));
242 * Convert a list to an array of void pointers.
244 * Starts from the list head and ends either on the last node of the list or
245 * when the provided array cannot store any more elements.
251 * Pre-allocated array of void *
254 * Number of elements in arr
259 void **list_to_array(struct list
*list
, void **arr
, size_t arrlen
);
262 * Delete a list and NULL its pointer.
264 * If non-null, list->del is called with each data element.
267 * pointer to list pointer; this will be set to NULL after the list has been
270 extern void list_delete(struct list
**plist
);
273 * Delete all nodes from a list without deleting the list itself.
275 * If non-null, list->del is called with each data element.
280 extern void list_delete_all_node(struct list
*list
);
283 * Delete a node from a list.
285 * list->del is not called with the data associated with the node.
295 extern void list_delete_node(struct list
*list
, struct listnode
*node
);
298 * Delete all nodes which satisfy a condition from a list.
299 * Deletes the node if cond function returns true for the node.
300 * If function ptr passed is NULL, it deletes all nodes
305 * function pointer which takes node data as input and return true or false
308 extern void list_filter_out_nodes(struct list
*list
, bool (*cond
)(void *data
));
311 * Insert a new element into a list with insertion sort if there is no
312 * duplicate element present in the list. This assumes the input list is
313 * sorted. If unsorted, it will check for duplicate until it finds out
314 * the position to do insertion sort with the unsorted list.
316 * If list->cmp is set, this function is used to determine the position to
317 * insert the new element. If it is not set, this function is equivalent to
318 * listnode_add. duplicate element is determined by cmp function returning 0.
326 * If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
329 extern bool listnode_add_sort_nodup(struct list
*list
, void *val
);
332 * Duplicate the specified list, creating a shallow copy of each of its
339 * the duplicated list
341 extern struct list
*list_dup(struct list
*list
);
343 /* List iteration macro.
344 * Usage: for (ALL_LIST_ELEMENTS (...) { ... }
345 * It is safe to delete the listnode using this macro.
347 #define ALL_LIST_ELEMENTS(list, node, nextnode, data) \
348 (node) = listhead(list), ((data) = NULL); \
350 && ((data) = static_cast(data, listgetdata(node)), \
351 (nextnode) = node->next, 1); \
352 (node) = (nextnode), ((data) = NULL)
354 /* read-only list iteration macro.
355 * Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
356 * use this macro when it is *immediately obvious* the listnode is not
357 * deleted in the body of the loop. Does not have forward-reference overhead
360 #define ALL_LIST_ELEMENTS_RO(list, node, data) \
361 (node) = listhead(list), ((data) = NULL); \
362 (node) != NULL && ((data) = static_cast(data, listgetdata(node)), 1); \
363 (node) = listnextnode(node), ((data) = NULL)
365 /* these *do not* cleanup list nodes and referenced data, as the functions
366 * do - these macros simply {de,at}tach a listnode from/to a list.
369 /* List node attach macro. */
370 #define LISTNODE_ATTACH(L, N) \
372 (N)->prev = (L)->tail; \
374 if ((L)->head == NULL) \
377 (L)->tail->next = (N); \
382 /* List node detach macro. */
383 #define LISTNODE_DETACH(L, N) \
386 (N)->prev->next = (N)->next; \
388 (L)->head = (N)->next; \
390 (N)->next->prev = (N)->prev; \
392 (L)->tail = (N)->prev; \
396 extern struct listnode
*listnode_lookup_nocheck(struct list
*list
, void *data
);
399 * Add a node to *list, if non-NULL. Otherwise, allocate a new list, mail
400 * it back in *list, and add a new node.
402 * Return: the new node.
404 extern struct listnode
*listnode_add_force(struct list
**list
, void *val
);
410 #endif /* _ZEBRA_LINKLIST_H */