Skip to contentSkip to conversation

util/oslib: qemu_try_memalign() improvements

PATCH Superseded Philippe Mathieu-Daudé 2 participants 6 messages Oct 20, 2020 – Oct 21, 2020

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover util/oslib: qemu_try_memalign() improvements

- Use _aligned_malloc for qemu_try_memalign on win32
- Assert qemu_try_memalign() alignment is a power of 2

Supersedes: <[email protected]>

Philippe Mathieu-Daudé (1):
util/oslib: Assert qemu_try_memalign() alignment is a power of 2

Richard Henderson (1):
util/oslib-win32: Use _aligned_malloc for qemu_try_memalign

 util/oslib-posix.c |  3 +++
 util/oslib-win32.c | 12 +++++-------
 2 files changed, 8 insertions(+), 7 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v4 1/2 util/oslib-win32: Use _aligned_malloc for qemu_try_memalign

From: Richard Henderson <[email protected]>

We do not need or want to be allocating page sized quanta.

Message-Id: <[email protected]>

---
 util/oslib-win32.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
util/oslib-win32.c +4 -7
index e99debfb8dd..29dd05d59d7 100644
@@ -39,6 +39,7 @@
 #include "trace.h"
 #include "qemu/sockets.h"
 #include "qemu/cutils.h"
+#include <malloc.h>
 
 /* this must come after including "trace.h" */
 #include <shlobj.h>
@@ -56,10 +57,8 @@ void *qemu_try_memalign(size_t alignment, size_t size)
 {
     void *ptr;
 
-    if (!size) {
-        abort();
-    }
-    ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
+    g_assert(size != 0);
+    ptr = _aligned_malloc(alignment, size);
     trace_qemu_memalign(alignment, size, ptr);
     return ptr;
 }
@@ -93,9 +92,7 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
 void qemu_vfree(void *ptr)
 {
     trace_qemu_vfree(ptr);
-    if (ptr) {
-        VirtualFree(ptr, 0, MEM_RELEASE);
-    }
+    _aligned_free(ptr);
 }
 
 void qemu_anon_ram_free(void *ptr, size_t size)
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH v4 1/2] util/oslib-win32: Use _aligned_malloc for qemu_try_memalign

Quoted 8 lines · Philippe Mathieu-Daudé

From: Richard Henderson <[email protected]>

We do not need or want to be allocating page sized quanta.

Message-Id: <[email protected]>

Oops I forgot:

Quoted 41 lines
---
  util/oslib-win32.c | 11 ++++-------
  1 file changed, 4 insertions(+), 7 deletions(-)
util/oslib-win32.c +4 -7
index e99debfb8dd..29dd05d59d7 100644
@@ -39,6 +39,7 @@
  #include "trace.h"
  #include "qemu/sockets.h"
  #include "qemu/cutils.h"
+#include <malloc.h>
 
  /* this must come after including "trace.h" */
  #include <shlobj.h>
@@ -56,10 +57,8 @@ void *qemu_try_memalign(size_t alignment, size_t size)
  {
      void *ptr;
 
-    if (!size) {
-        abort();
-    }
-    ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
+    g_assert(size != 0);
+    ptr = _aligned_malloc(alignment, size);
      trace_qemu_memalign(alignment, size, ptr);
      return ptr;
  }
@@ -93,9 +92,7 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
  void qemu_vfree(void *ptr)
  {
      trace_qemu_vfree(ptr);
-    if (ptr) {
-        VirtualFree(ptr, 0, MEM_RELEASE);
-    }
+    _aligned_free(ptr);
  }
 
  void qemu_anon_ram_free(void *ptr, size_t size)
#3 lore raw

PATCH v4 2/2 util/oslib: Assert qemu_try_memalign() alignment is a power of 2

In reply to Philippe Mathieu-Daudé

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.

---
 util/oslib-posix.c | 3 +++
 util/oslib-win32.c | 1 +
 2 files changed, 4 insertions(+)
util/oslib-posix.c +3 -0
index f15234b5c03..9d6451f9239 100644
@@ -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)
util/oslib-win32.c +1 -0
index 29dd05d59d7..72e4ee910ce 100644
@@ -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;
Signature · 1 line
2.26.2
#4 lore raw

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

util/oslib-posix.c void *qemu_try_memalign(size_t alignment, size_t size) · line 204

Quoted 6 lines · Philippe Mathieu-Daudé
util/oslib-posix.c +2 -0
@@ -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.

Review void *qemu_try_memalign(size_t alignment, size_t size) · line 205

Quoted 1 line
(quoted diff) +1 -0
+        g_assert(is_power_of_2(alignment));

r~

#5 lore raw

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

Quoted 9 lines · Richard Henderson

util/oslib-posix.c void *qemu_try_memalign(size_t alignment, size_t size) · line 204

Quoted 6 lines · Philippe Mathieu-Daudé
util/oslib-posix.c +2 -0
@@ -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 =)

Quoted 6 lines

Review void *qemu_try_memalign(size_t alignment, size_t size) · line 205

Quoted 2 lines
(quoted diff) +1 -0
+        g_assert(is_power_of_2(alignment));

r~