From nobody Thu Nov 12 16:38:07 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jQQzz3QTSz70sNj for ; Thu, 12 Nov 2026 16:38:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jQQzz236kz4JKF for ; Thu, 12 Nov 2026 16:38:07 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794501487; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yNLShuMSICsTl55bJ6S61OCgxOZ3HFICU1FFQyOvbJ0=; b=DimOjlQqRlQld1yDSsb9hMsTUEXH1ee9zDcwE/FlVEYatUobGGsb1BVwjt3LPUZSzjJktB T+oyQZxn0hKKIrH3ZlGyJdPQPI9toSqif28dbnp5qm/iPSAZY2oabB0e6/gMQ72qhz0r8F fgMpB4DZuFE09cPezOGr1N5icJSB9DuvrdGHJpnkkEsYWu8fmyiUpSlB23dRRacL2aD1P9 g/oNnmRkgpxObEr+kuc6khi5OKzmwAyn4J6/qvmhZz8tko5Eag1XEx3diO61nV4+quqrnt PLen62C+EBsGHqyXodQDJNI2l1Wbc+2SuscBeZhFZQMNXNo9B3rLOQXxlL0Elg== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794501487; b=BfKVmXtIsU2ruzQaqBE89WZUuBCEdrKcvpOQxaIlXZlYGa248UA3afH6tfhr548/X3natH xjahHu2ElJer6gsbCobXBfPrt7cDW9ONsXYZT0YfkmHzxhiXrX48j14a5n3onPF/0Wiyt1 LywWNQDn0r+gqq9RAnav7RHn4fSHE0LJCJjCC6HJ1mI9ShLSUByUdI/tcEsjhRJLS/Mb0R ZabFK7bn1nB6gzFWQRy/iK3CoY6zcK7pJBvBxwqKP65ZdMHFCuQjMdHPLQkd8Oz97mQIoc 7hVxuDjI9ObClyGD7Qvl/6BK4T59HE3oWIMjaR7HbqdHQtTuzaSVlDarCVT9Lg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794501487; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yNLShuMSICsTl55bJ6S61OCgxOZ3HFICU1FFQyOvbJ0=; b=j7BqIRJyrh4P+yB4iYmrlrrb6zny4W3vGOEeJeUM+PJhpeM66mMvOM+nGnF8YNo0qRqrrN zwN5sv6vpoihWQol5GGtBlWJkwAbK75ySPwWz08Xf7gguRxVDNYrq/KXm4rc/+a35H2S5X sV2Qlg3WI/+RwaRV+cLrMqik/hGIpt2vG3aCfOrvI4ZwnLatN8ISRYSNPUYXYZ46pjFLdx k3qPzvO3W20VFsG1OYEI37vI03+AMeTatwHspp2HAn4QD72jKzPapRbA3qn3QmDBc9JJxB pqOR8nl5n76MeNgmfdMWX4rrWeodUcvwc8VSFsOQRkDu1qMOluvUo6/OKn+xFA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jQQzz1jc9z19Pr for ; Thu, 12 Nov 2026 16:38:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3ea9 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 12 Nov 2026 16:38:07 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: d63b11caae6c - main - sound/midi: Replace MIDIQ with a simple byte queue List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d63b11caae6cc2615be31de5660cbc3e2320ddf5 Auto-Submitted: auto-generated Date: Thu, 12 Nov 2026 16:38:07 +0000 Message-Id: <6af5eb6f.3ea9.5c8b3936@gitrepo.freebsd.org> The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=d63b11caae6cc2615be31de5660cbc3e2320ddf5 commit d63b11caae6cc2615be31de5660cbc3e2320ddf5 Author: Christos Margiolis AuthorDate: 2026-11-10 13:36:26 +0000 Commit: Christos Margiolis CommitDate: 2026-11-12 16:35:48 +0000 sound/midi: Replace MIDIQ with a simple byte queue MFC after: 1 week Sponsored by: The FreeBSD Foundation --- sys/dev/sound/midi/midi.c | 90 +++++++++++++++++++++++++++++++--------------- sys/dev/sound/midi/midiq.h | 87 -------------------------------------------- 2 files changed, 62 insertions(+), 115 deletions(-) diff --git a/sys/dev/sound/midi/midi.c b/sys/dev/sound/midi/midi.c index 7364e5ba3e74..5b812af749e3 100644 --- a/sys/dev/sound/midi/midi.c +++ b/sys/dev/sound/midi/midi.c @@ -4,7 +4,7 @@ * Copyright (c) 2003 Mathew Kanner * Copyright (c) 1998 The NetBSD Foundation, Inc. * All rights reserved. - * Copyright (c) 2025 The FreeBSD Foundation + * Copyright (c) 2025-2026 The FreeBSD Foundation * * Portions of this software were developed by Christos Margiolis * under sponsorship from the FreeBSD Foundation. @@ -52,12 +52,17 @@ #endif #include -#include #include "mpu_if.h" MALLOC_DEFINE(M_MIDI, "midi buffers", "Midi data allocation area"); +struct midi_queue { + uint8_t *buf; + int size; + int used; /* Bytes queued. */ +}; + #define MIDI_NAMELEN 16 struct snd_midi { KOBJ_FIELDS; @@ -68,7 +73,7 @@ struct snd_midi { int channel; int flags; /* File flags */ - MIDIQ_HEAD(, char) inq, outq; + struct midi_queue inq, outq; int rchan, wchan; struct selinfo rsel, wsel; int hiwat; /* QLEN(outq)>High-water -> disable @@ -97,6 +102,33 @@ static struct cdevsw midi_cdevsw = { struct unrhdr *dev_unr = NULL; struct unrhdr *chn_unr = NULL; +static int +midi_queue_avail(struct midi_queue *q) +{ + return (q->size - q->used); +} + +static void +midi_queue_enq(struct midi_queue *q, uint8_t *src, int n) +{ + KASSERT(n >= 0 && n <= midi_queue_avail(q), + ("%s: bad length %d", __func__, n)); + + memcpy(q->buf + q->used, src, n); + q->used += n; +} + +static void +midi_queue_deq(struct midi_queue *q, uint8_t *dst, int n) +{ + KASSERT(n >= 0 && n <= q->used, + ("%s: bad length %d", __func__, n)); + + memcpy(dst, q->buf, n); + q->used -= n; + memmove(q->buf, q->buf + n, q->used); +} + /* * Register a new midi device. * @@ -130,8 +162,10 @@ midi_init(kobj_class_t cls, void *cookie) m->hiwat = outqsize / 2; - MIDIQ_INIT(m->inq, ibuf, inqsize); - MIDIQ_INIT(m->outq, obuf, outqsize); + m->inq.buf = ibuf; + m->inq.size = inqsize; + m->outq.buf = obuf; + m->outq.size = outqsize; m->flags = 0; m->unit = alloc_unr(dev_unr); @@ -152,8 +186,8 @@ midi_init(kobj_class_t cls, void *cookie) err2: mtx_destroy(&m->lock); - free(MIDIQ_BUF(m->inq), M_MIDI); - free(MIDIQ_BUF(m->outq), M_MIDI); + free(m->inq.buf, M_MIDI); + free(m->outq.buf, M_MIDI); err1: free(m, M_MIDI); return NULL; @@ -176,8 +210,8 @@ midi_uninit(struct snd_midi *m) destroy_dev(m->dev); free_unr(dev_unr, m->unit); free_unr(chn_unr, m->channel); - free(MIDIQ_BUF(m->inq), M_MIDI); - free(MIDIQ_BUF(m->outq), M_MIDI); + free(m->inq.buf, M_MIDI); + free(m->outq.buf, M_MIDI); mtx_destroy(&m->lock); free(m, M_MIDI); @@ -205,9 +239,9 @@ midi_in(struct snd_midi *m, uint8_t *buf, int size) used = 0; - if (MIDIQ_AVAIL(m->inq) > size) { + if (size <= midi_queue_avail(&m->inq)) { used = size; - MIDIQ_ENQ(m->inq, buf, size); + midi_queue_enq(&m->inq, buf, size); } else { mtx_unlock(&m->lock); return 0; @@ -236,14 +270,14 @@ midi_out(struct snd_midi *m, uint8_t *buf, int size) return (0); } - used = min(size, MIDIQ_LEN(m->outq)); + used = min(size, m->outq.used); if (used) - MIDIQ_DEQ(m->outq, buf, used); - if (MIDIQ_EMPTY(m->outq)) { + midi_queue_deq(&m->outq, buf, used); + if (m->outq.used == 0) { m->flags &= ~M_TXEN; MPU_CALLBACK(m, m->cookie, m->flags); } - if (used && MIDIQ_AVAIL(m->outq) > m->hiwat) { + if (used && midi_queue_avail(&m->outq) > m->hiwat) { if (m->wchan) { wakeup(&m->wchan); m->wchan = 0; @@ -268,7 +302,7 @@ midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td) retval = 0; if (flags & FREAD) { - if (MIDIQ_SIZE(m->inq) == 0) + if (m->inq.size == 0) retval = ENXIO; else if (m->flags & M_RX) retval = EBUSY; @@ -276,7 +310,7 @@ midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td) goto err; } if (flags & FWRITE) { - if (MIDIQ_SIZE(m->outq) == 0) + if (m->outq.size == 0) retval = ENXIO; else if (m->flags & M_TX) retval = EBUSY; @@ -293,7 +327,7 @@ midi_open(struct cdev *i_dev, int flags, int mode, struct thread *td) * Only clear the inq, the outq might still have data to drain * from a previous session */ - MIDIQ_CLEAR(m->inq); + m->inq.used = 0; } if (flags & FWRITE) @@ -350,7 +384,7 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag) struct snd_midi *m = i_dev->si_drv1; int retval; int used; - char buf[MIDI_RSIZE]; + uint8_t buf[MIDI_RSIZE]; retval = EIO; @@ -363,7 +397,7 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag) goto err1; while (uio->uio_resid > 0) { - while (MIDIQ_EMPTY(m->inq)) { + while (m->inq.used == 0) { retval = EWOULDBLOCK; if (ioflag & O_NONBLOCK) goto err1; @@ -387,10 +421,10 @@ midi_read(struct cdev *i_dev, struct uio *uio, int ioflag) * At this point, it is certain that m->inq has data */ - used = min(MIDIQ_LEN(m->inq), uio->uio_resid); + used = min(m->inq.used, uio->uio_resid); used = min(used, MIDI_RSIZE); - MIDIQ_DEQ(m->inq, buf, used); + midi_queue_deq(&m->inq, buf, used); mtx_unlock(&m->lock); retval = uiomove(buf, used, uio); if (retval) @@ -419,7 +453,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag) struct snd_midi *m = i_dev->si_drv1; int retval; int used; - char buf[MIDI_WSIZE]; + uint8_t buf[MIDI_WSIZE]; retval = EIO; if (m == NULL) @@ -431,7 +465,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag) goto err1; while (uio->uio_resid > 0) { - while (MIDIQ_AVAIL(m->outq) == 0) { + while (midi_queue_avail(&m->outq) == 0) { retval = EWOULDBLOCK; if (ioflag & O_NONBLOCK) goto err1; @@ -456,7 +490,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag) * We are certain than data can be placed on the queue */ - used = min(MIDIQ_AVAIL(m->outq), uio->uio_resid); + used = min(midi_queue_avail(&m->outq), uio->uio_resid); used = min(used, MIDI_WSIZE); mtx_unlock(&m->lock); @@ -464,7 +498,7 @@ midi_write(struct cdev *i_dev, struct uio *uio, int ioflag) if (retval) goto err0; mtx_lock(&m->lock); - MIDIQ_ENQ(m->outq, buf, used); + midi_queue_enq(&m->outq, buf, used); /* * Inform the bottom half that data can be written */ @@ -503,13 +537,13 @@ midi_poll(struct cdev *i_dev, int events, struct thread *td) mtx_lock(&m->lock); if (events & (POLLIN | POLLRDNORM)) { - if (!MIDIQ_EMPTY(m->inq)) + if (m->inq.used != 0) revents |= events & (POLLIN | POLLRDNORM); else selrecord(td, &m->rsel); } if (events & (POLLOUT | POLLWRNORM)) { - if (MIDIQ_AVAIL(m->outq) < m->hiwat) + if (midi_queue_avail(&m->outq) < m->hiwat) revents |= events & (POLLOUT | POLLWRNORM); else selrecord(td, &m->wsel); diff --git a/sys/dev/sound/midi/midiq.h b/sys/dev/sound/midi/midiq.h deleted file mode 100644 index 8ffa4a40d23d..000000000000 --- a/sys/dev/sound/midi/midiq.h +++ /dev/null @@ -1,87 +0,0 @@ -/*- - * SPDX-License-Identifier: BSD-2-Clause - * - * Copyright (c) 2003 Mathew Kanner - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#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)) -/* - * No protection against overflow, underflow - */ -#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