summaryrefslogtreecommitdiffstats
path: root/src/list.h
blob: 61d0e5b22123941fdd554c89720b0c0ff93e2aa9 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright © Tavian Barnes <tavianator@tavianator.com>
// SPDX-License-Identifier: 0BSD

/**
 * Intrusive linked lists.
 *
 * Singly-linked lists are declared like this:
 *
 *     struct item {
 *             struct item *next;
 *     };
 *
 *     struct list {
 *             struct item *head;
 *             struct item **tail;
 *     };
 *
 * The SLIST_*() macros manipulate singly-linked lists.
 *
 *     struct list list;
 *     SLIST_INIT(&list);
 *
 *     struct item item;
 *     SLIST_ITEM_INIT(&item);
 *     SLIST_APPEND(&list, &item);
 *
 * Doubly linked lists are similar:
 *
 *     struct item {
 *             struct item *next;
 *             struct item *prev;
 *     };
 *
 *     struct list {
 *             struct item *head;
 *             struct item *tail;
 *     };
 *
 *     struct list list;
 *     LIST_INIT(&list);
 *
 *     struct item item;
 *     LIST_ITEM_INIT(&item);
 *     LIST_APPEND(&list, &item);
 *
 * Items can be on multiple lists at once:
 *
 *     struct item {
 *             struct {
 *                     struct item *next;
 *             } chain;
 *
 *             struct {
 *                     struct item *next;
 *                     struct item *prev;
 *             } lru;
 *     };
 *
 *     struct items {
 *             struct {
 *                     struct item *head;
 *                     struct item **tail;
 *             } queue;
 *
 *             struct {
 *                     struct item *head;
 *                     struct item *tail;
 *             } cache;
 *     };
 *
 *     struct items items;
 *     SLIST_INIT(&items.queue);
 *     LIST_INIT(&items.cache);
 *
 *     struct item item;
 *     SLIST_ITEM_INIT(&item, chain);
 *     SLIST_APPEND(&items.queue, &item, chain);
 *     LIST_ITEM_INIT(&item, lru);
 *     LIST_APPEND(&items.cache, &item, lru);
 */

#ifndef BFS_LIST_H
#define BFS_LIST_H

#include "diag.h"
#include <stddef.h>
#include <string.h>

/**
 * Initialize a singly-linked list.
 *
 * @param list
 *         The list to initialize.
 *
 * ---
 *
 * Like many macros in this file, this macro delegates the bulk of its work to
 * some helper macros.  We explicitly parenthesize (list) here so the helpers
 * don't have to.
 */
#define SLIST_INIT(list) \
	SLIST_INIT_((list))

/**
 * Helper for SLIST_INIT().
 */
#define SLIST_INIT_(list) LIST_VOID_( \
	list->head = NULL, \
	list->tail = &list->head)

/**
 * Cast a list of expressions to void.
 */
#define LIST_VOID_(...) ((void)(__VA_ARGS__))

/**
 * Initialize a singly-linked list item.
 *
 * @param item
 *         The item to initialize.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 *
 * ---
 *
 * We play some tricks with variadic macros to handle the optional parameter:
 *
 *     SLIST_ITEM_INIT(item)       => item->next = NULL
 *     SLIST_ITEM_INIT(item, node) => item->node.next = NULL
 *
 * The first trick is that
 *
 *     #define SLIST_ITEM_INIT(item, ...)
 *
 * won't work because both commas are required (until C23; see N3033). As a
 * workaround, we dispatch to another macro and add a trailing comma.
 *
 *     SLIST_ITEM_INIT(item)       => SLIST_ITEM_INIT_(item, )
 *     SLIST_ITEM_INIT(item, node) => SLIST_ITEM_INIT_(item, node, )
 */
#define SLIST_ITEM_INIT(...) \
	SLIST_ITEM_INIT_(__VA_ARGS__, )

/**
 * Now we need a way to generate either ->next or ->node.next depending on
 * whether the node parameter was passed.  The approach is based on
 *
 *     #define FOO(...) BAR(__VA_ARGS__, 1, 2, )
 *     #define BAR(x, y, z, ...) z
 *
 *     FOO(a)    => 2
 *     FOO(a, b) => 1
 *
 * The LIST_NEXT_() macro uses this technique:
 *
 *     LIST_NEXT_()       => LIST_NODE_(next, )
 *     LIST_NEXT_(node, ) => LIST_NODE_(next, node, )
 */
#define LIST_NEXT_(...) \
	LIST_NODE_(next, __VA_ARGS__)

/**
 * LIST_NODE_() dispatches to yet another macro:
 *
 *     LIST_NODE_(next, )       => LIST_NODE__(next,     , . ,   , )
 *     LIST_NODE_(next, node, ) => LIST_NODE__(next, node,   , . , , )
 */
#define LIST_NODE_(dir, ...) \
	LIST_NODE__(dir, __VA_ARGS__, . , , )

/**
 * And finally, LIST_NODE__() adds the node and the dot if necessary.
 *
 *                 dir   node ignored dot
 *                  v     v      v     v
 *     LIST_NODE__(next,     ,   .   ,   , )   => next
 *     LIST_NODE__(next, node,       , . , , ) => node . next
 *                  ^     ^      ^     ^
 *                 dir   node ignored dot
 */
#define LIST_NODE__(dir, node, ignored, dot, ...) \
	node dot dir

/**
 * SLIST_ITEM_INIT_() uses LIST_NEXT_() to generate the right name for the list
 * node, and finally delegates to the actual implementation.
 */
#define SLIST_ITEM_INIT_(item, ...) \
	SLIST_ITEM_INIT__((item), LIST_NEXT_(__VA_ARGS__))

#define SLIST_ITEM_INIT__(item, next) \
	LIST_VOID_(item->next = NULL)

/**
 * Type-checking macro for singly-linked lists.
 */
#define SLIST_CHECK_(list) \
	(void)sizeof(list->tail - &list->head)

/**
 * Get the head of a singly-linked list.
 *
 * @param list
 *         The list in question.
 * @return
 *         The first item in the list.
 */
#define SLIST_HEAD(list) \
	SLIST_HEAD_((list))

#define SLIST_HEAD_(list) \
	(SLIST_CHECK_(list), list->head)

/**
 * Check if a singly-linked list is empty.
 */
#define SLIST_EMPTY(list) \
	(!SLIST_HEAD(list))

/**
 * Like container_of(), but using the head pointer instead of offsetof() since
 * we don't have the type around.
 */
#define SLIST_CONTAINER_(tail, head, next) \
	(void *)((char *)tail - ((char *)&head->next - (char *)head))

/**
 * Get the tail of a singly-linked list.
 *
 * @param list
 *         The list in question.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 * @return
 *         The last item in the list.
 */
#define SLIST_TAIL(...) \
	SLIST_TAIL_(__VA_ARGS__, )

#define SLIST_TAIL_(list, ...) \
	SLIST_TAIL__((list), LIST_NEXT_(__VA_ARGS__))

#define SLIST_TAIL__(list, next) \
	(list->head ? SLIST_CONTAINER_(list->tail, list->head, next) : NULL)

/**
 * Check if an item is attached to a singly-linked list.
 *
 * @param list
 *         The list to check.
 * @param item
 *         The item to check.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 * @return
 *         Whether the item is attached to the list.
 */
#define SLIST_ATTACHED(list, ...) \
	SLIST_ATTACHED_(list, __VA_ARGS__, )

#define SLIST_ATTACHED_(list, item, ...) \
	SLIST_ATTACHED__((list), (item), LIST_NEXT_(__VA_ARGS__))

#define SLIST_ATTACHED__(list, item, next) \
	(item->next || list->tail == &item->next)

/**
 * Insert an item into a singly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param cursor
 *         A pointer to the item to insert after, e.g. &list->head or list->tail.
 * @param item
 *         The item to insert.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 * @return
 *         A cursor for the next item.
 */
#define SLIST_INSERT(list, cursor, ...) \
	SLIST_INSERT_(list, cursor, __VA_ARGS__, )

#define SLIST_INSERT_(list, cursor, item, ...) \
	SLIST_INSERT__((list), (cursor), (item), LIST_NEXT_(__VA_ARGS__))

#define SLIST_INSERT__(list, cursor, item, next) \
	(bfs_assert(!SLIST_ATTACHED__(list, item, next)), \
	 item->next = *cursor, \
	 *cursor = item, \
	 list->tail = item->next ? list->tail : &item->next, \
	 &item->next)

/**
 * Add an item to the tail of a singly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param item
 *         The item to append.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 */
#define SLIST_APPEND(list, ...) \
	SLIST_APPEND_(list, __VA_ARGS__, )

#define SLIST_APPEND_(list, item, ...) \
	LIST_VOID_(SLIST_INSERT_(list, (list)->tail, item, __VA_ARGS__))

/**
 * Add an item to the head of a singly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param item
 *         The item to prepend.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 */
#define SLIST_PREPEND(list, ...) \
	SLIST_PREPEND_(list, __VA_ARGS__, )

#define SLIST_PREPEND_(list, item, ...) \
	LIST_VOID_(SLIST_INSERT_(list, &(list)->head, item, __VA_ARGS__))

/**
 * Add an entire singly-linked list to the tail of another.
 *
 * @param dest
 *         The destination list.
 * @param src
 *         The source list.
 */
#define SLIST_EXTEND(dest, src) \
	SLIST_EXTEND_((dest), (src))

#define SLIST_EXTEND_(dest, src) \
	(src->head ? (*dest->tail = src->head, dest->tail = src->tail, SLIST_INIT(src)) : (void)0)

/**
 * Remove an item from a singly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param cursor
 *         A pointer to the item to remove, either &list->head or &prev->next.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 * @return
 *         The removed item.
 */
#define SLIST_REMOVE(list, ...) \
	SLIST_REMOVE_(list, __VA_ARGS__, )

#define SLIST_REMOVE_(list, cursor, ...) \
	SLIST_REMOVE__((list), (cursor), LIST_NEXT_(__VA_ARGS__))

#define SLIST_REMOVE__(list, cursor, next) \
	(list->tail = (*cursor)->next ? list->tail : cursor, \
	 slist_remove_impl(*cursor, cursor, &(*cursor)->next, sizeof(*cursor)))

// Helper for SLIST_REMOVE()
static inline void *slist_remove_impl(void *ret, void *cursor, void *next, size_t size) {
	// ret = *cursor;
	// *cursor = ret->next;
	memcpy(cursor, next, size);
	// ret->next = NULL;
	memset(next, 0, size);
	return ret;
}

/**
 * Pop the head off a singly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param node (optional)
 *         If specified, use head->node.next rather than head->next.
 * @return
 *         The popped item, or NULL if the list was empty.
 */
#define SLIST_POP(...) \
	SLIST_POP_(__VA_ARGS__, )

#define SLIST_POP_(list, ...) \
	SLIST_POP__((list), __VA_ARGS__)

#define SLIST_POP__(list, ...) \
	(list->head ? SLIST_REMOVE_(list, &list->head, __VA_ARGS__) : NULL)

/**
 * Loop over the items in a singly-linked list.
 *
 * @param type
 *         The list item type.
 * @param item
 *         The induction variable name.
 * @param list
 *         The list to iterate.
 * @param node (optional)
 *         If specified, use head->node.next rather than head->next.
 */
#define for_slist(type, item, ...) \
	for_slist_(type, item, __VA_ARGS__, )

#define for_slist_(type, item, list, ...) \
	for_slist__(type, item, (list), LIST_NEXT_(__VA_ARGS__))

#define for_slist__(type, item, list, next) \
	for (type *item = list->head, *_next; \
	     item && (SLIST_CHECK_(list), _next = item->next, true); \
	     item = _next)

/**
 * Initialize a doubly-linked list.
 *
 * @param list
 *         The list to initialize.
 */
#define LIST_INIT(list) \
	LIST_INIT_((list))

#define LIST_INIT_(list) \
	LIST_VOID_(list->head = list->tail = NULL)

/**
 * LIST_PREV_()       => prev
 * LIST_PREV_(node, ) => node.prev
 */
#define LIST_PREV_(...) \
	LIST_NODE_(prev, __VA_ARGS__)

/**
 * Initialize a doubly-linked list item.
 *
 * @param item
 *         The item to initialize.
 * @param node (optional)
 *         If specified, use item->node.next rather than item->next.
 */
#define LIST_ITEM_INIT(...) \
	LIST_ITEM_INIT_(__VA_ARGS__, )

#define LIST_ITEM_INIT_(item, ...) \
	LIST_ITEM_INIT__((item), LIST_PREV_(__VA_ARGS__), LIST_NEXT_(__VA_ARGS__))

#define LIST_ITEM_INIT__(item, prev, next) \
	LIST_VOID_(item->prev = item->next = NULL)

/**
 * Type-checking macro for doubly-linked lists.
 */
#define LIST_CHECK_(list) \
	(void)sizeof(list->tail - list->head)

/**
 * Check if a doubly-linked list is empty.
 */
#define LIST_EMPTY(list) \
	LIST_EMPTY_((list))

#define LIST_EMPTY_(list) \
	(LIST_CHECK_(list), !list->head)

/**
 * Add an item to the tail of a doubly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param item
 *         The item to append.
 * @param node (optional)
 *         If specified, use item->node.{prev,next} rather than item->{prev,next}.
 */
#define LIST_APPEND(list, ...) \
	LIST_INSERT(list, (list)->tail, __VA_ARGS__)

/**
 * Add an item to the head of a doubly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param item
 *         The item to prepend.
 * @param node (optional)
 *         If specified, use item->node.{prev,next} rather than item->{prev,next}.
 */
#define LIST_PREPEND(list, ...) \
	LIST_INSERT(list, NULL, __VA_ARGS__)

/**
 * Check if an item is attached to a doubly-linked list.
 *
 * @param list
 *         The list to check.
 * @param item
 *         The item to check.
 * @param node (optional)
 *         If specified, use item->node.{prev,next} rather than item->{prev,next}.
 * @return
 *         Whether the item is attached to the list.
 */
#define LIST_ATTACHED(list, ...) \
	LIST_ATTACHED_(list, __VA_ARGS__, )

#define LIST_ATTACHED_(list, item, ...) \
	LIST_ATTACHED__((list), (item), LIST_PREV_(__VA_ARGS__), LIST_NEXT_(__VA_ARGS__))

#define LIST_ATTACHED__(list, item, prev, next) \
	(item->prev || item->next || list->head == item || list->tail == item)

/**
 * Insert into a doubly-linked list after the given cursor.
 *
 * @param list
 *         The list to modify.
 * @param cursor
 *         Insert after this element.
 * @param item
 *         The item to insert.
 * @param node (optional)
 *         If specified, use item->node.{prev,next} rather than item->{prev,next}.
 */
#define LIST_INSERT(list, cursor, ...) \
	LIST_INSERT_(list, cursor, __VA_ARGS__, )

#define LIST_INSERT_(list, cursor, item, ...) \
	LIST_INSERT__((list), (cursor), (item), LIST_PREV_(__VA_ARGS__), LIST_NEXT_(__VA_ARGS__))

#define LIST_INSERT__(list, cursor, item, prev, next) LIST_VOID_( \
	bfs_assert(!LIST_ATTACHED__(list, item, prev, next)), \
	item->prev = cursor, \
	item->next = cursor ? cursor->next : list->head, \
	*(item->prev ? &item->prev->next : &list->head) = item, \
	*(item->next ? &item->next->prev : &list->tail) = item)

/**
 * Remove an item from a doubly-linked list.
 *
 * @param list
 *         The list to modify.
 * @param item
 *         The item to remove.
 * @param node (optional)
 *         If specified, use item->node.{prev,next} rather than item->{prev,next}.
 */
#define LIST_REMOVE(list, ...) \
	LIST_REMOVE_(list, __VA_ARGS__, )

#define LIST_REMOVE_(list, item, ...) \
	LIST_REMOVE__((list), (item), LIST_PREV_(__VA_ARGS__), LIST_NEXT_(__VA_ARGS__))

#define LIST_REMOVE__(list, item, prev, next) LIST_VOID_( \
	*(item->prev ? &item->prev->next : &list->head) = item->next, \
	*(item->next ? &item->next->prev : &list->tail) = item->prev, \
	item->prev = item->next = NULL)

/**
 * Loop over the items in a doubly-linked list.
 *
 * @param type
 *         The list item type.
 * @param item
 *         The induction variable name.
 * @param list
 *         The list to iterate.
 * @param node (optional)
 *         If specified, use head->node.next rather than head->next.
 */
#define for_list(type, item, ...) \
	for_list_(type, item, __VA_ARGS__, )

#define for_list_(type, item, list, ...) \
	for_list__(type, item, (list), LIST_NEXT_(__VA_ARGS__))

#define for_list__(type, item, list, next) \
	for (type *item = list->head, *_next; \
	     item && (LIST_CHECK_(list), _next = item->next, true); \
	     item = _next)

#endif // BFS_LIST_H