|
| 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 | +typedef void *QUEUE[2]; |
| 20 | + |
| 21 | +/* Private macros. */ |
| 22 | +#define QUEUE_NEXT(q) ((*(q))[0]) |
| 23 | +#define QUEUE_PREV(q) ((*(q))[1]) |
| 24 | +#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT((QUEUE *) QUEUE_PREV(q))) |
| 25 | +#define QUEUE_NEXT_PREV(q) (QUEUE_PREV((QUEUE *) QUEUE_NEXT(q))) |
| 26 | + |
| 27 | +/* Public macros. */ |
| 28 | +#define QUEUE_DATA(ptr, type, field) \ |
| 29 | + ((type *) ((char *) (ptr) - ((long) &((type *) 0)->field))) |
| 30 | + |
| 31 | +#define QUEUE_FOREACH(q, h) \ |
| 32 | + for ((q) = (QUEUE *) (*(h))[0]; (q) != (h); (q) = (QUEUE *) (*(q))[0]) |
| 33 | + |
| 34 | +#define QUEUE_EMPTY(q) \ |
| 35 | + (QUEUE_NEXT(q) == (q)) |
| 36 | + |
| 37 | +#define QUEUE_HEAD(q) \ |
| 38 | + (QUEUE_NEXT(q)) |
| 39 | + |
| 40 | +#define QUEUE_INIT(q) \ |
| 41 | + do { \ |
| 42 | + QUEUE_NEXT(q) = (q); \ |
| 43 | + QUEUE_PREV(q) = (q); \ |
| 44 | + } \ |
| 45 | + while (0) |
| 46 | + |
| 47 | +#define QUEUE_ADD(h, n) \ |
| 48 | + do { \ |
| 49 | + QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n); \ |
| 50 | + QUEUE_NEXT_PREV(n) = QUEUE_PREV(h); \ |
| 51 | + QUEUE_PREV(h) = QUEUE_PREV(n); \ |
| 52 | + QUEUE_PREV_NEXT(h) = (h); \ |
| 53 | + } \ |
| 54 | + while (0) |
| 55 | + |
| 56 | +#define QUEUE_SPLIT(h, q, n) \ |
| 57 | + do { \ |
| 58 | + QUEUE_PREV(n) = QUEUE_PREV(h); \ |
| 59 | + QUEUE_PREV_NEXT(n) = (n); \ |
| 60 | + QUEUE_NEXT(n) = (q); \ |
| 61 | + QUEUE_PREV(h) = QUEUE_PREV(q); \ |
| 62 | + QUEUE_PREV_NEXT(h) = (h); \ |
| 63 | + QUEUE_PREV(q) = (n); \ |
| 64 | + } \ |
| 65 | + while (0) |
| 66 | + |
| 67 | +#define QUEUE_INSERT_HEAD(h, q) \ |
| 68 | + do { \ |
| 69 | + QUEUE_NEXT(q) = QUEUE_NEXT(h); \ |
| 70 | + QUEUE_PREV(q) = (h); \ |
| 71 | + QUEUE_NEXT_PREV(q) = (q); \ |
| 72 | + QUEUE_NEXT(h) = (q); \ |
| 73 | + } \ |
| 74 | + while (0) |
| 75 | + |
| 76 | +#define QUEUE_INSERT_TAIL(h, q) \ |
| 77 | + do { \ |
| 78 | + QUEUE_NEXT(q) = (h); \ |
| 79 | + QUEUE_PREV(q) = QUEUE_PREV(h); \ |
| 80 | + QUEUE_PREV_NEXT(q) = (q); \ |
| 81 | + QUEUE_PREV(h) = (q); \ |
| 82 | + } \ |
| 83 | + while (0) |
| 84 | + |
| 85 | +#define QUEUE_REMOVE(q) \ |
| 86 | + do { \ |
| 87 | + QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q); \ |
| 88 | + QUEUE_NEXT_PREV(q) = QUEUE_PREV(q); \ |
| 89 | + } \ |
| 90 | + while (0) |
| 91 | + |
| 92 | +#endif /* QUEUE_H_ */ |
0 commit comments