qom: Use qlit to represent property defaults
v3 cover qom: Use qlit to represent property defaults
Based-on: [email protected]
Git branch: https://gitlab.com/ehabkost/qemu/-/commits/work/qdev-qlit-defaults
This extend qlit.h to support all QNum types (signed int, unsigned int, and double), and use QLitObject to represent field property defaults.
It allows us to get rid of most type-specific .set_default_value functions for QOM property types.
The only remaining .set_default_value function in the code is field_prop_set_default_value_enum(). This will take a bit more work because the default is currently stored as int, but parsed as string, so it will be addressed in a separate series.
Changes v2 -> v3:
* qnum/qlit patches:
* Variable naming in qnum code
* Coding style changes in qnum code
* Split "qlit: Support all types of QNums"
in smaller patches
* Replace QLIT_QNUM with QLIT_QNUM_INT
* Extend test cases, move most of the new test case code to check-qlit.c
* Remove qnum_get_value() and qlit_get_type() function
* Removed kernel-doc conversion patch, to reduce noise in series review
* qom patches:
* Split "qom: Use qlit to represent property defaults" in smaller patches, hopefully making review easier
* Small changes in UUID property code (See
"qom: Don't ignore defval on UUID property" and
"qom: Fix documentation of UUID property type")
Changes v1 -> v2:
* Rebase to latest version of field properties series
* Fix unit test failure
* Coding style changes
Eduardo Habkost (19):
qnum: Make qnum_get_double() get const pointer
qnum: Make num_x/num_y variables at qnum_is_equal() const
qnum: QNumValue type for QNum value literals
qnum: qnum_value_is_equal() function
qlit: Use qnum_value_is_equal() when comparing QNums
qlit: Rename QLIT_QNUM to QLIT_QNUM_INT
qlit: Use QNumValue to represent QNums
qlit: Move qlit_equal_qobject() reference values to array
qlit: Add more test literals to qlit_equal_qobject() test case
qlit: Support all types of QNums
qom: field_prop_set_default_value() helper
qom: Replace defval value in Property with QLitObject
qom: Fix documentation of UUID property type
qom: Don't ignore defval on UUID property
qom: Make object_property_set_default() public
qom: Make PropertyInfo.set_default_value optional
qom: Delete field_prop_set_default_value_*int()
qom: Delete set_default_uuid()
qom: Delete set_default_value_bool()
include/hw/qdev-properties-system.h | 4 +- include/qapi/qmp/qlit.h | 11 ++- include/qapi/qmp/qnum.h | 26 +++++- include/qom/field-property-internal.h | 7 +- include/qom/field-property.h | 30 +++---- include/qom/object.h | 11 +++ include/qom/property-types.h | 18 ++-- hw/core/qdev-properties-system.c | 13 +-- qobject/qlit.c | 5 +- qobject/qnum.c | 113 ++++++++++++++------------ qom/field-property.c | 36 ++++++-- qom/object.c | 2 +- qom/property-types.c | 37 ++------- tests/check-qjson.c | 30 +++---- tests/check-qlit.c | 72 +++++++++++++--- tests/check-qnum.c | 14 ++-- 16 files changed, 253 insertions(+), 176 deletions(-)
Signature · 1 line
2.28.0
PATCH v3 1/19 qnum: Make qnum_get_double() get const pointer
qnum_get_double() won't change the object, the argument can be const.
--- include/qapi/qmp/qnum.h | 2 +- qobject/qnum.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/qapi/qmp/qnum.h +1 -1
@@ -64,7 +64,7 @@ int64_t qnum_get_int(const QNum *qn);bool qnum_get_try_uint (const QNum *qn,uint64_t *val);uint64_t qnum_get_uint (const QNum *qn); -double qnum_get_double (QNum *qn); +double qnum_get_double (const QNum *qn);char *qnum_to_string (QNum *qn);
qobject/qnum.c +1 -1
@@ -144,7 +144,7 @@ uint64_t qnum_get_uint(const QNum *qn)* * qnum_get_double() loses precision for integers beyond 53 bits. */ -double qnum_get_double (QNum *qn) +double qnum_get_double (const QNum *qn) {switch (qn->kind) {case QNUM_I64 :
Signature · 1 line
2.28.0
PATCH v3 2/19 qnum: Make num_x/num_y variables at qnum_is_equal() const
In reply to Eduardo Habkostqobject_to() drops const-ness by accident, but our function arguments (x, y) are const. Make num_x/num_y const too.
---
This is a new patch added in v3 of the series.
--- qobject/qnum.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
qobject/qnum.c +2 -2
@@ -209,8 +209,8 @@ char *qnum_to_string(QNum *qn)*/ bool qnum_is_equal (const QObject *x,const QObject *y) { -QNum *num_x =qobject_to (QNum, x); -QNum *num_y =qobject_to (QNum, y); +const QNum *num_x =qobject_to (QNum, x); +const QNum *num_y =qobject_to (QNum, y);switch (num_x->kind) {case QNUM_I64 :
Signature · 1 line
2.28.0
PATCH v3 3/19 qnum: QNumValue type for QNum value literals
In reply to Eduardo HabkostProvide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
---
Changes v2 -> v3:
* Fixed copy-pasta at qnum_from_value() documentation
* Removed qnum_get_value() function
* Moved doc comment of qnum_from_value() to .c file, for consistency with other functions.
* Removed "private:" doc comment at QNumValue.
* Removed unnecessary kernel-doc noise (obvious parameter descriptions).
* Removed space after type cast in qnum_from_*().
* qnum_is_equal() variable const-ness & renames:
* Renamed new QNumValue variables to val_x/val_y.
* Keep existing QNum num_x/num_y variable names.
* const-ness change of num_x/num_y was moved to a separate patch.
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields
--- include/qapi/qmp/qnum.h | 23 ++++++++++- qobject/qnum.c | 91 ++++++++++++++++++++++------------------- tests/check-qnum.c | 14 +++---- 3 files changed, 76 insertions(+), 52 deletions(-)
include/qapi/qmp/qnum.h +21 -2
@@ -44,16 +44,35 @@ typedef enum {* in range: qnum_get_try_int() / qnum_get_try_uint() check range and * convert under the hood. */ -struct QNum { -struct QObjectBase_ base; + +/** +* struct QNumValue: the value of a QNum +* +* QNumValue literals can be constructed using the `QNUM_VAL_INT`, +* `QNUM_VAL_UINT`, and `QNUM_VAL_DOUBLE` macros. +*/ +typedef struct QNumValue {QNumKind kind;union {int64_t i64;uint64_t u64;double dbl; } u; +}QNumValue ; + +#define QNUM_VAL_INT (value) \ + { .kind = QNUM_I64, .u.i64 = value } +#define QNUM_VAL_UINT (value) \ + { .kind = QNUM_U64, .u.u64 = value } +#define QNUM_VAL_DOUBLE (value) \ + { .kind = QNUM_DOUBLE, .u.dbl = value } + +struct QNum { +struct QObjectBase_ base; +QNumValue value; }; +QNum *qnum_from_value (QNumValue value);QNum *qnum_from_int (int64_t value);QNum *qnum_from_uint (uint64_t value);QNum *qnum_from_double (double value);
qobject/qnum.c +48 -43
199 lines not on this page · raw message
tests/check-qnum.c +7 -7
@@ -30,8 +30,8 @@ static void qnum_from_int_test(void) qn =qnum_from_int (value);g_assert (qn !=NULL ); - g_assert_cmpint(qn->kind, ==, QNUM_I64); - g_assert_cmpint(qn->u.i64, ==, value); + g_assert_cmpint(qn->value.kind, ==, QNUM_I64); + g_assert_cmpint(qn->value.u.i64, ==, value); g_assert_cmpint(qn->base.refcnt, ==, 1); g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM);
@@ -45,8 +45,8 @@ static void qnum_from_uint_test(void) qn =qnum_from_uint (value);g_assert (qn !=NULL ); - g_assert_cmpint(qn->kind, ==, QNUM_U64); -g_assert (qn->u.u64 == value); + g_assert_cmpint(qn->value.kind, ==, QNUM_U64); +g_assert (qn->value.u.u64 == value);g_assert (qn->base.refcnt ==1 );g_assert (qobject_type (QOBJECT (qn)) ==QTYPE_QNUM );
@@ -60,8 +60,8 @@ static void qnum_from_double_test(void) qn =qnum_from_double (value);g_assert (qn !=NULL ); - g_assert_cmpint(qn->kind, ==, QNUM_DOUBLE); - g_assert_cmpfloat(qn->u.dbl, ==, value); + g_assert_cmpint(qn->value.kind, ==, QNUM_DOUBLE); + g_assert_cmpfloat(qn->value.u.dbl, ==, value); g_assert_cmpint(qn->base.refcnt, ==, 1); g_assert_cmpint(qobject_type(QOBJECT(qn)), ==, QTYPE_QNUM);
@@ -74,7 +74,7 @@ static void qnum_from_int64_test(void)const int64_t value =0x1234567890abcdefLL ; qn =qnum_from_int (value); - g_assert_cmpint((int64_t) qn->u.i64, ==, value); + g_assert_cmpint((int64_t) qn->value.u.i64, ==, value);qobject_unref (qn); }
Signature · 1 line
2.28.0
PATCH v3 4/19 qnum: qnum_value_is_equal() function
In reply to Eduardo HabkostExtract the QNum value comparison logic to a function that takes QNumValue* as argument.
---
Changes v2 -> v3:
* Rename function parameters to val_x/val_y
* Insert blank line after variable declarations at qnum_is_equal()
--- include/qapi/qmp/qnum.h | 1 + qobject/qnum.c | 24 ++++++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-)
include/qapi/qmp/qnum.h +1 -0
@@ -87,6 +87,7 @@ double qnum_get_double(const QNum *qn);char *qnum_to_string (QNum *qn); +bool qnum_value_is_equal (const QNumValue *val_x,const QNumValue *val_y);bool qnum_is_equal (const QObject *x,const QObject *y);void qnum_destroy_obj (QObject *obj);
qobject/qnum.c +16 -8
@@ -202,7 +202,7 @@ char *qnum_to_string(QNum *qn) }/** -* qnum_is_equal(): Test whether the two QNums are equal +* qnum_value_is_equal(): Test whether two QNumValues are equal * * Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered
@@ -210,13 +210,8 @@ char *qnum_to_string(QNum *qn)* * Doubles are never considered equal to integers. */ -bool qnum_is_equal (const QObject *x,const QObject *y) +bool qnum_value_is_equal (const QNumValue *val_x,const QNumValue *val_y) { -const QNum *num_x =qobject_to (QNum, x); -const QNum *num_y =qobject_to (QNum, y); -const QNumValue *val_x = &num_x->value; -const QNumValue *val_y = &num_y->value; -switch (val_x->kind) {case QNUM_I64 :switch (val_y->kind) {
@@ -234,7 +229,7 @@ bool qnum_is_equal(const QObject *x, const QObject *y)case QNUM_U64 :switch (val_y->kind) {case QNUM_I64 : -return qnum_is_equal (y, x); +return qnum_value_is_equal (val_y, val_x);case QNUM_U64 :/* Comparison in native uint64_t type */ return val_x->u.u64 == val_y->u.u64;
@@ -257,6 +252,19 @@ bool qnum_is_equal(const QObject *x, const QObject *y)abort (); } +/** +* qnum_is_equal(): Test whether the two QNums are equal +* +* See qnum_value_is_equal() for details on the comparison rules. +*/ +bool qnum_is_equal (const QObject *x,const QObject *y) +{ +const QNum *qnum_x =qobject_to (QNum, x); +const QNum *qnum_y =qobject_to (QNum, y); + +return qnum_value_is_equal (&qnum_x->value, &qnum_y->value); +} +/** * qnum_destroy_obj(): Free all memory allocated by a * QNum object
Signature · 1 line
2.28.0
PATCH v3 5/19 qlit: Use qnum_value_is_equal() when comparing QNums
In reply to Eduardo HabkostCurrently, qlit_equal_qobject() crashes if getting a QNum that can't be represented as int64. Fix this by using qnum_value_is_equal().
---
This is a new patch added in v3 of the series.
--- qobject/qlit.c | 3 ++- tests/check-qlit.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)
qobject/qlit.c +2 -1
@@ -71,7 +71,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)case QTYPE_QBOOL :return lhs->value.qbool ==qbool_get_bool (qobject_to (QBool, rhs));case QTYPE_QNUM : -return lhs->value.qnum ==qnum_get_int (qobject_to (QNum, rhs)); +return qnum_value_is_equal (&(QNumValue )QNUM_VAL_INT (lhs->value.qnum), + &qobject_to (QNum, rhs)->value);case QTYPE_QSTRING :return (strcmp (lhs->value.qstr,qstring_get_str (qobject_to (QString, rhs))) ==0 );
tests/check-qlit.c +19 -0
@@ -65,6 +65,24 @@ static void qlit_equal_qobject_test(void)qobject_unref (qobj); } +static void qlit_equal_large_qnum_test (void ) +{ +/* 2^32-1 */ +QNum *large =qnum_from_uint (9223372036854775807LL ); +/* 2^32 */ +QNum *too_large =qnum_from_uint (9223372036854775808ULL ); +QNum *dbl =qnum_from_double (9223372036854775808.0 ); +QLitObject qlit_large =QLIT_QNUM (9223372036854775807LL ); + +g_assert (qlit_equal_qobject (&qlit_large,QOBJECT (large))); +g_assert (!qlit_equal_qobject (&qlit_large,QOBJECT (too_large))); +g_assert (!qlit_equal_qobject (&qlit_large,QOBJECT (dbl))); + +qobject_unref (dbl); +qobject_unref (large); +qobject_unref (too_large); +} +static void qobject_from_qlit_test (void ) {QObject *obj, *qobj =qobject_from_qlit (&qlit);
@@ -95,6 +113,7 @@ int main(int argc, char **argv)g_test_init (&argc, &argv,NULL );g_test_add_func ("/qlit/equal_qobject" , qlit_equal_qobject_test); +g_test_add_func ("/qlit/equal_large_qnum" , qlit_equal_large_qnum_test);g_test_add_func ("/qlit/qobject_from_qlit" , qobject_from_qlit_test);return g_test_run ();
Signature · 1 line
2.28.0
PATCH v3 6/19 qlit: Rename QLIT_QNUM to QLIT_QNUM_INT
In reply to Eduardo HabkostRename the existing QLIT_QNUM macro to indicate it only supports signed int values. We're going to add support to other types of QNums later.
---
This is a new patch added in v3 of this series.
In v2, the existing QLIT_QNUM() macro was being kept, now it is replaced by QLIT_QNUM_INT().
--- include/qapi/qmp/qlit.h | 2 +- tests/check-qjson.c | 30 +++++++++++++++--------------- tests/check-qlit.c | 10 +++++----- 3 files changed, 21 insertions(+), 21 deletions(-)
include/qapi/qmp/qlit.h +1 -1
@@ -39,7 +39,7 @@ struct QLitDictEntry { { .type = QTYPE_QNULL }#define QLIT_QBOOL (val) \ { .type = QTYPE_QBOOL, .value.qbool = (val) } -#define QLIT_QNUM (val) \ +#define QLIT_QNUM_INT (val) \ { .type = QTYPE_QNUM, .value.qnum = (val) }#define QLIT_QSTR (val) \ { .type = QTYPE_QSTRING, .value.qstr = (val) }
tests/check-qjson.c +15 -15
@@ -1062,7 +1062,7 @@ static void simple_dict(void) { .encoded = "{\"foo\": 42, \"bar\": \"hello world\"}", .decoded =QLIT_QDICT (((QLitDictEntry []){ - {"foo" ,QLIT_QNUM (42 ) }, + {"foo" ,QLIT_QNUM_INT (42 ) }, {"bar" ,QLIT_QSTR ("hello world" ) }, { } })),
@@ -1074,7 +1074,7 @@ static void simple_dict(void) }, { .encoded = "{\"foo\": 43}", .decoded =QLIT_QDICT (((QLitDictEntry []){ - {"foo" ,QLIT_QNUM (43 ) }, + {"foo" ,QLIT_QNUM_INT (43 ) }, { } })), },
@@ -1160,15 +1160,15 @@ static void simple_list(void) { .encoded = "[43,42]", .decoded = QLIT_QLIST(((QLitObject[]){ - QLIT_QNUM(43), - QLIT_QNUM(42), + QLIT_QNUM_INT(43), + QLIT_QNUM_INT(42), { } })), }, { .encoded = "[43]", .decoded = QLIT_QLIST(((QLitObject[]){ - QLIT_QNUM(43), + QLIT_QNUM_INT(43), { } })), },
@@ -1217,35 +1217,35 @@ static void simple_whitespace(void) { .encoded = " [ 43 , 42 ]", .decoded = QLIT_QLIST(((QLitObject[]){ - QLIT_QNUM(43), - QLIT_QNUM(42), + QLIT_QNUM_INT(43), + QLIT_QNUM_INT(42), { } })), }, { .encoded = "\t[ 43 , { 'h' : 'b' },\r\n\t[ ], 42 ]\n", .decoded = QLIT_QLIST(((QLitObject[]){ - QLIT_QNUM(43), + QLIT_QNUM_INT(43), QLIT_QDICT(((QLitDictEntry[]){ { "h", QLIT_QSTR("b") }, { }})), QLIT_QLIST(((QLitObject[]){ { }})), - QLIT_QNUM(42), + QLIT_QNUM_INT(42), { } })), }, { .encoded = " [ 43 , { 'h' : 'b' , 'a' : 32 }, [ ], 42 ]", .decoded = QLIT_QLIST(((QLitObject[]){ - QLIT_QNUM(43), + QLIT_QNUM_INT(43), QLIT_QDICT(((QLitDictEntry[]){ { "h", QLIT_QSTR("b") }, - { "a", QLIT_QNUM(32) }, + { "a", QLIT_QNUM_INT(32) }, { }})), QLIT_QLIST(((QLitObject[]){ { }})), - QLIT_QNUM(42), + QLIT_QNUM_INT(42), { } })), },
@@ -1275,11 +1275,11 @@ static void simple_interpolation(void)QObject *embedded_obj;QObject *obj;QLitObject decoded =QLIT_QLIST (((QLitObject []){ -QLIT_QNUM (1 ), +QLIT_QNUM_INT (1 ),QLIT_QSTR ("100%" ),QLIT_QLIST (((QLitObject []){ -QLIT_QNUM (32 ), -QLIT_QNUM (42 ), +QLIT_QNUM_INT (32 ), +QLIT_QNUM_INT (42 ), {}})), {}}));
tests/check-qlit.c +5 -5
@@ -17,12 +17,12 @@#include "qapi/qmp/qstring.h" static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) { - { "foo", QLIT_QNUM(42) }, + { "foo", QLIT_QNUM_INT(42) }, { "bar", QLIT_QSTR("hello world") }, { "baz", QLIT_QNULL }, { "bee", QLIT_QLIST(((QLitObject[]) { - QLIT_QNUM(43), - QLIT_QNUM(44), + QLIT_QNUM_INT(43), + QLIT_QNUM_INT(44), QLIT_QBOOL(true), { }, }))},
@@ -30,7 +30,7 @@ static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) { }));static QLitObject qlit_foo =QLIT_QDICT (((QLitDictEntry []) { - {"foo" ,QLIT_QNUM (42 ) }, + {"foo" ,QLIT_QNUM_INT (42 ) }, { }, }));
@@ -72,7 +72,7 @@ static void qlit_equal_large_qnum_test(void)/* 2^32 */ QNum *too_large =qnum_from_uint (9223372036854775808ULL );QNum *dbl =qnum_from_double (9223372036854775808.0 ); -QLitObject qlit_large =QLIT_QNUM (9223372036854775807LL ); +QLitObject qlit_large =QLIT_QNUM_INT (9223372036854775807LL );g_assert (qlit_equal_qobject (&qlit_large,QOBJECT (large)));g_assert (!qlit_equal_qobject (&qlit_large,QOBJECT (too_large)));
Signature · 1 line
2.28.0
PATCH v3 7/19 qlit: Use QNumValue to represent QNums
In reply to Eduardo HabkostReplace the existing int64_t field in QLitObject with QNumValue, so we can get support for other QNum types for free.
---
This is a new patch added in v3 of the series. It includes portions of a previous patch from v2:
"qlit: Support all types of QNums".
--- include/qapi/qmp/qlit.h | 5 +++-- qobject/qlit.c | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-)
include/qapi/qmp/qlit.h +3 -2
@@ -15,6 +15,7 @@#define QLIT_H #include "qobject.h" +#include "qnum.h" typedef struct QLitDictEntry QLitDictEntry ;typedef struct QLitObject QLitObject ;
@@ -23,7 +24,7 @@ struct QLitObject {QType type;union {bool qbool; -int64_t qnum; +QNumValue qnum;const char *qstr;QLitDictEntry *qdict;QLitObject *qlist;
@@ -40,7 +41,7 @@ struct QLitDictEntry {#define QLIT_QBOOL (val) \ { .type = QTYPE_QBOOL, .value.qbool = (val) }#define QLIT_QNUM_INT (val) \ - { .type = QTYPE_QNUM, .value.qnum = (val) } + { .type = QTYPE_QNUM, .value.qnum = QNUM_VAL_INT(val) }#define QLIT_QSTR (val) \ { .type = QTYPE_QSTRING, .value.qstr = (val) }#define QLIT_QDICT (val) \
qobject/qlit.c +2 -2
@@ -71,7 +71,7 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)case QTYPE_QBOOL :return lhs->value.qbool ==qbool_get_bool (qobject_to (QBool, rhs));case QTYPE_QNUM : -return qnum_value_is_equal (&(QNumValue )QNUM_VAL_INT (lhs->value.qnum), +return qnum_value_is_equal (&lhs->value.qnum, &qobject_to (QNum, rhs)->value);case QTYPE_QSTRING : return (strcmp(lhs->value.qstr,
@@ -95,7 +95,7 @@ QObject *qobject_from_qlit(const QLitObject *qlit)case QTYPE_QNULL :return QOBJECT (qnull ());case QTYPE_QNUM : -return QOBJECT (qnum_from_int (qlit->value.qnum)); +return QOBJECT (qnum_from_value (qlit->value.qnum));case QTYPE_QSTRING :return QOBJECT (qstring_from_str (qlit->value.qstr));case QTYPE_QDICT : {
Signature · 1 line
2.28.0
PATCH v3 8/19 qlit: Move qlit_equal_qobject() reference values to array
In reply to Eduardo HabkostAdd an array of values to qlit_equal_qobject_test(), so we can extend the test case to compare multiple literals, not just the ones at the existing qlit and qlit_foo variables.
---
This is a new patch added in v3 of this series.
--- tests/check-qlit.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
tests/check-qlit.c +19 -7
@@ -29,11 +29,6 @@ static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) { { }, })); -static QLitObject qlit_foo =QLIT_QDICT (((QLitDictEntry []) { - {"foo" ,QLIT_QNUM_INT (42 ) }, - { }, -})); -static QObject *make_qobject (void ) {QDict *qdict =qdict_new ();
@@ -53,16 +48,33 @@ static QObject *make_qobject(void)static void qlit_equal_qobject_test (void ) { +/* Each entry in the values[] array should be different from the others */ +QLitObject values[] = { + qlit, +QLIT_QDICT (((QLitDictEntry []) { + {"foo" ,QLIT_QNUM_INT (42 ) }, + { }, + })), + }; +int i;QObject *qobj =make_qobject ();g_assert (qlit_equal_qobject (&qlit, qobj)); -g_assert (!qlit_equal_qobject (&qlit_foo, qobj)); -qdict_put (qobject_to (QDict, qobj),"bee" ,qlist_new ());g_assert (!qlit_equal_qobject (&qlit, qobj));qobject_unref (qobj); + +for (i =0 ; i <ARRAY_SIZE (values); i++) { +int j; +QObject *o =qobject_from_qlit (&values[i]); +for (j =0 ; j <ARRAY_SIZE (values); j++) { +g_assert (qlit_equal_qobject (&values[j], o) == (i == j)); + } +qobject_unref (o); + } + }static void qlit_equal_large_qnum_test (void )
Signature · 1 line
2.28.0
Re: [PATCH v3 08/19] qlit: Move qlit_equal_qobject() reference values to array
Quoted 43 lines · Eduardo Habkost
Add an array of values to
qlit_equal_qobject_test(), so we can extend the test case to compare multiple literals, not just the ones at the existingqlitandqlit_foovariables.Signed-off-by Eduardo Habkost <[email protected]>---
This is a new patch added in v3 of this series.--- tests/check-qlit.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)tests/check-qlit.c +9
-5@@ -29,11 +29,6 @@ static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) { { }, })); -static QLitObject qlit_foo =QLIT_QDICT (((QLitDictEntry []) { - {"foo" ,QLIT_QNUM_INT (42 ) }, - { }, -})); -static QObject *make_qobject (void ) {QDict *qdict =qdict_new ();@@ -53,16 +48,33 @@ static QObject *make_qobject(void)static void qlit_equal_qobject_test (void ) { +/* Each entry in the values[] array should be different from the others */ +QLitObject values[] = { + qlit, +QLIT_QDICT (((QLitDictEntry []) { + {"foo" ,QLIT_QNUM_INT (42 ) }, + { }, + })), + }; +int i;QObject *qobj =make_qobject ();g_assert (qlit_equal_qobject (&qlit, qobj));
tests/check-qlit.c static QObject *make_qobject(void) · line 70
Quoted 10 lines
tests/check-qlit.c +3
-2-g_assert (!qlit_equal_qobject (&qlit_foo, qobj)); -qdict_put (qobject_to (QDict, qobj),"bee" ,qlist_new ());g_assert (!qlit_equal_qobject (&qlit, qobj));qobject_unref (qobj); + +for (i =0 ; i <ARRAY_SIZE (values); i++) { +int j;
I'd prefer to declare this one together with @i.
tests/check-qlit.c static QObject *make_qobject(void) · line 71
Quoted 1 line
tests/check-qlit.c +1
-0+QObject *o =qobject_from_qlit (&values[i]);
Blank line, if you don't mind.
Quoted 9 lines
tests/check-qlit.c +6
-0+for (j =0 ; j <ARRAY_SIZE (values); j++) { +g_assert (qlit_equal_qobject (&values[j], o) == (i == j)); + } +qobject_unref (o); + } + }static void qlit_equal_large_qnum_test (void )
Re: [PATCH v3 08/19] qlit: Move qlit_equal_qobject() reference values to array
Quoted 58 lines · Markus Armbruster
Quoted 43 lines · Eduardo Habkost
Add an array of values to
qlit_equal_qobject_test(), so we can extend the test case to compare multiple literals, not just the ones at the existingqlitandqlit_foovariables.Signed-off-by Eduardo Habkost <[email protected]>---
This is a new patch added in v3 of this series.--- tests/check-qlit.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)tests/check-qlit.c +9
-5@@ -29,11 +29,6 @@ static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) { { }, })); -static QLitObject qlit_foo =QLIT_QDICT (((QLitDictEntry []) { - {"foo" ,QLIT_QNUM_INT (42 ) }, - { }, -})); -static QObject *make_qobject (void ) {QDict *qdict =qdict_new ();@@ -53,16 +48,33 @@ static QObject *make_qobject(void)static void qlit_equal_qobject_test (void ) { +/* Each entry in the values[] array should be different from the others */ +QLitObject values[] = { + qlit, +QLIT_QDICT (((QLitDictEntry []) { + {"foo" ,QLIT_QNUM_INT (42 ) }, + { }, + })), + }; +int i;QObject *qobj =make_qobject ();g_assert (qlit_equal_qobject (&qlit, qobj));tests/check-qlit.c static QObject *make_qobject(void) · line 70
Quoted 10 lines
tests/check-qlit.c +3
-2-g_assert (!qlit_equal_qobject (&qlit_foo, qobj)); -qdict_put (qobject_to (QDict, qobj),"bee" ,qlist_new ());g_assert (!qlit_equal_qobject (&qlit, qobj));qobject_unref (qobj); + +for (i =0 ; i <ARRAY_SIZE (values); i++) { +int j;I'd prefer to declare this one together with @i.
Quoted 3 lines
tests/check-qlit.c static QObject *make_qobject(void) · line 71
Quoted 1 line
tests/check-qlit.c +1
-0+QObject *o =qobject_from_qlit (&values[i]);Blank line, if you don't mind.
I will surely do it if there's a v4, but I hope you don't make me submit v4 just to change these.
Quoted 10 lines
Quoted 10 lines
tests/check-qlit.c +6
-0+for (j =0 ; j <ARRAY_SIZE (values); j++) { +g_assert (qlit_equal_qobject (&values[j], o) == (i == j)); + } +qobject_unref (o); + } + }static void qlit_equal_large_qnum_test (void )
Signature · 1 line
Eduardo
Re: [PATCH v3 08/19] qlit: Move qlit_equal_qobject() reference values to array
Quoted 59 lines · Eduardo Habkost
Quoted 58 lines · Markus Armbruster
Quoted 43 lines · Eduardo Habkost
Add an array of values to
qlit_equal_qobject_test(), so we can extend the test case to compare multiple literals, not just the ones at the existingqlitandqlit_foovariables.Signed-off-by Eduardo Habkost <[email protected]>---
This is a new patch added in v3 of this series.--- tests/check-qlit.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)tests/check-qlit.c +9
-5@@ -29,11 +29,6 @@ static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) { { }, })); -static QLitObject qlit_foo =QLIT_QDICT (((QLitDictEntry []) { - {"foo" ,QLIT_QNUM_INT (42 ) }, - { }, -})); -static QObject *make_qobject (void ) {QDict *qdict =qdict_new ();@@ -53,16 +48,33 @@ static QObject *make_qobject(void)static void qlit_equal_qobject_test (void ) { +/* Each entry in the values[] array should be different from the others */ +QLitObject values[] = { + qlit, +QLIT_QDICT (((QLitDictEntry []) { + {"foo" ,QLIT_QNUM_INT (42 ) }, + { }, + })), + }; +int i;QObject *qobj =make_qobject ();g_assert (qlit_equal_qobject (&qlit, qobj));tests/check-qlit.c static QObject *make_qobject(void) · line 70
Quoted 10 lines
tests/check-qlit.c +3
-2-g_assert (!qlit_equal_qobject (&qlit_foo, qobj)); -qdict_put (qobject_to (QDict, qobj),"bee" ,qlist_new ());g_assert (!qlit_equal_qobject (&qlit, qobj));qobject_unref (qobj); + +for (i =0 ; i <ARRAY_SIZE (values); i++) { +int j;I'd prefer to declare this one together with @i.
Quoted 6 lines
Quoted 3 lines
tests/check-qlit.c static QObject *make_qobject(void) · line 71
Quoted 1 line
tests/check-qlit.c +1
-0+QObject *o =qobject_from_qlit (&values[i]);Blank line, if you don't mind.
I will surely do it if there's a v4, but I hope you don't make me submit v4 just to change these.
That would be a waste of your time and mine :)
If we decide to take the patches through my tree, I can tweak this in my tree.
If somebody else takes it, and prefers not to tweak, I can tweak on top if I care.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
tests/check-qlit.c +6
-0+for (j =0 ; j <ARRAY_SIZE (values); j++) { +g_assert (qlit_equal_qobject (&values[j], o) == (i == j)); + } +qobject_unref (o); + } + }static void qlit_equal_large_qnum_test (void )
PATCH v3 9/19 qlit: Add more test literals to qlit_equal_qobject() test case
In reply to Eduardo HabkostAdd a few examples of each qlit type, to make sure each one compare as equal to itself, but not equal to the other values.
---
This is a new patch added in v3 of this series.
--- tests/check-qlit.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
tests/check-qlit.c +16 -0
@@ -50,11 +50,27 @@ static void qlit_equal_qobject_test(void) {/* Each entry in the values[] array should be different from the others */ QLitObject values[] = { +QLIT_QNULL , +QLIT_QBOOL (false), +QLIT_QBOOL (true), +QLIT_QNUM_INT (-1 ), +QLIT_QNUM_INT (0 ), +QLIT_QNUM_INT (1 ), +QLIT_QNUM_INT (INT64_MIN ), +QLIT_QNUM_INT (INT64_MAX ), +QLIT_QSTR ("" ), +QLIT_QSTR ("foo" ), qlit,QLIT_QDICT (((QLitDictEntry []) { {"foo" ,QLIT_QNUM_INT (42 ) }, { }, })), +QLIT_QLIST (((QLitObject []){ +QLIT_QNUM_INT (-1 ), +QLIT_QNUM_INT (0 ), +QLIT_QNUM_INT (1 ), + { }, + })), };int i;QObject *qobj =make_qobject ();
Signature · 1 line
2.28.0
PATCH v3 10/19 qlit: Support all types of QNums
In reply to Eduardo HabkostAdd two new macros to support other types of QNums:QLIT_QNUM_UINT, and QLIT_QNUM_DOUBLE, and include them in the qlit_equal_qobject_test() test case.
---
Changes v2 -> v3:
* QLIT_QNUM macro doesn't exist anymore
* Addition of the QNumValue field to QLitObject is
now in a separate patch ("qlit: Use QNumValue to represent QNums")
* check-qjson test case changes dropped.
Instead, I'm only extending the qlit_equal_qobject_test() test case.
Changes v1 -> v2:
* Coding style fix at qlit_equal_qobject()
--- include/qapi/qmp/qlit.h | 4 ++++ tests/check-qlit.c | 5 +++++ 2 files changed, 9 insertions(+)
include/qapi/qmp/qlit.h +4 -0
@@ -42,6 +42,10 @@ struct QLitDictEntry { { .type = QTYPE_QBOOL, .value.qbool = (val) }#define QLIT_QNUM_INT (val) \ { .type = QTYPE_QNUM, .value.qnum = QNUM_VAL_INT(val) } +#define QLIT_QNUM_UINT (val) \ + { .type = QTYPE_QNUM, .value.qnum = QNUM_VAL_UINT(val) } +#define QLIT_QNUM_DOUBLE (val) \ + { .type = QTYPE_QNUM, .value.qnum = QNUM_VAL_DOUBLE(val) }#define QLIT_QSTR (val) \ { .type = QTYPE_QSTRING, .value.qstr = (val) }#define QLIT_QDICT (val) \
tests/check-qlit.c +5 -0
@@ -58,6 +58,11 @@ static void qlit_equal_qobject_test(void)QLIT_QNUM_INT (1 ),QLIT_QNUM_INT (INT64_MIN ),QLIT_QNUM_INT (INT64_MAX ), +QLIT_QNUM_UINT (UINT64_MAX ), +/* Larger than UINT64_MAX: */ +QLIT_QNUM_DOUBLE (18446744073709552e3 ), +/* Smaller than INT64_MIN: */ +QLIT_QNUM_DOUBLE (-92233720368547758e2 ),QLIT_QSTR ("" ),QLIT_QSTR ("foo" ), qlit,
Signature · 1 line
2.28.0
Re: [PATCH v3 10/19] qlit: Support all types of QNums
Quoted 41 lines · Eduardo Habkost
Add two new macros to support other types of QNums:
QLIT_QNUM_UINT, andQLIT_QNUM_DOUBLE, and include them in theqlit_equal_qobject_test()test case.Signed-off-by Eduardo Habkost <[email protected]>---
Changes v2 -> v3:
*QLIT_QNUMmacro doesn't exist anymore
* Addition of the QNumValue field to QLitObject is
now in a separate patch ("qlit: Use QNumValue to represent QNums")
* check-qjson test case changes dropped.
Instead, I'm only extending theqlit_equal_qobject_test()test case.Changes v1 -> v2:
* Coding style fix atqlit_equal_qobject()--- include/qapi/qmp/qlit.h | 4 ++++ tests/check-qlit.c | 5 +++++ 2 files changed, 9 insertions(+)include/qapi/qmp/qlit.h +4
-0@@ -42,6 +42,10 @@ struct QLitDictEntry { { .type = QTYPE_QBOOL, .value.qbool = (val) }#define QLIT_QNUM_INT (val) \ { .type = QTYPE_QNUM, .value.qnum = QNUM_VAL_INT(val) } +#define QLIT_QNUM_UINT (val) \ + { .type = QTYPE_QNUM, .value.qnum = QNUM_VAL_UINT(val) } +#define QLIT_QNUM_DOUBLE (val) \ + { .type = QTYPE_QNUM, .value.qnum = QNUM_VAL_DOUBLE(val) }#define QLIT_QSTR (val) \ { .type = QTYPE_QSTRING, .value.qstr = (val) }#define QLIT_QDICT (val) \tests/check-qlit.c +0
-0
tests/check-qlit.c static void qlit_equal_qobject_test(void) · line 65
Quoted 8 lines
tests/check-qlit.c +5
-0@@ -58,6 +58,11 @@ static void qlit_equal_qobject_test(void)QLIT_QNUM_INT (1 ),QLIT_QNUM_INT (INT64_MIN ),QLIT_QNUM_INT (INT64_MAX ), +QLIT_QNUM_UINT (UINT64_MAX ), +/* Larger than UINT64_MAX: */ +QLIT_QNUM_DOUBLE (18446744073709552e3 ), +/* Smaller than INT64_MIN: */ + QLIT_QNUM_DOUBLE(-92233720368547758e2),
Why "larger than UINT64_MAX" and "smaller than INT64_MIN"?
Quoted 3 lines
tests/check-qlit.c +0
-0QLIT_QSTR ("" ),QLIT_QSTR ("foo" ), qlit,
Re: [PATCH v3 10/19] qlit: Support all types of QNums
Quoted 6 lines · Markus Armbruster
tests/check-qlit.c static void qlit_equal_qobject_test(void) · line 65
Quoted 4 lines
tests/check-qlit.c +4
-0+/* Larger than UINT64_MAX: */ +QLIT_QNUM_DOUBLE (18446744073709552e3 ), +/* Smaller than INT64_MIN: */ + QLIT_QNUM_DOUBLE(-92233720368547758e2),Why "larger than
UINT64_MAX" and "smaller thanINT64_MIN"?
I guess the point is to test values that are only representable as a double, so (double)((uint64_t)INT64_MAX+1) wouldn't be very useful for that: as the expression shows, it would not be a QNUM_VAL_INT but it would be representable as QNUM_VAL_UINT.
So these are the cases that matter the most, even though -1, 0 and INT64_MAX+1 could be nice to have.
Paolo
Re: [PATCH v3 10/19] qlit: Support all types of QNums
Quoted 8 lines · Paolo Bonzini
Quoted 6 lines · Markus Armbruster
tests/check-qlit.c static void qlit_equal_qobject_test(void) · line 65
Quoted 4 lines
tests/check-qlit.c +4
-0+/* Larger than UINT64_MAX: */ +QLIT_QNUM_DOUBLE (18446744073709552e3 ), +/* Smaller than INT64_MIN: */ + QLIT_QNUM_DOUBLE(-92233720368547758e2),Why "larger than
UINT64_MAX" and "smaller thanINT64_MIN"?
Quoted 7 lines
I guess the point is to test values that are only representable as a double, so (double)((uint64_t)INT64_MAX+1) wouldn't be very useful for
that: as the expression shows, it would not be aQNUM_VAL_INTbut it would be representable asQNUM_VAL_UINT.So these are the cases that matter the most, even though -1, 0 and INT64_MAX+1 could be nice to have.
qnum_is_equal()'s contract:
* Doubles are never considered equal to integers.
Re: [PATCH v3 10/19] qlit: Support all types of QNums
Quoted 15 lines · Markus Armbruster
Quoted 13 lines · Paolo Bonzini
Quoted 6 lines · Markus Armbruster
tests/check-qlit.c static void qlit_equal_qobject_test(void) · line 65
Quoted 4 lines
tests/check-qlit.c +4
-0+/* Larger than UINT64_MAX: */ +QLIT_QNUM_DOUBLE (18446744073709552e3 ), +/* Smaller than INT64_MIN: */ + QLIT_QNUM_DOUBLE(-92233720368547758e2),Why "larger than
UINT64_MAX" and "smaller thanINT64_MIN"?I guess the point is to test values that are only representable as a double, so (double)((uint64_t)INT64_MAX+1) wouldn't be very useful for
that: as the expression shows, it would not be aQNUM_VAL_INTbut it would be representable asQNUM_VAL_UINT.
Quoted 6 lines
Quoted 2 lines
So these are the cases that matter the most, even though -1, 0 and INT64_MAX+1 could be nice to have.
qnum_is_equal()'s contract:
* Doubles are never considered equal to integers.
If that's part of the contract, it would be OK to include 0.0, -1.0, 1.0, INT64_MAX+1 in the list. I incorrectly assumed
qnum_is_equal(qnum_from_int(0), qnum_from_double(0.0))
was undefined.
However, if we really care about test coverage of qnum_is_equal(), we probably should be extending check-qnum.c, not check-qlit.c.
Signature · 1 line
Eduardo
PATCH v3 11/19 qom: field_prop_set_default_value() helper
In reply to Eduardo HabkostMove code that sets the property default value to a separate function, to reduce duplication and make refactoring easier.
---
This is a new patch added in v3 of this series. Hopefully, this will make the series easier to review.
The field_prop_set_default_value() was added in v2 at "qom: Use qlit to represent property defaults".
--- qom/field-property.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
qom/field-property.c +16 -8
@@ -62,6 +62,17 @@ static void static_prop_release_dynamic_prop(Object *obj, const char *name,g_free (prop); } +static void field_prop_set_default_value (ObjectProperty *op, +Property *prop) +{ +if (!prop->set_default) { +return ; + } + +assert (prop->info->set_default_value); + prop->info->set_default_value (op, prop); +} +ObjectProperty * object_property_add_field(Object *obj, const char *name, Property *prop,
@@ -83,11 +94,9 @@ object_property_add_field(Object *obj, const char *name,object_property_set_description (obj, name, newprop->info->description); -if (newprop->set_default) { - newprop->info->set_default_value (op, newprop); -if (op->init) { - op->init (obj, op); - } +field_prop_set_default_value (op, newprop); +if (op->init) { + op->init (obj, op); } op->allow_set = allow_set;
@@ -113,9 +122,8 @@ object_class_property_add_field_static(ObjectClass *oc, const char *name, prop->info->release, prop); } -if (prop->set_default) { - prop->info->set_default_value (op, prop); - } + +field_prop_set_default_value (op, prop);if (prop->info->description) {object_class_property_set_description (oc, name, prop->info->description);
Signature · 1 line
2.28.0
Re: [PATCH v3 11/19] qom: field_prop_set_default_value() helper
Quoted 39 lines · Eduardo Habkost
Move code that sets the property default value to a separate function, to reduce duplication and make refactoring easier.
Signed-off-by Eduardo Habkost <[email protected]>---
This is a new patch added in v3 of this series. Hopefully, this will make the series easier to review.The
field_prop_set_default_value()was added in v2 at "qom: Use qlit to represent property defaults".--- qom/field-property.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)qom/field-property.c +11
-0@@ -62,6 +62,17 @@ static void static_prop_release_dynamic_prop(Object *obj, const char *name,g_free (prop); } +static void field_prop_set_default_value (ObjectProperty *op, +Property *prop) +{ +if (!prop->set_default) { +return ; + } + +assert (prop->info->set_default_value); + prop->info->set_default_value (op, prop); +} +ObjectProperty * object_property_add_field(Object *obj, const char *name, Property *prop,@@ -83,11 +94,9 @@ object_property_add_field(Object *obj, const char *name,object_property_set_description (obj, name, newprop->info->description);
qom/field-property.c object_property_add_field(Object *obj, const char *name, · line 100
Quoted 10 lines
qom/field-property.c +3
-5-if (newprop->set_default) { - newprop->info->set_default_value (op, newprop); -if (op->init) { - op->init (obj, op); - } +field_prop_set_default_value (op, newprop); +if (op->init) { + op->init (obj, op); }
op->init() is now called even when !newprop->set_default. Why is that okay?
Quoted 14 lines
qom/field-property.c +2
-3op->allow_set = allow_set;@@ -113,9 +122,8 @@ object_class_property_add_field_static(ObjectClass *oc, const char *name, prop->info->release, prop); } -if (prop->set_default) { - prop->info->set_default_value (op, prop); - } + +field_prop_set_default_value (op, prop);if (prop->info->description) {object_class_property_set_description (oc, name, prop->info->description);
Re: [PATCH v3 11/19] qom: field_prop_set_default_value() helper
Quoted 42 lines · Markus Armbruster
Quoted 40 lines · Eduardo Habkost
Move code that sets the property default value to a separate function, to reduce duplication and make refactoring easier.
Signed-off-by Eduardo Habkost <[email protected]>---
This is a new patch added in v3 of this series. Hopefully, this will make the series easier to review.The
field_prop_set_default_value()was added in v2 at "qom: Use qlit to represent property defaults".--- qom/field-property.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)qom/field-property.c +11
-0@@ -62,6 +62,17 @@ static void static_prop_release_dynamic_prop(Object *obj, const char *name,g_free (prop); } +static void field_prop_set_default_value (ObjectProperty *op, +Property *prop) +{ +if (!prop->set_default) { +return ; + } + +assert (prop->info->set_default_value); + prop->info->set_default_value (op, prop); +} +ObjectProperty * object_property_add_field(Object *obj, const char *name, Property *prop,@@ -83,11 +94,9 @@ object_property_add_field(Object *obj, const char *name,object_property_set_description (obj, name, newprop->info->description);
Quoted 12 lines
qom/field-property.c object_property_add_field(Object *obj, const char *name, · line 100
Quoted 9 lines
qom/field-property.c +3
-5-if (newprop->set_default) { - newprop->info->set_default_value (op, newprop); -if (op->init) { - op->init (obj, op); - } +field_prop_set_default_value (op, newprop); +if (op->init) { + op->init (obj, op); }
op->init()is now called even when !newprop->set_default. Why is that okay?
op->init() will be NULL if object_property_set_default() was not called.
It's subtle, and worth mentioning in the commit message. I think we should encapsulate op->init() behind a object_property_reset_to_default_value() function.
Quoted 15 lines
Quoted 15 lines
qom/field-property.c +2
-3op->allow_set = allow_set;@@ -113,9 +122,8 @@ object_class_property_add_field_static(ObjectClass *oc, const char *name, prop->info->release, prop); } -if (prop->set_default) { - prop->info->set_default_value (op, prop); - } + +field_prop_set_default_value (op, prop);if (prop->info->description) {object_class_property_set_description (oc, name, prop->info->description);
Signature · 1 line
Eduardo
PATCH v3 12/19 qom: Replace defval value in Property with QLitObject
In reply to Eduardo HabkostQLitObject is capable of representing a wider range of values, and it will allow us to simplify a lot of the existing code setting property defaults, later. Replace the bool and union fields with QLitObject.
In short, this is just a direct translation from:
prop->set_default
to
prop->defval.type != QTYPE_NONE
from
prop->defval.i
to
qnum_get_int(qobject_to(QNum, prop->defval)
and from
prop->defval.u
to
qnum_get_uint(qobject_to(QNum, prop->defval)
Note that this doesn't replace any of the default property setters (yet), but just make them safer. Now the actual type of .defval is validated before it is used.
Also note that set_default_uuid_auto() is a bit weird: it ignores .defval completely. This patch keeps the existing behavior, and set_default_uuid_auto() weirdness will be addressed later.
---
This is a new patch added in v3 of this series.
I am splitting the changes and making them in smaller steps to make them easier to understand and review. With this, I intend to demonstrate that the conversion from bool+union to QLitObject is an improvement even if the removal of custom .set_defaul_value functions isn't 100% finished yet.
--- include/hw/qdev-properties-system.h | 7 ++++++- include/qom/field-property-internal.h | 9 ++++++--- include/qom/field-property.h | 27 ++++++++++----------------- include/qom/property-types.h | 18 ++++++------------ hw/core/qdev-properties-system.c | 3 ++- qom/field-property.c | 8 ++++++-- qom/property-types.c | 26 ++++++++++++++++++-------- 7 files changed, 54 insertions(+), 44 deletions(-)
include/hw/qdev-properties-system.h +6 -1
@@ -65,7 +65,12 @@ extern const PropertyInfo qdev_prop_pcie_link_width; #define DEFINE_PROP_UUID(_name, _state, _field) \ DEFINE_PROP(_name, _state, _field, qdev_prop_uuid, QemuUUID, \ - .set_default = true) + /* \ + * Note that set_default_uuid_auto() currently \ + * ignores the actual value value of .defval, \ + * we just need it to not be not QTYPE_NONE \ + */ \ + .defval = QLIT_QNULL)#define DEFINE_PROP_AUDIODEV (_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_audiodev, QEMUSoundCard)
include/qom/field-property-internal.h +6 -3
@@ -14,11 +14,14 @@ void field_prop_set_enum(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp);void field_prop_set_default_value_enum (ObjectProperty *op, -const Property *prop); +const Property *prop, +const QObject *defval);void field_prop_set_default_value_int (ObjectProperty *op, -const Property *prop); +const Property *prop, +const QObject *defval);void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop); +const Property *prop, +const QObject *defval);void field_prop_get_int32 (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp);
include/qom/field-property.h +10 -17
@@ -6,6 +6,7 @@#include "qom/object.h" #include "qapi/util.h" +#include "qapi/qmp/qlit.h" /** * struct Property: definition of a field property
@@ -27,21 +28,8 @@ struct Property {const PropertyInfo *info;ptrdiff_t offset;uint8_t bitnr; -/** -* @set_default: true if the default value should be set from @defval, -* in which case @info->set_default_value must not be NULL -* (if false then no default value is set by the property system -* and the field retains whatever value it was given by instance_init). -*/ -bool set_default; -/** -* @defval: default value for the property. This is used only if @set_default -* is true. -*/ -union { -int64_t i; -uint64_t u; - } defval; +/** @defval: If not QTYPE_NONE, the default value for the property */ +QLitObject defval;/* private: */ int arrayoffset;const PropertyInfo *arrayinfo;
@@ -61,8 +49,13 @@ struct PropertyInfo {const QEnumLookup *enum_table;/** @print: String formatting function, for the human monitor */ int (*print)(Object *obj,Property *prop,char *dest,size_t len); -/** @set_default_value: Callback for initializing the default value */ -void (*set_default_value)(ObjectProperty *op,const Property *prop); +/** +* @set_default_value: Callback for initializing the default value +* +* @defval is a weak reference. +*/ +void (*set_default_value)(ObjectProperty *op,const Property *prop, +const QObject *defval);/** @create: Optional callback for creation of property */ ObjectProperty *(*create)(ObjectClass *oc,const char *name,Property *prop);
include/qom/property-types.h +6 -12
@@ -25,34 +25,29 @@ extern const PropertyInfo prop_info_link;#define PROP_SIGNED (_state, _field, _defval, _prop, _type, ...) \ FIELD_PROP(_state, _field, _prop, _type, \ - .set_default = true, \ - .defval.i = (_type)_defval, \ + .defval = QLIT_QNUM_INT((_type)_defval), \ __VA_ARGS__)#define PROP_UNSIGNED (_state, _field, _defval, _prop, _type, ...) \ FIELD_PROP(_state, _field, _prop, _type, \ - .set_default = true, \ - .defval.u = (_type)_defval, \ + .defval = QLIT_QNUM_UINT((_type)_defval), \ __VA_ARGS__)#define PROP_BIT (_state, _field, _bit, _defval, ...) \ FIELD_PROP(_state, _field, prop_info_bit, uint32_t, \ .bitnr = (_bit), \ - .set_default = true, \ - .defval.u = (bool)_defval, \ + .defval = QLIT_QBOOL(_defval), \ __VA_ARGS__)#define PROP_BIT64 (_state, _field, _bit, _defval, ...) \ FIELD_PROP(_state, _field, prop_info_bit64, uint64_t, \ .bitnr = (_bit), \ - .set_default = true, \ - .defval.u = (bool)_defval, \ + .defval = QLIT_QBOOL(_defval), \ __VA_ARGS__)#define PROP_BOOL (_state, _field, _defval, ...) \ FIELD_PROP(_state, _field, prop_info_bool, bool, \ - .set_default = true, \ - .defval.u = (bool)_defval, \ + .defval = QLIT_QBOOL(_defval), \ __VA_ARGS__)#define PROP_LINK (_state, _field, _type, _ptr_type, ...) \
@@ -131,8 +126,7 @@ extern const PropertyInfo prop_info_link; _arrayfield, _arrayprop, _arraytype) \ DEFINE_PROP((PROP_ARRAY_LEN_PREFIX _name), \ _state, _field, prop_info_arraylen, uint32_t, \ - .set_default = true, \ - .defval.u = 0, \ + .defval = QLIT_QNUM_UINT(0), \ .arrayinfo = &(_arrayprop), \ .arrayfieldsize = sizeof(_arraytype), \ .arrayoffset = offsetof(_state, _arrayfield))
hw/core/qdev-properties-system.c +2 -1
@@ -1080,7 +1080,8 @@ static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,g_free (str); } -static void set_default_uuid_auto (ObjectProperty *op,const Property *prop) +static void set_default_uuid_auto (ObjectProperty *op,const Property *prop, +const QObject *defval) {object_property_set_default_str (op,UUID_VALUE_AUTO ); }
qom/field-property.c +6 -2
@@ -65,12 +65,16 @@ static void static_prop_release_dynamic_prop(Object *obj, const char *name,static void field_prop_set_default_value (ObjectProperty *op,Property *prop) { -if (!prop->set_default) { +QObject *defval; + +if (prop->defval.type ==QTYPE_NONE ) {return ; } + defval =qobject_from_qlit (&prop->defval);assert (prop->info->set_default_value); - prop->info->set_default_value (op, prop); + prop->info->set_default_value (op, prop, defval); +qobject_unref (defval); } ObjectProperty *
qom/property-types.c +18 -8
@@ -6,6 +6,8 @@#include "qapi/visitor.h" #include "qapi/error.h" #include "qemu/uuid.h" +#include "qapi/qmp/qnum.h" +#include "qapi/qmp/qbool.h" void field_prop_get_enum (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp)
@@ -26,10 +28,12 @@ void field_prop_set_enum(Object *obj, Visitor *v, const char *name, }void field_prop_set_default_value_enum (ObjectProperty *op, -const Property *prop) +const Property *prop, +const QObject *defval) { +QNum *qn =qobject_to (QNum, defval);object_property_set_default_str (op, -qapi_enum_lookup (prop->info->enum_table, prop->defval.i)); +qapi_enum_lookup (prop->info->enum_table,qnum_get_int (qn))); }const PropertyInfo prop_info_enum = {
@@ -80,9 +84,11 @@ static void prop_set_bit(Object *obj, Visitor *v, const char *name,bit_prop_set (obj, prop, value); } -static void set_default_value_bool (ObjectProperty *op,const Property *prop) +static void set_default_value_bool (ObjectProperty *op,const Property *prop, +const QObject *defval) { -object_property_set_default_bool (op, prop->defval.u); +QBool *qb =qobject_to (QBool, defval); +object_property_set_default_bool (op,qbool_get_bool (qb)); }const PropertyInfo prop_info_bit = {
@@ -190,15 +196,19 @@ static void set_uint8(Object *obj, Visitor *v, const char *name, void *opaque, }void field_prop_set_default_value_int (ObjectProperty *op, -const Property *prop) +const Property *prop, +const QObject *defval) { -object_property_set_default_int (op, prop->defval.i); +QNum *qn =qobject_to (QNum, defval); +object_property_set_default_int (op,qnum_get_int (qn)); }void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop) +const Property *prop, +const QObject *defval) { -object_property_set_default_uint (op, prop->defval.u); +QNum *qn =qobject_to (QNum, defval); +object_property_set_default_uint (op,qnum_get_uint (qn)); }const PropertyInfo prop_info_uint8 = {
Signature · 1 line
2.28.0
PATCH v3 13/19 qom: Fix documentation of UUID property type
In reply to Eduardo HabkostOn some cases, the documentation for UUID properties is lying:
properties defined using DEFINE_PROP_UUID_NODEFAULT are not set to "auto" by default. It's better to omit this instead of providing incorrect information.
---
This is a new patch addeed in v3 of this series.
--- hw/core/qdev-properties-system.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/core/qdev-properties-system.c +1 -1
@@ -1089,7 +1089,7 @@ static void set_default_uuid_auto(ObjectProperty *op, const Property *prop,const PropertyInfo qdev_prop_uuid = { .name ="str" , .description ="UUID (aka GUID) or \"" UUID_VALUE_AUTO -"\" for random value (default)" , +"\" for random value" , .get = get_uuid, .set = set_uuid, .set_default_value = set_default_uuid_auto,
Signature · 1 line
2.28.0
PATCH v3 14/19 qom: Don't ignore defval on UUID property
In reply to Eduardo HabkostUUID properties were weird because the value of .defval was completely ignored. Fix this by setting the default value to QLIT_QSTR("auto") on the default case, and actually using .defval inside .set_default_value.
---
This is a new patch added in v3 of this series.
This is similar (but not completely the same) to changes that were submitted as part of
"[PATCH v2 8/8] qom: Use qlit to represent property defaults".
--- include/hw/qdev-properties-system.h | 9 +++------ hw/core/qdev-properties-system.c | 10 +++++----- 2 files changed, 8 insertions(+), 11 deletions(-)
include/hw/qdev-properties-system.h +3 -6
@@ -63,14 +63,11 @@ extern const PropertyInfo qdev_prop_pcie_link_width;DEFINE_PROP_SIGNED (_n, _s, _f, _d, qdev_prop_pcie_link_width, \ PCIExpLinkWidth) +#define UUID_VALUE_AUTO "auto" +#define DEFINE_PROP_UUID (_name, _state, _field) \ DEFINE_PROP(_name, _state, _field, qdev_prop_uuid, QemuUUID, \ - /* \ - * Note that set_default_uuid_auto() currently \ - * ignores the actual value value of .defval, \ - * we just need it to not be not QTYPE_NONE \ - */ \ - .defval = QLIT_QNULL) + .defval = QLIT_QSTR(UUID_VALUE_AUTO))#define DEFINE_PROP_AUDIODEV (_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_audiodev, QEMUSoundCard)
hw/core/qdev-properties-system.c +5 -5
@@ -15,6 +15,7 @@#include "hw/qdev-properties-system.h" #include "qapi/error.h" #include "qapi/visitor.h" +#include "qapi/qmp/qstring.h" #include "qapi/qapi-types-block.h" #include "qapi/qapi-types-machine.h" #include "qapi/qapi-types-migration.h"
@@ -1059,8 +1060,6 @@ static void get_uuid(Object *obj, Visitor *v, const char *name, void *opaque,visit_type_str (v, name, &p, errp); } -#define UUID_VALUE_AUTO "auto" -static void set_uuid (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp) {
@@ -1080,10 +1079,11 @@ static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,g_free (str); } -static void set_default_uuid_auto (ObjectProperty *op,const Property *prop, +static void set_default_uuid (ObjectProperty *op,const Property *prop,const QObject *defval) { -object_property_set_default_str (op,UUID_VALUE_AUTO ); +QString *qs =qobject_to (QString, defval); +object_property_set_default_str (op,qstring_get_str (qs)); }const PropertyInfo qdev_prop_uuid = {
@@ -1092,5 +1092,5 @@ const PropertyInfo qdev_prop_uuid = { "\" for random value", .get = get_uuid, .set = set_uuid, - .set_default_value = set_default_uuid_auto, + .set_default_value = set_default_uuid, };
Signature · 1 line
2.28.0
PATCH v3 15/19 qom: Make object_property_set_default() public
In reply to Eduardo HabkostThe function will be used outside qom/object.c, to simplify the field property code that sets the property default value.
--- include/qom/object.h | 11 +++++++++++ qom/object.c | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-)
include/qom/object.h +11 -0
@@ -1090,6 +1090,17 @@ ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name, ObjectPropertyRelease *release, void *opaque); +/** +* object_property_set_default: +* @prop: the property to set +* @value: the value to be written to the property +* +* Set the property default value. +* +* Ownership of @value is transferred to the property. +*/ +void object_property_set_default (ObjectProperty *prop,QObject *value); +/** * object_property_set_default_bool: * @prop: the property to set
qom/object.c +1 -1
@@ -1547,7 +1547,7 @@ static void object_property_init_defval(Object *obj, ObjectProperty *prop)visit_free (v); } -static void object_property_set_default (ObjectProperty *prop,QObject *defval) +void object_property_set_default (ObjectProperty *prop,QObject *defval) {assert (!prop->defval);assert (!prop->init);
Signature · 1 line
2.28.0
PATCH v3 16/19 qom: Make PropertyInfo.set_default_value optional
In reply to Eduardo HabkostIf .set_default_value is not set, call object_property_set_default(). This will let us delete most of the .set_default_value functions later.
---
This is a new patch in v3 of this series.
In v2 of the series, equivalent functionality was implemented by "qom: Use qlit to represent property defaults".
--- include/qom/field-property.h | 3 +++ qom/field-property.c | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-)
include/qom/field-property.h +3 -0
@@ -53,6 +53,9 @@ struct PropertyInfo {* @set_default_value: Callback for initializing the default value * * @defval is a weak reference. +* +* Optional. If not set and Property.defval is not QTYPE_NONE, +* object_property_set_default() will be called. */ void (*set_default_value)(ObjectProperty *op,const Property *prop,const QObject *defval);
qom/field-property.c +10 -2
@@ -72,8 +72,16 @@ static void field_prop_set_default_value(ObjectProperty *op, } defval =qobject_from_qlit (&prop->defval); -assert (prop->info->set_default_value); - prop->info->set_default_value (op, prop, defval); +if (prop->info->set_default_value) { +/* .set_default_value() gets a weak reference */ + prop->info->set_default_value (op, prop, defval); + }else { +/* +* object_property_set_default() takes ownership, +* so qobject_ref() is needed. +*/ +object_property_set_default (op,qobject_ref (defval)); + }qobject_unref (defval); }
Signature · 1 line
2.28.0
PATCH v3 17/19 qom: Delete field_prop_set_default_value_*int()
In reply to Eduardo HabkostThe field_prop_set_default_value_*int() functions can be completely replaced by object_propert_set_default(), we don't need them anymore.
---
This is a new patch in v3 of this series.
In v2 of the series, equivalent changes were part of "qom: Use qlit to represent property defaults".
--- include/qom/field-property-internal.h | 6 ------ hw/core/qdev-properties-system.c | 2 -- qom/property-types.c | 25 ------------------------- 3 files changed, 33 deletions(-)
include/qom/field-property-internal.h +0 -6
@@ -16,12 +16,6 @@ void field_prop_set_enum(Object *obj, Visitor *v, const char *name,void field_prop_set_default_value_enum (ObjectProperty *op,const Property *prop,const QObject *defval); -void field_prop_set_default_value_int (ObjectProperty *op, -const Property *prop, -const QObject *defval); -void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop, -const QObject *defval);void field_prop_get_int32 (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp);
hw/core/qdev-properties-system.c +0 -2
@@ -571,7 +571,6 @@ const PropertyInfo qdev_prop_blocksize = { " and " MAX_BLOCK_SIZE_STR, .get = field_prop_get_size32, .set = set_blocksize, - .set_default_value = field_prop_set_default_value_uint, };/* --- Block device error handling policy --- */
@@ -769,7 +768,6 @@ const PropertyInfo qdev_prop_pci_devfn = { .print = print_pci_devfn, .get = field_prop_get_int32, .set = set_pci_devfn, - .set_default_value = field_prop_set_default_value_int, };/* --- pci host address --- */
qom/property-types.c +0 -25
@@ -195,27 +195,10 @@ static void set_uint8(Object *obj, Visitor *v, const char *name, void *opaque,visit_type_uint8 (v, name, ptr, errp); } -void field_prop_set_default_value_int (ObjectProperty *op, -const Property *prop, -const QObject *defval) -{ -QNum *qn =qobject_to (QNum, defval); -object_property_set_default_int (op,qnum_get_int (qn)); -} - -void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop, -const QObject *defval) -{ -QNum *qn =qobject_to (QNum, defval); -object_property_set_default_uint (op,qnum_get_uint (qn)); -} -const PropertyInfo prop_info_uint8 = { .name ="uint8" , .get = get_uint8, .set = set_uint8, - .set_default_value = field_prop_set_default_value_uint, };/* --- 16bit integer --- */
@@ -242,7 +225,6 @@ const PropertyInfo prop_info_uint16 = { .name = "uint16", .get = get_uint16, .set = set_uint16, - .set_default_value = field_prop_set_default_value_uint, };/* --- 32bit integer --- */
@@ -287,14 +269,12 @@ const PropertyInfo prop_info_uint32 = { .name = "uint32", .get = get_uint32, .set = set_uint32, - .set_default_value = field_prop_set_default_value_uint, };const PropertyInfo prop_info_int32 = { .name ="int32" , .get = field_prop_get_int32, .set = set_int32, - .set_default_value = field_prop_set_default_value_int, };/* --- 64bit integer --- */
@@ -339,14 +319,12 @@ const PropertyInfo prop_info_uint64 = { .name = "uint64", .get = get_uint64, .set = set_uint64, - .set_default_value = field_prop_set_default_value_uint, };const PropertyInfo prop_info_int64 = { .name ="int64" , .get = get_int64, .set = set_int64, - .set_default_value = field_prop_set_default_value_int, };/* --- string --- */
@@ -441,7 +419,6 @@ const PropertyInfo prop_info_size32 = { .name = "size", .get = field_prop_get_size32, .set = set_size32, - .set_default_value = field_prop_set_default_value_uint, };/* --- support for array properties --- */
@@ -505,7 +482,6 @@ const PropertyInfo prop_info_arraylen = { .name = "uint32", .get = get_uint32, .set = set_prop_arraylen, - .set_default_value = field_prop_set_default_value_uint, };/* --- 64bit unsigned int 'size' type --- */
@@ -532,7 +508,6 @@ const PropertyInfo prop_info_size = { .name = "size", .get = get_size, .set = set_size, - .set_default_value = field_prop_set_default_value_uint, };/* --- object link property --- */
Signature · 1 line
2.28.0
PATCH v3 18/19 qom: Delete set_default_uuid()
In reply to Eduardo HabkostThe function can be completely replaced by object_property_set_default(), we don't need it anymore.
---
This is a new patch in v3 of this series.
In v2 of the series, equivalent changes were part of "qom: Use qlit to represent property defaults".
--- hw/core/qdev-properties-system.c | 8 -------- 1 file changed, 8 deletions(-)
hw/core/qdev-properties-system.c +0 -8
@@ -1077,18 +1077,10 @@ static void set_uuid(Object *obj, Visitor *v, const char *name, void *opaque,g_free (str); } -static void set_default_uuid (ObjectProperty *op,const Property *prop, -const QObject *defval) -{ -QString *qs =qobject_to (QString, defval); -object_property_set_default_str (op,qstring_get_str (qs)); -} -const PropertyInfo qdev_prop_uuid = { .name ="str" , .description ="UUID (aka GUID) or \"" UUID_VALUE_AUTO "\" for random value" , .get = get_uuid, .set = set_uuid, - .set_default_value = set_default_uuid, };
Signature · 1 line
2.28.0
PATCH v3 19/19 qom: Delete set_default_value_bool()
In reply to Eduardo Habkostset_default_value_bool() can be completely replaced by object_property_set_default(), we don't need that function anymore.
---
This is a new patch in v3 of this series.
In v2 of the series, equivalent changes were part of "qom: Use qlit to represent property defaults".
--- qom/property-types.c | 10 ---------- 1 file changed, 10 deletions(-)
qom/property-types.c +0 -10
@@ -84,19 +84,11 @@ static void prop_set_bit(Object *obj, Visitor *v, const char *name,bit_prop_set (obj, prop, value); } -static void set_default_value_bool (ObjectProperty *op,const Property *prop, -const QObject *defval) -{ -QBool *qb =qobject_to (QBool, defval); -object_property_set_default_bool (op,qbool_get_bool (qb)); -} -const PropertyInfo prop_info_bit = { .name ="bool" , .description ="on/off" , .get = prop_get_bit, .set = prop_set_bit, - .set_default_value = set_default_value_bool, };/* Bit64 */
@@ -145,7 +137,6 @@ const PropertyInfo prop_info_bit64 = { .description = "on/off", .get = prop_get_bit64, .set = prop_set_bit64, - .set_default_value = set_default_value_bool, };/* --- bool --- */
@@ -172,7 +163,6 @@ const PropertyInfo prop_info_bool = { .name = "bool", .get = get_bool, .set = set_bool, - .set_default_value = set_default_value_bool, };/* --- 8bit integer --- */
Signature · 1 line
2.28.0