midiq.h revision 166322
1272343Sngie/*-
2272343Sngie * Copyright (c) 2003 Mathew Kanner
3272343Sngie * All rights reserved.
4272343Sngie *
5272343Sngie * Redistribution and use in source and binary forms, with or without
6272343Sngie * modification, are permitted provided that the following conditions
7272343Sngie * are met:
8272343Sngie * 1. Redistributions of source code must retain the above copyright
9272343Sngie *    notice, this list of conditions and the following disclaimer.
10272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer in the
12272343Sngie *    documentation and/or other materials provided with the distribution.
13272343Sngie *
14272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17272343Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24272343Sngie * SUCH DAMAGE.
25272343Sngie *
26272343Sngie * $FreeBSD: head/sys/dev/sound/midi/midiq.h 166322 2007-01-28 20:38:07Z joel $
27272343Sngie */
28272343Sngie
29272343Sngie#ifndef MIDIQ_H
30272343Sngie#define MIDIQ_H
31272343Sngie
32272343Sngie#define MIDIQ_MOVE(a,b,c) bcopy(b,a,c)
33272343Sngie
34272343Sngie#define MIDIQ_HEAD(name, type)          \
35272343Sngiestruct name {                           \
36272343Sngie        int h, t, s;                    \
37272343Sngie        type * b;                        \
38272343Sngie}
39272343Sngie
40272343Sngie#define MIDIQ_INIT(head, buf, size) do {                \
41272343Sngie        (head).h=(head).t=0;                          \
42272343Sngie        (head).s=size;                                 \
43272343Sngie        (head).b=buf;                                  \
44272343Sngie} while (0)
45272343Sngie
46272343Sngie#define MIDIQ_EMPTY(head)       ((head).h == (head).t )
47272343Sngie
48272343Sngie#define MIDIQ_LENBASE(head)         ((head).h - (head).t < 0 ? \
49272343Sngie                                        (head).h - (head).t + (head).s : \
50272343Sngie                                        (head).h - (head).t)
51272343Sngie
52272343Sngie#define MIDIQ_FULL(head)        ((head).h == -1)
53272343Sngie#define MIDIQ_AVAIL(head)       (MIDIQ_FULL(head) ? 0 : (head).s - MIDIQ_LENBASE(head))
54272343Sngie#define MIDIQ_LEN(head)		((head).s - MIDIQ_AVAIL(head))
55272343Sngie#define MIDIQ_DEBUG 0
56272343Sngie/*
57272343Sngie * No protection against overflow, underflow
58272343Sngie */
59272343Sngie#define MIDIQ_ENQ(head, buf, size) do {                                                                 \
60272343Sngie		if(MIDIQ_DEBUG)\
61272343Sngie                	printf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n",            \
62272343Sngie			       &(head).b[(head).h], (buf),                                        \
63272343Sngie			       (intmax_t)(sizeof(*(head).b) *                                     \
64272343Sngie					  MIN( (size), (head).s - (head).h) ),                   \
65272343Sngie			       (size), (head).h, (head).t);               \
66272343Sngie                MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * MIN((size), (head).s - (head).h));                       \
67272343Sngie                if( (head).s - (head).h < (size) ) {                                                    \
68272343Sngie			if(MIDIQ_DEBUG) \
69272343Sngie                        	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) );      \
70272343Sngie                        MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
71272343Sngie		} \
72272343Sngie                (head).h+=(size);                                                                         \
73272343Sngie                (head).h%=(head).s;                                                                     \
74272343Sngie		if(MIDIQ_EMPTY(head)) (head).h=-1; \
75272343Sngie		if(MIDIQ_DEBUG)\
76272343Sngie                	printf("#E h %d t %d\n", (head).h, (head).t);                       \
77272343Sngie} while (0)
78272343Sngie
79272343Sngie#define MIDIQ_DEQ_I(head, buf, size, move, update) do {                                                                 \
80272343Sngie		if(MIDIQ_FULL(head)) (head).h=(head).t; \
81272343Sngie		if(MIDIQ_DEBUG)\
82272343Sngie                	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);                       \
83272343Sngie                if (move) MIDIQ_MOVE((buf), &(head).b[(head).t], sizeof(*(head).b) * MIN((size), (head).s - (head).t));                       \
84272343Sngie                if( (head).s - (head).t < (size) ) {                                                    \
85272343Sngie			if(MIDIQ_DEBUG) \
86272343Sngie                        	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) );      \
87272343Sngie                        if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
88272343Sngie		} \
89272343Sngie		if (update) { \
90272343Sngie                (head).t+=(size);                                                                         \
91272343Sngie                (head).t%=(head).s;                                                                     \
92272343Sngie		} else { \
93272343Sngie		  if (MIDIQ_EMPTY(head)) (head).h=-1; \
94272343Sngie		} \
95272343Sngie		if(MIDIQ_DEBUG)\
96272343Sngie                	printf("#E h %d t %d\n", (head).h, (head).t);                       \
97272343Sngie} while (0)
98272343Sngie
99272343Sngie#define MIDIQ_SIZE(head) ((head).s)
100272343Sngie#define MIDIQ_CLEAR(head) ((head).h = (head).t = 0)
101272343Sngie#define MIDIQ_BUF(head) ((head).b)
102272343Sngie#define MIDIQ_DEQ(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 1)
103272343Sngie#define MIDIQ_PEEK(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 0)
104272343Sngie#define MIDIQ_POP(head, size) MIDIQ_DEQ_I(head, &head, size, 0, 1)
105272343Sngie
106272343Sngie#endif
107272343Sngie