From: Jes.Sorensen@redhat.com
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 8/9] Consolidate oom_check() functions
Date: Fri, 15 Oct 2010 16:05:52 +0200 [thread overview]
Message-ID: <1287151553-16894-9-git-send-email-Jes.Sorensen@redhat.com> (raw)
In-Reply-To: <1287151553-16894-1-git-send-email-Jes.Sorensen@redhat.com>
From: Jes Sorensen <Jes.Sorensen@redhat.com>
This consolidates the duplicated oom_check() functions, as well as
splitting them into OS dependant versions to avoid the #ifdef
grossness that was present in the old osdep.c version.
Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
---
os-posix-lib.c | 13 +++++++++++--
os-win32-lib.c | 13 +++++++++++--
osdep.c | 15 ---------------
qemu-common.h | 1 +
qemu-malloc.c | 14 +++-----------
5 files changed, 26 insertions(+), 30 deletions(-)
diff --git a/os-posix-lib.c b/os-posix-lib.c
index 773e998..cb146dd 100644
--- a/os-posix-lib.c
+++ b/os-posix-lib.c
@@ -33,6 +33,15 @@
#include "qemu-options.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 *ptr;
@@ -45,9 +54,9 @@ void *qemu_memalign(size_t alignment, size_t size)
abort();
}
#elif defined(CONFIG_BSD)
- ptr = oom_check(valloc(size));
+ ptr = qemu_oom_check(valloc(size));
#else
- ptr = oom_check(memalign(alignment, size));
+ ptr = qemu_oom_check(memalign(alignment, size));
#endif
trace_qemu_memalign(alignment, size, ptr);
return ptr;
diff --git a/os-win32-lib.c b/os-win32-lib.c
index b5b6def..a1c7ca8 100644
--- a/os-win32-lib.c
+++ b/os-win32-lib.c
@@ -38,6 +38,15 @@
#include "qemu-options.h"
#include "qemu_socket.h"
+void *qemu_oom_check(void *ptr)
+{
+ 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;
@@ -45,7 +54,7 @@ void *qemu_memalign(size_t alignment, size_t size)
if (!size) {
abort();
}
- ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
+ ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
trace_qemu_memalign(alignment, size, ptr);
return ptr;
}
@@ -60,7 +69,7 @@ void *qemu_vmalloc(size_t size)
if (!size) {
abort();
}
- ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
+ ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
trace_qemu_vmalloc(size, ptr);
return ptr;
}
diff --git a/osdep.c b/osdep.c
index 702c9d9..0d48561 100644
--- a/osdep.c
+++ b/osdep.c
@@ -57,21 +57,6 @@ extern int madvise(caddr_t, size_t, int);
#include "sysemu.h"
#include "qemu_socket.h"
-#if !defined(_POSIX_C_SOURCE) || defined(_WIN32) || defined(__sun__)
-static void *oom_check(void *ptr)
-{
- if (ptr == NULL) {
-#if defined(_WIN32)
- fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
-#else
- fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
-#endif
- abort();
- }
- return ptr;
-}
-#endif
-
int qemu_madvise(void *addr, size_t len, int advice)
{
if (advice == QEMU_MADV_INVALID) {
diff --git a/qemu-common.h b/qemu-common.h
index 81aafa0..ead1d83 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -174,6 +174,7 @@ const char *path(const char *pathname);
#define qemu_isascii(c) isascii((unsigned char)(c))
#define qemu_toascii(c) toascii((unsigned char)(c))
+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);
diff --git a/qemu-malloc.c b/qemu-malloc.c
index ecffb67..28fb05a 100644
--- a/qemu-malloc.c
+++ b/qemu-malloc.c
@@ -25,14 +25,6 @@
#include "trace.h"
#include <stdlib.h>
-static void *oom_check(void *ptr)
-{
- if (ptr == NULL) {
- abort();
- }
- return ptr;
-}
-
void qemu_free(void *ptr)
{
trace_qemu_free(ptr);
@@ -54,7 +46,7 @@ void *qemu_malloc(size_t size)
if (!size && !allow_zero_malloc()) {
abort();
}
- ptr = oom_check(malloc(size ? size : 1));
+ ptr = qemu_oom_check(malloc(size ? size : 1));
trace_qemu_malloc(size, ptr);
return ptr;
}
@@ -65,7 +57,7 @@ void *qemu_realloc(void *ptr, size_t size)
if (!size && !allow_zero_malloc()) {
abort();
}
- newptr = oom_check(realloc(ptr, size ? size : 1));
+ newptr = qemu_oom_check(realloc(ptr, size ? size : 1));
trace_qemu_realloc(ptr, size, newptr);
return newptr;
}
@@ -75,7 +67,7 @@ void *qemu_mallocz(size_t size)
if (!size && !allow_zero_malloc()) {
abort();
}
- return oom_check(calloc(1, size ? size : 1));
+ return qemu_oom_check(calloc(1, size ? size : 1));
}
char *qemu_strdup(const char *str)
--
1.7.2.3
next prev parent reply other threads:[~2010-10-15 14:21 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-15 14:05 [Qemu-devel] [PATCH 0/9] Re-factor osdep code + macro and brace fixes Jes.Sorensen
2010-10-15 14:05 ` [Qemu-devel] [PATCH 1/9] Move QEMU OS dependant library functions to OS specific files Jes.Sorensen
2010-10-15 20:07 ` Blue Swirl
2010-10-15 14:05 ` [Qemu-devel] [PATCH 2/9] Move osdep socket code to os-{posix, win32}-lib.c Jes.Sorensen
2010-10-15 14:05 ` [Qemu-devel] [PATCH 3/9] qemu_pipe() is used only by POSIX code, so move to os-posix-lib.c Jes.Sorensen
2010-10-15 14:05 ` [Qemu-devel] [PATCH 4/9] We only support eventfd under POSIX, move qemu_eventfd() to os-posix.c Jes.Sorensen
2010-10-15 14:05 ` [Qemu-devel] [PATCH 5/9] Move qemu_gettimeofday() to OS specific files Jes.Sorensen
2010-10-15 16:39 ` [Qemu-devel] " Paolo Bonzini
2010-10-16 14:27 ` Jes Sorensen
2010-10-15 14:05 ` [Qemu-devel] [PATCH 6/9] Do not redefine reserved key-words TRUE/FALSE Jes.Sorensen
2010-10-15 14:05 ` [Qemu-devel] [PATCH 7/9] Separate qemu_pidfile() into OS specific versions Jes.Sorensen
2010-10-15 14:05 ` Jes.Sorensen [this message]
2010-10-15 14:05 ` [Qemu-devel] [PATCH 9/9] Remove unncessary includes Jes.Sorensen
2010-10-15 16:41 ` [Qemu-devel] Re: [PATCH 0/9] Re-factor osdep code + macro and brace fixes Paolo Bonzini
-- strict thread matches above, loose matches on Subject: below --
2010-10-16 16:04 [Qemu-devel] [PATCH v2 " Jes.Sorensen
2010-10-16 16:04 ` [Qemu-devel] [PATCH 8/9] Consolidate oom_check() functions Jes.Sorensen
2010-10-18 8:15 [Qemu-devel] [PATCH v3 0/9] Re-factor osdep code + macro and brace fixes Jes.Sorensen
2010-10-18 8:15 ` [Qemu-devel] [PATCH 8/9] Consolidate oom_check() functions Jes.Sorensen
2010-10-26 8:39 [Qemu-devel] [PATCH v4 0/9] Re-factor osdep code + macro and brace fixes Jes.Sorensen
2010-10-26 8:39 ` [Qemu-devel] [PATCH 8/9] Consolidate oom_check() functions Jes.Sorensen
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=1287151553-16894-9-git-send-email-Jes.Sorensen@redhat.com \
--to=jes.sorensen@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).