yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
05bba0
From 90fad0c40a1947e229fdf82a30219e33a3e1dad8 Mon Sep 17 00:00:00 2001
05bba0
From: Max Reitz <mreitz@redhat.com>
05bba0
Date: Sat, 13 Jun 2015 16:21:57 +0200
05bba0
Subject: [PATCH 03/42] block: Introduce qemu_try_blockalign()
05bba0
05bba0
Message-id: <1434212556-3927-4-git-send-email-mreitz@redhat.com>
05bba0
Patchwork-id: 66022
05bba0
O-Subject: [RHEL-7.2 qemu-kvm PATCH 03/42] block: Introduce qemu_try_blockalign()
05bba0
Bugzilla: 1129893
05bba0
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
05bba0
RH-Acked-by: Fam Zheng <famz@redhat.com>
05bba0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
05bba0
From: Kevin Wolf <kwolf@redhat.com>
05bba0
05bba0
BZ: 1129893
05bba0
05bba0
This function returns NULL instead of aborting when an allocation fails.
05bba0
05bba0
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
05bba0
Reviewed-by: Benoit Canet <benoit@irqsave.net>
05bba0
(cherry picked from commit 7d2a35cc921ea4832083a7e8598461868bb538ce)
05bba0
05bba0
Signed-off-by: Max Reitz <mreitz@redhat.com>
05bba0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
---
05bba0
 block.c               | 13 +++++++++++++
05bba0
 include/block/block.h |  1 +
05bba0
 include/qemu/osdep.h  |  1 +
05bba0
 util/oslib-posix.c    | 16 ++++++++++------
05bba0
 util/oslib-win32.c    |  9 +++++++--
05bba0
 5 files changed, 32 insertions(+), 8 deletions(-)
05bba0
05bba0
diff --git a/block.c b/block.c
05bba0
index f2caf20..caea2ea 100644
05bba0
--- a/block.c
05bba0
+++ b/block.c
05bba0
@@ -5175,6 +5175,19 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size)
05bba0
     return qemu_memalign(bdrv_opt_mem_align(bs), size);
05bba0
 }
05bba0
 
05bba0
+void *qemu_try_blockalign(BlockDriverState *bs, size_t size)
05bba0
+{
05bba0
+    size_t align = bdrv_opt_mem_align(bs);
05bba0
+
05bba0
+    /* Ensure that NULL is never returned on success */
05bba0
+    assert(align > 0);
05bba0
+    if (size == 0) {
05bba0
+        size = align;
05bba0
+    }
05bba0
+
05bba0
+    return qemu_try_memalign(align, size);
05bba0
+}
05bba0
+
05bba0
 /*
05bba0
  * Check if all memory in this vector is sector aligned.
05bba0
  */
05bba0
diff --git a/include/block/block.h b/include/block/block.h
05bba0
index 3170cbc..7b538b7 100644
05bba0
--- a/include/block/block.h
05bba0
+++ b/include/block/block.h
05bba0
@@ -417,6 +417,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
05bba0
 size_t bdrv_opt_mem_align(BlockDriverState *bs);
05bba0
 void bdrv_set_guest_block_size(BlockDriverState *bs, int align);
05bba0
 void *qemu_blockalign(BlockDriverState *bs, size_t size);
05bba0
+void *qemu_try_blockalign(BlockDriverState *bs, size_t size);
05bba0
 bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov);
05bba0
 
05bba0
 struct HBitmapIter;
05bba0
diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
05bba0
index 7a5ae28..8984da0 100644
05bba0
--- a/include/qemu/osdep.h
05bba0
+++ b/include/qemu/osdep.h
05bba0
@@ -95,6 +95,7 @@ typedef signed int              int_fast16_t;
05bba0
 #define qemu_printf printf
05bba0
 
05bba0
 int qemu_daemon(int nochdir, int noclose);
05bba0
+void *qemu_try_memalign(size_t alignment, size_t size);
05bba0
 void *qemu_memalign(size_t alignment, size_t size);
05bba0
 void *qemu_anon_ram_alloc(size_t size);
05bba0
 void qemu_vfree(void *ptr);
05bba0
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
05bba0
index fef840a..f0fb297 100644
05bba0
--- a/util/oslib-posix.c
05bba0
+++ b/util/oslib-posix.c
05bba0
@@ -83,7 +83,7 @@ void *qemu_oom_check(void *ptr)
05bba0
     return ptr;
05bba0
 }
05bba0
 
05bba0
-void *qemu_memalign(size_t alignment, size_t size)
05bba0
+void *qemu_try_memalign(size_t alignment, size_t size)
05bba0
 {
05bba0
     void *ptr;
05bba0
 
05bba0
@@ -95,19 +95,23 @@ void *qemu_memalign(size_t alignment, size_t size)
05bba0
     int ret;
05bba0
     ret = posix_memalign(&ptr, alignment, size);
05bba0
     if (ret != 0) {
05bba0
-        fprintf(stderr, "Failed to allocate %zu B: %s\n",
05bba0
-                size, strerror(ret));
05bba0
-        abort();
05bba0
+        errno = ret;
05bba0
+        ptr = NULL;
05bba0
     }
05bba0
 #elif defined(CONFIG_BSD)
05bba0
-    ptr = qemu_oom_check(valloc(size));
05bba0
+    ptr = valloc(size);
05bba0
 #else
05bba0
-    ptr = qemu_oom_check(memalign(alignment, size));
05bba0
+    ptr = memalign(alignment, size);
05bba0
 #endif
05bba0
     trace_qemu_memalign(alignment, size, ptr);
05bba0
     return ptr;
05bba0
 }
05bba0
 
05bba0
+void *qemu_memalign(size_t alignment, size_t size)
05bba0
+{
05bba0
+    return qemu_oom_check(qemu_try_memalign(alignment, size));
05bba0
+}
05bba0
+
05bba0
 /* alloc shared memory pages */
05bba0
 void *qemu_anon_ram_alloc(size_t size)
05bba0
 {
05bba0
diff --git a/util/oslib-win32.c b/util/oslib-win32.c
05bba0
index 332e743..0fb78ef 100644
05bba0
--- a/util/oslib-win32.c
05bba0
+++ b/util/oslib-win32.c
05bba0
@@ -46,18 +46,23 @@ void *qemu_oom_check(void *ptr)
05bba0
     return ptr;
05bba0
 }
05bba0
 
05bba0
-void *qemu_memalign(size_t alignment, size_t size)
05bba0
+void *qemu_try_memalign(size_t alignment, size_t size)
05bba0
 {
05bba0
     void *ptr;
05bba0
 
05bba0
     if (!size) {
05bba0
         abort();
05bba0
     }
05bba0
-    ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
05bba0
+    ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
05bba0
     trace_qemu_memalign(alignment, size, ptr);
05bba0
     return ptr;
05bba0
 }
05bba0
 
05bba0
+void *qemu_memalign(size_t alignment, size_t size)
05bba0
+{
05bba0
+    return qemu_oom_check(qemu_try_memalign(alignment, size));
05bba0
+}
05bba0
+
05bba0
 void *qemu_anon_ram_alloc(size_t size)
05bba0
 {
05bba0
     void *ptr;
05bba0
-- 
05bba0
1.8.3.1
05bba0