From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40462) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XCmp3-0006eg-1c for qemu-devel@nongnu.org; Thu, 31 Jul 2014 05:38:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XCmoy-0004wl-11 for qemu-devel@nongnu.org; Thu, 31 Jul 2014 05:38:52 -0400 Received: from mail-we0-x235.google.com ([2a00:1450:400c:c03::235]:44518) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XCmox-0004wg-PI for qemu-devel@nongnu.org; Thu, 31 Jul 2014 05:38:47 -0400 Received: by mail-we0-f181.google.com with SMTP id k48so2513475wev.12 for ; Thu, 31 Jul 2014 02:38:46 -0700 (PDT) Sender: Paolo Bonzini Message-ID: <53DA0EA2.5030207@redhat.com> Date: Thu, 31 Jul 2014 11:38:42 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <94678df77e4485703fc990aacc9c01b823d5cbee.1406784296.git.peter.crosthwaite@xilinx.com> In-Reply-To: <94678df77e4485703fc990aacc9c01b823d5cbee.1406784296.git.peter.crosthwaite@xilinx.com> Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH qom v1 1/2] qom: object_property_add: Add automatic arrayification List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Crosthwaite , qemu-devel@nongnu.org Cc: afaerber@suse.de Il 31/07/2014 07:34, Peter Crosthwaite ha scritto: > If "[*]" is given as the last part of a QOM property name, treat that > as an array property. The added property is given the first available > name, replacing the * with a decimal number counting from 0. > > First add with name "foo[*]" will be "foo[0]". Second "foo[1]" and so > on. > > Signed-off-by: Peter Crosthwaite > --- > Suggest by Paolo and first pass discussion on list about the feature > here: > > https://lists.nongnu.org/archive/html/qemu-devel/2014-06/msg03794.html > > qom/object.c | 22 ++++++++++++++++++++++ > 1 file changed, 22 insertions(+) > > diff --git a/qom/object.c b/qom/object.c > index 0e8267b..c869e8e 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -739,6 +739,28 @@ object_property_add(Object *obj, const char *name, const char *type, > { > ObjectProperty *prop; > > + if (strlen(name) >= 3 && !strncmp(name + strlen(name) - 3, "[*]", 3)) { Please cache strlen in a variable, and use memcmp(..., "[*]", 4). strncmp is used often to compare just a prefix, and it's not obvious that you're doing something else. Otherwise looks good, thanks! Paolo > + int i; > + ObjectProperty *ret; > + char *name_no_array = g_strdup(name); > + > + name_no_array[strlen(name) - 3] = '\0'; > + for (i = 0; ; ++i) { > + char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); > + Error *local_err = NULL; > + ret = object_property_add(obj, full_name, type, get, set, > + release, opaque, &local_err); > + > + g_free(full_name); > + if (!local_err) { > + break; > + } > + error_free(local_err); > + } > + g_free(name_no_array); > + return ret; > + } > + > QTAILQ_FOREACH(prop, &obj->properties, node) { > if (strcmp(prop->name, name) == 0) { > error_setg(errp, "attempt to add duplicate property '%s'" >