Welcome to mirror list, hosted at ThFree Co, Russian Federation.

safe_list.c - git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16f57e084f4bb7dd59afee1366191f5d069a0fec (plain)
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
/*
 * safe_list - linked list protected against recursive iteration with deletes
 *
 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "safe_list.h"

struct safe_list_iterator {
	struct safe_list_iterator **head;
	struct safe_list_iterator *next_i;
	struct safe_list *next;
};

static void
__safe_list_set_iterator(struct safe_list *list,
			 struct safe_list_iterator *i)
{
	struct safe_list_iterator *next_i;
	struct safe_list *next;

	next = list_entry(list->list.next, struct safe_list, list);
	next_i = next->i;

	next->i = i;
	i->next = next;
	i->head = &next->i;

	i->next_i = next_i;
	if (next_i)
		next_i->head = &i->next_i;
}

static void
__safe_list_del_iterator(struct safe_list_iterator *i)
{
	*i->head = i->next_i;
	if (i->next_i)
		i->next_i->head = i->head;
}

static void
__safe_list_move_iterator(struct safe_list *list,
			  struct safe_list_iterator *i)
{
	__safe_list_del_iterator(i);
	__safe_list_set_iterator(list, i);
}

int safe_list_for_each(struct safe_list *head,
		       int (*cb)(void *ctx, struct safe_list *list),
		       void *ctx)
{
	struct safe_list_iterator i;
	struct safe_list *cur;
	int ret = 0;

	for (cur = list_entry(head->list.next, struct safe_list, list),
	     __safe_list_set_iterator(cur, &i);
	     cur != head;
	     cur = i.next, __safe_list_move_iterator(cur, &i)) {
		ret = cb(ctx, cur);
		if (ret)
			break;
	}

	__safe_list_del_iterator(&i);
	return ret;
}

void safe_list_add(struct safe_list *list, struct safe_list *head)
{
	list->i = NULL;
	list_add_tail(&list->list, &head->list);
}

void safe_list_add_first(struct safe_list *list, struct safe_list *head)
{
	list->i = NULL;
	list_add(&list->list, &head->list);
}

void safe_list_del(struct safe_list *list)
{
	struct safe_list_iterator *i, *next_i, **tail;
	struct safe_list *next;

	next = list_entry(list->list.next, struct safe_list, list);
	list_del(&list->list);

	if (!list->i)
		return;

	next_i = next->i;
	tail = &next->i;

	for (i = list->i; i; i = i->next_i) {
		tail = &i->next_i;
		i->next = next;
	}

	next->i = list->i;
	list->i->head = &next->i;
	*tail = next_i;
	if (next_i)
		next_i->head = tail;

	list->i = NULL;
}