util/cutils: New qemu_strtosz_metric()

To parse numbers with metric suffixes, we use

qemu_strtosz_suffix_unit(nptr, &eptr, QEMU_STRTOSZ_DEFSUFFIX_B, 1000)

Capture this in a new function for legibility:

qemu_strtosz_metric(nptr, &eptr)

Replace test_qemu_strtosz_suffix_unit() by test_qemu_strtosz_metric().

Rename qemu_strtosz_suffix_unit() to do_strtosz() and give it internal
linkage.

Backports commit d2734d2629266006b0413433778474d5801c60be from qemu
This commit is contained in:
Markus Armbruster 2018-03-02 08:47:39 -05:00 committed by Lioncash
parent fb962d2e74
commit f656cd91ec
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
3 changed files with 11 additions and 7 deletions

View file

@ -147,8 +147,8 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
int64_t qemu_strtosz(const char *nptr, char **end); int64_t qemu_strtosz(const char *nptr, char **end);
int64_t qemu_strtosz_suffix(const char *nptr, char **end, int64_t qemu_strtosz_suffix(const char *nptr, char **end,
const char default_suffix); const char default_suffix);
int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end, int64_t qemu_strtosz_metric(const char *nptr, char **end);
const char default_suffix, int64_t unit);
#define K_BYTE (1ULL << 10) #define K_BYTE (1ULL << 10)
#define M_BYTE (1ULL << 20) #define M_BYTE (1ULL << 20)
#define G_BYTE (1ULL << 30) #define G_BYTE (1ULL << 30)

View file

@ -2184,8 +2184,7 @@ static void x86_cpu_parse_featurestr(CPUState *cs, char *features,
int64_t tsc_freq; int64_t tsc_freq;
char *err; char *err;
tsc_freq = qemu_strtosz_suffix_unit(val, &err, tsc_freq = qemu_strtosz_metric(val, &err);
QEMU_STRTOSZ_DEFSUFFIX_B, 1000);
if (tsc_freq < 0 || *err) { if (tsc_freq < 0 || *err) {
error_setg(errp, "bad numerical value %s", val); error_setg(errp, "bad numerical value %s", val);
return; return;

View file

@ -154,8 +154,8 @@ static int64_t suffix_mul(char suffix, int64_t unit)
* in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on * in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on
* other error. * other error.
*/ */
int64_t qemu_strtosz_suffix_unit(const char *nptr, char **end, static int64_t do_strtosz(const char *nptr, char **end,
const char default_suffix, int64_t unit) const char default_suffix, int64_t unit)
{ {
int64_t retval = -EINVAL; int64_t retval = -EINVAL;
char *endptr; char *endptr;
@ -200,7 +200,7 @@ fail:
int64_t qemu_strtosz_suffix(const char *nptr, char **end, int64_t qemu_strtosz_suffix(const char *nptr, char **end,
const char default_suffix) const char default_suffix)
{ {
return qemu_strtosz_suffix_unit(nptr, end, default_suffix, 1024); return do_strtosz(nptr, end, default_suffix, 1024);
} }
int64_t qemu_strtosz(const char *nptr, char **end) int64_t qemu_strtosz(const char *nptr, char **end)
@ -208,6 +208,11 @@ int64_t qemu_strtosz(const char *nptr, char **end)
return qemu_strtosz_suffix(nptr, end, QEMU_STRTOSZ_DEFSUFFIX_MB); return qemu_strtosz_suffix(nptr, end, QEMU_STRTOSZ_DEFSUFFIX_MB);
} }
int64_t qemu_strtosz_metric(const char *nptr, char **end)
{
return do_strtosz(nptr, end, QEMU_STRTOSZ_DEFSUFFIX_B, 1000);
}
/** /**
* Helper function for error checking after strtol() and the like * Helper function for error checking after strtol() and the like
*/ */