diff mbox

[1/3] qdev-properties.h: Explicitly set the default value for arraylen properties

Message ID 1499788408-10096-2-git-send-email-peter.maydell@linaro.org
State New
Headers show

Commit Message

Peter Maydell July 11, 2017, 3:53 p.m. UTC
In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen)
which has a .set_default_value member we will set the field to a default
value. That default value will be zero, by the C rule that struct
initialization sets unmentioned members to zero if at least one member
is initialized. However it's clearer to state it explicitly.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
If my eyeball of the sources is correct, this is the only case we
have of a Property struct that uses a PropertyInfo with a non-NULL
.set_default_value but which doesn't explicitly set .defval.u.
---
 include/hw/qdev-properties.h | 1 +
 1 file changed, 1 insertion(+)

Comments

Marc-André Lureau July 11, 2017, 4:46 p.m. UTC | #1
----- Original Message -----
> In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen)
> which has a .set_default_value member we will set the field to a default
> value. That default value will be zero, by the C rule that struct
> initialization sets unmentioned members to zero if at least one member
> is initialized. However it's clearer to state it explicitly.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
> If my eyeball of the sources is correct, this is the only case we
> have of a Property struct that uses a PropertyInfo with a non-NULL
> .set_default_value but which doesn't explicitly set .defval.u.
> ---
>  include/hw/qdev-properties.h | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
> index 0604c33..36d040c 100644
> --- a/include/hw/qdev-properties.h
> +++ b/include/hw/qdev-properties.h
> @@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
>                            _arrayfield, _arrayprop, _arraytype) {        \
>          .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
>          .info = &(qdev_prop_arraylen),                                  \
> +        .defval.u = 0,                                                  \
>          .offset = offsetof(_state, _field)                              \
>              + type_check(uint32_t, typeof_field(_state, _field)),       \
>          .arrayinfo = &(_arrayprop),                                     \
> --
> 2.7.4
> 
>
Markus Armbruster July 13, 2017, 2:11 p.m. UTC | #2
Peter Maydell <peter.maydell@linaro.org> writes:

> In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen)
> which has a .set_default_value member we will set the field to a default
> value. That default value will be zero, by the C rule that struct
> initialization sets unmentioned members to zero if at least one member
> is initialized. However it's clearer to state it explicitly.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> If my eyeball of the sources is correct, this is the only case we
> have of a Property struct that uses a PropertyInfo with a non-NULL
> .set_default_value but which doesn't explicitly set .defval.u.
> ---
>  include/hw/qdev-properties.h | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
> index 0604c33..36d040c 100644
> --- a/include/hw/qdev-properties.h
> +++ b/include/hw/qdev-properties.h
> @@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
>                            _arrayfield, _arrayprop, _arraytype) {        \
>          .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
>          .info = &(qdev_prop_arraylen),                                  \
> +        .defval.u = 0,                                                  \
>          .offset = offsetof(_state, _field)                              \
>              + type_check(uint32_t, typeof_field(_state, _field)),       \
>          .arrayinfo = &(_arrayprop),                                     \

Hmm.

.info and .defval apply to the array *length*.
.info.set_default_value() is set_default_uint(), which uses .defval.u.
Setting it here is correct.

The array *elements* are described by .arrayinfo.  There is no
.arraydefval, but that's okay, because .arrayinfo.set_default_value()
doesn't get called.  Correct?
Peter Maydell July 13, 2017, 2:26 p.m. UTC | #3
On 13 July 2017 at 15:11, Markus Armbruster <armbru@redhat.com> wrote:
> Peter Maydell <peter.maydell@linaro.org> writes:
>
>> In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen)
>> which has a .set_default_value member we will set the field to a default
>> value. That default value will be zero, by the C rule that struct
>> initialization sets unmentioned members to zero if at least one member
>> is initialized. However it's clearer to state it explicitly.
>>
>> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
>> ---
>> If my eyeball of the sources is correct, this is the only case we
>> have of a Property struct that uses a PropertyInfo with a non-NULL
>> .set_default_value but which doesn't explicitly set .defval.u.
>> ---
>>  include/hw/qdev-properties.h | 1 +
>>  1 file changed, 1 insertion(+)
>>
>> diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
>> index 0604c33..36d040c 100644
>> --- a/include/hw/qdev-properties.h
>> +++ b/include/hw/qdev-properties.h
>> @@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
>>                            _arrayfield, _arrayprop, _arraytype) {        \
>>          .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
>>          .info = &(qdev_prop_arraylen),                                  \
>> +        .defval.u = 0,                                                  \
>>          .offset = offsetof(_state, _field)                              \
>>              + type_check(uint32_t, typeof_field(_state, _field)),       \
>>          .arrayinfo = &(_arrayprop),                                     \
>
> Hmm.
>
> .info and .defval apply to the array *length*.
> .info.set_default_value() is set_default_uint(), which uses .defval.u.
> Setting it here is correct.
>
> The array *elements* are described by .arrayinfo.  There is no
> .arraydefval, but that's okay, because .arrayinfo.set_default_value()
> doesn't get called.  Correct?

Correct. (There's no way to specify a default value for array properties.)

thanks
-- PMM
Markus Armbruster July 13, 2017, 4:21 p.m. UTC | #4
Peter Maydell <peter.maydell@linaro.org> writes:

> On 13 July 2017 at 15:11, Markus Armbruster <armbru@redhat.com> wrote:
>> Peter Maydell <peter.maydell@linaro.org> writes:
>>
>>> In DEFINE_PROP_ARRAY, because we use a PropertyInfo (qdev_prop_arraylen)
>>> which has a .set_default_value member we will set the field to a default
>>> value. That default value will be zero, by the C rule that struct
>>> initialization sets unmentioned members to zero if at least one member
>>> is initialized. However it's clearer to state it explicitly.
>>>
>>> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
>>> ---
>>> If my eyeball of the sources is correct, this is the only case we
>>> have of a Property struct that uses a PropertyInfo with a non-NULL
>>> .set_default_value but which doesn't explicitly set .defval.u.
>>> ---
>>>  include/hw/qdev-properties.h | 1 +
>>>  1 file changed, 1 insertion(+)
>>>
>>> diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
>>> index 0604c33..36d040c 100644
>>> --- a/include/hw/qdev-properties.h
>>> +++ b/include/hw/qdev-properties.h
>>> @@ -110,6 +110,7 @@ extern PropertyInfo qdev_prop_arraylen;
>>>                            _arrayfield, _arrayprop, _arraytype) {        \
>>>          .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
>>>          .info = &(qdev_prop_arraylen),                                  \
>>> +        .defval.u = 0,                                                  \
>>>          .offset = offsetof(_state, _field)                              \
>>>              + type_check(uint32_t, typeof_field(_state, _field)),       \
>>>          .arrayinfo = &(_arrayprop),                                     \
>>
>> Hmm.
>>
>> .info and .defval apply to the array *length*.
>> .info.set_default_value() is set_default_uint(), which uses .defval.u.
>> Setting it here is correct.
>>
>> The array *elements* are described by .arrayinfo.  There is no
>> .arraydefval, but that's okay, because .arrayinfo.set_default_value()
>> doesn't get called.  Correct?
>
> Correct. (There's no way to specify a default value for array properties.)

Thanks!

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

Patch

diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
index 0604c33..36d040c 100644
--- a/include/hw/qdev-properties.h
+++ b/include/hw/qdev-properties.h
@@ -110,6 +110,7 @@  extern PropertyInfo qdev_prop_arraylen;
                           _arrayfield, _arrayprop, _arraytype) {        \
         .name = (PROP_ARRAY_LEN_PREFIX _name),                          \
         .info = &(qdev_prop_arraylen),                                  \
+        .defval.u = 0,                                                  \
         .offset = offsetof(_state, _field)                              \
             + type_check(uint32_t, typeof_field(_state, _field)),       \
         .arrayinfo = &(_arrayprop),                                     \