aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorkinichiro <kinichiro.inoguchi@gmail.com>2020-09-13 12:58:37 +0900
committerkinichiro <kinichiro.inoguchi@gmail.com>2020-09-13 21:56:34 +0900
commit461ec45335747276503fd6cb863d51782127919e (patch)
treea1041b64af9a0b5c2d0fbe2bac92f750dbf1aa4a /include
parent65b01cff7a843d1d363c5dffcb57da110ce614a3 (diff)
downloadportable-461ec45335747276503fd6cb863d51782127919e.tar.gz
portable-461ec45335747276503fd6cb863d51782127919e.tar.bz2
portable-461ec45335747276503fd6cb863d51782127919e.zip
Add queue.h tree.h _null.h
Import queue.h tree.h _null.h header files from OpenBSD upstream since x509_issuer_cache.c requires them.
Diffstat (limited to 'include')
-rw-r--r--include/Makefile.am3
-rw-r--r--include/compat/sys/_null.h18
-rw-r--r--include/compat/sys/queue.h536
-rw-r--r--include/compat/sys/tree.h1006
4 files changed, 1563 insertions, 0 deletions
diff --git a/include/Makefile.am b/include/Makefile.am
index 6d808cc..4184cf8 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -32,12 +32,15 @@ noinst_HEADERS += compat/netinet/in.h
32noinst_HEADERS += compat/netinet/ip.h 32noinst_HEADERS += compat/netinet/ip.h
33noinst_HEADERS += compat/netinet/tcp.h 33noinst_HEADERS += compat/netinet/tcp.h
34 34
35noinst_HEADERS += compat/sys/_null.h
35noinst_HEADERS += compat/sys/ioctl.h 36noinst_HEADERS += compat/sys/ioctl.h
36noinst_HEADERS += compat/sys/mman.h 37noinst_HEADERS += compat/sys/mman.h
37noinst_HEADERS += compat/sys/param.h 38noinst_HEADERS += compat/sys/param.h
39noinst_HEADERS += compat/sys/queue.h
38noinst_HEADERS += compat/sys/select.h 40noinst_HEADERS += compat/sys/select.h
39noinst_HEADERS += compat/sys/socket.h 41noinst_HEADERS += compat/sys/socket.h
40noinst_HEADERS += compat/sys/stat.h 42noinst_HEADERS += compat/sys/stat.h
43noinst_HEADERS += compat/sys/tree.h
41noinst_HEADERS += compat/sys/time.h 44noinst_HEADERS += compat/sys/time.h
42noinst_HEADERS += compat/sys/types.h 45noinst_HEADERS += compat/sys/types.h
43noinst_HEADERS += compat/sys/uio.h 46noinst_HEADERS += compat/sys/uio.h
diff --git a/include/compat/sys/_null.h b/include/compat/sys/_null.h
new file mode 100644
index 0000000..5d15401
--- /dev/null
+++ b/include/compat/sys/_null.h
@@ -0,0 +1,18 @@
1/* $OpenBSD: _null.h,v 1.2 2016/09/09 22:07:58 millert Exp $ */
2
3/*
4 * Written by Todd C. Miller, September 9, 2016
5 * Public domain.
6 */
7
8#ifndef NULL
9#if !defined(__cplusplus)
10#define NULL ((void *)0)
11#elif __cplusplus >= 201103L
12#define NULL nullptr
13#elif defined(__GNUG__)
14#define NULL __null
15#else
16#define NULL 0L
17#endif
18#endif
diff --git a/include/compat/sys/queue.h b/include/compat/sys/queue.h
new file mode 100644
index 0000000..f28ba89
--- /dev/null
+++ b/include/compat/sys/queue.h
@@ -0,0 +1,536 @@
1/* $OpenBSD: queue.h,v 1.45 2018/07/12 14:22:54 sashan Exp $ */
2/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
3
4/*
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)queue.h 8.5 (Berkeley) 8/20/94
33 */
34
35#ifndef _SYS_QUEUE_H_
36#define _SYS_QUEUE_H_
37
38#include <sys/_null.h>
39
40/*
41 * This file defines five types of data structures: singly-linked lists,
42 * lists, simple queues, tail queues and XOR simple queues.
43 *
44 *
45 * A singly-linked list is headed by a single forward pointer. The elements
46 * are singly linked for minimum space and pointer manipulation overhead at
47 * the expense of O(n) removal for arbitrary elements. New elements can be
48 * added to the list after an existing element or at the head of the list.
49 * Elements being removed from the head of the list should use the explicit
50 * macro for this purpose for optimum efficiency. A singly-linked list may
51 * only be traversed in the forward direction. Singly-linked lists are ideal
52 * for applications with large datasets and few or no removals or for
53 * implementing a LIFO queue.
54 *
55 * A list is headed by a single forward pointer (or an array of forward
56 * pointers for a hash table header). The elements are doubly linked
57 * so that an arbitrary element can be removed without a need to
58 * traverse the list. New elements can be added to the list before
59 * or after an existing element or at the head of the list. A list
60 * may only be traversed in the forward direction.
61 *
62 * A simple queue is headed by a pair of pointers, one to the head of the
63 * list and the other to the tail of the list. The elements are singly
64 * linked to save space, so elements can only be removed from the
65 * head of the list. New elements can be added to the list before or after
66 * an existing element, at the head of the list, or at the end of the
67 * list. A simple queue may only be traversed in the forward direction.
68 *
69 * A tail queue is headed by a pair of pointers, one to the head of the
70 * list and the other to the tail of the list. The elements are doubly
71 * linked 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 or
73 * after an existing element, at the head of the list, or at the end of
74 * the list. A tail queue may be traversed in either direction.
75 *
76 * An XOR simple queue is used in the same way as a regular simple queue.
77 * The difference is that the head structure also includes a "cookie" that
78 * is XOR'd with the queue pointer (first, last or next) to generate the
79 * real pointer value.
80 *
81 * For details on the use of these macros, see the queue(3) manual page.
82 */
83
84#if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
85#define _Q_INVALID ((void *)-1)
86#define _Q_INVALIDATE(a) (a) = _Q_INVALID
87#else
88#define _Q_INVALIDATE(a)
89#endif
90
91/*
92 * Singly-linked List definitions.
93 */
94#define SLIST_HEAD(name, type) \
95struct name { \
96 struct type *slh_first; /* first element */ \
97}
98
99#define SLIST_HEAD_INITIALIZER(head) \
100 { NULL }
101
102#define SLIST_ENTRY(type) \
103struct { \
104 struct type *sle_next; /* next element */ \
105}
106
107/*
108 * Singly-linked List access methods.
109 */
110#define SLIST_FIRST(head) ((head)->slh_first)
111#define SLIST_END(head) NULL
112#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
113#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
114
115#define SLIST_FOREACH(var, head, field) \
116 for((var) = SLIST_FIRST(head); \
117 (var) != SLIST_END(head); \
118 (var) = SLIST_NEXT(var, field))
119
120#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
121 for ((var) = SLIST_FIRST(head); \
122 (var) && ((tvar) = SLIST_NEXT(var, field), 1); \
123 (var) = (tvar))
124
125/*
126 * Singly-linked List functions.
127 */
128#define SLIST_INIT(head) { \
129 SLIST_FIRST(head) = SLIST_END(head); \
130}
131
132#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
133 (elm)->field.sle_next = (slistelm)->field.sle_next; \
134 (slistelm)->field.sle_next = (elm); \
135} while (0)
136
137#define SLIST_INSERT_HEAD(head, elm, field) do { \
138 (elm)->field.sle_next = (head)->slh_first; \
139 (head)->slh_first = (elm); \
140} while (0)
141
142#define SLIST_REMOVE_AFTER(elm, field) do { \
143 (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
144} while (0)
145
146#define SLIST_REMOVE_HEAD(head, field) do { \
147 (head)->slh_first = (head)->slh_first->field.sle_next; \
148} while (0)
149
150#define SLIST_REMOVE(head, elm, type, field) do { \
151 if ((head)->slh_first == (elm)) { \
152 SLIST_REMOVE_HEAD((head), field); \
153 } else { \
154 struct type *curelm = (head)->slh_first; \
155 \
156 while (curelm->field.sle_next != (elm)) \
157 curelm = curelm->field.sle_next; \
158 curelm->field.sle_next = \
159 curelm->field.sle_next->field.sle_next; \
160 } \
161 _Q_INVALIDATE((elm)->field.sle_next); \
162} while (0)
163
164/*
165 * List definitions.
166 */
167#define LIST_HEAD(name, type) \
168struct name { \
169 struct type *lh_first; /* first element */ \
170}
171
172#define LIST_HEAD_INITIALIZER(head) \
173 { NULL }
174
175#define LIST_ENTRY(type) \
176struct { \
177 struct type *le_next; /* next element */ \
178 struct type **le_prev; /* address of previous next element */ \
179}
180
181/*
182 * List access methods.
183 */
184#define LIST_FIRST(head) ((head)->lh_first)
185#define LIST_END(head) NULL
186#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
187#define LIST_NEXT(elm, field) ((elm)->field.le_next)
188
189#define LIST_FOREACH(var, head, field) \
190 for((var) = LIST_FIRST(head); \
191 (var)!= LIST_END(head); \
192 (var) = LIST_NEXT(var, field))
193
194#define LIST_FOREACH_SAFE(var, head, field, tvar) \
195 for ((var) = LIST_FIRST(head); \
196 (var) && ((tvar) = LIST_NEXT(var, field), 1); \
197 (var) = (tvar))
198
199/*
200 * List functions.
201 */
202#define LIST_INIT(head) do { \
203 LIST_FIRST(head) = LIST_END(head); \
204} while (0)
205
206#define LIST_INSERT_AFTER(listelm, elm, field) do { \
207 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
208 (listelm)->field.le_next->field.le_prev = \
209 &(elm)->field.le_next; \
210 (listelm)->field.le_next = (elm); \
211 (elm)->field.le_prev = &(listelm)->field.le_next; \
212} while (0)
213
214#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
215 (elm)->field.le_prev = (listelm)->field.le_prev; \
216 (elm)->field.le_next = (listelm); \
217 *(listelm)->field.le_prev = (elm); \
218 (listelm)->field.le_prev = &(elm)->field.le_next; \
219} while (0)
220
221#define LIST_INSERT_HEAD(head, elm, field) do { \
222 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
223 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
224 (head)->lh_first = (elm); \
225 (elm)->field.le_prev = &(head)->lh_first; \
226} while (0)
227
228#define LIST_REMOVE(elm, field) do { \
229 if ((elm)->field.le_next != NULL) \
230 (elm)->field.le_next->field.le_prev = \
231 (elm)->field.le_prev; \
232 *(elm)->field.le_prev = (elm)->field.le_next; \
233 _Q_INVALIDATE((elm)->field.le_prev); \
234 _Q_INVALIDATE((elm)->field.le_next); \
235} while (0)
236
237#define LIST_REPLACE(elm, elm2, field) do { \
238 if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
239 (elm2)->field.le_next->field.le_prev = \
240 &(elm2)->field.le_next; \
241 (elm2)->field.le_prev = (elm)->field.le_prev; \
242 *(elm2)->field.le_prev = (elm2); \
243 _Q_INVALIDATE((elm)->field.le_prev); \
244 _Q_INVALIDATE((elm)->field.le_next); \
245} while (0)
246
247/*
248 * Simple queue definitions.
249 */
250#define SIMPLEQ_HEAD(name, type) \
251struct name { \
252 struct type *sqh_first; /* first element */ \
253 struct type **sqh_last; /* addr of last next element */ \
254}
255
256#define SIMPLEQ_HEAD_INITIALIZER(head) \
257 { NULL, &(head).sqh_first }
258
259#define SIMPLEQ_ENTRY(type) \
260struct { \
261 struct type *sqe_next; /* next element */ \
262}
263
264/*
265 * Simple queue access methods.
266 */
267#define SIMPLEQ_FIRST(head) ((head)->sqh_first)
268#define SIMPLEQ_END(head) NULL
269#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
270#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
271
272#define SIMPLEQ_FOREACH(var, head, field) \
273 for((var) = SIMPLEQ_FIRST(head); \
274 (var) != SIMPLEQ_END(head); \
275 (var) = SIMPLEQ_NEXT(var, field))
276
277#define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
278 for ((var) = SIMPLEQ_FIRST(head); \
279 (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
280 (var) = (tvar))
281
282/*
283 * Simple queue functions.
284 */
285#define SIMPLEQ_INIT(head) do { \
286 (head)->sqh_first = NULL; \
287 (head)->sqh_last = &(head)->sqh_first; \
288} while (0)
289
290#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
291 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
292 (head)->sqh_last = &(elm)->field.sqe_next; \
293 (head)->sqh_first = (elm); \
294} while (0)
295
296#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
297 (elm)->field.sqe_next = NULL; \
298 *(head)->sqh_last = (elm); \
299 (head)->sqh_last = &(elm)->field.sqe_next; \
300} while (0)
301
302#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
303 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
304 (head)->sqh_last = &(elm)->field.sqe_next; \
305 (listelm)->field.sqe_next = (elm); \
306} while (0)
307
308#define SIMPLEQ_REMOVE_HEAD(head, field) do { \
309 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
310 (head)->sqh_last = &(head)->sqh_first; \
311} while (0)
312
313#define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
314 if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
315 == NULL) \
316 (head)->sqh_last = &(elm)->field.sqe_next; \
317} while (0)
318
319#define SIMPLEQ_CONCAT(head1, head2) do { \
320 if (!SIMPLEQ_EMPTY((head2))) { \
321 *(head1)->sqh_last = (head2)->sqh_first; \
322 (head1)->sqh_last = (head2)->sqh_last; \
323 SIMPLEQ_INIT((head2)); \
324 } \
325} while (0)
326
327/*
328 * XOR Simple queue definitions.
329 */
330#define XSIMPLEQ_HEAD(name, type) \
331struct name { \
332 struct type *sqx_first; /* first element */ \
333 struct type **sqx_last; /* addr of last next element */ \
334 unsigned long sqx_cookie; \
335}
336
337#define XSIMPLEQ_ENTRY(type) \
338struct { \
339 struct type *sqx_next; /* next element */ \
340}
341
342/*
343 * XOR Simple queue access methods.
344 */
345#define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
346 (unsigned long)(ptr)))
347#define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
348#define XSIMPLEQ_END(head) NULL
349#define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
350#define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
351
352
353#define XSIMPLEQ_FOREACH(var, head, field) \
354 for ((var) = XSIMPLEQ_FIRST(head); \
355 (var) != XSIMPLEQ_END(head); \
356 (var) = XSIMPLEQ_NEXT(head, var, field))
357
358#define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
359 for ((var) = XSIMPLEQ_FIRST(head); \
360 (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
361 (var) = (tvar))
362
363/*
364 * XOR Simple queue functions.
365 */
366#define XSIMPLEQ_INIT(head) do { \
367 arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
368 (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
369 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
370} while (0)
371
372#define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
373 if (((elm)->field.sqx_next = (head)->sqx_first) == \
374 XSIMPLEQ_XOR(head, NULL)) \
375 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
376 (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
377} while (0)
378
379#define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
380 (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
381 *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
382 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
383} while (0)
384
385#define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
386 if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
387 XSIMPLEQ_XOR(head, NULL)) \
388 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
389 (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
390} while (0)
391
392#define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
393 if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
394 (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
395 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
396} while (0)
397
398#define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
399 if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
400 (elm)->field.sqx_next)->field.sqx_next) \
401 == XSIMPLEQ_XOR(head, NULL)) \
402 (head)->sqx_last = \
403 XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
404} while (0)
405
406
407/*
408 * Tail queue definitions.
409 */
410#define TAILQ_HEAD(name, type) \
411struct name { \
412 struct type *tqh_first; /* first element */ \
413 struct type **tqh_last; /* addr of last next element */ \
414}
415
416#define TAILQ_HEAD_INITIALIZER(head) \
417 { NULL, &(head).tqh_first }
418
419#define TAILQ_ENTRY(type) \
420struct { \
421 struct type *tqe_next; /* next element */ \
422 struct type **tqe_prev; /* address of previous next element */ \
423}
424
425/*
426 * Tail queue access methods.
427 */
428#define TAILQ_FIRST(head) ((head)->tqh_first)
429#define TAILQ_END(head) NULL
430#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
431#define TAILQ_LAST(head, headname) \
432 (*(((struct headname *)((head)->tqh_last))->tqh_last))
433/* XXX */
434#define TAILQ_PREV(elm, headname, field) \
435 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
436#define TAILQ_EMPTY(head) \
437 (TAILQ_FIRST(head) == TAILQ_END(head))
438
439#define TAILQ_FOREACH(var, head, field) \
440 for((var) = TAILQ_FIRST(head); \
441 (var) != TAILQ_END(head); \
442 (var) = TAILQ_NEXT(var, field))
443
444#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
445 for ((var) = TAILQ_FIRST(head); \
446 (var) != TAILQ_END(head) && \
447 ((tvar) = TAILQ_NEXT(var, field), 1); \
448 (var) = (tvar))
449
450
451#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
452 for((var) = TAILQ_LAST(head, headname); \
453 (var) != TAILQ_END(head); \
454 (var) = TAILQ_PREV(var, headname, field))
455
456#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
457 for ((var) = TAILQ_LAST(head, headname); \
458 (var) != TAILQ_END(head) && \
459 ((tvar) = TAILQ_PREV(var, headname, field), 1); \
460 (var) = (tvar))
461
462/*
463 * Tail queue functions.
464 */
465#define TAILQ_INIT(head) do { \
466 (head)->tqh_first = NULL; \
467 (head)->tqh_last = &(head)->tqh_first; \
468} while (0)
469
470#define TAILQ_INSERT_HEAD(head, elm, field) do { \
471 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
472 (head)->tqh_first->field.tqe_prev = \
473 &(elm)->field.tqe_next; \
474 else \
475 (head)->tqh_last = &(elm)->field.tqe_next; \
476 (head)->tqh_first = (elm); \
477 (elm)->field.tqe_prev = &(head)->tqh_first; \
478} while (0)
479
480#define TAILQ_INSERT_TAIL(head, elm, field) do { \
481 (elm)->field.tqe_next = NULL; \
482 (elm)->field.tqe_prev = (head)->tqh_last; \
483 *(head)->tqh_last = (elm); \
484 (head)->tqh_last = &(elm)->field.tqe_next; \
485} while (0)
486
487#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
488 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
489 (elm)->field.tqe_next->field.tqe_prev = \
490 &(elm)->field.tqe_next; \
491 else \
492 (head)->tqh_last = &(elm)->field.tqe_next; \
493 (listelm)->field.tqe_next = (elm); \
494 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
495} while (0)
496
497#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
498 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
499 (elm)->field.tqe_next = (listelm); \
500 *(listelm)->field.tqe_prev = (elm); \
501 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
502} while (0)
503
504#define TAILQ_REMOVE(head, elm, field) do { \
505 if (((elm)->field.tqe_next) != NULL) \
506 (elm)->field.tqe_next->field.tqe_prev = \
507 (elm)->field.tqe_prev; \
508 else \
509 (head)->tqh_last = (elm)->field.tqe_prev; \
510 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
511 _Q_INVALIDATE((elm)->field.tqe_prev); \
512 _Q_INVALIDATE((elm)->field.tqe_next); \
513} while (0)
514
515#define TAILQ_REPLACE(head, elm, elm2, field) do { \
516 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
517 (elm2)->field.tqe_next->field.tqe_prev = \
518 &(elm2)->field.tqe_next; \
519 else \
520 (head)->tqh_last = &(elm2)->field.tqe_next; \
521 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
522 *(elm2)->field.tqe_prev = (elm2); \
523 _Q_INVALIDATE((elm)->field.tqe_prev); \
524 _Q_INVALIDATE((elm)->field.tqe_next); \
525} while (0)
526
527#define TAILQ_CONCAT(head1, head2, field) do { \
528 if (!TAILQ_EMPTY(head2)) { \
529 *(head1)->tqh_last = (head2)->tqh_first; \
530 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
531 (head1)->tqh_last = (head2)->tqh_last; \
532 TAILQ_INIT((head2)); \
533 } \
534} while (0)
535
536#endif /* !_SYS_QUEUE_H_ */
diff --git a/include/compat/sys/tree.h b/include/compat/sys/tree.h
new file mode 100644
index 0000000..ffcac90
--- /dev/null
+++ b/include/compat/sys/tree.h
@@ -0,0 +1,1006 @@
1/* $OpenBSD: tree.h,v 1.29 2017/07/30 19:27:20 deraadt Exp $ */
2/*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef _SYS_TREE_H_
28#define _SYS_TREE_H_
29
30#include <sys/_null.h>
31
32/*
33 * This file defines data structures for different types of trees:
34 * splay trees and red-black trees.
35 *
36 * A splay tree is a self-organizing data structure. Every operation
37 * on the tree causes a splay to happen. The splay moves the requested
38 * node to the root of the tree and partly rebalances it.
39 *
40 * This has the benefit that request locality causes faster lookups as
41 * the requested nodes move to the top of the tree. On the other hand,
42 * every lookup causes memory writes.
43 *
44 * The Balance Theorem bounds the total access time for m operations
45 * and n inserts on an initially empty tree as O((m + n)lg n). The
46 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
47 *
48 * A red-black tree is a binary search tree with the node color as an
49 * extra attribute. It fulfills a set of conditions:
50 * - every search path from the root to a leaf consists of the
51 * same number of black nodes,
52 * - each red node (except for the root) has a black parent,
53 * - each leaf node is black.
54 *
55 * Every operation on a red-black tree is bounded as O(lg n).
56 * The maximum height of a red-black tree is 2lg (n+1).
57 */
58
59#define SPLAY_HEAD(name, type) \
60struct name { \
61 struct type *sph_root; /* root of the tree */ \
62}
63
64#define SPLAY_INITIALIZER(root) \
65 { NULL }
66
67#define SPLAY_INIT(root) do { \
68 (root)->sph_root = NULL; \
69} while (0)
70
71#define SPLAY_ENTRY(type) \
72struct { \
73 struct type *spe_left; /* left element */ \
74 struct type *spe_right; /* right element */ \
75}
76
77#define SPLAY_LEFT(elm, field) (elm)->field.spe_left
78#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
79#define SPLAY_ROOT(head) (head)->sph_root
80#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
81
82/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
83#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
84 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
85 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
86 (head)->sph_root = tmp; \
87} while (0)
88
89#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
90 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
91 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
92 (head)->sph_root = tmp; \
93} while (0)
94
95#define SPLAY_LINKLEFT(head, tmp, field) do { \
96 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
97 tmp = (head)->sph_root; \
98 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
99} while (0)
100
101#define SPLAY_LINKRIGHT(head, tmp, field) do { \
102 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
103 tmp = (head)->sph_root; \
104 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
105} while (0)
106
107#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
108 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
109 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
110 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
111 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
112} while (0)
113
114/* Generates prototypes and inline functions */
115
116#define SPLAY_PROTOTYPE(name, type, field, cmp) \
117void name##_SPLAY(struct name *, struct type *); \
118void name##_SPLAY_MINMAX(struct name *, int); \
119struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
120struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
121 \
122/* Finds the node with the same key as elm */ \
123static __unused __inline struct type * \
124name##_SPLAY_FIND(struct name *head, struct type *elm) \
125{ \
126 if (SPLAY_EMPTY(head)) \
127 return(NULL); \
128 name##_SPLAY(head, elm); \
129 if ((cmp)(elm, (head)->sph_root) == 0) \
130 return (head->sph_root); \
131 return (NULL); \
132} \
133 \
134static __unused __inline struct type * \
135name##_SPLAY_NEXT(struct name *head, struct type *elm) \
136{ \
137 name##_SPLAY(head, elm); \
138 if (SPLAY_RIGHT(elm, field) != NULL) { \
139 elm = SPLAY_RIGHT(elm, field); \
140 while (SPLAY_LEFT(elm, field) != NULL) { \
141 elm = SPLAY_LEFT(elm, field); \
142 } \
143 } else \
144 elm = NULL; \
145 return (elm); \
146} \
147 \
148static __unused __inline struct type * \
149name##_SPLAY_MIN_MAX(struct name *head, int val) \
150{ \
151 name##_SPLAY_MINMAX(head, val); \
152 return (SPLAY_ROOT(head)); \
153}
154
155/* Main splay operation.
156 * Moves node close to the key of elm to top
157 */
158#define SPLAY_GENERATE(name, type, field, cmp) \
159struct type * \
160name##_SPLAY_INSERT(struct name *head, struct type *elm) \
161{ \
162 if (SPLAY_EMPTY(head)) { \
163 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
164 } else { \
165 int __comp; \
166 name##_SPLAY(head, elm); \
167 __comp = (cmp)(elm, (head)->sph_root); \
168 if(__comp < 0) { \
169 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
170 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
171 SPLAY_LEFT((head)->sph_root, field) = NULL; \
172 } else if (__comp > 0) { \
173 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
174 SPLAY_LEFT(elm, field) = (head)->sph_root; \
175 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
176 } else \
177 return ((head)->sph_root); \
178 } \
179 (head)->sph_root = (elm); \
180 return (NULL); \
181} \
182 \
183struct type * \
184name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
185{ \
186 struct type *__tmp; \
187 if (SPLAY_EMPTY(head)) \
188 return (NULL); \
189 name##_SPLAY(head, elm); \
190 if ((cmp)(elm, (head)->sph_root) == 0) { \
191 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
192 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
193 } else { \
194 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
195 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
196 name##_SPLAY(head, elm); \
197 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
198 } \
199 return (elm); \
200 } \
201 return (NULL); \
202} \
203 \
204void \
205name##_SPLAY(struct name *head, struct type *elm) \
206{ \
207 struct type __node, *__left, *__right, *__tmp; \
208 int __comp; \
209\
210 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
211 __left = __right = &__node; \
212\
213 while ((__comp = (cmp)(elm, (head)->sph_root))) { \
214 if (__comp < 0) { \
215 __tmp = SPLAY_LEFT((head)->sph_root, field); \
216 if (__tmp == NULL) \
217 break; \
218 if ((cmp)(elm, __tmp) < 0){ \
219 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
220 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
221 break; \
222 } \
223 SPLAY_LINKLEFT(head, __right, field); \
224 } else if (__comp > 0) { \
225 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
226 if (__tmp == NULL) \
227 break; \
228 if ((cmp)(elm, __tmp) > 0){ \
229 SPLAY_ROTATE_LEFT(head, __tmp, field); \
230 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
231 break; \
232 } \
233 SPLAY_LINKRIGHT(head, __left, field); \
234 } \
235 } \
236 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
237} \
238 \
239/* Splay with either the minimum or the maximum element \
240 * Used to find minimum or maximum element in tree. \
241 */ \
242void name##_SPLAY_MINMAX(struct name *head, int __comp) \
243{ \
244 struct type __node, *__left, *__right, *__tmp; \
245\
246 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
247 __left = __right = &__node; \
248\
249 while (1) { \
250 if (__comp < 0) { \
251 __tmp = SPLAY_LEFT((head)->sph_root, field); \
252 if (__tmp == NULL) \
253 break; \
254 if (__comp < 0){ \
255 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
256 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
257 break; \
258 } \
259 SPLAY_LINKLEFT(head, __right, field); \
260 } else if (__comp > 0) { \
261 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
262 if (__tmp == NULL) \
263 break; \
264 if (__comp > 0) { \
265 SPLAY_ROTATE_LEFT(head, __tmp, field); \
266 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
267 break; \
268 } \
269 SPLAY_LINKRIGHT(head, __left, field); \
270 } \
271 } \
272 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
273}
274
275#define SPLAY_NEGINF -1
276#define SPLAY_INF 1
277
278#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
279#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
280#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
281#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
282#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
283 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
284#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
285 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
286
287#define SPLAY_FOREACH(x, name, head) \
288 for ((x) = SPLAY_MIN(name, head); \
289 (x) != NULL; \
290 (x) = SPLAY_NEXT(name, head, x))
291
292/* Macros that define a red-black tree */
293#define RB_HEAD(name, type) \
294struct name { \
295 struct type *rbh_root; /* root of the tree */ \
296}
297
298#define RB_INITIALIZER(root) \
299 { NULL }
300
301#define RB_INIT(root) do { \
302 (root)->rbh_root = NULL; \
303} while (0)
304
305#define RB_BLACK 0
306#define RB_RED 1
307#define RB_ENTRY(type) \
308struct { \
309 struct type *rbe_left; /* left element */ \
310 struct type *rbe_right; /* right element */ \
311 struct type *rbe_parent; /* parent element */ \
312 int rbe_color; /* node color */ \
313}
314
315#define RB_LEFT(elm, field) (elm)->field.rbe_left
316#define RB_RIGHT(elm, field) (elm)->field.rbe_right
317#define RB_PARENT(elm, field) (elm)->field.rbe_parent
318#define RB_COLOR(elm, field) (elm)->field.rbe_color
319#define RB_ROOT(head) (head)->rbh_root
320#define RB_EMPTY(head) (RB_ROOT(head) == NULL)
321
322#define RB_SET(elm, parent, field) do { \
323 RB_PARENT(elm, field) = parent; \
324 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
325 RB_COLOR(elm, field) = RB_RED; \
326} while (0)
327
328#define RB_SET_BLACKRED(black, red, field) do { \
329 RB_COLOR(black, field) = RB_BLACK; \
330 RB_COLOR(red, field) = RB_RED; \
331} while (0)
332
333#ifndef RB_AUGMENT
334#define RB_AUGMENT(x) do {} while (0)
335#endif
336
337#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
338 (tmp) = RB_RIGHT(elm, field); \
339 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
340 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
341 } \
342 RB_AUGMENT(elm); \
343 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
344 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
345 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
346 else \
347 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
348 } else \
349 (head)->rbh_root = (tmp); \
350 RB_LEFT(tmp, field) = (elm); \
351 RB_PARENT(elm, field) = (tmp); \
352 RB_AUGMENT(tmp); \
353 if ((RB_PARENT(tmp, field))) \
354 RB_AUGMENT(RB_PARENT(tmp, field)); \
355} while (0)
356
357#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
358 (tmp) = RB_LEFT(elm, field); \
359 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
360 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
361 } \
362 RB_AUGMENT(elm); \
363 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
364 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
365 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
366 else \
367 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
368 } else \
369 (head)->rbh_root = (tmp); \
370 RB_RIGHT(tmp, field) = (elm); \
371 RB_PARENT(elm, field) = (tmp); \
372 RB_AUGMENT(tmp); \
373 if ((RB_PARENT(tmp, field))) \
374 RB_AUGMENT(RB_PARENT(tmp, field)); \
375} while (0)
376
377/* Generates prototypes and inline functions */
378#define RB_PROTOTYPE(name, type, field, cmp) \
379 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
380#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
381 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
382#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
383attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
384attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
385attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
386attr struct type *name##_RB_INSERT(struct name *, struct type *); \
387attr struct type *name##_RB_FIND(struct name *, struct type *); \
388attr struct type *name##_RB_NFIND(struct name *, struct type *); \
389attr struct type *name##_RB_NEXT(struct type *); \
390attr struct type *name##_RB_PREV(struct type *); \
391attr struct type *name##_RB_MINMAX(struct name *, int); \
392 \
393
394/* Main rb operation.
395 * Moves node close to the key of elm to top
396 */
397#define RB_GENERATE(name, type, field, cmp) \
398 RB_GENERATE_INTERNAL(name, type, field, cmp,)
399#define RB_GENERATE_STATIC(name, type, field, cmp) \
400 RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
401#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
402attr void \
403name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
404{ \
405 struct type *parent, *gparent, *tmp; \
406 while ((parent = RB_PARENT(elm, field)) && \
407 RB_COLOR(parent, field) == RB_RED) { \
408 gparent = RB_PARENT(parent, field); \
409 if (parent == RB_LEFT(gparent, field)) { \
410 tmp = RB_RIGHT(gparent, field); \
411 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
412 RB_COLOR(tmp, field) = RB_BLACK; \
413 RB_SET_BLACKRED(parent, gparent, field);\
414 elm = gparent; \
415 continue; \
416 } \
417 if (RB_RIGHT(parent, field) == elm) { \
418 RB_ROTATE_LEFT(head, parent, tmp, field);\
419 tmp = parent; \
420 parent = elm; \
421 elm = tmp; \
422 } \
423 RB_SET_BLACKRED(parent, gparent, field); \
424 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
425 } else { \
426 tmp = RB_LEFT(gparent, field); \
427 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
428 RB_COLOR(tmp, field) = RB_BLACK; \
429 RB_SET_BLACKRED(parent, gparent, field);\
430 elm = gparent; \
431 continue; \
432 } \
433 if (RB_LEFT(parent, field) == elm) { \
434 RB_ROTATE_RIGHT(head, parent, tmp, field);\
435 tmp = parent; \
436 parent = elm; \
437 elm = tmp; \
438 } \
439 RB_SET_BLACKRED(parent, gparent, field); \
440 RB_ROTATE_LEFT(head, gparent, tmp, field); \
441 } \
442 } \
443 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
444} \
445 \
446attr void \
447name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
448{ \
449 struct type *tmp; \
450 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
451 elm != RB_ROOT(head)) { \
452 if (RB_LEFT(parent, field) == elm) { \
453 tmp = RB_RIGHT(parent, field); \
454 if (RB_COLOR(tmp, field) == RB_RED) { \
455 RB_SET_BLACKRED(tmp, parent, field); \
456 RB_ROTATE_LEFT(head, parent, tmp, field);\
457 tmp = RB_RIGHT(parent, field); \
458 } \
459 if ((RB_LEFT(tmp, field) == NULL || \
460 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
461 (RB_RIGHT(tmp, field) == NULL || \
462 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
463 RB_COLOR(tmp, field) = RB_RED; \
464 elm = parent; \
465 parent = RB_PARENT(elm, field); \
466 } else { \
467 if (RB_RIGHT(tmp, field) == NULL || \
468 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
469 struct type *oleft; \
470 if ((oleft = RB_LEFT(tmp, field)))\
471 RB_COLOR(oleft, field) = RB_BLACK;\
472 RB_COLOR(tmp, field) = RB_RED; \
473 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
474 tmp = RB_RIGHT(parent, field); \
475 } \
476 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
477 RB_COLOR(parent, field) = RB_BLACK; \
478 if (RB_RIGHT(tmp, field)) \
479 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
480 RB_ROTATE_LEFT(head, parent, tmp, field);\
481 elm = RB_ROOT(head); \
482 break; \
483 } \
484 } else { \
485 tmp = RB_LEFT(parent, field); \
486 if (RB_COLOR(tmp, field) == RB_RED) { \
487 RB_SET_BLACKRED(tmp, parent, field); \
488 RB_ROTATE_RIGHT(head, parent, tmp, field);\
489 tmp = RB_LEFT(parent, field); \
490 } \
491 if ((RB_LEFT(tmp, field) == NULL || \
492 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
493 (RB_RIGHT(tmp, field) == NULL || \
494 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
495 RB_COLOR(tmp, field) = RB_RED; \
496 elm = parent; \
497 parent = RB_PARENT(elm, field); \
498 } else { \
499 if (RB_LEFT(tmp, field) == NULL || \
500 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
501 struct type *oright; \
502 if ((oright = RB_RIGHT(tmp, field)))\
503 RB_COLOR(oright, field) = RB_BLACK;\
504 RB_COLOR(tmp, field) = RB_RED; \
505 RB_ROTATE_LEFT(head, tmp, oright, field);\
506 tmp = RB_LEFT(parent, field); \
507 } \
508 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
509 RB_COLOR(parent, field) = RB_BLACK; \
510 if (RB_LEFT(tmp, field)) \
511 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
512 RB_ROTATE_RIGHT(head, parent, tmp, field);\
513 elm = RB_ROOT(head); \
514 break; \
515 } \
516 } \
517 } \
518 if (elm) \
519 RB_COLOR(elm, field) = RB_BLACK; \
520} \
521 \
522attr struct type * \
523name##_RB_REMOVE(struct name *head, struct type *elm) \
524{ \
525 struct type *child, *parent, *old = elm; \
526 int color; \
527 if (RB_LEFT(elm, field) == NULL) \
528 child = RB_RIGHT(elm, field); \
529 else if (RB_RIGHT(elm, field) == NULL) \
530 child = RB_LEFT(elm, field); \
531 else { \
532 struct type *left; \
533 elm = RB_RIGHT(elm, field); \
534 while ((left = RB_LEFT(elm, field))) \
535 elm = left; \
536 child = RB_RIGHT(elm, field); \
537 parent = RB_PARENT(elm, field); \
538 color = RB_COLOR(elm, field); \
539 if (child) \
540 RB_PARENT(child, field) = parent; \
541 if (parent) { \
542 if (RB_LEFT(parent, field) == elm) \
543 RB_LEFT(parent, field) = child; \
544 else \
545 RB_RIGHT(parent, field) = child; \
546 RB_AUGMENT(parent); \
547 } else \
548 RB_ROOT(head) = child; \
549 if (RB_PARENT(elm, field) == old) \
550 parent = elm; \
551 (elm)->field = (old)->field; \
552 if (RB_PARENT(old, field)) { \
553 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
554 RB_LEFT(RB_PARENT(old, field), field) = elm;\
555 else \
556 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
557 RB_AUGMENT(RB_PARENT(old, field)); \
558 } else \
559 RB_ROOT(head) = elm; \
560 RB_PARENT(RB_LEFT(old, field), field) = elm; \
561 if (RB_RIGHT(old, field)) \
562 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
563 if (parent) { \
564 left = parent; \
565 do { \
566 RB_AUGMENT(left); \
567 } while ((left = RB_PARENT(left, field))); \
568 } \
569 goto color; \
570 } \
571 parent = RB_PARENT(elm, field); \
572 color = RB_COLOR(elm, field); \
573 if (child) \
574 RB_PARENT(child, field) = parent; \
575 if (parent) { \
576 if (RB_LEFT(parent, field) == elm) \
577 RB_LEFT(parent, field) = child; \
578 else \
579 RB_RIGHT(parent, field) = child; \
580 RB_AUGMENT(parent); \
581 } else \
582 RB_ROOT(head) = child; \
583color: \
584 if (color == RB_BLACK) \
585 name##_RB_REMOVE_COLOR(head, parent, child); \
586 return (old); \
587} \
588 \
589/* Inserts a node into the RB tree */ \
590attr struct type * \
591name##_RB_INSERT(struct name *head, struct type *elm) \
592{ \
593 struct type *tmp; \
594 struct type *parent = NULL; \
595 int comp = 0; \
596 tmp = RB_ROOT(head); \
597 while (tmp) { \
598 parent = tmp; \
599 comp = (cmp)(elm, parent); \
600 if (comp < 0) \
601 tmp = RB_LEFT(tmp, field); \
602 else if (comp > 0) \
603 tmp = RB_RIGHT(tmp, field); \
604 else \
605 return (tmp); \
606 } \
607 RB_SET(elm, parent, field); \
608 if (parent != NULL) { \
609 if (comp < 0) \
610 RB_LEFT(parent, field) = elm; \
611 else \
612 RB_RIGHT(parent, field) = elm; \
613 RB_AUGMENT(parent); \
614 } else \
615 RB_ROOT(head) = elm; \
616 name##_RB_INSERT_COLOR(head, elm); \
617 return (NULL); \
618} \
619 \
620/* Finds the node with the same key as elm */ \
621attr struct type * \
622name##_RB_FIND(struct name *head, struct type *elm) \
623{ \
624 struct type *tmp = RB_ROOT(head); \
625 int comp; \
626 while (tmp) { \
627 comp = cmp(elm, tmp); \
628 if (comp < 0) \
629 tmp = RB_LEFT(tmp, field); \
630 else if (comp > 0) \
631 tmp = RB_RIGHT(tmp, field); \
632 else \
633 return (tmp); \
634 } \
635 return (NULL); \
636} \
637 \
638/* Finds the first node greater than or equal to the search key */ \
639attr struct type * \
640name##_RB_NFIND(struct name *head, struct type *elm) \
641{ \
642 struct type *tmp = RB_ROOT(head); \
643 struct type *res = NULL; \
644 int comp; \
645 while (tmp) { \
646 comp = cmp(elm, tmp); \
647 if (comp < 0) { \
648 res = tmp; \
649 tmp = RB_LEFT(tmp, field); \
650 } \
651 else if (comp > 0) \
652 tmp = RB_RIGHT(tmp, field); \
653 else \
654 return (tmp); \
655 } \
656 return (res); \
657} \
658 \
659/* ARGSUSED */ \
660attr struct type * \
661name##_RB_NEXT(struct type *elm) \
662{ \
663 if (RB_RIGHT(elm, field)) { \
664 elm = RB_RIGHT(elm, field); \
665 while (RB_LEFT(elm, field)) \
666 elm = RB_LEFT(elm, field); \
667 } else { \
668 if (RB_PARENT(elm, field) && \
669 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
670 elm = RB_PARENT(elm, field); \
671 else { \
672 while (RB_PARENT(elm, field) && \
673 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
674 elm = RB_PARENT(elm, field); \
675 elm = RB_PARENT(elm, field); \
676 } \
677 } \
678 return (elm); \
679} \
680 \
681/* ARGSUSED */ \
682attr struct type * \
683name##_RB_PREV(struct type *elm) \
684{ \
685 if (RB_LEFT(elm, field)) { \
686 elm = RB_LEFT(elm, field); \
687 while (RB_RIGHT(elm, field)) \
688 elm = RB_RIGHT(elm, field); \
689 } else { \
690 if (RB_PARENT(elm, field) && \
691 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
692 elm = RB_PARENT(elm, field); \
693 else { \
694 while (RB_PARENT(elm, field) && \
695 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
696 elm = RB_PARENT(elm, field); \
697 elm = RB_PARENT(elm, field); \
698 } \
699 } \
700 return (elm); \
701} \
702 \
703attr struct type * \
704name##_RB_MINMAX(struct name *head, int val) \
705{ \
706 struct type *tmp = RB_ROOT(head); \
707 struct type *parent = NULL; \
708 while (tmp) { \
709 parent = tmp; \
710 if (val < 0) \
711 tmp = RB_LEFT(tmp, field); \
712 else \
713 tmp = RB_RIGHT(tmp, field); \
714 } \
715 return (parent); \
716}
717
718#define RB_NEGINF -1
719#define RB_INF 1
720
721#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
722#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
723#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
724#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
725#define RB_NEXT(name, x, y) name##_RB_NEXT(y)
726#define RB_PREV(name, x, y) name##_RB_PREV(y)
727#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
728#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
729
730#define RB_FOREACH(x, name, head) \
731 for ((x) = RB_MIN(name, head); \
732 (x) != NULL; \
733 (x) = name##_RB_NEXT(x))
734
735#define RB_FOREACH_SAFE(x, name, head, y) \
736 for ((x) = RB_MIN(name, head); \
737 ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
738 (x) = (y))
739
740#define RB_FOREACH_REVERSE(x, name, head) \
741 for ((x) = RB_MAX(name, head); \
742 (x) != NULL; \
743 (x) = name##_RB_PREV(x))
744
745#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
746 for ((x) = RB_MAX(name, head); \
747 ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
748 (x) = (y))
749
750
751/*
752 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
753 *
754 * Permission to use, copy, modify, and distribute this software for any
755 * purpose with or without fee is hereby granted, provided that the above
756 * copyright notice and this permission notice appear in all copies.
757 *
758 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
759 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
760 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
761 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
762 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
763 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
764 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
765 */
766
767struct rb_type {
768 int (*t_compare)(const void *, const void *);
769 void (*t_augment)(void *);
770 unsigned int t_offset; /* offset of rb_entry in type */
771};
772
773struct rb_tree {
774 struct rb_entry *rbt_root;
775};
776
777struct rb_entry {
778 struct rb_entry *rbt_parent;
779 struct rb_entry *rbt_left;
780 struct rb_entry *rbt_right;
781 unsigned int rbt_color;
782};
783
784#define RBT_HEAD(_name, _type) \
785struct _name { \
786 struct rb_tree rbh_root; \
787}
788
789#define RBT_ENTRY(_type) struct rb_entry
790
791static inline void
792_rb_init(struct rb_tree *rbt)
793{
794 rbt->rbt_root = NULL;
795}
796
797static inline int
798_rb_empty(struct rb_tree *rbt)
799{
800 return (rbt->rbt_root == NULL);
801}
802
803void *_rb_insert(const struct rb_type *, struct rb_tree *, void *);
804void *_rb_remove(const struct rb_type *, struct rb_tree *, void *);
805void *_rb_find(const struct rb_type *, struct rb_tree *, const void *);
806void *_rb_nfind(const struct rb_type *, struct rb_tree *, const void *);
807void *_rb_root(const struct rb_type *, struct rb_tree *);
808void *_rb_min(const struct rb_type *, struct rb_tree *);
809void *_rb_max(const struct rb_type *, struct rb_tree *);
810void *_rb_next(const struct rb_type *, void *);
811void *_rb_prev(const struct rb_type *, void *);
812void *_rb_left(const struct rb_type *, void *);
813void *_rb_right(const struct rb_type *, void *);
814void *_rb_parent(const struct rb_type *, void *);
815void _rb_set_left(const struct rb_type *, void *, void *);
816void _rb_set_right(const struct rb_type *, void *, void *);
817void _rb_set_parent(const struct rb_type *, void *, void *);
818void _rb_poison(const struct rb_type *, void *, unsigned long);
819int _rb_check(const struct rb_type *, void *, unsigned long);
820
821#define RBT_INITIALIZER(_head) { { NULL } }
822
823#define RBT_PROTOTYPE(_name, _type, _field, _cmp) \
824extern const struct rb_type *const _name##_RBT_TYPE; \
825 \
826__unused static inline void \
827_name##_RBT_INIT(struct _name *head) \
828{ \
829 _rb_init(&head->rbh_root); \
830} \
831 \
832__unused static inline struct _type * \
833_name##_RBT_INSERT(struct _name *head, struct _type *elm) \
834{ \
835 return _rb_insert(_name##_RBT_TYPE, &head->rbh_root, elm); \
836} \
837 \
838__unused static inline struct _type * \
839_name##_RBT_REMOVE(struct _name *head, struct _type *elm) \
840{ \
841 return _rb_remove(_name##_RBT_TYPE, &head->rbh_root, elm); \
842} \
843 \
844__unused static inline struct _type * \
845_name##_RBT_FIND(struct _name *head, const struct _type *key) \
846{ \
847 return _rb_find(_name##_RBT_TYPE, &head->rbh_root, key); \
848} \
849 \
850__unused static inline struct _type * \
851_name##_RBT_NFIND(struct _name *head, const struct _type *key) \
852{ \
853 return _rb_nfind(_name##_RBT_TYPE, &head->rbh_root, key); \
854} \
855 \
856__unused static inline struct _type * \
857_name##_RBT_ROOT(struct _name *head) \
858{ \
859 return _rb_root(_name##_RBT_TYPE, &head->rbh_root); \
860} \
861 \
862__unused static inline int \
863_name##_RBT_EMPTY(struct _name *head) \
864{ \
865 return _rb_empty(&head->rbh_root); \
866} \
867 \
868__unused static inline struct _type * \
869_name##_RBT_MIN(struct _name *head) \
870{ \
871 return _rb_min(_name##_RBT_TYPE, &head->rbh_root); \
872} \
873 \
874__unused static inline struct _type * \
875_name##_RBT_MAX(struct _name *head) \
876{ \
877 return _rb_max(_name##_RBT_TYPE, &head->rbh_root); \
878} \
879 \
880__unused static inline struct _type * \
881_name##_RBT_NEXT(struct _type *elm) \
882{ \
883 return _rb_next(_name##_RBT_TYPE, elm); \
884} \
885 \
886__unused static inline struct _type * \
887_name##_RBT_PREV(struct _type *elm) \
888{ \
889 return _rb_prev(_name##_RBT_TYPE, elm); \
890} \
891 \
892__unused static inline struct _type * \
893_name##_RBT_LEFT(struct _type *elm) \
894{ \
895 return _rb_left(_name##_RBT_TYPE, elm); \
896} \
897 \
898__unused static inline struct _type * \
899_name##_RBT_RIGHT(struct _type *elm) \
900{ \
901 return _rb_right(_name##_RBT_TYPE, elm); \
902} \
903 \
904__unused static inline struct _type * \
905_name##_RBT_PARENT(struct _type *elm) \
906{ \
907 return _rb_parent(_name##_RBT_TYPE, elm); \
908} \
909 \
910__unused static inline void \
911_name##_RBT_SET_LEFT(struct _type *elm, struct _type *left) \
912{ \
913 return _rb_set_left(_name##_RBT_TYPE, elm, left); \
914} \
915 \
916__unused static inline void \
917_name##_RBT_SET_RIGHT(struct _type *elm, struct _type *right) \
918{ \
919 return _rb_set_right(_name##_RBT_TYPE, elm, right); \
920} \
921 \
922__unused static inline void \
923_name##_RBT_SET_PARENT(struct _type *elm, struct _type *parent) \
924{ \
925 return _rb_set_parent(_name##_RBT_TYPE, elm, parent); \
926} \
927 \
928__unused static inline void \
929_name##_RBT_POISON(struct _type *elm, unsigned long poison) \
930{ \
931 return _rb_poison(_name##_RBT_TYPE, elm, poison); \
932} \
933 \
934__unused static inline int \
935_name##_RBT_CHECK(struct _type *elm, unsigned long poison) \
936{ \
937 return _rb_check(_name##_RBT_TYPE, elm, poison); \
938}
939
940#define RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _aug) \
941static int \
942_name##_RBT_COMPARE(const void *lptr, const void *rptr) \
943{ \
944 const struct _type *l = lptr, *r = rptr; \
945 return _cmp(l, r); \
946} \
947static const struct rb_type _name##_RBT_INFO = { \
948 _name##_RBT_COMPARE, \
949 _aug, \
950 offsetof(struct _type, _field), \
951}; \
952const struct rb_type *const _name##_RBT_TYPE = &_name##_RBT_INFO
953
954#define RBT_GENERATE_AUGMENT(_name, _type, _field, _cmp, _aug) \
955static void \
956_name##_RBT_AUGMENT(void *ptr) \
957{ \
958 struct _type *p = ptr; \
959 return _aug(p); \
960} \
961RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _name##_RBT_AUGMENT)
962
963#define RBT_GENERATE(_name, _type, _field, _cmp) \
964 RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, NULL)
965
966#define RBT_INIT(_name, _head) _name##_RBT_INIT(_head)
967#define RBT_INSERT(_name, _head, _elm) _name##_RBT_INSERT(_head, _elm)
968#define RBT_REMOVE(_name, _head, _elm) _name##_RBT_REMOVE(_head, _elm)
969#define RBT_FIND(_name, _head, _key) _name##_RBT_FIND(_head, _key)
970#define RBT_NFIND(_name, _head, _key) _name##_RBT_NFIND(_head, _key)
971#define RBT_ROOT(_name, _head) _name##_RBT_ROOT(_head)
972#define RBT_EMPTY(_name, _head) _name##_RBT_EMPTY(_head)
973#define RBT_MIN(_name, _head) _name##_RBT_MIN(_head)
974#define RBT_MAX(_name, _head) _name##_RBT_MAX(_head)
975#define RBT_NEXT(_name, _elm) _name##_RBT_NEXT(_elm)
976#define RBT_PREV(_name, _elm) _name##_RBT_PREV(_elm)
977#define RBT_LEFT(_name, _elm) _name##_RBT_LEFT(_elm)
978#define RBT_RIGHT(_name, _elm) _name##_RBT_RIGHT(_elm)
979#define RBT_PARENT(_name, _elm) _name##_RBT_PARENT(_elm)
980#define RBT_SET_LEFT(_name, _elm, _l) _name##_RBT_SET_LEFT(_elm, _l)
981#define RBT_SET_RIGHT(_name, _elm, _r) _name##_RBT_SET_RIGHT(_elm, _r)
982#define RBT_SET_PARENT(_name, _elm, _p) _name##_RBT_SET_PARENT(_elm, _p)
983#define RBT_POISON(_name, _elm, _p) _name##_RBT_POISON(_elm, _p)
984#define RBT_CHECK(_name, _elm, _p) _name##_RBT_CHECK(_elm, _p)
985
986#define RBT_FOREACH(_e, _name, _head) \
987 for ((_e) = RBT_MIN(_name, (_head)); \
988 (_e) != NULL; \
989 (_e) = RBT_NEXT(_name, (_e)))
990
991#define RBT_FOREACH_SAFE(_e, _name, _head, _n) \
992 for ((_e) = RBT_MIN(_name, (_head)); \
993 (_e) != NULL && ((_n) = RBT_NEXT(_name, (_e)), 1); \
994 (_e) = (_n))
995
996#define RBT_FOREACH_REVERSE(_e, _name, _head) \
997 for ((_e) = RBT_MAX(_name, (_head)); \
998 (_e) != NULL; \
999 (_e) = RBT_PREV(_name, (_e)))
1000
1001#define RBT_FOREACH_REVERSE_SAFE(_e, _name, _head, _n) \
1002 for ((_e) = RBT_MAX(_name, (_head)); \
1003 (_e) != NULL && ((_n) = RBT_PREV(_name, (_e)), 1); \
1004 (_e) = (_n))
1005
1006#endif /* _SYS_TREE_H_ */