introduce visitor for parsing suffixed integer
v2 cover introduce visitor for parsing suffixed integer
v2:
* Naming changes:
- s/visit_type_uint_suffixed_int/visit_type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
* Added documentation to visit_type_suffixed_int()
* Fixed errp check.
* Style fixes
Reference to previous version dicussion:
http://lists.gnu.org/archive/html/qemu-devel/2012-12/msg00758.html
Git tree for testing:
https://github.com/imammedo/qemu/tree/type_suffixed_int_v2
Igor Mammedov (2):
add visitor for parsing int[KMGT] input string
target-i386: use visit_type_suffixed_int() to parse tsc_freq property
value
qapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ target-i386/cpu.c | 3 ++- 5 files changed, 74 insertions(+), 1 deletion(-)
Signature · 1 line
1.7.11.7
PATCH v2 1/2 add visitor for parsing int[KMGT] input string
Caller of visit_type_suffixed_int() have to specify value of 'K' suffix using suffix_factor argument. Example of selecting suffix_factor value:
* Kbytes: 1024
* Khz: 1000
---
v3:
- Fix errp check. Spotted-By: Andreas Färber <[email protected]>
- s/type_unit_suffixed_int/type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
- document visit_type_suffixed_int()
- add comment on current impl. limitation
v2:
- convert type_freq to type_unit_suffixed_int.
- provide qapi_dealloc_type_unit_suffixed_int() impl.
--- qapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ 4 files changed, 72 insertions(+)
qapi/qapi-dealloc-visitor.c +8 -0
@@ -143,6 +143,13 @@ static void qapi_dealloc_type_enum(Visitor *v, int *obj, const char *strings[], { } +static void qapi_dealloc_type_suffixed_int (Visitor *v,int64_t *obj, +const char *name, +const int suffix_factor, +Error **errp) +{ +} +Visitor *qapi_dealloc_get_visitor (QapiDeallocVisitor *v) {return &v->visitor;
@@ -170,6 +177,7 @@ QapiDeallocVisitor *qapi_dealloc_visitor_new(void) v->visitor.type_str = qapi_dealloc_type_str; v->visitor.type_number = qapi_dealloc_type_number; v->visitor.type_size = qapi_dealloc_type_size; + v->visitor.type_suffixed_int = qapi_dealloc_type_suffixed_int;QTAILQ_INIT (&v->stack);
qapi/qapi-visit-core.c +35 -0
@@ -311,3 +311,38 @@ void input_type_enum(Visitor *v, int *obj, const char *strings[],g_free (enum_str); *obj = value; } + +/** +* visit_type_suffixed_int: +* @v: visitor used for accesing external representation of value +* @obj: object that stores internal value +* @name: name of the option +* @suffix_factor: multiplication factor of suffix 'K' +* @errp: object to store error code +* +* Converts option value represented by @v taking in account multiplication +* factor of a suffix character that might follow number. +* i.e. resulted *@obj = number * suffix. +* +* Where following suffixes are recognised: +* no suffix = 1 +* K = @suffix_factor +* M = K * @suffix_factor +* G = M * @suffix_factor +* T = G * @suffix_factor +* +* In case @v doesn't provide type_suffixed_int() parser, convertion +* fall-backs to suffix-less type_int64() parser. +*/ +void visit_type_suffixed_int (Visitor *v,int64_t *obj,const char *name, +const int suffix_factor,Error **errp) +{ +if (error_is_set (errp)) { +return ; + } +if (v->type_suffixed_int) { + v->type_suffixed_int (v, obj, name, suffix_factor, errp); + }else { +visit_type_int64 (v, obj, name, errp); + } +}
qapi/qapi-visit-core.h +4 -0
@@ -62,6 +62,8 @@ struct Visitorvoid (*type_int64)(Visitor *v,int64_t *obj,const char *name,Error **errp);/* visit_type_size() falls back to (*type_uint64)() if type_size is unset */ void (*type_size)(Visitor *v,uint64_t *obj,const char *name,Error **errp); +void (*type_suffixed_int)(Visitor *v,int64_t *obj,const char *name, +const int uffix_factor,Error **errp); }; void visit_start_handle(Visitor *v, void **obj, const char *kind,
@@ -91,5 +93,7 @@ void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);void visit_type_bool (Visitor *v,bool *obj,const char *name,Error **errp);void visit_type_str (Visitor *v,char **obj,const char *name,Error **errp);void visit_type_number (Visitor *v,double *obj,const char *name,Error **errp); +void visit_type_suffixed_int (Visitor *v,int64_t *obj,const char *name, +const int suffix_factor,Error **errp);#endif
qapi/string-input-visitor.c +25 -0
@@ -110,6 +110,30 @@ static void parse_start_optional(Visitor *v, bool *present, *present = true; } +static void parse_type_suffixed_int (Visitor *v,int64_t *obj,const char *name, +const int suffix_factor,Error **errp) +{ +StringInputVisitor *siv =DO_UPCAST (StringInputVisitor, visitor, v); +char *endp = (char *) siv->string; +long long val =0 ; + +if (siv->string) { +/* TODO: presently strtosz_suffix_unit() is limited to [0..INT64_MAX] +* it should be fixed to handle [INT64_MIN..INT64_MAX], +* to cover whole int64_t range +*/ + val =strtosz_suffix_unit (siv->string, &endp, +STRTOSZ_DEFSUFFIX_B , suffix_factor); + } +if (!siv->string || val ==-1 || *endp) { +error_set (errp,QERR_INVALID_PARAMETER_VALUE , name, +"a value representable as a non-negative int64" ); +return ; + } + + *obj = val; +} +Visitor *string_input_get_visitor (StringInputVisitor *v) {return &v->visitor;
@@ -132,6 +156,7 @@ StringInputVisitor *string_input_visitor_new(const char *str) v->visitor.type_str = parse_type_str; v->visitor.type_number = parse_type_number; v->visitor.start_optional = parse_start_optional; + v->visitor.type_suffixed_int = parse_type_suffixed_int; v->string = str;return v;
Signature · 1 line
1.7.11.7
Re: [Qemu-devel] [PATCH 1/2] add visitor for parsing int[KMGT] input string
Quoted 7 lines · Igor Mammedov
Caller of
visit_type_suffixed_int()have to specify value of 'K' suffix usingsuffix_factorargument. Example of selectingsuffix_factorvalue:
* Kbytes: 1024
* Khz: 1000Signed-off-by Igor Mammedov <[email protected]>
I wonder if we could later introduce a visit_type_frequency() function that simply calls visit_type_suffixed_int(). This would allow us to use a 'frequency' type on QAPI, like the existing 'size' type we already have.
I suggest having explicitly distinct types on QAPI because the 'size' type probably won't abort (and maybe it _can't_ abort, to keep compatibility) in case it finds a "100MB" string. Likewise, the 'frequency' type wouldn't abort in case it finds a "100MHz" string.
With separate types, we could also make the 'frequency' type _not_ accept "100B" as a valid string (strtosz_suffix_unit() accepts "B" as a valid suffix, today).
Quoted 17 lines
---
v3:
- Fix errp check. Spotted-By: Andreas Färber <[email protected]>
- s/type_unit_suffixed_int/type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
- documentvisit_type_suffixed_int()
- add comment on current impl. limitation
v2:
- converttype_freqtotype_unit_suffixed_int.
- provideqapi_dealloc_type_unit_suffixed_int()impl.--- qapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ 4 files changed, 72 insertions(+)
[...]
Signature · 1 line
Eduardo
Re: [Qemu-devel] [PATCH 1/2] add visitor for parsing int[KMGT] input string
Quoted 12 lines · On Wed, 12 Dec 2012 16:16:42 -0200 Eduardo Habkost <[email protected]> wrote:
Quoted 7 lines · Igor Mammedov
Caller of
visit_type_suffixed_int()have to specify value of 'K' suffix usingsuffix_factorargument. Example of selectingsuffix_factorvalue:
* Kbytes: 1024
* Khz: 1000Signed-off-by Igor Mammedov <[email protected]>Reviewed-by Eduardo Habkost <[email protected]>
Quoted 8 lines
I wonder if we could later introduce a
visit_type_frequency()function that simply callsvisit_type_suffixed_int(). This would allow us to use a 'frequency' type on QAPI, like the existing 'size' type we already have.I suggest having explicitly distinct types on QAPI because the 'size' type probably won't abort (and maybe it _can't_ abort, to keep compatibility) in case it finds a "100MB" string. Likewise, the
It won't accept MB with current code, but we could probably pass something like custom suffix table {KHz => 1000, MHz=>1000000, ...} instead of unit for variables that accept frequency, and a corresponding table for sizes and whatever else if needed. Than we could use only visit_type_suffixed_int() and avoid creating an extra boiler code for every kind of units that might be needed in future.
Quoted 29 lines
'frequency' type wouldn't abort in case it finds a "100MHz" string.
With separate types, we could also make the 'frequency' type _not_ accept "100B" as a valid string (
strtosz_suffix_unit()accepts "B" as a valid suffix, today).Quoted 17 lines
---
v3:
- Fix errp check. Spotted-By: Andreas Färber <[email protected]>
- s/type_unit_suffixed_int/type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
- documentvisit_type_suffixed_int()
- add comment on current impl. limitation
v2:
- converttype_freqtotype_unit_suffixed_int.
- provideqapi_dealloc_type_unit_suffixed_int()impl.--- qapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ 4 files changed, 72 insertions(+)[...]
Signature · 1 line
Eduardo
Signature · 2 lines
Regards, Igor
Re: [Qemu-devel] [PATCH 1/2] add visitor for parsing int[KMGT] input string
Quoted 23 lines · Igor Mammedov
Quoted 12 lines · On Wed, 12 Dec 2012 16:16:42 -0200 Eduardo Habkost <[email protected]> wrote:
Quoted 7 lines · Igor Mammedov
Caller of
visit_type_suffixed_int()have to specify value of 'K' suffix usingsuffix_factorargument. Example of selectingsuffix_factorvalue:
* Kbytes: 1024
* Khz: 1000Signed-off-by Igor Mammedov <[email protected]>Reviewed-by Eduardo Habkost <[email protected]>Quoted 8 lines
I wonder if we could later introduce a
visit_type_frequency()function that simply callsvisit_type_suffixed_int(). This would allow us to use a 'frequency' type on QAPI, like the existing 'size' type we already have.I suggest having explicitly distinct types on QAPI because the 'size' type probably won't abort (and maybe it _can't_ abort, to keep compatibility) in case it finds a "100MB" string. Likewise, the
Quoted 6 lines
It won't accept MB with current code, but we could probably pass something like custom suffix table {KHz => 1000, MHz=>1000000, ...} instead of unit for variables that accept frequency, and a corresponding table for sizes and whatever else if needed. Than we could use only
visit_type_suffixed_int()and avoid creating an extra boiler code for every kind of units that might be needed in future.
The above make sense, but I wasn't worrying about how it's done
internally when calling/configuring the parsing code. My only point is
about the type definitions in QAPI. I believe that having this in QAPI:
'type':'frequency'
'type':'size'
looks better than:
'type': 'suffixed_int', 'suffix_factor': 1000, 'unit': 'Hz'
'type': 'suffixed_int', 'suffix_factor': 1024, 'unit': 'B'
or, even worse:
'type': 'suffixed_int', 'suffix_table': { 'K': 1000, 'KHz': 1000, 'M': 1000000, 'MHz': 10000000, ... } 'type': 'suffixed_int', 'suffix_table': { 'K': 1024, 'KB': 1024, 'M': 1048576, 'MB': 1048576, ... }
or:
'type': 'suffixed_int', 'suffix_table': { 'K': 1000, 'M': 1000000, ... }, 'unit': 'Hz' 'type': 'suffixed_int', 'suffix_table': { 'K': 1024, 'M': 1048576, ... }, 'unit': 'B'
Quoted 35 lines
Quoted 30 lines
'frequency' type wouldn't abort in case it finds a "100MHz" string.
With separate types, we could also make the 'frequency' type _not_ accept "100B" as a valid string (
strtosz_suffix_unit()accepts "B" as a valid suffix, today).Quoted 17 lines
---
v3:
- Fix errp check. Spotted-By: Andreas Färber <[email protected]>
- s/type_unit_suffixed_int/type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
- documentvisit_type_suffixed_int()
- add comment on current impl. limitation
v2:
- converttype_freqtotype_unit_suffixed_int.
- provideqapi_dealloc_type_unit_suffixed_int()impl.--- qapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ 4 files changed, 72 insertions(+)[...]
Signature · 1 line
EduardoSignature · 2 lines
Regards, Igor
Signature · 1 line
Eduardo
PATCH v2 2/2 target-i386: use visit_type_suffixed_int() to parse tsc_freq property value
In reply to Igor Mammedov---
v3:
- s/visit_type_unit_suffixed_int/visit_type_suffixed_int/
v2:
- replace visit_type_freq() with visit_type_unit_suffixed_int()
in x86_cpuid_set_tsc_freq() --- target-i386/cpu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target-i386/cpu.c +2 -1
@@ -1194,8 +1194,9 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,const int64_t min =0 ;const int64_t max =INT64_MAX ;int64_t value; +const int kHz_factor =1000 ; -visit_type_int (v, &value, name, errp); +visit_type_suffixed_int (v, &value, name, kHz_factor, errp);if (error_is_set (errp)) {return ; }
Signature · 1 line
1.7.11.7
Re: [Qemu-devel] [PATCH 2/2] target-i386: use visit_type_suffixed_int() to parse tsc_freq property value
Quoted 1 line · Igor Mammedov
Signed-off-by Igor Mammedov <[email protected]>
Like I said in the previous patch, maybe it would be nice to have a visit_type_frequency() helper, to hide the specific details/parameters required to parse frequency values.
Quoted 29 lines
---
v3:
- s/visit_type_unit_suffixed_int/visit_type_suffixed_int/
v2:
- replacevisit_type_freq()withvisit_type_unit_suffixed_int()in x86_cpuid_set_tsc_freq() --- target-i386/cpu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)target-i386/cpu.c +2
-1@@ -1194,8 +1194,9 @@ static void x86_cpuid_set_tsc_freq(Object *obj, Visitor *v, void *opaque,const int64_t min =0 ;const int64_t max =INT64_MAX ;int64_t value; +const int kHz_factor =1000 ; -visit_type_int (v, &value, name, errp); +visit_type_suffixed_int (v, &value, name, kHz_factor, errp);if (error_is_set (errp)) {return ; }Signature · 1 line
1.7.11.7
Signature · 1 line
Eduardo
Quoted 7 lines · Igor Mammedov
v2:
* Naming changes:
- s/visit_type_uint_suffixed_int/visit_type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
* Added documentation tovisit_type_suffixed_int()
* Fixed errp check.
* Style fixes
This is not how visitors are supposed to be used.
Just treat tsc_freq as a string property and parse it in the setter.
Regards,
Anthony Liguori
Quoted 21 lines
Reference to previous version dicussion:
http://lists.gnu.org/archive/html/qemu-devel/2012-12/msg00758.htmlGit tree for testing:
https://github.com/imammedo/qemu/tree/type_suffixed_int_v2Igor Mammedov (2):
add visitor for parsing int[KMGT] input string
target-i386: usevisit_type_suffixed_int()to parsetsc_freqproperty
valueqapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ target-i386/cpu.c | 3 ++- 5 files changed, 74 insertions(+), 1 deletion(-)Signature · 1 line
1.7.11.7
Quoted 11 lines · On Sun, 23 Dec 2012 14:34:21 -0600 Anthony Liguori <[email protected]>…
Quoted 7 lines · Igor Mammedov
v2:
* Naming changes:
- s/visit_type_uint_suffixed_int/visit_type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
* Added documentation tovisit_type_suffixed_int()
* Fixed errp check.
* Style fixesThis is not how visitors are supposed to be used.
We were trying to generalize and make code more reusable. Later could be used as a common base for specialized size and Hz visitors.
Quoted 1 line
Just treat
tsc_freqas a string property and parse it in the setter.
Would http://lists.gnu.org/archive/html/qemu-devel/2012-10/msg03835.html
be better or you suggest just abandon generalization idea and parse it locally in target-i386/cpu.c?
Quoted 26 lines
Regards,
Anthony Liguori
Quoted 21 lines
Reference to previous version dicussion:
http://lists.gnu.org/archive/html/qemu-devel/2012-12/msg00758.htmlGit tree for testing:
https://github.com/imammedo/qemu/tree/type_suffixed_int_v2Igor Mammedov (2):
add visitor for parsing int[KMGT] input string
target-i386: usevisit_type_suffixed_int()to parsetsc_freqproperty
valueqapi/qapi-dealloc-visitor.c | 8 ++++++++ qapi/qapi-visit-core.c | 35 +++++++++++++++++++++++++++++++++++ qapi/qapi-visit-core.h | 4 ++++ qapi/string-input-visitor.c | 25 +++++++++++++++++++++++++ target-i386/cpu.c | 3 ++- 5 files changed, 74 insertions(+), 1 deletion(-)Signature · 1 line
1.7.11.7
Signature · 2 lines
Regards, Igor
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Igor Mammedov
v2:
* Naming changes:
- s/visit_type_uint_suffixed_int/visit_type_suffixed_int/
- use 'suffix_factor' instead of 'unit'
* Added documentation tovisit_type_suffixed_int()
* Fixed errp check.
* Style fixes
Quoted 3 lines
This is not how visitors are supposed to be used.
Just treat
tsc_freqas a string property and parse it in the setter.
So, why visit_type_size() does exist? Should we work to eliminate it? If not, why is it different from a "frequency" type?
Signature · 1 line
Eduardo