[libvirt] [PATCH 09/11] internal: Introduce VIR_RETURN_PTR

Ján Tomko jtomko at redhat.com
Tue Apr 9 15:49:18 UTC 2019


On Fri, Apr 05, 2019 at 06:04:21PM +0200, Peter Krempa wrote:
>With the introduction of more and more internal data types which support
>VIR_AUTOPTR it's becoming common to see the following pattern:
>
>  VIR_AUTOPTR(virSomething) some = NULL
>  virSomethingPtr ret = NULL;
>
>  ... (ret is not touched ) ...
>
>  VIR_STEAL_PTR(ret, some);
>  return ret;
>
>This patch introduces a macro named VIR_RETURN_PTR which returns the
>pointer directly without the need for an explicitly defined return
>variable and use of VIR_STEAL_PTR. Internally obviously a temporary
>pointer is created to allow setting the original pointer to NULL so that
>the VIR_AUTOPTR function does not free the memory which we want to
>actually return.
>
>The name of the temporary variable is deliberately long and complex to
>minimize the possibility of collision.

Both my gcc and clang warn in that case:
conf/domain_conf.c:2209:5: error: declaration shadows a local variable [-Werror,-Wshadow]
    VIR_RETURN_PTR(virTemporaryReturnPointer);
    ^
./internal.h:292:21: note: expanded from macro 'VIR_RETURN_PTR'
        typeof(ptr) virTemporaryReturnPointer = (ptr); \
                    ^
conf/domain_conf.c:2199:26: note: previous declaration is here
    virDomainVsockDefPtr virTemporaryReturnPointer = NULL;
                         ^
>
>Signed-off-by: Peter Krempa <pkrempa at redhat.com>
>---
> src/internal.h | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>
>diff --git a/src/internal.h b/src/internal.h
>index cf03a82105..5a868bb00c 100644
>--- a/src/internal.h
>+++ b/src/internal.h
>@@ -279,6 +279,21 @@
>         (b) = NULL; \
>     } while (0)
>
>+/**
>+ * VIR_RETURN_PTR:
>+ * @ret: pointer to return
>+ *
>+ * Returns value of @ret while clearing @ret. This ensures that pointers
>+ * freed by using VIR_AUTOPTR can be easily passed back to the caller without
>+ * any temporary variable. @ptr is evaluated more than once.
>+ */
>+# define VIR_RETURN_PTR(ptr) \
>+    do { \
>+        typeof(ptr) virTemporaryReturnPointer = (ptr); \
>+        (ptr) = NULL; \
>+        return virTemporaryReturnPointer; \
>+    } while (0)
>+
> /**

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/20190409/4a495fe9/attachment-0001.sig>


More information about the libvir-list mailing list