diff mbox series

[v5,2/2] util/oslib: Assert qemu_try_memalign() alignment is a power of 2

Message ID 20201021173803.2619054-3-philmd@redhat.com
State Superseded
Headers show
Series util/oslib: qemu_try_memalign() improvements | expand

Commit Message

Philippe Mathieu-Daudé Oct. 21, 2020, 5:38 p.m. UTC
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>
---
 util/oslib-posix.c | 2 ++
 util/oslib-win32.c | 1 +
 2 files changed, 3 insertions(+)
diff mbox series

Patch

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index f15234b5c03..3e022d7206b 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -200,6 +200,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)
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 29dd05d59d7..72e4ee910ce 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -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;