]> xenbits.xensource.com Git - qemu-xen.git/commitdiff
util/oslib: Assert qemu_try_memalign() alignment is a power of 2
authorPhilippe Mathieu-Daudé <philmd@redhat.com>
Wed, 21 Oct 2020 17:38:03 +0000 (19:38 +0200)
committerRichard Henderson <richard.henderson@linaro.org>
Thu, 7 Jan 2021 15:09:06 +0000 (05:09 -1000)
qemu_try_memalign() expects a power of 2 alignment:

- posix_memalign(3):

  The address of the allocated memory will be a multiple of alignment,
  which must be a power of two and a multiple of sizeof(void *).

- _aligned_malloc()

  The alignment value, which must be an integer power of 2.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20201021173803.2619054-3-philmd@redhat.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
util/oslib-posix.c
util/oslib-win32.c

index f1e2801b11560878007d52847dbc025f232d3552..359c52df123b37a4e4f1d328ac1194b87e2f14c8 100644 (file)
@@ -201,6 +201,8 @@ void *qemu_try_memalign(size_t alignment, size_t size)
 
     if (alignment < sizeof(void*)) {
         alignment = sizeof(void*);
+    } else {
+        g_assert(is_power_of_2(alignment));
     }
 
 #if defined(CONFIG_POSIX_MEMALIGN)
index 8adc65125913713d6de540a8f5d6fdc45582aa37..e6f83e10edb2e9cbdd164e712ebf517d80aad9e2 100644 (file)
@@ -58,6 +58,7 @@ void *qemu_try_memalign(size_t alignment, size_t size)
     void *ptr;
 
     g_assert(size != 0);
+    g_assert(is_power_of_2(alignment));
     ptr = _aligned_malloc(alignment, size);
     trace_qemu_memalign(alignment, size, ptr);
     return ptr;