Commit 7d2a35cc authored by Kevin Wolf's avatar Kevin Wolf

block: Introduce qemu_try_blockalign()

This function returns NULL instead of aborting when an allocation fails.
Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
Reviewed-by: default avatarBenoit Canet <benoit@irqsave.net>
parent 23d20b5b
...@@ -5258,6 +5258,19 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size) ...@@ -5258,6 +5258,19 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size)
return qemu_memalign(bdrv_opt_mem_align(bs), size); return qemu_memalign(bdrv_opt_mem_align(bs), size);
} }
void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
{
size_t align = bdrv_opt_mem_align(bs);
/* Ensure that NULL is never returned on success */
assert(align > 0);
if (size == 0) {
size = align;
}
return qemu_try_memalign(align, size);
}
/* /*
* Check if all memory in this vector is sector aligned. * Check if all memory in this vector is sector aligned.
*/ */
......
...@@ -455,6 +455,7 @@ void bdrv_img_create(const char *filename, const char *fmt, ...@@ -455,6 +455,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
size_t bdrv_opt_mem_align(BlockDriverState *bs); size_t bdrv_opt_mem_align(BlockDriverState *bs);
void bdrv_set_guest_block_size(BlockDriverState *bs, int align); void bdrv_set_guest_block_size(BlockDriverState *bs, int align);
void *qemu_blockalign(BlockDriverState *bs, size_t size); void *qemu_blockalign(BlockDriverState *bs, size_t size);
void *qemu_try_blockalign(BlockDriverState *bs, size_t size);
bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov); bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov);
struct HBitmapIter; struct HBitmapIter;
......
...@@ -95,6 +95,7 @@ typedef signed int int_fast16_t; ...@@ -95,6 +95,7 @@ typedef signed int int_fast16_t;
#define qemu_printf printf #define qemu_printf printf
int qemu_daemon(int nochdir, int noclose); int qemu_daemon(int nochdir, int noclose);
void *qemu_try_memalign(size_t alignment, size_t size);
void *qemu_memalign(size_t alignment, size_t size); void *qemu_memalign(size_t alignment, size_t size);
void *qemu_anon_ram_alloc(size_t size); void *qemu_anon_ram_alloc(size_t size);
void qemu_vfree(void *ptr); void qemu_vfree(void *ptr);
......
...@@ -94,7 +94,7 @@ void *qemu_oom_check(void *ptr) ...@@ -94,7 +94,7 @@ void *qemu_oom_check(void *ptr)
return ptr; return ptr;
} }
void *qemu_memalign(size_t alignment, size_t size) void *qemu_try_memalign(size_t alignment, size_t size)
{ {
void *ptr; void *ptr;
...@@ -106,19 +106,23 @@ void *qemu_memalign(size_t alignment, size_t size) ...@@ -106,19 +106,23 @@ void *qemu_memalign(size_t alignment, size_t size)
int ret; int ret;
ret = posix_memalign(&ptr, alignment, size); ret = posix_memalign(&ptr, alignment, size);
if (ret != 0) { if (ret != 0) {
fprintf(stderr, "Failed to allocate %zu B: %s\n", errno = ret;
size, strerror(ret)); ptr = NULL;
abort();
} }
#elif defined(CONFIG_BSD) #elif defined(CONFIG_BSD)
ptr = qemu_oom_check(valloc(size)); ptr = valloc(size);
#else #else
ptr = qemu_oom_check(memalign(alignment, size)); ptr = memalign(alignment, size);
#endif #endif
trace_qemu_memalign(alignment, size, ptr); trace_qemu_memalign(alignment, size, ptr);
return ptr; return ptr;
} }
void *qemu_memalign(size_t alignment, size_t size)
{
return qemu_oom_check(qemu_try_memalign(alignment, size));
}
/* alloc shared memory pages */ /* alloc shared memory pages */
void *qemu_anon_ram_alloc(size_t size) void *qemu_anon_ram_alloc(size_t size)
{ {
......
...@@ -50,18 +50,23 @@ void *qemu_oom_check(void *ptr) ...@@ -50,18 +50,23 @@ void *qemu_oom_check(void *ptr)
return ptr; return ptr;
} }
void *qemu_memalign(size_t alignment, size_t size) void *qemu_try_memalign(size_t alignment, size_t size)
{ {
void *ptr; void *ptr;
if (!size) { if (!size) {
abort(); abort();
} }
ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
trace_qemu_memalign(alignment, size, ptr); trace_qemu_memalign(alignment, size, ptr);
return ptr; return ptr;
} }
void *qemu_memalign(size_t alignment, size_t size)
{
return qemu_oom_check(qemu_try_memalign(alignment, size));
}
void *qemu_anon_ram_alloc(size_t size) void *qemu_anon_ram_alloc(size_t size)
{ {
void *ptr; void *ptr;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment