[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 03/18] util: Return valid allocation for qemu_try_memalign() with
From: |
Peter Maydell |
Subject: |
[PULL 03/18] util: Return valid allocation for qemu_try_memalign() with zero size |
Date: |
Mon, 7 Mar 2022 16:46:54 +0000 |
Currently qemu_try_memalign()'s behaviour if asked to allocate
0 bytes is rather variable:
* on Windows, we will assert
* on POSIX platforms, we get the underlying behaviour of
the posix_memalign() or equivalent function, which may be
either "return a valid non-NULL pointer" or "return NULL"
Explictly check for 0 byte allocations, so we get consistent
behaviour across platforms. We handle them by incrementing the size
so that we return a valid non-NULL pointer that can later be passed
to qemu_vfree(). This is permitted behaviour for the
posix_memalign() API and is the most usual way that underlying
malloc() etc implementations handle a zero-sized allocation request,
because it won't trip up calling code that assumes NULL means an
error. (This includes our own qemu_memalign(), which will abort on
NULL.)
This change is a preparation for sharing the qemu_try_memalign() code
between Windows and POSIX.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
util/oslib-posix.c | 3 +++
util/oslib-win32.c | 4 +++-
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 0278902ee79..f7e22f4ff9b 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -209,6 +209,9 @@ void *qemu_try_memalign(size_t alignment, size_t size)
g_assert(is_power_of_2(alignment));
}
+ if (size == 0) {
+ size++;
+ }
#if defined(CONFIG_POSIX_MEMALIGN)
int ret;
ret = posix_memalign(&ptr, alignment, size);
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 05857414695..8c28d70904d 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -48,12 +48,14 @@ void *qemu_try_memalign(size_t alignment, size_t size)
{
void *ptr;
- g_assert(size != 0);
if (alignment < sizeof(void *)) {
alignment = sizeof(void *);
} else {
g_assert(is_power_of_2(alignment));
}
+ if (size == 0) {
+ size++;
+ }
ptr = _aligned_malloc(size, alignment);
trace_qemu_memalign(alignment, size, ptr);
return ptr;
--
2.25.1
- [PULL 00/18] target-arm queue, Peter Maydell, 2022/03/07
- [PULL 01/18] util: Make qemu_oom_check() a static function, Peter Maydell, 2022/03/07
- [PULL 02/18] util: Unify implementations of qemu_memalign(), Peter Maydell, 2022/03/07
- [PULL 03/18] util: Return valid allocation for qemu_try_memalign() with zero size,
Peter Maydell <=
- [PULL 05/18] util: Share qemu_try_memalign() implementation between POSIX and Windows, Peter Maydell, 2022/03/07
- [PULL 04/18] meson.build: Don't misdetect posix_memalign() on Windows, Peter Maydell, 2022/03/07
- [PULL 07/18] util: Put qemu_vfree() in memalign.c, Peter Maydell, 2022/03/07
- [PULL 06/18] util: Use meson checks for valloc() and memalign() presence, Peter Maydell, 2022/03/07
- [PULL 09/18] target/arm/translate-neon: UNDEF if VLD1/VST1 stride bits are non-zero, Peter Maydell, 2022/03/07
- [PULL 08/18] osdep: Move memalign-related functions to their own header, Peter Maydell, 2022/03/07
- [PULL 10/18] target/arm/translate-neon: Simplify align field check for VLD3, Peter Maydell, 2022/03/07
- [PULL 11/18] hw/intc/arm_gicv3_its: Add trace events for commands, Peter Maydell, 2022/03/07
- [PULL 12/18] hw/intc/arm_gicv3_its: Add trace events for table reads and writes, Peter Maydell, 2022/03/07
- [PULL 13/18] hw/intc/arm_gicv3: Specify valid and impl in MemoryRegionOps, Peter Maydell, 2022/03/07