From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E62FDC63697 for ; Thu, 19 Nov 2020 18:25:17 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4066224199 for ; Thu, 19 Nov 2020 18:25:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eUfQmoL8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4066224199 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:37360 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kfocN-0006pp-Vj for qemu-devel@archiver.kernel.org; Thu, 19 Nov 2020 13:25:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:57280) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kfoZg-0004Nh-5B for qemu-devel@nongnu.org; Thu, 19 Nov 2020 13:22:28 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:37645) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kfoZR-0002LN-Tp for qemu-devel@nongnu.org; Thu, 19 Nov 2020 13:22:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1605810129; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tQlQJfmk6pQ0uvLwcyCD9/Ja601u4dfcgR2idiY1aLY=; b=eUfQmoL8azyXDg6NaD5Ejj695+Qv5K5nwUNV6sJpUY3zh0J2xfytFagUKSpt+hZsrWsGJY oayf+juQ4yv6K9fLU4WBlBTYOtkcCr8eQzWqkA45Cx0mf6CqkoDwlf51+Q4t53Z1l6qi7S z4ZgM/7ToHxdOY2bPAaVRcXfGEPRnbU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-576-Fe6Yo4ypOtmGLovtVZj-8g-1; Thu, 19 Nov 2020 13:22:07 -0500 X-MC-Unique: Fe6Yo4ypOtmGLovtVZj-8g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DD219107AFA8; Thu, 19 Nov 2020 18:22:04 +0000 (UTC) Received: from localhost (ovpn-115-101.rdu2.redhat.com [10.10.115.101]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BAC8100AE39; Thu, 19 Nov 2020 18:21:59 +0000 (UTC) Date: Thu, 19 Nov 2020 13:21:58 -0500 From: Eduardo Habkost To: Markus Armbruster Subject: Re: [PATCH v2 3/8] qnum: QNumValue type for QNum value literals Message-ID: <20201119182158.GX1509407@habkost.net> References: <20201116224143.1284278-1-ehabkost@redhat.com> <20201116224143.1284278-4-ehabkost@redhat.com> <20201117144246.GD1235237@habkost.net> <87mtzdd4p7.fsf@dusky.pond.sub.org> MIME-Version: 1.0 In-Reply-To: <87mtzdd4p7.fsf@dusky.pond.sub.org> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ehabkost@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=63.128.21.124; envelope-from=ehabkost@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , =?utf-8?Q?Marc-Andr=C3=A9?= Lureau , Daniel =?utf-8?B?UC4gQmVycmFuZ8Op?= , QEMU Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" On Thu, Nov 19, 2020 at 11:24:52AM +0100, Markus Armbruster wrote: > Marc-André Lureau writes: > > > On Tue, Nov 17, 2020 at 6:42 PM Eduardo Habkost wrote: > > > >> On Tue, Nov 17, 2020 at 12:37:56PM +0400, Marc-André Lureau wrote: > >> > On Tue, Nov 17, 2020 at 2:43 AM Eduardo Habkost > >> wrote: > >> > > >> > > 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 > >> > > --- > >> > > 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(-) > >> > > > >> > > diff --git a/include/qapi/qmp/qnum.h b/include/qapi/qmp/qnum.h > >> > > index 55c27b1c24..62fbdfda68 100644 > >> > > --- a/include/qapi/qmp/qnum.h > >> > > +++ b/include/qapi/qmp/qnum.h > >> > > @@ -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: */ > > 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? > > >> > > 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 > >> > > > >> > > >> > 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. > >> > >> > > >> > 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. > >> > >> > > >> > + * @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; > >> > > +} > >> > > + > >> > > > >> > > >> > 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? > >> > > > > 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 :) > > >> > >> > > >> > bool qnum_get_try_int(const QNum *qn, int64_t *val); > >> > > int64_t qnum_get_int(const QNum *qn); > >> > > > >> > > diff --git a/qobject/qnum.c b/qobject/qnum.c > >> > > index 69fd9a82d9..f80d4efd76 100644 > >> > > --- a/qobject/qnum.c > >> > > +++ b/qobject/qnum.c > >> > > @@ -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). > >> > >> 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. > > >> > > >> > +{ > >> > > + 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)); > > 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. > > >> > > } > >> > > > >> > > /** > >> > > @@ -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; > > 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. 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. > > >> > > @@ -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) > >> > > > >> > > char *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. > > Me too. Even in external code. Understood. > > > no, it's ok to me > > > > > >> > >> > > >> > char *buffer; > >> > > int 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); > > 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). > > >> > > + const QNumValue *num_x = &qnum_x->value; > >> > > + const QNumValue *num_y = &qnum_y->value; > >> > > > >> > > switch (num_x->kind) { > >> > > case QNUM_I64: > >> > > diff --git a/tests/check-qnum.c b/tests/check-qnum.c > >> > > index 4105015872..9499b0d845 100644 > >> > > --- a/tests/check-qnum.c > >> > > +++ b/tests/check-qnum.c > >> > > @@ -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); > >> > > } > >> > > -- > >> > > 2.28.0 > >> > > > >> > > > >> > > > >> > lgtm otherwise > >> > >> Thanks! > >> > >> -- > >> Eduardo > >> > >> -- Eduardo