Concord - C Discord API library
A Discord API wrapper library written in C
queue.h
Go to the documentation of this file.
1/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15
16#ifndef QUEUE_H_
17#define QUEUE_H_
18
19#include <stddef.h>
20
21typedef void *QUEUE[2];
22
23/* Improve readability by letting user specify underlying type. */
24#define QUEUE(type) QUEUE
25
26/* Private macros. */
27#define QUEUE_NEXT(q) (*(QUEUE **) &((*(q))[0]))
28#define QUEUE_PREV(q) (*(QUEUE **) &((*(q))[1]))
29#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))
30#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))
31
32/* Public macros. */
33#define QUEUE_DATA(ptr, type, field) \
34 ((type *) ((char *) (ptr) - offsetof(type, field)))
35
36/* Important note: mutating the list while QUEUE_FOREACH is
37 * iterating over its elements results in undefined behavior.
38 */
39#define QUEUE_FOREACH(q, h) \
40 for ((q) = QUEUE_NEXT(h); (q) != (h); (q) = QUEUE_NEXT(q))
41
42#define QUEUE_EMPTY(q) \
43 ((const QUEUE *) (q) == (const QUEUE *) QUEUE_NEXT(q))
44
45#define QUEUE_HEAD(q) \
46 (QUEUE_NEXT(q))
47
48#define QUEUE_INIT(q) \
49 do { \
50 QUEUE_NEXT(q) = (q); \
51 QUEUE_PREV(q) = (q); \
52 } \
53 while (0)
54
55#define QUEUE_ADD(h, n) \
56 do { \
57 QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n); \
58 QUEUE_NEXT_PREV(n) = QUEUE_PREV(h); \
59 QUEUE_PREV(h) = QUEUE_PREV(n); \
60 QUEUE_PREV_NEXT(h) = (h); \
61 } \
62 while (0)
63
64#define QUEUE_SPLIT(h, q, n) \
65 do { \
66 QUEUE_PREV(n) = QUEUE_PREV(h); \
67 QUEUE_PREV_NEXT(n) = (n); \
68 QUEUE_NEXT(n) = (q); \
69 QUEUE_PREV(h) = QUEUE_PREV(q); \
70 QUEUE_PREV_NEXT(h) = (h); \
71 QUEUE_PREV(q) = (n); \
72 } \
73 while (0)
74
75#define QUEUE_MOVE(h, n) \
76 do { \
77 if (QUEUE_EMPTY(h)) \
78 QUEUE_INIT(n); \
79 else { \
80 QUEUE* q = QUEUE_HEAD(h); \
81 QUEUE_SPLIT(h, q, n); \
82 } \
83 } \
84 while (0)
85
86#define QUEUE_INSERT_HEAD(h, q) \
87 do { \
88 QUEUE_NEXT(q) = QUEUE_NEXT(h); \
89 QUEUE_PREV(q) = (h); \
90 QUEUE_NEXT_PREV(q) = (q); \
91 QUEUE_NEXT(h) = (q); \
92 } \
93 while (0)
94
95#define QUEUE_INSERT_TAIL(h, q) \
96 do { \
97 QUEUE_NEXT(q) = (h); \
98 QUEUE_PREV(q) = QUEUE_PREV(h); \
99 QUEUE_PREV_NEXT(q) = (q); \
100 QUEUE_PREV(h) = (q); \
101 } \
102 while (0)
103
104#define QUEUE_REMOVE(q) \
105 do { \
106 QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q); \
107 QUEUE_NEXT_PREV(q) = QUEUE_PREV(q); \
108 } \
109 while (0)
110
111#endif /* QUEUE_H_ */
#define QUEUE(type)
Definition: queue.h:24