From ece4e31c52de1dc0fed205ecf710824a02e63a53 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 21 Jul 2013 07:53:10 +0200 Subject: [PATCH] Convert from homebrew linked list to BSD TAILQ using OpenBSD r1.38 sys/queue.h - OpenBSD http://www.openbsd.org/cgi-bin/cvsweb/src/sys/sys/queue.h rev 1.38 - Convert lList*() to TAILQ_*() - Minor local variable rename to clarify usage Signed-off-by: Joachim Nilsson --- libuev.c | 175 ++++++++------- libuev.h | 37 ++-- llist.h | 159 -------------- queue.h | 648 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 764 insertions(+), 255 deletions(-) delete mode 100644 llist.h create mode 100644 queue.h diff --git a/libuev.c b/libuev.c index 7e463ed..29466cb 100644 --- a/libuev.c +++ b/libuev.c @@ -53,7 +53,7 @@ uev_io_t *uev_io_create(uev_t *ctx, int fd, uev_dir_t dir, uev_io_cb_t handler, w->data = data; w->index = -1; - lListAppend(&ctx->io_list, w); + TAILQ_INSERT_TAIL(&ctx->io_list, w, link); return w; } @@ -67,7 +67,7 @@ int uev_io_delete(uev_t *ctx, uev_io_t *w) return -1; } - lListForeachIn(ent, &ctx->io_delist) { + TAILQ_FOREACH(ent, &ctx->io_delist, link) { if (ent->ent == w) { errno = EALREADY; return -1; /* Already removed */ @@ -75,7 +75,7 @@ int uev_io_delete(uev_t *ctx, uev_io_t *w) } w->handler = NULL; - lListAppend(&ctx->io_delist, &w->rem); + TAILQ_INSERT_TAIL(&ctx->io_delist, &w->rem, link); return 0; } @@ -87,14 +87,14 @@ uev_timer_t *uev_timer_create(uev_t *ctx, int msec, uev_timer_cb_t handler, void { int diff; clock_t now; - uev_timer_t *w, *ret, *ent; + uev_timer_t *w, *ret, *tmp; if (!ctx) { errno = EINVAL; return NULL; } - w = (uev_timer_t *)malloc(sizeof(*ent)); + w = (uev_timer_t *)malloc(sizeof(*w)); if (!w) return NULL; @@ -102,35 +102,37 @@ uev_timer_t *uev_timer_create(uev_t *ctx, int msec, uev_timer_cb_t handler, void w->handler = (void *)handler; w->data = data; + /* Zero delay is a special case: A oneshot workproc: Must go first */ if (0 == msec) { - /* Zero delay is a special case: A oneshot workproc: Must go first */ - lListInsertFirst(&ctx->timer_list, w); - + TAILQ_INSERT_HEAD(&ctx->timer_list, w, link); return w; } /* Adjust timers. * Jiffie wraparound is OK since we only care about diff time */ - now = ctx->use_next ? ctx->next_time : times(NULL); - diff = (now - ctx->base_time) / clock_tick * 1000 + - ((now - ctx->base_time) % clock_tick) * 1000 / clock_tick; + now = ctx->use_next ? ctx->next_time : times(NULL); + diff = TIME_DIFF_MSEC(now, ctx->base_time); + /* To be returned below */ ret = w; - lListForeachIn(ent, &ctx->timer_list) { - if (ent->due > diff) - ent->due -= diff; - else - ent->due = 0; - if (w && ent->due > msec) { - lListInsert(&ctx->timer_list, w, ent); + /* Update all timers and add this entry (ordered) to the list */ + TAILQ_FOREACH(tmp, &ctx->timer_list, link) { + if (tmp->due > diff) + tmp->due -= diff; + else + tmp->due = 0; + + if (w && tmp->due > msec) { + TAILQ_INSERT_BEFORE(tmp, w, link); w = NULL; } } if (w) - lListAppend(&ctx->timer_list, w); + TAILQ_INSERT_TAIL(&ctx->timer_list, w, link); + /* Update time base */ ctx->base_time = now; return ret; @@ -141,22 +143,22 @@ uev_timer_t *uev_timer_create(uev_t *ctx, int msec, uev_timer_cb_t handler, void */ int uev_timer_delete(uev_t *ctx, uev_timer_t *w) { - uev_timer_t *ent; + uev_timer_t *tmp; if (!ctx || !w) { errno = EINVAL; return -1; } - lListForeachIn(ent, &ctx->timer_delist) { - if (ent == w) { + TAILQ_FOREACH(tmp, &ctx->timer_delist, link) { + if (tmp == w) { errno = EALREADY; return -1; /* Already removed */ } } - lListRemove(&ctx->timer_list, w); - lListAppend(&ctx->timer_delist, w); /* Remember to free it (at a safe time) */ + TAILQ_REMOVE(&ctx->timer_list, w, link); + TAILQ_INSERT_TAIL(&ctx->timer_delist, w, link); return 0; } @@ -166,44 +168,56 @@ int uev_timer_delete(uev_t *ctx, uev_timer_t *w) */ static int do_run_pending(uev_t *ctx, int immediate) { - int result, timeout = 0; - clock_t now; - uev_timer_t *timer; + int i, result, diff = 0; + size_t num = 0; + clock_t now = 0; + uev_io_t *w; + uev_timer_t *timer, *tmp; /* Do due timers */ - if (lListHead(&ctx->timer_list)) { - now = ctx->use_next ? ctx->next_time : times(NULL); - timeout = (now - ctx->base_time) / clock_tick * 1000 - + ((now - ctx->base_time) % clock_tick) * 1000 / clock_tick; - while ((timer = lListHead(&ctx->timer_list)) && timer->due <= timeout) { + if (!TAILQ_EMPTY(&ctx->timer_list)) { + now = ctx->use_next ? ctx->next_time : times(NULL); + diff = TIME_DIFF_MSEC(now, ctx->base_time); + + TAILQ_FOREACH(timer, &ctx->timer_list, link) { + if (timer->due > diff) + continue; + timer->handler((struct uev *)ctx, timer, timer->data); uev_timer_delete(ctx, timer); - /* base_time changes for every uev_timer_create */ - timeout = (now - ctx->base_time) / clock_tick * 1000 - + ((now - ctx->base_time) % clock_tick) * 1000 / clock_tick; + /* base_time changes for every uev_timer_create(), which + * can be called in every timer->handler() above. */ + diff = TIME_DIFF_MSEC(now, ctx->base_time); } - lListPurge(&ctx->timer_delist); /* Reap timer zombies */ + /* Reap timer zombies */ + TAILQ_FOREACH_SAFE(timer, &ctx->timer_delist, link, tmp) { + TAILQ_REMOVE(&ctx->timer_delist, timer, link); + free(timer); + } } /* Calculate waiting time */ if (ctx->exiting) - timeout = 0; - else if ((timer = lListHead(&ctx->timer_list)) && timer->due > timeout) - timeout = timer->due - timeout; + diff = 0; + else if ((timer = TAILQ_FIRST(&ctx->timer_list)) && timer->due > diff) + diff = timer->due - diff; else if (NULL != timer) - timeout = 0; + diff = 0; else - timeout = -1; + diff = -1; /* Do pipe handlers */ - if (lListHead(&ctx->io_list) && !ctx->exiting) { - int i = 0; - struct pollfd polls[lListLength(&ctx->io_list)]; - uev_io_t *w; + TAILQ_FOREACH(w, &ctx->io_list, link) + num++; - lListForeachIn(w, &ctx->io_list) { + if (!TAILQ_EMPTY(&ctx->io_list) && !ctx->exiting) { + struct pollfd polls[num]; + + /* Prepare all I/O watchers for poll() */ + i = 0; + TAILQ_FOREACH(w, &ctx->io_list, link) { w->index = i; polls[i].fd = w->fd; polls[i].events = UEV_FHIN == w->dir ? POLLIN : POLLOUT; @@ -211,14 +225,12 @@ static int do_run_pending(uev_t *ctx, int immediate) i++; } -// fprintf(stderr, "Polling %d sec\n", timeout); - result = poll(polls, i, immediate ? 0 : timeout); - if (result < 0) { + while ((result = poll(polls, i, immediate ? 0 : diff)) < 0) { if (EINTR == errno) - return 0; + continue; /* Signalled, try again */ if (EBADF == errno) { - lListForeachIn(w, &ctx->io_list) { + TAILQ_FOREACH(w, &ctx->io_list, link) { if (fcntl(w->fd, F_GETFD) == -1 && EBADF == errno) w->handler((struct uev *)ctx, w, w->fd, w->data); } @@ -227,31 +239,32 @@ static int do_run_pending(uev_t *ctx, int immediate) assert(false); return; } - } else if (result > 0) { - lListForeachIn(w, &ctx->io_list) { -// fprintf(stderr, "I/O event!\n"); - if (w->handler && w->index >= 0 && polls[w->index].revents) { + } + + if (result > 0) { + TAILQ_FOREACH(w, &ctx->io_list, link) { + if (w->handler && w->index >= 0 && polls[w->index].revents) w->handler((struct uev *)ctx, w, w->fd, w->data); - } } } - if (lListHead(&ctx->io_delist)) { - uev_io_rem_t *w; + if (!TAILQ_EMPTY(&ctx->io_delist)) { + uev_io_rem_t *tmp; - while ((w = lListHead(&ctx->io_delist))) { - lListRemove(&ctx->io_delist, w); - lListRemove(&ctx->io_list, w->ent); - free(w->ent); + TAILQ_FOREACH(tmp, &ctx->io_delist, link) { + TAILQ_REMOVE(&ctx->io_delist, tmp, link); + TAILQ_REMOVE(&ctx->io_list, tmp->ent, link); + free(tmp->ent); } } - } else if (timeout > 0) { + } else if (diff > 0) { if (!immediate) - poll(NULL, 0, timeout); - } else if (timeout == -1) - timeout = -2; /* Nothing more to do */ + poll(NULL, 0, diff); + } else if (diff == -1) { + diff = -2; /* Nothing more to do */ + } - return timeout; + return diff; } /** @@ -280,8 +293,8 @@ void uev_run(uev_t *ctx) */ void uev_run_tick(uev_t *ctx, int msec) { - // Obviously we are executing a callback right now (arent we always) - // The time leap is taken into account before falling back into poll() below + /* Obviously we are executing a callback right now (arent we always) + * The time leap is taken into account before falling back into poll() below */ if (msec >= 0) { if (!ctx->use_next) { ctx->use_next = true; @@ -325,10 +338,10 @@ uev_t *uev_ctx_create(void) if (!ctx) return NULL; - lListInit(&ctx->io_list); - lListInit(&ctx->timer_list); - lListInit(&ctx->io_delist); - lListInit(&ctx->timer_delist); + TAILQ_INIT(&ctx->io_list); + TAILQ_INIT(&ctx->timer_list); + TAILQ_INIT(&ctx->io_delist); + TAILQ_INIT(&ctx->timer_delist); ctx->base_time = times(NULL); ctx->next_time = 0; @@ -336,6 +349,7 @@ uev_t *uev_ctx_create(void) ctx->exiting = false; + /* Get system clock resolution, in ticks/second */ if (0 == clock_tick) clock_tick = sysconf(_SC_CLK_TCK); @@ -347,8 +361,19 @@ uev_t *uev_ctx_create(void) */ void uev_ctx_delete(uev_t *ctx) { - lListPurge(&ctx->io_list); - lListPurge(&ctx->timer_list); + uev_io_t *io, *tmp_io; + uev_timer_t *timer, *tmp_timer; + + TAILQ_FOREACH_SAFE(io, &ctx->io_list, link, tmp_io) { + TAILQ_REMOVE(&ctx->io_list, io, link); + free(io); + } + + TAILQ_FOREACH_SAFE(timer, &ctx->timer_list, link, tmp_timer) { + TAILQ_REMOVE(&ctx->timer_list, timer, link); + free(timer); + } + free(ctx); } diff --git a/libuev.h b/libuev.h index 070f983..4f4fdc9 100644 --- a/libuev.h +++ b/libuev.h @@ -26,7 +26,12 @@ #ifndef LIBUEV_H_ #define LIBUEV_H_ -#include "llist.h" +#include "queue.h" + +/* Elapsed time between two times() tick measurements in msec */ +#define TIME_DIFF_MSEC(now, then) \ + ( (now - then) / clock_tick * 1000 + \ + ((now - then) % clock_tick) * 1000 / clock_tick ) /* Forward declarations due to dependencys, don't try this at home. */ struct uev; @@ -41,12 +46,12 @@ typedef enum { /* I/O event watcher */ typedef struct uev_io_rem { - LListField(struct uev_io_rem); + TAILQ_ENTRY(uev_io_rem) link; struct uev_io *ent; } uev_io_rem_t; typedef struct uev_io { - LListField(struct uev_io); ///< Elements for linked list + TAILQ_ENTRY(uev_io) link; uev_io_rem_t rem; ///< For postponed removal int fd; ///< File descriptor @@ -54,36 +59,26 @@ typedef struct uev_io { int index; ///< Index in poll array int (*handler)(struct uev *, struct uev_io *, int, void *); - void *data; ///< Callback optional parameter + void *data; } uev_io_t; /* Timer event watcher */ typedef struct uev_timer { - LListField(struct uev_timer); ///< Linked list elements + TAILQ_ENTRY(uev_timer) link; - int due; ///< Epoch timestamp + int due; int (*handler)(struct uev *, struct uev_timer *, void *); - void *data; ///< Timer callback parameter + void *data; } uev_timer_t; -/** - * @cond doxygen does not like this construction - */ -typedef LList(uev_timer_t) uev_timer_list_t; -typedef LList(uev_io_t) uev_io_list_t; -typedef LList(uev_io_rem_t) uev_io_rem_list_t; -/** - * @endcond - */ - /* Main libuev context type */ typedef struct { - uev_io_list_t io_list; ///< File handlers - uev_timer_list_t timer_list; ///< Timer handlers + TAILQ_HEAD(, uev_io) io_list; ///< File handlers + TAILQ_HEAD(, uev_timer) timer_list; ///< Timer handlers - uev_io_rem_list_t io_delist; ///< List of file handles to be garbage collected - uev_timer_list_t timer_delist; ///< List of timer handles to be garbage collected + TAILQ_HEAD(, uev_io_rem) io_delist; ///< List of file handles to be garbage collected + TAILQ_HEAD(, uev_timer) timer_delist; ///< List of timer handles to be garbage collected clock_t base_time; ///< Time at last timer tick clock_t next_time; ///< Next timer tick epoch diff --git a/llist.h b/llist.h deleted file mode 100644 index 8ac35da..0000000 --- a/llist.h +++ /dev/null @@ -1,159 +0,0 @@ -/* libuev - (Doubly) Linked lists anchored at both ends - * - * Copyright (c) 2012 Flemming Madsen - * Copyright (c) 2013 Joachim Nilsson - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -/* Struct mix-in. Use this inside your struct declaration */ -#define LListField(tn) tn * _llNext; tn * _llPrev - -/* Anchor Declaration. Use like: LList(typename) varname; */ -#define LList(tn) struct { tn *head; tn *tail; int numElm; } - -/* List init */ -#define lListInit(a) ((a)->head = (a)->tail = NULL, (a)->numElm = 0, (a)) - -/* List empty */ -#define lListEmpty(a) ((a)->head == NULL) - -/* List head */ -#define lListHead(a) ((a)->head) - -/* List tail */ -#define lListTail(a) ((a)->tail) - -/* List length */ -#define lListLength(a) ((a)->numElm) - -/* List element next */ -#define lListNext(e) ((e)->_llNext) - -/* List element prev */ -#define lListPrev(e) ((e)->_llPrev) - -/* Append element e to list e */ -#define lListAppend(a, e) \ - { \ - if (lListEmpty(a)) \ - (a)->head = (e); \ - else \ - (a)->tail->_llNext = (e); \ - (e)->_llPrev = (a)->tail; \ - (a)->tail = (e); \ - (e)->_llNext = NULL; \ - (a)->numElm++; \ - } - -/* Insert element n before element e in list a */ -#define lListInsertFirst(a, n) lListInsert(a, n, (a)->head) - -/* Insert element n before element e in list a */ -#define lListInsert(a, n, e) \ - { \ - if (!(e) || (a)->head == (e)) { \ - n->_llNext = (a)->head; \ - n->_llPrev = NULL; \ - (a)->head = (n); \ - } else { \ - n->_llNext = (e); \ - n->_llPrev = (e)->_llPrev; \ - (e)->_llPrev->_llNext = n; \ - } \ - if (n->_llNext) n->_llNext->_llPrev = n; \ - (a)->numElm++; \ - } - -/* Remove element e from list a. Caller must free element. */ -#define lListRemove(a, e) \ - { \ - if ((a)->head) { \ - if ((e) == (a)->head) \ - (a)->head = (e)->_llNext; \ - else \ - (e)->_llPrev->_llNext = (e)->_llNext; \ - if ((e) == (a)->tail) \ - (a)->tail = (e)->_llPrev; \ - else \ - (e)->_llNext->_llPrev = (e)->_llPrev; \ - (a)->numElm--; \ - } \ - } - -/* Purge list a elements with free() */ -#define lListPurgeT(a, T) \ - { \ - T p1, p2; \ - for (p1 = (a)->head; p1; ) { \ - p2 = p1; \ - p1 = p1->_llNext; free(p2); \ - } \ - lListInit(a); \ - } - -/* Remove last element from list a */ -#define lListRemoveTailT(a, T) \ - { \ - T p; \ - if ((a)->tail) { \ - p = (a)->tail; \ - (a)->tail = (a)->tail->_llPrev; \ - if ((a)->tail) \ - (a)->tail->_llNext = NULL; \ - else \ - (a)->head = NULL; \ - free(p); \ - (a)->numElm--; \ - } \ - } - -/* Remove first element from list a */ -#define lListRemoveHeadT(a, T) \ - { \ - T p; \ - if ((a)->head) { \ - p = (a)->head; \ - (a)->head = (a)->head->_llNext; \ - if ((a)->head) \ - (a)->head->_llPrev = NULL; \ - else \ - (a)->tail = NULL; \ - free(p); \ - (a)->numElm--; \ - } \ - } - -/* Convenience - * Only use if you have typeof() eg. gcc */ -#define lListPurge(a) lListPurgeT(a, typeof((a)->head)) -#define lListRemoveTail(a) lListRemoveTailT(a, typeof((a)->head)) -#define lListRemoveHead(a) lListRemoveHeadT(a, typeof((a)->head)) - -/* Traverse the list foreach e in a */ -#define lListForeachIn(e, a) for (e = (a)->head; e; e = e->_llNext) - -/** - * Local Variables: - * version-control: t - * indent-tabs-mode: t - * c-file-style: "linux" - * End: - */ diff --git a/queue.h b/queue.h new file mode 100644 index 0000000..f8f09bf --- /dev/null +++ b/queue.h @@ -0,0 +1,648 @@ +/* $OpenBSD: queue.h,v 1.38 2013/07/03 15:05:21 fgsch Exp $ */ +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */ + +/* + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)queue.h 8.5 (Berkeley) 8/20/94 + */ + +#ifndef _SYS_QUEUE_H_ +#define _SYS_QUEUE_H_ + +/* + * This file defines five types of data structures: singly-linked lists, + * lists, simple queues, tail queues, and circular queues. + * + * + * A singly-linked list is headed by a single forward pointer. The elements + * are singly linked for minimum space and pointer manipulation overhead at + * the expense of O(n) removal for arbitrary elements. New elements can be + * added to the list after an existing element or at the head of the list. + * Elements being removed from the head of the list should use the explicit + * macro for this purpose for optimum efficiency. A singly-linked list may + * only be traversed in the forward direction. Singly-linked lists are ideal + * for applications with large datasets and few or no removals or for + * implementing a LIFO queue. + * + * A list is headed by a single forward pointer (or an array of forward + * pointers for a hash table header). The elements are doubly linked + * so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before + * or after an existing element or at the head of the list. A list + * may only be traversed in the forward direction. + * + * A simple queue is headed by a pair of pointers, one the head of the + * list and the other to the tail of the list. The elements are singly + * linked to save space, so elements can only be removed from the + * head of the list. New elements can be added to the list before or after + * an existing element, at the head of the list, or at the end of the + * list. A simple queue may only be traversed in the forward direction. + * + * A tail queue is headed by a pair of pointers, one to the head of the + * list and the other to the tail of the list. The elements are doubly + * linked so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before or + * after an existing element, at the head of the list, or at the end of + * the list. A tail queue may be traversed in either direction. + * + * A circle queue is headed by a pair of pointers, one to the head of the + * list and the other to the tail of the list. The elements are doubly + * linked so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before or after + * an existing element, at the head of the list, or at the end of the list. + * A circle queue may be traversed in either direction, but has a more + * complex end of list detection. + * + * For details on the use of these macros, see the queue(3) manual page. + */ + +#if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC)) +#define _Q_INVALIDATE(a) (a) = ((void *)-1) +#else +#define _Q_INVALIDATE(a) +#endif + +/* + * Singly-linked List definitions. + */ +#define SLIST_HEAD(name, type) \ +struct name { \ + struct type *slh_first; /* first element */ \ +} + +#define SLIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define SLIST_ENTRY(type) \ +struct { \ + struct type *sle_next; /* next element */ \ +} + +/* + * Singly-linked List access methods. + */ +#define SLIST_FIRST(head) ((head)->slh_first) +#define SLIST_END(head) NULL +#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) + +#define SLIST_FOREACH(var, head, field) \ + for((var) = SLIST_FIRST(head); \ + (var) != SLIST_END(head); \ + (var) = SLIST_NEXT(var, field)) + +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST(head); \ + (var) && ((tvar) = SLIST_NEXT(var, field), 1); \ + (var) = (tvar)) + +/* + * Singly-linked List functions. + */ +#define SLIST_INIT(head) { \ + SLIST_FIRST(head) = SLIST_END(head); \ +} + +#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ + (elm)->field.sle_next = (slistelm)->field.sle_next; \ + (slistelm)->field.sle_next = (elm); \ +} while (0) + +#define SLIST_INSERT_HEAD(head, elm, field) do { \ + (elm)->field.sle_next = (head)->slh_first; \ + (head)->slh_first = (elm); \ +} while (0) + +#define SLIST_REMOVE_AFTER(elm, field) do { \ + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \ +} while (0) + +#define SLIST_REMOVE_HEAD(head, field) do { \ + (head)->slh_first = (head)->slh_first->field.sle_next; \ +} while (0) + +#define SLIST_REMOVE(head, elm, type, field) do { \ + if ((head)->slh_first == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } else { \ + struct type *curelm = (head)->slh_first; \ + \ + while (curelm->field.sle_next != (elm)) \ + curelm = curelm->field.sle_next; \ + curelm->field.sle_next = \ + curelm->field.sle_next->field.sle_next; \ + _Q_INVALIDATE((elm)->field.sle_next); \ + } \ +} while (0) + +/* + * List definitions. + */ +#define LIST_HEAD(name, type) \ +struct name { \ + struct type *lh_first; /* first element */ \ +} + +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define LIST_ENTRY(type) \ +struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ +} + +/* + * List access methods + */ +#define LIST_FIRST(head) ((head)->lh_first) +#define LIST_END(head) NULL +#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) +#define LIST_NEXT(elm, field) ((elm)->field.le_next) + +#define LIST_FOREACH(var, head, field) \ + for((var) = LIST_FIRST(head); \ + (var)!= LIST_END(head); \ + (var) = LIST_NEXT(var, field)) + +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = LIST_FIRST(head); \ + (var) && ((tvar) = LIST_NEXT(var, field), 1); \ + (var) = (tvar)) + +/* + * List functions. + */ +#define LIST_INIT(head) do { \ + LIST_FIRST(head) = LIST_END(head); \ +} while (0) + +#define LIST_INSERT_AFTER(listelm, elm, field) do { \ + if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ + (listelm)->field.le_next->field.le_prev = \ + &(elm)->field.le_next; \ + (listelm)->field.le_next = (elm); \ + (elm)->field.le_prev = &(listelm)->field.le_next; \ +} while (0) + +#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + (elm)->field.le_next = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &(elm)->field.le_next; \ +} while (0) + +#define LIST_INSERT_HEAD(head, elm, field) do { \ + if (((elm)->field.le_next = (head)->lh_first) != NULL) \ + (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ + (head)->lh_first = (elm); \ + (elm)->field.le_prev = &(head)->lh_first; \ +} while (0) + +#define LIST_REMOVE(elm, field) do { \ + if ((elm)->field.le_next != NULL) \ + (elm)->field.le_next->field.le_prev = \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev = (elm)->field.le_next; \ + _Q_INVALIDATE((elm)->field.le_prev); \ + _Q_INVALIDATE((elm)->field.le_next); \ +} while (0) + +#define LIST_REPLACE(elm, elm2, field) do { \ + if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ + (elm2)->field.le_next->field.le_prev = \ + &(elm2)->field.le_next; \ + (elm2)->field.le_prev = (elm)->field.le_prev; \ + *(elm2)->field.le_prev = (elm2); \ + _Q_INVALIDATE((elm)->field.le_prev); \ + _Q_INVALIDATE((elm)->field.le_next); \ +} while (0) + +/* + * Simple queue definitions. + */ +#define SIMPLEQ_HEAD(name, type) \ +struct name { \ + struct type *sqh_first; /* first element */ \ + struct type **sqh_last; /* addr of last next element */ \ +} + +#define SIMPLEQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).sqh_first } + +#define SIMPLEQ_ENTRY(type) \ +struct { \ + struct type *sqe_next; /* next element */ \ +} + +/* + * Simple queue access methods. + */ +#define SIMPLEQ_FIRST(head) ((head)->sqh_first) +#define SIMPLEQ_END(head) NULL +#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head)) +#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) + +#define SIMPLEQ_FOREACH(var, head, field) \ + for((var) = SIMPLEQ_FIRST(head); \ + (var) != SIMPLEQ_END(head); \ + (var) = SIMPLEQ_NEXT(var, field)) + +#define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SIMPLEQ_FIRST(head); \ + (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \ + (var) = (tvar)) + +/* + * Simple queue functions. + */ +#define SIMPLEQ_INIT(head) do { \ + (head)->sqh_first = NULL; \ + (head)->sqh_last = &(head)->sqh_first; \ +} while (0) + +#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (head)->sqh_first = (elm); \ +} while (0) + +#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \ + (elm)->field.sqe_next = NULL; \ + *(head)->sqh_last = (elm); \ + (head)->sqh_last = &(elm)->field.sqe_next; \ +} while (0) + +#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (listelm)->field.sqe_next = (elm); \ +} while (0) + +#define SIMPLEQ_REMOVE_HEAD(head, field) do { \ + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ + (head)->sqh_last = &(head)->sqh_first; \ +} while (0) + +#define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \ + if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \ + == NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ +} while (0) + +/* + * XOR Simple queue definitions. + */ +#define XSIMPLEQ_HEAD(name, type) \ +struct name { \ + struct type *sqx_first; /* first element */ \ + struct type **sqx_last; /* addr of last next element */ \ + unsigned long sqx_cookie; \ +} + +#define XSIMPLEQ_ENTRY(type) \ +struct { \ + struct type *sqx_next; /* next element */ \ +} + +/* + * XOR Simple queue access methods. + */ +#define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \ + (unsigned long)(ptr))) +#define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first)) +#define XSIMPLEQ_END(head) NULL +#define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head)) +#define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next)) + + +#define XSIMPLEQ_FOREACH(var, head, field) \ + for ((var) = XSIMPLEQ_FIRST(head); \ + (var) != XSIMPLEQ_END(head); \ + (var) = XSIMPLEQ_NEXT(head, var, field)) + +#define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = XSIMPLEQ_FIRST(head); \ + (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \ + (var) = (tvar)) + +/* + * XOR Simple queue functions. + */ +#define XSIMPLEQ_INIT(head) do { \ + arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \ + (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \ +} while (0) + +#define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \ + if (((elm)->field.sqx_next = (head)->sqx_first) == \ + XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ + (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \ +} while (0) + +#define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \ + (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \ + *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ +} while (0) + +#define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ + if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \ + XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ + (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \ +} while (0) + +#define XSIMPLEQ_REMOVE_HEAD(head, field) do { \ + if (((head)->sqx_first = XSIMPLEQ_XOR(head, \ + (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \ +} while (0) + +#define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \ + if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \ + (elm)->field.sqx_next)->field.sqx_next) \ + == XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = \ + XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ +} while (0) + + +/* + * Tail queue definitions. + */ +#define TAILQ_HEAD(name, type) \ +struct name { \ + struct type *tqh_first; /* first element */ \ + struct type **tqh_last; /* addr of last next element */ \ +} + +#define TAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).tqh_first } + +#define TAILQ_ENTRY(type) \ +struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ +} + +/* + * tail queue access methods + */ +#define TAILQ_FIRST(head) ((head)->tqh_first) +#define TAILQ_END(head) NULL +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) +#define TAILQ_LAST(head, headname) \ + (*(((struct headname *)((head)->tqh_last))->tqh_last)) +/* XXX */ +#define TAILQ_PREV(elm, headname, field) \ + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) +#define TAILQ_EMPTY(head) \ + (TAILQ_FIRST(head) == TAILQ_END(head)) + +#define TAILQ_FOREACH(var, head, field) \ + for((var) = TAILQ_FIRST(head); \ + (var) != TAILQ_END(head); \ + (var) = TAILQ_NEXT(var, field)) + +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = TAILQ_FIRST(head); \ + (var) != TAILQ_END(head) && \ + ((tvar) = TAILQ_NEXT(var, field), 1); \ + (var) = (tvar)) + + +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for((var) = TAILQ_LAST(head, headname); \ + (var) != TAILQ_END(head); \ + (var) = TAILQ_PREV(var, headname, field)) + +#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ + for ((var) = TAILQ_LAST(head, headname); \ + (var) != TAILQ_END(head) && \ + ((tvar) = TAILQ_PREV(var, headname, field), 1); \ + (var) = (tvar)) + +/* + * Tail queue functions. + */ +#define TAILQ_INIT(head) do { \ + (head)->tqh_first = NULL; \ + (head)->tqh_last = &(head)->tqh_first; \ +} while (0) + +#define TAILQ_INSERT_HEAD(head, elm, field) do { \ + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ + (head)->tqh_first->field.tqe_prev = \ + &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (head)->tqh_first = (elm); \ + (elm)->field.tqe_prev = &(head)->tqh_first; \ +} while (0) + +#define TAILQ_INSERT_TAIL(head, elm, field) do { \ + (elm)->field.tqe_next = NULL; \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &(elm)->field.tqe_next; \ +} while (0) + +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ + (elm)->field.tqe_next->field.tqe_prev = \ + &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (listelm)->field.tqe_next = (elm); \ + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ +} while (0) + +#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + (elm)->field.tqe_next = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ +} while (0) + +#define TAILQ_REMOVE(head, elm, field) do { \ + if (((elm)->field.tqe_next) != NULL) \ + (elm)->field.tqe_next->field.tqe_prev = \ + (elm)->field.tqe_prev; \ + else \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ + _Q_INVALIDATE((elm)->field.tqe_prev); \ + _Q_INVALIDATE((elm)->field.tqe_next); \ +} while (0) + +#define TAILQ_REPLACE(head, elm, elm2, field) do { \ + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ + (elm2)->field.tqe_next->field.tqe_prev = \ + &(elm2)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm2)->field.tqe_next; \ + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ + *(elm2)->field.tqe_prev = (elm2); \ + _Q_INVALIDATE((elm)->field.tqe_prev); \ + _Q_INVALIDATE((elm)->field.tqe_next); \ +} while (0) + +/* + * Circular queue definitions. + */ +#define CIRCLEQ_HEAD(name, type) \ +struct name { \ + struct type *cqh_first; /* first element */ \ + struct type *cqh_last; /* last element */ \ +} + +#define CIRCLEQ_HEAD_INITIALIZER(head) \ + { CIRCLEQ_END(&head), CIRCLEQ_END(&head) } + +#define CIRCLEQ_ENTRY(type) \ +struct { \ + struct type *cqe_next; /* next element */ \ + struct type *cqe_prev; /* previous element */ \ +} + +/* + * Circular queue access methods + */ +#define CIRCLEQ_FIRST(head) ((head)->cqh_first) +#define CIRCLEQ_LAST(head) ((head)->cqh_last) +#define CIRCLEQ_END(head) ((void *)(head)) +#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) +#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) +#define CIRCLEQ_EMPTY(head) \ + (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head)) + +#define CIRCLEQ_FOREACH(var, head, field) \ + for((var) = CIRCLEQ_FIRST(head); \ + (var) != CIRCLEQ_END(head); \ + (var) = CIRCLEQ_NEXT(var, field)) + +#define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = CIRCLEQ_FIRST(head); \ + (var) != CIRCLEQ_END(head) && \ + ((tvar) = CIRCLEQ_NEXT(var, field), 1); \ + (var) = (tvar)) + +#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ + for((var) = CIRCLEQ_LAST(head); \ + (var) != CIRCLEQ_END(head); \ + (var) = CIRCLEQ_PREV(var, field)) + +#define CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ + for ((var) = CIRCLEQ_LAST(head, headname); \ + (var) != CIRCLEQ_END(head) && \ + ((tvar) = CIRCLEQ_PREV(var, headname, field), 1); \ + (var) = (tvar)) + +/* + * Circular queue functions. + */ +#define CIRCLEQ_INIT(head) do { \ + (head)->cqh_first = CIRCLEQ_END(head); \ + (head)->cqh_last = CIRCLEQ_END(head); \ +} while (0) + +#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ + (elm)->field.cqe_next = (listelm)->field.cqe_next; \ + (elm)->field.cqe_prev = (listelm); \ + if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm); \ + else \ + (listelm)->field.cqe_next->field.cqe_prev = (elm); \ + (listelm)->field.cqe_next = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ + (elm)->field.cqe_next = (listelm); \ + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ + if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm); \ + else \ + (listelm)->field.cqe_prev->field.cqe_next = (elm); \ + (listelm)->field.cqe_prev = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ + (elm)->field.cqe_next = (head)->cqh_first; \ + (elm)->field.cqe_prev = CIRCLEQ_END(head); \ + if ((head)->cqh_last == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm); \ + else \ + (head)->cqh_first->field.cqe_prev = (elm); \ + (head)->cqh_first = (elm); \ +} while (0) + +#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ + (elm)->field.cqe_next = CIRCLEQ_END(head); \ + (elm)->field.cqe_prev = (head)->cqh_last; \ + if ((head)->cqh_first == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm); \ + else \ + (head)->cqh_last->field.cqe_next = (elm); \ + (head)->cqh_last = (elm); \ +} while (0) + +#define CIRCLEQ_REMOVE(head, elm, field) do { \ + if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm)->field.cqe_prev; \ + else \ + (elm)->field.cqe_next->field.cqe_prev = \ + (elm)->field.cqe_prev; \ + if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm)->field.cqe_next; \ + else \ + (elm)->field.cqe_prev->field.cqe_next = \ + (elm)->field.cqe_next; \ + _Q_INVALIDATE((elm)->field.cqe_prev); \ + _Q_INVALIDATE((elm)->field.cqe_next); \ +} while (0) + +#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \ + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \ + CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm2); \ + else \ + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \ + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \ + CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm2); \ + else \ + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \ + _Q_INVALIDATE((elm)->field.cqe_prev); \ + _Q_INVALIDATE((elm)->field.cqe_next); \ +} while (0) + +#endif /* !_SYS_QUEUE_H_ */