From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:40847) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ti8dy-0004TU-3N for qemu-devel@nongnu.org; Mon, 10 Dec 2012 14:04:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ti8dr-0005Aj-EH for qemu-devel@nongnu.org; Mon, 10 Dec 2012 14:03:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:29486) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ti8dr-0005Af-5C for qemu-devel@nongnu.org; Mon, 10 Dec 2012 14:03:51 -0500 Date: Mon, 10 Dec 2012 20:03:41 +0100 From: Igor Mammedov Message-ID: <20121210200341.5125ac96@thinkpad.mammed.net> In-Reply-To: <20121210172746.GG30686@vm> References: <1354828325-16568-1-git-send-email-imammedo@redhat.com> <1354828325-16568-2-git-send-email-imammedo@redhat.com> <50C23C1F.6010102@suse.de> <20121210170138.4c8aa6e3@nial.usersys.redhat.com> <20121210172746.GG30686@vm> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 1/2] qapi: add visitor for parsing int[KMGT] input string List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: mdroth Cc: qemu-devel@nongnu.org, Andreas =?UTF-8?B?RsOkcmJlcg==?= , ehabkost@redhat.com On Mon, 10 Dec 2012 11:27:46 -0600 mdroth wrote: > On Mon, Dec 10, 2012 at 05:01:38PM +0100, Igor Mammedov wrote: > > On Fri, 07 Dec 2012 19:57:35 +0100 > > Andreas F=C3=A4rber wrote: > >=20 > > > Am 06.12.2012 22:12, schrieb Igor Mammedov: > > > > Caller of visit_type_unit_suffixed_int() will have to specify > > > > value of 'K' suffix via unit argument. > > > > For Kbytes it's 1024, for Khz it's 1000. > > > >=20 > > > > Signed-off-by: Igor Mammedov > > > > --- > > > > v2: > > > > - convert type_freq to type_unit_suffixed_int. > > > > - provide qapi_dealloc_type_unit_suffixed_int() impl. > > > > --- > > > > qapi/qapi-dealloc-visitor.c | 7 +++++++ > > > > qapi/qapi-visit-core.c | 13 +++++++++++++ > > > > qapi/qapi-visit-core.h | 8 ++++++++ > > > > qapi/string-input-visitor.c | 22 ++++++++++++++++++++++ > > > > 4 files changed, 50 insertions(+) > > > >=20 > > > > diff --git a/qapi/qapi-dealloc-visitor.c b/qapi/qapi-dealloc-visito= r.c > > > > index 75214e7..57e662c 100644 > > > > --- a/qapi/qapi-dealloc-visitor.c > > > > +++ b/qapi/qapi-dealloc-visitor.c > > > > @@ -143,6 +143,12 @@ static void qapi_dealloc_type_enum(Visitor *v,= int > > > > *obj, const char *strings[], { > > > > } > > > > =20 > > > > +static void qapi_dealloc_type_unit_suffixed_int(Visitor *v, int64_= t *obj, > > > > + const char *name, > > > > + const int unit, Er= ror > > > > **errp) +{ > > > > +} > > > > + > > > > Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v) > > > > { > > > > return &v->visitor; > > > > @@ -170,6 +176,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(vo= id) > > > > v->visitor.type_str =3D qapi_dealloc_type_str; > > > > v->visitor.type_number =3D qapi_dealloc_type_number; > > > > v->visitor.type_size =3D qapi_dealloc_type_size; > > > > + v->visitor.type_unit_suffixed_int =3D > > > > qapi_dealloc_type_unit_suffixed_int;=20 > > > > QTAILQ_INIT(&v->stack); > > > > =20 > > > > diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c > > > > index 7a82b63..dcbc1a9 100644 > > > > --- a/qapi/qapi-visit-core.c > > > > +++ b/qapi/qapi-visit-core.c > > > > @@ -311,3 +311,16 @@ void input_type_enum(Visitor *v, int *obj, con= st > > > > char *strings[], g_free(enum_str); > > > > *obj =3D value; > > > > } > > > > + > > > > +void visit_type_unit_suffixed_int(Visitor *v, int64_t *obj, const = char > > > > *name, > > > > + const int unit, Error **errp) > > > > +{ > > > > + if (!error_is_set(errp)) { > > >=20 > > > if (error_is_set(errp)) { > > Thanks, I'll fix it. > >=20 > > > > + return; > > > > + } > > > > + if (v->type_unit_suffixed_int) { > > > > + v->type_unit_suffixed_int(v, obj, name, unit, errp); > > > > + } else { > > > > + visit_type_int64(v, obj, name, errp); > > > > + } > > > > +} > > > > diff --git a/qapi/qapi-visit-core.h b/qapi/qapi-visit-core.h > > > > index 60aceda..04e690a 100644 > > > > --- a/qapi/qapi-visit-core.h > > > > +++ b/qapi/qapi-visit-core.h > > > > @@ -62,6 +62,12 @@ struct Visitor > > > > void (*type_int64)(Visitor *v, int64_t *obj, const char *name,= Error > > > > **errp); /* visit_type_size() falls back to (*type_uint64)() if typ= e_size > > > > is unset */ void (*type_size)(Visitor *v, uint64_t *obj, const char > > > > *name, Error **errp); > > > > + /* > > > > + * visit_unit_suffixed_int() falls back to (*type_int64)() > > > > + * if type_unit_suffixed_int is unset > > > > + */ > > >=20 > > > Indentation is one off. > > ditto > >=20 > > >=20 > > > > + void (*type_unit_suffixed_int)(Visitor *v, int64_t *obj, const= char > > > > *name, > > > > + const int unit, Error **errp); > > >=20 > > > Are we expecting differently suffixed ints? Otherwise we could > > > optionally shorten to type_suffixed_int (but that probably still does= n't > > > fit within one comment line ;)). > > Not with current implementation. I'll shorten it as you've suggested. > >=20 > > >=20 > > > > }; > > > > =20 > > > > void visit_start_handle(Visitor *v, void **obj, const char *kind, > > > > @@ -91,5 +97,7 @@ void visit_type_size(Visitor *v, uint64_t *obj, c= onst > > > > char *name, Error **errp); void visit_type_bool(Visitor *v, bool *o= bj, > > > > const char *name, Error **errp); void visit_type_str(Visitor *v, ch= ar > > > > **obj, const char *name, Error **errp); void visit_type_number(Visi= tor > > > > *v, double *obj, const char *name, Error **errp); +void > > > > visit_type_unit_suffixed_int(Visitor *v, int64_t *obj, const char *= name, > > > > + const int unit, Error **errp); > > > > =20 > > > > #endif > > > > diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visito= r.c > > > > index 497eb9a..d2bd154 100644 > > > > --- a/qapi/string-input-visitor.c > > > > +++ b/qapi/string-input-visitor.c > > > > @@ -110,6 +110,27 @@ static void parse_start_optional(Visitor *v, b= ool > > > > *present, *present =3D true; > > > > } > > > > =20 > > > > +static void parse_type_unit_suffixed_int(Visitor *v, int64_t *obj, > > > > + const char *name, const i= nt > > > > unit, > > > > + Error **errp) > > > > +{ > > > > + StringInputVisitor *siv =3D DO_UPCAST(StringInputVisitor, visi= tor, v); > > > > + char *endp =3D (char *) siv->string; > > > > + long long val =3D 0; > > > > + > > > > + if (siv->string) { > > > > + val =3D strtosz_suffix_unit(siv->string, &endp, > > > > + STRTOSZ_DEFSUFFIX_B, unit); > > > > + } > > > > + if (!siv->string || val =3D=3D -1 || *endp) { > > > > + error_set(errp, QERR_INVALID_PARAMETER_VALUE, name, > > > > + "a value representable as a non-negative int64"); > > >=20 > > > Weird indentation remaining, looks as if we could align with errp wit= hin > > > 80 chars. > > Thanks, I'll fix it. > >=20 > > >=20 > > > However, I wonder if "unit" is the (physically etc.) correct term her= e? > > > Isn't the "unit" Hz / byte / ... and 1000 more of a conversion factor= or > > > something? At least that's the way I've seen unit used in the API of > > > another project, passing an enum of Hertz, gram, meter/second, etc. > > If we are to generalize it to integer than units might not make much se= nse, > > they could be anything. Perhaps 'suffix_factor' would be descriptive en= ough > > + adding documentation comment to the visitor. >=20 > The real distinction I think is base=3D2 vs. base=3D10, but that might ca= use > confusion WRT to how the numberical value should be intepreted (10=3D=3D2 > vs. 10=3D=3D10), so maybe suffix_base=3D=3D2|10, or suffix_factor=3D=3D10= 00/1024 as you > suggested. I think I'd prefer the former but either works for me. I'd prefer suffix_factor to avoid confusion with word 'base' which is commo= nly used in conversion routines (i.e. man strtoll). > > >=20 > > >=20 > > > Andreas > > >=20 > > > > + return; > > > > + } > > > > + > > > > + *obj =3D val; > > > > +} > > > > + > > > > Visitor *string_input_get_visitor(StringInputVisitor *v) > > > > { > > > > return &v->visitor; > > > > @@ -132,6 +153,7 @@ StringInputVisitor *string_input_visitor_new(co= nst > > > > char *str) v->visitor.type_str =3D parse_type_str; > > > > v->visitor.type_number =3D parse_type_number; > > > > v->visitor.start_optional =3D parse_start_optional; > > > > + v->visitor.type_unit_suffixed_int =3D parse_type_unit_suffixed= _int; > > > > =20 > > > > v->string =3D str; > > > > return v; > > > >=20 > > >=20 > > >=20 > >=20 >=20 --=20 Regards, Igor