qom: Use qlit to represent property defaults
v2 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.
Changes v1 -> v2:
* Rebase to latest version of field properties series
* Fix unit test failure
* Coding style changes
Eduardo Habkost (8):
qobject: Include API docs in docs/devel/qobject.html
qnum: Make qnum_get_double() get const pointer
qnum: QNumValue type for QNum value literals
qnum: qnum_value_is_equal() function
qlit: Support all types of QNums
qlit: qlit_type() function
qom: Make object_property_set_default() public
qom: Use qlit to represent property defaults
docs/devel/index.rst | 1 + docs/devel/qobject.rst | 11 +++ include/hw/qdev-properties-system.h | 2 +- include/qapi/qmp/qlit.h | 16 +++- include/qapi/qmp/qnum.h | 47 ++++++++++- include/qapi/qmp/qobject.h | 48 +++++++---- include/qom/field-property-internal.h | 4 - include/qom/field-property.h | 26 +++--- include/qom/object.h | 11 +++ include/qom/property-types.h | 19 ++--- hw/core/qdev-properties-system.c | 8 -- qobject/qlit.c | 5 +- qobject/qnum.c | 116 +++++++++++++++----------- qom/field-property.c | 27 ++++-- qom/object.c | 2 +- qom/property-types.c | 36 ++------ tests/check-qjson.c | 72 ++++++++++++++-- tests/check-qnum.c | 14 ++-- 18 files changed, 301 insertions(+), 164 deletions(-) create mode 100644 docs/devel/qobject.rst
Signature · 1 line
2.28.0
PATCH v2 1/8 qobject: Include API docs in docs/devel/qobject.html
Render existing doc comments at docs/devel/qobject.html.
--- docs/devel/index.rst | 1 + docs/devel/qobject.rst | 11 +++++++++ include/qapi/qmp/qnum.h | 4 +++- include/qapi/qmp/qobject.h | 48 +++++++++++++++++++++++++------------- qobject/qnum.c | 19 ++++++++++++--- 5 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 docs/devel/qobject.rst
docs/devel/index.rst +1 -0
@@ -35,3 +35,4 @@ Contents: clocks qom block-coroutine-wrapper + qobject
docs/devel/qobject.rst new file +11 -0
@@ -0,0 +1,11 @@ +QObject API +=========== + +.. kernel-doc:: include/qapi/qmp/qobject.h + +QNum module +----------- + +.. kernel-doc:: include/qapi/qmp/qnum.h + +.. kernel-doc:: qobject/qnum.c
include/qapi/qmp/qnum.h +3 -1
@@ -23,7 +23,9 @@ typedef enum { QNUM_DOUBLE } QNumKind; -/* +/** +* DOC: +* * QNum encapsulates how our dialect of JSON fills in the blanks left * by the JSON specification (RFC 8259) regarding numbers. *
include/qapi/qmp/qobject.h +32 -16
97 lines not on this page · raw message
qobject/qnum.c +16 -3
@@ -17,6 +17,7 @@/** * qnum_from_int(): Create a new QNum from an int64_t +* @value: int64_t value * * Return strong reference. */
@@ -33,6 +34,7 @@ QNum *qnum_from_int(int64_t value)/** * qnum_from_uint(): Create a new QNum from an uint64_t +* @value: uint64_t value * * Return strong reference. */
@@ -49,6 +51,7 @@ QNum *qnum_from_uint(uint64_t value)/** * qnum_from_double(): Create a new QNum from a double +* @value: double value * * Return strong reference. */
@@ -65,6 +68,8 @@ QNum *qnum_from_double(double value)/** * qnum_get_try_int(): Get an integer representation of the number +* @qn: QNum object +* @val: pointer to value * * Return true on success. */
@@ -90,6 +95,7 @@ bool qnum_get_try_int(const QNum *qn, int64_t *val)/** * qnum_get_int(): Get an integer representation of the number +* @qn: QNum object * * assert() on failure. */
@@ -102,7 +108,9 @@ int64_t qnum_get_int(const QNum *qn) }/** -* qnum_get_uint(): Get an unsigned integer from the number +* qnum_value_get_try_uint(): Get an unsigned integer from the number +* @qn: QNum object +* @val: pointer to value * * Return true on success. */
@@ -128,6 +136,7 @@ bool qnum_get_try_uint(const QNum *qn, uint64_t *val)/** * qnum_get_uint(): Get an unsigned integer from the number +* @qn: QNum object * * assert() on failure. */
@@ -141,6 +150,7 @@ uint64_t qnum_get_uint(const QNum *qn)/** * qnum_get_double(): Get a float representation of the number +* @qn: QNum object * * qnum_get_double() loses precision for integers beyond 53 bits. */
@@ -200,6 +210,8 @@ char *qnum_to_string(QNum *qn)/** * qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object * * Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered
@@ -253,8 +265,9 @@ bool qnum_is_equal(const QObject *x, const QObject *y) }/** -* qnum_destroy_obj(): Free all memory allocated by a -* QNum object +* qnum_destroy_obj(): Free all memory allocated by a QNum object +* +* @obj: QNum object to be destroyed */ void qnum_destroy_obj (QObject *obj) {
Signature · 1 line
2.28.0
Re: [PATCH v2 1/8] qobject: Include API docs in docs/devel/qobject.html
Quoted 4 lines · Eduardo Habkost
Render existing doc comments at docs/devel/qobject.html.
Signed-off-by Eduardo Habkost <[email protected]>
---
Quoted 258 lines
docs/devel/index.rst | 1 + docs/devel/qobject.rst | 11 +++++++++ include/qapi/qmp/qnum.h | 4 +++- include/qapi/qmp/qobject.h | 48 +++++++++++++++++++++++++------------- qobject/qnum.c | 19 ++++++++++++--- 5 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 docs/devel/qobject.rstdocs/devel/index.rst +1
-0@@ -35,3 +35,4 @@ Contents: clocks qom block-coroutine-wrapper + qobjectdocs/devel/qobject.rst new file +11
-0@@ -0,0 +1,11 @@ +QObject API +=========== + +.. kernel-doc:: include/qapi/qmp/qobject.h + +QNum module +----------- + +.. kernel-doc:: include/qapi/qmp/qnum.h + +.. kernel-doc:: qobject/qnum.cinclude/qapi/qmp/qnum.h +3
-1@@ -23,7 +23,9 @@ typedef enum { QNUM_DOUBLE } QNumKind; -/* +/** +* DOC: +* * QNum encapsulates how our dialect of JSON fills in the blanks left * by the JSON specification (RFC 8259) regarding numbers. * include/qapi/qmp/qobject.h +1
-1@@ -1,5 +1,5 @@/* -* QEMU Object Model. +* QObject API * * Based on ideas by Avi Kivity <[email protected]> * @@ -10,24 +10,31 @@* * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
+ */include/qapi/qmp/qobject.h +12
-12+ +/** +* DOC: QObject Reference Counts Terminology * -* QObject Reference Counts Terminology -* ------------------------------------ +* Returning references +* -------------------- * -* - Returning references: A function that returns an object may -* return it as either a weak or a strong reference. If the -* reference is strong, you are responsible for calling -* qobject_unref() on the reference when you are done. +* A function that returns an object may return it as either a +* weak or a strong reference. If the reference is strong, you +* are responsible for calling qobject_unref() on the reference +* when you are done. * -* If the reference is weak, the owner of the reference may free it at -* any time in the future. Before storing the reference anywhere, you -* should call qobject_ref() to make the reference strong. +* If the reference is weak, the owner of the reference may free it at +* any time in the future. Before storing the reference anywhere, you +* should call qobject_ref() to make the reference strong. * -* - Transferring ownership: when you transfer ownership of a reference -* by calling a function, you are no longer responsible for calling -* qobject_unref() when the reference is no longer needed. In other words,
- * when the function returns you must behave as if the reference to the
- * passed object was weak.
+ * Transferring ownership
+ * ----------------------
+ *
+ * When you transfer ownership of a reference by calling a
+ * function, you are no longer responsible for calling
+ *qobject_unref()when the reference is no longer needed. In
+ * other words, when the function returns you must behave as if
+ * the reference to the passed object was weak.
*/
#ifndefQOBJECT_H
#defineQOBJECT_Hinclude/qapi/qmp/qobject.h +10
-1@@ -81,6 +88,8 @@ static inline void qobject_ref_impl(QObject *obj)/** * qobject_is_equal(): Return whether the two objects are equal. +* @x: QObject pointer +* @y: QObject pointer * * Any of the pointers may be NULL; return true if both are. Always * return false if only one is (therefore a QNull object is not @@ -90,6 +99,7 @@ bool qobject_is_equal(const QObject *x, const QObject *y);/** * qobject_destroy(): Free resources used by the object +* @obj: QObject pointer */ void qobject_destroy (QObject *obj);@@ -103,6 +113,7 @@ static inline void qobject_unref_impl(QObject *obj)/** * qobject_ref(): Increment QObject's reference count +* @obj: QObject pointer * * Returns: the same @obj. The type of @obj will be propagated to the * return type. @@ -115,12 +126,14 @@ static inline void qobject_unref_impl(QObject *obj)/** * qobject_unref(): Decrement QObject's reference count, deallocate -* when it reaches zero +* when it reaches zero +* @obj: QObject pointer */ #define qobject_unref (obj) qobject_unref_impl(QOBJECT(obj))/** * qobject_type(): Return the QObject's type +* @obj: QObject pointer */ static inline QType qobject_type (const QObject *obj) {@@ -130,6 +143,9 @@ static inline QType qobject_type(const QObject *obj)/** * qobject_check_type(): Helper function for the qobject_to() macro. +* @obj: QObject pointer +* @type: Expected type of QObject +* * Return @obj, but only if @obj is not NULL and @type is equal to * @obj's type. Return NULL otherwise. */ qobject/qnum.c +16
-4@@ -17,6 +17,7 @@/** * qnum_from_int(): Create a new QNum from an int64_t +* @value: int64_t value * * Return strong reference. */ @@ -33,6 +34,7 @@ QNum *qnum_from_int(int64_t value)/** * qnum_from_uint(): Create a new QNum from an uint64_t +* @value: uint64_t value * * Return strong reference. */ @@ -49,6 +51,7 @@ QNum *qnum_from_uint(uint64_t value)/** * qnum_from_double(): Create a new QNum from a double +* @value: double value * * Return strong reference. */ @@ -65,6 +68,8 @@ QNum *qnum_from_double(double value)/** * qnum_get_try_int(): Get an integer representation of the number +* @qn: QNum object +* @val: pointer to value * * Return true on success. */ @@ -90,6 +95,7 @@ bool qnum_get_try_int(const QNum *qn, int64_t *val)/** * qnum_get_int(): Get an integer representation of the number +* @qn: QNum object * * assert() on failure. */ @@ -102,7 +108,9 @@ int64_t qnum_get_int(const QNum *qn) }/** -* qnum_get_uint(): Get an unsigned integer from the number +* qnum_value_get_try_uint(): Get an unsigned integer from the number +* @qn: QNum object +* @val: pointer to value * * Return true on success. */ @@ -128,6 +136,7 @@ bool qnum_get_try_uint(const QNum *qn, uint64_t *val)/** * qnum_get_uint(): Get an unsigned integer from the number +* @qn: QNum object * * assert() on failure. */ @@ -141,6 +150,7 @@ uint64_t qnum_get_uint(const QNum *qn)/** * qnum_get_double(): Get a float representation of the number +* @qn: QNum object * * qnum_get_double() loses precision for integers beyond 53 bits. */ @@ -200,6 +210,8 @@ char *qnum_to_string(QNum *qn)/** * qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object * * Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered @@ -253,8 +265,9 @@ bool qnum_is_equal(const QObject *x, const QObject *y) }/** -* qnum_destroy_obj(): Free all memory allocated by a -* QNum object +* qnum_destroy_obj(): Free all memory allocated by a QNum object +* +* @obj: QNum object to be destroyed */ void qnum_destroy_obj (QObject *obj) { --2.28.0
Signature · 1 line
Marc-André Lureau
Re: [PATCH v2 1/8] qobject: Include API docs in docs/devel/qobject.html
In reply to Eduardo HabkostQuoted 43 lines · Eduardo Habkost
Render existing doc comments at docs/devel/qobject.html.
Signed-off-by Eduardo Habkost <[email protected]>--- docs/devel/index.rst | 1 + docs/devel/qobject.rst | 11 +++++++++ include/qapi/qmp/qnum.h | 4 +++- include/qapi/qmp/qobject.h | 48 +++++++++++++++++++++++++------------- qobject/qnum.c | 19 ++++++++++++--- 5 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 docs/devel/qobject.rstdocs/devel/index.rst +1
-0@@ -35,3 +35,4 @@ Contents: clocks qom block-coroutine-wrapper + qobjectdocs/devel/qobject.rst new file +11
-0@@ -0,0 +1,11 @@ +QObject API +=========== + +.. kernel-doc:: include/qapi/qmp/qobject.h + +QNum module +----------- + +.. kernel-doc:: include/qapi/qmp/qnum.h + +.. kernel-doc:: qobject/qnum.cinclude/qapi/qmp/qnum.h +0
-0
include/qapi/qmp/qnum.h typedef enum { · line 31
Quoted 10 lines
include/qapi/qmp/qnum.h +3
-1@@ -23,7 +23,9 @@ typedef enum { QNUM_DOUBLE } QNumKind; -/* +/** +* DOC: +* * QNum encapsulates how our dialect of JSON fills in the blanks left * by the JSON specification (RFC 8259) regarding numbers. *
Educate me: what does DOC: do?
Quoted 191 lines
include/qapi/qmp/qobject.h +32
-16@@ -1,5 +1,5 @@/* -* QEMU Object Model. +* QObject API * * Based on ideas by Avi Kivity <[email protected]> * @@ -10,24 +10,31 @@* * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. * See the COPYING.LIB file in the top-level directory. +*/ + +/** +* DOC: QObject Reference Counts Terminology * -* QObject Reference Counts Terminology -* ------------------------------------ +* Returning references +* -------------------- * -* - Returning references: A function that returns an object may -* return it as either a weak or a strong reference. If the -* reference is strong, you are responsible for calling -* qobject_unref() on the reference when you are done. +* A function that returns an object may return it as either a +* weak or a strong reference. If the reference is strong, you +* are responsible for calling qobject_unref() on the reference +* when you are done. * -* If the reference is weak, the owner of the reference may free it at -* any time in the future. Before storing the reference anywhere, you -* should call qobject_ref() to make the reference strong. +* If the reference is weak, the owner of the reference may free it at +* any time in the future. Before storing the reference anywhere, you +* should call qobject_ref() to make the reference strong. * -* - Transferring ownership: when you transfer ownership of a reference -* by calling a function, you are no longer responsible for calling -* qobject_unref() when the reference is no longer needed. In other words, -* when the function returns you must behave as if the reference to the -* passed object was weak. +* Transferring ownership +* ---------------------- +* +* When you transfer ownership of a reference by calling a +* function, you are no longer responsible for calling +* qobject_unref() when the reference is no longer needed. In +* other words, when the function returns you must behave as if +* the reference to the passed object was weak. */ #ifndef QOBJECT_H #define QOBJECT_H @@ -81,6 +88,8 @@ static inline void qobject_ref_impl(QObject *obj)/** * qobject_is_equal(): Return whether the two objects are equal. +* @x: QObject pointer +* @y: QObject pointer * * Any of the pointers may be NULL; return true if both are. Always * return false if only one is (therefore a QNull object is not @@ -90,6 +99,7 @@ bool qobject_is_equal(const QObject *x, const QObject *y);/** * qobject_destroy(): Free resources used by the object +* @obj: QObject pointer */ void qobject_destroy (QObject *obj);@@ -103,6 +113,7 @@ static inline void qobject_unref_impl(QObject *obj)/** * qobject_ref(): Increment QObject's reference count +* @obj: QObject pointer * * Returns: the same @obj. The type of @obj will be propagated to the * return type. @@ -115,12 +126,14 @@ static inline void qobject_unref_impl(QObject *obj)/** * qobject_unref(): Decrement QObject's reference count, deallocate -* when it reaches zero +* when it reaches zero +* @obj: QObject pointer */ #define qobject_unref (obj) qobject_unref_impl(QOBJECT(obj))/** * qobject_type(): Return the QObject's type +* @obj: QObject pointer */ static inline QType qobject_type (const QObject *obj) {@@ -130,6 +143,9 @@ static inline QType qobject_type(const QObject *obj)/** * qobject_check_type(): Helper function for the qobject_to() macro. +* @obj: QObject pointer +* @type: Expected type of QObject +* * Return @obj, but only if @obj is not NULL and @type is equal to * @obj's type. Return NULL otherwise. */ qobject/qnum.c +13
-1@@ -17,6 +17,7 @@/** * qnum_from_int(): Create a new QNum from an int64_t +* @value: int64_t value * * Return strong reference. */ @@ -33,6 +34,7 @@ QNum *qnum_from_int(int64_t value)/** * qnum_from_uint(): Create a new QNum from an uint64_t +* @value: uint64_t value * * Return strong reference. */ @@ -49,6 +51,7 @@ QNum *qnum_from_uint(uint64_t value)/** * qnum_from_double(): Create a new QNum from a double +* @value: double value * * Return strong reference. */ @@ -65,6 +68,8 @@ QNum *qnum_from_double(double value)/** * qnum_get_try_int(): Get an integer representation of the number +* @qn: QNum object +* @val: pointer to value * * Return true on success. */ @@ -90,6 +95,7 @@ bool qnum_get_try_int(const QNum *qn, int64_t *val)/** * qnum_get_int(): Get an integer representation of the number +* @qn: QNum object * * assert() on failure. */ @@ -102,7 +108,9 @@ int64_t qnum_get_int(const QNum *qn) }/** -* qnum_get_uint(): Get an unsigned integer from the number +* qnum_value_get_try_uint(): Get an unsigned integer from the number +* @qn: QNum object +* @val: pointer to value * * Return true on success. */ @@ -128,6 +136,7 @@ bool qnum_get_try_uint(const QNum *qn, uint64_t *val)/** * qnum_get_uint(): Get an unsigned integer from the number +* @qn: QNum object * * assert() on failure. */ @@ -141,6 +150,7 @@ uint64_t qnum_get_uint(const QNum *qn)/** * qnum_get_double(): Get a float representation of the number +* @qn: QNum object * * qnum_get_double() loses precision for integers beyond 53 bits. */ @@ -200,6 +210,8 @@ char *qnum_to_string(QNum *qn)/** * qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object * * Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered @@ -253,8 +265,9 @@ bool qnum_is_equal(const QObject *x, const QObject *y) }
qobject/qnum.c bool qnum_is_equal(const QObject *x, const QObject *y) · line 273
Quoted 10 lines
qobject/qnum.c +3
-2/** -* qnum_destroy_obj(): Free all memory allocated by a -* QNum object +* qnum_destroy_obj(): Free all memory allocated by a QNum object +* +* @obj: QNum object to be destroyed */ void qnum_destroy_obj (QObject *obj) {
Many lines of the form
+ * @foo: a foo
One of my reasons to dislike GTK-Doc. Oh well, it's what we're using.
Re: [PATCH v2 1/8] qobject: Include API docs in docs/devel/qobject.html
CCing Peter, Sphinx documentation machinery maintainer.
Quoted 6 lines · Markus Armbruster
Quoted 4 lines · Eduardo Habkost
Render existing doc comments at docs/devel/qobject.html.
Signed-off-by Eduardo Habkost <[email protected]>---
[...]
Quoted 10 lines
Quoted 9 lines
/**
- *qnum_destroy_obj(): Free all memory allocated by a
- * QNum object
+ *qnum_destroy_obj(): Free all memory allocated by a QNum object
+ *
+ * @obj: QNum object to be destroyed
*/
voidqnum_destroy_obj(QObject *obj)
{
Quoted 3 lines
Many lines of the form
+ * @foo: a foo
I hate them too.
Quoted 2 lines
One of my reasons to dislike GTK-Doc. Oh well, it's what we're using.
It's not gtk-doc. It's kernel-doc who generates warnings if some parameters are not documented, and maybe we should silence them.
Signature · 1 line
Eduardo
PATCH v2 2/8 qnum: Make qnum_get_double() get const pointer
In reply to Eduardo Habkostqnum_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
@@ -66,7 +66,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
@@ -154,7 +154,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
Re: [PATCH v2 2/8] qnum: Make qnum_get_double() get const pointer
Quoted 5 lines · Eduardo Habkost
qnum_get_double()won't change the object, the argument can be const.Signed-off-by Eduardo Habkost <[email protected]>
No idea why I didn't make it const in the first place.
---
Quoted 35 lines
include/qapi/qmp/qnum.h | 2 +- qobject/qnum.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)include/qapi/qmp/qnum.h +1
-1@@ -66,7 +66,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
-2@@ -154,7 +154,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 : --2.28.0
Signature · 1 line
Marc-André Lureau
PATCH v2 3/8 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 v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields
--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)
include/qapi/qmp/qnum.h +38 -2
@@ -46,20 +46,56 @@ 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 { +/* private: */ 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_from_int(): Create a new QNum from a QNumValue +* @value: QNumValue +* +* Return strong reference. +*/ +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); +/** +* qnum_get_value(): Get QNumValue from QNum +* @qn: QNum object +*/ +static inline const QNumValue *qnum_get_value (const QNum *qn) +{ +return &qn->value; +} +bool qnum_get_try_int (const QNum *qn,int64_t *val);int64_t qnum_get_int (const QNum *qn);
qobject/qnum.c +39 -39
156 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
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 47 lines · Eduardo Habkost
Provide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)include/qapi/qmp/qnum.h +16
-2@@ -46,20 +46,56 @@ 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 { +/* private: */ 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) \
include/qapi/qmp/qnum.h typedef enum { · line 80
Quoted 10 lines
include/qapi/qmp/qnum.h +7
-0+ { .kind = QNUM_DOUBLE, .u.dbl = value } + +struct QNum { +struct QObjectBase_ base; +QNumValue value; }; +/** +* qnum_from_int(): Create a new QNum from a QNumValue
Copy-pasta qnum_from_int() -> qnum_from_value()
I wonder if there is a check for that kind of mistake, as it may be common.
+ * @value: QNumValue
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 5 lines
include/qapi/qmp/qnum.h +4
-0+* +* Return strong reference. +*/ +QNum *qnum_from_value (QNumValue value);
+
Quoted 4 lines
include/qapi/qmp/qnum.h +0
-0QNum *qnum_from_int (int64_t value);QNum *qnum_from_uint (uint64_t value);QNum *qnum_from_double (double value);
include/qapi/qmp/qnum.h typedef enum { · line 101
Quoted 10 lines
include/qapi/qmp/qnum.h +9
-0+/** +* qnum_get_value(): Get QNumValue from QNum +* @qn: QNum object +*/ +static inline const QNumValue *qnum_get_value (const QNum *qn) +{ +return &qn->value; +} +
Nothing uses that function in this patch. Should be put into use.
bool qnum_get_try_int(const QNum *qn, int64_t *val);
qobject/qnum.c line 19
Quoted 12 lines
include/qapi/qmp/qnum.h +0
-0int64_t qnum_get_int (const QNum *qn);qobject/qnum.c +1
-0@@ -15,6 +15,15 @@#include "qemu/osdep.h" #include "qapi/qmp/qnum.h" +QNum *qnum_from_value (QNumValue value)
I wonder if it shouldn't be made "inline" in header too (if that can help avoid argument copy).
+{
Quoted 120 lines
qobject/qnum.c +28
-33+QNum *qn =g_new (QNum,1 ); + +qobject_init (QOBJECT (qn),QTYPE_QNUM ); + qn->value = value; +return qn; +} +/** * qnum_from_int(): Create a new QNum from an int64_t * @value: int64_t value @@ -23,13 +32,7 @@*/ QNum *qnum_from_int (int64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_I64 ; - qn->u.i64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_INT (value)); }/** @@ -40,13 +43,7 @@ QNum *qnum_from_int(int64_t value)*/ QNum *qnum_from_uint (uint64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_U64 ; - qn->u.u64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_UINT (value)); }/** @@ -57,13 +54,7 @@ QNum *qnum_from_uint(uint64_t value)*/ QNum *qnum_from_double (double value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_DOUBLE ; - qn->u.dbl = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_DOUBLE (value)); }/** @@ -75,15 +66,17 @@ QNum *qnum_from_double(double value)*/ bool qnum_get_try_int (const QNum *qn,int64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : -if (qn->u.u64 >INT64_MAX ) { +if (qv->u.u64 >INT64_MAX ) {return false; } - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -116,15 +109,17 @@ int64_t qnum_get_int(const QNum *qn)*/ bool qnum_get_try_uint (const QNum *qn,uint64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -if (qn->u.i64 <0 ) { +if (qv->u.i64 <0 ) {return false; } - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -156,13 +151,15 @@ uint64_t qnum_get_uint(const QNum *qn)*/ double qnum_get_double (const QNum *qn) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -return qn->u.i64; +return qv->u.i64;case QNUM_U64 : -return qn->u.u64; +return qv->u.u64;case QNUM_DOUBLE : -return qn->u.dbl; +return qv->u.dbl; }assert (0 );
qobject/qnum.c double qnum_get_double(const QNum *qn) · line 172
Quoted 5 lines
qobject/qnum.c +1
-0@@ -171,14 +168,15 @@ double qnum_get_double(const QNum *qn)char *qnum_to_string (QNum *qn) { +const QNumValue *qv = &qn->value;
qnum_get_value() ?
char *buffer;
Quoted 82 lines
qobject/qnum.c +8
-6int len; -switch (qn->kind) { +switch (qv->kind) {case QNUM_I64 : -return g_strdup_printf ("%" PRId64, qn->u.i64); +return g_strdup_printf ("%" PRId64, qv->u.i64);case QNUM_U64 : -return g_strdup_printf ("%" PRIu64, qn->u.u64); +return g_strdup_printf ("%" PRIu64, qv->u.u64);case QNUM_DOUBLE :/* FIXME: snprintf() is locale dependent; but JSON requires * numbers to be formatted as if in the C locale. Dependence @@ -189,7 +187,7 @@ char *qnum_to_string(QNum *qn)* rounding errors; we should be using DBL_DECIMAL_DIG (17), * and only rounding to a shorter number if the result would * still produce the same floating point value. */ - buffer =g_strdup_printf ("%f" , qn->u.dbl); + buffer =g_strdup_printf ("%f" , qv->u.dbl); len =strlen (buffer);while (len >0 && buffer[len -1 ] =='0' ) { len--;@@ -221,8 +219,10 @@ 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 *qnum_x =qobject_to (QNum, x); +const QNum *qnum_y =qobject_to (QNum, y); +const QNumValue *num_x = &qnum_x->value; +const QNumValue *num_y = &qnum_y->value;switch (num_x->kind) {case QNUM_I64 :tests/check-qnum.c +7
-8@@ -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); } --
Quoted 4 lines
2.28.0
lgtm otherwise
Signature · 1 line
Marc-André Lureau
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 56 lines · Marc-André Lureau
Quoted 54 lines · Eduardo Habkost
Provide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)include/qapi/qmp/qnum.h +21
-2@@ -46,20 +46,56 @@ 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 { +/* private: */ 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; };
Quoted 5 lines
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 3 lines
include/qapi/qmp/qnum.h +2
-0+/** +* qnum_from_int(): Create a new QNum from a QNumValue Copy-pasta
qnum_from_int()->qnum_from_value()
Oops! I will fix it in v3, or submit a fixup patch if that's the only needed change.
Quoted 2 lines
I wonder if there is a check for that kind of mistake, as it may be common.
It looks like kernel-doc ignores what's before the colon in the summary line. It probably shouldn't.
Quoted 12 lines
+ * @value: QNumValue
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 5 lines
include/qapi/qmp/qnum.h +4
-0+* +* Return strong reference. +*/ +QNum *qnum_from_value (QNumValue value);+
Quoted 4 lines
include/qapi/qmp/qnum.h +0
-0QNum *qnum_from_int (int64_t value);QNum *qnum_from_uint (uint64_t value);QNum *qnum_from_double (double value);
Quoted 12 lines
include/qapi/qmp/qnum.h typedef enum { · line 101
Quoted 10 lines
include/qapi/qmp/qnum.h +9
-0+/** +* qnum_get_value(): Get QNumValue from QNum +* @qn: QNum object +*/ +static inline const QNumValue *qnum_get_value (const QNum *qn) +{ +return &qn->value; +} +Nothing uses that function in this patch. Should be put into use.
It is used in patch 5/8. Why do you think it's necessary to use it in internal code too?
Quoted 12 lines
bool
qnum_get_try_int(const QNum *qn, int64_t *val);Quoted 10 lines
include/qapi/qmp/qnum.h +0
-0int64_t qnum_get_int (const QNum *qn);qobject/qnum.c +0
-0@@ -15,6 +15,15 @@#include "qemu/osdep.h" #include "qapi/qmp/qnum.h"
Quoted 5 lines
qobject/qnum.c line 19
Quoted 2 lines
qobject/qnum.c +1
-0+QNum *qnum_from_value (QNumValue value)I wonder if it shouldn't be made "inline" in header too (if that can help avoid argument copy).
I'm unsure. I would make it inline (in a separate patch) if there's evidence it's worth it. I expect the g_new() call to be more expensive than any argument copying, though.
Quoted 123 lines
+{
Quoted 121 lines
qobject/qnum.c +28
-33+QNum *qn =g_new (QNum,1 ); + +qobject_init (QOBJECT (qn),QTYPE_QNUM ); + qn->value = value; +return qn; +} +/** * qnum_from_int(): Create a new QNum from an int64_t * @value: int64_t value @@ -23,13 +32,7 @@*/ QNum *qnum_from_int (int64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_I64 ; - qn->u.i64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_INT (value)); }/** @@ -40,13 +43,7 @@ QNum *qnum_from_int(int64_t value)*/ QNum *qnum_from_uint (uint64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_U64 ; - qn->u.u64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_UINT (value)); }/** @@ -57,13 +54,7 @@ QNum *qnum_from_uint(uint64_t value)*/ QNum *qnum_from_double (double value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_DOUBLE ; - qn->u.dbl = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_DOUBLE (value)); }/** @@ -75,15 +66,17 @@ QNum *qnum_from_double(double value)*/ bool qnum_get_try_int (const QNum *qn,int64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : -if (qn->u.u64 >INT64_MAX ) { +if (qv->u.u64 >INT64_MAX ) {return false; } - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -116,15 +109,17 @@ int64_t qnum_get_int(const QNum *qn)*/ bool qnum_get_try_uint (const QNum *qn,uint64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -if (qn->u.i64 <0 ) { +if (qv->u.i64 <0 ) {return false; } - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -156,13 +151,15 @@ uint64_t qnum_get_uint(const QNum *qn)*/ double qnum_get_double (const QNum *qn) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -return qn->u.i64; +return qv->u.i64;case QNUM_U64 : -return qn->u.u64; +return qv->u.u64;case QNUM_DOUBLE : -return qn->u.dbl; +return qv->u.dbl; }assert (0 );@@ -171,14 +168,15 @@ double qnum_get_double(const QNum *qn)
Quoted 6 lines
qobject/qnum.c double qnum_get_double(const QNum *qn) · line 174
Quoted 4 lines
qobject/qnum.c +1
-0char *qnum_to_string (QNum *qn) { +const QNumValue *qv = &qn->value;
qnum_get_value()?
I prefer to not hide this behind a function call, in internal code. But I don't mind changing it if you think it's important.
Quoted 81 lines
char *buffer;Quoted 79 lines
qobject/qnum.c +8
-6int len; -switch (qn->kind) { +switch (qv->kind) {case QNUM_I64 : -return g_strdup_printf ("%" PRId64, qn->u.i64); +return g_strdup_printf ("%" PRId64, qv->u.i64);case QNUM_U64 : -return g_strdup_printf ("%" PRIu64, qn->u.u64); +return g_strdup_printf ("%" PRIu64, qv->u.u64);case QNUM_DOUBLE :/* FIXME: snprintf() is locale dependent; but JSON requires * numbers to be formatted as if in the C locale. Dependence @@ -189,7 +187,7 @@ char *qnum_to_string(QNum *qn)* rounding errors; we should be using DBL_DECIMAL_DIG (17), * and only rounding to a shorter number if the result would * still produce the same floating point value. */ - buffer =g_strdup_printf ("%f" , qn->u.dbl); + buffer =g_strdup_printf ("%f" , qv->u.dbl); len =strlen (buffer);while (len >0 && buffer[len -1 ] =='0' ) { len--;@@ -221,8 +219,10 @@ 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 *qnum_x =qobject_to (QNum, x); +const QNum *qnum_y =qobject_to (QNum, y); +const QNumValue *num_x = &qnum_x->value; +const QNumValue *num_y = &qnum_y->value;switch (num_x->kind) {case QNUM_I64 :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);
Quoted 8 lines
Quoted 7 lines
tests/check-qnum.c +0
-1qobject_unref (qn); } --2.28.0
lgtm otherwise
Thanks!
Signature · 1 line
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 99 lines · Eduardo Habkost
Quoted 1 line · Marc-André Lureau
On Tue, Nov 17, 2020 at 2:43 AM Eduardo Habkost <[email protected]>
Quoted 55 lines · wrote:
Quoted 55 lines
Provide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)include/qapi/qmp/qnum.h +21
-2@@ -46,20 +46,56 @@ 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 { +/* private: */ 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; };Quoted 5 lines
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 3 lines
include/qapi/qmp/qnum.h +2
-0+/** +* qnum_from_int(): Create a new QNum from a QNumValue Copy-pasta
qnum_from_int()->qnum_from_value()Oops! I will fix it in v3, or submit a fixup patch if that's the only needed change.
Quoted 2 lines
I wonder if there is a check for that kind of mistake, as it may be
common.
It looks like kernel-doc ignores what's before the colon in the summary line. It probably shouldn't.
Quoted 12 lines
+ * @value: QNumValue
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 5 lines
include/qapi/qmp/qnum.h +4
-0+* +* Return strong reference. +*/ +QNum *qnum_from_value (QNumValue value);+
Quoted 4 lines
include/qapi/qmp/qnum.h +0
-0QNum *qnum_from_int (int64_t value);QNum *qnum_from_uint (uint64_t value);QNum *qnum_from_double (double value);Quoted 12 lines
include/qapi/qmp/qnum.h typedef enum { · line 101
Quoted 10 lines
include/qapi/qmp/qnum.h +9
-0+/** +* qnum_get_value(): Get QNumValue from QNum +* @qn: QNum object +*/ +static inline const QNumValue *qnum_get_value (const QNum *qn) +{ +return &qn->value; +} +Nothing uses that function in this patch. Should be put into use.
Quoted 3 lines
It is used in patch 5/8. Why do you think it's necessary to use it in internal code too?
Not necessarily, just want to make sure we don't introduce dead code. Fine it's used later, perhaps worth noting in the commit message.
Quoted 19 lines
Quoted 13 lines
bool
qnum_get_try_int(const QNum *qn, int64_t *val);Quoted 10 lines
include/qapi/qmp/qnum.h +0
-0int64_t qnum_get_int (const QNum *qn);qobject/qnum.c +0
-0@@ -15,6 +15,15 @@#include "qemu/osdep.h" #include "qapi/qmp/qnum.h" Quoted 5 lines
qobject/qnum.c line 19
Quoted 2 lines
qobject/qnum.c +1
-0+QNum *qnum_from_value (QNumValue value)I wonder if it shouldn't be made "inline" in header too (if that can help avoid argument copy).
Quoted 4 lines
I'm unsure. I would make it inline (in a separate patch) if there's evidence it's worth it. I expect the
g_new()call to be more expensive than any argument copying, though.
ok
Quoted 130 lines
Quoted 123 lines
+{
Quoted 121 lines
qobject/qnum.c +28
-33+QNum *qn =g_new (QNum,1 ); + +qobject_init (QOBJECT (qn),QTYPE_QNUM ); + qn->value = value; +return qn; +} +/** * qnum_from_int(): Create a new QNum from an int64_t * @value: int64_t value @@ -23,13 +32,7 @@*/ QNum *qnum_from_int (int64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_I64 ; - qn->u.i64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_INT (value)); }/** @@ -40,13 +43,7 @@ QNum *qnum_from_int(int64_t value)*/ QNum *qnum_from_uint (uint64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_U64 ; - qn->u.u64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_UINT (value)); }/** @@ -57,13 +54,7 @@ QNum *qnum_from_uint(uint64_t value)*/ QNum *qnum_from_double (double value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_DOUBLE ; - qn->u.dbl = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_DOUBLE (value)); }/** @@ -75,15 +66,17 @@ QNum *qnum_from_double(double value)*/ bool qnum_get_try_int (const QNum *qn,int64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : -if (qn->u.u64 >INT64_MAX ) { +if (qv->u.u64 >INT64_MAX ) {return false; } - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -116,15 +109,17 @@ int64_t qnum_get_int(const QNum *qn)*/ bool qnum_get_try_uint (const QNum *qn,uint64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -if (qn->u.i64 <0 ) { +if (qv->u.i64 <0 ) {return false; } - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -156,13 +151,15 @@ uint64_t qnum_get_uint(const QNum *qn)*/ double qnum_get_double (const QNum *qn) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -return qn->u.i64; +return qv->u.i64;case QNUM_U64 : -return qn->u.u64; +return qv->u.u64;case QNUM_DOUBLE : -return qn->u.dbl; +return qv->u.dbl; }assert (0 );@@ -171,14 +168,15 @@ double qnum_get_double(const QNum *qn)Quoted 6 lines
qobject/qnum.c double qnum_get_double(const QNum *qn) · line 174
Quoted 4 lines
qobject/qnum.c +1
-0char *qnum_to_string (QNum *qn) { +const QNumValue *qv = &qn->value;
qnum_get_value()?
Quoted 3 lines
I prefer to not hide this behind a function call, in internal code. But I don't mind changing it if you think it's important.
no, it's ok to me
Quoted 97 lines
Quoted 82 lines
char *buffer;Quoted 79 lines
qobject/qnum.c +8
-6int len; -switch (qn->kind) { +switch (qv->kind) {case QNUM_I64 : -return g_strdup_printf ("%" PRId64, qn->u.i64); +return g_strdup_printf ("%" PRId64, qv->u.i64);case QNUM_U64 : -return g_strdup_printf ("%" PRIu64, qn->u.u64); +return g_strdup_printf ("%" PRIu64, qv->u.u64);case QNUM_DOUBLE :/* FIXME: snprintf() is locale dependent; but JSON requires * numbers to be formatted as if in the C locale. Dependence @@ -189,7 +187,7 @@ char *qnum_to_string(QNum *qn)* rounding errors; we should be using DBL_DECIMAL_DIG (17), * and only rounding to a shorter number if the result would * still produce the same floating point value. */ - buffer =g_strdup_printf ("%f" , qn->u.dbl); + buffer =g_strdup_printf ("%f" , qv->u.dbl); len =strlen (buffer);while (len >0 && buffer[len -1 ] =='0' ) { len--;@@ -221,8 +219,10 @@ 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 *qnum_x =qobject_to (QNum, x); +const QNum *qnum_y =qobject_to (QNum, y); +const QNumValue *num_x = &qnum_x->value; +const QNumValue *num_y = &qnum_y->value;switch (num_x->kind) {case QNUM_I64 :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);Quoted 8 lines
Quoted 7 lines
tests/check-qnum.c +0
-1qobject_unref (qn); } --2.28.0
lgtm otherwise
Thanks!
--
Eduardo
Signature · 1 line
Marc-André Lureau
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 32 lines · Marc-André Lureau
Quoted 30 lines · Eduardo Habkost
Quoted 1 line · Marc-André Lureau
On Tue, Nov 17, 2020 at 2:43 AM Eduardo Habkost <[email protected]>
Quoted 27 lines · wrote:
Quoted 27 lines
Provide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)include/qapi/qmp/qnum.h +1
-2@@ -46,20 +46,56 @@ typedef enum {* in range: qnum_get_try_int() / qnum_get_try_uint() check range and * convert under the hood. */ -struct QNum { -struct QObjectBase_ base; +
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
include/qapi/qmp/qnum.h typedef enum { · line 57
Quoted 8 lines
include/qapi/qmp/qnum.h +8
-0+/** +* 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 { + /* private: */
Do we care?
Quoted 61 lines
Quoted 61 lines
Quoted 20 lines
Quoted 20 lines
include/qapi/qmp/qnum.h +12
-0QNumKind 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; };Quoted 5 lines
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 3 lines
include/qapi/qmp/qnum.h +2
-0+/** +* qnum_from_int(): Create a new QNum from a QNumValue Copy-pasta
qnum_from_int()->qnum_from_value()Oops! I will fix it in v3, or submit a fixup patch if that's the only needed change.
Quoted 2 lines
I wonder if there is a check for that kind of mistake, as it may be
common.
It looks like kernel-doc ignores what's before the colon in the summary line. It probably shouldn't.
Quoted 24 lines
+ * @value: QNumValue
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 5 lines
include/qapi/qmp/qnum.h +4
-0+* +* Return strong reference. +*/ +QNum *qnum_from_value (QNumValue value);+
Quoted 4 lines
include/qapi/qmp/qnum.h +0
-0QNum *qnum_from_int (int64_t value);QNum *qnum_from_uint (uint64_t value);QNum *qnum_from_double (double value);include/qapi/qmp/qnum.h typedef enum { · line 101
Quoted 10 lines
include/qapi/qmp/qnum.h +9
-0+/** +* qnum_get_value(): Get QNumValue from QNum +* @qn: QNum object +*/ +static inline const QNumValue *qnum_get_value (const QNum *qn) +{ +return &qn->value; +} +Nothing uses that function in this patch. Should be put into use.
Quoted 6 lines
Quoted 3 lines
It is used in patch 5/8. Why do you think it's necessary to use it in internal code too?
Not necessarily, just want to make sure we don't introduce dead code. Fine it's used later, perhaps worth noting in the commit message.
Yes, use later in the same series suffices.
The whole patch makes sense only because later patches put QNumValue to use. The commit message could perhaps be more explicit about that. No need to be explicit about every new function, though.
That said, I wouldn't bother with a getter function. Yes, we have similar trivial getters for other QFoo. I don't care for them, either. The actual structure of these data types is trivial, and not worth hiding.
Also, I'm wary of inline functions in headers. *Especially* when they require additional #include (which this one doesn't). I accept them when they provide a worthwhile performance improvement. Guesses don't count as evidence :)
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
bool
qnum_get_try_int(const QNum *qn, int64_t *val);qobject/qnum.c line 19
Quoted 12 lines
include/qapi/qmp/qnum.h +0
-0int64_t qnum_get_int (const QNum *qn);qobject/qnum.c +1
-0@@ -15,6 +15,15 @@#include "qemu/osdep.h" #include "qapi/qmp/qnum.h" +QNum *qnum_from_value (QNumValue value)I wonder if it shouldn't be made "inline" in header too (if that can help avoid argument copy).
Quoted 5 lines
Quoted 3 lines
I'm unsure. I would make it inline (in a separate patch) if there's evidence it's worth it. I expect the
g_new()call to be more expensive than any argument copying, though.ok
I'm with Eduardo here.
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
+{
Quoted 14 lines
qobject/qnum.c +7
-0+QNum *qn =g_new (QNum,1 ); + +qobject_init (QOBJECT (qn),QTYPE_QNUM ); + qn->value = value; +return qn; +} +/** * qnum_from_int(): Create a new QNum from an int64_t * @value: int64_t value @@ -23,13 +32,7 @@*/ QNum *qnum_from_int (int64_t value) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
qobject/qnum.c line 35
Quoted 8 lines
qobject/qnum.c +1
-7-QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_I64 ; - qn->u.i64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_INT (value));
No space between between (type) and its operand, please.
Could we lift the cast into the macro somehow?
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
qobject/qnum.c +6
-17}/** @@ -40,13 +43,7 @@ QNum *qnum_from_int(int64_t value)*/ QNum *qnum_from_uint (uint64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_U64 ; - qn->u.u64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_UINT (value)); }/** @@ -57,13 +54,7 @@ QNum *qnum_from_uint(uint64_t value)*/ QNum *qnum_from_double (double value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_DOUBLE ; - qn->u.dbl = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_DOUBLE (value)); }/** @@ -75,15 +66,17 @@ QNum *qnum_from_double(double value)*/ bool qnum_get_try_int (const QNum *qn,int64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : -if (qn->u.u64 >INT64_MAX ) {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
qobject/qnum.c QNum *qnum_from_double(double value) · line 82
Quoted 8 lines
qobject/qnum.c +2
-1+if (qv->u.u64 >INT64_MAX ) {return false; } - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;
Here you add a new variable to shorten "qn->value." to "qv->". In tests/check-qnum.c you don't. I'm not sure the variable is worthwhile.
Quoted 51 lines
Quoted 51 lines
Quoted 50 lines
Quoted 43 lines
qobject/qnum.c +12
-8@@ -116,15 +109,17 @@ int64_t qnum_get_int(const QNum *qn)*/ bool qnum_get_try_uint (const QNum *qn,uint64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -if (qn->u.i64 <0 ) { +if (qv->u.i64 <0 ) {return false; } - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -156,13 +151,15 @@ uint64_t qnum_get_uint(const QNum *qn)*/ double qnum_get_double (const QNum *qn) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -return qn->u.i64; +return qv->u.i64;case QNUM_U64 : -return qn->u.u64; +return qv->u.u64;case QNUM_DOUBLE : -return qn->u.dbl; +return qv->u.dbl; }assert (0 );qobject/qnum.c double qnum_get_double(const QNum *qn) · line 172
Quoted 5 lines
qobject/qnum.c +1
-0@@ -171,14 +168,15 @@ double qnum_get_double(const QNum *qn)char *qnum_to_string (QNum *qn) { +const QNumValue *qv = &qn->value;
qnum_get_value()?
Quoted 2 lines
Quoted 2 lines
I prefer to not hide this behind a function call, in internal code. But I don't mind changing it if you think it's important.
Me too. Even in external code.
Quoted 28 lines
no, it's ok to me
Quoted 25 lines
Quoted 25 lines
char *buffer;Quoted 22 lines
qobject/qnum.c +4
-4int len; -switch (qn->kind) { +switch (qv->kind) {case QNUM_I64 : -return g_strdup_printf ("%" PRId64, qn->u.i64); +return g_strdup_printf ("%" PRId64, qv->u.i64);case QNUM_U64 : -return g_strdup_printf ("%" PRIu64, qn->u.u64); +return g_strdup_printf ("%" PRIu64, qv->u.u64);case QNUM_DOUBLE :/* FIXME: snprintf() is locale dependent; but JSON requires * numbers to be formatted as if in the C locale. Dependence @@ -189,7 +187,7 @@ char *qnum_to_string(QNum *qn)* rounding errors; we should be using DBL_DECIMAL_DIG (17), * and only rounding to a shorter number if the result would * still produce the same floating point value. */ - buffer =g_strdup_printf ("%f" , qn->u.dbl); + buffer =g_strdup_printf ("%f" , qv->u.dbl); len =strlen (buffer);while (len >0 && buffer[len -1 ] =='0' ) { len--;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
qobject/qnum.c char *qnum_to_string(QNum *qn) · line 223
Quoted 8 lines
qobject/qnum.c +2
-2@@ -221,8 +219,10 @@ 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 *qnum_x =qobject_to (QNum, x); +const QNum *qnum_y =qobject_to (QNum, y);
A bit of drive-by constification. Okay.
Quoted 64 lines
Quoted 64 lines
Quoted 49 lines
Quoted 49 lines
qobject/qnum.c +2
-0+const QNumValue *num_x = &qnum_x->value; +const QNumValue *num_y = &qnum_y->value;switch (num_x->kind) {case QNUM_I64 :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);Quoted 8 lines
Quoted 7 lines
tests/check-qnum.c +0
-1qobject_unref (qn); } --2.28.0
lgtm otherwise
Thanks!
--
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 36 lines · Markus Armbruster
Quoted 34 lines · Marc-André Lureau
Quoted 32 lines · Eduardo Habkost
Quoted 1 line · Marc-André Lureau
On Tue, Nov 17, 2020 at 2:43 AM Eduardo Habkost <[email protected]>
Quoted 29 lines · wrote:
Quoted 29 lines
Provide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)include/qapi/qmp/qnum.h +3
-2@@ -46,20 +46,56 @@ 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
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
include/qapi/qmp/qnum.h typedef enum { · line 57
Quoted 6 lines
include/qapi/qmp/qnum.h +6
-0+* +* QNumValue literals can be constructed using the `QNUM_VAL_INT`, +* `QNUM_VAL_UINT`, and `QNUM_VAL_DOUBLE` macros. +*/ +typedef struct QNumValue { + /* private: */Do we care?
Are you asking if we want to make them private, or if we want to document them as private (assuming they are).
The answer to the latter is yes ("private:" is an indication to kernel-doc). The answer to the former seems to be "no", based on your other comments.
Or maybe kind should be public and u should be private?
Quoted 139 lines
Quoted 62 lines
Quoted 62 lines
Quoted 21 lines
Quoted 21 lines
include/qapi/qmp/qnum.h +12
-0QNumKind 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; };Quoted 5 lines
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 3 lines
include/qapi/qmp/qnum.h +2
-0+/** +* qnum_from_int(): Create a new QNum from a QNumValue Copy-pasta
qnum_from_int()->qnum_from_value()Oops! I will fix it in v3, or submit a fixup patch if that's the only needed change.
Quoted 2 lines
I wonder if there is a check for that kind of mistake, as it may be
common.
It looks like kernel-doc ignores what's before the colon in the summary line. It probably shouldn't.
Quoted 24 lines
+ * @value: QNumValue
include/qapi/qmp/qnum.h typedef enum { · line 89
Quoted 5 lines
include/qapi/qmp/qnum.h +4
-0+* +* Return strong reference. +*/ +QNum *qnum_from_value (QNumValue value);+
Quoted 4 lines
include/qapi/qmp/qnum.h +0
-0QNum *qnum_from_int (int64_t value);QNum *qnum_from_uint (uint64_t value);QNum *qnum_from_double (double value);include/qapi/qmp/qnum.h typedef enum { · line 101
Quoted 10 lines
include/qapi/qmp/qnum.h +9
-0+/** +* qnum_get_value(): Get QNumValue from QNum +* @qn: QNum object +*/ +static inline const QNumValue *qnum_get_value (const QNum *qn) +{ +return &qn->value; +} +Nothing uses that function in this patch. Should be put into use.
Quoted 6 lines
Quoted 3 lines
It is used in patch 5/8. Why do you think it's necessary to use it in internal code too?
Not necessarily, just want to make sure we don't introduce dead code. Fine it's used later, perhaps worth noting in the commit message.
Yes, use later in the same series suffices.
The whole patch makes sense only because later patches put QNumValue to use. The commit message could perhaps be more explicit about that. No need to be explicit about every new function, though.
That said, I wouldn't bother with a getter function. Yes, we have similar trivial getters for other QFoo. I don't care for them, either. The actual structure of these data types is trivial, and not worth hiding.
Also, I'm wary of inline functions in headers. *Especially* when they require additional #include (which this one doesn't). I accept them when they provide a worthwhile performance improvement. Guesses don't count as evidence :)
Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
bool
qnum_get_try_int(const QNum *qn, int64_t *val);qobject/qnum.c line 19
Quoted 12 lines
include/qapi/qmp/qnum.h +0
-0int64_t qnum_get_int (const QNum *qn);qobject/qnum.c +1
-0@@ -15,6 +15,15 @@#include "qemu/osdep.h" #include "qapi/qmp/qnum.h" +QNum *qnum_from_value (QNumValue value)I wonder if it shouldn't be made "inline" in header too (if that can help avoid argument copy).
Quoted 5 lines
Quoted 3 lines
I'm unsure. I would make it inline (in a separate patch) if there's evidence it's worth it. I expect the
g_new()call to be more expensive than any argument copying, though.ok
I'm with Eduardo here.
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
+{
Quoted 14 lines
qobject/qnum.c +7
-0+QNum *qn =g_new (QNum,1 ); + +qobject_init (QOBJECT (qn),QTYPE_QNUM ); + qn->value = value; +return qn; +} +/** * qnum_from_int(): Create a new QNum from an int64_t * @value: int64_t value @@ -23,13 +32,7 @@*/ QNum *qnum_from_int (int64_t value) {Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
qobject/qnum.c line 35
Quoted 8 lines
qobject/qnum.c +1
-7-QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_I64 ; - qn->u.i64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_INT (value));No space between between (type) and its operand, please.
Quoted 1 line
Could we lift the cast into the macro somehow?
I think we can. I had thought the cast in the macro would break usage as static variable initializers. I was wrong.
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
Quoted 51 lines
qobject/qnum.c +7
-17}/** @@ -40,13 +43,7 @@ QNum *qnum_from_int(int64_t value)*/ QNum *qnum_from_uint (uint64_t value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_U64 ; - qn->u.u64 = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_UINT (value)); }/** @@ -57,13 +54,7 @@ QNum *qnum_from_uint(uint64_t value)*/ QNum *qnum_from_double (double value) { -QNum *qn =g_new (QNum,1 ); - -qobject_init (QOBJECT (qn),QTYPE_QNUM ); - qn->kind =QNUM_DOUBLE ; - qn->u.dbl = value; - -return qn; +return qnum_from_value ((QNumValue )QNUM_VAL_DOUBLE (value)); }/** @@ -75,15 +66,17 @@ QNum *qnum_from_double(double value)*/ bool qnum_get_try_int (const QNum *qn,int64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : -if (qn->u.u64 >INT64_MAX ) { +if (qv->u.u64 >INT64_MAX ) {return false; }
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
qobject/qnum.c QNum *qnum_from_double(double value) · line 82
Quoted 5 lines
qobject/qnum.c +1
-1- *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;Here you add a new variable to shorten "
qn->value." to "qv->". Intests/check-qnum.cyou don't. I'm not sure the variable is worthwhile.
The variable is worthwhile if it becomes a parameter to qnum_value_get_try_int().
If we don't create a qnum_value_get_try_int() function, I still think the variable makes the code easier to read, but I'm not too attached to it.
Quoted 52 lines
Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
Quoted 44 lines
qobject/qnum.c +12
-8@@ -116,15 +109,17 @@ int64_t qnum_get_int(const QNum *qn)*/ bool qnum_get_try_uint (const QNum *qn,uint64_t *val) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -if (qn->u.i64 <0 ) { +if (qv->u.i64 <0 ) {return false; } - *val = qn->u.i64; + *val = qv->u.i64;return true;case QNUM_U64 : - *val = qn->u.u64; + *val = qv->u.u64;return true;case QNUM_DOUBLE :return false;@@ -156,13 +151,15 @@ uint64_t qnum_get_uint(const QNum *qn)*/ double qnum_get_double (const QNum *qn) { -switch (qn->kind) { +const QNumValue *qv = &qn->value; + +switch (qv->kind) {case QNUM_I64 : -return qn->u.i64; +return qv->u.i64;case QNUM_U64 : -return qn->u.u64; +return qv->u.u64;case QNUM_DOUBLE : -return qn->u.dbl; +return qv->u.dbl; }assert (0 );qobject/qnum.c double qnum_get_double(const QNum *qn) · line 172
Quoted 5 lines
qobject/qnum.c +1
-0@@ -171,14 +168,15 @@ double qnum_get_double(const QNum *qn)char *qnum_to_string (QNum *qn) { +const QNumValue *qv = &qn->value;
qnum_get_value()?
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
I prefer to not hide this behind a function call, in internal code. But I don't mind changing it if you think it's important.
Me too. Even in external code.
Understood.
Quoted 31 lines
Quoted 31 lines
no, it's ok to me
Quoted 27 lines
Quoted 27 lines
char *buffer;Quoted 24 lines
qobject/qnum.c +4
-4int len; -switch (qn->kind) { +switch (qv->kind) {case QNUM_I64 : -return g_strdup_printf ("%" PRId64, qn->u.i64); +return g_strdup_printf ("%" PRId64, qv->u.i64);case QNUM_U64 : -return g_strdup_printf ("%" PRIu64, qn->u.u64); +return g_strdup_printf ("%" PRIu64, qv->u.u64);case QNUM_DOUBLE :/* FIXME: snprintf() is locale dependent; but JSON requires * numbers to be formatted as if in the C locale. Dependence @@ -189,7 +187,7 @@ char *qnum_to_string(QNum *qn)* rounding errors; we should be using DBL_DECIMAL_DIG (17), * and only rounding to a shorter number if the result would * still produce the same floating point value. */ - buffer =g_strdup_printf ("%f" , qn->u.dbl); + buffer =g_strdup_printf ("%f" , qv->u.dbl); len =strlen (buffer);while (len >0 && buffer[len -1 ] =='0' ) { len--;@@ -221,8 +219,10 @@ char *qnum_to_string(QNum *qn)*/
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
qobject/qnum.c char *qnum_to_string(QNum *qn) · line 223
Quoted 6 lines
qobject/qnum.c +2
-2bool 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 *qnum_x =qobject_to (QNum, x); +const QNum *qnum_y =qobject_to (QNum, y);A bit of drive-by constification. Okay.
In my defense, I will argue that those are four new variables. the variables QNum *num_x, *num_y don't exist anymore.
Anyway, I will probably keep the QNumValue *num_x, *num_y variables untouched in the next version, and choose another name for the new QNumValue variables (which will become qnum_value_is_equal() parameters in the next patch).
Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 50 lines
Quoted 50 lines
qobject/qnum.c +2
-0+const QNumValue *num_x = &qnum_x->value; +const QNumValue *num_y = &qnum_y->value;switch (num_x->kind) {case QNUM_I64 :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);Quoted 8 lines
Quoted 7 lines
tests/check-qnum.c +0
-1qobject_unref (qn); } --2.28.0
lgtm otherwise
Thanks!
--
Eduardo
Signature · 1 line
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 1 line · Eduardo Habkost
On Thu, Nov 19, 2020 at 11:24:52AM +0100, Markus Armbruster wrote:
[...]
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ return
qnum_from_value((QNumValue) QNUM_VAL_INT(value));No space between between (type) and its operand, please.
Could we lift the cast into the macro somehow?
I think we can. I had thought the cast in the macro would break usage as static variable initializers. I was wrong.
Actually, including the cast in the macro breaks QLIT_QDICT initializers (which use (QLitDictEntry[]) compound literals), and I don't know why.
Compound literals in initializers of static variables is a GCC extension. I don't understand why it doesn't work inside array compound literals, though.
Any language lawyers around?
This works:
typedef struct QLit {
int x, y;
} QLit;
typedef struct Entry {
int key;
QLit value;
} Entry;
Entry e = { .key = 0, .value = (QLit) { 1, 2 } };This works:
Entry *es1 = (Entry[]) {
{ .key = 0, .value = { 1, 2 } },
};But this doesn't:
Entry *es2 = (Entry[]) {
{ .key = 0, .value = (QLit) { 1, 2 } },
};dict.c:16:24: error: initializer element is not constant
16 | Entry *es2 = (Entry[]) {
| ^
dict.c:16:24: note: (near initialization for ‘es2’)
(gcc (GCC) 10.2.1 20201005 (Red Hat 10.2.1-5))
Signature · 1 line
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 47 lines · Eduardo Habkost
Quoted 1 line · Eduardo Habkost
On Thu, Nov 19, 2020 at 11:24:52AM +0100, Markus Armbruster wrote:
[...]
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ return
qnum_from_value((QNumValue) QNUM_VAL_INT(value));No space between between (type) and its operand, please.
Could we lift the cast into the macro somehow?
I think we can. I had thought the cast in the macro would break usage as static variable initializers. I was wrong.
Actually, including the cast in the macro breaks
QLIT_QDICTinitializers (which use (QLitDictEntry[]) compound literals), and I don't know why.Compound literals in initializers of static variables is a GCC extension. I don't understand why it doesn't work inside array compound literals, though.
Any language lawyers around?
This works:
typedef struct QLit { int x, y; } QLit; typedef struct Entry { int key; QLit value; } Entry; Entry e = { .key = 0, .value = (QLit) { 1, 2 } };This works:
Entry *es1 = (Entry[]) { { .key = 0, .value = { 1, 2 } }, };But this doesn't:
Entry *es2 = (Entry[]) { { .key = 0, .value = (QLit) { 1, 2 } }, };
Quoted 6 lines
dict.c:16:24: error: initializer element is not constant
16 | Entry *es2 = (Entry[]) {
| ^
dict.c:16:24: note: (near initialization for ‘es2’)(gcc (GCC) 10.2.1 20201005 (Red Hat 10.2.1-5))
Can't explain this offhand.
Another pecularity: a const QLitObject is for the most part not actually const. Evidence:
$ size bld-x86/libqemu-x86_64-softmmu.fa.p/meson-generated_.._qapi_qapi-introspect.c.o
text data bss dec hex filename
19590 351600 48 371238 5aa26 bld-x86/libqemu-x86_64-softmmu.fa.p/meson-generated_.._qapi_qapi-introspect.c.oScore 5 out of 100 points.
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
In reply to Eduardo HabkostQuoted 46 lines · Eduardo Habkost
Quoted 36 lines · Markus Armbruster
Quoted 34 lines · Marc-André Lureau
Quoted 32 lines · Eduardo Habkost
Quoted 1 line · Marc-André Lureau
On Tue, Nov 17, 2020 at 2:43 AM Eduardo Habkost <[email protected]>
Quoted 29 lines · wrote:
Quoted 29 lines
Provide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)include/qapi/qmp/qnum.h +3
-2@@ -46,20 +46,56 @@ 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 Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
include/qapi/qmp/qnum.h typedef enum { · line 57
Quoted 6 lines
include/qapi/qmp/qnum.h +6
-0+* +* QNumValue literals can be constructed using the `QNUM_VAL_INT`, +* `QNUM_VAL_UINT`, and `QNUM_VAL_DOUBLE` macros. +*/ +typedef struct QNumValue { + /* private: */Do we care?
Quoted 8 lines
Are you asking if we want to make them private, or if we want to document them as private (assuming they are).
The answer to the latter is yes ("private:" is an indication to kernel-doc). The answer to the former seems to be "no", based on your other comments.
Or maybe
kindshould be public andushould be private?
You're factoring out a part of struct QNum into new struct QNumValue. struct QNum is not private before the patch. I see no need to start making it or parts of it private now.
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
[...]
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 55 lines · Markus Armbruster
Quoted 53 lines · Eduardo Habkost
Quoted 36 lines · Markus Armbruster
Quoted 34 lines · Marc-André Lureau
Quoted 32 lines · Eduardo Habkost
Quoted 1 line · Marc-André Lureau
On Tue, Nov 17, 2020 at 2:43 AM Eduardo Habkost <[email protected]>
Quoted 29 lines · wrote:
Quoted 29 lines
Provide a separate QNumValue type that can be used for QNum value literals without the referencing counting and memory allocation features provided by QObject.
Signed-off-by Eduardo Habkost <[email protected]>---
Changes v1 -> v2:
* Fix "make check" failure, by updating check-qnum unit test to use the new struct fields--- include/qapi/qmp/qnum.h | 40 +++++++++++++++++++-- qobject/qnum.c | 78 ++++++++++++++++++++--------------------- tests/check-qnum.c | 14 ++++---- 3 files changed, 84 insertions(+), 48 deletions(-)include/qapi/qmp/qnum.h +3
-2@@ -46,20 +46,56 @@ 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 Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
include/qapi/qmp/qnum.h typedef enum { · line 57
Quoted 6 lines
include/qapi/qmp/qnum.h +6
-0+* +* QNumValue literals can be constructed using the `QNUM_VAL_INT`, +* `QNUM_VAL_UINT`, and `QNUM_VAL_DOUBLE` macros. +*/ +typedef struct QNumValue { + /* private: */Do we care?
Are you asking if we want to make them private, or if we want to document them as private (assuming they are).
The answer to the latter is yes ("private:" is an indication to kernel-doc). The answer to the former seems to be "no", based on your other comments.
Quoted 5 lines
Quoted 1 line
Or maybe
kindshould be public andushould be private?You're factoring out a part of struct QNum into new struct QNumValue. struct QNum is not private before the patch. I see no need to start making it or parts of it private now.
I don't want to change the rules, just to document the existing implicit rules. If you say QNum.u was never private, I won't argue.
Quoted 4 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Signature · 1 line
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 11 lines · Markus Armbruster
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Quoted 6 lines
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
I don't know what we are talking about anymore. None of this applies to the QNum API, right?
QNum/QNumValue are not complex data structures, and the reason we need the compiler to know the size of QNumValue is not related to performance at all.
We might still want to discourage users of the QNum API from accessing QNum.u/QNumValue.u directly. Documenting the field as private is a very easy way to do it.
Signature · 1 line
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 19 lines · Eduardo Habkost
Quoted 11 lines · Markus Armbruster
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Quoted 6 lines
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
Quoted 6 lines
I don't know what we are talking about anymore. None of this applies to the QNum API, right?
QNum/QNumValue are not complex data structures, and the reason we need the compiler to know the size of QNumValue is not related to performance at all.
We started with the question whether to make QNumValue's members private. We digressed to the question when to make members private. So back to the original question.
Quoted 3 lines
We might still want to discourage users of the QNum API from accessing QNum.u/QNumValue.u directly. Documenting the field as private is a very easy way to do it.
It's a complete non-issue. QNum has been around for years, and we haven't had any issues that could've been plausibly avoided by asking people to refrain from accessing its members.
If there was an actual need to keep the members private, I'd move the struct out of the header, so the compiler enforces privacy.
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 36 lines · Markus Armbruster
Quoted 19 lines · Eduardo Habkost
Quoted 11 lines · Markus Armbruster
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Quoted 6 lines
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
Quoted 6 lines
I don't know what we are talking about anymore. None of this applies to the QNum API, right?
QNum/QNumValue are not complex data structures, and the reason we need the compiler to know the size of QNumValue is not related to performance at all.
We started with the question whether to make QNumValue's members private. We digressed to the question when to make members private. So back to the original question.
Quoted 3 lines
We might still want to discourage users of the QNum API from accessing QNum.u/QNumValue.u directly. Documenting the field as private is a very easy way to do it.
Quoted 6 lines
It's a complete non-issue. QNum has been around for years, and we haven't had any issues that could've been plausibly avoided by asking people to refrain from accessing its members.
If there was an actual need to keep the members private, I'd move the struct out of the header, so the compiler enforces privacy.
Understood. There's still a question I'd like to answer, to decide how the API documentation should look like:
Is QNum.u/QNumValue.u required to be part of the API documentation?
If accessing that field directly is not necessary for using the API, I don't think it should appear in the documentation (because it would be just noise).
Signature · 1 line
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 47 lines · Eduardo Habkost
Quoted 36 lines · Markus Armbruster
Quoted 19 lines · Eduardo Habkost
Quoted 11 lines · Markus Armbruster
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Quoted 6 lines
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
Quoted 6 lines
I don't know what we are talking about anymore. None of this applies to the QNum API, right?
QNum/QNumValue are not complex data structures, and the reason we need the compiler to know the size of QNumValue is not related to performance at all.
We started with the question whether to make QNumValue's members private. We digressed to the question when to make members private. So back to the original question.
Quoted 3 lines
We might still want to discourage users of the QNum API from accessing QNum.u/QNumValue.u directly. Documenting the field as private is a very easy way to do it.
Quoted 6 lines
It's a complete non-issue. QNum has been around for years, and we haven't had any issues that could've been plausibly avoided by asking people to refrain from accessing its members.
If there was an actual need to keep the members private, I'd move the struct out of the header, so the compiler enforces privacy.
Understood. There's still a question I'd like to answer, to decide how the API documentation should look like:
Quoted 6 lines
Is QNum.u/QNumValue.u required to be part of the API documentation?
If accessing that field directly is not necessary for using the API, I don't think it should appear in the documentation (because it would be just noise).
The current patch's comment on QNumValue looks good to me.
Does this answer your question?
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 52 lines · Markus Armbruster
Quoted 50 lines · Eduardo Habkost
Quoted 36 lines · Markus Armbruster
Quoted 19 lines · Eduardo Habkost
Quoted 11 lines · Markus Armbruster
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Quoted 6 lines
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
Quoted 6 lines
I don't know what we are talking about anymore. None of this applies to the QNum API, right?
QNum/QNumValue are not complex data structures, and the reason we need the compiler to know the size of QNumValue is not related to performance at all.
We started with the question whether to make QNumValue's members private. We digressed to the question when to make members private. So back to the original question.
Quoted 3 lines
We might still want to discourage users of the QNum API from accessing QNum.u/QNumValue.u directly. Documenting the field as private is a very easy way to do it.
Quoted 6 lines
It's a complete non-issue. QNum has been around for years, and we haven't had any issues that could've been plausibly avoided by asking people to refrain from accessing its members.
If there was an actual need to keep the members private, I'd move the struct out of the header, so the compiler enforces privacy.
Understood. There's still a question I'd like to answer, to decide how the API documentation should look like:
Is QNum.u/QNumValue.u required to be part of the API documentation?
Quoted 7 lines
Quoted 3 lines
If accessing that field directly is not necessary for using the API, I don't think it should appear in the documentation (because it would be just noise).
The current patch's comment on QNumValue looks good to me.
Does this answer your question?
The current patch (v3) doesn't address the question. It doesn't include documentation for the field, but doesn't hide it. kernel-doc will print a warning on that case.
Signature · 1 line
Eduardo
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 57 lines · Eduardo Habkost
Quoted 56 lines · Markus Armbruster
Quoted 53 lines · Eduardo Habkost
Quoted 36 lines · Markus Armbruster
Quoted 19 lines · Eduardo Habkost
Quoted 11 lines · Markus Armbruster
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Quoted 6 lines
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
Quoted 6 lines
I don't know what we are talking about anymore. None of this applies to the QNum API, right?
QNum/QNumValue are not complex data structures, and the reason we need the compiler to know the size of QNumValue is not related to performance at all.
We started with the question whether to make QNumValue's members private. We digressed to the question when to make members private. So back to the original question.
Quoted 3 lines
We might still want to discourage users of the QNum API from accessing QNum.u/QNumValue.u directly. Documenting the field as private is a very easy way to do it.
Quoted 6 lines
It's a complete non-issue. QNum has been around for years, and we haven't had any issues that could've been plausibly avoided by asking people to refrain from accessing its members.
If there was an actual need to keep the members private, I'd move the struct out of the header, so the compiler enforces privacy.
Understood. There's still a question I'd like to answer, to decide how the API documentation should look like:
Is QNum.u/QNumValue.u required to be part of the API documentation?
If accessing that field directly is not necessary for using the API, I don't think it should appear in the documentation (because it would be just noise).
Quoted 7 lines
Quoted 3 lines
The current patch's comment on QNumValue looks good to me.
Does this answer your question?
The current patch (v3) doesn't address the question. It doesn't include documentation for the field, but doesn't hide it. kernel-doc will print a warning on that case.
Do we care? How many such warnings exist before the patch? Does this series add just this one, or more?
Use your judgement, then be ready to explain it :)
Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals
Quoted 61 lines · Markus Armbruster
Quoted 59 lines · Eduardo Habkost
Quoted 58 lines · Markus Armbruster
Quoted 47 lines · Eduardo Habkost
Quoted 36 lines · Markus Armbruster
Quoted 19 lines · Eduardo Habkost
Quoted 11 lines · Markus Armbruster
Quoted 1 line · Eduardo Habkost
On Fri, Nov 20, 2020 at 06:29:16AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
Quoted 3 lines
When the structure of a data type is to be kept away from its users, I prefer to keep it out of the public header, so the compiler enforces the encapsulation.
I prefer that too, except that it is impossible when users of the API need the compiler to know the struct size.
Quoted 6 lines
There are cases where the structure of a data type should be encapsulated, yet its size must be made known for performance (avoid dynamic memory allocation and pointer chasing).
Need for encapsulation correlates with complex algorithms and data structures. The cost of dynamic allocation is often in the noise then.
Quoted 6 lines
I don't know what we are talking about anymore. None of this applies to the QNum API, right?
QNum/QNumValue are not complex data structures, and the reason we need the compiler to know the size of QNumValue is not related to performance at all.
We started with the question whether to make QNumValue's members private. We digressed to the question when to make members private. So back to the original question.
Quoted 3 lines
We might still want to discourage users of the QNum API from accessing QNum.u/QNumValue.u directly. Documenting the field as private is a very easy way to do it.
Quoted 6 lines
It's a complete non-issue. QNum has been around for years, and we haven't had any issues that could've been plausibly avoided by asking people to refrain from accessing its members.
If there was an actual need to keep the members private, I'd move the struct out of the header, so the compiler enforces privacy.
Understood. There's still a question I'd like to answer, to decide how the API documentation should look like:
Quoted 6 lines
Is QNum.u/QNumValue.u required to be part of the API documentation?
If accessing that field directly is not necessary for using the API, I don't think it should appear in the documentation (because it would be just noise).
The current patch's comment on QNumValue looks good to me.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Does this answer your question?
The current patch (v3) doesn't address the question. It doesn't include documentation for the field, but doesn't hide it. kernel-doc will print a warning on that case.
Do we care?
Yes. Peter will reject pull requests if it generates kernel-doc warnings.
Quoted 1 line
How many such warnings exist before the patch?
Zero.
Quoted 1 line
Does this series add just this one, or more?
The current series (v3) doesn't add any, because I dropped the patch that added QObject and QNum documentation to docs/devel. I still want to resubmit that patch later, though.
Quoted 2 lines
Use your judgement, then be ready to explain it :)
OK!
Signature · 1 line
Eduardo
PATCH v2 4/8 qnum: qnum_value_is_equal() function
In reply to Eduardo HabkostExtract the QNum value comparison logic to a function that takes QNumValue* as argument.
--- include/qapi/qmp/qnum.h | 1 + qobject/qnum.c | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-)
include/qapi/qmp/qnum.h +1 -0
@@ -106,6 +106,7 @@ double qnum_get_double(const QNum *qn);char *qnum_to_string (QNum *qn); +bool qnum_value_is_equal (const QNumValue *num_x,const QNumValue *num_y);bool qnum_is_equal (const QObject *x,const QObject *y);void qnum_destroy_obj (QObject *obj);
qobject/qnum.c +19 -10
@@ -207,9 +207,9 @@ char *qnum_to_string(QNum *qn) }/** -* qnum_is_equal(): Test whether the two QNums are equal -* @x: QNum object -* @y: QNum object +* qnum_value_is_equal(): Test whether two QNumValues are equal +* @num_x: QNum value +* @num_y: QNum value * * Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered
@@ -217,13 +217,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 *num_x,const QNumValue *num_y) { -const QNum *qnum_x =qobject_to (QNum, x); -const QNum *qnum_y =qobject_to (QNum, y); -const QNumValue *num_x = &qnum_x->value; -const QNumValue *num_y = &qnum_y->value; -switch (num_x->kind) {case QNUM_I64 :switch (num_y->kind) {
@@ -241,7 +236,7 @@ bool qnum_is_equal(const QObject *x, const QObject *y)case QNUM_U64 :switch (num_y->kind) {case QNUM_I64 : -return qnum_is_equal (y, x); +return qnum_value_is_equal (num_y, num_x);case QNUM_U64 :/* Comparison in native uint64_t type */ return num_x->u.u64 == num_y->u.u64;
@@ -264,6 +259,20 @@ bool qnum_is_equal(const QObject *x, const QObject *y)abort (); } +/** +* qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object +* +* 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
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
Quoted 26 lines · Eduardo Habkost
Extract the QNum value comparison logic to a function that takes QNumValue* as argument.
Signed-off-by Eduardo Habkost <[email protected]>--- include/qapi/qmp/qnum.h | 1 + qobject/qnum.c | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-)include/qapi/qmp/qnum.h +1
-0@@ -106,6 +106,7 @@ double qnum_get_double(const QNum *qn);char *qnum_to_string (QNum *qn); +bool qnum_value_is_equal (const QNumValue *num_x,const QNumValue *num_y);bool qnum_is_equal (const QObject *x,const QObject *y);void qnum_destroy_obj (QObject *obj);qobject/qnum.c +0
-0
qobject/qnum.c char *qnum_to_string(QNum *qn) · line 213
Quoted 10 lines
qobject/qnum.c +3
-3@@ -207,9 +207,9 @@ char *qnum_to_string(QNum *qn) }/** -* qnum_is_equal(): Test whether the two QNums are equal -* @x: QNum object -* @y: QNum object +* qnum_value_is_equal(): Test whether two QNumValues are equal +* @num_x: QNum value +* @num_y: QNum value
val_x: a QNumValue ?
*
Quoted 48 lines
qobject/qnum.c +16
-8* Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered @@ -217,13 +217,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 *num_x,const QNumValue *num_y) { -const QNum *qnum_x =qobject_to (QNum, x); -const QNum *qnum_y =qobject_to (QNum, y); -const QNumValue *num_x = &qnum_x->value; -const QNumValue *num_y = &qnum_y->value; -switch (num_x->kind) {case QNUM_I64 :switch (num_y->kind) {@@ -241,7 +236,7 @@ bool qnum_is_equal(const QObject *x, const QObject *y)case QNUM_U64 :switch (num_y->kind) {case QNUM_I64 : -return qnum_is_equal (y, x); +return qnum_value_is_equal (num_y, num_x);case QNUM_U64 :/* Comparison in native uint64_t type */ return num_x->u.u64 == num_y->u.u64;@@ -264,6 +259,20 @@ bool qnum_is_equal(const QObject *x, const QObject *y)abort (); } +/** +* qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object +* +* 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 * --
Quoted 4 lines
2.28.0
beside that,
Signature · 1 line
Marc-André Lureau
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
Quoted 30 lines · Marc-André Lureau
Quoted 28 lines · Eduardo Habkost
Extract the QNum value comparison logic to a function that takes QNumValue* as argument.
Signed-off-by Eduardo Habkost <[email protected]>--- include/qapi/qmp/qnum.h | 1 + qobject/qnum.c | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-)include/qapi/qmp/qnum.h +1
-0@@ -106,6 +106,7 @@ double qnum_get_double(const QNum *qn);char *qnum_to_string (QNum *qn); +bool qnum_value_is_equal (const QNumValue *num_x,const QNumValue *num_y);bool qnum_is_equal (const QObject *x,const QObject *y);void qnum_destroy_obj (QObject *obj);qobject/qnum.c +0
-0@@ -207,9 +207,9 @@ char *qnum_to_string(QNum *qn) }
Quoted 10 lines
qobject/qnum.c char *qnum_to_string(QNum *qn)
Quoted 8 lines
qobject/qnum.c +3
-3/** -* qnum_is_equal(): Test whether the two QNums are equal -* @x: QNum object -* @y: QNum object +* qnum_value_is_equal(): Test whether two QNumValues are equal +* @num_x: QNum value +* @num_y: QNum value
val_x: a QNumValue ?
Do you mean:
@num_x: a QNumValue
?
I was not planning to rename the existing num_x/num_y variables.
Quoted 55 lines
*
Quoted 48 lines
qobject/qnum.c +16
-8* Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered @@ -217,13 +217,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 *num_x,const QNumValue *num_y) { -const QNum *qnum_x =qobject_to (QNum, x); -const QNum *qnum_y =qobject_to (QNum, y); -const QNumValue *num_x = &qnum_x->value; -const QNumValue *num_y = &qnum_y->value; -switch (num_x->kind) {case QNUM_I64 :switch (num_y->kind) {@@ -241,7 +236,7 @@ bool qnum_is_equal(const QObject *x, const QObject *y)case QNUM_U64 :switch (num_y->kind) {case QNUM_I64 : -return qnum_is_equal (y, x); +return qnum_value_is_equal (num_y, num_x);case QNUM_U64 :/* Comparison in native uint64_t type */ return num_x->u.u64 == num_y->u.u64;@@ -264,6 +259,20 @@ bool qnum_is_equal(const QObject *x, const QObject *y)abort (); } +/** +* qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object +* +* 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 * -- Quoted 4 lines
2.28.0
beside that,
Quoted 1 line
Reviewed-by Marc-André Lureau <[email protected]>
Thanks!
Signature · 1 line
Eduardo
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
Quoted 44 lines · Eduardo Habkost
Quoted 1 line · Marc-André Lureau
On Tue, Nov 17, 2020 at 2:42 AM Eduardo Habkost <[email protected]>
Quoted 16 lines · wrote:
Quoted 16 lines
Extract the QNum value comparison logic to a function that takes QNumValue* as argument.
Signed-off-by Eduardo Habkost <[email protected]>--- include/qapi/qmp/qnum.h | 1 + qobject/qnum.c | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-)include/qapi/qmp/qnum.h +0
-0@@ -106,6 +106,7 @@ double qnum_get_double(const QNum *qn);Quoted 3 lines
include/qapi/qmp/qnum.h double qnum_get_double(const QNum *qn);
Quoted 3 lines
include/qapi/qmp/qnum.h +1
-0char *qnum_to_string (QNum *qn); +bool qnum_value_is_equal(const QNumValue *num_x, const QNumValue
*num_y);Quoted 10 lines
Quoted 10 lines
include/qapi/qmp/qnum.h +0
-0bool qnum_is_equal (const QObject *x,const QObject *y);void qnum_destroy_obj (QObject *obj);qobject/qnum.c +0
-0@@ -207,9 +207,9 @@ char *qnum_to_string(QNum *qn) }Quoted 10 lines
qobject/qnum.c char *qnum_to_string(QNum *qn)
Quoted 8 lines
qobject/qnum.c +3
-3/** -* qnum_is_equal(): Test whether the two QNums are equal -* @x: QNum object -* @y: QNum object +* qnum_value_is_equal(): Test whether two QNumValues are equal +* @num_x: QNum value +* @num_y: QNum value
val_x: a QNumValue ?
Quoted 3 lines
Do you mean:
@num_x: a QNumValue
?
Quoted 4 lines
I was not planning to rename the existing num_x/num_y variables.
Not renaming because that would make some churn? But this is already quite confusing, so it's better to use "val" for QNumVal and "num" for QNum I guess.
If you don't want to rename it in the code, may I suggest doing it at the declaration side? Not sure it's a better idea.
Quoted 66 lines
Quoted 10 lines
*
qobject/qnum.c char *qnum_to_string(QNum *qn) · line 220
Quoted 8 lines
qobject/qnum.c +1
-1* Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered @@ -217,13 +217,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 *num_x, const QNumValue
*num_y)Quoted 10 lines
qobject/qnum.c bool qnum_is_equal(const QObject *x, const QObject
Quoted 10 lines
qobject/qnum.c +0
-5{ -const QNum *qnum_x =qobject_to (QNum, x); -const QNum *qnum_y =qobject_to (QNum, y); -const QNumValue *num_x = &qnum_x->value; -const QNumValue *num_y = &qnum_y->value; -switch (num_x->kind) {case QNUM_I64 :switch (num_y->kind) {@@ -241,7 +236,7 @@ bool qnum_is_equal(const QObject *x, const QObject*y)
Quoted 9 lines
qobject/qnum.c bool qnum_is_equal(const QObject *x, const
Quoted 9 lines
qobject/qnum.c +1
-1case QNUM_U64 :switch (num_y->kind) {case QNUM_I64 : -return qnum_is_equal (y, x); +return qnum_value_is_equal (num_y, num_x);case QNUM_U64 :/* Comparison in native uint64_t type */ return num_x->u.u64 == num_y->u.u64;@@ -264,6 +259,20 @@ bool qnum_is_equal(const QObject *x, constQObject *y)
Quoted 26 lines
Quoted 21 lines
qobject/qnum.c +14
-1abort (); } +/** +* qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object +* +* 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 * -- Quoted 4 lines
2.28.0
beside that,
Quoted 1 line
Reviewed-by Marc-André Lureau <[email protected]>Thanks!
--
Eduardo
Signature · 1 line
Marc-André Lureau
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
Quoted 54 lines · Marc-André Lureau
Quoted 44 lines · Eduardo Habkost
Quoted 1 line · Marc-André Lureau
On Tue, Nov 17, 2020 at 2:42 AM Eduardo Habkost <[email protected]>
Quoted 16 lines · wrote:
Quoted 16 lines
Extract the QNum value comparison logic to a function that takes QNumValue* as argument.
Signed-off-by Eduardo Habkost <[email protected]>--- include/qapi/qmp/qnum.h | 1 + qobject/qnum.c | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-)include/qapi/qmp/qnum.h +0
-0@@ -106,6 +106,7 @@ double qnum_get_double(const QNum *qn);Quoted 3 lines
include/qapi/qmp/qnum.h double qnum_get_double(const QNum *qn);
Quoted 3 lines
include/qapi/qmp/qnum.h +1
-0char *qnum_to_string (QNum *qn); +bool qnum_value_is_equal(const QNumValue *num_x, const QNumValue
*num_y);Quoted 10 lines
Quoted 10 lines
include/qapi/qmp/qnum.h +0
-0bool qnum_is_equal (const QObject *x,const QObject *y);void qnum_destroy_obj (QObject *obj);qobject/qnum.c +0
-0@@ -207,9 +207,9 @@ char *qnum_to_string(QNum *qn) }Quoted 10 lines
qobject/qnum.c char *qnum_to_string(QNum *qn)
Quoted 8 lines
qobject/qnum.c +3
-3/** -* qnum_is_equal(): Test whether the two QNums are equal -* @x: QNum object -* @y: QNum object +* qnum_value_is_equal(): Test whether two QNumValues are equal +* @num_x: QNum value +* @num_y: QNum value
val_x: a QNumValue ?Quoted 3 lines
Do you mean:
@num_x: a QNumValue?
Quoted 4 lines
I was not planning to rename the existing num_x/num_y variables.
Quoted 6 lines
Not renaming because that would make some churn? But this is already quite confusing, so it's better to use "val" for QNumVal and "num" for QNum I guess.
If you don't want to rename it in the code, may I suggest doing it at the declaration side? Not sure it's a better idea.
Yeah, I was not renaming them just to avoid churn.
However, I'm already replacing qn variables with qv at patch 3/8. Replacing num_x/num_y with val_x/val_y at qnum_is_equal() (at patch 3/8 as well) wouldn't hurt too much.
Signature · 1 line
Eduardo
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
In reply to Eduardo HabkostQuoted 67 lines · Eduardo Habkost
Extract the QNum value comparison logic to a function that takes QNumValue* as argument.
Signed-off-by Eduardo Habkost <[email protected]>--- include/qapi/qmp/qnum.h | 1 + qobject/qnum.c | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-)include/qapi/qmp/qnum.h +1
-0@@ -106,6 +106,7 @@ double qnum_get_double(const QNum *qn);char *qnum_to_string (QNum *qn); +bool qnum_value_is_equal (const QNumValue *num_x,const QNumValue *num_y);bool qnum_is_equal (const QObject *x,const QObject *y);void qnum_destroy_obj (QObject *obj);qobject/qnum.c +6
-10@@ -207,9 +207,9 @@ char *qnum_to_string(QNum *qn) }/** -* qnum_is_equal(): Test whether the two QNums are equal -* @x: QNum object -* @y: QNum object +* qnum_value_is_equal(): Test whether two QNumValues are equal +* @num_x: QNum value +* @num_y: QNum value * * Negative integers are never considered equal to unsigned integers, * but positive integers in the range [0, INT64_MAX] are considered @@ -217,13 +217,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 *num_x,const QNumValue *num_y) { -const QNum *qnum_x =qobject_to (QNum, x); -const QNum *qnum_y =qobject_to (QNum, y); -const QNumValue *num_x = &qnum_x->value; -const QNumValue *num_y = &qnum_y->value; -switch (num_x->kind) {case QNUM_I64 :switch (num_y->kind) {@@ -241,7 +236,7 @@ bool qnum_is_equal(const QObject *x, const QObject *y)case QNUM_U64 :switch (num_y->kind) {case QNUM_I64 : -return qnum_is_equal (y, x); +return qnum_value_is_equal (num_y, num_x);case QNUM_U64 :/* Comparison in native uint64_t type */ return num_x->u.u64 == num_y->u.u64;@@ -264,6 +259,20 @@ bool qnum_is_equal(const QObject *x, const QObject *y)abort (); } +/**
qobject/qnum.c bool qnum_is_equal(const QObject *x, const QObject *y) · line 272
Quoted 10 lines
qobject/qnum.c +10
-0+* qnum_is_equal(): Test whether the two QNums are equal +* @x: QNum object +* @y: QNum object +* +* 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);
Humor me: blank line between declarations and statements, please.
Quoted 6 lines
qobject/qnum.c +3
-0+return qnum_value_is_equal (&qnum_x->value, &qnum_y->value); +} +/** * qnum_destroy_obj(): Free all memory allocated by a QNum object *
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
On Thu, Nov 19, 2020 at 11:27:40AM +0100, Markus Armbruster wrote:
[...]
Quoted 6 lines
qobject/qnum.c bool qnum_is_equal(const QObject *x, const QObject *y) · line 272
Quoted 4 lines
qobject/qnum.c +4
-0+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);Humor me: blank line between declarations and statements, please.
I can do it. But why do you prefer it that way?
Quoted 7 lines
Quoted 7 lines
qobject/qnum.c +3
-0+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
Eduardo
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
Quoted 10 lines · Eduardo Habkost
On Thu, Nov 19, 2020 at 11:27:40AM +0100, Markus Armbruster wrote:
[...]Quoted 6 lines
qobject/qnum.c bool qnum_is_equal(const QObject *x, const QObject *y) · line 272
Quoted 4 lines
qobject/qnum.c +4
-0+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);Humor me: blank line between declarations and statements, please.
I can do it. But why do you prefer it that way?
Habit borne out of C lacking other visual cues to distinguish declarations and statements.
Declaration or statement? Tell me quick, don't analyze!
mumble(*mutter)();
This "obviously" declares @mutter as pointer to function returning mumble.
Except when @mumble isn't a typedef name, but a function taking one argument and returning a function that takes no argument. Then it passes *mutter to mumble(), and calls its return value.
The whole point of coding style is to help readers along. Two stylistic conventions that can help here:
1. In a function call, no space between the expression denoting the called function and the (parenthesized) argument list. Elsewhere, space.
So, when the example above is indeed a declaration, write it as
mumble (*mutter)();
If it's function calls, write it as
mumble(*mutter)();
2. Separate declarations from statements with a blank line. Do not mix them.
[...]
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Eduardo Habkost
On Thu, Nov 19, 2020 at 11:27:40AM +0100, Markus Armbruster wrote:
[...]Quoted 6 lines
qobject/qnum.c bool qnum_is_equal(const QObject *x, const QObject *y) · line 272
Quoted 4 lines
qobject/qnum.c +4
-0+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);Humor me: blank line between declarations and statements, please.
Quoted 4 lines
Quoted 1 line
I can do it. But why do you prefer it that way?
Habit borne out of C lacking other visual cues to distinguish declarations and statements.
Why is the distinction important, when many variable declarations also include initializer expressions that can be as complex as other statements?
(The qobject_to() calls above are an example).
Quoted 19 lines
Declaration or statement? Tell me quick, don't analyze!
mumble(*mutter)();This "obviously" declares @mutter as pointer to function returning mumble.
Except when @mumble isn't a typedef name, but a function taking one argument and returning a function that takes no argument. Then it passes *mutter to
mumble(), and calls its return value.The whole point of coding style is to help readers along. Two stylistic conventions that can help here:
1. In a function call, no space between the expression denoting the called function and the (parenthesized) argument list. Elsewhere, space.
Quoted 7 lines
So, when the example above is indeed a declaration, write it as
mumble (*mutter)();If it's function calls, write it as
mumble(*mutter)();
This makes lots of sense. Starting with a word followed by space is what makes declarations visually distinguishable.
Quoted 3 lines
2. Separate declarations from statements with a blank line. Do not mix them.
I'm not sure about this one, and I'm actually glad it is not part of CODING_STYLE. :)
(I'll still follow your advice as maintainer of that piece of code, of course)
Signature · 1 line
Eduardo
Re: [PATCH v2 4/8] qnum: qnum_value_is_equal() function
Quoted 14 lines · Eduardo Habkost
Quoted 13 lines · Markus Armbruster
Quoted 10 lines · Eduardo Habkost
On Thu, Nov 19, 2020 at 11:27:40AM +0100, Markus Armbruster wrote:
[...]Quoted 6 lines
qobject/qnum.c bool qnum_is_equal(const QObject *x, const QObject *y) · line 272
Quoted 4 lines
qobject/qnum.c +4
-0+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);Humor me: blank line between declarations and statements, please.
I can do it. But why do you prefer it that way?
Quoted 8 lines
Quoted 2 lines
Habit borne out of C lacking other visual cues to distinguish declarations and statements.
Why is the distinction important, when many variable declarations also include initializer expressions that can be as complex as other statements?
(The
qobject_to()calls above are an example).
We read left to right, and we're not good at backtracking. The earlier I know I'm reading a declaration, the better.
Quoted 20 lines
Quoted 20 lines
Declaration or statement? Tell me quick, don't analyze!
mumble(*mutter)();This "obviously" declares @mutter as pointer to function returning mumble.
Except when @mumble isn't a typedef name, but a function taking one argument and returning a function that takes no argument. Then it passes *mutter to
mumble(), and calls its return value.The whole point of coding style is to help readers along. Two stylistic conventions that can help here:
1. In a function call, no space between the expression denoting the called function and the (parenthesized) argument list. Elsewhere, space.
So, when the example above is indeed a declaration, write it as
Quoted 8 lines
Quoted 5 lines
mumble (*mutter)();If it's function calls, write it as
mumble(*mutter)();This makes lots of sense. Starting with a word followed by space is what makes declarations visually distinguishable.
Declarations need not match that pattern. Also, it's a rather subtle cue.
Quoted 5 lines
Quoted 2 lines
2. Separate declarations from statements with a blank line. Do not mix them.
I'm not sure about this one, and I'm actually glad it is not part of
CODING_STYLE. :)
That's why I ask to "humor me" :)
For what it's worth, the convention is common enough to be supported by the traditional BSD indent program.
Quoted 2 lines
(I'll still follow your advice as maintainer of that piece of code, of course)
Thanks!
PATCH v2 5/8 qlit: Support all types of QNums
In reply to Eduardo HabkostUse QNumValue to represent QNums, so we can also support uint64_t and double QNum values. Add new QLIT_QNUM_(INT|UINT|DOUBLE) macros for each case.
The QLIT_QNUM() macro is being kept for compatibility with existing code, but becomes just a wrapper for QLIT_QNUM_INT().
---
Changes v1 -> v2:
* Coding style fix at qlit_equal_qobject()
--- include/qapi/qmp/qlit.h | 11 +++++-- qobject/qlit.c | 5 +-- tests/check-qjson.c | 72 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 9 deletions(-)
include/qapi/qmp/qlit.h +9 -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;
@@ -39,8 +40,14 @@ struct QLitDictEntry { { .type = QTYPE_QNULL }#define QLIT_QBOOL (val) \ { .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_QNUM (val) \ - { .type = QTYPE_QNUM, .value.qnum = (val) } + QLIT_QNUM_INT(val)#define QLIT_QSTR (val) \ { .type = QTYPE_QSTRING, .value.qstr = (val) }#define QLIT_QDICT (val) \
qobject/qlit.c +3 -2
@@ -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 (&lhs->value.qnum, +qnum_get_value (qobject_to (QNum, rhs)));case QTYPE_QSTRING :return (strcmp (lhs->value.qstr,qstring_get_str (qobject_to (QString, rhs))) ==0 );
@@ -94,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 : {
tests/check-qjson.c +67 -5
108 lines not on this page · raw message
Signature · 1 line
2.28.0
Re: [PATCH v2 5/8] qlit: Support all types of QNums
Quoted 7 lines · Eduardo Habkost
Use QNumValue to represent QNums, so we can also support
uint64_tand double QNum values. Add newQLIT_QNUM_(INT|UINT|DOUBLE)macros for each case.The
QLIT_QNUM()macro is being kept for compatibility with existing code, but becomes just a wrapper forQLIT_QNUM_INT().
I am not sure it's worth to keep. (furthermore, it's only used in tests afaics)
Quoted 2 lines
Signed-off-by Eduardo Habkost <[email protected]>
---
Quoted 190 lines
Changes v1 -> v2:
* Coding style fix atqlit_equal_qobject()--- include/qapi/qmp/qlit.h | 11 +++++-- qobject/qlit.c | 5 +-- tests/check-qjson.c | 72 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 9 deletions(-)include/qapi/qmp/qlit.h +9
-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;@@ -39,8 +40,14 @@ struct QLitDictEntry { { .type = QTYPE_QNULL }#define QLIT_QBOOL (val) \ { .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_QNUM (val) \ - { .type = QTYPE_QNUM, .value.qnum = (val) } + QLIT_QNUM_INT(val)#define QLIT_QSTR (val) \ { .type = QTYPE_QSTRING, .value.qstr = (val) }#define QLIT_QDICT (val) \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 (&lhs->value.qnum, +qnum_get_value (qobject_to (QNum, rhs)));case QTYPE_QSTRING : return (strcmp(lhs->value.qstr,qstring_get_str(qobject_to(QString, rhs))) == 0);qobject/qlit.c +1
-1@@ -94,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 : {tests/check-qjson.c +67
-6@@ -796,20 +796,23 @@ static void simple_number(void)int i;struct {const char *encoded; +QLitObject qlit;int64_t decoded;int skip; } test_cases[] = { - {"0" ,0 }, - {"1234" ,1234 }, - {"1" ,1 }, - {"-32" ,-32 }, - {"-0" ,0 , .skip =1 }, + {"0" ,QLIT_QNUM (0 ),0 , }, + {"1234" ,QLIT_QNUM (1234 ),1234 , }, + {"1" ,QLIT_QNUM (1 ),1 , }, + {"-32" ,QLIT_QNUM (-32 ),-32 , }, + {"-0" ,QLIT_QNUM (0 ),0 , .skip =1 }, { }, };for (i =0 ; test_cases[i].encoded; i++) {QNum *qnum;int64_t val; +QNum *qlit_num; +int64_t qlit_val; qnum = qobject_to(QNum, qobject_from_json(test_cases[i].encoded,@@ -817,6 +820,7 @@ static void simple_number(void)g_assert (qnum);g_assert (qnum_get_try_int (qnum, &val)); g_assert_cmpint(val, ==, test_cases[i].decoded); +if (test_cases[i].skip ==0 ) {QString *str;@@ -826,9 +830,66 @@ static void simple_number(void) }qobject_unref (qnum); + + qlit_num =qobject_to (QNum, +qobject_from_qlit (&test_cases[i].qlit)); +g_assert (qlit_num); +g_assert (qnum_get_try_int (qlit_num, &qlit_val)); + g_assert_cmpint(qlit_val, ==, test_cases[i].decoded); + +qobject_unref (qlit_num); } } +static void qlit_large_number (void ) +{ +QLitObject maxu64 =QLIT_QNUM_UINT (UINT64_MAX ); +QLitObject maxi64 =QLIT_QNUM (INT64_MAX ); +QLitObject mini64 =QLIT_QNUM (INT64_MIN ); +QLitObject gtu64 =QLIT_QNUM_DOUBLE (18446744073709552e3 ); +QLitObject lti64 =QLIT_QNUM_DOUBLE (-92233720368547758e2 ); +QNum *qnum; +uint64_t val; +int64_t ival; + + qnum =qobject_to (QNum,qobject_from_qlit (&maxu64)); +g_assert (qnum); + g_assert_cmpuint(qnum_get_uint(qnum), ==, UINT64_MAX); +g_assert (!qnum_get_try_int (qnum, &ival)); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (&maxi64)); +g_assert (qnum); + g_assert_cmpuint(qnum_get_uint(qnum), ==, INT64_MAX); + g_assert_cmpint(qnum_get_int(qnum), ==, INT64_MAX); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (&mini64)); +g_assert (qnum); +g_assert (!qnum_get_try_uint (qnum, &val)); + g_assert_cmpuint(qnum_get_int(qnum), ==, INT64_MIN); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (>u64)); +g_assert (qnum); + g_assert_cmpfloat(qnum_get_double(qnum), ==, 18446744073709552e3); +g_assert (!qnum_get_try_uint (qnum, &val)); +g_assert (!qnum_get_try_int (qnum, &ival)); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (<i64)); +g_assert (qnum); + g_assert_cmpfloat(qnum_get_double(qnum), ==, -92233720368547758e2); +g_assert (!qnum_get_try_uint (qnum, &val)); +g_assert (!qnum_get_try_int (qnum, &ival)); + +qobject_unref (qnum); +} +static void large_number (void ) {const char *maxu64 ="18446744073709551615" ;/* 2^64-1 */ @@ -1472,6 +1533,7 @@ int main(int argc, char **argv)g_test_add_func ("/literals/string/utf8" , utf8_string);g_test_add_func ("/literals/number/simple" , simple_number); +g_test_add_func ("/literals/number/qlit_large" , qlit_large_number);g_test_add_func ("/literals/number/large" , large_number);g_test_add_func ("/literals/number/float" , float_number); --2.28.0
Signature · 1 line
Marc-André Lureau
Re: [PATCH v2 5/8] qlit: Support all types of QNums
Quoted 6 lines · Marc-André Lureau
Quoted 4 lines · Eduardo Habkost
Use QNumValue to represent QNums, so we can also support
uint64_tand double QNum values. Add newQLIT_QNUM_(INT|UINT|DOUBLE)macros for each case.
Quoted 6 lines
Quoted 3 lines
The
QLIT_QNUM()macro is being kept for compatibility with existing code, but becomes just a wrapper forQLIT_QNUM_INT().I am not sure it's worth to keep. (furthermore, it's only used in tests afaics)
Seconded.
Quoted 56 lines
Quoted 2 lines
Signed-off-by Eduardo Habkost <[email protected]>Reviewed-by Marc-André Lureau <[email protected]>---
Quoted 49 lines
Changes v1 -> v2:
* Coding style fix atqlit_equal_qobject()--- include/qapi/qmp/qlit.h | 11 +++++-- qobject/qlit.c | 5 +-- tests/check-qjson.c | 72 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 79 insertions(+), 9 deletions(-)include/qapi/qmp/qlit.h +9
-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;@@ -39,8 +40,14 @@ struct QLitDictEntry { { .type = QTYPE_QNULL }#define QLIT_QBOOL (val) \ { .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_QNUM (val) \ - { .type = QTYPE_QNUM, .value.qnum = (val) } + QLIT_QNUM_INT(val)#define QLIT_QSTR (val) \ { .type = QTYPE_QSTRING, .value.qstr = (val) }#define QLIT_QDICT (val) \qobject/qlit.c +0
-0
Quoted 8 lines
qobject/qlit.c bool qlit_equal_qobject(const QLitObject *lhs, const · line 76
Quoted 8 lines
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 (&lhs->value.qnum, +qnum_get_value (qobject_to (QNum, rhs)));
Before the patch, we crash when @rhs can't be represented as int64_t.
Afterwards, we return false (I think).
Please note this in the commit message. A separate fix preceding this patch would be even better, but may not be worth the trouble. Up to you.
Quoted 26 lines
Quoted 26 lines
qobject/qlit.c +0
-0case QTYPE_QSTRING : return (strcmp(lhs->value.qstr,qstring_get_str(qobject_to(QString, rhs))) == 0);qobject/qlit.c +1
-1@@ -94,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 : {tests/check-qjson.c +1
-2@@ -796,20 +796,23 @@ static void simple_number(void)int i;struct {const char *encoded; +QLitObject qlit;int64_t decoded;int skip; } test_cases[] = { - {"0" ,0 }, - {"1234" ,1234 },
Quoted 8 lines
tests/check-qjson.c static void simple_number(void) · line 807
Quoted 8 lines
tests/check-qjson.c +5
-3- { "1", 1 }, - { "-32", -32 }, - { "-0", 0, .skip = 1 }, + { "0", QLIT_QNUM(0), 0, }, + { "1234", QLIT_QNUM(1234), 1234, }, + { "1", QLIT_QNUM(1), 1, }, + { "-32", QLIT_QNUM(-32), -32, }, + { "-0", QLIT_QNUM(0), 0, .skip = 1 },
Note .qlit is always QLIT_QNUM(.decoded). Would doing without .qlit result in a simpler patch?
Quoted 99 lines
Quoted 99 lines
tests/check-qjson.c +61
-1{ }, };for (i =0 ; test_cases[i].encoded; i++) {QNum *qnum;int64_t val; +QNum *qlit_num; +int64_t qlit_val; qnum = qobject_to(QNum, qobject_from_json(test_cases[i].encoded,@@ -817,6 +820,7 @@ static void simple_number(void)g_assert (qnum);g_assert (qnum_get_try_int (qnum, &val)); g_assert_cmpint(val, ==, test_cases[i].decoded); +if (test_cases[i].skip ==0 ) {QString *str;@@ -826,9 +830,66 @@ static void simple_number(void) }qobject_unref (qnum); + + qlit_num =qobject_to (QNum, +qobject_from_qlit (&test_cases[i].qlit)); +g_assert (qlit_num); +g_assert (qnum_get_try_int (qlit_num, &qlit_val)); + g_assert_cmpint(qlit_val, ==, test_cases[i].decoded); + +qobject_unref (qlit_num); } } +static void qlit_large_number (void ) +{ +QLitObject maxu64 =QLIT_QNUM_UINT (UINT64_MAX ); +QLitObject maxi64 =QLIT_QNUM (INT64_MAX ); +QLitObject mini64 =QLIT_QNUM (INT64_MIN ); +QLitObject gtu64 =QLIT_QNUM_DOUBLE (18446744073709552e3 ); +QLitObject lti64 =QLIT_QNUM_DOUBLE (-92233720368547758e2 ); +QNum *qnum; +uint64_t val; +int64_t ival; + + qnum =qobject_to (QNum,qobject_from_qlit (&maxu64)); +g_assert (qnum); + g_assert_cmpuint(qnum_get_uint(qnum), ==, UINT64_MAX); +g_assert (!qnum_get_try_int (qnum, &ival)); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (&maxi64)); +g_assert (qnum); + g_assert_cmpuint(qnum_get_uint(qnum), ==, INT64_MAX); + g_assert_cmpint(qnum_get_int(qnum), ==, INT64_MAX); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (&mini64)); +g_assert (qnum); +g_assert (!qnum_get_try_uint (qnum, &val)); + g_assert_cmpuint(qnum_get_int(qnum), ==, INT64_MIN); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (>u64)); +g_assert (qnum); + g_assert_cmpfloat(qnum_get_double(qnum), ==, 18446744073709552e3); +g_assert (!qnum_get_try_uint (qnum, &val)); +g_assert (!qnum_get_try_int (qnum, &ival)); + +qobject_unref (qnum); + + qnum =qobject_to (QNum,qobject_from_qlit (<i64)); +g_assert (qnum); + g_assert_cmpfloat(qnum_get_double(qnum), ==, -92233720368547758e2); +g_assert (!qnum_get_try_uint (qnum, &val)); +g_assert (!qnum_get_try_int (qnum, &ival)); + +qobject_unref (qnum); +} +static void large_number (void ) {const char *maxu64 ="18446744073709551615" ;/* 2^64-1 */ @@ -1472,6 +1533,7 @@ int main(int argc, char **argv)g_test_add_func ("/literals/string/utf8" , utf8_string);g_test_add_func ("/literals/number/simple" , simple_number); +g_test_add_func ("/literals/number/qlit_large" , qlit_large_number);g_test_add_func ("/literals/number/large" , large_number);g_test_add_func ("/literals/number/float" , float_number); --2.28.0
Re: [PATCH v2 5/8] qlit: Support all types of QNums
Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Marc-André Lureau
Quoted 4 lines · Eduardo Habkost
Use QNumValue to represent QNums, so we can also support
uint64_tand double QNum values. Add newQLIT_QNUM_(INT|UINT|DOUBLE)macros for each case.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
The
QLIT_QNUM()macro is being kept for compatibility with existing code, but becomes just a wrapper forQLIT_QNUM_INT().I am not sure it's worth to keep. (furthermore, it's only used in tests afaics)
Seconded.
Understood, I will remove it. I thought the QAPI code generator was using it.
[...]
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
qobject/qlit.c +0
-0@@ -71,7 +71,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
Quoted 8 lines
Quoted 6 lines
qobject/qlit.c bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs) · line 75
Quoted 6 lines
qobject/qlit.c +2
-1case 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 (&lhs->value.qnum, +qnum_get_value (qobject_to (QNum, rhs)));Before the patch, we crash when @rhs can't be represented as
int64_t.
I thought it was expected behavior? QLit never supported QNUM_U64 or QNUM_DOUBLE as input.
Quoted 6 lines
Afterwards, we return false (I think).
Please note this in the commit message. A separate fix preceding this patch would be even better, but may not be worth the trouble. Up to you.
The fix would be 3 or 4 extra lines of code that would be immediately deleted. I'll just mention it as a side effect of the new feature.
Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
qobject/qlit.c +0
-0case QTYPE_QSTRING : return (strcmp(lhs->value.qstr,qstring_get_str(qobject_to(QString, rhs))) == 0);qobject/qlit.c +1
-1@@ -94,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 : {tests/check-qjson.c +1
-5@@ -796,20 +796,23 @@ static void simple_number(void)int i;struct {const char *encoded; +QLitObject qlit;int64_t decoded;int skip; } test_cases[] = { - {"0" ,0 }, - {"1234" ,1234 }, - {"1" ,1 }, - {"-32" ,-32 }, - {"-0" ,0 , .skip =1 },
Quoted 8 lines
Quoted 5 lines
tests/check-qjson.c static void simple_number(void) · line 807
Quoted 5 lines
tests/check-qjson.c +5
-0+ { "0", QLIT_QNUM(0), 0, }, + { "1234", QLIT_QNUM(1234), 1234, }, + { "1", QLIT_QNUM(1), 1, }, + { "-32", QLIT_QNUM(-32), -32, }, + { "-0", QLIT_QNUM(0), 0, .skip = 1 },Note .qlit is always
QLIT_QNUM(.decoded). Would doing without .qlit result in a simpler patch?
Good point. When I wrote this, I mistakenly thought we would end up having different types of qlits in the array.
I still want to test multiple types of QNums here, not just QNUM_I64. I will try to get something that is simple but also gets us more coverage. Maybe as a separate test function and/or a separate patch.
Quoted 35 lines
Quoted 35 lines
Quoted 35 lines
tests/check-qjson.c +11
-0{ }, };for (i =0 ; test_cases[i].encoded; i++) {QNum *qnum;int64_t val; +QNum *qlit_num; +int64_t qlit_val; qnum = qobject_to(QNum, qobject_from_json(test_cases[i].encoded,@@ -817,6 +820,7 @@ static void simple_number(void)g_assert (qnum);g_assert (qnum_get_try_int (qnum, &val)); g_assert_cmpint(val, ==, test_cases[i].decoded); +if (test_cases[i].skip ==0 ) {QString *str;@@ -826,9 +830,66 @@ static void simple_number(void) }qobject_unref (qnum); + + qlit_num =qobject_to (QNum, +qobject_from_qlit (&test_cases[i].qlit)); +g_assert (qlit_num); +g_assert (qnum_get_try_int (qlit_num, &qlit_val)); + g_assert_cmpint(qlit_val, ==, test_cases[i].decoded); + +qobject_unref (qlit_num); } }
[...]
Signature · 1 line
Eduardo
Re: [PATCH v2 5/8] qlit: Support all types of QNums
Quoted 37 lines · Eduardo Habkost
Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Marc-André Lureau
Quoted 4 lines · Eduardo Habkost
Use QNumValue to represent QNums, so we can also support
uint64_tand double QNum values. Add newQLIT_QNUM_(INT|UINT|DOUBLE)macros for each case.Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
The
QLIT_QNUM()macro is being kept for compatibility with existing code, but becomes just a wrapper forQLIT_QNUM_INT().I am not sure it's worth to keep. (furthermore, it's only used in tests afaics)
Seconded.
Understood, I will remove it. I thought the QAPI code generator was using it.
[...]
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
qobject/qlit.c +0
-0@@ -71,7 +71,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)Quoted 8 lines
Quoted 6 lines
qobject/qlit.c bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs) · line 75
Quoted 6 lines
qobject/qlit.c +2
-1case 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 (&lhs->value.qnum, +qnum_get_value (qobject_to (QNum, rhs)));Before the patch, we crash when @rhs can't be represented as
int64_t.
Quoted 2 lines
I thought it was expected behavior? QLit never supported
QNUM_U64orQNUM_DOUBLEas input.
Yes. It's a known limitation, not a bug. You're lifting the limitation, and that's worth noting in the commit message.
Quoted 9 lines
Quoted 5 lines
Afterwards, we return false (I think).
Please note this in the commit message. A separate fix preceding this patch would be even better, but may not be worth the trouble. Up to you.
The fix would be 3 or 4 extra lines of code that would be immediately deleted. I'll just mention it as a side effect of the new feature.
That's okay.
Quoted 38 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
qobject/qlit.c +0
-0case QTYPE_QSTRING : return (strcmp(lhs->value.qstr,qstring_get_str(qobject_to(QString, rhs))) == 0);qobject/qlit.c +1
-1@@ -94,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 : {tests/check-qjson.c +1
-5@@ -796,20 +796,23 @@ static void simple_number(void)int i;struct {const char *encoded; +QLitObject qlit;int64_t decoded;int skip; } test_cases[] = { - {"0" ,0 }, - {"1234" ,1234 }, - {"1" ,1 }, - {"-32" ,-32 }, - {"-0" ,0 , .skip =1 },Quoted 8 lines
Quoted 5 lines
tests/check-qjson.c static void simple_number(void) · line 807
Quoted 5 lines
tests/check-qjson.c +5
-0+ { "0", QLIT_QNUM(0), 0, }, + { "1234", QLIT_QNUM(1234), 1234, }, + { "1", QLIT_QNUM(1), 1, }, + { "-32", QLIT_QNUM(-32), -32, }, + { "-0", QLIT_QNUM(0), 0, .skip = 1 },Note .qlit is always
QLIT_QNUM(.decoded). Would doing without .qlit result in a simpler patch?
Quoted 7 lines
Good point. When I wrote this, I mistakenly thought we would end up having different types of qlits in the array.
I still want to test multiple types of QNums here, not just
QNUM_I64. I will try to get something that is simple but also gets us more coverage. Maybe as a separate test function and/or a separate patch.
Use your judgement :)
[...]
PATCH v2 6/8 qlit: qlit_type() function
In reply to Eduardo HabkostUseful function where we need to check for the qlit type before converting it to an actual QObject.
--- include/qapi/qmp/qlit.h | 5 +++++ 1 file changed, 5 insertions(+)
include/qapi/qmp/qlit.h +5 -0
@@ -59,4 +59,9 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);QObject *qobject_from_qlit (const QLitObject *qlit); +static inline QType qlit_type (const QLitObject *qlit) +{ +return qlit->type; +} +#endif /* QLIT_H */
Signature · 1 line
2.28.0
Re: [PATCH v2 6/8] qlit: qlit_type() function
Quoted 5 lines · Eduardo Habkost
Useful function where we need to check for the qlit type before converting it to an actual QObject.
Signed-off-by Eduardo Habkost <[email protected]>
---
Quoted 23 lines
include/qapi/qmp/qlit.h | 5 +++++ 1 file changed, 5 insertions(+)include/qapi/qmp/qlit.h +5
-0@@ -59,4 +59,9 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);QObject *qobject_from_qlit (const QLitObject *qlit); +static inline QType qlit_type (const QLitObject *qlit) +{ +return qlit->type; +} +#endif /*
QLIT_H*/
--
2.28.0
Signature · 1 line
Marc-André Lureau
Re: [PATCH v2 6/8] qlit: qlit_type() function
In reply to Eduardo HabkostQuoted 13 lines · Eduardo Habkost
Useful function where we need to check for the qlit type before converting it to an actual QObject.
Signed-off-by Eduardo Habkost <[email protected]>--- include/qapi/qmp/qlit.h | 5 +++++ 1 file changed, 5 insertions(+)include/qapi/qmp/qlit.h +0
-0
include/qapi/qmp/qlit.h bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs); · line 67
Quoted 9 lines
include/qapi/qmp/qlit.h +5
-0@@ -59,4 +59,9 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);QObject *qobject_from_qlit (const QLitObject *qlit); +static inline QType qlit_type (const QLitObject *qlit) +{ +return qlit->type; +} +#endif /* QLIT_H */
Hiding qlit->type behind a function makes sense only when the structure of QLitObject is an implementation secret. It don't think it is.
Re: [PATCH v2 6/8] qlit: qlit_type() function
Quoted 18 lines · Markus Armbruster
Quoted 16 lines · Eduardo Habkost
Useful function where we need to check for the qlit type before converting it to an actual QObject.
Signed-off-by Eduardo Habkost <[email protected]>--- include/qapi/qmp/qlit.h | 5 +++++ 1 file changed, 5 insertions(+)include/qapi/qmp/qlit.h +0
-0@@ -59,4 +59,9 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs);QObject *qobject_from_qlit (const QLitObject *qlit);
Quoted 9 lines
include/qapi/qmp/qlit.h bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs); · line 67
Quoted 6 lines
include/qapi/qmp/qlit.h +5
-0+static inline QType qlit_type (const QLitObject *qlit) +{ +return qlit->type; +} +#endif /* QLIT_H */ Hiding
qlit->typebehind a function makes sense only when the structure of QLitObject is an implementation secret. It don't think it is.
I thought all QLitObject fields were considered private. If they are not, I can happily get rid of that function.
Signature · 1 line
Eduardo
PATCH v2 7/8 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
Re: [PATCH v2 7/8] qom: Make object_property_set_default() public
Quoted 5 lines · Eduardo Habkost
The function will be used outside
qom/object.c, to simplify the field property code that sets the property default value.Signed-off-by Eduardo Habkost <[email protected]>
---
Quoted 47 lines
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);--
2.28.0
Signature · 1 line
Marc-André Lureau
PATCH v2 8/8 qom: Use qlit to represent property defaults
In reply to Eduardo HabkostUsing QLitObject lets us get rid of most of the
.set_default_value functions, and just useobject_property_set_default() directly.
---
Changes v1 -> v2:
* Instead of initializing defval to QLIT_QNULL
by default, just check for QTYPE_NONE, to find out if .defval was explicitly set. This avoids extra complexity at set_prop_arraylen().
--- include/hw/qdev-properties-system.h | 2 +- include/qom/field-property-internal.h | 4 --- include/qom/field-property.h | 26 ++++++++----------- include/qom/property-types.h | 19 ++++++-------- hw/core/qdev-properties-system.c | 8 ------ qom/field-property.c | 27 ++++++++++++++------ qom/property-types.c | 36 ++++----------------------- 7 files changed, 42 insertions(+), 80 deletions(-)
include/hw/qdev-properties-system.h +1 -1
@@ -65,7 +65,7 @@ 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) + .defval = QLIT_QSTR("auto"))#define DEFINE_PROP_AUDIODEV (_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_audiodev, QEMUSoundCard)
include/qom/field-property-internal.h +0 -4
@@ -15,10 +15,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); -void field_prop_set_default_value_int (ObjectProperty *op, -const Property *prop); -void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop);void field_prop_get_int32 (Object *obj,Visitor *v,const char *name,void *opaque,Error **errp);
include/qom/field-property.h +10 -16
@@ -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,7 +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 */ +/** +* @set_default_value: Optional callback for initializing the default value +* +* Most property types don't need to set this, as by default +* object_property_set_default() is called with the value at +* Property.defval. +*/ void (*set_default_value)(ObjectProperty *op,const Property *prop);/** @create: Optional callback for creation of property */ ObjectProperty *(*create)(ObjectClass *oc, const char *name,
include/qom/property-types.h +7 -12
@@ -5,6 +5,7 @@#define QOM_PROPERTY_TYPES_H #include "qom/field-property.h" +#include "qapi/qmp/qlit.h" extern const PropertyInfo prop_info_bit;extern const PropertyInfo prop_info_bit64;
@@ -25,34 +26,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 +127,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 +0 -8
@@ -570,7 +570,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 --- */
@@ -768,7 +767,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 --- */
@@ -1080,16 +1078,10 @@ 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) -{ -object_property_set_default_str (op,UUID_VALUE_AUTO ); -} -const PropertyInfo qdev_prop_uuid = { .name ="str" , .description ="UUID (aka GUID) or \"" UUID_VALUE_AUTO "\" for random value (default)" , .get = get_uuid, .set = set_uuid, - .set_default_value = set_default_uuid_auto, };
qom/field-property.c +19 -8
@@ -47,6 +47,20 @@ static ObjectPropertyAccessor *field_prop_setter(const PropertyInfo *info)return info->set ? field_prop_set :NULL ; } +static void field_prop_set_default_value (ObjectProperty *op, +Property *prop) +{ +if (qlit_type (&prop->defval) ==QTYPE_NONE ) { +return ; + } + +if (prop->info->set_default_value) { + prop->info->set_default_value (op, prop); + }else { +object_property_set_default (op,qobject_from_qlit (&prop->defval)); + } +} +/* * Property release callback for dynamically-created properties: * We call the underlying element's property release hook, and
@@ -83,11 +97,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, prop); +if (op->init) { + op->init (obj, op); } op->allow_set = allow_set;
@@ -113,9 +125,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);
qom/property-types.c +5 -31
@@ -28,8 +28,11 @@ void field_prop_set_enum(Object *obj, Visitor *v, const char *name,void field_prop_set_default_value_enum (ObjectProperty *op,const Property *prop) { -object_property_set_default_str (op, -qapi_enum_lookup (prop->info->enum_table, prop->defval.i)); +QObject *defval =qobject_from_qlit (&prop->defval); +const char *str =qapi_enum_lookup (prop->info->enum_table, +qnum_get_int (qobject_to (QNum, defval))); +object_property_set_default_str (op, str); +qobject_unref (defval); }const PropertyInfo prop_info_enum = {
@@ -80,17 +83,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) -{ -object_property_set_default_bool (op, prop->defval.u); -} -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 */
@@ -139,7 +136,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 --- */
@@ -166,7 +162,6 @@ const PropertyInfo prop_info_bool = { .name = "bool", .get = get_bool, .set = set_bool, - .set_default_value = set_default_value_bool, };/* --- 8bit integer --- */
@@ -189,23 +184,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) -{ -object_property_set_default_int (op, prop->defval.i); -} - -void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop) -{ -object_property_set_default_uint (op, prop->defval.u); -} -const PropertyInfo prop_info_uint8 = { .name ="uint8" , .get = get_uint8, .set = set_uint8, - .set_default_value = field_prop_set_default_value_uint, };/* --- 16bit integer --- */
@@ -232,7 +214,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 --- */
@@ -277,14 +258,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 --- */
@@ -329,14 +308,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 --- */
@@ -431,7 +408,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 --- */
@@ -495,7 +471,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 --- */
@@ -522,7 +497,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
Quoted 6 lines · Eduardo Habkost
Using QLitObject lets us get rid of most of the
.set_default_value functions, and just useobject_property_set_default()directly.Signed-off-by Eduardo Habkost <[email protected]>
---
Quoted 404 lines
Changes v1 -> v2:
* Instead of initializing defval toQLIT_QNULL
by default, just check forQTYPE_NONE, to find out if .defval was explicitly set. This avoids extra complexity atset_prop_arraylen().--- include/hw/qdev-properties-system.h | 2 +- include/qom/field-property-internal.h | 4 --- include/qom/field-property.h | 26 ++++++++----------- include/qom/property-types.h | 19 ++++++-------- hw/core/qdev-properties-system.c | 8 ------ qom/field-property.c | 27 ++++++++++++++------ qom/property-types.c | 36 ++++----------------------- 7 files changed, 42 insertions(+), 80 deletions(-)include/hw/qdev-properties-system.h +1
-1@@ -65,7 +65,7 @@ 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) + .defval = QLIT_QSTR("auto"))#define DEFINE_PROP_AUDIODEV (_n, _s, _f) \ DEFINE_PROP(_n, _s, _f, qdev_prop_audiodev, QEMUSoundCard)include/qom/field-property-internal.h +0
-4@@ -15,10 +15,6 @@ void field_prop_set_enum(Object *obj, Visitor *v, constchar *name, void field_prop_set_default_value_enum(ObjectProperty *op,const Property *prop); -void field_prop_set_default_value_int (ObjectProperty *op, -const Property *prop); -void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop); void field_prop_get_int32(Object *obj, Visitor *v, const char *name,void *opaque, Error **errp);include/qom/field-property.h +1
-5@@ -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).include/qom/field-property.h +0
-4-*/ -bool set_default; -/** -* @defval: default value for the property. This is used only if @set_default
include/qom/field-property.h +4
-7-* 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,7 +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 */ +/** +* @set_default_value: Optional callback for initializing the default value
include/qom/field-property.h +5
-0+* +* Most property types don't need to set this, as by default +* object_property_set_default() is called with the value at +* Property.defval. +*/ void (*set_default_value)(ObjectProperty *op,const Property *prop);/** @create: Optional callback for creation of property */ ObjectProperty *(*create)(ObjectClass *oc, const char *name,include/qom/property-types.h +7
-12@@ -5,6 +5,7 @@#define QOM_PROPERTY_TYPES_H #include "qom/field-property.h" +#include "qapi/qmp/qlit.h" extern const PropertyInfo prop_info_bit;extern const PropertyInfo prop_info_bit64;@@ -25,34 +26,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 +127,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 +0
-8@@ -570,7 +570,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 --- */ @@ -768,7 +767,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 --- */ @@ -1080,16 +1078,10 @@ 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) -{ -object_property_set_default_str (op,UUID_VALUE_AUTO ); -} -const PropertyInfo qdev_prop_uuid = { .name ="str" , .description ="UUID (aka GUID) or \"" UUID_VALUE_AUTO "\" for random value (default)" , .get = get_uuid, .set = set_uuid, - .set_default_value = set_default_uuid_auto, };qom/field-property.c +14
-0@@ -47,6 +47,20 @@ static ObjectPropertyAccessor *field_prop_setter(const PropertyInfo *info)return info->set ? field_prop_set :NULL ; } +static void field_prop_set_default_value (ObjectProperty *op, +Property *prop) +{ +if (qlit_type (&prop->defval) ==QTYPE_NONE ) { +return ; + } + +if (prop->info->set_default_value) { + prop->info->set_default_value (op, prop); + }else { +object_property_set_default (op,qobject_from_qlit (&prop->defval)); + } +} +/* * Property release callback for dynamically-created properties: * We call the underlying element's property release hook, and
qom/field-property.c +5
-8@@ -83,11 +97,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, prop); +if (op->init) { + op->init (obj, op); } op->allow_set = allow_set;@@ -113,9 +125,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);qom/property-types.c +5
-2@@ -28,8 +28,11 @@ void field_prop_set_enum(Object *obj, Visitor *v, constchar *name, void field_prop_set_default_value_enum(ObjectProperty *op, const Property *prop) { -object_property_set_default_str (op , - qapi_enum_lookup(prop->info->enum_table, prop->defval.i)); + QObject *defval = qobject_from_qlit(&prop->defval); +const char *str =qapi_enum_lookup (prop->info->enum_table, +qnum_get_int (qobject_to (QNum, defval))); +object_property_set_default_str (op, str); +qobject_unref (defval); }const PropertyInfo
prop_info_enum= {qom/property-types.c +0
-6@@ -80,17 +83,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) -{ -object_property_set_default_bool (op, prop->defval.u); -} -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 */
qom/property-types.c +0
-15@@ -139,7 +136,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 --- */ @@ -166,7 +162,6 @@ const PropertyInfo prop_info_bool = { .name = "bool", .get = get_bool, .set = set_bool, - .set_default_value = set_default_value_bool, };/* --- 8bit integer --- */ @@ -189,23 +184,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) -{ -object_property_set_default_int (op, prop->defval.i); -} - -void field_prop_set_default_value_uint (ObjectProperty *op, -const Property *prop) -{ -object_property_set_default_uint (op, prop->defval.u); -} -const PropertyInfo prop_info_uint8 = { .name ="uint8" , .get = get_uint8, .set = set_uint8, - .set_default_value = field_prop_set_default_value_uint, };/* --- 16bit integer --- */
qom/property-types.c +0
-9@@ -232,7 +214,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 --- */ @@ -277,14 +258,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 --- */ @@ -329,14 +308,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 --- */ @@ -431,7 +408,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 --- */ @@ -495,7 +471,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 --- */ @@ -522,7 +497,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 --- */ --2.28.0
Signature · 1 line
Marc-André Lureau
Quoted 9 lines · Eduardo Habkost
Based-on: [email protected]
Git branch: https://gitlab.com/ehabkost/qemu/-/commits/work/qdev-qlit-defaultsThis extend
qlit.hto 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.
What's left?
I'm asking because if you create a new way to get rid of most of an old way, you're still left with two ways, which may or may not be an improvement.
Moving defaults from code to data sounds attractive to me. Data is easier to reason about than code. For QAPI, we've been talking about defining defaults in the schema for a long time, but nobody has gotten around to finish an implementation.
Quoted 5 lines · Markus Armbruster
Quoted 3 lines · Eduardo Habkost
Based-on: [email protected]
Git branch: https://gitlab.com/ehabkost/qemu/-/commits/work/qdev-qlit-defaults
Quoted 8 lines
Quoted 6 lines
This extend
qlit.hto 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.
What's left?
Enums. Enums properties are a mess to implement, and I plan to tackle them later.
On all other cases, the external representation of the property value is similar to the internal representation. In the case of enums, the external representation is a string, but the internal representation is an integer.
Quoted 4 lines
I'm asking because if you create a new way to get rid of most of an old way, you're still left with two ways, which may or may not be an improvement.
I don't think this is an accurate description. We had five different ways of doing this[1]. I replaced four of them with the new qlit-based mechanism, and now we have two.
[1] The five different ways were:
qdev_propinfo_set_default_value_enum,
qdev_propinfo_set_default_value_int,
qdev_propinfo_set_default_value_uint,
set_default_uuid_auto, and
set_default_value_bool.
Quoted 5 lines
Moving defaults from code to data sounds attractive to me. Data is easier to reason about than code. For QAPI, we've been talking about defining defaults in the schema for a long time, but nobody has gotten around to finish an implementation.
Signature · 1 line
Eduardo
Quoted 9 lines · Eduardo Habkost
Quoted 1 line
What's left?
Enums. Enums properties are a mess to implement, and I plan to tackle them later.
On all other cases, the external representation of the property value is similar to the internal representation. In the case of enums, the external representation is a string, but the internal representation is an integer.
I would have expected a string QLit to work with enums, is there a reason why it doesn't?
Paolo
Quoted 5 lines · Paolo Bonzini
Quoted 4 lines · Eduardo Habkost
Quoted 1 line
What's left?
Enums. Enums properties are a mess to implement, and I plan to tackle them later.
Quoted 8 lines
Quoted 5 lines
On all other cases, the external representation of the property value is similar to the internal representation. In the case of enums, the external representation is a string, but the internal representation is an integer.
I would have expected a string QLit to work with enums, is there a reason why it doesn't?
It would work, but it would be more inconvenient for callers. Right now they use the C enum constant instead of a string.
Signature · 1 line
Eduardo
Quoted 12 lines · Eduardo Habkost
Quoted 11 lines · Paolo Bonzini
Quoted 9 lines · Eduardo Habkost
Quoted 1 line
What's left?
Enums. Enums properties are a mess to implement, and I plan to tackle them later.
On all other cases, the external representation of the property value is similar to the internal representation. In the case of enums, the external representation is a string, but the internal representation is an integer.
Quoted 5 lines
Quoted 2 lines
I would have expected a string QLit to work with enums, is there a reason why it doesn't?
It would work, but it would be more inconvenient for callers. Right now they use the C enum constant instead of a string.
It matches what you have to do already for compat props, so it's not a big deal. I would say just use strings.
Paolo
Quoted 13 lines · Paolo Bonzini
Quoted 12 lines · Eduardo Habkost
Quoted 11 lines · Paolo Bonzini
Quoted 9 lines · Eduardo Habkost
Quoted 1 line
What's left?
Enums. Enums properties are a mess to implement, and I plan to tackle them later.
On all other cases, the external representation of the property value is similar to the internal representation. In the case of enums, the external representation is a string, but the internal representation is an integer.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I would have expected a string QLit to work with enums, is there a reason why it doesn't?
It would work, but it would be more inconvenient for callers. Right now they use the C enum constant instead of a string.
It matches what you have to do already for compat props, so it's not a big deal. I would say just use strings.
No problem to me. I'll do.
Signature · 1 line
Eduardo