[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH V2] bring xxx-user qemu_malloc behavior up to speed
From: |
Jean-Christophe DUBOIS |
Subject: |
[Qemu-devel] [PATCH V2] bring xxx-user qemu_malloc behavior up to speed with main qemu_malloc |
Date: |
Tue, 8 Sep 2009 22:53:22 +0200 |
From: Jean-Christophe Dubois <address@hidden>
the linux-user and bsd-user qemu_malloc behavior is not the same as
the main qemu_malloc behavior
This patch tries to make the xxx-user behavior on par with the main
qemu_malloc behavior. In particular qemu_malloc and friends should
abort if memory allocation fails.
It also add the qemu_strdup and qemu_strndup function that were
missing.
This is the V2 version of the patch that was updated afert malc comments.
Signed-off-by: Jean-Christophe DUBOIS <address@hidden>
---
bsd-user/mmap.c | 49 +++++++++++++++++++++++++++++++++++++++++++------
linux-user/mmap.c | 51 +++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 88 insertions(+), 12 deletions(-)
diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index ff207cd..2e02372 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -83,6 +83,9 @@ void *qemu_vmalloc(size_t size)
p = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
+ if (p == MAP_FAILED)
+ abort();
+
addr = (unsigned long)p;
if (addr == (target_ulong) addr) {
/* Allocated region overlaps guest address space.
@@ -98,6 +101,10 @@ void *qemu_vmalloc(size_t size)
void *qemu_malloc(size_t size)
{
char * p;
+
+ if (!size)
+ abort();
+
size += 16;
p = qemu_vmalloc(size);
*(size_t *)p = size;
@@ -122,19 +129,49 @@ void qemu_free(void *ptr)
void *qemu_realloc(void *ptr, size_t size)
{
- size_t old_size, copy;
- void *new_ptr;
+ void *new_ptr = NULL;
if (!ptr)
return qemu_malloc(size);
- old_size = *(size_t *)((char *)ptr - 16);
- copy = old_size < size ? old_size : size;
- new_ptr = qemu_malloc(size);
- memcpy(new_ptr, ptr, copy);
+
+ if (size) {
+ size_t old_size, copy;
+
+ old_size = *(size_t *)((char *)ptr - 16);
+ copy = old_size < size ? old_size : size;
+ new_ptr = qemu_malloc(size);
+ memcpy(new_ptr, ptr, copy);
+ }
+
qemu_free(ptr);
return new_ptr;
}
+char *qemu_strdup(const char *str)
+{
+ char *ptr;
+ size_t len = strlen(str);
+ ptr = qemu_malloc(len + 1);
+
+ return memcpy(ptr, str, len + 1);
+}
+
+char *qemu_strndup(const char *str, size_t size)
+{
+ const char *end = memchr(str, 0, size);
+ char *ptr;
+
+ if (end) {
+ size = end - str;
+ }
+
+ ptr = qemu_malloc(size + 1);
+
+ ptr[size] = 0;
+
+ return memcpy(ptr, str, size);
+}
+
/* NOTE: all the constants are the HOST ones, but addresses are target. */
int target_mprotect(abi_ulong start, abi_ulong len, int prot)
{
diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index 6ce4167..1cb79ae 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -86,6 +86,9 @@ void *qemu_vmalloc(size_t size)
p = mmap(NULL, size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (p == MAP_FAILED)
+ abort();
+
addr = (unsigned long)p;
if (addr == (target_ulong) addr) {
/* Allocated region overlaps guest address space.
@@ -101,6 +104,10 @@ void *qemu_vmalloc(size_t size)
void *qemu_malloc(size_t size)
{
char * p;
+
+ if (!size)
+ abort();
+
size += 16;
p = qemu_vmalloc(size);
*(size_t *)p = size;
@@ -125,19 +132,49 @@ void qemu_free(void *ptr)
void *qemu_realloc(void *ptr, size_t size)
{
- size_t old_size, copy;
- void *new_ptr;
+ void *new_ptr = NULL;
if (!ptr)
return qemu_malloc(size);
- old_size = *(size_t *)((char *)ptr - 16);
- copy = old_size < size ? old_size : size;
- new_ptr = qemu_malloc(size);
- memcpy(new_ptr, ptr, copy);
+
+ if (size) {
+ size_t old_size, copy;
+
+ old_size = *(size_t *)((char *)ptr - 16);
+ copy = old_size < size ? old_size : size;
+ new_ptr = qemu_malloc(size);
+ memcpy(new_ptr, ptr, copy);
+ }
+
qemu_free(ptr);
return new_ptr;
}
+char *qemu_strdup(const char *str)
+{
+ char *ptr;
+
+ size_t len = strlen(str);
+
+ ptr = qemu_malloc(len + 1);
+
+ return memcpy(ptr, str, len + 1);
+}
+
+char *qemu_strndup(const char *str, size_t size)
+{
+ const char *end = memchr(str, 0, size);
+ char *ptr;
+
+ if (end)
+ size = end - str;
+
+ ptr = qemu_malloc(size + 1);
+
+ ptr[size] = 0;
+ return memcpy(ptr, str, size);
+}
+
/* NOTE: all the constants are the HOST ones, but addresses are target. */
int target_mprotect(abi_ulong start, abi_ulong len, int prot)
{
@@ -426,6 +463,8 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int
prot,
if (!(flags & MAP_ANONYMOUS)) {
p = mmap(g2h(mmap_start), len, prot,
flags | MAP_FIXED, fd, host_offset);
+ if (p == MAP_FAILED)
+ goto fail;
host_start += offset - host_offset;
}
start = h2g(host_start);
--
1.6.0.4
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Qemu-devel] [PATCH V2] bring xxx-user qemu_malloc behavior up to speed with main qemu_malloc,
Jean-Christophe DUBOIS <=