qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 1/2] malloc: move memalign/vmalloc to qemu-malloc.c
Date: Mon,  2 May 2011 09:59:21 +0200	[thread overview]
Message-ID: <1304323162-24702-2-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1304323162-24702-1-git-send-email-pbonzini@redhat.com>

This splits qemu_memalign and qemu_vfree in two: a part that does
just the allocation in oslib, and a part that includes the tracing
in qemu-malloc.c.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 osdep.h       |    5 ++---
 oslib-posix.c |   32 +++++++-------------------------
 oslib-win32.c |   31 ++-----------------------------
 qemu-common.h |    7 +++++--
 qemu-malloc.c |   33 +++++++++++++++++++++++++++++++++
 trace-events  |    1 -
 6 files changed, 49 insertions(+), 60 deletions(-)

diff --git a/osdep.h b/osdep.h
index 970d767..4ca7c9e 100644
--- a/osdep.h
+++ b/osdep.h
@@ -88,9 +88,8 @@
 # define QEMU_GNUC_PREREQ(maj, min) 0
 #endif
 
-void *qemu_memalign(size_t alignment, size_t size);
-void *qemu_vmalloc(size_t size);
-void qemu_vfree(void *ptr);
+void *os_memalign(size_t alignment, size_t size);
+void os_vfree(void *ptr);
 
 #define QEMU_MADV_INVALID -1
 
diff --git a/oslib-posix.c b/oslib-posix.c
index 7bc5f7c..1566f18 100644
--- a/oslib-posix.c
+++ b/oslib-posix.c
@@ -28,47 +28,29 @@
 
 #include "config-host.h"
 #include "sysemu.h"
-#include "trace.h"
 #include "qemu_socket.h"
 
-void *qemu_oom_check(void *ptr)
-{
-    if (ptr == NULL) {
-        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
-        abort();
-    }
-    return ptr;
-}
-
-void *qemu_memalign(size_t alignment, size_t size)
+void *os_memalign(size_t alignment, size_t size)
 {
     void *ptr;
+    assert(alignment > sizeof(void *));
+    assert((alignment & (alignment - 1)) == 0);
 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
     int ret;
     ret = posix_memalign(&ptr, alignment, size);
     if (ret != 0) {
-        fprintf(stderr, "Failed to allocate %zu B: %s\n",
-                size, strerror(ret));
-        abort();
+        ptr = NULL;
     }
 #elif defined(CONFIG_BSD)
-    ptr = qemu_oom_check(valloc(size));
+    ptr = valloc(size);
 #else
-    ptr = qemu_oom_check(memalign(alignment, size));
+    ptr = memalign(alignment, size);
 #endif
-    trace_qemu_memalign(alignment, size, ptr);
     return ptr;
 }
 
-/* alloc shared memory pages */
-void *qemu_vmalloc(size_t size)
-{
-    return qemu_memalign(getpagesize(), size);
-}
-
-void qemu_vfree(void *ptr)
+void os_vfree(void *ptr)
 {
-    trace_qemu_vfree(ptr);
     free(ptr);
 }
 
diff --git a/oslib-win32.c b/oslib-win32.c
index 5f0759f..a32451e 100644
--- a/oslib-win32.c
+++ b/oslib-win32.c
@@ -28,48 +28,21 @@
 #include <windows.h>
 #include "config-host.h"
 #include "sysemu.h"
-#include "trace.h"
 #include "qemu_socket.h"
 
-void *qemu_oom_check(void *ptr)
+void *os_memalign(size_t alignment, size_t size)
 {
-    if (ptr == NULL) {
-        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
-        abort();
-    }
-    return ptr;
-}
-
-void *qemu_memalign(size_t alignment, size_t size)
-{
-    void *ptr;
-
-    if (!size) {
-        abort();
-    }
-    ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
-    trace_qemu_memalign(alignment, size, ptr);
-    return ptr;
-}
-
-void *qemu_vmalloc(size_t size)
-{
-    void *ptr;
-
     /* FIXME: this is not exactly optimal solution since VirtualAlloc
        has 64Kb granularity, but at least it guarantees us that the
        memory is page aligned. */
     if (!size) {
         abort();
     }
-    ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
-    trace_qemu_vmalloc(size, ptr);
-    return ptr;
+    return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
 }
 
 void qemu_vfree(void *ptr)
 {
-    trace_qemu_vfree(ptr);
     VirtualFree(ptr, 0, MEM_RELEASE);
 }
 
diff --git a/qemu-common.h b/qemu-common.h
index f9f705d..1382e7e 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -206,13 +206,16 @@ const char *path(const char *pathname);
 int ffs(int i);
 #endif
 
-void *qemu_oom_check(void *ptr);
 void *qemu_malloc(size_t size);
 void *qemu_realloc(void *ptr, size_t size);
 void *qemu_mallocz(size_t size);
-void qemu_free(void *ptr);
 char *qemu_strdup(const char *str);
 char *qemu_strndup(const char *str, size_t size);
+void *qemu_memalign(size_t alignment, size_t size);
+void *qemu_vmalloc(size_t size);
+
+void qemu_free(void *ptr);
+void qemu_vfree(void *ptr);
 
 void qemu_mutex_lock_iothread(void);
 void qemu_mutex_unlock_iothread(void);
diff --git a/qemu-malloc.c b/qemu-malloc.c
index b9b3851..cc1305c 100644
--- a/qemu-malloc.c
+++ b/qemu-malloc.c
@@ -31,6 +31,15 @@ void qemu_free(void *ptr)
     free(ptr);
 }
 
+static void *qemu_oom_check(void *ptr)
+{
+    if (ptr == NULL) {
+        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
+        abort();
+    }
+    return ptr;
+}
+
 static int allow_zero_malloc(void)
 {
 #if defined(CONFIG_ZERO_MALLOC)
@@ -96,3 +105,27 @@ char *qemu_strndup(const char *str, size_t size)
 
     return memcpy(new, str, size);
 }
+
+void *qemu_memalign(size_t alignment, size_t size)
+{
+    char *ptr;
+    assert(size <= LONG_MAX);
+    if (!size && !allow_zero_malloc()) {
+        abort();
+    }
+    ptr = qemu_oom_check(os_memalign(alignment, size ? size : 1));
+    trace_qemu_memalign(alignment, size, (char *)ptr);
+    return ptr;
+}
+
+/* alloc shared memory pages */
+void *qemu_vmalloc(size_t size)
+{
+    return qemu_memalign(getpagesize(), size);
+}
+
+void qemu_vfree(void *ptr)
+{
+    trace_qemu_vfree(ptr);
+    os_vfree(ptr);
+}
diff --git a/trace-events b/trace-events
index 77c96a5..bd2f74e 100644
--- a/trace-events
+++ b/trace-events
@@ -35,7 +35,6 @@ disable qemu_free(void *ptr) "ptr %p"
 
 # osdep.c
 disable qemu_memalign(size_t alignment, size_t size, void *ptr) "alignment %zu size %zu ptr %p"
-disable qemu_vmalloc(size_t size, void *ptr) "size %zu ptr %p"
 disable qemu_vfree(void *ptr) "ptr %p"
 
 # hw/virtio.c
-- 
1.7.4.4

  reply	other threads:[~2011-05-02  7:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-02  7:59 [Qemu-devel] [PATCH 0/2] add malloc statistics infrastructure Paolo Bonzini
2011-05-02  7:59 ` Paolo Bonzini [this message]
2011-05-02  7:59 ` [Qemu-devel] [PATCH 2/2] malloc: add " Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1304323162-24702-2-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).