avl: add AVL_TREE macro to define an initialized struct avl_tree
[project/libubox.git] / safe_list.h
1 /*
2 * safe_list - linked list protected against recursive iteration with deletes
3 *
4 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /*
20 * Use this linked list implementation as a replacement for list.h if you
21 * want to allow deleting arbitrary list entries from within one or more
22 * recursive iterator calling context
23 */
24
25 #ifndef __LIBUBOX_SAFE_LIST_H
26 #define __LIBUBOX_SAFE_LIST_H
27
28 #include <stdbool.h>
29 #include "list.h"
30 #include "utils.h"
31
32 struct safe_list;
33 struct safe_list_iterator;
34
35 struct safe_list {
36 struct list_head list;
37 struct safe_list_iterator *i;
38 };
39
40 int safe_list_for_each(struct safe_list *list,
41 int (*cb)(void *ctx, struct safe_list *list),
42 void *ctx);
43
44 void safe_list_add(struct safe_list *list, struct safe_list *head);
45 void safe_list_add_first(struct safe_list *list, struct safe_list *head);
46 void safe_list_del(struct safe_list *list);
47
48 #define INIT_SAFE_LIST(_head) \
49 do { \
50 INIT_LIST_HEAD(_head.list); \
51 (_head)->i = NULL; \
52 } while (0)
53
54 #define SAFE_LIST_INIT(_name) { LIST_HEAD_INIT(_name.list), NULL }
55 #define SAFE_LIST(_name) struct safe_list _name = SAFE_LIST_INIT(_name)
56
57 static inline bool safe_list_empty(struct safe_list *head)
58 {
59 return list_empty(&head->list);
60 }
61
62 #endif