qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v2 4/9] util: Return valid allocation for qemu_try_memalign()


From: Richard Henderson
Subject: Re: [PATCH v2 4/9] util: Return valid allocation for qemu_try_memalign() with zero size
Date: Fri, 4 Mar 2022 10:18:04 -1000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0

On 3/4/22 01:21, Peter Maydell wrote:
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>
---
  util/oslib-posix.c | 3 +++
  util/oslib-win32.c | 4 +++-
  2 files changed, 6 insertions(+), 1 deletion(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~



reply via email to

[Prev in Thread] Current Thread [Next in Thread]