qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Chris Krumme <Chris.Krumme@windriver.com>
To: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [RFC 3/8] qapi: add QemuFileInputVisitor
Date: Mon, 24 Oct 2011 18:59:31 -0500	[thread overview]
Message-ID: <4EA5FBE3.1020202@windriver.com> (raw)
In-Reply-To: <1316443309-23843-4-git-send-email-mdroth@linux.vnet.ibm.com>

On 09/19/2011 09:41 AM, Michael Roth wrote:
> Visitor interfaces to read values from a QEMUFile
>
> Signed-off-by: Michael Roth<mdroth@linux.vnet.ibm.com>
> ---
>   Makefile.objs                  |    1 +
>   qapi/qemu-file-input-visitor.c |  350 ++++++++++++++++++++++++++++++++++++++++
>   qapi/qemu-file-input-visitor.h |   26 +++
>   3 files changed, 377 insertions(+), 0 deletions(-)
>   create mode 100644 qapi/qemu-file-input-visitor.c
>   create mode 100644 qapi/qemu-file-input-visitor.h
>
> diff --git a/Makefile.objs b/Makefile.objs
> index 48fe0c4..6bc8555 100644
> --- a/Makefile.objs
> +++ b/Makefile.objs
> @@ -82,6 +82,7 @@ fsdev-obj-$(CONFIG_VIRTFS) += $(addprefix fsdev/, $(fsdev-nested-y))
>   common-obj-y = $(block-obj-y) blockdev.o
>   common-obj-y += $(qapi-obj-y)
>   common-obj-y += qapi/qemu-file-output-visitor.o
> +common-obj-y += qapi/qemu-file-input-visitor.o
>   common-obj-y += $(net-obj-y)
>   common-obj-y += $(qobject-obj-y)
>   common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))
> diff --git a/qapi/qemu-file-input-visitor.c b/qapi/qemu-file-input-visitor.c
> new file mode 100644
> index 0000000..7217125
> --- /dev/null
> +++ b/qapi/qemu-file-input-visitor.c
> @@ -0,0 +1,350 @@
> +/*
> + * QEMUFile Output Visitor
> + *
> + * Copyright IBM, Corp. 2011
> + *
> + * Authors:
> + *  Michael Roth<mdroth@linux.vnet.ibm.com>
> + *
> + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
> + * See the COPYING.LIB file in the top-level directory.
> + *
> + */
> +
> +#include "qemu-file-input-visitor.h"
> +#include "qemu-queue.h"
> +#include "qemu-common.h"
> +#include "qemu-objects.h"
> +#include "hw/hw.h"
> +#include "qerror.h"
> +
> +typedef struct {
> +    size_t elem_count;
> +    size_t elem_size;
> +    size_t pos;
> +} ArrayInfo;
> +
> +typedef struct StackEntry
> +{
> +    enum {
> +        QFIV_ARRAY,
> +        QFIV_LIST,
> +        QFIV_STRUCT,
> +    } type;
> +    ArrayInfo array_info;
> +    QTAILQ_ENTRY(StackEntry) node;
> +} StackEntry;
> +
> +struct QemuFileInputVisitor
> +{
> +    Visitor visitor;
> +    QTAILQ_HEAD(, StackEntry) stack;
> +    QEMUFile *file;
> +};
> +
> +static QemuFileInputVisitor *to_iv(Visitor *v)
> +{
> +    return container_of(v, QemuFileInputVisitor, visitor);
> +}
> +
> +static void qemu_file_input_push(QemuFileInputVisitor *iv, StackEntry *e)
> +{
> +    QTAILQ_INSERT_HEAD(&iv->stack, e, node);
> +}
> +
> +static void qemu_file_input_push_array(QemuFileInputVisitor *iv, ArrayInfo ai)
> +{
> +    StackEntry *e = g_malloc0(sizeof(*e));
> +    e->type = QFIV_ARRAY;
> +    e->array_info = ai;
> +    qemu_file_input_push(iv, e);
> +}
> +
> +static void qemu_file_input_push_list(QemuFileInputVisitor *iv)
> +{
> +    StackEntry *e = g_malloc0(sizeof(*e));
> +    e->type = QFIV_LIST;
> +    qemu_file_input_push(iv, e);
> +}
> +
> +static void qemu_file_input_push_struct(QemuFileInputVisitor *iv)
> +{
> +    StackEntry *e = g_malloc0(sizeof(*e));
> +    e->type = QFIV_STRUCT;
> +    qemu_file_input_push(iv, e);
> +}
> +
> +static void *qemu_file_input_pop(QemuFileInputVisitor *iv)
> +{
> +    StackEntry *e = QTAILQ_FIRST(&iv->stack);
> +    QTAILQ_REMOVE(&iv->stack, e, node);
> +    return e;
> +}
> +
> +static bool qemu_file_input_is_array(QemuFileInputVisitor *iv)
> +{
> +    StackEntry *e = QTAILQ_FIRST(&iv->stack);
> +    return e->type == QFIV_ARRAY;
> +}
> +
> +static bool qemu_file_input_is_list(QemuFileInputVisitor *ov)
> +{
> +    StackEntry *e = QTAILQ_FIRST(&ov->stack);
> +    return e&&  e->type == QFIV_LIST;
> +}
> +
> +static void qemu_file_input_start_struct(Visitor *v, void **obj,
> +                                         const char *kind,
> +                                         const char *name, size_t size,
> +                                         Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +
> +    if (obj&&  *obj == NULL) {
> +        *obj = g_malloc0(size);
> +    }
> +    qemu_file_input_push_struct(iv);
> +}
> +
> +static void qemu_file_input_end_struct(Visitor *v, Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +    StackEntry *e = qemu_file_input_pop(iv);
> +
> +    if (!e || e->type != QFIV_STRUCT) {
> +        error_set(errp, QERR_UNDEFINED_ERROR);
> +        return;
> +    }
> +    g_free(e);

Hello Michael,

I was looking at the code again to see what (private) comment I had made 
the first time I read the code, and now I see additional issues.

The error test above and below will leak the e pointer when the type is 
wrong.

> +}
> +
> +static void qemu_file_input_start_list(Visitor *v, const char *name,
> +                                       Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +    qemu_file_input_push_list(iv);
> +}
> +
> +static GenericList *qemu_file_input_next_list(Visitor *v, GenericList **list,
> +                                           Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +    GenericList *entry;
> +
> +    if (!qemu_file_input_is_list(iv)) {
> +        error_set(errp, QERR_UNDEFINED_ERROR);
> +    }
> +
> +    entry = g_malloc0(sizeof(*entry));
> +    if (*list) {
> +        (*list)->next = entry;
> +    }
> +
> +    *list = entry;
> +    return entry;
> +}
> +
> +static void qemu_file_input_end_list(Visitor *v, Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +    StackEntry *e = qemu_file_input_pop(iv);
> +    if (!e || e->type != QFIV_LIST) {
> +        error_set(errp, QERR_UNDEFINED_ERROR);

and here

> +        return;
> +    }
> +    g_free(e);
> +}
> +
> +static void qemu_file_input_start_array(Visitor *v, void **obj,
> +                                        const char *name,
> +                                        size_t elem_count,
> +                                        size_t elem_size,
> +                                        Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +    ArrayInfo ai = {
> +        .elem_count = elem_count,
> +        .elem_size = elem_size,
> +        .pos = 0
> +    };
> +    if (*obj == NULL) {
> +        *obj = g_malloc0(elem_count * elem_size);
> +    }
> +    qemu_file_input_push_array(iv, ai);
> +}
> +
> +static void qemu_file_input_next_array(Visitor *v, Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +    StackEntry *e = QTAILQ_FIRST(&iv->stack);
> +
> +    if (!qemu_file_input_is_array(iv) ||
> +        e->array_info.pos>= e->array_info.elem_count) {
> +        error_set(errp, QERR_UNDEFINED_ERROR);
> +    }
> +
> +    e->array_info.pos++;
> +}
> +
> +static void qemu_file_input_end_array(Visitor *v, Error **errp)
> +{
> +    QemuFileInputVisitor *iv = to_iv(v);
> +    StackEntry *e = qemu_file_input_pop(iv);
> +    if (!e || e->type != QFIV_ARRAY) {
> +        error_set(errp, QERR_UNDEFINED_ERROR);

and here.

Thanks

Chris

> +        return;
> +    }
> +    g_free(e);
> +}
> +
> +static void qemu_file_input_type_str(Visitor *v, char **obj, const char *name,
> +                                  Error **errp)
> +{
> +    if (obj) {
> +        g_free(*obj);
> +    }
> +}
> +
> +static void qemu_file_input_type_uint8_t(Visitor *v, uint8_t *obj,
> +                                         const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = qemu_get_byte(ov->file);
> +}
> +
> +static void qemu_file_input_type_uint16_t(Visitor *v, uint16_t *obj,
> +                                          const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = qemu_get_byte(ov->file)<<  8;
> +    *obj |= qemu_get_byte(ov->file);
> +}
> +
> +static void qemu_file_input_type_uint32_t(Visitor *v, uint32_t *obj,
> +                                          const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = qemu_get_byte(ov->file)<<  24;
> +    *obj |= qemu_get_byte(ov->file)<<  16;
> +    *obj |= qemu_get_byte(ov->file)<<  8;
> +    *obj |= qemu_get_byte(ov->file);
> +}
> +
> +static void qemu_file_input_type_uint64_t(Visitor *v, uint64_t *obj,
> +                                          const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = (uint64_t)qemu_get_byte(ov->file)<<  56;
> +    *obj |= (uint64_t)qemu_get_byte(ov->file)<<  48;
> +    *obj |= (uint64_t)qemu_get_byte(ov->file)<<  40;
> +    *obj |= (uint64_t)qemu_get_byte(ov->file)<<  32;
> +    *obj |= qemu_get_byte(ov->file)<<  24;
> +    *obj |= qemu_get_byte(ov->file)<<  16;
> +    *obj |= qemu_get_byte(ov->file)<<  8;
> +    *obj |= qemu_get_byte(ov->file);
> +}
> +
> +static void qemu_file_input_type_int8_t(Visitor *v, int8_t *obj,
> +                                        const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = qemu_get_sbyte(ov->file);
> +}
> +
> +static void qemu_file_input_type_int16_t(Visitor *v, int16_t *obj,
> +                                         const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = qemu_get_sbyte(ov->file)<<  8;
> +    *obj |= qemu_get_sbyte(ov->file);
> +}
> +
> +static void qemu_file_input_type_int32_t(Visitor *v, int32_t *obj,
> +                                         const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = qemu_get_sbyte(ov->file)<<  24;
> +    *obj |= qemu_get_sbyte(ov->file)<<  16;
> +    *obj |= qemu_get_sbyte(ov->file)<<  8;
> +    *obj |= qemu_get_sbyte(ov->file);
> +}
> +
> +static void qemu_file_input_type_int64_t(Visitor *v, int64_t *obj,
> +                                         const char *name, Error **errp)
> +{
> +    QemuFileInputVisitor *ov = container_of(v, QemuFileInputVisitor, visitor);
> +    *obj = (int64_t)qemu_get_sbyte(ov->file)<<  56;
> +    *obj |= (int64_t)qemu_get_sbyte(ov->file)<<  48;
> +    *obj |= (int64_t)qemu_get_sbyte(ov->file)<<  40;
> +    *obj |= (int64_t)qemu_get_sbyte(ov->file)<<  32;
> +    *obj |= qemu_get_sbyte(ov->file)<<  24;
> +    *obj |= qemu_get_sbyte(ov->file)<<  16;
> +    *obj |= qemu_get_sbyte(ov->file)<<  8;
> +    *obj |= qemu_get_sbyte(ov->file);
> +}
> +
> +static void qemu_file_input_type_bool(Visitor *v, bool *obj, const char *name,
> +                                     Error **errp)
> +{
> +    uint8_t val;
> +    qemu_file_input_type_uint8_t(v,&val, name, errp);
> +    *obj = val;
> +}
> +
> +static void qemu_file_input_type_number(Visitor *v, double *obj,
> +                                        const char *name, Error **errp)
> +{
> +    uint64_t *val = (uint64_t *)obj;
> +    qemu_file_input_type_uint64_t(v, val, name, errp);
> +}
> +
> +static void qemu_file_input_type_enum(Visitor *v, int *obj,
> +                                      const char *strings[], const char *kind,
> +                                      const char *name, Error **errp)
> +{
> +}
> +
> +Visitor *qemu_file_input_get_visitor(QemuFileInputVisitor *ov)
> +{
> +    return&ov->visitor;
> +}
> +
> +void qemu_file_input_visitor_cleanup(QemuFileInputVisitor *ov)
> +{
> +    g_free(ov);
> +}
> +
> +QemuFileInputVisitor *qemu_file_input_visitor_new(QEMUFile *f)
> +{
> +    QemuFileInputVisitor *v;
> +
> +    v = g_malloc0(sizeof(*v));
> +
> +    v->file = f;
> +
> +    v->visitor.start_struct = qemu_file_input_start_struct;
> +    v->visitor.end_struct = qemu_file_input_end_struct;
> +    v->visitor.start_list = qemu_file_input_start_list;
> +    v->visitor.next_list = qemu_file_input_next_list;
> +    v->visitor.end_list = qemu_file_input_end_list;
> +    v->visitor.start_array = qemu_file_input_start_array;
> +    v->visitor.next_array = qemu_file_input_next_array;
> +    v->visitor.end_array = qemu_file_input_end_array;
> +    v->visitor.type_enum = qemu_file_input_type_enum;
> +    v->visitor.type_int = qemu_file_input_type_int64_t;
> +    v->visitor.type_uint8_t = qemu_file_input_type_uint8_t;
> +    v->visitor.type_uint16_t = qemu_file_input_type_uint16_t;
> +    v->visitor.type_uint32_t = qemu_file_input_type_uint32_t;
> +    v->visitor.type_uint64_t = qemu_file_input_type_uint64_t;
> +    v->visitor.type_int8_t = qemu_file_input_type_int8_t;
> +    v->visitor.type_int16_t = qemu_file_input_type_int16_t;
> +    v->visitor.type_int32_t = qemu_file_input_type_int32_t;
> +    v->visitor.type_int64_t = qemu_file_input_type_int64_t;
> +    v->visitor.type_bool = qemu_file_input_type_bool;
> +    v->visitor.type_str = qemu_file_input_type_str;
> +    v->visitor.type_number = qemu_file_input_type_number;
> +
> +    QTAILQ_INIT(&v->stack);
> +
> +    return v;
> +}
> diff --git a/qapi/qemu-file-input-visitor.h b/qapi/qemu-file-input-visitor.h
> new file mode 100644
> index 0000000..67b5554
> --- /dev/null
> +++ b/qapi/qemu-file-input-visitor.h
> @@ -0,0 +1,26 @@
> +/*
> + * QEMUFile Visitor
> + *
> + * Copyright IBM, Corp. 2011
> + *
> + * Authors:
> + *  Michael Roth<mdroth@linux.vnet.ibm.com>
> + *
> + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
> + * See the COPYING.LIB file in the top-level directory.
> + *
> + */
> +
> +#ifndef QEMU_FILE_INPUT_VISITOR_H
> +#define QEMU_FILE_INPUT_VISITOR_H
> +
> +#include "qapi-visit-core.h"
> +
> +typedef struct QemuFileInputVisitor QemuFileInputVisitor;
> +
> +QemuFileInputVisitor *qemu_file_input_visitor_new(QEMUFile *f);
> +void qemu_file_input_visitor_cleanup(QemuFileInputVisitor *d);
> +
> +Visitor *qemu_file_input_get_visitor(QemuFileInputVisitor *v);
> +
> +#endif

  reply	other threads:[~2011-10-24 23:59 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-19 14:41 [Qemu-devel] [RFC] New Migration Protocol using Visitor Interface Michael Roth
2011-09-19 14:41 ` [Qemu-devel] [RFC 1/8] qapi: add Visitor interfaces for uint*_t and int*_t Michael Roth
2011-09-19 14:41 ` [Qemu-devel] [RFC 2/8] qapi: add QemuFileOutputVisitor Michael Roth
2011-09-19 14:41 ` [Qemu-devel] [RFC 3/8] qapi: add QemuFileInputVisitor Michael Roth
2011-10-24 23:59   ` Chris Krumme [this message]
2011-09-19 14:41 ` [Qemu-devel] [RFC 4/8] savevm: move QEMUFile interfaces into qemu-file.c Michael Roth
2011-09-24  7:23   ` Blue Swirl
2011-09-19 14:41 ` [Qemu-devel] [RFC 5/8] qapi: test cases for QEMUFile input/output visitors Michael Roth
2011-09-19 14:41 ` [Qemu-devel] [RFC 6/8] savevm: add QEMUFile->visitor lookup routines Michael Roth
2011-09-19 14:41 ` [Qemu-devel] [RFC 7/8] cutil: add strocat(), to concat a string to an offset in another Michael Roth
2011-09-20 10:43   ` Paolo Bonzini
2011-09-19 14:41 ` [Qemu-devel] [RFC 8/8] slirp: convert save/load function to visitor interface Michael Roth
2011-09-30 13:39   ` Anthony Liguori
2011-09-30 14:08     ` Michael Roth
2011-10-02 20:21 ` [Qemu-devel] [RFC] New Migration Protocol using Visitor Interface Stefan Berger
2011-10-02 21:08   ` Michael S. Tsirkin
2011-10-03 12:55     ` Anthony Liguori
2011-10-03 13:10       ` Stefan Berger
2011-10-03 13:18         ` Anthony Liguori
2011-10-03 13:30           ` Michael S. Tsirkin
2011-10-03 13:48             ` Anthony Liguori
2011-10-03 14:18               ` Michael S. Tsirkin
2011-10-03 14:56                 ` Anthony Liguori
2011-10-03 15:42                   ` Michael S. Tsirkin
2011-10-03 13:38       ` Michael S. Tsirkin
2011-10-03 13:51         ` Anthony Liguori
2011-10-03 14:41           ` Michael S. Tsirkin
2011-10-03 15:00             ` Anthony Liguori
2011-10-03 15:45               ` Michael S. Tsirkin
2011-10-03 16:05                 ` Anthony Liguori
2011-10-03 16:24                   ` Daniel P. Berrange
2011-10-03 16:51                   ` Michael S. Tsirkin
2011-10-05 11:28               ` Michael S. Tsirkin
2011-10-05 12:46                 ` Anthony Liguori
2011-10-03  6:46 ` Michael S. Tsirkin
2011-10-03 12:51   ` Anthony Liguori
2011-10-03 13:24     ` Michael S. Tsirkin
2011-10-03 13:43       ` Anthony Liguori
2011-10-03 14:11         ` Michael S. Tsirkin
2011-10-03 14:42           ` Anthony Liguori
2011-10-03 15:29             ` Michael S. Tsirkin
2011-10-03 15:44               ` Anthony Liguori
2011-10-03 15:58                 ` Michael S. Tsirkin
2011-10-03 16:02                   ` Anthony Liguori
2011-10-03 14:15         ` Michael S. Tsirkin
2011-10-03 14:55           ` Anthony Liguori
2011-10-03 15:41             ` Michael S. Tsirkin
2011-10-05  2:05         ` Stefan Berger
2011-10-05 12:54           ` Anthony Liguori
2011-10-05 19:06             ` Michael S. Tsirkin

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=4EA5FBE3.1020202@windriver.com \
    --to=chris.krumme@windriver.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).