1 /******************************************************************************
2  *
3  * Copyright (C) 2002 Jason Evans <jasone@FreeBSD.org>.
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(s), this list of conditions and the following disclaimer
11  *    unmodified other than the allowable addition of one or more
12  *    copyright notices.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice(s), this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  ******************************************************************************/
31 
32 #ifndef QR_H_
33 #define	QR_H_
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: stable/9/lib/libc/stdlib/qr.h 203329 2010-01-31 23:16:10Z jasone $");
37 
38 /* Ring definitions. */
39 #define qr(a_type)							\
40 struct {								\
41 	a_type	*qre_next;						\
42 	a_type	*qre_prev;						\
43 }
44 
45 /* Ring functions. */
46 #define qr_new(a_qr, a_field) do {					\
47 	(a_qr)->a_field.qre_next = (a_qr);				\
48 	(a_qr)->a_field.qre_prev = (a_qr);				\
49 } while (0)
50 
51 #define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next)
52 
53 #define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev)
54 
55 #define qr_before_insert(a_qrelm, a_qr, a_field) do {			\
56 	(a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev;		\
57 	(a_qr)->a_field.qre_next = (a_qrelm);				\
58 	(a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr);		\
59 	(a_qrelm)->a_field.qre_prev = (a_qr);				\
60 } while (0)
61 
62 #define qr_after_insert(a_qrelm, a_qr, a_field)				\
63     do									\
64     {									\
65 	(a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next;		\
66 	(a_qr)->a_field.qre_prev = (a_qrelm);				\
67 	(a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr);		\
68 	(a_qrelm)->a_field.qre_next = (a_qr);				\
69     } while (0)
70 
71 #define qr_meld(a_qr_a, a_qr_b, a_field) do {				\
72 	void *t;							\
73 	(a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b);	\
74 	(a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a);	\
75 	t = (a_qr_a)->a_field.qre_prev;					\
76 	(a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev;	\
77 	(a_qr_b)->a_field.qre_prev = t;					\
78 } while (0)
79 
80 /* qr_meld() and qr_split() are functionally equivalent, so there's no need to
81  * have two copies of the code. */
82 #define qr_split(a_qr_a, a_qr_b, a_field)				\
83 	qr_meld((a_qr_a), (a_qr_b), a_field)
84 
85 #define qr_remove(a_qr, a_field) do {					\
86 	(a_qr)->a_field.qre_prev->a_field.qre_next			\
87 	    = (a_qr)->a_field.qre_next;					\
88 	(a_qr)->a_field.qre_next->a_field.qre_prev			\
89 	    = (a_qr)->a_field.qre_prev;					\
90 	(a_qr)->a_field.qre_next = (a_qr);				\
91 	(a_qr)->a_field.qre_prev = (a_qr);				\
92 } while (0)
93 
94 #define qr_foreach(var, a_qr, a_field)					\
95 	for ((var) = (a_qr);						\
96 	    (var) != NULL;						\
97 	    (var) = (((var)->a_field.qre_next != (a_qr))		\
98 	    ? (var)->a_field.qre_next : NULL))
99 
100 #define qr_reverse_foreach(var, a_qr, a_field)				\
101 	for ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL;	\
102 	    (var) != NULL;						\
103 	    (var) = (((var) != (a_qr))					\
104 	    ? (var)->a_field.qre_prev : NULL))
105 
106 #endif /* QR_H_ */
107