1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
// SPDX-License-Identifier: GPL-2.0-or-later
/* Generic linked list
* Copyright (C) 1997, 2000 Kunihiro Ishiguro
*/
#ifndef _ZEBRA_LINKLIST_H
#define _ZEBRA_LINKLIST_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* NOTICE:
*
* If you are reading this file in an effort to add a new list structure
* this is the wrong place to be using it. Please see the typesafe
* data structures, or ask one of the other developers.
*
* If you are reading this file as a way to update an existing usage
* of this data structure, please consider just converting the data
* structure to one of the typesafe data structures instead.
*/
/* listnodes must always contain data to be valid. Adding an empty node
* to a list is invalid
*/
struct listnode {
struct listnode *next;
struct listnode *prev;
/* private member, use getdata() to retrieve, do not access directly */
void *data;
};
struct list {
struct listnode *head;
struct listnode *tail;
/* invariant: count is the number of listnodes in the list */
unsigned int count;
uint8_t flags;
/* Indicates that listnode memory is managed by the application and
* doesn't need to be freed by this library via listnode_delete etc.
*/
#define LINKLIST_FLAG_NODE_MEM_BY_APP (1 << 0)
/*
* Returns -1 if val1 < val2, 0 if equal?, 1 if val1 > val2.
* Used as definition of sorted for listnode_add_sort
*/
int (*cmp)(void *val1, void *val2);
/* callback to free user-owned data when listnode is deleted. supplying
* this callback is very much encouraged!
*/
void (*del)(void *val);
};
#define listnextnode(X) ((X) ? ((X)->next) : NULL)
#define listnextnode_unchecked(X) ((X)->next)
#define listhead(X) ((X) ? ((X)->head) : NULL)
#define listhead_unchecked(X) ((X)->head)
#define listtail(X) ((X) ? ((X)->tail) : NULL)
#define listtail_unchecked(X) ((X)->tail)
#define listcount(X) ((X)->count)
#define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
/* return X->data only if X and X->data are not NULL */
static inline void *listgetdata(const struct listnode *X)
{
assert((X != NULL) && ((X)->data != NULL));
return X->data;
}
/* App is going to manage listnode memory */
#define listset_app_node_mem(X) ((X)->flags |= LINKLIST_FLAG_NODE_MEM_BY_APP)
#define listnode_init(X, val) ((X)->data = (val))
/*
* Create a new linked list.
*
* Returns:
* the created linked list
*/
extern struct list *list_new(void);
/*
* Add a new element to the tail of a list.
*
* Runtime is O(1).
*
* list
* list to operate on
*
* data
* element to add
*/
extern struct listnode *listnode_add(struct list *list, void *data);
/*
* Add a new element to the beginning of a list.
*
* Runtime is O(1).
*
* list
* list to operate on
*
* data
* If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
*/
extern void listnode_add_head(struct list *list, void *data);
/*
* Insert a new element into a list with insertion sort.
*
* If list->cmp is set, this function is used to determine the position to
* insert the new element. If it is not set, this function is equivalent to
* listnode_add.
*
* Runtime is O(N).
*
* list
* list to operate on
*
* val
* If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
*/
extern void listnode_add_sort(struct list *list, void *val);
/*
* Insert a new element into a list after another element.
*
* Runtime is O(1).
*
* list
* list to operate on
*
* pp
* listnode to insert after
*
* data
* If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
*
* Returns:
* pointer to newly created listnode that contains the inserted data
*/
extern struct listnode *listnode_add_after(struct list *list,
struct listnode *pp, void *data);
/*
* Insert a new element into a list before another element.
*
* Runtime is O(1).
*
* list
* list to operate on
*
* pp
* listnode to insert before
*
* data
* If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
*
* Returns:
* pointer to newly created listnode that contains the inserted data
*/
extern struct listnode *listnode_add_before(struct list *list,
struct listnode *pp, void *data);
/*
* Move a node to the tail of a list.
*
* Runtime is O(1).
*
* list
* list to operate on
*
* node
* node to move to tail
*/
extern void listnode_move_to_tail(struct list *list, struct listnode *node);
/*
* Delete an element from a list.
*
* Runtime is O(N).
*
* list
* list to operate on
*
* data
* data to insert into list
*/
extern void listnode_delete(struct list *list, const void *data);
/*
* Find the listnode corresponding to an element in a list.
*
* list
* list to operate on
*
* data
* data to search for
*
* Returns:
* pointer to listnode storing the given data if found, NULL otherwise
*/
extern struct listnode *listnode_lookup(struct list *list, const void *data);
/*
* Retrieve the element at the head of a list.
*
* list
* list to operate on
*
* Returns:
* data at head of list, or NULL if list is empty
*/
extern void *listnode_head(struct list *list);
/*
* Sort a list in place.
*
* The sorting algorithm used is quicksort. Runtimes are equivalent to those of
* quicksort plus N. The sort is not stable.
*
* For portability reasons, the comparison function takes a pointer to pointer
* to void. This pointer should be dereferenced to get the actual data pointer.
* It is always safe to do this.
*
* list
* list to sort
*
* cmp
* comparison function for quicksort. Should return less than, equal to or
* greater than zero if the first argument is less than, equal to or greater
* than the second argument.
*/
extern void list_sort(struct list *list,
int (*cmp)(const void **, const void **));
/*
* Convert a list to an array of void pointers.
*
* Starts from the list head and ends either on the last node of the list or
* when the provided array cannot store any more elements.
*
* list
* list to convert
*
* arr
* Pre-allocated array of void *
*
* arrlen
* Number of elements in arr
*
* Returns:
* arr
*/
void **list_to_array(struct list *list, void **arr, size_t arrlen);
/*
* Delete a list and NULL its pointer.
*
* If non-null, list->del is called with each data element.
*
* plist
* pointer to list pointer; this will be set to NULL after the list has been
* deleted
*/
extern void list_delete(struct list **plist);
/*
* Delete all nodes from a list without deleting the list itself.
*
* If non-null, list->del is called with each data element.
*
* list
* list to operate on
*/
extern void list_delete_all_node(struct list *list);
/*
* Delete a node from a list.
*
* list->del is not called with the data associated with the node.
*
* Runtime is O(1).
*
* list
* list to operate on
*
* node
* the node to delete
*/
extern void list_delete_node(struct list *list, struct listnode *node);
/*
* Insert a new element into a list with insertion sort if there is no
* duplicate element present in the list. This assumes the input list is
* sorted. If unsorted, it will check for duplicate until it finds out
* the position to do insertion sort with the unsorted list.
*
* If list->cmp is set, this function is used to determine the position to
* insert the new element. If it is not set, this function is equivalent to
* listnode_add. duplicate element is determined by cmp function returning 0.
*
* Runtime is O(N).
*
* list
* list to operate on
*
* val
* If MEM_BY_APP is set this is listnode. Otherwise it is element to add.
*/
extern bool listnode_add_sort_nodup(struct list *list, void *val);
/*
* Duplicate the specified list, creating a shallow copy of each of its
* elements.
*
* list
* list to duplicate
*
* Returns:
* the duplicated list
*/
extern struct list *list_dup(struct list *list);
/* List iteration macro.
* Usage: for (ALL_LIST_ELEMENTS (...) { ... }
* It is safe to delete the listnode using this macro.
*/
#define ALL_LIST_ELEMENTS(list, node, nextnode, data) \
(node) = listhead(list), ((data) = NULL); \
(node) != NULL \
&& ((data) = static_cast(data, listgetdata(node)), \
(nextnode) = node->next, 1); \
(node) = (nextnode), ((data) = NULL)
/* read-only list iteration macro.
* Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
* use this macro when it is *immediately obvious* the listnode is not
* deleted in the body of the loop. Does not have forward-reference overhead
* of previous macro.
*/
#define ALL_LIST_ELEMENTS_RO(list, node, data) \
(node) = listhead(list), ((data) = NULL); \
(node) != NULL && ((data) = static_cast(data, listgetdata(node)), 1); \
(node) = listnextnode(node), ((data) = NULL)
extern struct listnode *listnode_lookup_nocheck(struct list *list, void *data);
/*
* Add a node to *list, if non-NULL. Otherwise, allocate a new list, mail
* it back in *list, and add a new node.
*
* Return: the new node.
*/
extern struct listnode *listnode_add_force(struct list **list, void *val);
#ifdef __cplusplus
}
#endif
#endif /* _ZEBRA_LINKLIST_H */
|