qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: qemu-trivial@nongnu.org, Roman Kiryanov <rkir@google.com>,
	Daniel Berrange <berrange@redhat.com>
Subject: [PULL 29/46] bitmap: Use g_try_new0/g_new0/g_renew
Date: Mon,  6 May 2024 09:51:08 +0200	[thread overview]
Message-ID: <20240506075125.8238-30-pbonzini@redhat.com> (raw)
In-Reply-To: <20240506075125.8238-1-pbonzini@redhat.com>

Avoids an explicit use of sizeof().  The GLib allocation macros
ensure that the multiplication by the size of the element
uses the right type and does not overflow.

While at it, change bitmap_new() to use g_new0 directly.  Its current
impl of calling bitmap_try_new() followed by a plain abort() has
worse diagnostics than g_new0, which uses g_error to report the actual
allocation size that failed.

Cc: qemu-trivial@nongnu.org
Cc: Roman Kiryanov <rkir@google.com>
Reviewed-by: Daniel Berrange <berrange@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/bitmap.h | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/include/qemu/bitmap.h b/include/qemu/bitmap.h
index 97806811eeb..1cf288445f3 100644
--- a/include/qemu/bitmap.h
+++ b/include/qemu/bitmap.h
@@ -92,17 +92,14 @@ long slow_bitmap_count_one(const unsigned long *bitmap, long nbits);
 
 static inline unsigned long *bitmap_try_new(long nbits)
 {
-    long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
-    return g_try_malloc0(len);
+    long nelem = BITS_TO_LONGS(nbits);
+    return g_try_new0(unsigned long, nelem);
 }
 
 static inline unsigned long *bitmap_new(long nbits)
 {
-    unsigned long *ptr = bitmap_try_new(nbits);
-    if (ptr == NULL) {
-        abort();
-    }
-    return ptr;
+    long nelem = BITS_TO_LONGS(nbits);
+    return g_new0(unsigned long, nelem);
 }
 
 static inline void bitmap_zero(unsigned long *dst, long nbits)
@@ -265,10 +262,10 @@ unsigned long bitmap_find_next_zero_area(unsigned long *map,
 static inline unsigned long *bitmap_zero_extend(unsigned long *old,
                                                 long old_nbits, long new_nbits)
 {
-    long new_len = BITS_TO_LONGS(new_nbits) * sizeof(unsigned long);
-    unsigned long *new = g_realloc(old, new_len);
-    bitmap_clear(new, old_nbits, new_nbits - old_nbits);
-    return new;
+    long new_nelem = BITS_TO_LONGS(new_nbits);
+    unsigned long *ptr = g_renew(unsigned long, old, new_nelem);
+    bitmap_clear(ptr, old_nbits, new_nbits - old_nbits);
+    return ptr;
 }
 
 void bitmap_to_le(unsigned long *dst, const unsigned long *src,
-- 
2.44.0



  parent reply	other threads:[~2024-05-06  7:54 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-06  7:50 [PULL 00/46] Mostly build system and other cleanups patches for 2024-05-06 Paolo Bonzini
2024-05-06  7:50 ` [PULL 01/46] target/i386: Give IRQs a chance when resetting HF_INHIBIT_IRQ_MASK Paolo Bonzini
2024-05-06  7:50 ` [PULL 02/46] Kconfig: kvm: allow building without any board Paolo Bonzini
2024-05-06  7:50 ` [PULL 03/46] tests/qtest: skip m48t59-test if the machine is absent Paolo Bonzini
2024-05-06  7:50 ` [PULL 04/46] gitlab-ci: adjust msys2-64bit to be able to run qtest Paolo Bonzini
2024-05-06  7:50 ` [PULL 05/46] kvm: ppc: disable sPAPR code if CONFIG_PSERIES is disabled Paolo Bonzini
2024-05-06  7:50 ` [PULL 06/46] configs: list "implied" device groups in the default configs Paolo Bonzini
2024-05-06  7:50 ` [PULL 07/46] alpha: switch boards to "default y" Paolo Bonzini
2024-05-06  7:50 ` [PULL 08/46] arm: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 09/46] avr: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 10/46] cris: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 11/46] hppa: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 12/46] i386: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 13/46] loongarch: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 14/46] m68k: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 15/46] microblaze: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 16/46] meson: make target endianneess available to Kconfig Paolo Bonzini
2024-05-06  7:50 ` [PULL 17/46] mips: switch boards to "default y" Paolo Bonzini
2024-05-06  7:50 ` [PULL 18/46] openrisc: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 19/46] ppc: " Paolo Bonzini
2024-05-06  7:50 ` [PULL 20/46] riscv: " Paolo Bonzini
2024-05-06  7:51 ` [PULL 21/46] rx: " Paolo Bonzini
2024-05-06  7:51 ` [PULL 22/46] s390x: " Paolo Bonzini
2024-05-06  7:51 ` [PULL 23/46] sh4: " Paolo Bonzini
2024-05-06  7:51 ` [PULL 24/46] sparc: " Paolo Bonzini
2024-05-06  7:51 ` [PULL 25/46] tricore: " Paolo Bonzini
2024-05-06  7:51 ` [PULL 26/46] xtensa: " Paolo Bonzini
2024-05-06  7:51 ` [PULL 27/46] docs: document new convention for Kconfig board symbols Paolo Bonzini
2024-05-06  7:51 ` [PULL 28/46] target/i386: Introduce SapphireRapids-v3 to add missing features Paolo Bonzini
2024-05-06  7:51 ` Paolo Bonzini [this message]
2024-05-06  7:51 ` [PULL 30/46] build: do not build virtio-vga-gl if virgl/opengl not available Paolo Bonzini
2024-05-06  7:51 ` [PULL 31/46] fw_cfg: remove useless declarations from typedefs.h Paolo Bonzini
2024-05-06  7:51 ` [PULL 32/46] qdev-core: remove DeviceListener " Paolo Bonzini
2024-05-06  7:51 ` [PULL 33/46] numa: remove types " Paolo Bonzini
2024-05-06  7:51 ` [PULL 34/46] net: remove AnnounceTimer " Paolo Bonzini
2024-05-06  7:51 ` [PULL 35/46] qemu-option: remove QemuOpt " Paolo Bonzini
2024-05-06  7:51 ` [PULL 36/46] intc: remove PICCommonState " Paolo Bonzini
2024-05-06  7:51 ` [PULL 37/46] lockable: remove QemuLockable " Paolo Bonzini
2024-05-06  7:51 ` [PULL 38/46] migration: remove PostcopyDiscardState " Paolo Bonzini
2024-05-06  7:51 ` [PULL 39/46] monitor: remove MonitorDef " Paolo Bonzini
2024-05-06  7:51 ` [PULL 40/46] qapi/machine: remove types " Paolo Bonzini
2024-05-06  7:51 ` [PULL 41/46] display: remove GraphicHwOps " Paolo Bonzini
2024-05-06  7:51 ` [PULL 42/46] tcg: remove CPU* types " Paolo Bonzini
2024-05-06  7:51 ` [PULL 43/46] pci: remove some " Paolo Bonzini
2024-05-06  7:51 ` [PULL 44/46] kvm: move target-dependent interrupt routing out of kvm-all.c Paolo Bonzini
2024-05-06  7:51 ` [PULL 45/46] migration: do not include coroutine_int.h Paolo Bonzini
2024-05-06  7:51 ` [PULL 46/46] qga/commands-posix: fix typo in qmp_guest_set_user_password Paolo Bonzini
2024-05-06 17:17 ` [PULL 00/46] Mostly build system and other cleanups patches for 2024-05-06 Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240506075125.8238-30-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=berrange@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    --cc=rkir@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).