Patchwork [8/9] Consolidate oom_check() functions

login
register
mail settings
Submitter Jes Sorensen
Date Oct. 16, 2010, 4:04 p.m.
Message ID <1287245083-26110-9-git-send-email-Jes.Sorensen@redhat.com>
Download mbox | patch
Permalink /patch/68045/
State New
Headers show

Comments

Jes Sorensen - Oct. 16, 2010, 4:04 p.m.
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>
---
 oslib-posix.c |    8 +++-----
 oslib-win32.c |    6 +++---
 qemu-common.h |    1 +
 qemu-malloc.c |   14 +++-----------
 4 files changed, 10 insertions(+), 19 deletions(-)
Blue Swirl - Oct. 16, 2010, 7:02 p.m.
On Sat, Oct 16, 2010 at 4:04 PM,  <Jes.Sorensen@redhat.com> wrote:
> 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.

This would break user emulators:
  LINK  i386-linux-user/qemu-i386
qemu-malloc.o: In function `qemu_realloc':
/src/qemu/qemu-malloc.c:60: undefined reference to `qemu_oom_check'
qemu-malloc.o: In function `qemu_malloc':
/src/qemu/qemu-malloc.c:49: undefined reference to `qemu_oom_check'
qemu-malloc.o: In function `qemu_mallocz':
/src/qemu/qemu-malloc.c:70: undefined reference to `qemu_oom_check'
collect2: ld returned 1 exit status
Jes Sorensen - Oct. 18, 2010, 7:18 a.m.
On 10/16/10 21:02, Blue Swirl wrote:
> On Sat, Oct 16, 2010 at 4:04 PM,  <Jes.Sorensen@redhat.com> wrote:
>> 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.
> 
> This would break user emulators:
>   LINK  i386-linux-user/qemu-i386
> qemu-malloc.o: In function `qemu_realloc':
> /src/qemu/qemu-malloc.c:60: undefined reference to `qemu_oom_check'
> qemu-malloc.o: In function `qemu_malloc':
> /src/qemu/qemu-malloc.c:49: undefined reference to `qemu_oom_check'
> qemu-malloc.o: In function `qemu_mallocz':
> /src/qemu/qemu-malloc.c:70: undefined reference to `qemu_oom_check'
> collect2: ld returned 1 exit status

I'll have a look.

Thanks,
Jes

Patch

diff --git a/oslib-posix.c b/oslib-posix.c
index ad44b17..6e9b0c3 100644
--- a/oslib-posix.c
+++ b/oslib-posix.c
@@ -31,8 +31,7 @@ 
 #include "trace.h"
 #include "qemu_socket.h"
 
-#if !defined(_POSIX_C_SOURCE) || defined(__sun__)
-static void *oom_check(void *ptr)
+void *qemu_oom_check(void *ptr)
 {
     if (ptr == NULL) {
         fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
@@ -40,7 +39,6 @@  static void *oom_check(void *ptr)
     }
     return ptr;
 }
-#endif
 
 void *qemu_memalign(size_t alignment, size_t size)
 {
@@ -54,9 +52,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/oslib-win32.c b/oslib-win32.c
index e03c472..ab29eae 100644
--- a/oslib-win32.c
+++ b/oslib-win32.c
@@ -31,7 +31,7 @@ 
 #include "trace.h"
 #include "qemu_socket.h"
 
-static void *oom_check(void *ptr)
+void *qemu_oom_check(void *ptr)
 {
     if (ptr == NULL) {
         fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
@@ -47,7 +47,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;
 }
@@ -62,7 +62,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/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)