kernel/sound: Port MIDI support.
[dragonfly.git] / sys / dev / sound / midi / midiq.h
1 /*-
2  * Copyright (c) 2003 Mathew Kanner
3  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: head/sys/dev/sound/midi/midiq.h 166971 2007-02-25 13:51:52Z netchild $
27  */
28
29 #ifndef MIDIQ_H
30 #define MIDIQ_H
31
32 #define MIDIQ_MOVE(a,b,c) bcopy(b,a,c)
33
34 #define MIDIQ_HEAD(name, type)          \
35 struct name {                           \
36         int h, t, s;                    \
37         type * b;                        \
38 }
39
40 #define MIDIQ_INIT(head, buf, size) do {                \
41         (head).h=(head).t=0;                          \
42         (head).s=size;                                 \
43         (head).b=buf;                                  \
44 } while (0)
45
46 #define MIDIQ_EMPTY(head)       ((head).h == (head).t )
47
48 #define MIDIQ_LENBASE(head)         ((head).h - (head).t < 0 ? \
49                                         (head).h - (head).t + (head).s : \
50                                         (head).h - (head).t)
51
52 #define MIDIQ_FULL(head)        ((head).h == -1)
53 #define MIDIQ_AVAIL(head)       (MIDIQ_FULL(head) ? 0 : (head).s - MIDIQ_LENBASE(head))
54 #define MIDIQ_LEN(head)         ((head).s - MIDIQ_AVAIL(head))
55 #define MIDIQ_DEBUG 0
56 /*
57  * No protection against overflow, underflow
58  */
59 #define MIDIQ_ENQ(head, buf, size) do {                                                                 \
60                 if(MIDIQ_DEBUG)\
61                         kprintf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n",            \
62                                &(head).b[(head).h], (buf),                                        \
63                                (intmax_t)(sizeof(*(head).b) *                                     \
64                                           MIN( (size), (head).s - (head).h) ),                   \
65                                (size), (head).h, (head).t);               \
66                 MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * MIN((size), (head).s - (head).h));                       \
67                 if( (head).s - (head).h < (size) ) {                                                    \
68                         if(MIDIQ_DEBUG) \
69                                 kprintf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).h, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
70                         MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
71                 } \
72                 (head).h+=(size);                                                                         \
73                 (head).h%=(head).s;                                                                     \
74                 if(MIDIQ_EMPTY(head)) (head).h=-1; \
75                 if(MIDIQ_DEBUG)\
76                         kprintf("#E h %d t %d\n", (head).h, (head).t);                       \
77 } while (0)
78
79 #define MIDIQ_DEQ_I(head, buf, size, move, update) do {                                                                 \
80                 if(MIDIQ_FULL(head)) (head).h=(head).t; \
81                 if(MIDIQ_DEBUG)\
82                         kprintf("#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);                       \
83                 if (move) MIDIQ_MOVE((buf), &(head).b[(head).t], sizeof(*(head).b) * MIN((size), (head).s - (head).t));                       \
84                 if( (head).s - (head).t < (size) ) {                                                    \
85                         if(MIDIQ_DEBUG) \
86                                 kprintf("#2 %p %p bytes copied %jd\n",  (head).b, (buf) + (head).s - (head).t, (intmax_t)sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
87                         if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
88                 } \
89                 if (update) { \
90                 (head).t+=(size);                                                                         \
91                 (head).t%=(head).s;                                                                     \
92                 } else { \
93                   if (MIDIQ_EMPTY(head)) (head).h=-1; \
94                 } \
95                 if(MIDIQ_DEBUG)\
96                         kprintf("#E h %d t %d\n", (head).h, (head).t);                       \
97 } while (0)
98
99 #define MIDIQ_SIZE(head) ((head).s)
100 #define MIDIQ_CLEAR(head) ((head).h = (head).t = 0)
101 #define MIDIQ_BUF(head) ((head).b)
102 #define MIDIQ_DEQ(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 1)
103 #define MIDIQ_PEEK(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 0)
104 #define MIDIQ_POP(head, size) MIDIQ_DEQ_I(head, &head, size, 0, 1)
105
106 #endif