summaryrefslogtreecommitdiffstats
path: root/include/libtf/list.h
blob: 22b76a88017d76984554c9762c7ab6a69e2b91fa (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
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
/* list.h - libtf single and double linked list macros
 *
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 or later as
 * published by the Free Software Foundation.
 *
 * See http://www.gnu.org/ for details.
 *
 * This based to some degree to the code in the linux kernel. This is subset
 * of the functionality, and tf_list has separate head and node types.
 */

#ifndef TF_LIST_H
#define TF_LIST_H

#include <libtf/defines.h>

struct tf_hlist_head {
	struct tf_hlist_node *first;
};

struct tf_hlist_node {
	struct tf_hlist_node *next;
	struct tf_hlist_node **pprev;
};

static inline int tf_hlist_empty(const struct tf_hlist_head *h)
{
	return !h->first;
}

static inline int tf_hlist_hashed(const struct tf_hlist_node *n)
{
	return n->pprev != NULL;
}

static inline void tf_hlist_del(struct tf_hlist_node *n)
{
	*n->pprev = n->next;
	n->next = NULL;
	n->pprev = NULL;
}

static inline void tf_hlist_add_head(struct tf_hlist_node *n, struct tf_hlist_head *h)
{
	struct tf_hlist_node *first = h->first;

	n->next = first;
	n->pprev = &h->first;
	h->first = n;
}

static inline void tf_hlist_add_after(struct tf_hlist_node *n, struct tf_hlist_node *prev)
{
	n->next = prev->next;
	n->pprev = &prev->next;
	prev->next = n;
}

static inline struct tf_hlist_node **tf_hlist_tail_ptr(struct tf_hlist_head *h)
{
	struct tf_hlist_node *n = h->first;
	if (n == NULL)
		return &h->first;
	while (n->next != NULL)
		n = n->next;
	return &n->next;
}

#define tf_hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define tf_hlist_for_each(pos, head) \
	for (pos = (head)->first; pos; pos = pos->next)

#define tf_hlist_for_each_safe(pos, n, head) \
	for (pos = (head)->first; pos && ({ n = pos->next; 1; }); pos = n)

#define tf_hlist_for_each_entry(tpos, pos, head, member)			 \
	for (pos = (head)->first; pos && 				 \
		({ tpos = tf_hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

#define tf_hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
	for (pos = (head)->first;					 \
	     pos && ({ n = pos->next; 1; }) &&				 \
		({ tpos = tf_hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = n)


struct tf_list_node {
	struct tf_list_node *next, *prev;
};

struct tf_list_head {
	struct tf_list_node node;
};

#define TF_LIST_INITIALIZER(l) { .next = &l, .prev = &l }
#define TF_LIST_HEAD_INITIALIZER(l) { .node = TF_LIST_INITIALIZER((l).node) }

static inline void tf_list_init(struct tf_list_node *list)
{
	list->next = list;
	list->prev = list;
}

static inline void tf_list_init_head(struct tf_list_head *head)
{
	tf_list_init(&head->node);
}

static inline void __tf_list_add(struct tf_list_node *new,
			      struct tf_list_node *prev,
			      struct tf_list_node *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

static inline void tf_list_add_after(struct tf_list_node *new, struct tf_list_node *head)
{
	__tf_list_add(new, head, head->next);
}

static inline void tf_list_add_before(struct tf_list_node *new, struct tf_list_node *head)
{
	__tf_list_add(new, head->prev, head);
}

static inline void tf_list_add_head(struct tf_list_node *new, struct tf_list_head *head)
{
	tf_list_add_after(new, &head->node);
}

static inline void tf_list_add_tail(struct tf_list_node *new, struct tf_list_head *head)
{
	tf_list_add_before(new, &head->node);
}

static inline void __tf_list_del(struct tf_list_node * prev, struct tf_list_node *next)
{
	next->prev = prev;
	prev->next = next;
}

static inline void tf_list_del(struct tf_list_node *entry)
{
	__tf_list_del(entry->prev, entry->next);
	entry->next = NULL;
	entry->prev = NULL;
}

static inline int tf_list_hashed(const struct tf_list_node *n)
{
	return n->next != n && n->next != NULL;
}

static inline int tf_list_empty(const struct tf_list_head *h)
{
	return !tf_list_hashed(&h->node);
}

#define tf_list_next(ptr, type, member) \
	(tf_list_hashed(ptr) ? container_of((ptr)->next,type,member) : NULL)

#define tf_list_first(head, type, member) \
	tf_list_next(&((head)->node), type, member)

#define tf_list_entry(ptr, type, member) container_of(ptr,type,member)

#define tf_list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

#define tf_list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)

#define tf_list_for_each_entry(pos, head, member)				\
	for (pos = tf_list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = tf_list_entry(pos->member.next, typeof(*pos), member))

#define tf_list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = tf_list_entry((head)->next, typeof(*pos), member),	\
		n = tf_list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = n, n = tf_list_entry(n->member.next, typeof(*n), member))

#endif