Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AE808C43458 for ; Mon, 13 Jul 2026 08:44:38 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wjCFz-0004QX-3p; Mon, 13 Jul 2026 04:43:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wjCFw-0004Pf-UC for qemu-devel@nongnu.org; Mon, 13 Jul 2026 04:43:16 -0400 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wjCFv-0003Be-2S for qemu-devel@nongnu.org; Mon, 13 Jul 2026 04:43:16 -0400 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-493ec555a26so18710555e9.0 for ; Mon, 13 Jul 2026 01:43:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mihalicyn.com; s=mihalicyn; t=1783932193; x=1784536993; darn=nongnu.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ogc16JUbVnYmLt9FW0jIh4XHU0HGfMQjrBdoB+HO0eY=; b=hoe+dEljohmHv0gCu/8baWmbMaUfJltRXJmD5bzntwCMc6c1S19GP1tXO2DEiw7+30 bGogCdipT3stafGa6Qnh9FiiclUru8sOp/2XcM2aYC4M9jdL9gmaqz0HYIiUjsaCwbp6 ezsItfs35zaGBniaiDrXuzJ0NU35Z2+Y7TEbI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783932193; x=1784536993; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ogc16JUbVnYmLt9FW0jIh4XHU0HGfMQjrBdoB+HO0eY=; b=EcxqGBCdMfRC6o4YXpNJtGzQII9gBek4RYZWHXQGWUJm5jEqPJgHAH1Xxezayu8b/Y /pGpiCN3DBGMYHbNmhsGOU65KoGBOpaf1D3zBE4dWgTvKgjLn2BGZvMxdSIqgqm2svjg FvrmeCAKRWCDqFvFSM9r7Ov7bnfTmSyV9wLjff/9+nxo2USWXUniOg4LgYLpvCYmyqP0 id9ugRazlrL2RhhkdyfT/qDZJvQR4gyHt/8HjzQ8zStjaFxLsV7EG7o+2+iN1UqtR0I6 gEgasPEU8SldY8FPjMQBqojregEMDnAMflHHu0lKMyU4jjYpiMR6WJWe1DHe57t6kEy8 Wb6w== X-Gm-Message-State: AOJu0Yw9xWxntF7XH36Frw+9JcIpRrzROkhUqGHJRKJW5Z2tLlDeQcBp rGQs9KoX1tEWl4THmhZUc67B9/0+KyawlMiBgUi33u5oc4bc/W6vM/RQgCHq9QTui56GveKAtZo 5AQjbrybglQ== X-Gm-Gg: AfdE7cnhjbXTWVTv7kVh16LKcseveziZsNuqbLNMtZYzYq9xaa0Q4DxTAvtEtBJIlXj 6TC2j39+u5CCsCdelu+MtdRLsW/qV+xqyuWqI8ZtvPUHvU3XurlCFSmY1e7/ChruaTX6Abjv4w/ 7dqAGbrMH/F0BVIhQ9gPD2zJehFrA6sktZYUZA4JPNcE8xhh3n3D1kTfcbJONCYWeSqFENlpC1J h4fGrZviiDUP5rzyUCMGHPDjiZLPC0+apCuA0iE1IgbVFUGdmHZDjyCcs+ZCGk4CvUGtQtIpvX/ Uo/gbA+f2+/nUWbWcK2j1y1W73HEtO2KEznQHWiu1XK83bfCKUTTPCpWb/z2VKqze/Z3Ptng/WV B7fY22pbwE9yov8f3Wm5kbSI9kifSG/gXoTn6zXWWe9ngB4rK32OXVE1r8iR0vfa1MH+eCpICim lKpsYDQThCkZfk3KxnM8HWTUikhXtMa/7pJvsMTQuqc77cRUsgQKSIuXh8i3t77WGzlCpD+6d+P BCI3WDZTouiQ7r17GxY2Dg= X-Received: by 2002:a05:600c:348e:b0:493:bd37:1cdf with SMTP id 5b1f17b1804b1-493f87d6219mr84108355e9.2.1783932193202; Mon, 13 Jul 2026 01:43:13 -0700 (PDT) Received: from alex-laptop.lan (p200300cf574a7200912ca59176a42017.dip0.t-ipconnect.de. [2003:cf:574a:7200:912c:a591:76a4:2017]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493f2d97527sm287254815e9.2.2026.07.13.01.43.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Jul 2026 01:43:12 -0700 (PDT) From: Alexander Mikhalitsyn To: qemu-devel@nongnu.org Cc: Alexander Mikhalitsyn , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Gerd Hoffmann , =?UTF-8?q?Volker=20R=C3=BCmelin?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , =?UTF-8?q?St=C3=A9phane=20Graber?= , Alexander Mikhalitsyn Subject: [PATCH v4 04/10] hw/audio/virtio-sound: allocate an array of streams Date: Mon, 13 Jul 2026 10:43:01 +0200 Message-ID: <20260713084307.79593-5-alexander@mihalicyn.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260713084307.79593-1-alexander@mihalicyn.com> References: <20260713084307.79593-1-alexander@mihalicyn.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::32d; envelope-from=alexander@mihalicyn.com; helo=mail-wm1-x32d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Volker Rümelin It is much easier to migrate an array of structs than individual structs that are accessed via a pointer to a pointer to an array of pointers to struct. For this reason, allocate an array of streams in virtio_snd_realize() and initialise all stream variables that are constant at runtime immediately after allocation. This makes it easier to remove the virtio_snd_set_pcm_params() and virtio_snd_pcm_prepare() calls in the realisation phase and to migrate the audio streams of the virtio sound device after the next few patches. Signed-off-by: Volker Rümelin [AM: there were too many conflicts, I did `git checkout --ours -- <.>` and then reimplemented the patch idea /AM] Signed-off-by: Alexander Mikhalitsyn Reviewed-by: Marc-André Lureau --- v4: - fixed stream->info.channels_max value (problem was noticed by Manos Pitsidianakis) v3: As suggested by Marc-André Lureau: - drop VirtIOSoundPCMStream's id field --- hw/audio/virtio-snd.c | 33 +++++++++++++++++++++------------ include/hw/audio/virtio-snd.h | 2 +- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 27018df8485..b2b711457bb 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -436,12 +436,9 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id) stream = virtio_snd_pcm_get_stream(s, stream_id); if (stream == NULL) { - stream = g_new0(VirtIOSoundPCMStream, 1); + stream = &s->streams[stream_id]; stream->active = false; - stream->id = stream_id; - stream->s = s; stream->latency_bytes = 0; - QSIMPLEQ_INIT(&stream->queue); /* * stream_id >= s->snd_conf.streams was checked before so this is @@ -451,14 +448,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id) } virtio_snd_get_qemu_audsettings(&as, params); - stream->info.direction = stream_id < s->snd_conf.streams / 2 + - (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT; - stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID; - stream->info.features = 0; - stream->info.channels_min = 1; stream->info.channels_max = as.nchannels; - stream->info.formats = supported_formats; - stream->info.rates = supported_rates; stream->params = *params; stream->as = as; @@ -1044,6 +1034,24 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) vsnd->vmstate = qemu_add_vm_change_state_handler(virtio_snd_vm_state_change, vsnd); + vsnd->streams = g_new0(VirtIOSoundPCMStream, vsnd->snd_conf.streams); + + for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) { + VirtIOSoundPCMStream *stream = &vsnd->streams[i]; + + stream->s = vsnd; + QSIMPLEQ_INIT(&stream->queue); + stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID; + stream->info.features = 0; + stream->info.formats = supported_formats; + stream->info.rates = supported_rates; + stream->info.direction = + i < vsnd->snd_conf.streams / 2 + (vsnd->snd_conf.streams & 1) + ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT; + stream->info.channels_min = 1; + stream->info.channels_max = 2; + } + vsnd->pcm.streams = g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams); vsnd->pcm.pcm_params = @@ -1312,12 +1320,13 @@ static void virtio_snd_unrealize(DeviceState *dev) if (stream) { virtio_snd_process_cmdq(stream->s); virtio_snd_pcm_close(stream); - g_free(stream); } } g_free(vsnd->pcm.streams); } g_free(vsnd->pcm.pcm_params); + g_free(vsnd->streams); + vsnd->streams = NULL; virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_CONTROL]); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_EVENT]); virtio_delete_queue(vsnd->queues[VIRTIO_SND_VQ_TX]); diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index 884c475ffd9..97ffaae18be 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -136,7 +136,6 @@ struct VirtIOSoundPCM { struct VirtIOSoundPCMStream { virtio_snd_pcm_info info; virtio_snd_pcm_set_params params; - uint32_t id; VirtIOSound *s; audsettings as; union { @@ -211,6 +210,7 @@ struct VirtIOSound { VirtQueue *queues[VIRTIO_SND_VQ_MAX]; uint64_t features; VirtIOSoundPCM pcm; + VirtIOSoundPCMStream *streams; AudioBackend *audio_be; VMChangeStateEntry *vmstate; virtio_snd_config snd_conf; -- 2.47.3