xref: /freebsd/sys/dev/sound/midi/midiq.h (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*-
2  * (c) 2003 Mathew Kanner
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: 1. Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer. 2.
8  * Redistributions in binary form must reproduce the above copyright notice,
9  * this list of conditions and the following disclaimer in the documentation
10  * and/or other materials provided with the distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
16  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22  * SUCH DAMAGE.
23  *
24  * $FreeBSD$
25  */
26 
27 #ifndef MIDIQ_H
28 #define MIDIQ_H
29 
30 #define MIDIQ_MOVE(a,b,c) bcopy(b,a,c)
31 
32 #define MIDIQ_HEAD(name, type)          \
33 struct name {                           \
34         int h, t, s;                    \
35         type * b;                        \
36 }
37 
38 #define MIDIQ_INIT(head, buf, size) do {                \
39         (head).h=(head).t=0;                          \
40         (head).s=size;                                 \
41         (head).b=buf;                                  \
42 } while (0)
43 
44 #define MIDIQ_EMPTY(head)       ((head).h == (head).t )
45 
46 #define MIDIQ_LENBASE(head)         ((head).h - (head).t < 0 ? \
47                                         (head).h - (head).t + (head).s : \
48                                         (head).h - (head).t)
49 
50 #define MIDIQ_FULL(head)        ((head).h == -1)
51 #define MIDIQ_AVAIL(head)       (MIDIQ_FULL(head) ? 0 : (head).s - MIDIQ_LENBASE(head))
52 #define MIDIQ_LEN(head)		((head).s - MIDIQ_AVAIL(head))
53 #define MIDIQ_DEBUG 0
54 /*
55  * No protection against overflow, underflow
56  */
57 #define MIDIQ_ENQ(head, buf, size) do {                                                                 \
58 		if(MIDIQ_DEBUG)\
59                 	printf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n",            \
60 			       &(head).b[(head).h], (buf),                                        \
61 			       (intmax_t)(sizeof(*(head).b) *                                     \
62 					  MIN( (size), (head).s - (head).h) ),                   \
63 			       (size), (head).h, (head).t);               \
64                 MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * MIN((size), (head).s - (head).h));                       \
65                 if( (head).s - (head).h < (size) ) {                                                    \
66 			if(MIDIQ_DEBUG) \
67                         	printf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).h, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
68                         MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
69 		} \
70                 (head).h+=(size);                                                                         \
71                 (head).h%=(head).s;                                                                     \
72 		if(MIDIQ_EMPTY(head)) (head).h=-1; \
73 		if(MIDIQ_DEBUG)\
74                 	printf("#E h %d t %d\n", (head).h, (head).t);                       \
75 } while (0)
76 
77 #define MIDIQ_DEQ_I(head, buf, size, move, update) do {                                                                 \
78 		if(MIDIQ_FULL(head)) (head).h=(head).t; \
79 		if(MIDIQ_DEBUG)\
80                 	printf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n", &(head).b[(head).t], (buf), (intmax_t)sizeof(*(head).b) * MIN((size), (head).s - (head).t), (size), (head).h, (head).t);                       \
81                 if (move) MIDIQ_MOVE((buf), &(head).b[(head).t], sizeof(*(head).b) * MIN((size), (head).s - (head).t));                       \
82                 if( (head).s - (head).t < (size) ) {                                                    \
83 			if(MIDIQ_DEBUG) \
84                         	printf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).t, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
85                         if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
86 		} \
87 		if (update) { \
88                 (head).t+=(size);                                                                         \
89                 (head).t%=(head).s;                                                                     \
90 		} else { \
91 		  if (MIDIQ_EMPTY(head)) (head).h=-1; \
92 		} \
93 		if(MIDIQ_DEBUG)\
94                 	printf("#E h %d t %d\n", (head).h, (head).t);                       \
95 } while (0)
96 
97 #define MIDIQ_SIZE(head) ((head).s)
98 #define MIDIQ_CLEAR(head) ((head).h = (head).t = 0)
99 #define MIDIQ_BUF(head) ((head).b)
100 #define MIDIQ_DEQ(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 1)
101 #define MIDIQ_PEEK(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 0)
102 #define MIDIQ_POP(head, size) MIDIQ_DEQ_I(head, &head, size, 0, 1)
103 
104 #endif
105