提交 d644f8be 编写于 作者: M malc

Add out of memory and zero size argument checks to be consistent with

the qemu_malloc routines
上级 e8d2a887
...@@ -51,10 +51,23 @@ ...@@ -51,10 +51,23 @@
#include "sysemu.h" #include "sysemu.h"
#include "qemu_socket.h" #include "qemu_socket.h"
#if !defined(_POSIX_C_SOURCE) || defined(_WIN32)
static void *oom_check(void *ptr)
{
if (ptr == NULL) {
abort();
}
return ptr;
}
#endif
#if defined(_WIN32) #if defined(_WIN32)
void *qemu_memalign(size_t alignment, size_t size) void *qemu_memalign(size_t alignment, size_t size)
{ {
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); if (!size) {
abort();
}
return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
} }
void *qemu_vmalloc(size_t size) void *qemu_vmalloc(size_t size)
...@@ -62,7 +75,10 @@ void *qemu_vmalloc(size_t size) ...@@ -62,7 +75,10 @@ void *qemu_vmalloc(size_t size)
/* FIXME: this is not exactly optimal solution since VirtualAlloc /* FIXME: this is not exactly optimal solution since VirtualAlloc
has 64Kb granularity, but at least it guarantees us that the has 64Kb granularity, but at least it guarantees us that the
memory is page aligned. */ memory is page aligned. */
return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); if (!size) {
abort();
}
return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
} }
void qemu_vfree(void *ptr) void qemu_vfree(void *ptr)
...@@ -106,6 +122,10 @@ static void *kqemu_vmalloc(size_t size) ...@@ -106,6 +122,10 @@ static void *kqemu_vmalloc(size_t size)
struct statfs stfs; struct statfs stfs;
#endif #endif
if (!size) {
abort ();
}
if (phys_ram_fd < 0) { if (phys_ram_fd < 0) {
tmpdir = getenv("QEMU_TMPDIR"); tmpdir = getenv("QEMU_TMPDIR");
if (!tmpdir) if (!tmpdir)
...@@ -188,12 +208,12 @@ void *qemu_memalign(size_t alignment, size_t size) ...@@ -188,12 +208,12 @@ void *qemu_memalign(size_t alignment, size_t size)
void *ptr; void *ptr;
ret = posix_memalign(&ptr, alignment, size); ret = posix_memalign(&ptr, alignment, size);
if (ret != 0) if (ret != 0)
return NULL; abort();
return ptr; return ptr;
#elif defined(HOST_BSD) #elif defined(HOST_BSD)
return valloc(size); return oom_check(valloc(size));
#else #else
return memalign(alignment, size); return oom_check(memalign(alignment, size));
#endif #endif
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册