[PATCH v3 5/7] qapi: Introduce QAPI_LIST_APPEND

Eric Blake posted 7 patches 4 years, 9 months ago
There is a newer version of this series
[PATCH v3 5/7] qapi: Introduce QAPI_LIST_APPEND
Posted by Eric Blake 4 years, 9 months ago
Similar to the existing QAPI_LIST_PREPEND, but designed for use where
we want to preserve insertion order.  Callers will be added in
upcoming patches.  Note the difference in signature: PREPEND takes
List*, APPEND takes List**.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 include/qapi/util.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/include/qapi/util.h b/include/qapi/util.h
index 6178e98e97a5..8b4967990c0d 100644
--- a/include/qapi/util.h
+++ b/include/qapi/util.h
@@ -37,4 +37,17 @@ int parse_qapi_name(const char *name, bool complete);
     (list) = _tmp; \
 } while (0)

+/*
+ * For any pointer to a GenericList @tail, insert @element at the back and
+ * update the tail.
+ *
+ * Note that this macro evaluates @element exactly once, so it is safe
+ * to have side-effects with that argument.
+ */
+#define QAPI_LIST_APPEND(tail, element) do { \
+    *(tail) = g_malloc0(sizeof(**(tail))); \
+    (*(tail))->value = (element); \
+    (tail) = &(*tail)->next; \
+} while (0)
+
 #endif
-- 
2.29.2


Re: [PATCH v3 5/7] qapi: Introduce QAPI_LIST_APPEND
Posted by Vladimir Sementsov-Ogievskiy 4 years, 9 months ago
24.12.2020 01:11, Eric Blake wrote:
> Similar to the existing QAPI_LIST_PREPEND, but designed for use where
> we want to preserve insertion order.  Callers will be added in
> upcoming patches.  Note the difference in signature: PREPEND takes
> List*, APPEND takes List**.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>   include/qapi/util.h | 13 +++++++++++++
>   1 file changed, 13 insertions(+)
> 
> diff --git a/include/qapi/util.h b/include/qapi/util.h
> index 6178e98e97a5..8b4967990c0d 100644
> --- a/include/qapi/util.h
> +++ b/include/qapi/util.h
> @@ -37,4 +37,17 @@ int parse_qapi_name(const char *name, bool complete);
>       (list) = _tmp; \
>   } while (0)
> 
> +/*
> + * For any pointer to a GenericList @tail, insert @element at the back and
> + * update the tail.

Would be more obvious for me, if directly mention that tail is a pointer to 'next' field in the last element of the list.

> + *
> + * Note that this macro evaluates @element exactly once, so it is safe
> + * to have side-effects with that argument.
> + */
> +#define QAPI_LIST_APPEND(tail, element) do { \
> +    *(tail) = g_malloc0(sizeof(**(tail))); \
> +    (*(tail))->value = (element); \
> +    (tail) = &(*tail)->next; \
> +} while (0)
> +
>   #endif
> 

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

-- 
Best regards,
Vladimir

Re: [PATCH v3 5/7] qapi: Introduce QAPI_LIST_APPEND
Posted by Markus Armbruster 4 years, 8 months ago
Eric Blake <eblake@redhat.com> writes:

> Similar to the existing QAPI_LIST_PREPEND, but designed for use where
> we want to preserve insertion order.  Callers will be added in
> upcoming patches.  Note the difference in signature: PREPEND takes
> List*, APPEND takes List**.
>
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  include/qapi/util.h | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/include/qapi/util.h b/include/qapi/util.h
> index 6178e98e97a5..8b4967990c0d 100644
> --- a/include/qapi/util.h
> +++ b/include/qapi/util.h
> @@ -37,4 +37,17 @@ int parse_qapi_name(const char *name, bool complete);
>      (list) = _tmp; \
>  } while (0)
>
> +/*
> + * For any pointer to a GenericList @tail, insert @element at the back and
> + * update the tail.
> + *
> + * Note that this macro evaluates @element exactly once, so it is safe
> + * to have side-effects with that argument.
> + */
> +#define QAPI_LIST_APPEND(tail, element) do { \
> +    *(tail) = g_malloc0(sizeof(**(tail))); \
> +    (*(tail))->value = (element); \
> +    (tail) = &(*tail)->next; \
> +} while (0)
> +
>  #endif

You mentioned parenthesizing the lone unparenthesized occurence of
@tail, like

  +    (tail) = &(*(tail))->next; \

Did you decide not to?


Re: [PATCH v3 5/7] qapi: Introduce QAPI_LIST_APPEND
Posted by Eric Blake 4 years, 8 months ago
On 1/13/21 7:04 AM, Markus Armbruster wrote:
> Eric Blake <eblake@redhat.com> writes:
> 
>> Similar to the existing QAPI_LIST_PREPEND, but designed for use where
>> we want to preserve insertion order.  Callers will be added in
>> upcoming patches.  Note the difference in signature: PREPEND takes
>> List*, APPEND takes List**.
>>
>> Signed-off-by: Eric Blake <eblake@redhat.com>
>> ---
>>  include/qapi/util.h | 13 +++++++++++++
>>  1 file changed, 13 insertions(+)
>>
>> diff --git a/include/qapi/util.h b/include/qapi/util.h
>> index 6178e98e97a5..8b4967990c0d 100644
>> --- a/include/qapi/util.h
>> +++ b/include/qapi/util.h
>> @@ -37,4 +37,17 @@ int parse_qapi_name(const char *name, bool complete);
>>      (list) = _tmp; \
>>  } while (0)
>>
>> +/*
>> + * For any pointer to a GenericList @tail, insert @element at the back and
>> + * update the tail.
>> + *
>> + * Note that this macro evaluates @element exactly once, so it is safe
>> + * to have side-effects with that argument.
>> + */
>> +#define QAPI_LIST_APPEND(tail, element) do { \
>> +    *(tail) = g_malloc0(sizeof(**(tail))); \
>> +    (*(tail))->value = (element); \
>> +    (tail) = &(*tail)->next; \
>> +} while (0)
>> +
>>  #endif
> 
> You mentioned parenthesizing the lone unparenthesized occurence of
> @tail, like
> 
>   +    (tail) = &(*(tail))->next; \
> 
> Did you decide not to?

Hmm; not sure what happened. I still want the () added.  I'll respin
anyways, since my v3 crossed paths with what you already checked in.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org