Path: blob/master/libmupen64plus/mupen64plus-video-z64/src/queue.h
2 views
/*1* Copyright (c) 1991, 19932* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*28* @(#)queue.h 8.5 (Berkeley) 8/20/9429*/3031#ifndef _SYS_QUEUE_H_32#define _SYS_QUEUE_H_3334/*35* This file defines five types of data structures: singly-linked lists,36* lists, simple queues, tail queues, and circular queues.37*38* A singly-linked list is headed by a single forward pointer. The39* elements are singly linked for minimum space and pointer manipulation40* overhead at the expense of O(n) removal for arbitrary elements. New41* elements can be added to the list after an existing element or at the42* head of the list. Elements being removed from the head of the list43* should use the explicit macro for this purpose for optimum44* efficiency. A singly-linked list may only be traversed in the forward45* direction. Singly-linked lists are ideal for applications with large46* datasets and few or no removals or for implementing a LIFO queue.47*48* A list is headed by a single forward pointer (or an array of forward49* pointers for a hash table header). The elements are doubly linked50* so that an arbitrary element can be removed without a need to51* traverse the list. New elements can be added to the list before52* or after an existing element or at the head of the list. A list53* may only be traversed in the forward direction.54*55* A simple queue is headed by a pair of pointers, one the head of the56* list and the other to the tail of the list. The elements are singly57* linked to save space, so elements can only be removed from the58* head of the list. New elements can be added to the list after59* an existing element, at the head of the list, or at the end of the60* list. A simple queue may only be traversed in the forward direction.61*62* A tail queue is headed by a pair of pointers, one to the head of the63* list and the other to the tail of the list. The elements are doubly64* linked so that an arbitrary element can be removed without a need to65* traverse the list. New elements can be added to the list before or66* after an existing element, at the head of the list, or at the end of67* the list. A tail queue may be traversed in either direction.68*69* A circle queue is headed by a pair of pointers, one to the head of the70* list and the other to the tail of the list. The elements are doubly71* linked so that an arbitrary element can be removed without a need to72* traverse the list. New elements can be added to the list before or after73* an existing element, at the head of the list, or at the end of the list.74* A circle queue may be traversed in either direction, but has a more75* complex end of list detection.76*77* For details on the use of these macros, see the queue(3) manual page.78*/7980/*81* List definitions.82*/83#define LIST_HEAD(name, type) \84struct name { \85struct type *lh_first; /* first element */ \86}8788#define LIST_HEAD_INITIALIZER(head) \89{ NULL }9091#define LIST_ENTRY(type) \92struct { \93struct type *le_next; /* next element */ \94struct type **le_prev; /* address of previous next element */ \95}9697/*98* List functions.99*/100#define LIST_INIT(head) do { \101(head)->lh_first = NULL; \102} while (/*CONSTCOND*/0)103104#define LIST_INSERT_AFTER(listelm, elm, field) do { \105if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \106(listelm)->field.le_next->field.le_prev = \107&(elm)->field.le_next; \108(listelm)->field.le_next = (elm); \109(elm)->field.le_prev = &(listelm)->field.le_next; \110} while (/*CONSTCOND*/0)111112#define LIST_INSERT_BEFORE(listelm, elm, field) do { \113(elm)->field.le_prev = (listelm)->field.le_prev; \114(elm)->field.le_next = (listelm); \115*(listelm)->field.le_prev = (elm); \116(listelm)->field.le_prev = &(elm)->field.le_next; \117} while (/*CONSTCOND*/0)118119#define LIST_INSERT_HEAD(head, elm, field) do { \120if (((elm)->field.le_next = (head)->lh_first) != NULL) \121(head)->lh_first->field.le_prev = &(elm)->field.le_next;\122(head)->lh_first = (elm); \123(elm)->field.le_prev = &(head)->lh_first; \124} while (/*CONSTCOND*/0)125126#define LIST_REMOVE(elm, field) do { \127if ((elm)->field.le_next != NULL) \128(elm)->field.le_next->field.le_prev = \129(elm)->field.le_prev; \130*(elm)->field.le_prev = (elm)->field.le_next; \131} while (/*CONSTCOND*/0)132133#define LIST_FOREACH(var, head, field) \134for ((var) = ((head)->lh_first); \135(var); \136(var) = ((var)->field.le_next))137138/*139* List access methods.140*/141#define LIST_EMPTY(head) ((head)->lh_first == NULL)142#define LIST_FIRST(head) ((head)->lh_first)143#define LIST_NEXT(elm, field) ((elm)->field.le_next)144145146/*147* Singly-linked List definitions.148*/149#define SLIST_HEAD(name, type) \150struct name { \151struct type *slh_first; /* first element */ \152}153154#define SLIST_HEAD_INITIALIZER(head) \155{ NULL }156157#define SLIST_ENTRY(type) \158struct { \159struct type *sle_next; /* next element */ \160}161162/*163* Singly-linked List functions.164*/165#define SLIST_INIT(head) do { \166(head)->slh_first = NULL; \167} while (/*CONSTCOND*/0)168169#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \170(elm)->field.sle_next = (slistelm)->field.sle_next; \171(slistelm)->field.sle_next = (elm); \172} while (/*CONSTCOND*/0)173174#define SLIST_INSERT_HEAD(head, elm, field) do { \175(elm)->field.sle_next = (head)->slh_first; \176(head)->slh_first = (elm); \177} while (/*CONSTCOND*/0)178179#define SLIST_REMOVE_HEAD(head, field) do { \180(head)->slh_first = (head)->slh_first->field.sle_next; \181} while (/*CONSTCOND*/0)182183#define SLIST_REMOVE(head, elm, type, field) do { \184if ((head)->slh_first == (elm)) { \185SLIST_REMOVE_HEAD((head), field); \186} \187else { \188struct type *curelm = (head)->slh_first; \189while(curelm->field.sle_next != (elm)) \190curelm = curelm->field.sle_next; \191curelm->field.sle_next = \192curelm->field.sle_next->field.sle_next; \193} \194} while (/*CONSTCOND*/0)195196#define SLIST_FOREACH(var, head, field) \197for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)198199/*200* Singly-linked List access methods.201*/202#define SLIST_EMPTY(head) ((head)->slh_first == NULL)203#define SLIST_FIRST(head) ((head)->slh_first)204#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)205206207/*208* Singly-linked Tail queue declarations.209*/210#define STAILQ_HEAD(name, type) \211struct name { \212struct type *stqh_first; /* first element */ \213struct type **stqh_last; /* addr of last next element */ \214}215216#define STAILQ_HEAD_INITIALIZER(head) \217{ NULL, &(head).stqh_first }218219#define STAILQ_ENTRY(type) \220struct { \221struct type *stqe_next; /* next element */ \222}223224/*225* Singly-linked Tail queue functions.226*/227#define STAILQ_INIT(head) do { \228(head)->stqh_first = NULL; \229(head)->stqh_last = &(head)->stqh_first; \230} while (/*CONSTCOND*/0)231232#define STAILQ_INSERT_HEAD(head, elm, field) do { \233if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \234(head)->stqh_last = &(elm)->field.stqe_next; \235(head)->stqh_first = (elm); \236} while (/*CONSTCOND*/0)237238#define STAILQ_INSERT_TAIL(head, elm, field) do { \239(elm)->field.stqe_next = NULL; \240*(head)->stqh_last = (elm); \241(head)->stqh_last = &(elm)->field.stqe_next; \242} while (/*CONSTCOND*/0)243244#define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \245if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\246(head)->stqh_last = &(elm)->field.stqe_next; \247(listelm)->field.stqe_next = (elm); \248} while (/*CONSTCOND*/0)249250#define STAILQ_REMOVE_HEAD(head, field) do { \251if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \252(head)->stqh_last = &(head)->stqh_first; \253} while (/*CONSTCOND*/0)254255#define STAILQ_REMOVE(head, elm, type, field) do { \256if ((head)->stqh_first == (elm)) { \257STAILQ_REMOVE_HEAD((head), field); \258} else { \259struct type *curelm = (head)->stqh_first; \260while (curelm->field.stqe_next != (elm)) \261curelm = curelm->field.stqe_next; \262if ((curelm->field.stqe_next = \263curelm->field.stqe_next->field.stqe_next) == NULL) \264(head)->stqh_last = &(curelm)->field.stqe_next; \265} \266} while (/*CONSTCOND*/0)267268#define STAILQ_FOREACH(var, head, field) \269for ((var) = ((head)->stqh_first); \270(var); \271(var) = ((var)->field.stqe_next))272273/*274* Singly-linked Tail queue access methods.275*/276#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)277#define STAILQ_FIRST(head) ((head)->stqh_first)278#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)279280281/*282* Simple queue definitions.283*/284#define SIMPLEQ_HEAD(name, type) \285struct name { \286struct type *sqh_first; /* first element */ \287struct type **sqh_last; /* addr of last next element */ \288}289290#define SIMPLEQ_HEAD_INITIALIZER(head) \291{ NULL, &(head).sqh_first }292293#define SIMPLEQ_ENTRY(type) \294struct { \295struct type *sqe_next; /* next element */ \296}297298/*299* Simple queue functions.300*/301#define SIMPLEQ_INIT(head) do { \302(head)->sqh_first = NULL; \303(head)->sqh_last = &(head)->sqh_first; \304} while (/*CONSTCOND*/0)305306#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \307if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \308(head)->sqh_last = &(elm)->field.sqe_next; \309(head)->sqh_first = (elm); \310} while (/*CONSTCOND*/0)311312#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \313(elm)->field.sqe_next = NULL; \314*(head)->sqh_last = (elm); \315(head)->sqh_last = &(elm)->field.sqe_next; \316} while (/*CONSTCOND*/0)317318#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \319if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\320(head)->sqh_last = &(elm)->field.sqe_next; \321(listelm)->field.sqe_next = (elm); \322} while (/*CONSTCOND*/0)323324#define SIMPLEQ_REMOVE_HEAD(head, field) do { \325if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \326(head)->sqh_last = &(head)->sqh_first; \327} while (/*CONSTCOND*/0)328329#define SIMPLEQ_REMOVE(head, elm, type, field) do { \330if ((head)->sqh_first == (elm)) { \331SIMPLEQ_REMOVE_HEAD((head), field); \332} else { \333struct type *curelm = (head)->sqh_first; \334while (curelm->field.sqe_next != (elm)) \335curelm = curelm->field.sqe_next; \336if ((curelm->field.sqe_next = \337curelm->field.sqe_next->field.sqe_next) == NULL) \338(head)->sqh_last = &(curelm)->field.sqe_next; \339} \340} while (/*CONSTCOND*/0)341342#define SIMPLEQ_FOREACH(var, head, field) \343for ((var) = ((head)->sqh_first); \344(var); \345(var) = ((var)->field.sqe_next))346347/*348* Simple queue access methods.349*/350#define SIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)351#define SIMPLEQ_FIRST(head) ((head)->sqh_first)352#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)353354355/*356* Tail queue definitions.357*/358#define _TAILQ_HEAD(name, type, qual) \359struct name { \360qual type *tqh_first; /* first element */ \361qual type *qual *tqh_last; /* addr of last next element */ \362}363#define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type,)364365#define TAILQ_HEAD_INITIALIZER(head) \366{ NULL, &(head).tqh_first }367368#define _TAILQ_ENTRY(type, qual) \369struct { \370qual type *tqe_next; /* next element */ \371qual type *qual *tqe_prev; /* address of previous next element */\372}373#define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type,)374375/*376* Tail queue functions.377*/378#define TAILQ_INIT(head) do { \379(head)->tqh_first = NULL; \380(head)->tqh_last = &(head)->tqh_first; \381} while (/*CONSTCOND*/0)382383#define TAILQ_INSERT_HEAD(head, elm, field) do { \384if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \385(head)->tqh_first->field.tqe_prev = \386&(elm)->field.tqe_next; \387else \388(head)->tqh_last = &(elm)->field.tqe_next; \389(head)->tqh_first = (elm); \390(elm)->field.tqe_prev = &(head)->tqh_first; \391} while (/*CONSTCOND*/0)392393#define TAILQ_INSERT_TAIL(head, elm, field) do { \394(elm)->field.tqe_next = NULL; \395(elm)->field.tqe_prev = (head)->tqh_last; \396*(head)->tqh_last = (elm); \397(head)->tqh_last = &(elm)->field.tqe_next; \398} while (/*CONSTCOND*/0)399400#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \401if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\402(elm)->field.tqe_next->field.tqe_prev = \403&(elm)->field.tqe_next; \404else \405(head)->tqh_last = &(elm)->field.tqe_next; \406(listelm)->field.tqe_next = (elm); \407(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \408} while (/*CONSTCOND*/0)409410#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \411(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \412(elm)->field.tqe_next = (listelm); \413*(listelm)->field.tqe_prev = (elm); \414(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \415} while (/*CONSTCOND*/0)416417#define TAILQ_REMOVE(head, elm, field) do { \418if (((elm)->field.tqe_next) != NULL) \419(elm)->field.tqe_next->field.tqe_prev = \420(elm)->field.tqe_prev; \421else \422(head)->tqh_last = (elm)->field.tqe_prev; \423*(elm)->field.tqe_prev = (elm)->field.tqe_next; \424} while (/*CONSTCOND*/0)425426#define TAILQ_FOREACH(var, head, field) \427for ((var) = ((head)->tqh_first); \428(var); \429(var) = ((var)->field.tqe_next))430431#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \432for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \433(var); \434(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))435436/*437* Tail queue access methods.438*/439#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)440#define TAILQ_FIRST(head) ((head)->tqh_first)441#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)442443#define TAILQ_LAST(head, headname) \444(*(((struct headname *)((head)->tqh_last))->tqh_last))445#define TAILQ_PREV(elm, headname, field) \446(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))447448449/*450* Circular queue definitions.451*/452#define CIRCLEQ_HEAD(name, type) \453struct name { \454struct type *cqh_first; /* first element */ \455struct type *cqh_last; /* last element */ \456}457458#define CIRCLEQ_HEAD_INITIALIZER(type, head) \459{ (type *)&head, (type *)&head }460461#define CIRCLEQ_ENTRY(type) \462struct { \463struct type *cqe_next; /* next element */ \464struct type *cqe_prev; /* previous element */ \465}466467/*468* Circular queue functions.469*/470#define CIRCLEQ_INIT(type, head) do { \471(head)->cqh_first = (type *)(head); \472(head)->cqh_last = (type *)(head); \473} while (/*CONSTCOND*/0)474475#define CIRCLEQ_INSERT_AFTER(type, head, listelm, elm, field) do { \476(elm)->field.cqe_next = (listelm)->field.cqe_next; \477(elm)->field.cqe_prev = (listelm); \478if ((listelm)->field.cqe_next == (type *)(head)) \479(head)->cqh_last = (elm); \480else \481(listelm)->field.cqe_next->field.cqe_prev = (elm); \482(listelm)->field.cqe_next = (elm); \483} while (/*CONSTCOND*/0)484485#define CIRCLEQ_INSERT_BEFORE(type, head, listelm, elm, field) do { \486(elm)->field.cqe_next = (listelm); \487(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \488if ((listelm)->field.cqe_prev == (type *)(head)) \489(head)->cqh_first = (elm); \490else \491(listelm)->field.cqe_prev->field.cqe_next = (elm); \492(listelm)->field.cqe_prev = (elm); \493} while (/*CONSTCOND*/0)494495#define CIRCLEQ_INSERT_HEAD(type, head, elm, field) do { \496(elm)->field.cqe_next = (head)->cqh_first; \497(elm)->field.cqe_prev = (type *)(head); \498if ((head)->cqh_last == (type *)(head)) \499(head)->cqh_last = (elm); \500else \501(head)->cqh_first->field.cqe_prev = (elm); \502(head)->cqh_first = (elm); \503} while (/*CONSTCOND*/0)504505#define CIRCLEQ_INSERT_TAIL(type, head, elm, field) do { \506(elm)->field.cqe_next = (type *)(head); \507(elm)->field.cqe_prev = (head)->cqh_last; \508if ((head)->cqh_first == (type *)(head)) \509(head)->cqh_first = (elm); \510else \511(head)->cqh_last->field.cqe_next = (elm); \512(head)->cqh_last = (elm); \513} while (/*CONSTCOND*/0)514515#define CIRCLEQ_REMOVE(head, elm, field) do { \516if ((elm)->field.cqe_next == (void *)(head)) \517(head)->cqh_last = (elm)->field.cqe_prev; \518else \519(elm)->field.cqe_next->field.cqe_prev = \520(elm)->field.cqe_prev; \521if ((elm)->field.cqe_prev == (void *)(head)) \522(head)->cqh_first = (elm)->field.cqe_next; \523else \524(elm)->field.cqe_prev->field.cqe_next = \525(elm)->field.cqe_next; \526} while (/*CONSTCOND*/0)527528#define CIRCLEQ_FOREACH(type, var, head, field) \529for ((var) = ((head)->cqh_first); \530(var) != (const type *)(head); \531(var) = ((var)->field.cqe_next))532533#define CIRCLEQ_FOREACH_REVERSE(type, var, head, field) \534for ((var) = ((head)->cqh_last); \535(var) != (const type *)(head); \536(var) = ((var)->field.cqe_prev))537538/*539* Circular queue access methods.540*/541#define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))542#define CIRCLEQ_FIRST(head) ((head)->cqh_first)543#define CIRCLEQ_LAST(head) ((head)->cqh_last)544#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)545#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)546547#define CIRCLEQ_LOOP_NEXT(head, elm, field) \548(((elm)->field.cqe_next == (void *)(head)) \549? ((head)->cqh_first) \550: (elm->field.cqe_next))551#define CIRCLEQ_LOOP_PREV(head, elm, field) \552(((elm)->field.cqe_prev == (void *)(head)) \553? ((head)->cqh_last) \554: (elm->field.cqe_prev))555556#endif /* sys/queue.h */557558559