diff mbox series

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

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

Commit Message

Philippe Mathieu-Daudé Oct. 20, 2020, 11:17 a.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 | 3 +++
 util/oslib-win32.c | 1 +
 2 files changed, 4 insertions(+)

Comments

Richard Henderson Oct. 21, 2020, 4:01 p.m. UTC | #1
On 10/20/20 4:17 AM, Philippe Mathieu-Daudé wrote:
> @@ -200,6 +200,9 @@ void *qemu_try_memalign(size_t alignment, size_t size)
>  
>      if (alignment < sizeof(void*)) {
>          alignment = sizeof(void*);
> +    } else {
> +        g_assert(QEMU_IS_ALIGNED(alignment, sizeof(void *)));

This is redundant with the if above, and the assert below.

> +        g_assert(is_power_of_2(alignment));


r~
Philippe Mathieu-Daudé Oct. 21, 2020, 4:21 p.m. UTC | #2
On 10/21/20 6:01 PM, Richard Henderson wrote:
> On 10/20/20 4:17 AM, Philippe Mathieu-Daudé wrote:

>> @@ -200,6 +200,9 @@ void *qemu_try_memalign(size_t alignment, size_t size)

>>   

>>       if (alignment < sizeof(void*)) {

>>           alignment = sizeof(void*);

>> +    } else {

>> +        g_assert(QEMU_IS_ALIGNED(alignment, sizeof(void *)));

> 

> This is redundant with the if above, and the assert below.


Indeed, thanks =)

> 

>> +        g_assert(is_power_of_2(alignment));

> 

> 

> r~

>
diff mbox series

Patch

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index f15234b5c03..9d6451f9239 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -200,6 +200,9 @@  void *qemu_try_memalign(size_t alignment, size_t size)
 
     if (alignment < sizeof(void*)) {
         alignment = sizeof(void*);
+    } else {
+        g_assert(QEMU_IS_ALIGNED(alignment, sizeof(void *)));
+        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;