make file i/o able to cross fifo bundary

This commit is contained in:
Alexandre Ratchov 2012-11-03 17:30:30 +01:00
parent 5fddbed369
commit 5142109f22
11 changed files with 79 additions and 94 deletions

View File

@ -43,9 +43,9 @@ abuf_log(struct abuf *buf)
#endif #endif
void void
abuf_init(struct abuf *buf, unsigned int len) abuf_init(struct abuf *buf, unsigned int len, char *tag)
{ {
buf->data = xmalloc(len); buf->data = xmalloc(len, tag);
buf->len = len; buf->len = len;
buf->used = 0; buf->used = 0;
buf->start = 0; buf->start = 0;

View File

@ -24,7 +24,7 @@ struct abuf {
unsigned char *data; unsigned char *data;
}; };
void abuf_init(struct abuf *, unsigned int); void abuf_init(struct abuf *, unsigned int, char *);
void abuf_done(struct abuf *); void abuf_done(struct abuf *);
void abuf_log(struct abuf *); void abuf_log(struct abuf *);
unsigned char *abuf_rgetblk(struct abuf *, int *); unsigned char *abuf_rgetblk(struct abuf *, int *);

View File

@ -31,7 +31,7 @@ extern unsigned int log_level;
/* /*
* MIDI buffer size * MIDI buffer size
*/ */
#define MIDI_BUFSZ 125 /* 1/25 second at 31.25kbit/s */ #define MIDI_BUFSZ 125 /* 1 second at 31.25kbit/s */
/* /*
* units used for MTC clock. * units used for MTC clock.

View File

@ -974,7 +974,7 @@ dev_new(char *path, struct aparams *par,
log_puts("too many devices\n"); log_puts("too many devices\n");
return NULL; return NULL;
} }
d = xmalloc(sizeof(struct dev)); d = xmalloc(sizeof(struct dev), "dev");
d->num = dev_sndnum++; d->num = dev_sndnum++;
d->midi = midi_new(&dev_midiops, d, MODE_MIDIIN | MODE_MIDIOUT); d->midi = midi_new(&dev_midiops, d, MODE_MIDIIN | MODE_MIDIOUT);
midi_tag(d->midi, d->num); midi_tag(d->midi, d->num);
@ -1057,14 +1057,16 @@ dev_open(struct dev *d)
/* /*
* Create device <-> demuxer buffer * Create device <-> demuxer buffer
*/ */
d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t)); d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t),
"dev_rbuf");
/* /*
* Insert a converter, if needed. * Insert a converter, if needed.
*/ */
if (!aparams_native(&d->par)) { if (!aparams_native(&d->par)) {
dec_init(&d->dec, &d->par, d->rchan); dec_init(&d->dec, &d->par, d->rchan);
d->decbuf = xmalloc(d->round * d->rchan * d->par.bps); d->decbuf = xmalloc(d->round * d->rchan * d->par.bps,
"dev_dec");
} else } else
d->decbuf = NULL; d->decbuf = NULL;
} }
@ -1072,7 +1074,7 @@ dev_open(struct dev *d)
/* /*
* Create device <-> mixer buffer * Create device <-> mixer buffer
*/ */
d->pbuf = xmalloc(d->bufsz * d->pchan * sizeof(adata_t)); d->pbuf = xmalloc(d->bufsz * d->pchan * sizeof(adata_t), "dev_pbuf");
d->poffs = 0; d->poffs = 0;
d->mode |= MODE_MON; d->mode |= MODE_MON;
@ -1081,7 +1083,7 @@ dev_open(struct dev *d)
*/ */
if (!aparams_native(&d->par)) { if (!aparams_native(&d->par)) {
enc_init(&d->enc, &d->par, d->pchan); enc_init(&d->enc, &d->par, d->pchan);
d->encbuf = xmalloc(d->round * d->pchan * d->par.bps); d->encbuf = xmalloc(d->round * d->pchan * d->par.bps, "dev_enc");
} else } else
d->encbuf = NULL; d->encbuf = NULL;
} }
@ -1683,13 +1685,13 @@ slot_attach(struct slot *s)
if (!aparams_native(&s->par)) { if (!aparams_native(&s->par)) {
dec_init(&s->mix.dec, &s->par, slot_nch); dec_init(&s->mix.dec, &s->par, slot_nch);
s->mix.decbuf = s->mix.decbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t)); xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_mix_dec");
} }
if (s->rate != d->rate) { if (s->rate != d->rate) {
resamp_init(&s->mix.resamp, s->round, d->round, resamp_init(&s->mix.resamp, s->round, d->round,
slot_nch); slot_nch);
s->mix.resampbuf = s->mix.resampbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t)); xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_mix_resamp");
} }
#ifdef DEBUG #ifdef DEBUG
if (log_level >= 3) { if (log_level >= 3) {
@ -1738,12 +1740,12 @@ slot_attach(struct slot *s)
resamp_init(&s->sub.resamp, d->round, s->round, resamp_init(&s->sub.resamp, d->round, s->round,
slot_nch); slot_nch);
s->sub.resampbuf = s->sub.resampbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t)); xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_sub_resamp");
} }
if (!aparams_native(&s->par)) { if (!aparams_native(&s->par)) {
enc_init(&s->sub.enc, &s->par, slot_nch); enc_init(&s->sub.enc, &s->par, slot_nch);
s->sub.encbuf = s->sub.encbuf =
xmalloc(d->round * slot_nch * sizeof(adata_t)); xmalloc(d->round * slot_nch * sizeof(adata_t), "slot_sub_enc");
} }
#ifdef DEBUG #ifdef DEBUG
@ -1818,7 +1820,7 @@ slot_start(struct slot *s)
#endif #endif
s->mix.bpf = s->par.bps * s->mix.bpf = s->par.bps *
(s->mix.slot_cmax - s->mix.slot_cmin + 1); (s->mix.slot_cmax - s->mix.slot_cmin + 1);
abuf_init(&s->mix.buf, bufsz * s->mix.bpf); abuf_init(&s->mix.buf, bufsz * s->mix.bpf, "slot_mix");
} }
if (s->mode & MODE_RECMASK) { if (s->mode & MODE_RECMASK) {
#ifdef DEBUG #ifdef DEBUG
@ -1833,7 +1835,7 @@ slot_start(struct slot *s)
#endif #endif
s->sub.bpf = s->par.bps * s->sub.bpf = s->par.bps *
(s->sub.slot_cmax - s->sub.slot_cmin + 1); (s->sub.slot_cmax - s->sub.slot_cmin + 1);
abuf_init(&s->sub.buf, bufsz * s->sub.bpf); abuf_init(&s->sub.buf, bufsz * s->sub.bpf, "slot_sub");
} }
s->mix.weight = MIDI_TO_ADATA(MIDI_MAXCTL); s->mix.weight = MIDI_TO_ADATA(MIDI_MAXCTL);
#ifdef DEBUG #ifdef DEBUG

View File

@ -230,7 +230,7 @@ file_new(struct fileops *ops, void *arg, char *name, unsigned int nfds)
#endif #endif
return NULL; return NULL;
} }
f = xmalloc(sizeof(struct file)); f = xmalloc(sizeof(struct file), "file");
f->nfds = nfds; f->nfds = nfds;
f->ops = ops; f->ops = ops;
f->arg = arg; f->arg = arg;

View File

@ -109,11 +109,11 @@ listen_new_un(char *path)
goto bad_close; goto bad_close;
} }
umask(oldumask); umask(oldumask);
f = xmalloc(sizeof(struct listen)); f = xmalloc(sizeof(struct listen), "listen_un");
f->file = file_new(&listen_fileops, f, path, 1); f->file = file_new(&listen_fileops, f, path, 1);
if (f->file == NULL) if (f->file == NULL)
goto bad_close; goto bad_close;
f->path = xstrdup(path); f->path = xstrdup(path, "listen_path");
if (f->path == NULL) { if (f->path == NULL) {
perror("strdup"); perror("strdup");
exit(1); exit(1);
@ -174,7 +174,7 @@ listen_new_tcp(char *addr, unsigned int port)
perror("listen"); perror("listen");
goto bad_close; goto bad_close;
} }
f = xmalloc(sizeof(struct listen)); f = xmalloc(sizeof(struct listen), "listen_tcp");
f->file = file_new(&listen_fileops, f, addr, 1); f->file = file_new(&listen_fileops, f, addr, 1);
if (f == NULL) { if (f == NULL) {
bad_close: bad_close:

View File

@ -84,8 +84,10 @@ midi_ontimo(void *arg)
for (i = MIDI_NEP, ep = midi_ep; i > 0; i--, ep++) { for (i = MIDI_NEP, ep = midi_ep; i > 0; i--, ep++) {
ep->tickets = MIDI_XFER; ep->tickets = MIDI_XFER;
if (ep->ibuf.used > 0) if (ep->ibuf.used > 0) {
midi_in(ep); while (midi_in(ep))
; /* nothing */
}
} }
timo_add(&midi_timo, MIDI_TIMO); timo_add(&midi_timo, MIDI_TIMO);
} }
@ -130,10 +132,10 @@ midi_new(struct midiops *ops, void *arg, int mode)
* to client input * to client input
*/ */
if (ep->mode & MODE_MIDIOUT) { if (ep->mode & MODE_MIDIOUT) {
abuf_init(&ep->ibuf, MIDI_BUFSZ); abuf_init(&ep->ibuf, MIDI_BUFSZ, "midi_ibuf");
} }
if (ep->mode & MODE_MIDIIN) { if (ep->mode & MODE_MIDIIN) {
abuf_init(&ep->obuf, MIDI_BUFSZ); abuf_init(&ep->obuf, MIDI_BUFSZ, "midi_obuf");
} }
return ep; return ep;
} }
@ -252,9 +254,9 @@ midi_in(struct midi *ep)
return 0; return 0;
} }
idata = abuf_rgetblk(&ep->ibuf, &icount); idata = abuf_rgetblk(&ep->ibuf, &icount);
if (icount > ep->tickets) //if (icount > ep->tickets)
icount = ep->tickets; // icount = ep->tickets;
ep->tickets -= icount; //ep->tickets -= icount;
#ifdef DEBUG #ifdef DEBUG
if (log_level >= 4) { if (log_level >= 4) {
midi_log(ep); midi_log(ep);
@ -404,7 +406,7 @@ port_new(char *path, unsigned int mode)
{ {
struct port *c; struct port *c;
c = xmalloc(sizeof(struct port)); c = xmalloc(sizeof(struct port), "port");
c->path = path; c->path = path;
c->state = PORT_CFG; c->state = PORT_CFG;
c->midi = midi_new(&port_midiops, c, mode); c->midi = midi_new(&port_midiops, c, mode);

View File

@ -99,7 +99,8 @@ port_mio_in(void *arg)
if (n == 0) if (n == 0)
break; break;
abuf_wcommit(&ep->ibuf, n); abuf_wcommit(&ep->ibuf, n);
midi_in(ep); while (midi_in(ep))
; /* nothing */
if (n < count) if (n < count)
break; break;
} }

View File

@ -46,7 +46,7 @@ opt_new(char *name, struct dev *dev,
exit(1); exit(1);
} }
} }
o = xmalloc(sizeof(struct opt)); o = xmalloc(sizeof(struct opt), "opt");
if (mode & MODE_PLAY) { if (mode & MODE_PLAY) {
o->pmin = pmin; o->pmin = pmin;
o->pmax = pmax; o->pmax = pmax;

View File

@ -281,7 +281,7 @@ sock_new(int fd)
{ {
struct sock *f; struct sock *f;
f = xmalloc(sizeof(struct sock)); f = xmalloc(sizeof(struct sock), "sock");
f->pstate = SOCK_AUTH; f->pstate = SOCK_AUTH;
f->opt = NULL; f->opt = NULL;
f->slot = NULL; f->slot = NULL;
@ -521,32 +521,16 @@ sock_rdata(struct sock *f)
buf = &f->slot->mix.buf; buf = &f->slot->mix.buf;
else else
buf = &f->midi->ibuf; buf = &f->midi->ibuf;
data = abuf_wgetblk(buf, &count) + f->rsize - f->rtodo; while (f->rtodo > 0) {
#ifdef DEBUG data = abuf_wgetblk(buf, &count);
/* if (count > f->rtodo)
* XXX: this can happen in MIDIOUT mode, since we dont count = f->rtodo;
* have flow control n = sock_fdread(f, data, count);
*/ if (n == 0)
if (count < f->rtodo) { return 0;
sock_log(f);
log_puts(": data read buffer overrun\n");
panic();
}
#endif
n = sock_fdread(f, data, f->rtodo);
if (n == 0)
return 0;
if (n < f->rtodo) {
f->rtodo -= n; f->rtodo -= n;
return 0; abuf_wcommit(buf, n);
} }
/*
* XXX: commit data earlier, don't sacrify a full block in case
* of xrun
*/
abuf_wcommit(buf, f->rsize);
f->rtodo = 0;
#ifdef DEBUG #ifdef DEBUG
if (log_level >= 4) { if (log_level >= 4) {
sock_log(f); sock_log(f);
@ -556,18 +540,9 @@ sock_rdata(struct sock *f)
if (f->slot) if (f->slot)
slot_write(f->slot); slot_write(f->slot);
if (f->midi) { if (f->midi) {
midi_in(f->midi); while (midi_in(f->midi))
#ifdef DEBUG ; /* nothing */
if (f->midi->ibuf.used > 0) { f->fillpending += f->midi->ibuf.len - f->midi->ibuf.used;
if (log_level >= 1) {
sock_log(f);
log_puts(": midi buffer not emptied\n");
}
panic();
}
#endif
f->midi->ibuf.start = 0;
f->fillpending += f->rsize;
} }
return 1; return 1;
} }
@ -591,31 +566,37 @@ sock_wdata(struct sock *f)
panic(); panic();
} }
#endif #endif
if (f->slot) { if (f->pstate == SOCK_STOP) {
buf = &f->slot->sub.buf; while (f->wtodo > 0) {
if (f->pstate == SOCK_STOP) n = sock_fdwrite(f, dummy, f->wtodo);
data = dummy; if (n == 0)
else { return 0;
data = abuf_rgetblk(buf, &count) + f->wsize - f->wtodo; f->wtodo -= n;
} }
#ifdef DEBUG
if (log_level >= 4) {
sock_log(f);
log_puts(": zero-filled remaining block\n");
}
#endif
return 1;
} }
if (f->midi) { if (f->slot)
buf = &f->slot->sub.buf;
else
buf = &f->midi->obuf; buf = &f->midi->obuf;
data = abuf_rgetblk(buf, &count) + f->wsize - f->wtodo; while (f->wtodo > 0) {
} data = abuf_rgetblk(buf, &count);
n = sock_fdwrite(f, data, f->wtodo); if (count > f->wtodo)
if (n == 0) count = f->wtodo;
return 0; n = sock_fdwrite(f, data, f->wtodo);
if (n < f->wtodo) { if (n == 0)
return 0;
f->wtodo -= n; f->wtodo -= n;
return 0; abuf_rdiscard(buf, n);
} }
if (f->pstate != SOCK_STOP) { if (f->slot)
abuf_rdiscard(buf, f->wsize); slot_read(f->slot);
if (f->slot)
slot_read(f->slot);
}
f->wtodo = 0;
#ifdef DEBUG #ifdef DEBUG
if (log_level >= 4) { if (log_level >= 4) {
sock_log(f); sock_log(f);
@ -877,6 +858,7 @@ sock_hello(struct sock *f)
#endif #endif
return 0; return 0;
} }
f->pstate = SOCK_INIT;
if (mode & MODE_MIDIMASK) { if (mode & MODE_MIDIMASK) {
f->slot = NULL; f->slot = NULL;
f->midi = midi_new(&sock_midiops, f, mode); f->midi = midi_new(&sock_midiops, f, mode);
@ -945,7 +927,6 @@ sock_hello(struct sock *f)
s->mix.maxweight = f->opt->maxweight; s->mix.maxweight = f->opt->maxweight;
s->dup = f->opt->dup; s->dup = f->opt->dup;
/* XXX: must convert to slot rate */ /* XXX: must convert to slot rate */
f->pstate = SOCK_INIT;
f->slot = s; f->slot = s;
return 1; return 1;
} }
@ -1401,9 +1382,8 @@ sock_buildmsg(struct sock *f)
} }
if (f->midi != NULL && f->midi->obuf.used > 0) { if (f->midi != NULL && f->midi->obuf.used > 0) {
size = f->midi->obuf.len - f->midi->obuf.start; /* XXX: use tickets */
if (size > f->midi->obuf.used) size = f->midi->obuf.used;
size = f->midi->obuf.used;
if (size > AMSG_DATAMAX) if (size > AMSG_DATAMAX)
size = AMSG_DATAMAX; size = AMSG_DATAMAX;
AMSG_INIT(&f->wmsg); AMSG_INIT(&f->wmsg);
@ -1418,7 +1398,6 @@ sock_buildmsg(struct sock *f)
* If data available, build a DATA message. * If data available, build a DATA message.
*/ */
if (f->slot != NULL && f->slot->sub.buf.used > 0 && f->wmax > 0) { if (f->slot != NULL && f->slot->sub.buf.used > 0 && f->wmax > 0) {
/* XXX: round to bpf */
size = f->slot->sub.buf.used; size = f->slot->sub.buf.used;
if (size > AMSG_DATAMAX) if (size > AMSG_DATAMAX)
size = AMSG_DATAMAX; size = AMSG_DATAMAX;
@ -1426,6 +1405,7 @@ sock_buildmsg(struct sock *f)
size = f->walign; size = f->walign;
if (size > f->wmax) if (size > f->wmax)
size = f->wmax; size = f->wmax;
size -= size % f->slot->sub.bpf;
#ifdef DEBUG #ifdef DEBUG
if (size == 0) { if (size == 0) {
sock_log(f); sock_log(f);

View File

@ -32,9 +32,9 @@ char *mem_strdup(const char *, char *);
void mem_free(void *); void mem_free(void *);
void mem_stats(void); void mem_stats(void);
#define xmalloc(s) (mem_alloc((s), (char *)__func__)) #define xmalloc(size, tag) (mem_alloc((size), (tag)))
#define xstrdup(s) (mem_strdup((s), (char *)__func__)) #define xstrdup(str, tag) (mem_strdup((str), (tag)))
#define xfree(p) (mem_free((p))) #define xfree(ptr) (mem_free(ptr))
void memrnd(void *, size_t); void memrnd(void *, size_t);