Allow preserving the holy serenity of the stack by clearing out temp
pointer when leaving the scope.
---
src/libvirt_private.syms | 1 +
src/util/viralloc.c | 7 +++++++
src/util/viralloc.h | 15 +++++++++++++++
3 files changed, 23 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index bae7f646fe..2f98ac901f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1425,6 +1425,7 @@ virAllocTestHook;
virAllocTestInit;
virAllocTestOOM;
virAllocVar;
+virClearPtr;
virDeleteElementsN;
virDispose;
virDisposeString;
diff --git a/src/util/viralloc.c b/src/util/viralloc.c
index e82bfa0acd..6dd19fb36b 100644
--- a/src/util/viralloc.c
+++ b/src/util/viralloc.c
@@ -634,3 +634,10 @@ virDisposeString(char **strptr)
virDispose(strptr, strlen(*strptr), sizeof(char), NULL);
}
+
+
+void
+virClearPtr(void *ptrptr)
+{
+ *(void **)ptrptr = NULL;
+}
diff --git a/src/util/viralloc.h b/src/util/viralloc.h
index c8133938bf..1b1d23b3af 100644
--- a/src/util/viralloc.h
+++ b/src/util/viralloc.h
@@ -81,6 +81,8 @@ void virDispose(void *ptrptr, size_t count, size_t element_size, size_t
*countpt
ATTRIBUTE_NONNULL(1);
void virDisposeString(char **strptr)
ATTRIBUTE_NONNULL(1);
+void virClearPtr(void *varptr)
+ ATTRIBUTE_NONNULL(1);
/**
* VIR_ALLOC:
@@ -694,4 +696,17 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
# define VIR_AUTOUNREF(type) \
__attribute__((cleanup(virObjectAutoUnref))) type
+/**
+ * VIR_TMP:
+ * @type: type of the pointer to be NULLed automatically
+ *
+ * Marks the pointer as temporary which should be cleared when leaving scope.
+ *
+ * This macro declares a temporary variable of @type which is set to NULL when
+ * the variable is leaving scope. This keeps the stack tidy. Note that no
+ * resources are freed or cleared otherwise. @type must be a pointer.
+ */
+# define VIR_TMP(type) \
+ __attribute__((cleanup(virClearPtr))) type
+
#endif /* LIBVIRT_VIRALLOC_H */
--
2.20.1