qemu-devel
[Top][All Lists]
Advanced

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

[PATCH v5 2/2] util/oslib: Assert qemu_try_memalign() alignment is a pow


From: Philippe Mathieu-Daudé
Subject: [PATCH v5 2/2] util/oslib: Assert qemu_try_memalign() alignment is a power of 2
Date: Wed, 21 Oct 2020 19:38:03 +0200

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 --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;
-- 
2.26.2




reply via email to

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