From mboxrd@z Thu Jan 1 00:00:00 1970 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 3C0CEC55184 for ; Mon, 3 Aug 2026 08:13:30 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wqnmP-0001cU-2H; Mon, 03 Aug 2026 04:12:13 -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 1wqnmN-0001bm-00 for qemu-devel@nongnu.org; Mon, 03 Aug 2026 04:12:11 -0400 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wqnmJ-0003fE-Hb for qemu-devel@nongnu.org; Mon, 03 Aug 2026 04:12:09 -0400 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-496bb7cdf51so18092785e9.2 for ; Mon, 03 Aug 2026 01:12:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mihalicyn.com; s=mihalicyn; t=1785744726; x=1786349526; 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=gaXNKce+6SBp9rMa7VVO5dpXFHXBN1RPswRUdk4bqXs=; b=JfFGKboiXdXgaOS1A/Jmb4mKU6LOadguvcneUNFJfneQJx4gjpzSr/EbmWrJOa4eT/ oJy99kYEtFIueydIEKh1LJidCSN19N3bT+/nQbEHxrap/YszvyvvJBqF0JZ6H33yThKY W8fVVfF5qh86WnkGbm9GXtLRnXuOfHzhPg5sA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785744726; x=1786349526; 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=gaXNKce+6SBp9rMa7VVO5dpXFHXBN1RPswRUdk4bqXs=; b=lEd7GtF5Rv4KWeX3vjJ6GWgnPRc87kiXGAhH3mn2VNWuVAI2sLY3QdDi1zOED94yca tblA0TvOHtuMHv9EbQZhZUBsJ5qyjUM0RogyM4VrQEL5PtbuthL6JTTzSDvSOJ0Ud/FR LL+2sWxb1GXcocCxEzyf1dYANZWXZTc+GYyE64QhbTA+RJUaIwEYX0RgHm6y4wA+MAZt nmCSCWozP1JxXra9vfrSSczV0UIwIv+5QrKN2L4trXlnwD2H+8wyTHIBHWH1kwZbimdY iO2C/tB2OEO+bHDqnQ0fVxBKqBR+AhBWs8Qdr//dbA4IZ4dpCelyGvQejuX2DcBOvOSJ 1HHw== X-Gm-Message-State: AOJu0YzVCZSkGl4eMtF6jP8SqFCKqvKJclqs0xUuzq1GFTEb0UIhietL pZTQLDGC2XOoo2MZxvbhRwG2kctLJjqmBJZU76J5tX1633/bcl+tMWWx17Wvgx+WYySu8cUZHMj dC1Hx X-Gm-Gg: AR+sD109XfC0nfCQtGozk4HuOrA6bffFoEZUTPlaTQ8aIqlrNR2IaSKCB7uyFp0CVig AMn4MYp0vlS7a4y7mL1o07sI3Y9dgL1B/vx6Ue9iJJkqc3S295N92xg7G/ZhnBI+Vxg9dI7+rF7 pKu5rxoYEBygrtPXGcA+qd91IH/MkIYdeNM4uh2SljhDluX5hw+EwvKR6RVtb+HVe0qL90RQCR8 NjnZKM53oX6f/3fjKAGoPdUHNihxOt1E6YpRYp1RsW9TLDVDNedeVD5NThM/EDKPGCFNq3NANYd HvkPcTFv7/n2KZbiNvaVqoTP7r48nnI27ylDgmiGgnRqO/PZzEjJTos2hGLdgKgqY5+PUbEkpqF +rftVVmnZ6RanKsYtjxD0I5EELHnONzkCZFr5avoLUYusO7IgMJ6CeUFGrnWT0vElSZmPn6sOuc 8EELqWiccP4kwJZ9OWDl+LKjeTgnezwaAiguPbxBKfrGTMZRDOx2KLmBKUuH4jvlLncZHeU+wf6 X3IEaASGmVNFmOI1OVbaj1n/juLgHlPjKFMSwWUfRvtbiGmc2iYxNbh0EfQ X-Received: by 2002:a05:600c:608e:b0:493:f140:c3fb with SMTP id 5b1f17b1804b1-4980c64a978mr245000195e9.7.1785744725877; Mon, 03 Aug 2026 01:12:05 -0700 (PDT) Received: from alex-laptop.lan (p200300cf574a7200803110ea947f174a.dip0.t-ipconnect.de. [2003:cf:574a:7200:8031:10ea:947f:174a]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49807b98284sm209358435e9.12.2026.08.03.01.12.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 01:12:05 -0700 (PDT) From: Alexander Mikhalitsyn To: qemu-devel@nongnu.org Cc: Manos Pitsidianakis , =?UTF-8?q?Volker=20R=C3=BCmelin?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Gerd Hoffmann , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Alexander Mikhalitsyn , "Michael S. Tsirkin" , =?UTF-8?q?St=C3=A9phane=20Graber?= , Alexander Mikhalitsyn Subject: [PATCH v5 04/10] hw/audio/virtio-sound: allocate an array of streams Date: Mon, 3 Aug 2026 10:11:53 +0200 Message-ID: <20260803081159.91981-5-alexander@mihalicyn.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260803081159.91981-1-alexander@mihalicyn.com> References: <20260803081159.91981-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::329; envelope-from=alexander@mihalicyn.com; helo=mail-wm1-x329.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 cbf739f8a7d..84083be8825 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; @@ -1056,6 +1046,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 = @@ -1324,12 +1332,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