static void *oom_check(void *ptr)
{
- if (ptr == NULL)
+ if (ptr == NULL) {
abort();
+ }
return ptr;
}
free(ptr);
}
+static int allow_zero_malloc(void)
+{
+#if defined(CONFIG_ZERO_MALLOC)
+ return 1;
+#else
+ return 0;
+#endif
+}
+
void *qemu_malloc(size_t size)
{
- if (!size)
+ if (!size && !allow_zero_malloc()) {
abort();
- return oom_check(malloc(size));
+ }
+ return oom_check(malloc(size ? size : 1));
}
void *qemu_realloc(void *ptr, size_t size)
{
- if (size)
- return oom_check(realloc(ptr, size));
- else {
- if (ptr)
- return realloc(ptr, size);
+ if (!size && !allow_zero_malloc()) {
+ abort();
}
- abort();
+ return oom_check(realloc(ptr, size ? size : 1));
}
void *qemu_mallocz(size_t size)
const char *end = memchr(str, 0, size);
char *new;
- if (end)
+ if (end) {
size = end - str;
+ }
new = qemu_malloc(size + 1);
new[size] = 0;