introduce QEMU_AUTO_VFREE
authorVladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Mon, 28 Jun 2021 12:11:32 +0000 (15:11 +0300)
committerKevin Wolf <kwolf@redhat.com>
Tue, 29 Jun 2021 14:51:21 +0000 (16:51 +0200)
Introduce a convenient macro, that works for qemu_memalign() like
g_autofree works with g_malloc.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210628121133.193984-2-vsementsov@virtuozzo.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
include/qemu/osdep.h

index c3656b755af9823e527d9fed3cbd9d4ec846cd19..c91a78b5e617e4cd9cc659ea99e68eec77a36b4a 100644 (file)
@@ -386,6 +386,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().