diff mbox series

[v6,01/11] block: Simplify QAPI_LIST_ADD

Message ID 20201027050556.269064-2-eblake@redhat.com (mailing list archive)
State New, archived
Headers show
Series Exposing backing-chain allocation over NBD | expand

Commit Message

Eric Blake Oct. 27, 2020, 5:05 a.m. UTC
There is no need to rely on the verbosity of the gcc/clang compiler
extension of g_new(typeof(X), 1) when we can instead use the standard
g_malloc(sizeof(X)).  In general, we like g_new over g_malloc for
returning type X rather than void* to let the compiler catch more
potential typing mistakes, but in this particular macro, our other use
of typeof on the same line already ensures we are getting correct
results.

Suggested-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
---
 block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Vladimir Sementsov-Ogievskiy Oct. 27, 2020, 10:06 a.m. UTC | #1
27.10.2020 08:05, Eric Blake wrote:
> There is no need to rely on the verbosity of the gcc/clang compiler
> extension of g_new(typeof(X), 1) when we can instead use the standard
> g_malloc(sizeof(X)).  In general, we like g_new over g_malloc for
> returning type X rather than void* to let the compiler catch more
> potential typing mistakes, but in this particular macro, our other use
> of typeof on the same line already ensures we are getting correct
> results.
> 
> Suggested-by: Markus Armbruster<armbru@redhat.com>
> Signed-off-by: Eric Blake<eblake@redhat.com>

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Markus Armbruster Oct. 27, 2020, 12:58 p.m. UTC | #2
Eric Blake <eblake@redhat.com> writes:

> There is no need to rely on the verbosity of the gcc/clang compiler
> extension of g_new(typeof(X), 1) when we can instead use the standard
> g_malloc(sizeof(X)).  In general, we like g_new over g_malloc for
> returning type X rather than void* to let the compiler catch more
> potential typing mistakes, but in this particular macro, our other use
> of typeof on the same line already ensures we are getting correct
> results.
>
> Suggested-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  block.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/block.c b/block.c
> index 430edf79bb10..857387f3912f 100644
> --- a/block.c
> +++ b/block.c
> @@ -5231,7 +5231,7 @@ BlockDeviceInfoList *bdrv_named_nodes_list(bool flat,
>  }
>
>  #define QAPI_LIST_ADD(list, element) do { \
> -    typeof(list) _tmp = g_new(typeof(*(list)), 1); \
> +    typeof(list) _tmp = g_malloc(sizeof(*(list))); \
>      _tmp->value = (element); \
>      _tmp->next = (list); \
>      (list) = _tmp; \

Reviewed-by: Markus Armbruster <armbru@redhat.com>
diff mbox series

Patch

diff --git a/block.c b/block.c
index 430edf79bb10..857387f3912f 100644
--- a/block.c
+++ b/block.c
@@ -5231,7 +5231,7 @@  BlockDeviceInfoList *bdrv_named_nodes_list(bool flat,
 }

 #define QAPI_LIST_ADD(list, element) do { \
-    typeof(list) _tmp = g_new(typeof(*(list)), 1); \
+    typeof(list) _tmp = g_malloc(sizeof(*(list))); \
     _tmp->value = (element); \
     _tmp->next = (list); \
     (list) = _tmp; \