#ifndef MIDIQ_H
#define MIDIQ_H
#define MIDIQ_MOVE(a,b,c) bcopy(b,a,c)
#define MIDIQ_HEAD(name, type) \
struct name { \
int h, t, s; \
type * b; \
}
#define MIDIQ_INIT(head, buf, size) do { \
(head).h=(head).t=0; \
(head).s=size; \
(head).b=buf; \
} while (0)
#define MIDIQ_EMPTY(head) ((head).h == (head).t )
#define MIDIQ_LENBASE(head) ((head).h - (head).t < 0 ? \
(head).h - (head).t + (head).s : \
(head).h - (head).t)
#define MIDIQ_FULL(head) ((head).h == -1)
#define MIDIQ_AVAIL(head) (MIDIQ_FULL(head) ? 0 : (head).s - MIDIQ_LENBASE(head))
#define MIDIQ_LEN(head) ((head).s - MIDIQ_AVAIL(head))
#define MIDIQ_ENQ(head, buf, size) do { \
MIDIQ_MOVE(&(head).b[(head).h], (buf), sizeof(*(head).b) * min((size), (head).s - (head).h)); \
if( (head).s - (head).h < (size) ) { \
MIDIQ_MOVE((head).b, (buf) + (head).s - (head).h, sizeof(*(head).b) * ((size) - (head).s + (head).h) ); \
} \
(head).h+=(size); \
(head).h%=(head).s; \
if(MIDIQ_EMPTY(head)) (head).h=-1; \
} while (0)
#define MIDIQ_DEQ_I(head, buf, size, move, update) do { \
if(MIDIQ_FULL(head)) (head).h=(head).t; \
if (move) MIDIQ_MOVE((buf), &(head).b[(head).t], sizeof(*(head).b) * min((size), (head).s - (head).t)); \
if( (head).s - (head).t < (size) ) { \
if (move) MIDIQ_MOVE((buf) + (head).s - (head).t, (head).b, sizeof(*(head).b) * ((size) - (head).s + (head).t) ); \
} \
if (update) { \
(head).t+=(size); \
(head).t%=(head).s; \
} else { \
if (MIDIQ_EMPTY(head)) (head).h=-1; \
} \
} while (0)
#define MIDIQ_SIZE(head) ((head).s)
#define MIDIQ_CLEAR(head) ((head).h = (head).t = 0)
#define MIDIQ_BUF(head) ((head).b)
#define MIDIQ_DEQ(head, buf, size) MIDIQ_DEQ_I(head, buf, size, 1, 1)
#endif