2 * Copyright (c) 2003 Mathew Kanner
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: head/sys/dev/sound/midi/midiq.h 166971 2007-02-25 13:51:52Z netchild $
32 #define MIDIQ_MOVE(a,b,c) bcopy(b,a,c)
34 #define MIDIQ_HEAD(name, type) \
40 #define MIDIQ_INIT(head, buf, size) do { \
41 (head).h=(head).t=0; \
46 #define MIDIQ_EMPTY(head) ((head).h == (head).t )
48 #define MIDIQ_LENBASE(head) ((head).h - (head).t < 0 ? \
49 (head).h - (head).t + (head).s : \
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))
57 * No protection against overflow, underflow
59 #define MIDIQ_ENQ(head, buf, size) do { \
61 printf("#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) ) { \
69 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) ); \
70 MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) ); \
74 if(MIDIQ_EMPTY(head)) (head).h=-1; \
76 printf("#E h %d t %d\n", (head).h, (head).t); \
79 #define MIDIQ_DEQ_I(head, buf, size, move, update) do { \
80 if(MIDIQ_FULL(head)) (head).h=(head).t; \
82 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); \
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) ) { \
86 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) ); \
87 if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) ); \
93 if (MIDIQ_EMPTY(head)) (head).h=-1; \
96 printf("#E h %d t %d\n", (head).h, (head).t); \
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)