summaryrefslogtreecommitdiff
path: root/src/kernel/queue.h
blob: 896824fe0a4328c3bf96e060b7c3aed11778ea55 (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
/* documented in queue(9) */
#include <kernel/panic.h>

/* struct QueueNode {
 *   ...
 *   struct QueueNode *name_next;
 *   ...
 * }
 * struct QueueHead {
 *   struct QueueNode *head, **slot;
 * }
 */

#define QUEUE_INIT(q) do { \
	(q)->head = NULL; \
	(q)->slot = &(q)->head; \
} while(0)

#define QUEUE_APPEND(q, name, el) do { \
	assert((el)->name##_next == NULL); \
	assert((q)->slot != NULL); \
	assert(*(q)->slot == NULL); \
	*(q)->slot = (el); \
	(q)->slot = &(el)->name##_next; \
} while(0)

#define QUEUE_POP(q, name) ({ \
	typeof((q)->head) __q_el = (q)->head; \
	if (__q_el) { \
		(q)->head = __q_el->name##_next; \
		__q_el->name##_next = NULL; \
		if ((q)->slot == &__q_el->name##_next) { \
			(q)->slot = &(q)->head; \
		} \
	} \
	__q_el; \
})