source: trunk/debian/packages/libubox/trunk/safe_list.h @ 857

Last change on this file since 857 was 857, checked in by amain, 4 years ago

libubox: initial import / part 4

File size: 1.9 KB
Line 
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
32struct safe_list;
33struct safe_list_iterator;
34
35struct safe_list {
36        struct list_head list;
37        struct safe_list_iterator *i;
38};
39
40int safe_list_for_each(struct safe_list *list,
41                       int (*cb)(void *ctx, struct safe_list *list),
42                       void *ctx);
43
44void safe_list_add(struct safe_list *list, struct safe_list *head);
45void safe_list_add_first(struct safe_list *list, struct safe_list *head);
46void 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
57static inline bool safe_list_empty(struct safe_list *head)
58{
59        return list_empty(&head->list);
60}
61
62#endif
Note: See TracBrowser for help on using the repository browser.