libubox
C utility functions for OpenWrt.
safe_list.h
Go to the documentation of this file.
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;
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
static bool list_empty(const struct list_head *head)
Definition: list.h:69
static bool safe_list_empty(struct safe_list *head)
Definition: safe_list.h:57
void safe_list_add(struct safe_list *list, struct safe_list *head)
Definition: safe_list.c:83
int safe_list_for_each(struct safe_list *list, int(*cb)(void *ctx, struct safe_list *list), void *ctx)
Definition: safe_list.c:62
void safe_list_add_first(struct safe_list *list, struct safe_list *head)
Definition: safe_list.c:89
void safe_list_del(struct safe_list *list)
Definition: safe_list.c:95
Definition: list.h:53
struct safe_list_iterator ** head
Definition: safe_list.c:22
struct list_head list
Definition: safe_list.h:36
struct safe_list_iterator * i
Definition: safe_list.h:37