From nobody Mon Feb 9 13:58:55 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1680183607; cv=none; d=zohomail.com; s=zohoarc; b=KxQktmup2RnaNsUlOnUjUU5TrM1B1R/fZzcjmvxC53acaiGd31P6xX4GFps+L9V8SecEbX7vyD5djrpKyS7co0xdAUXiTRZgDGv6aE4ZNHOyKLSvVhlQCUL0oz7I9I8TRvBPW+HqPMQ2x+IcpIpiXZcGwaG5qNjdzIAdGEAPC0U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680183607; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=g+4EdXvbpheWNy1nYAFd7qaDfOWtx7fzD3Md2WW1vTA=; b=Wn+HKkxJgJQePHodi+trAoiQbW11xUjG9E8jS2lGCMIzvwgdG6WEglaw8bwyZO9hsV2/RNWhCeyW3xTHP4sp2ufOmUH2KzSACW0zVpbqVirI5PS45+L9ku5TXoaSf/k22NtJNC+hA8TXAZ96IFg5FE1+zVvq+F7DJnmisSJEoaM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1680183607738879.2384935277166; Thu, 30 Mar 2023 06:40:07 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-34-cYmJwcpaNbS8Ed3msOxEkw-1; Thu, 30 Mar 2023 09:39:20 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 96CC4801210; Thu, 30 Mar 2023 13:39:17 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8536B1121330; Thu, 30 Mar 2023 13:39:17 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 03B4B19465BB; Thu, 30 Mar 2023 13:38:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 625B61946587 for ; Thu, 30 Mar 2023 13:38:35 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 4F4152166B34; Thu, 30 Mar 2023 13:38:30 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.45.242.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id BE23F2166B33 for ; Thu, 30 Mar 2023 13:38:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680183606; h=from:from:sender:sender: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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=g+4EdXvbpheWNy1nYAFd7qaDfOWtx7fzD3Md2WW1vTA=; b=EkizelhZtnMP2GIcHpVRm3wq4kC0pt+jZT3NBa4L0It8dErF8Ua4POfDzNTU06eBHkIulp iVWmOflD4TVDH7VbZaGnCxtziSUHrFJFKVkaG+95U8iNRfQjEpW4b5klcL/IXvGA0bZmcD AfFUJTZzquQfu2hgVrJLBbM/+Umfshs= X-MC-Unique: cYmJwcpaNbS8Ed3msOxEkw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 03/31] virCapabilitiesAllocMachines: Use NULL-terminated list as argument and return count Date: Thu, 30 Mar 2023 15:37:57 +0200 Message-Id: <7c9c06d336f5d51c13ef0b35894191e1907da915.1680182808.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1680183608402100001 Content-Type: text/plain; charset="utf-8" Simplify use of the function by determining the number of elements inside the function. Signed-off-by: Peter Krempa --- src/conf/capabilities.c | 12 +++++++----- src/conf/capabilities.h | 2 +- src/libxl/libxl_capabilities.c | 9 +++++---- tests/testutilsqemu.c | 23 +++-------------------- tests/testutilsxen.c | 26 +++++++------------------- 5 files changed, 23 insertions(+), 49 deletions(-) diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index 9144ced498..6d2f6e282d 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -380,21 +380,23 @@ virCapabilitiesHostNUMAAddCell(virCapsHostNUMA *caps, /** * virCapabilitiesAllocMachines: - * @machines: machine variants for emulator ('pc', or 'isapc', etc) - * @nmachines: number of machine variants for emulator + * @machines: NULL-terminated list of machine variants for emulator ('pc',= or 'isapc', etc) + * @nmachines: filled with number of machine variants for emulator * * Allocate a table of virCapsGuestMachine *from the supplied table * of machine names. */ virCapsGuestMachine ** -virCapabilitiesAllocMachines(const char *const *names, int nnames) +virCapabilitiesAllocMachines(const char *const *names, + int *nnames) { virCapsGuestMachine **machines; size_t i; - machines =3D g_new0(virCapsGuestMachine *, nnames); + *nnames =3D g_strv_length((gchar **)names); + machines =3D g_new0(virCapsGuestMachine *, *nnames); - for (i =3D 0; i < nnames; i++) { + for (i =3D 0; i < *nnames; i++) { machines[i] =3D g_new0(virCapsGuestMachine, 1); machines[i]->name =3D g_strdup(names[i]); } diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h index ef6e8ab685..07f7a3ef58 100644 --- a/src/conf/capabilities.h +++ b/src/conf/capabilities.h @@ -261,7 +261,7 @@ virCapabilitiesHostNUMAAddCell(virCapsHostNUMA *caps, virCapsGuestMachine ** virCapabilitiesAllocMachines(const char *const *names, - int nnames); + int *nnames); void virCapabilitiesFreeMachines(virCapsGuestMachine **machines, int nmachines); diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c index 82c38d1b26..2f4bc8bde8 100644 --- a/src/libxl/libxl_capabilities.c +++ b/src/libxl/libxl_capabilities.c @@ -464,6 +464,7 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCaps *caps) for (i =3D 0; i < nr_guest_archs; ++i) { virCapsGuest *guest; virCapsGuestMachine **machines; + int nmachines; virDomainOSType ostype =3D VIR_DOMAIN_OSTYPE_XEN; const char *loader =3D NULL; @@ -473,22 +474,22 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCaps *caps) ostype =3D VIR_DOMAIN_OSTYPE_HVM; loader =3D LIBXL_FIRMWARE_DIR "/hvmloader"; - machines =3D virCapabilitiesAllocMachines(xen_machines, 1); + machines =3D virCapabilitiesAllocMachines(xen_machines, &nmach= ines); } else if (guest_archs[i].pvh) { char const *const xen_machines[] =3D { "xenpvh", NULL }; ostype =3D VIR_DOMAIN_OSTYPE_XENPVH; - machines =3D virCapabilitiesAllocMachines(xen_machines, 1); + machines =3D virCapabilitiesAllocMachines(xen_machines, &nmach= ines); } else { char const *const xen_machines[] =3D { "xenpv", NULL }; ostype =3D VIR_DOMAIN_OSTYPE_XEN; - machines =3D virCapabilitiesAllocMachines(xen_machines, 1); + machines =3D virCapabilitiesAllocMachines(xen_machines, &nmach= ines); } guest =3D virCapabilitiesAddGuest(caps, ostype, guest_archs[i].arc= h, LIBXL_EXECBIN_DIR "/qemu-system-i3= 86", - loader, 1, machines); + loader, nmachines, machines); virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN, NULL, NULL, 0, NULL); diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index d5922f302d..f6d49bc193 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -158,7 +158,7 @@ static int testQemuAddGuest(virCaps *caps, virArch arch) { - size_t nmachines; + int nmachines; virCapsGuestMachine **machines =3D NULL; virCapsGuest *guest; virArch emu_arch =3D arch; @@ -169,19 +169,11 @@ testQemuAddGuest(virCaps *caps, if (qemu_emulators[emu_arch] =3D=3D NULL) return 0; - nmachines =3D g_strv_length((gchar **)qemu_machines[emu_arch]); - machines =3D virCapabilitiesAllocMachines(qemu_machines[emu_arch], - nmachines); - if (machines =3D=3D NULL) - goto error; - + machines =3D virCapabilitiesAllocMachines(qemu_machines[emu_arch], &nm= achines); guest =3D virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, arch, qemu_emulators[emu_arch], NULL, nmachines, machines); - machines =3D NULL; - nmachines =3D 0; - if (arch =3D=3D VIR_ARCH_I686 || arch =3D=3D VIR_ARCH_X86_64) virCapabilitiesAddGuestFeature(guest, VIR_CAPS_GUEST_FEATURE_TYPE_= CPUSELECTION); @@ -189,21 +181,12 @@ testQemuAddGuest(virCaps *caps, virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL); - nmachines =3D g_strv_length((char **)qemu_machines[emu_arch]); - machines =3D virCapabilitiesAllocMachines(qemu_machines[emu_arch], - nmachines); - if (machines =3D=3D NULL) - goto error; - + machines =3D virCapabilitiesAllocMachines(qemu_machines[emu_arch], &nm= achines); virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM, qemu_emulators[emu_arch], NULL, nmachines, machines); return 0; - - error: - virCapabilitiesFreeMachines(machines, nmachines); - return -1; } diff --git a/tests/testutilsxen.c b/tests/testutilsxen.c index 821ee49d94..3484cccbf2 100644 --- a/tests/testutilsxen.c +++ b/tests/testutilsxen.c @@ -16,13 +16,13 @@ testXLInitCaps(void) virCapsGuestMachine **machines; int nmachines; static const char *const x86_machines[] =3D { - "xenfv" + "xenfv", NULL, }; static const char *const xen_machines[] =3D { - "xenpv", + "xenpv", NULL, }; static const char *const pvh_machines[] =3D { - "xenpvh", + "xenpvh", NULL, }; if ((caps =3D virCapabilitiesNew(virArchFromHost(), @@ -31,48 +31,36 @@ testXLInitCaps(void) caps->host.cpu =3D virCPUDefCopy(&cpuDefaultData); - nmachines =3D G_N_ELEMENTS(x86_machines); - if ((machines =3D virCapabilitiesAllocMachines(x86_machines, nmachines= )) =3D=3D NULL) - goto cleanup; + machines =3D virCapabilitiesAllocMachines(x86_machines, &nmachines); guest =3D virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_X86_64, "/usr/lib/xen/bin/qemu-system-i386", "/usr/lib/xen/boot/hvmloader", nmachines, machines); - machines =3D NULL; + virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN, NULL, NULL, 0, NULL); - nmachines =3D G_N_ELEMENTS(xen_machines); - if ((machines =3D virCapabilitiesAllocMachines(xen_machines, nmachines= )) =3D=3D NULL) - goto cleanup; + machines =3D virCapabilitiesAllocMachines(xen_machines, &nmachines); guest =3D virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_XEN, VIR_ARCH_X86_64, "/usr/lib/xen/bin/qemu-system-i386", NULL, nmachines, machines); - machines =3D NULL; virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN, NULL, NULL, 0, NULL); - nmachines =3D G_N_ELEMENTS(pvh_machines); - if ((machines =3D virCapabilitiesAllocMachines(pvh_machines, nmachines= )) =3D=3D NULL) - goto cleanup; + machines =3D virCapabilitiesAllocMachines(pvh_machines, &nmachines); guest =3D virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_XENPVH, VIR_ARCH_X86_64, "/usr/lib/xen/bin/qemu-system-i386", NULL, nmachines, machines); - machines =3D NULL; virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_XEN, NULL, NULL, 0, NULL); return g_steal_pointer(&caps); - - cleanup: - virCapabilitiesFreeMachines(machines, nmachines); - return NULL; } --=20 2.39.2