qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Jes.Sorensen@redhat.com
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, armbru@redhat.com
Subject: [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count.
Date: Wed, 13 Oct 2010 09:20:47 +0200	[thread overview]
Message-ID: <1286954450-30860-2-git-send-email-Jes.Sorensen@redhat.com> (raw)
In-Reply-To: <1286954450-30860-1-git-send-email-Jes.Sorensen@redhat.com>

From: Jes Sorensen <Jes.Sorensen@redhat.com>

strtosz() returns -1 on error. It now supports human unit formats in
eg. 1.0G, with better error handling.

The following suffixes are supported:
B/b = bytes
K/k = KB
M/m = MB
G/g = GB
T/t = TB

This patch changes -numa and -m input to use strtosz().

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
---
 cutils.c      |   79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qemu-common.h |    1 +
 vl.c          |   31 +++++++---------------
 3 files changed, 90 insertions(+), 21 deletions(-)

diff --git a/cutils.c b/cutils.c
index 5883737..44a862a 100644
--- a/cutils.c
+++ b/cutils.c
@@ -23,6 +23,7 @@
  */
 #include "qemu-common.h"
 #include "host-utils.h"
+#include <math.h>
 
 void pstrcpy(char *buf, int buf_size, const char *str)
 {
@@ -283,3 +284,81 @@ int fcntl_setfl(int fd, int flag)
 }
 #endif
 
+/*
+ * Convert string to bytes, allowing either B/b for bytes, K/k for KB,
+ * M/m for MB, G/g for GB or T/t for TB. Default without any postfix
+ * is MB. End pointer will be returned in *end, if not NULL.
+ * Return -1 on error.
+ */
+ssize_t strtosz(const char *nptr, char **end)
+{
+    ssize_t retval = -1;
+    char *endptr, c;
+    int mul_required = 0;
+    double val, mul, integral, fraction;
+
+    errno = 0;
+    val = strtod(nptr, &endptr);
+    if (isnan(val) || endptr == nptr || errno != 0 || val < 0 ||
+        val >= HUGE_VAL) {
+        goto fail;
+    }
+    integral = modf(val, &fraction);
+    if (integral != 0) {
+        mul_required = 1;
+    }
+    /*
+     * Any whitespace character is fine for terminating the number,
+     * in addition we accept ',' to handle strings where the size is
+     * part of a multi token argument. Otherwise check that the suffix
+     * is just one character.
+     */
+    c = *endptr++;
+    if (isspace(c) || c == '\0' || c == ',') {
+        c = 0;
+    } else if (!isspace(*endptr) && *endptr != 0) {
+        goto fail;
+    }
+    switch (c) {
+    case 'B':
+    case 'b':
+        mul = 1;
+        if (mul_required) {
+            goto fail;
+        }
+        break;
+    case 'K':
+    case 'k':
+        mul = 1 << 10;
+        break;
+    case 0:
+        if (mul_required) {
+            goto fail;
+        }
+    case 'M':
+    case 'm':
+        mul = 1ULL << 20;
+        break;
+    case 'G':
+    case 'g':
+        mul = 1ULL << 30;
+        break;
+    case 'T':
+    case 't':
+        mul = 1ULL << 40;
+        break;
+    default:
+        goto fail;
+    }
+    if (val * mul >= ~(size_t)0) {
+        goto fail;
+    }
+    retval = (val * mul);
+
+    if (end) {
+        *end = endptr;
+    }
+
+fail:
+    return retval;
+}
diff --git a/qemu-common.h b/qemu-common.h
index 81aafa0..0a062d4 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -153,6 +153,7 @@ time_t mktimegm(struct tm *tm);
 int qemu_fls(int i);
 int qemu_fdatasync(int fd);
 int fcntl_setfl(int fd, int flag);
+ssize_t strtosz(const char *nptr, char **end);
 
 /* path.c */
 void init_paths(const char *prefix);
diff --git a/vl.c b/vl.c
index df414ef..6043fa2 100644
--- a/vl.c
+++ b/vl.c
@@ -734,16 +734,13 @@ static void numa_add(const char *optarg)
         if (get_param_value(option, 128, "mem", optarg) == 0) {
             node_mem[nodenr] = 0;
         } else {
-            value = strtoull(option, &endptr, 0);
-            switch (*endptr) {
-            case 0: case 'M': case 'm':
-                value <<= 20;
-                break;
-            case 'G': case 'g':
-                value <<= 30;
-                break;
+            ssize_t sval;
+            sval = strtosz(option, NULL);
+            if (sval < 0) {
+                fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
+                exit(1);
             }
-            node_mem[nodenr] = value;
+            node_mem[nodenr] = sval;
         }
         if (get_param_value(option, 128, "cpus", optarg) == 0) {
             node_cpumask[nodenr] = 0;
@@ -2163,18 +2160,10 @@ int main(int argc, char **argv, char **envp)
                 exit(0);
                 break;
             case QEMU_OPTION_m: {
-                uint64_t value;
-                char *ptr;
+                ssize_t value;
 
-                value = strtoul(optarg, &ptr, 10);
-                switch (*ptr) {
-                case 0: case 'M': case 'm':
-                    value <<= 20;
-                    break;
-                case 'G': case 'g':
-                    value <<= 30;
-                    break;
-                default:
+                value = strtosz(optarg, NULL);
+                if (value < 0) {
                     fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
                     exit(1);
                 }
-- 
1.7.2.3

  reply	other threads:[~2010-10-13  7:21 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-13  7:20 [Qemu-devel] [PATCH v7 0/4] Introduce strtosz and make use of it Jes.Sorensen
2010-10-13  7:20 ` Jes.Sorensen [this message]
2010-10-13  8:19   ` [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count Markus Armbruster
2010-10-13  7:20 ` [Qemu-devel] [PATCH 2/4] Add support for 'o' octet (bytes) format as monitor parameter Jes.Sorensen
2010-10-13  7:20 ` [Qemu-devel] [PATCH 3/4] Switch migrate_set_speed() to take an 'o' argument rather than a float Jes.Sorensen
2010-10-13  7:20 ` [Qemu-devel] [PATCH 4/4] Remove obsolete 'f' double parameter type Jes.Sorensen
2010-10-13  8:24 ` [Qemu-devel] [PATCH v7 0/4] Introduce strtosz and make use of it Markus Armbruster
  -- strict thread matches above, loose matches on Subject: below --
2010-10-21 15:15 [Qemu-devel] [PATCH v9 " Jes.Sorensen
2010-10-21 15:15 ` [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count Jes.Sorensen
2010-10-13  8:48 [Qemu-devel] [PATCH v8 0/4] Introduce strtosz and make use of it Jes.Sorensen
2010-10-13  8:48 ` [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count Jes.Sorensen
2010-10-13 13:28   ` Markus Armbruster
2010-10-12 11:10 [Qemu-devel] [PATCH v6 0/4] Introduce strtosz and make use of it Jes.Sorensen
2010-10-12 11:10 ` [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count Jes.Sorensen
2010-10-12 15:52   ` Markus Armbruster
2010-10-13  6:47     ` Jes Sorensen
2010-10-13  8:07       ` Markus Armbruster
2010-10-11 12:54 [Qemu-devel] [PATCH v5 0/4] Introduce strtosz and make use of it Jes.Sorensen
2010-10-11 12:54 ` [Qemu-devel] [PATCH 1/4] Introduce strtosz() library function to convert a string to a byte count Jes.Sorensen
2010-10-11 16:42   ` Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1286954450-30860-2-git-send-email-Jes.Sorensen@redhat.com \
    --to=jes.sorensen@redhat.com \
    --cc=armbru@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).