提交 e591591b 编写于 作者: M Markus Armbruster

util/cutils: Rename qemu_strtosz() to qemu_strtosz_MiB()

With qemu_strtosz(), no suffix means mebibytes.  It's used rarely.
I'm going to add a similar function where no suffix means bytes.
Rename qemu_strtosz() to qemu_strtosz_MiB() to make the name
qemu_strtosz() available for the new function.
Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-Id: <1487708048-2131-16-git-send-email-armbru@redhat.com>
上级 d2734d26
...@@ -1385,7 +1385,7 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict) ...@@ -1385,7 +1385,7 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
break; break;
case MIGRATION_PARAMETER_MAX_BANDWIDTH: case MIGRATION_PARAMETER_MAX_BANDWIDTH:
p.has_max_bandwidth = true; p.has_max_bandwidth = true;
valuebw = qemu_strtosz(valuestr, &endp); valuebw = qemu_strtosz_MiB(valuestr, &endp);
if (valuebw < 0 || (size_t)valuebw != valuebw if (valuebw < 0 || (size_t)valuebw != valuebw
|| *endp != '\0') { || *endp != '\0') {
error_setg(&err, "Invalid size %s", valuestr); error_setg(&err, "Invalid size %s", valuestr);
......
...@@ -1268,7 +1268,7 @@ static void ivshmem_realize(PCIDevice *dev, Error **errp) ...@@ -1268,7 +1268,7 @@ static void ivshmem_realize(PCIDevice *dev, Error **errp)
s->legacy_size = 4 << 20; /* 4 MB default */ s->legacy_size = 4 << 20; /* 4 MB default */
} else { } else {
char *end; char *end;
int64_t size = qemu_strtosz(s->sizearg, &end); int64_t size = qemu_strtosz_MiB(s->sizearg, &end);
if (size < 0 || (size_t)size != size || *end != '\0' if (size < 0 || (size_t)size != size || *end != '\0'
|| !is_power_of_2(size)) { || !is_power_of_2(size)) {
error_setg(errp, "Invalid size %s", s->sizearg); error_setg(errp, "Invalid size %s", s->sizearg);
......
...@@ -153,9 +153,9 @@ int parse_uint_full(const char *s, unsigned long long *value, int base); ...@@ -153,9 +153,9 @@ int parse_uint_full(const char *s, unsigned long long *value, int base);
#define QEMU_STRTOSZ_DEFSUFFIX_MB 'M' #define QEMU_STRTOSZ_DEFSUFFIX_MB 'M'
#define QEMU_STRTOSZ_DEFSUFFIX_KB 'K' #define QEMU_STRTOSZ_DEFSUFFIX_KB 'K'
#define QEMU_STRTOSZ_DEFSUFFIX_B 'B' #define QEMU_STRTOSZ_DEFSUFFIX_B 'B'
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_MiB(const char *nptr, char **end);
int64_t qemu_strtosz_metric(const char *nptr, char **end); int64_t qemu_strtosz_metric(const char *nptr, char **end);
#define K_BYTE (1ULL << 10) #define K_BYTE (1ULL << 10)
......
...@@ -2811,7 +2811,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, ...@@ -2811,7 +2811,7 @@ static QDict *monitor_parse_arguments(Monitor *mon,
break; break;
} }
} }
val = qemu_strtosz(p, &end); val = qemu_strtosz_MiB(p, &end);
if (val < 0) { if (val < 0) {
monitor_printf(mon, "invalid size\n"); monitor_printf(mon, "invalid size\n");
goto fail; goto fail;
......
...@@ -1376,16 +1376,16 @@ static void test_qemu_strtosz_simple(void) ...@@ -1376,16 +1376,16 @@ static void test_qemu_strtosz_simple(void)
int64_t res; int64_t res;
str = "0"; str = "0";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, 0); g_assert_cmpint(res, ==, 0);
g_assert(endptr == str + 1); g_assert(endptr == str + 1);
str = "12345M"; str = "12345M";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, 12345 * M_BYTE); g_assert_cmpint(res, ==, 12345 * M_BYTE);
g_assert(endptr == str + 6); g_assert(endptr == str + 6);
res = qemu_strtosz(str, NULL); res = qemu_strtosz_MiB(str, NULL);
g_assert_cmpint(res, ==, 12345 * M_BYTE); g_assert_cmpint(res, ==, 12345 * M_BYTE);
/* Note: precision is 53 bits since we're parsing with strtod() */ /* Note: precision is 53 bits since we're parsing with strtod() */
...@@ -1433,35 +1433,35 @@ static void test_qemu_strtosz_units(void) ...@@ -1433,35 +1433,35 @@ static void test_qemu_strtosz_units(void)
int64_t res; int64_t res;
/* default is M */ /* default is M */
res = qemu_strtosz(none, &endptr); res = qemu_strtosz_MiB(none, &endptr);
g_assert_cmpint(res, ==, M_BYTE); g_assert_cmpint(res, ==, M_BYTE);
g_assert(endptr == none + 1); g_assert(endptr == none + 1);
res = qemu_strtosz(b, &endptr); res = qemu_strtosz_MiB(b, &endptr);
g_assert_cmpint(res, ==, 1); g_assert_cmpint(res, ==, 1);
g_assert(endptr == b + 2); g_assert(endptr == b + 2);
res = qemu_strtosz(k, &endptr); res = qemu_strtosz_MiB(k, &endptr);
g_assert_cmpint(res, ==, K_BYTE); g_assert_cmpint(res, ==, K_BYTE);
g_assert(endptr == k + 2); g_assert(endptr == k + 2);
res = qemu_strtosz(m, &endptr); res = qemu_strtosz_MiB(m, &endptr);
g_assert_cmpint(res, ==, M_BYTE); g_assert_cmpint(res, ==, M_BYTE);
g_assert(endptr == m + 2); g_assert(endptr == m + 2);
res = qemu_strtosz(g, &endptr); res = qemu_strtosz_MiB(g, &endptr);
g_assert_cmpint(res, ==, G_BYTE); g_assert_cmpint(res, ==, G_BYTE);
g_assert(endptr == g + 2); g_assert(endptr == g + 2);
res = qemu_strtosz(t, &endptr); res = qemu_strtosz_MiB(t, &endptr);
g_assert_cmpint(res, ==, T_BYTE); g_assert_cmpint(res, ==, T_BYTE);
g_assert(endptr == t + 2); g_assert(endptr == t + 2);
res = qemu_strtosz(p, &endptr); res = qemu_strtosz_MiB(p, &endptr);
g_assert_cmpint(res, ==, P_BYTE); g_assert_cmpint(res, ==, P_BYTE);
g_assert(endptr == p + 2); g_assert(endptr == p + 2);
res = qemu_strtosz(e, &endptr); res = qemu_strtosz_MiB(e, &endptr);
g_assert_cmpint(res, ==, E_BYTE); g_assert_cmpint(res, ==, E_BYTE);
g_assert(endptr == e + 2); g_assert(endptr == e + 2);
} }
...@@ -1472,7 +1472,7 @@ static void test_qemu_strtosz_float(void) ...@@ -1472,7 +1472,7 @@ static void test_qemu_strtosz_float(void)
char *endptr = NULL; char *endptr = NULL;
int64_t res; int64_t res;
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, 12.345 * M_BYTE); g_assert_cmpint(res, ==, 12.345 * M_BYTE);
g_assert(endptr == str + 7); g_assert(endptr == str + 7);
} }
...@@ -1484,17 +1484,17 @@ static void test_qemu_strtosz_invalid(void) ...@@ -1484,17 +1484,17 @@ static void test_qemu_strtosz_invalid(void)
int64_t res; int64_t res;
str = ""; str = "";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, -EINVAL); g_assert_cmpint(res, ==, -EINVAL);
g_assert(endptr == str); g_assert(endptr == str);
str = " \t "; str = " \t ";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, -EINVAL); g_assert_cmpint(res, ==, -EINVAL);
g_assert(endptr == str); g_assert(endptr == str);
str = "crap"; str = "crap";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, -EINVAL); g_assert_cmpint(res, ==, -EINVAL);
g_assert(endptr == str); g_assert(endptr == str);
} }
...@@ -1506,12 +1506,12 @@ static void test_qemu_strtosz_trailing(void) ...@@ -1506,12 +1506,12 @@ static void test_qemu_strtosz_trailing(void)
int64_t res; int64_t res;
str = "123xxx"; str = "123xxx";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, 123 * M_BYTE); g_assert_cmpint(res, ==, 123 * M_BYTE);
g_assert(endptr == str + 3); g_assert(endptr == str + 3);
str = "1kiB"; str = "1kiB";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, 1024); g_assert_cmpint(res, ==, 1024);
g_assert(endptr == str + 2); g_assert(endptr == str + 2);
} }
...@@ -1523,7 +1523,7 @@ static void test_qemu_strtosz_erange(void) ...@@ -1523,7 +1523,7 @@ static void test_qemu_strtosz_erange(void)
int64_t res; int64_t res;
str = "-1"; str = "-1";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, -ERANGE); g_assert_cmpint(res, ==, -ERANGE);
g_assert(endptr == str + 2); g_assert(endptr == str + 2);
...@@ -1543,7 +1543,7 @@ static void test_qemu_strtosz_erange(void) ...@@ -1543,7 +1543,7 @@ static void test_qemu_strtosz_erange(void)
g_assert(endptr == str + 19); g_assert(endptr == str + 19);
str = "10E"; str = "10E";
res = qemu_strtosz(str, &endptr); res = qemu_strtosz_MiB(str, &endptr);
g_assert_cmpint(res, ==, -ERANGE); g_assert_cmpint(res, ==, -ERANGE);
g_assert(endptr == str + 3); g_assert(endptr == str + 3);
} }
......
...@@ -254,7 +254,7 @@ int64_t qemu_strtosz_suffix(const char *nptr, char **end, ...@@ -254,7 +254,7 @@ int64_t qemu_strtosz_suffix(const char *nptr, char **end,
return do_strtosz(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_MiB(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);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册