Path: blob/main/core/coreutils/src/compat/queue.h
1398 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#define STAILQ_CONCAT(head1, head2) do { \274if (!STAILQ_EMPTY((head2))) { \275*(head1)->stqh_last = (head2)->stqh_first; \276(head1)->stqh_last = (head2)->stqh_last; \277STAILQ_INIT((head2)); \278} \279} while (/*CONSTCOND*/0)280281/*282* Singly-linked Tail queue access methods.283*/284#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)285#define STAILQ_FIRST(head) ((head)->stqh_first)286#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)287288289/*290* Simple queue definitions.291*/292#define SIMPLEQ_HEAD(name, type) \293struct name { \294struct type *sqh_first; /* first element */ \295struct type **sqh_last; /* addr of last next element */ \296}297298#define SIMPLEQ_HEAD_INITIALIZER(head) \299{ NULL, &(head).sqh_first }300301#define SIMPLEQ_ENTRY(type) \302struct { \303struct type *sqe_next; /* next element */ \304}305306/*307* Simple queue functions.308*/309#define SIMPLEQ_INIT(head) do { \310(head)->sqh_first = NULL; \311(head)->sqh_last = &(head)->sqh_first; \312} while (/*CONSTCOND*/0)313314#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \315if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \316(head)->sqh_last = &(elm)->field.sqe_next; \317(head)->sqh_first = (elm); \318} while (/*CONSTCOND*/0)319320#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \321(elm)->field.sqe_next = NULL; \322*(head)->sqh_last = (elm); \323(head)->sqh_last = &(elm)->field.sqe_next; \324} while (/*CONSTCOND*/0)325326#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \327if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\328(head)->sqh_last = &(elm)->field.sqe_next; \329(listelm)->field.sqe_next = (elm); \330} while (/*CONSTCOND*/0)331332#define SIMPLEQ_REMOVE_HEAD(head, field) do { \333if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \334(head)->sqh_last = &(head)->sqh_first; \335} while (/*CONSTCOND*/0)336337#define SIMPLEQ_REMOVE(head, elm, type, field) do { \338if ((head)->sqh_first == (elm)) { \339SIMPLEQ_REMOVE_HEAD((head), field); \340} else { \341struct type *curelm = (head)->sqh_first; \342while (curelm->field.sqe_next != (elm)) \343curelm = curelm->field.sqe_next; \344if ((curelm->field.sqe_next = \345curelm->field.sqe_next->field.sqe_next) == NULL) \346(head)->sqh_last = &(curelm)->field.sqe_next; \347} \348} while (/*CONSTCOND*/0)349350#define SIMPLEQ_FOREACH(var, head, field) \351for ((var) = ((head)->sqh_first); \352(var); \353(var) = ((var)->field.sqe_next))354355/*356* Simple queue access methods.357*/358#define SIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL)359#define SIMPLEQ_FIRST(head) ((head)->sqh_first)360#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)361362363/*364* Tail queue definitions.365*/366#define _TAILQ_HEAD(name, type, qual) \367struct name { \368qual type *tqh_first; /* first element */ \369qual type *qual *tqh_last; /* addr of last next element */ \370}371#define TAILQ_HEAD(name, type) _TAILQ_HEAD(name, struct type,)372373#define TAILQ_HEAD_INITIALIZER(head) \374{ NULL, &(head).tqh_first }375376#define _TAILQ_ENTRY(type, qual) \377struct { \378qual type *tqe_next; /* next element */ \379qual type *qual *tqe_prev; /* address of previous next element */\380}381#define TAILQ_ENTRY(type) _TAILQ_ENTRY(struct type,)382383/*384* Tail queue functions.385*/386#define TAILQ_INIT(head) do { \387(head)->tqh_first = NULL; \388(head)->tqh_last = &(head)->tqh_first; \389} while (/*CONSTCOND*/0)390391#define TAILQ_INSERT_HEAD(head, elm, field) do { \392if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \393(head)->tqh_first->field.tqe_prev = \394&(elm)->field.tqe_next; \395else \396(head)->tqh_last = &(elm)->field.tqe_next; \397(head)->tqh_first = (elm); \398(elm)->field.tqe_prev = &(head)->tqh_first; \399} while (/*CONSTCOND*/0)400401#define TAILQ_INSERT_TAIL(head, elm, field) do { \402(elm)->field.tqe_next = NULL; \403(elm)->field.tqe_prev = (head)->tqh_last; \404*(head)->tqh_last = (elm); \405(head)->tqh_last = &(elm)->field.tqe_next; \406} while (/*CONSTCOND*/0)407408#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \409if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\410(elm)->field.tqe_next->field.tqe_prev = \411&(elm)->field.tqe_next; \412else \413(head)->tqh_last = &(elm)->field.tqe_next; \414(listelm)->field.tqe_next = (elm); \415(elm)->field.tqe_prev = &(listelm)->field.tqe_next; \416} while (/*CONSTCOND*/0)417418#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \419(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \420(elm)->field.tqe_next = (listelm); \421*(listelm)->field.tqe_prev = (elm); \422(listelm)->field.tqe_prev = &(elm)->field.tqe_next; \423} while (/*CONSTCOND*/0)424425#define TAILQ_REMOVE(head, elm, field) do { \426if (((elm)->field.tqe_next) != NULL) \427(elm)->field.tqe_next->field.tqe_prev = \428(elm)->field.tqe_prev; \429else \430(head)->tqh_last = (elm)->field.tqe_prev; \431*(elm)->field.tqe_prev = (elm)->field.tqe_next; \432} while (/*CONSTCOND*/0)433434#define TAILQ_FOREACH(var, head, field) \435for ((var) = ((head)->tqh_first); \436(var); \437(var) = ((var)->field.tqe_next))438439#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \440for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \441(var); \442(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))443444#define TAILQ_CONCAT(head1, head2, field) do { \445if (!TAILQ_EMPTY(head2)) { \446*(head1)->tqh_last = (head2)->tqh_first; \447(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \448(head1)->tqh_last = (head2)->tqh_last; \449TAILQ_INIT((head2)); \450} \451} while (/*CONSTCOND*/0)452453/*454* Tail queue access methods.455*/456#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)457#define TAILQ_FIRST(head) ((head)->tqh_first)458#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)459460#define TAILQ_LAST(head, headname) \461(*(((struct headname *)((head)->tqh_last))->tqh_last))462#define TAILQ_PREV(elm, headname, field) \463(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))464465466/*467* Circular queue definitions.468*/469#define CIRCLEQ_HEAD(name, type) \470struct name { \471struct type *cqh_first; /* first element */ \472struct type *cqh_last; /* last element */ \473}474475#define CIRCLEQ_HEAD_INITIALIZER(head) \476{ (void *)&head, (void *)&head }477478#define CIRCLEQ_ENTRY(type) \479struct { \480struct type *cqe_next; /* next element */ \481struct type *cqe_prev; /* previous element */ \482}483484/*485* Circular queue functions.486*/487#define CIRCLEQ_INIT(head) do { \488(head)->cqh_first = (void *)(head); \489(head)->cqh_last = (void *)(head); \490} while (/*CONSTCOND*/0)491492#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \493(elm)->field.cqe_next = (listelm)->field.cqe_next; \494(elm)->field.cqe_prev = (listelm); \495if ((listelm)->field.cqe_next == (void *)(head)) \496(head)->cqh_last = (elm); \497else \498(listelm)->field.cqe_next->field.cqe_prev = (elm); \499(listelm)->field.cqe_next = (elm); \500} while (/*CONSTCOND*/0)501502#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \503(elm)->field.cqe_next = (listelm); \504(elm)->field.cqe_prev = (listelm)->field.cqe_prev; \505if ((listelm)->field.cqe_prev == (void *)(head)) \506(head)->cqh_first = (elm); \507else \508(listelm)->field.cqe_prev->field.cqe_next = (elm); \509(listelm)->field.cqe_prev = (elm); \510} while (/*CONSTCOND*/0)511512#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \513(elm)->field.cqe_next = (head)->cqh_first; \514(elm)->field.cqe_prev = (void *)(head); \515if ((head)->cqh_last == (void *)(head)) \516(head)->cqh_last = (elm); \517else \518(head)->cqh_first->field.cqe_prev = (elm); \519(head)->cqh_first = (elm); \520} while (/*CONSTCOND*/0)521522#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \523(elm)->field.cqe_next = (void *)(head); \524(elm)->field.cqe_prev = (head)->cqh_last; \525if ((head)->cqh_first == (void *)(head)) \526(head)->cqh_first = (elm); \527else \528(head)->cqh_last->field.cqe_next = (elm); \529(head)->cqh_last = (elm); \530} while (/*CONSTCOND*/0)531532#define CIRCLEQ_REMOVE(head, elm, field) do { \533if ((elm)->field.cqe_next == (void *)(head)) \534(head)->cqh_last = (elm)->field.cqe_prev; \535else \536(elm)->field.cqe_next->field.cqe_prev = \537(elm)->field.cqe_prev; \538if ((elm)->field.cqe_prev == (void *)(head)) \539(head)->cqh_first = (elm)->field.cqe_next; \540else \541(elm)->field.cqe_prev->field.cqe_next = \542(elm)->field.cqe_next; \543} while (/*CONSTCOND*/0)544545#define CIRCLEQ_FOREACH(var, head, field) \546for ((var) = ((head)->cqh_first); \547(var) != (const void *)(head); \548(var) = ((var)->field.cqe_next))549550#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \551for ((var) = ((head)->cqh_last); \552(var) != (const void *)(head); \553(var) = ((var)->field.cqe_prev))554555/*556* Circular queue access methods.557*/558#define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))559#define CIRCLEQ_FIRST(head) ((head)->cqh_first)560#define CIRCLEQ_LAST(head) ((head)->cqh_last)561#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)562#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)563564#define CIRCLEQ_LOOP_NEXT(head, elm, field) \565(((elm)->field.cqe_next == (void *)(head)) \566? ((head)->cqh_first) \567: (elm->field.cqe_next))568#define CIRCLEQ_LOOP_PREV(head, elm, field) \569(((elm)->field.cqe_prev == (void *)(head)) \570? ((head)->cqh_last) \571: (elm->field.cqe_prev))572573#endif /* sys/queue.h */574575