midiq.h revision 166971
1189251Ssam/*- 2189251Ssam * Copyright (c) 2003 Mathew Kanner 3189251Ssam * All rights reserved. 4189251Ssam * 5189251Ssam * Redistribution and use in source and binary forms, with or without 6189251Ssam * modification, are permitted provided that the following conditions 7189251Ssam * are met: 8189251Ssam * 1. Redistributions of source code must retain the above copyright 9189251Ssam * notice, this list of conditions and the following disclaimer. 10189251Ssam * 2. Redistributions in binary form must reproduce the above copyright 11189251Ssam * notice, this list of conditions and the following disclaimer in the 12189251Ssam * documentation and/or other materials provided with the distribution. 13189251Ssam * 14189251Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15189251Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16189251Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17189251Ssam * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18189251Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19189251Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20189251Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21189251Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22189251Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23189251Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24189251Ssam * SUCH DAMAGE. 25189251Ssam * 26189251Ssam * $FreeBSD: head/sys/dev/sound/midi/midiq.h 166971 2007-02-25 13:51:52Z netchild $ 27189251Ssam */ 28189251Ssam 29189251Ssam#ifndef MIDIQ_H 30189251Ssam#define MIDIQ_H 31189251Ssam 32189251Ssam#define MIDIQ_MOVE(a,b,c) bcopy(b,a,c) 33189251Ssam 34189251Ssam#define MIDIQ_HEAD(name, type) \ 35189251Ssamstruct name { \ 36189251Ssam int h, t, s; \ 37189251Ssam type * b; \ 38189251Ssam} 39189251Ssam 40189251Ssam#define MIDIQ_INIT(head, buf, size) do { \ 41189251Ssam (head).h=(head).t=0; \ 42189251Ssam (head).s=size; \ 43189251Ssam (head).b=buf; \ 44189251Ssam} while (0) 45189251Ssam 46189251Ssam#define MIDIQ_EMPTY(head) ((head).h == (head).t ) 47189251Ssam 48189251Ssam#define MIDIQ_LENBASE(head) ((head).h - (head).t < 0 ? \ 49189251Ssam (head).h - (head).t + (head).s : \ 50189251Ssam (head).h - (head).t) 51189251Ssam 52189251Ssam#define MIDIQ_FULL(head) ((head).h == -1) 53189251Ssam#define MIDIQ_AVAIL(head) (MIDIQ_FULL(head) ? 0 : (head).s - MIDIQ_LENBASE(head)) 54189251Ssam#define MIDIQ_LEN(head) ((head).s - MIDIQ_AVAIL(head)) 55189251Ssam#define MIDIQ_DEBUG 0 56189251Ssam/* 57189251Ssam * No protection against overflow, underflow 58189251Ssam */ 59189251Ssam#define MIDIQ_ENQ(head, buf, size) do { \ 60189251Ssam if(MIDIQ_DEBUG)\ 61189251Ssam printf("#1 %p %p bytes copied %jd tran req s %d h %d t %d\n", \ 62189251Ssam &(head).b[(head).h], (buf), \ 63189251Ssam (intmax_t)(sizeof(*(head).b) * \ 64189251Ssam MIN( (size), (head).s - (head).h) ), \ 65189251Ssam (size), (head).h, (head).t); \ 66189251Ssam MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * MIN((size), (head).s - (head).h)); \ 67189251Ssam if( (head).s - (head).h < (size) ) { \ 68189251Ssam if(MIDIQ_DEBUG) \ 69189251Ssam 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) ); \ 70189251Ssam MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) ); \ 71189251Ssam } \ 72189251Ssam (head).h+=(size); \ 73189251Ssam (head).h%=(head).s; \ 74189251Ssam if(MIDIQ_EMPTY(head)) (head).h=-1; \ 75189251Ssam if(MIDIQ_DEBUG)\ 76189251Ssam printf("#E h %d t %d\n", (head).h, (head).t); \ 77189251Ssam} while (0) 78189251Ssam 79189251Ssam#define MIDIQ_DEQ_I(head, buf, size, move, update) do { \ 80189251Ssam if(MIDIQ_FULL(head)) (head).h=(head).t; \ 81189251Ssam if(MIDIQ_DEBUG)\ 82189251Ssam 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); \ 83189251Ssam if (move) MIDIQ_MOVE((buf), &(head).b[(head).t], sizeof(*(head).b) * MIN((size), (head).s - (head).t)); \ 84189251Ssam if( (head).s - (head).t < (size) ) { \ 85189251Ssam if(MIDIQ_DEBUG) \ 86189251Ssam 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) ); \ 87189251Ssam if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) ); \ 88189251Ssam } \ 89189251Ssam if (update) { \ 90189251Ssam (head).t+=(size); \ 91189251Ssam (head).t%=(head).s; \ 92189251Ssam } else { \ 93189251Ssam if (MIDIQ_EMPTY(head)) (head).h=-1; \ 94189251Ssam } \ 95189251Ssam if(MIDIQ_DEBUG)\ 96189251Ssam printf("#E h %d t %d\n", (head).h, (head).t); \ 97189251Ssam} while (0) 98189251Ssam 99189251Ssam#define MIDIQ_SIZE(head) ((head).s) 100189251Ssam#define MIDIQ_CLEAR(head) ((head).h = (head).t = 0) 101189251Ssam#define MIDIQ_BUF(head) ((head).b) 102189251Ssam#define MIDIQ_DEQ(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 1) 103189251Ssam#define MIDIQ_PEEK(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 0) 104189251Ssam#define MIDIQ_POP(head, size) MIDIQ_DEQ_I(head, &head, size, 0, 1) 105189251Ssam 106189251Ssam#endif 107189251Ssam