summaryrefslogtreecommitdiff
path: root/lab03/list.h
blob: 7d4ca23559995b6361a692e2198a9b0d7456d900 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* SPDX-License-Identifier: GPL-2.0 */

/**
 * @file list.h
 * @brief Intrusive circular doubly linked list primitives.
 *
 * A list node is embedded in the structure that owns it. The list head is
 * represented by a sentinel `struct list_head` whose links point to itself
 * when the list is empty.
 */
#ifndef LIST_H
#define LIST_H

#include <stddef.h>
#include <stdbool.h>
#include <assert.h>

#include "container_of.h"

/**
 * @brief A node in an intrusive circular doubly linked list.
 */
struct list_head {
  struct list_head *next;
  struct list_head *prev;
};

/** @brief Poison value written to a deleted node's `next` link. */
#define LIST_POISON1  ((void *) 0x100)

/** @brief Poison value written to a deleted node's `prev` link. */
#define LIST_POISON2  ((void *) 0x122)

/**
 * @brief Circular doubly linked list implementation.
 *
 * Some internal functions are useful when manipulating whole lists because
 * the neighboring nodes are already known.
 */

/** @brief Initializes a list head initializer. */
#define LIST_HEAD_INIT(name) { &(name), &(name) }

/**
 * @brief Declares and initializes an empty list head.
 * @param name The list head variable name.
 */
#define LIST_HEAD(name) \
    struct list_head name = LIST_HEAD_INIT(name)

/**
 * @brief Initializes a list node or list head.
 *
 * The node is made self-linked. When used as a list head, this represents
 * an empty list.
 *
 * @param list The node or list head to initialize.
 * @post `list->next == list`.
 * @post `list->prev == list`.
 */
static inline void INIT_LIST_HEAD(struct list_head *list)
{
    list->next = list;
    list->prev = list;
}

/**
 * @internal
 * @brief Links a node between two known adjacent nodes.
 */
static inline void __list_add(struct list_head *node,
                              struct list_head *prev,
                              struct list_head *next)
{
    next->prev = node;
    node->next = next;
    node->prev = prev;
    prev->next = node;
}

/**
 * @brief Inserts a node immediately after a list node.
 *
 * This operation is suitable for stack-like insertion.
 *
 * @param node The node to insert.
 * @param head The node after which `node` is inserted.
 * @pre `node` is not currently linked into a list.
 */
static inline void list_add(struct list_head *node,
                            struct list_head *head)
{
    __list_add(node, head, head->next);
}

/**
 * @brief Inserts a node immediately before a list node.
 *
 * This operation is suitable for queue-like insertion when `head` is the
 * list sentinel.
 *
 * @param node The node to insert.
 * @param head The node before which `node` is inserted.
 * @pre `node` is not currently linked into a list.
 */
static inline void list_add_tail(struct list_head *node,
                                 struct list_head *head)
{
    __list_add(node, head->prev, head);
}

/**
 * @internal
 * @brief Unlinks the range between two known adjacent nodes.
 */
static inline void __list_del(struct list_head *prev,
                              struct list_head *next)
{
    next->prev = prev;
    prev->next = next;
}

/**
 * @internal
 * @brief Unlinks a node using its stored neighboring links.
 */
static inline void __list_del_entry(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
}

/**
 * @brief Removes a node from its list.
 *
 * After this operation, the node's links contain poison values and must not
 * be used for list traversal.
 *
 * @param entry The node to remove.
 * @pre `entry` belongs to a valid list.
 */
static inline void list_del(struct list_head *entry)
{
	__list_del_entry(entry);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}

/**
 * @brief Replaces one list node with another.
 *
 * The replacement node takes the position of the old node.
 *
 * @param old The node to replace.
 * @param new The replacement node.
 * @pre `old` belongs to a valid list.
 * @pre `new` is not currently linked into a list.
 */
static inline void list_replace(struct list_head *old,
				struct list_head *new)
{
	new->next = old->next;
	new->next->prev = new;
	new->prev = old->prev;
	new->prev->next = new;
}

/**
 * @brief Replaces one node and reinitializes the old node.
 *
 * @param old The node to replace.
 * @param new The replacement node.
 * @pre `old` belongs to a valid list.
 * @pre `new` is not currently linked into a list.
 * @post `old` is a self-linked standalone node.
 */
static inline void list_replace_init(struct list_head *old,
				     struct list_head *new)
{
	list_replace(old, new);
	INIT_LIST_HEAD(old);
}

/**
 * @brief Removes a node from its list and inserts it after `head`.
 *
 * @param list The node to move.
 * @param head The node that precedes the moved node.
 * @pre `list` belongs to a valid list.
 */
static inline void list_move(struct list_head *list, struct list_head *head)
{
	__list_del_entry(list);
	list_add(list, head);
}

/**
 * @brief Removes a node from its list and inserts it before `head`.
 *
 * @param list The node to move.
 * @param head The node that follows the moved node.
 * @pre `list` belongs to a valid list.
 */
static inline void list_move_tail(struct list_head *list,
				  struct list_head *head)
{
	__list_del_entry(list);
	list_add_tail(list, head);
}

/**
 * @brief Tests whether a node is the first entry in a list.
 *
 * @param list The node to test.
 * @param head The list head.
 * @return Nonzero if `list` is the first entry; zero otherwise.
 */
static inline int list_is_first(const struct list_head *list, const struct list_head *head)
{
	return list->prev == head;
}

/**
 * @brief Tests whether a node is the last entry in a list.
 *
 * @param list The node to test.
 * @param head The list head.
 * @return Nonzero if `list` is the last entry; zero otherwise.
 */
static inline int list_is_last(const struct list_head *list, const struct list_head *head)
{
	return list->next == head;
}

/**
 * @brief Tests whether a node is the list head.
 *
 * @param list The node to test.
 * @param head The list head.
 * @return Nonzero if `list == head`; zero otherwise.
 */
static inline int list_is_head(const struct list_head *list, const struct list_head *head)
{
	return list == head;
}

/**
 * @brief Tests whether a list is empty.
 *
 * @param head The list head.
 * @return Nonzero if the list contains no entries; zero otherwise.
 */
static inline int list_empty(const struct list_head *head)
{
	return head->next == head;
}

/**
 * @brief Returns the containing structure for a list node.
 *
 * @param ptr A pointer to the embedded `struct list_head`.
 * @param type The containing structure type.
 * @param member The embedded list member.
 * @return A pointer to the containing structure.
 */
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)

/**
 * @brief Returns the first containing structure in a list.
 *
 * @param ptr The list head.
 * @param type The containing structure type.
 * @param member The embedded list member.
 * @pre The list is not empty.
 */
#define list_first_entry(ptr, type, member) \
	list_entry((ptr)->next, type, member)

/**
 * @brief Returns the last containing structure in a list.
 *
 * @param ptr The list head.
 * @param type The containing structure type.
 * @param member The embedded list member.
 * @pre The list is not empty.
 */
#define list_last_entry(ptr, type, member) \
	list_entry((ptr)->prev, type, member)

/**
 * @brief Returns the containing structure following the current node.
 *
 * @param pos The typed cursor.
 * @param member The embedded list member.
 * @return The next containing structure.
 */
#define list_next_entry(pos, member) \
	list_entry((pos)->member.next, typeof(*(pos)), member)

/**
 * @brief Returns the containing structure preceding the current node.
 *
 * @param pos The typed cursor.
 * @param member The embedded list member.
 * @return The previous containing structure.
 */
#define list_prev_entry(pos, member) \
	list_entry((pos)->member.prev, typeof(*(pos)), member)

/**
 * @brief Tests whether a typed cursor refers to the list head.
 *
 * @param pos The typed cursor.
 * @param head The list head.
 * @param member The embedded list member.
 * @return Nonzero if `pos` refers to `head`; zero otherwise.
 */
#define list_entry_is_head(pos, head, member)				\
	(&pos->member == (head))

/**
 * @brief Iterates over containing structures in forward order.
 *
 * Example:
 * @code
 * struct item *pos;
 * list_for_each_entry(pos, &head, link) {
 *     use_item(pos);
 * }
 * @endcode
 *
 * @param pos The typed loop cursor.
 * @param head The list head.
 * @param member The embedded list member.
 */
#define list_for_each_entry(pos, head, member)				\
	for (pos = list_first_entry(head, typeof(*pos), member);	\
	     !list_entry_is_head(pos, head, member);			\
	     pos = list_next_entry(pos, member))

/**
 * @brief Iterates over containing structures in reverse order.
 *
 * @param pos The typed loop cursor.
 * @param head The list head.
 * @param member The embedded list member.
 */
#define list_for_each_entry_reverse(pos, head, member)			\
	for (pos = list_last_entry(head, typeof(*pos), member);		\
	     !list_entry_is_head(pos, head, member); 			\
	     pos = list_prev_entry(pos, member))

/**
 * @brief Iterates over containing structures while allowing removal.
 *
 * The additional cursor stores the next entry before the loop body runs, so
 * the current entry may be removed safely from the list.
 *
 * @param pos The typed loop cursor.
 * @param n The temporary typed cursor.
 * @param head The list head.
 * @param member The embedded list member.
 */
#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_first_entry(head, typeof(*pos), member),	\
		n = list_next_entry(pos, member);			\
	     !list_entry_is_head(pos, head, member);			\
	     pos = n, n = list_next_entry(n, member))

#endif /* LIST_H */