[libvirt] [PATCH 04/23] util: add VIR_AUTOSTRUCT for directly calling a struct free function

Ján Tomko jtomko at redhat.com
Wed Jul 10 15:32:16 UTC 2019


On Thu, Jun 27, 2019 at 10:54:33AM +0100, Daniel P. Berrangé wrote:
>The current VIR_AUTOPTR macro assumes that the struct needs to have a
>auto-free function auto-generated to call the real free function.
>
>The new VIR_AUTOSTRUCT macro allows for structs which already have a
>free function which takes a pointer to a pointer to the struct and can
>thus be used directly for auto-cleanup.
>
>Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
>---
> src/util/viralloc.h | 13 +++++++++++++
> 1 file changed, 13 insertions(+)
>
>diff --git a/src/util/viralloc.h b/src/util/viralloc.h
>index 2b82096fde..5de18b9ea0 100644
>--- a/src/util/viralloc.h
>+++ b/src/util/viralloc.h
>@@ -614,3 +614,16 @@ void virAllocTestHook(void (*func)(int, void*), void *data);
>  * when the variable goes out of scope.
>  */
> #define VIR_AUTOFREE(type) __attribute__((cleanup(virFree))) type
>+
>+/**
>+ * VIR_AUTOSTRUCT:
>+ * @type: type of the struct variable to be freed automatically
>+ *
>+ * Macro to automatically free the memory allocated to
>+ * the struct variable declared with it by calling vir$STRUCTFree
>+ * when the variable goes out of scope.
>+ *
>+ * The vir$STRUCTFree function must take a pointer to a pointer
>+ * to the struct.
>+ */
>+#define VIR_AUTOSTRUCT(type) __attribute__((cleanup(type ## Free))) type

commit a4bfc2521f8aeff8f4e4431a8e2332cc74806b8a
    util: Move the VIR_AUTO(CLEAN|PTR) helper macros into a separate header

moved the macros not depending on other stuff from viralloc.h to virautoclean.h

This macro would better fit there.

Also, sc_require_attribute_cleanup_initialization will need a touch-up.
Reviewed-by: Ján Tomko <jtomko at redhat.com>

Jano
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20190710/ee55c98f/attachment-0001.sig>


More information about the libvir-list mailing list