Skip to contentSkip to conversation

introduce QEMU_AUTO_VFREE

PATCH Applied Vladimir Sementsov-Ogievskiy 2 participants 4 messages Jun 28, 2021 – Jun 29, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover introduce QEMU_AUTO_VFREE

Hi all!

There is a good movement to use g_autofree macro, that helps to automatically call g_free on exit from code block.

We lack similar possibility for qemu_memalign() functions family. Let's add, it seems rather simple with help of "cleanup" attribute.

v2: rewrite 01 (as first version was just wrong)

Vladimir Sementsov-Ogievskiy (2):
introduce QEMU_AUTO_VFREE
block/commit: use QEMU_AUTO_VFREE

 include/qemu/osdep.h | 15 +++++++++++++++
 block/commit.c       | 25 +++++++++----------------
 2 files changed, 24 insertions(+), 16 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v2 1/2 introduce QEMU_AUTO_VFREE

Introduce a convenient macro, that works for qemu_memalign() like g_autofree works with g_malloc.

---
 include/qemu/osdep.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
include/qemu/osdep.h +15 -0
index 0a54bf7be8..e3a62cce89 100644
@@ -370,6 +370,21 @@ void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
 void qemu_vfree(void *ptr);
 void qemu_anon_ram_free(void *ptr, size_t size);
 
+/*
+ * It's an analog of GLIB's g_autoptr_cleanup_generic_gfree(), used to define
+ * g_autofree macro.
+ */
+static inline void qemu_cleanup_generic_vfree(void *p)
+{
+  void **pp = (void **)p;
+  qemu_vfree(*pp);
+}
+
+/*
+ * Analog of g_autofree, but qemu_vfree is called on cleanup instead of g_free.
+ */
+#define QEMU_AUTO_VFREE __attribute__((cleanup(qemu_cleanup_generic_vfree)))
+
 /*
  * Abstraction of PROT_ and MAP_ flags as passed to mmap(), for example,
  * consumed by qemu_ram_mmap().
Signature · 1 line
2.29.2
#2 lore raw

PATCH v2 2/2 block/commit: use QEMU_AUTO_VFREE

In reply to Vladimir Sementsov-Ogievskiy
---
 block/commit.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
block/commit.c +9 -16
index b7f0c7c061..42792b4556 100644
@@ -119,24 +119,24 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
     uint64_t delay_ns = 0;
     int ret = 0;
     int64_t n = 0; /* bytes */
-    void *buf = NULL;
+    QEMU_AUTO_VFREE void *buf = NULL;
     int64_t len, base_len;
 
-    ret = len = blk_getlength(s->top);
+    len = blk_getlength(s->top);
     if (len < 0) {
-        goto out;
+        return len;
     }
     job_progress_set_remaining(&s->common.job, len);
 
-    ret = base_len = blk_getlength(s->base);
+    base_len = blk_getlength(s->base);
     if (base_len < 0) {
-        goto out;
+        return base_len;
     }
 
     if (base_len < len) {
         ret = blk_truncate(s->base, len, false, PREALLOC_MODE_OFF, 0, NULL);
         if (ret) {
-            goto out;
+            return ret;
         }
     }
 
@@ -174,7 +174,7 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
                 block_job_error_action(&s->common, s->on_error,
                                        error_in_source, -ret);
             if (action == BLOCK_ERROR_ACTION_REPORT) {
-                goto out;
+                return ret;
             } else {
                 n = 0;
                 continue;
@@ -190,12 +190,7 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
         }
     }
 
-    ret = 0;
-
-out:
-    qemu_vfree(buf);
-
-    return ret;
+    return 0;
 }
 
 static const BlockJobDriver commit_job_driver = {
@@ -435,7 +430,7 @@ int bdrv_commit(BlockDriverState *bs)
     int ro;
     int64_t n;
     int ret = 0;
-    uint8_t *buf = NULL;
+    QEMU_AUTO_VFREE uint8_t *buf = NULL;
     Error *local_err = NULL;
 
     if (!drv)
@@ -556,8 +551,6 @@ int bdrv_commit(BlockDriverState *bs)
 
     ret = 0;
 ro_cleanup:
-    qemu_vfree(buf);
-
     blk_unref(backing);
     if (bdrv_cow_bs(bs) != backing_file_bs) {
         bdrv_set_backing_hd(bs, backing_file_bs, &error_abort);
Signature · 1 line
2.29.2
Applied
#3 lore raw
In reply to Vladimir Sementsov-Ogievskiy

Am 28.06.2021 um 14:11 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 9 lines

Hi all!

There is a good movement to use g_autofree macro, that helps to automatically call g_free on exit from code block.

We lack similar possibility for qemu_memalign() functions family. Let's add, it seems rather simple with help of "cleanup" attribute.

v2: rewrite 01 (as first version was just wrong)

Thanks, applied to the block branch.

Kevin