1158979Snetchild/*-
2166322Sjoel * Copyright (c) 2003 Mathew Kanner
3166322Sjoel * All rights reserved.
4166322Sjoel *
5158979Snetchild * Redistribution and use in source and binary forms, with or without
6166322Sjoel * modification, are permitted provided that the following conditions
7166322Sjoel * are met:
8166322Sjoel * 1. Redistributions of source code must retain the above copyright
9166322Sjoel *    notice, this list of conditions and the following disclaimer.
10166322Sjoel * 2. Redistributions in binary form must reproduce the above copyright
11166322Sjoel *    notice, this list of conditions and the following disclaimer in the
12166322Sjoel *    documentation and/or other materials provided with the distribution.
13166322Sjoel *
14166322Sjoel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15166322Sjoel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16166322Sjoel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17166322Sjoel * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18166322Sjoel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19166322Sjoel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20166322Sjoel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21166322Sjoel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22158979Snetchild * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23158979Snetchild * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24158979Snetchild * SUCH DAMAGE.
25158979Snetchild *
26158979Snetchild * $FreeBSD$
27158979Snetchild */
28158979Snetchild
29158979Snetchild#ifndef MIDIQ_H
30158979Snetchild#define MIDIQ_H
31158979Snetchild
32158979Snetchild#define MIDIQ_MOVE(a,b,c) bcopy(b,a,c)
33158979Snetchild
34158979Snetchild#define MIDIQ_HEAD(name, type)          \
35158979Snetchildstruct name {                           \
36158979Snetchild        int h, t, s;                    \
37158979Snetchild        type * b;                        \
38166971Snetchild}
39158979Snetchild
40158979Snetchild#define MIDIQ_INIT(head, buf, size) do {                \
41158979Snetchild        (head).h=(head).t=0;                          \
42158979Snetchild        (head).s=size;                                 \
43158979Snetchild        (head).b=buf;                                  \
44158979Snetchild} while (0)
45158979Snetchild
46158979Snetchild#define MIDIQ_EMPTY(head)       ((head).h == (head).t )
47158979Snetchild
48158979Snetchild#define MIDIQ_LENBASE(head)         ((head).h - (head).t < 0 ? \
49158979Snetchild                                        (head).h - (head).t + (head).s : \
50158979Snetchild                                        (head).h - (head).t)
51158979Snetchild
52158979Snetchild#define MIDIQ_FULL(head)        ((head).h == -1)
53158979Snetchild#define MIDIQ_AVAIL(head)       (MIDIQ_FULL(head) ? 0 : (head).s - MIDIQ_LENBASE(head))
54158979Snetchild#define MIDIQ_LEN(head)		((head).s - MIDIQ_AVAIL(head))
55158979Snetchild#define MIDIQ_DEBUG 0
56158979Snetchild/*
57158979Snetchild * No protection against overflow, underflow
58158979Snetchild */
59158979Snetchild#define MIDIQ_ENQ(head, buf, size) do {                                                                 \
60158979Snetchild		if(MIDIQ_DEBUG)\
61158979Snetchild                	printf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n",            \
62158979Snetchild			       &(head).b[(head).h], (buf),                                        \
63158979Snetchild			       (intmax_t)(sizeof(*(head).b) *                                     \
64158979Snetchild					  MIN( (size), (head).s - (head).h) ),                   \
65158979Snetchild			       (size), (head).h, (head).t);               \
66158979Snetchild                MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * MIN((size), (head).s - (head).h));                       \
67158979Snetchild                if( (head).s - (head).h < (size) ) {                                                    \
68158979Snetchild			if(MIDIQ_DEBUG) \
69158979Snetchild                        	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) );      \
70158979Snetchild                        MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) );      \
71158979Snetchild		} \
72158979Snetchild                (head).h+=(size);                                                                         \
73158979Snetchild                (head).h%=(head).s;                                                                     \
74158979Snetchild		if(MIDIQ_EMPTY(head)) (head).h=-1; \
75158979Snetchild		if(MIDIQ_DEBUG)\
76158979Snetchild                	printf("#E h %d t %d\n", (head).h, (head).t);                       \
77158979Snetchild} while (0)
78158979Snetchild
79158979Snetchild#define MIDIQ_DEQ_I(head, buf, size, move, update) do {                                                                 \
80158979Snetchild		if(MIDIQ_FULL(head)) (head).h=(head).t; \
81158979Snetchild		if(MIDIQ_DEBUG)\
82158979Snetchild                	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);                       \
83158979Snetchild                if (move) MIDIQ_MOVE((buf), &(head).b[(head).t], sizeof(*(head).b) * MIN((size), (head).s - (head).t));                       \
84158979Snetchild                if( (head).s - (head).t < (size) ) {                                                    \
85158979Snetchild			if(MIDIQ_DEBUG) \
86158979Snetchild                        	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) );      \
87158979Snetchild                        if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) );      \
88158979Snetchild		} \
89158979Snetchild		if (update) { \
90158979Snetchild                (head).t+=(size);                                                                         \
91158979Snetchild                (head).t%=(head).s;                                                                     \
92158979Snetchild		} else { \
93158979Snetchild		  if (MIDIQ_EMPTY(head)) (head).h=-1; \
94158979Snetchild		} \
95158979Snetchild		if(MIDIQ_DEBUG)\
96158979Snetchild                	printf("#E h %d t %d\n", (head).h, (head).t);                       \
97158979Snetchild} while (0)
98158979Snetchild
99158979Snetchild#define MIDIQ_SIZE(head) ((head).s)
100158979Snetchild#define MIDIQ_CLEAR(head) ((head).h = (head).t = 0)
101158979Snetchild#define MIDIQ_BUF(head) ((head).b)
102158979Snetchild#define MIDIQ_DEQ(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 1)
103158979Snetchild#define MIDIQ_PEEK(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 0)
104158979Snetchild#define MIDIQ_POP(head, size) MIDIQ_DEQ_I(head, &head, size, 0, 1)
105158979Snetchild
106158979Snetchild#endif
107