提交 267f1e6d 编写于 作者: P Peter Krempa

internal: Introduce VIR_RETURN_PTR

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.
Signed-off-by: NPeter Krempa <pkrempa@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
上级 c9cec6a8
......@@ -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)
/**
* virCheckFlags:
* @supported: an OR'ed set of supported flags
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册