• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  * Copyright (C) 2002 Jason Evans <jasone@canonware.com>.
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 /* Ring definitions. */
33 #define qr(a_type)							\
34 struct {								\
35 	a_type	*qre_next;						\
36 	a_type	*qre_prev;						\
37 }
38 
39 /* Ring functions. */
40 #define qr_new(a_qr, a_field) do {					\
41 	(a_qr)->a_field.qre_next = (a_qr);				\
42 	(a_qr)->a_field.qre_prev = (a_qr);				\
43 } while (0)
44 
45 #define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next)
46 
47 #define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev)
48 
49 #define qr_before_insert(a_qrelm, a_qr, a_field) do {			\
50 	(a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev;		\
51 	(a_qr)->a_field.qre_next = (a_qrelm);				\
52 	(a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr);		\
53 	(a_qrelm)->a_field.qre_prev = (a_qr);				\
54 } while (0)
55 
56 #define qr_after_insert(a_qrelm, a_qr, a_field)				\
57     do									\
58     {									\
59 	(a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next;		\
60 	(a_qr)->a_field.qre_prev = (a_qrelm);				\
61 	(a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr);		\
62 	(a_qrelm)->a_field.qre_next = (a_qr);				\
63     } while (0)
64 
65 #define qr_meld(a_qr_a, a_qr_b, a_field) do {				\
66 	void *t;							\
67 	(a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b);	\
68 	(a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a);	\
69 	t = (a_qr_a)->a_field.qre_prev;					\
70 	(a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev;	\
71 	(a_qr_b)->a_field.qre_prev = t;					\
72 } while (0)
73 
74 /* qr_meld() and qr_split() are functionally equivalent, so there's no need to
75  * have two copies of the code. */
76 #define qr_split(a_qr_a, a_qr_b, a_field)				\
77 	qr_meld((a_qr_a), (a_qr_b), a_field)
78 
79 #define qr_remove(a_qr, a_field) do {					\
80 	(a_qr)->a_field.qre_prev->a_field.qre_next			\
81 	    = (a_qr)->a_field.qre_next;					\
82 	(a_qr)->a_field.qre_next->a_field.qre_prev			\
83 	    = (a_qr)->a_field.qre_prev;					\
84 	(a_qr)->a_field.qre_next = (a_qr);				\
85 	(a_qr)->a_field.qre_prev = (a_qr);				\
86 } while (0)
87 
88 #define qr_foreach(var, a_qr, a_field)					\
89 	for ((var) = (a_qr);						\
90 	    (var) != NULL;						\
91 	    (var) = (((var)->a_field.qre_next != (a_qr))		\
92 	    ? (var)->a_field.qre_next : NULL))
93 
94 #define qr_reverse_foreach(var, a_qr, a_field)				\
95 	for ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL;	\
96 	    (var) != NULL;						\
97 	    (var) = (((var) != (a_qr))					\
98 	    ? (var)->a_field.qre_prev : NULL))
99