[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v4 01/21] block: Introduce qemu_try_blockalign()
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PATCH v4 01/21] block: Introduce qemu_try_blockalign() |
Date: |
Tue, 24 Jun 2014 17:36:43 +0200 |
This function returns NULL instead of aborting when an allocation fails.
Signed-off-by: Kevin Wolf <address@hidden>
Reviewed-by: Benoit Canet <address@hidden>
---
block.c | 13 +++++++++++++
include/block/block.h | 1 +
include/qemu/osdep.h | 1 +
util/oslib-posix.c | 16 ++++++++++------
util/oslib-win32.c | 9 +++++++--
5 files changed, 32 insertions(+), 8 deletions(-)
diff --git a/block.c b/block.c
index 43abe96..cebbaf7 100644
--- a/block.c
+++ b/block.c
@@ -5250,6 +5250,19 @@ void *qemu_blockalign(BlockDriverState *bs, size_t 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.
*/
diff --git a/include/block/block.h b/include/block/block.h
index f15b99b..22aa333 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -452,6 +452,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
size_t bdrv_opt_mem_align(BlockDriverState *bs);
void bdrv_set_guest_block_size(BlockDriverState *bs, int align);
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);
struct HBitmapIter;
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
index 6d35c1b..a39b978 100644
--- a/include/qemu/osdep.h
+++ b/include/qemu/osdep.h
@@ -95,6 +95,7 @@ typedef signed int int_fast16_t;
#define qemu_printf printf
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_anon_ram_alloc(size_t size);
void qemu_vfree(void *ptr);
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 1524ead..abbf8b2 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -94,7 +94,7 @@ void *qemu_oom_check(void *ptr)
return ptr;
}
-void *qemu_memalign(size_t alignment, size_t size)
+void *qemu_try_memalign(size_t alignment, size_t size)
{
void *ptr;
@@ -106,19 +106,23 @@ void *qemu_memalign(size_t alignment, size_t size)
int ret;
ret = posix_memalign(&ptr, alignment, size);
if (ret != 0) {
- fprintf(stderr, "Failed to allocate %zu B: %s\n",
- size, strerror(ret));
- abort();
+ errno = ret;
+ ptr = NULL;
}
#elif defined(CONFIG_BSD)
- ptr = qemu_oom_check(valloc(size));
+ ptr = valloc(size);
#else
- ptr = qemu_oom_check(memalign(alignment, size));
+ ptr = memalign(alignment, size);
#endif
trace_qemu_memalign(alignment, size, 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 */
void *qemu_anon_ram_alloc(size_t size)
{
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
index 507cedd..a3eab4a 100644
--- a/util/oslib-win32.c
+++ b/util/oslib-win32.c
@@ -50,18 +50,23 @@ void *qemu_oom_check(void *ptr)
return ptr;
}
-void *qemu_memalign(size_t alignment, size_t size)
+void *qemu_try_memalign(size_t alignment, size_t size)
{
void *ptr;
if (!size) {
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);
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 *ptr;
--
1.8.3.1
- [Qemu-devel] [PATCH v4 15/21] rbd: Handle failure for potentially large allocations, (continued)
- [Qemu-devel] [PATCH v4 15/21] rbd: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/05
- [Qemu-devel] [PATCH v4 17/21] vhdx: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/05
- [Qemu-devel] [PATCH v4 18/21] vmdk: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/05
- [Qemu-devel] [PATCH v4 19/21] vpc: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/05
- [Qemu-devel] [PATCH v4 21/21] qcow2: Return useful error code in refcount_init(), Kevin Wolf, 2014/06/05
- [Qemu-devel] [PATCH v4 20/21] mirror: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/05
- Re: [Qemu-devel] [PATCH v4 00/21] block: Handle failure for potentially large allocations, Stefan Hajnoczi, 2014/06/11
- Re: [Qemu-devel] [PATCH v4 00/21] block: Handle failure for potentially large allocations, Stefan Hajnoczi, 2014/06/11
- [Qemu-devel] [PATCH v4 00/21] block: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 01/21] block: Introduce qemu_try_blockalign(),
Kevin Wolf <=
- [Qemu-devel] [PATCH v4 02/21] block: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 04/21] cloop: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 03/21] bochs: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 05/21] curl: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 06/21] dmg: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 07/21] iscsi: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 08/21] nfs: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 09/21] parallels: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 12/21] qed: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24
- [Qemu-devel] [PATCH v4 10/21] qcow1: Handle failure for potentially large allocations, Kevin Wolf, 2014/06/24