From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:37965) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RuPc8-0006pw-GV for qemu-devel@nongnu.org; Mon, 06 Feb 2012 09:32:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RuPc4-0003Pc-3B for qemu-devel@nongnu.org; Mon, 06 Feb 2012 09:32:16 -0500 Received: from mail-pw0-f45.google.com ([209.85.160.45]:39971) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RuPc3-0003PP-UY for qemu-devel@nongnu.org; Mon, 06 Feb 2012 09:32:12 -0500 Received: by pbaa11 with SMTP id a11so6793002pba.4 for ; Mon, 06 Feb 2012 06:32:11 -0800 (PST) Message-ID: <4F2FE467.6070308@codemonkey.ws> Date: Mon, 06 Feb 2012 08:32:07 -0600 From: Anthony Liguori MIME-Version: 1.0 References: <1328342577-25732-1-git-send-email-pbonzini@redhat.com> <1328342577-25732-17-git-send-email-pbonzini@redhat.com> In-Reply-To: <1328342577-25732-17-git-send-email-pbonzini@redhat.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2 16/27] qdev: remove print/parse methods from LostTickPolicy properties List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: qemu-devel@nongnu.org On 02/04/2012 02:02 AM, Paolo Bonzini wrote: > Also generalize the code so that we can have more enum properties > in the future. > > Signed-off-by: Paolo Bonzini Reviewed-by: Anthony Liguori Regards, Anthony Liguori > --- > hw/qdev-properties.c | 62 +++++++++++++++++++++++++------------------------- > hw/qdev.h | 1 + > qemu-common.h | 1 + > 3 files changed, 33 insertions(+), 31 deletions(-) > > diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c > index 1fc77b5..dea287a 100644 > --- a/hw/qdev-properties.c > +++ b/hw/qdev-properties.c > @@ -893,50 +893,50 @@ PropertyInfo qdev_prop_macaddr = { > > /* --- lost tick policy --- */ > > -static const struct { > - const char *name; > - LostTickPolicy code; > -} lost_tick_policy_table[] = { > - { .name = "discard", .code = LOST_TICK_DISCARD }, > - { .name = "delay", .code = LOST_TICK_DELAY }, > - { .name = "merge", .code = LOST_TICK_MERGE }, > - { .name = "slew", .code = LOST_TICK_SLEW }, > +static const char *lost_tick_policy_table[LOST_TICK_MAX+1] = { > + [LOST_TICK_DISCARD] = "discard", > + [LOST_TICK_DELAY] = "delay", > + [LOST_TICK_MERGE] = "merge", > + [LOST_TICK_SLEW] = "slew", > + [LOST_TICK_MAX] = NULL, > }; > > -static int parse_lost_tick_policy(DeviceState *dev, Property *prop, > - const char *str) > +QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int)); > + > +static void get_enum(Object *obj, Visitor *v, void *opaque, > + const char *name, Error **errp) > { > - LostTickPolicy *ptr = qdev_get_prop_ptr(dev, prop); > - int i; > + DeviceState *dev = DEVICE(obj); > + Property *prop = opaque; > + int *ptr = qdev_get_prop_ptr(dev, prop); > > - for (i = 0; i< ARRAY_SIZE(lost_tick_policy_table); i++) { > - if (!strcasecmp(str, lost_tick_policy_table[i].name)) { > - *ptr = lost_tick_policy_table[i].code; > - break; > - } > - } > - if (i == ARRAY_SIZE(lost_tick_policy_table)) { > - return -EINVAL; > - } > - return 0; > + visit_type_enum(v, ptr, prop->info->enum_table, > + prop->info->name, prop->name, errp); > } > > -static int print_lost_tick_policy(DeviceState *dev, Property *prop, char *dest, > - size_t len) > +static void set_enum(Object *obj, Visitor *v, void *opaque, > + const char *name, Error **errp) > { > - LostTickPolicy *ptr = qdev_get_prop_ptr(dev, prop); > + DeviceState *dev = DEVICE(obj); > + Property *prop = opaque; > + int *ptr = qdev_get_prop_ptr(dev, prop); > + > + if (dev->state != DEV_STATE_CREATED) { > + error_set(errp, QERR_PERMISSION_DENIED); > + return; > + } > > - return snprintf(dest, len, "%s", lost_tick_policy_table[*ptr].name); > + visit_type_enum(v, ptr, prop->info->enum_table, > + prop->info->name, prop->name, errp); > } > > PropertyInfo qdev_prop_losttickpolicy = { > - .name = "lost_tick_policy", > + .name = "LostTickPolicy", > .type = PROP_TYPE_LOSTTICKPOLICY, > .size = sizeof(LostTickPolicy), > - .parse = parse_lost_tick_policy, > - .print = print_lost_tick_policy, > - .get = get_generic, > - .set = set_generic, > + .enum_table = lost_tick_policy_table, > + .get = get_enum, > + .set = set_enum, > }; > > /* --- pci address --- */ > diff --git a/hw/qdev.h b/hw/qdev.h > index ab53273..c31dc4e 100644 > --- a/hw/qdev.h > +++ b/hw/qdev.h > @@ -140,6 +140,7 @@ struct PropertyInfo { > const char *legacy_name; > size_t size; > enum PropertyType type; > + const char **enum_table; > int64_t min; > int64_t max; > int (*parse)(DeviceState *dev, Property *prop, const char *str); > diff --git a/qemu-common.h b/qemu-common.h > index 8b69a9e..9b997f8 100644 > --- a/qemu-common.h > +++ b/qemu-common.h > @@ -255,6 +255,7 @@ typedef enum LostTickPolicy { > LOST_TICK_DELAY, > LOST_TICK_MERGE, > LOST_TICK_SLEW, > + LOST_TICK_MAX > } LostTickPolicy; > > void tcg_exec_init(unsigned long tb_size);