All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hu Tao <hutao@cn.fujitsu.com>
To: qemu-devel@nongnu.org
Cc: pbonzini@redhat.com, lersek@redhat.com,
	Wanlong Gao <gaowanlong@cn.fujitsu.com>,
	imammedo@redhat.com
Subject: Re: [Qemu-devel] [PATCH v18 11/14] qapi: make string input visitor parse int list
Date: Wed, 19 Feb 2014 16:17:27 +0800	[thread overview]
Message-ID: <20140219081727.GB11157@G08FNSTD100614.fnst.cn.fujitsu.com> (raw)
In-Reply-To: <2b9b6745ac6f7a5b697f479d684b226505952701.1392794450.git.hutao@cn.fujitsu.com>

On Wed, Feb 19, 2014 at 03:54:02PM +0800, Hu Tao wrote:
> Cc: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
> ---
>  qapi/string-input-visitor.c       | 160 ++++++++++++++++++++++++++++++++++++--
>  tests/test-string-input-visitor.c |  22 ++++++
>  2 files changed, 176 insertions(+), 6 deletions(-)
> 
> diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c
> index a152f5d..4540ca3 100644
> --- a/qapi/string-input-visitor.c
> +++ b/qapi/string-input-visitor.c
> @@ -15,30 +15,175 @@
>  #include "qapi/visitor-impl.h"
>  #include "qapi/qmp/qerror.h"
>  
> +enum ListMode {
> +    LM_NONE,             /* not traversing a list of repeated options */
> +    LM_STARTED,          /* start_list() succeeded */
> +
> +    LM_IN_PROGRESS,      /* next_list() has been called.
> +                          *
> +                          * Generating the next list link will consume the most
> +                          * recently parsed QemuOpt instance of the repeated
> +                          * option.
> +                          *
> +                          * Parsing a value into the list link will examine the
> +                          * next QemuOpt instance of the repeated option, and
> +                          * possibly enter LM_SIGNED_INTERVAL or
> +                          * LM_UNSIGNED_INTERVAL.
> +                          */
> +
> +    LM_SIGNED_INTERVAL,  /* next_list() has been called.
> +                          *
> +                          * Generating the next list link will consume the most
> +                          * recently stored element from the signed interval,
> +                          * parsed from the most recent QemuOpt instance of the
> +                          * repeated option. This may consume QemuOpt itself
> +                          * and return to LM_IN_PROGRESS.
> +                          *
> +                          * Parsing a value into the list link will store the
> +                          * next element of the signed interval.
> +                          */
> +
> +    LM_UNSIGNED_INTERVAL,/* Same as above, only for an unsigned interval. */
> +
> +    LM_END
> +};
> +
> +typedef enum ListMode ListMode;
> +
>  struct StringInputVisitor
>  {
>      Visitor visitor;
> +
> +    ListMode list_mode;
> +
> +    /* When parsing a list of repeating options as integers, values of the form
> +     * "a-b", representing a closed interval, are allowed. Elements in the
> +     * range are generated individually.
> +     */
> +    union {
> +        int64_t s;
> +        uint64_t u;
> +    } range_next, range_limit;
> +
>      const char *string;
>  };
>  
> +static void
> +start_list(Visitor *v, const char *name, Error **errp)
> +{
> +    StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
> +
> +    /* we can't traverse a list in a list */
> +    assert(siv->list_mode == LM_NONE);
> +    siv->list_mode = LM_STARTED;
> +}
> +
> +static GenericList *
> +next_list(Visitor *v, GenericList **list, Error **errp)
> +{
> +    StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
> +    GenericList **link;
> +
> +    switch (siv->list_mode) {
> +    case LM_STARTED:
> +        siv->list_mode = LM_IN_PROGRESS;
> +        link = list;
> +        break;
> +
> +    case LM_SIGNED_INTERVAL:
> +    case LM_UNSIGNED_INTERVAL:
> +        link = &(*list)->next;
> +
> +        if (siv->list_mode == LM_SIGNED_INTERVAL) {
> +            if (siv->range_next.s < siv->range_limit.s) {
> +                ++siv->range_next.s;
> +                break;
> +            }
> +        } else if (siv->range_next.u < siv->range_limit.u) {
> +            ++siv->range_next.u;
> +            break;
> +        }
> +        siv->list_mode = LM_END;
> +        /* range has been completed, fall through */
> +
> +    case LM_END:
> +        return NULL;
> +
> +    case LM_IN_PROGRESS:
> +        link = &(*list)->next;
> +        break;
> +
> +    default:
> +        abort();
> +    }
> +
> +    *link = g_malloc0(sizeof **link);
> +    return *link;
> +}
> +
> +static void
> +end_list(Visitor *v, Error **errp)
> +{
> +    StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
> +
> +    assert(siv->list_mode == LM_STARTED ||
> +           siv->list_mode == LM_END ||
> +           siv->list_mode == LM_IN_PROGRESS ||
> +           siv->list_mode == LM_SIGNED_INTERVAL ||
> +           siv->list_mode == LM_UNSIGNED_INTERVAL);
> +    siv->list_mode = LM_NONE;
> +}
> +
>  static void parse_type_int(Visitor *v, int64_t *obj, const char *name,
>                             Error **errp)
>  {
>      StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
> -    char *endp = (char *) siv->string;
> +    char *str = (char *) siv->string;
>      long long val;
> +    char *endptr;
>  
> -    errno = 0;
> -    if (siv->string) {
> -        val = strtoll(siv->string, &endp, 0);
> +    if (siv->list_mode == LM_SIGNED_INTERVAL) {
> +        *obj = siv->range_next.s;
> +        return;
>      }
> -    if (!siv->string || errno || endp == siv->string || *endp) {
> +
> +    if (!siv->string) {
>          error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
>                    "integer");
>          return;
>      }
>  
> -    *obj = val;
> +    errno = 0;
> +    val = strtoll(siv->string, &endptr, 0);
> +
> +    if (errno == 0 && endptr > str && INT64_MIN <= val && val <= INT64_MAX) {
> +        if (*endptr == '\0') {
> +            *obj = val;
> +            siv->list_mode = LM_END;
> +            return;
> +        }
> +        if (*endptr == '-' && siv->list_mode == LM_IN_PROGRESS) {
> +            long long val2;
> +
> +            str = endptr + 1;
> +            val2 = strtoll(str, &endptr, 0);
> +            if (errno == 0 && endptr > str && *endptr == '\0' &&
> +                INT64_MIN <= val2 && val2 <= INT64_MAX && val <= val2 &&
> +                (val > INT64_MAX - 65536 ||
> +                 val2 < val + 65536)) {
> +                siv->range_next.s = val;
> +                siv->range_limit.s = val2;
> +                siv->list_mode = LM_SIGNED_INTERVAL;
> +
> +                /* as if entering on the top */
> +                *obj = siv->range_next.s;
> +                return;
> +            }
> +        }
> +    }
> +    error_set(errp, QERR_INVALID_PARAMETER_VALUE, name,
> +              (siv->list_mode == LM_NONE) ? "an int64 value" :
> +                                           "an int64 value or range");
>  }

Two problems:

1. the code is mostly copied from OptsVisitor. maybe we can share the
   code?

2. int list is not implemented in string outout visitor. but there is
   currently no user of it. Should we implement it or not?

  reply	other threads:[~2014-02-19  8:21 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-19  7:53 [Qemu-devel] [PATCH v18 00/14] Add support for binding guest numa nodes to host numa nodes Hu Tao
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 01/14] NUMA: move numa related code to new file numa.c Hu Tao
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 02/14] NUMA: check if the total numa memory size is equal to ram_size Hu Tao
2014-02-25 13:38   ` Eric Blake
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 03/14] NUMA: Add numa_info structure to contain numa nodes info Hu Tao
2014-02-19  9:26   ` Igor Mammedov
2014-02-21  2:54     ` hu tao
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 04/14] NUMA: convert -numa option to use OptsVisitor Hu Tao
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 05/14] NUMA: expand MAX_NODES from 64 to 128 Hu Tao
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 06/14] qapi: add SIZE type parser to string_input_visitor Hu Tao
2014-02-19  9:54   ` Igor Mammedov
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 07/14] add memdev backend infrastructure Hu Tao
2014-02-19  9:15   ` Igor Mammedov
2014-02-19  7:53 ` [Qemu-devel] [PATCH v18 08/14] pc: pass QEMUMachineInitArgs to pc_memory_init Hu Tao
2014-02-19  7:54 ` [Qemu-devel] [PATCH v18 09/14] numa: introduce memory_region_allocate_system_memory Hu Tao
2014-02-19  7:54 ` [Qemu-devel] [PATCH v18 10/14] numa: add -numa node, memdev= option Hu Tao
2014-02-19  9:50   ` Igor Mammedov
2014-02-19 11:53     ` Paolo Bonzini
2014-03-04  0:10   ` Eric Blake
2014-03-04  2:20     ` Hu Tao
2014-02-19  7:54 ` [Qemu-devel] [PATCH v18 11/14] qapi: make string input visitor parse int list Hu Tao
2014-02-19  8:17   ` Hu Tao [this message]
2014-02-19  8:42     ` Paolo Bonzini
2014-02-19  7:54 ` [Qemu-devel] [PATCH v18 12/14] qapi: add HostMemPolicy enum type Hu Tao
2014-02-19  9:08   ` Paolo Bonzini
2014-02-19 11:23   ` Igor Mammedov
2014-02-19  7:54 ` [Qemu-devel] [PATCH v18 13/14] memory backend: fill memory backend ram fields Hu Tao
2014-02-19  9:03   ` Paolo Bonzini
2014-02-19  9:36     ` Igor Mammedov
2014-02-25 10:20       ` Hu Tao
2014-02-25 14:15         ` Paolo Bonzini
2014-02-26  5:00           ` Hu Tao
2014-02-26  8:47             ` Igor Mammedov
2014-02-26  8:59               ` Hu Tao
2014-02-26 12:19                 ` Igor Mammedov
2014-02-26 11:22               ` Paolo Bonzini
2014-02-26  5:57       ` Hu Tao
2014-02-26  9:05         ` Paolo Bonzini
2014-02-26  9:10         ` Igor Mammedov
2014-02-26 10:33           ` Paolo Bonzini
2014-02-26 12:31             ` Igor Mammedov
2014-02-26 12:45               ` Paolo Bonzini
2014-02-26 12:58                 ` Marcelo Tosatti
2014-02-26 13:14                   ` Paolo Bonzini
2014-02-26 13:43                 ` Igor Mammedov
2014-02-26 13:47                   ` Paolo Bonzini
2014-02-26 14:25                     ` Igor Mammedov
2014-02-26 14:39                       ` Paolo Bonzini
2014-02-25 10:09     ` Hu Tao
2014-03-03  3:24       ` Hu Tao
2014-02-19  7:54 ` [Qemu-devel] [PATCH v18 14/14] amp: add query-memdev Hu Tao
2014-02-19  8:14   ` Hu Tao
2014-02-19  9:07   ` Paolo Bonzini

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=20140219081727.GB11157@G08FNSTD100614.fnst.cn.fujitsu.com \
    --to=hutao@cn.fujitsu.com \
    --cc=gaowanlong@cn.fujitsu.com \
    --cc=imammedo@redhat.com \
    --cc=lersek@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.