Patchwork [2/9] qom: add object_property_add_unnamed_child

login
register
mail settings
Submitter Michael Roth
Date May 3, 2013, 4:03 p.m.
Message ID <1367597032-28934-3-git-send-email-mdroth@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/241337/
State New
Headers show

Comments

Michael Roth - May 3, 2013, 4:03 p.m.
This interface allows us to add a child property without specifying a
name. Instead, a unique name is created and passed back after adding
the property.

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 include/qom/object.h |   16 ++++++++++++++++
 qom/object.c         |   25 +++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
Paolo Bonzini - May 6, 2013, 7:44 a.m.
Il 03/05/2013 18:03, Michael Roth ha scritto:
> This interface allows us to add a child property without specifying a
> name. Instead, a unique name is created and passed back after adding
> the property.
> 
> Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
> ---
>  include/qom/object.h |   16 ++++++++++++++++
>  qom/object.c         |   25 +++++++++++++++++++++++++
>  2 files changed, 41 insertions(+)
> 
> diff --git a/include/qom/object.h b/include/qom/object.h
> index 86f1e2e..ca0fce8 100644
> --- a/include/qom/object.h
> +++ b/include/qom/object.h
> @@ -1041,6 +1041,22 @@ void object_property_add_child(Object *obj, const char *name,
>                                 Object *child, struct Error **errp);
>  
>  /**
> + * object_property_add_unnamed_child:
> + *
> + * @obj: the object to add a property to
> + * @name: the name of the property
> + * @child: the child object
> + * @errp: if an error occurs, a pointer to an area to store the area
> + *
> + * Same as object_property_add_child, but will allocate a unique name to
> + * identify the child property.
> + *
> + * Returns: The name assigned to the child property, or NULL on failure.
> + */
> +char *object_property_add_unnamed_child(Object *obj, Object *child,
> +                                        struct Error **errp);
> +
> +/**
>   * object_property_add_link:
>   * @obj: the object to add a property to
>   * @name: the name of the property
> diff --git a/qom/object.c b/qom/object.c
> index c932f64..229a9a7 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -926,6 +926,31 @@ static void object_finalize_child_property(Object *obj, const char *name,
>      object_unref(child);
>  }
>  
> +char *object_property_add_unnamed_child(Object *obj, Object *child, Error **errp)
> +{
> +    int idx = 0;
> +    bool next_idx_found = false;
> +    char name[64];
> +    ObjectProperty *prop;
> +
> +    while (!next_idx_found) {
> +        sprintf(name, "unnamed[%d]", idx);
> +        QTAILQ_FOREACH(prop, &obj->properties, node) {
> +            if (strcmp(name, prop->name) == 0) {
> +                idx++;
> +                break;
> +            }
> +        }
> +        if (!prop) {
> +            next_idx_found = true;
> +        }
> +    }
> +
> +    object_property_add_child(obj, name, child, errp);
> +
> +    return error_is_set(errp) ? NULL : g_strdup(name);
> +}

This is O(n^3) for adding N children.  O(n^2) would be not-that-great
but fine; can you take the occasion to convert the properties list to a
hashtable?

Paolo

> +
>  void object_property_add_child(Object *obj, const char *name,
>                                 Object *child, Error **errp)
>  {
>
Michael Roth - May 6, 2013, 6:48 p.m.
On Mon, May 06, 2013 at 09:44:13AM +0200, Paolo Bonzini wrote:
> Il 03/05/2013 18:03, Michael Roth ha scritto:
> > This interface allows us to add a child property without specifying a
> > name. Instead, a unique name is created and passed back after adding
> > the property.
> > 
> > Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
> > ---
> >  include/qom/object.h |   16 ++++++++++++++++
> >  qom/object.c         |   25 +++++++++++++++++++++++++
> >  2 files changed, 41 insertions(+)
> > 
> > diff --git a/include/qom/object.h b/include/qom/object.h
> > index 86f1e2e..ca0fce8 100644
> > --- a/include/qom/object.h
> > +++ b/include/qom/object.h
> > @@ -1041,6 +1041,22 @@ void object_property_add_child(Object *obj, const char *name,
> >                                 Object *child, struct Error **errp);
> >  
> >  /**
> > + * object_property_add_unnamed_child:
> > + *
> > + * @obj: the object to add a property to
> > + * @name: the name of the property
> > + * @child: the child object
> > + * @errp: if an error occurs, a pointer to an area to store the area
> > + *
> > + * Same as object_property_add_child, but will allocate a unique name to
> > + * identify the child property.
> > + *
> > + * Returns: The name assigned to the child property, or NULL on failure.
> > + */
> > +char *object_property_add_unnamed_child(Object *obj, Object *child,
> > +                                        struct Error **errp);
> > +
> > +/**
> >   * object_property_add_link:
> >   * @obj: the object to add a property to
> >   * @name: the name of the property
> > diff --git a/qom/object.c b/qom/object.c
> > index c932f64..229a9a7 100644
> > --- a/qom/object.c
> > +++ b/qom/object.c
> > @@ -926,6 +926,31 @@ static void object_finalize_child_property(Object *obj, const char *name,
> >      object_unref(child);
> >  }
> >  
> > +char *object_property_add_unnamed_child(Object *obj, Object *child, Error **errp)
> > +{
> > +    int idx = 0;
> > +    bool next_idx_found = false;
> > +    char name[64];
> > +    ObjectProperty *prop;
> > +
> > +    while (!next_idx_found) {
> > +        sprintf(name, "unnamed[%d]", idx);
> > +        QTAILQ_FOREACH(prop, &obj->properties, node) {
> > +            if (strcmp(name, prop->name) == 0) {
> > +                idx++;
> > +                break;
> > +            }
> > +        }
> > +        if (!prop) {
> > +            next_idx_found = true;
> > +        }
> > +    }
> > +
> > +    object_property_add_child(obj, name, child, errp);
> > +
> > +    return error_is_set(errp) ? NULL : g_strdup(name);
> > +}
> 
> This is O(n^3) for adding N children.  O(n^2) would be not-that-great
> but fine; can you take the occasion to convert the properties list to a
> hashtable?

Sure, I'll look into it.

> 
> Paolo
> 
> > +
> >  void object_property_add_child(Object *obj, const char *name,
> >                                 Object *child, Error **errp)
> >  {
> > 
>
Stefan Hajnoczi - May 8, 2013, 11:33 a.m.
On Mon, May 06, 2013 at 01:48:34PM -0500, mdroth wrote:
> On Mon, May 06, 2013 at 09:44:13AM +0200, Paolo Bonzini wrote:
> > Il 03/05/2013 18:03, Michael Roth ha scritto:
> > > This interface allows us to add a child property without specifying a
> > > name. Instead, a unique name is created and passed back after adding
> > > the property.
> > > 
> > > Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
> > > ---
> > >  include/qom/object.h |   16 ++++++++++++++++
> > >  qom/object.c         |   25 +++++++++++++++++++++++++
> > >  2 files changed, 41 insertions(+)
> > > 
> > > diff --git a/include/qom/object.h b/include/qom/object.h
> > > index 86f1e2e..ca0fce8 100644
> > > --- a/include/qom/object.h
> > > +++ b/include/qom/object.h
> > > @@ -1041,6 +1041,22 @@ void object_property_add_child(Object *obj, const char *name,
> > >                                 Object *child, struct Error **errp);
> > >  
> > >  /**
> > > + * object_property_add_unnamed_child:
> > > + *
> > > + * @obj: the object to add a property to
> > > + * @name: the name of the property
> > > + * @child: the child object
> > > + * @errp: if an error occurs, a pointer to an area to store the area
> > > + *
> > > + * Same as object_property_add_child, but will allocate a unique name to
> > > + * identify the child property.
> > > + *
> > > + * Returns: The name assigned to the child property, or NULL on failure.
> > > + */
> > > +char *object_property_add_unnamed_child(Object *obj, Object *child,
> > > +                                        struct Error **errp);
> > > +
> > > +/**
> > >   * object_property_add_link:
> > >   * @obj: the object to add a property to
> > >   * @name: the name of the property
> > > diff --git a/qom/object.c b/qom/object.c
> > > index c932f64..229a9a7 100644
> > > --- a/qom/object.c
> > > +++ b/qom/object.c
> > > @@ -926,6 +926,31 @@ static void object_finalize_child_property(Object *obj, const char *name,
> > >      object_unref(child);
> > >  }
> > >  
> > > +char *object_property_add_unnamed_child(Object *obj, Object *child, Error **errp)
> > > +{
> > > +    int idx = 0;
> > > +    bool next_idx_found = false;
> > > +    char name[64];
> > > +    ObjectProperty *prop;
> > > +
> > > +    while (!next_idx_found) {
> > > +        sprintf(name, "unnamed[%d]", idx);
> > > +        QTAILQ_FOREACH(prop, &obj->properties, node) {
> > > +            if (strcmp(name, prop->name) == 0) {
> > > +                idx++;
> > > +                break;
> > > +            }
> > > +        }
> > > +        if (!prop) {
> > > +            next_idx_found = true;
> > > +        }
> > > +    }
> > > +
> > > +    object_property_add_child(obj, name, child, errp);
> > > +
> > > +    return error_is_set(errp) ? NULL : g_strdup(name);
> > > +}
> > 
> > This is O(n^3) for adding N children.  O(n^2) would be not-that-great
> > but fine; can you take the occasion to convert the properties list to a
> > hashtable?
> 
> Sure, I'll look into it.

Given that we already have the child pointer, perhaps just use the
uintptr_t child memory address as a unique name.  It's guaranteed to be
unique unless you add the same child twice.

Stefan

Patch

diff --git a/include/qom/object.h b/include/qom/object.h
index 86f1e2e..ca0fce8 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1041,6 +1041,22 @@  void object_property_add_child(Object *obj, const char *name,
                                Object *child, struct Error **errp);
 
 /**
+ * object_property_add_unnamed_child:
+ *
+ * @obj: the object to add a property to
+ * @name: the name of the property
+ * @child: the child object
+ * @errp: if an error occurs, a pointer to an area to store the area
+ *
+ * Same as object_property_add_child, but will allocate a unique name to
+ * identify the child property.
+ *
+ * Returns: The name assigned to the child property, or NULL on failure.
+ */
+char *object_property_add_unnamed_child(Object *obj, Object *child,
+                                        struct Error **errp);
+
+/**
  * object_property_add_link:
  * @obj: the object to add a property to
  * @name: the name of the property
diff --git a/qom/object.c b/qom/object.c
index c932f64..229a9a7 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -926,6 +926,31 @@  static void object_finalize_child_property(Object *obj, const char *name,
     object_unref(child);
 }
 
+char *object_property_add_unnamed_child(Object *obj, Object *child, Error **errp)
+{
+    int idx = 0;
+    bool next_idx_found = false;
+    char name[64];
+    ObjectProperty *prop;
+
+    while (!next_idx_found) {
+        sprintf(name, "unnamed[%d]", idx);
+        QTAILQ_FOREACH(prop, &obj->properties, node) {
+            if (strcmp(name, prop->name) == 0) {
+                idx++;
+                break;
+            }
+        }
+        if (!prop) {
+            next_idx_found = true;
+        }
+    }
+
+    object_property_add_child(obj, name, child, errp);
+
+    return error_is_set(errp) ? NULL : g_strdup(name);
+}
+
 void object_property_add_child(Object *obj, const char *name,
                                Object *child, Error **errp)
 {