From nobody Mon Feb 9 10:34:54 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1729167020051513.2000743727964; Thu, 17 Oct 2024 05:10:20 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 87ED212B1; Thu, 17 Oct 2024 08:10:17 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 7B1811283; Thu, 17 Oct 2024 08:09:29 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 230271212; Thu, 17 Oct 2024 08:09:22 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 5215F121A for ; Thu, 17 Oct 2024 08:09:20 -0400 (EDT) Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-563-kkeQiDbGN7-I2_EIb5DXDA-1; Thu, 17 Oct 2024 08:09:18 -0400 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BE2F21955EA7 for ; Thu, 17 Oct 2024 12:09:17 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.45.242.4]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EE043195607C for ; Thu, 17 Oct 2024 12:09:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: *** X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SBL_CSS, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729166959; 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: in-reply-to:in-reply-to:references:references; bh=Koz4tLAnrB+3HHR0N4Q3Uxpxv5Mq+uiLMeeKxAPbG74=; b=iMJ5qYJ4XCEter9YVP9a/nknAXiGT6uQ9qnbycYAvo8pPokN0zUYaT4cgKknjnfBKJ0GOW udEGQxdkrxek56TuimkgvBvkJhFG6m4oWKekD5H6Gsgt7BhIeMZwVOy8hLmX/VgOpvvUNW 8JYPrHsI1xoU6kTJjVx6ThA9yQjIF3A= X-MC-Unique: kkeQiDbGN7-I2_EIb5DXDA-1 From: Peter Krempa To: devel@lists.libvirt.org Subject: [PATCH 2/4] virbitmap: Extract and reuse buffer size calculation into a function Date: Thu, 17 Oct 2024 14:09:10 +0200 Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: AASYGDT522FG26GA3CDK3AQHIEE5LUK2 X-Message-ID-Hash: AASYGDT522FG26GA3CDK3AQHIEE5LUK2 X-MailFrom: pkrempa@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1729167021488116600 Content-Type: text/plain; charset="utf-8" Calculating the number of element can come handy in multiple places, extract it from virBitmapNew. Signed-off-by: Peter Krempa --- src/util/virbitmap.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c index a1a8c5d126..7dc63da6db 100644 --- a/src/util/virbitmap.c +++ b/src/util/virbitmap.c @@ -49,6 +49,22 @@ struct _virBitmap { #define VIR_BITMAP_BIT(b) (1UL << VIR_BITMAP_BIT_OFFSET(b)) +/** + * Calculates and returns the number of elements in the bitmap buffer to f= it @bits. + */ +static size_t +virBitmapBuffsize(size_t nbits) +{ + if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < nbits) { + /* VIR_DIV_UP would overflow, let's overallocate by 1 entry instea= d of + * the potential overflow */ + return (nbits / VIR_BITMAP_BITS_PER_UNIT) + 1; + } + + return VIR_DIV_UP(nbits, VIR_BITMAP_BITS_PER_UNIT); +} + + /** * virBitmapNew: * @size: number of bits @@ -61,15 +77,7 @@ virBitmap * virBitmapNew(size_t size) { virBitmap *bitmap; - size_t sz; - - if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size) { - /* VIR_DIV_UP would overflow, let's overallocate by 1 entry instea= d of - * the potential overflow */ - sz =3D (size / VIR_BITMAP_BITS_PER_UNIT) + 1; - } else { - sz =3D VIR_DIV_UP(size, VIR_BITMAP_BITS_PER_UNIT); - } + size_t sz =3D virBitmapBuffsize(size); bitmap =3D g_new0(virBitmap, 1); @@ -133,7 +141,7 @@ static void virBitmapExpand(virBitmap *map, size_t b) { - size_t new_len =3D VIR_DIV_UP(b + 1, VIR_BITMAP_BITS_PER_UNIT); + size_t new_len =3D virBitmapBuffsize(b + 1); /* resize the memory if necessary */ if (map->map_len < new_len) { --=20 2.47.0