From nobody Mon Feb 9 10:39:54 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=1628009352; cv=none; d=zohomail.com; s=zohoarc; b=NU0LDhWzYfvgSJpFM7WnMRUXWjrotUa6PNB/eCPYh2aQGW8zqlMnIMN/lMELcb8cOypv9Fi5cmHvdiOXOEzwoOM5TS56J+u9KvTUSioJAf3WnGfWoCHZEMibULDLXM+5DuwUU04idYbaLpuMEBcP6R7qyUkrkeBgMMZa0Zzsv5M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628009352; 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=eMWC51/cUlTQ5VpjuNxbLvS2JHNzrobUxuFiz44m5Xs=; b=XsgQx9X7nyG3MR5v0L4Vx/fEcZTvb9wbCxptiXHpdhJ3ULdJ6vaJm7d7Opbzc+F/oy1AKAg9yjMHcWXdjDkoPUqxvwTI/hheUROQV8AtscYgfpcDNGWr4kVBEJZeJSrIV9G8hb9ZsEtXMs228oAPLYgFGnjPnngsW9Rq6wbKjag= 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 1628009352635880.1202889887525; Tue, 3 Aug 2021 09:49:12 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-233-QO-rvVy9N8mj1RXii-NFpA-1; Tue, 03 Aug 2021 12:47:25 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3A997100F767; Tue, 3 Aug 2021 16:47:17 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1E0E5226E8; Tue, 3 Aug 2021 16:47:17 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D86CB4A712; Tue, 3 Aug 2021 16:47:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 173CWpZ8022519 for ; Tue, 3 Aug 2021 08:32:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 079E65E27D; Tue, 3 Aug 2021 12:32:51 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.192.146]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7C37C5E275 for ; Tue, 3 Aug 2021 12:32:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628009351; 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=eMWC51/cUlTQ5VpjuNxbLvS2JHNzrobUxuFiz44m5Xs=; b=YOEjdLSJHvNhwbGzRV5oJoDPVHt6eMYIG9Szo/vYpTBRfjHYqNKicD45yAo2Sv8rfbt+ua TiMtlKVHelswUDoyWBkoxMo8TQb33iAb7L7Lr0LdKUOu95eBX0NvcMA4S5hJVNW4qu8YHw eQSrKDgEPWMUdYGIs1t7FZ75NVlsZUk= X-MC-Unique: QO-rvVy9N8mj1RXii-NFpA-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/7] vmx: Rework disk def allocation Date: Tue, 3 Aug 2021 14:32:27 +0200 Message-Id: <1bd6b5543a7e01b891b12fe13834a379c3289d53.1627993626.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com 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: 1628009353555100001 Content-Type: text/plain; charset="utf-8" The way we parse VMX configuration is rather unfortunate, especially when it comes to disks. We allocate an array that can handle all possible disks but leave the array counter (ndisks) at zero and increase it only after successful parsing. But, we never size the array down to release unneeded chunks of memory. We can do better: we can use VIR_APPEND_ELEMENT() to allocate array as needed. Signed-off-by: Michal Privoznik --- src/vmx/vmx.c | 84 ++++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c index c3c00a5c7e..7a934e067d 100644 --- a/src/vmx/vmx.c +++ b/src/vmx/vmx.c @@ -1719,10 +1719,6 @@ virVMXParseConfig(virVMXContext *ctx, if (def->graphics[def->ngraphics] !=3D NULL) ++def->ngraphics; =20 - /* def:disks: 4 * 15 scsi + 4 * 30 sata + 2 * 2 ide + 2 floppy =3D 186= */ - def->disks =3D g_new0(virDomainDiskDef *, 186); - def->ndisks =3D 0; - /* def:disks (scsi) */ for (controller =3D 0; controller < 4; ++controller) { if (virVMXParseSCSIController(conf, controller, &present, @@ -1734,6 +1730,8 @@ virVMXParseConfig(virVMXContext *ctx, continue; =20 for (unit =3D 0; unit < 16; ++unit) { + g_autoptr(virDomainDiskDef) disk =3D NULL; + if (unit =3D=3D 7) { /* * SCSI unit 7 is assigned to the SCSI controller and cann= ot be @@ -1744,25 +1742,23 @@ virVMXParseConfig(virVMXContext *ctx, =20 if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= DISK, VIR_DOMAIN_DISK_BUS_SCSI, controller, unit, - &def->disks[def->ndisks], def) < 0) { + &disk, def) < 0) { goto cleanup; } =20 - if (def->disks[def->ndisks] !=3D NULL) { - ++def->ndisks; + if (!disk && + virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= CDROM, + VIR_DOMAIN_DISK_BUS_SCSI, controller, unit, + &disk, def) < 0) { + goto cleanup; + } + + if (!disk) continue; - } =20 - if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= CDROM, - VIR_DOMAIN_DISK_BUS_SCSI, controller, uni= t, - &def->disks[def->ndisks], def) < 0) { + if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) goto cleanup; - } - - if (def->disks[def->ndisks] !=3D NULL) - ++def->ndisks; } - } =20 /* add all the SCSI controllers we've seen, up until the last one that= is @@ -1787,27 +1783,27 @@ virVMXParseConfig(virVMXContext *ctx, continue; =20 for (unit =3D 0; unit < 30; ++unit) { + g_autoptr(virDomainDiskDef) disk =3D NULL; + if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= DISK, VIR_DOMAIN_DISK_BUS_SATA, controller, unit, - &def->disks[def->ndisks], def) < 0) { + &disk, def) < 0) { goto cleanup; } =20 - if (def->disks[def->ndisks] !=3D NULL) { - ++def->ndisks; - continue; - } - - if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= CDROM, + if (!disk && + virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= CDROM, VIR_DOMAIN_DISK_BUS_SATA, controller, uni= t, - &def->disks[def->ndisks], def) < 0) { + &disk, def) < 0) { goto cleanup; } =20 - if (def->disks[def->ndisks] !=3D NULL) - ++def->ndisks; + if (!disk) + continue; + + if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) + goto cleanup; } - } =20 /* add all the SATA controllers we've seen, up until the last one that= is @@ -1824,38 +1820,44 @@ virVMXParseConfig(virVMXContext *ctx, /* def:disks (ide) */ for (bus =3D 0; bus < 2; ++bus) { for (unit =3D 0; unit < 2; ++unit) { + g_autoptr(virDomainDiskDef) disk =3D NULL; + if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= DISK, VIR_DOMAIN_DISK_BUS_IDE, bus, unit, - &def->disks[def->ndisks], def) < 0) { + &disk, def) < 0) { goto cleanup; } =20 - if (def->disks[def->ndisks] !=3D NULL) { - ++def->ndisks; + if (!disk && + virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= CDROM, + VIR_DOMAIN_DISK_BUS_IDE, bus, unit, + &disk, def) < 0) { + goto cleanup; + } + + if (!disk) continue; - } =20 - if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_= CDROM, - VIR_DOMAIN_DISK_BUS_IDE, bus, unit, - &def->disks[def->ndisks], def) < 0) { + if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) goto cleanup; - } - - if (def->disks[def->ndisks] !=3D NULL) - ++def->ndisks; } } =20 /* def:disks (floppy) */ for (unit =3D 0; unit < 2; ++unit) { + g_autoptr(virDomainDiskDef) disk =3D NULL; + if (virVMXParseDisk(ctx, xmlopt, conf, VIR_DOMAIN_DISK_DEVICE_FLOP= PY, VIR_DOMAIN_DISK_BUS_FDC, 0, unit, - &def->disks[def->ndisks], def) < 0) { + &disk, def) < 0) { goto cleanup; } =20 - if (def->disks[def->ndisks] !=3D NULL) - ++def->ndisks; + if (!disk) + continue; + + if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) + goto cleanup; } =20 /* def:fss */ --=20 2.31.1