Add helpers for using automatic stack'd variable cleaning for lockable
objects.

Signed-off-by: Peter Krempa <pkre...@redhat.com>
---
 src/libvirt_private.syms |  2 ++
 src/util/virobject.c     | 29 +++++++++++++++++++++++++++++
 src/util/virobject.h     | 30 ++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 7b681fac64..5c8fd6a929 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2635,6 +2635,8 @@ virClassForObjectRWLockable;
 virClassIsDerivedFrom;
 virClassName;
 virClassNew;
+virObjectAutoRelease;
+virObjectAutoUnlock;
 virObjectAutoUnref;
 virObjectFreeCallback;
 virObjectFreeHashData;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 919519735a..6bc8b08c89 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -381,6 +381,35 @@ virObjectAutoUnref(void *objptr)
 }


+/**
+ * virObjectAutoUnlock:
+ *
+ * Helper used by VIR_AUTOUNLOCK
+ */
+void
+virObjectAutoUnlock(void *objptr)
+{
+    virObjectPtr *obj = objptr;
+    virObjectUnlock(*obj);
+    *obj = NULL;
+}
+
+
+/**
+ * virObjectAutoRelease:
+ *
+ * Helper used by VIR_AUTORELEASE
+ */
+void
+virObjectAutoRelease(void *objptr)
+{
+    virObjectPtr *obj = objptr;
+    virObjectUnlock(*obj);
+    virObjectUnref(*obj);
+    *obj = NULL;
+}
+
+
 /**
  * virObjectRef:
  * @anyobj: any instance of virObjectPtr
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 6fca4cd166..2f3f6d207e 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -112,6 +112,12 @@ virObjectUnref(void *obj);
 void
 virObjectAutoUnref(void *objptr);

+void
+virObjectAutoUnlock(void *objptr);
+
+void
+virObjectAutoRelease(void *objptr);
+
 /**
  * VIR_AUTOUNREF:
  * @type: type of an virObject subclass to be unref'd automatically
@@ -124,6 +130,30 @@ virObjectAutoUnref(void *objptr);
 #define VIR_AUTOUNREF(type) \
     __attribute__((cleanup(virObjectAutoUnref))) type

+/**
+ * VIR_AUTOUNLOCK:
+ * @type: type of an virObjectLockable subclass to be unlocked automatically
+ *
+ * Declares a variable of @type which will be automatically unlocked when
+ * control goes out of the scope. The lockable object referenced by the pointer
+ * assigned to the variable declared by this macro must already be locked
+ * at the time of assignment.
+ */
+#define VIR_AUTOUNLOCK(type) \
+    __attribute__((cleanup(virObjectAutoUnlock))) type
+
+/**
+ * VIR_AUTORELEASE:
+ * @type: type of an virObjectLockable subclass to be unlocked and unref'd 
automatically
+ *
+ * Declares a variable of @type which will be automatically unlocked and 
unref'd
+ * when control goes out of the scope. The lockable object referenced by the
+ * pointer assigned to the variable declared by this macro must already be
+ * locked and referenced at the time of assignment.
+ */
+#define VIR_AUTORELEASE(type) \
+    __attribute__((cleanup(virObjectAutoRelease))) type
+
 void *
 virObjectRef(void *obj);

-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to