Add helper for utilizing __attribute__(cleanup())) for unref-ing
instances of sublasses of virObject.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/viralloc.h | 10 ++++++++++
src/util/virobject.c | 13 +++++++++++++
src/util/virobject.h | 3 +++
4 files changed, 27 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 0a959c4bf9..b6fec233f1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2511,6 +2511,7 @@ virClassForObjectRWLockable;
virClassIsDerivedFrom;
virClassName;
virClassNew;
+virObjectAutoUnref;
virObjectFreeCallback;
virObjectFreeHashData;
virObjectIsClass;
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
index aa1b92241d..1d67fff95c 100644
--- a/src/util/viralloc.h
+++ b/src/util/viralloc.h
@@ -637,4 +637,14 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
# define VIR_AUTOPTR(type) \
__attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type *
+/**
+ * VIR_AUTOUNREF:
+ * @type: type of an virObject subclass to be unref'd automatically
+ *
+ * Declares a variable of @type which will be automatically unref'd when
+ * control goes out of the scope.
+ */
+# define VIR_AUTOUNREF(type) \
+ __attribute__((cleanup(virObjectAutoUnref))) type
+
#endif /* LIBVIRT_VIRALLOC_H */
diff --git a/src/util/virobject.c b/src/util/virobject.c
index b6ea299420..a4cbd08077 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -362,6 +362,19 @@ virObjectUnref(void *anyobj)
}
+/**
+ * virObjectAutoUnref:
+ *
+ * Helper used by VIR_AUTOUNREF
+ */
+void
+virObjectAutoUnref(void *objptr)
+{
+ virObjectPtr *obj = objptr;
+ virObjectUnref(*obj);
+}
+
+
/**
* virObjectRef:
* @anyobj: any instance of virObjectPtr
diff --git a/src/util/virobject.h b/src/util/virobject.h
index b39b9946ff..56bc9f2324 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -102,6 +102,9 @@ virObjectNew(virClassPtr klass)
bool
virObjectUnref(void *obj);
+void
+virObjectAutoUnref(void *objptr);
+
void *
virObjectRef(void *obj);
--
2.20.1