NimBLE-Arduino 2.1.2
Loading...
Searching...
No Matches
queue.h
1/*
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)queue.h 8.5 (Berkeley) 8/20/94
30 * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.7 2002/04/17 14:21:02 des Exp $
31 */
32
33#ifndef _QUEUE_H_
34#define _QUEUE_H_
35
36/* The common BSD linked list queue macros are already defined here for ESP-IDF */
37#include <sys/queue.h>
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/*
44 * This file defines circular queues. The other types of data structures:
45 * singly-linked lists, singly-linked tail queues, lists and tail queues
46 * are used from sys/queue.h
47 *
48 * A singly-linked list is headed by a single forward pointer. The elements
49 * are singly linked for minimum space and pointer manipulation overhead at
50 * the expense of O(n) removal for arbitrary elements. New elements can be
51 * added to the list after an existing element or at the head of the list.
52 * Elements being removed from the head of the list should use the explicit
53 * macro for this purpose for optimum efficiency. A singly-linked list may
54 * only be traversed in the forward direction. Singly-linked lists are ideal
55 * for applications with large datasets and few or no removals or for
56 * implementing a LIFO queue.
57 *
58 * A singly-linked tail queue is headed by a pair of pointers, one to the
59 * head of the list and the other to the tail of the list. The elements are
60 * singly linked for minimum space and pointer manipulation overhead at the
61 * expense of O(n) removal for arbitrary elements. New elements can be added
62 * to the list after an existing element, at the head of the list, or at the
63 * end of the list. Elements being removed from the head of the tail queue
64 * should use the explicit macro for this purpose for optimum efficiency.
65 * A singly-linked tail queue may only be traversed in the forward direction.
66 * Singly-linked tail queues are ideal for applications with large datasets
67 * and few or no removals or for implementing a FIFO queue.
68 *
69 * A list is headed by a single forward pointer (or an array of forward
70 * pointers for a hash table header). The elements are doubly linked
71 * so that an arbitrary element can be removed without a need to
72 * traverse the list. New elements can be added to the list before
73 * or after an existing element or at the head of the list. A list
74 * may only be traversed in the forward direction.
75 *
76 * A tail queue is headed by a pair of pointers, one to the head of the
77 * list and the other to the tail of the list. The elements are doubly
78 * linked so that an arbitrary element can be removed without a need to
79 * traverse the list. New elements can be added to the list before or
80 * after an existing element, at the head of the list, or at the end of
81 * the list. A tail queue may be traversed in either direction.
82 *
83 * A circle queue is headed by a pair of pointers, one to the head of the
84 * list and the other to the tail of the list. The elements are doubly
85 * linked so that an arbitrary element can be removed without a need to
86 * traverse the list. New elements can be added to the list before or after
87 * an existing element, at the head of the list, or at the end of the list.
88 * A circle queue may be traversed in either direction, but has a more
89 * complex end of list detection.
90 *
91 * For details on the use of these macros, see the queue(3) manual page.
92 *
93 *
94 * SLIST LIST STAILQ TAILQ CIRCLEQ
95 * _HEAD + + + + +
96 * _HEAD_INITIALIZER + + + + +
97 * _ENTRY + + + + +
98 * _INIT + + + + +
99 * _EMPTY + + + + +
100 * _FIRST + + + + +
101 * _NEXT + + + + +
102 * _PREV - - - + +
103 * _LAST - - + + +
104 * _FOREACH + + + + +
105 * _FOREACH_REVERSE - - - + +
106 * _INSERT_HEAD + + + + +
107 * _INSERT_BEFORE - + - + +
108 * _INSERT_AFTER + + + + +
109 * _INSERT_TAIL - - + + +
110 * _REMOVE_HEAD + - + - -
111 * _REMOVE + + + + +
112 *
113 */
114
115/*
116 * Circular queue declarations.
117 */
118#define CIRCLEQ_HEAD(name, type) \
119struct name { \
120 struct type *cqh_first; /* first element */ \
121 struct type *cqh_last; /* last element */ \
122}
123
124#define CIRCLEQ_HEAD_INITIALIZER(head) \
125 { (void *)&(head), (void *)&(head) }
126
127#define CIRCLEQ_ENTRY(type) \
128struct { \
129 struct type *cqe_next; /* next element */ \
130 struct type *cqe_prev; /* previous element */ \
131}
132
133/*
134 * Circular queue functions.
135 */
136#define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
137
138#define CIRCLEQ_FIRST(head) ((head)->cqh_first)
139
140#define CIRCLEQ_FOREACH(var, head, field) \
141 for ((var) = CIRCLEQ_FIRST((head)); \
142 (var) != (void *)(head) || ((var) = NULL); \
143 (var) = CIRCLEQ_NEXT((var), field))
144
145#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \
146 for ((var) = CIRCLEQ_LAST((head)); \
147 (var) != (void *)(head) || ((var) = NULL); \
148 (var) = CIRCLEQ_PREV((var), field))
149
150#define CIRCLEQ_INIT(head) do { \
151 CIRCLEQ_FIRST((head)) = (void *)(head); \
152 CIRCLEQ_LAST((head)) = (void *)(head); \
153} while (0)
154
155#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
156 CIRCLEQ_NEXT((elm), field) = CIRCLEQ_NEXT((listelm), field); \
157 CIRCLEQ_PREV((elm), field) = (listelm); \
158 if (CIRCLEQ_NEXT((listelm), field) == (void *)(head)) \
159 CIRCLEQ_LAST((head)) = (elm); \
160 else \
161 CIRCLEQ_PREV(CIRCLEQ_NEXT((listelm), field), field) = (elm);\
162 CIRCLEQ_NEXT((listelm), field) = (elm); \
163} while (0)
164
165#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
166 CIRCLEQ_NEXT((elm), field) = (listelm); \
167 CIRCLEQ_PREV((elm), field) = CIRCLEQ_PREV((listelm), field); \
168 if (CIRCLEQ_PREV((listelm), field) == (void *)(head)) \
169 CIRCLEQ_FIRST((head)) = (elm); \
170 else \
171 CIRCLEQ_NEXT(CIRCLEQ_PREV((listelm), field), field) = (elm);\
172 CIRCLEQ_PREV((listelm), field) = (elm); \
173} while (0)
174
175#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
176 CIRCLEQ_NEXT((elm), field) = CIRCLEQ_FIRST((head)); \
177 CIRCLEQ_PREV((elm), field) = (void *)(head); \
178 if (CIRCLEQ_LAST((head)) == (void *)(head)) \
179 CIRCLEQ_LAST((head)) = (elm); \
180 else \
181 CIRCLEQ_PREV(CIRCLEQ_FIRST((head)), field) = (elm); \
182 CIRCLEQ_FIRST((head)) = (elm); \
183} while (0)
184
185#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
186 CIRCLEQ_NEXT((elm), field) = (void *)(head); \
187 CIRCLEQ_PREV((elm), field) = CIRCLEQ_LAST((head)); \
188 if (CIRCLEQ_FIRST((head)) == (void *)(head)) \
189 CIRCLEQ_FIRST((head)) = (elm); \
190 else \
191 CIRCLEQ_NEXT(CIRCLEQ_LAST((head)), field) = (elm); \
192 CIRCLEQ_LAST((head)) = (elm); \
193} while (0)
194
195#define CIRCLEQ_LAST(head) ((head)->cqh_last)
196
197#define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
198
199#define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
200
201#define CIRCLEQ_REMOVE(head, elm, field) do { \
202 if (CIRCLEQ_NEXT((elm), field) == (void *)(head)) \
203 CIRCLEQ_LAST((head)) = CIRCLEQ_PREV((elm), field); \
204 else \
205 CIRCLEQ_PREV(CIRCLEQ_NEXT((elm), field), field) = \
206 CIRCLEQ_PREV((elm), field); \
207 if (CIRCLEQ_PREV((elm), field) == (void *)(head)) \
208 CIRCLEQ_FIRST((head)) = CIRCLEQ_NEXT((elm), field); \
209 else \
210 CIRCLEQ_NEXT(CIRCLEQ_PREV((elm), field), field) = \
211 CIRCLEQ_NEXT((elm), field); \
212} while (0)
213
214#ifdef __cplusplus
215}
216#endif
217
218#endif /* !_SYS_QUEUE_H_ */