diff mbox

[2/3] add object_property_add_alias

Message ID 20140604175446.484888751@amt.cnet
State New
Headers show

Commit Message

Marcelo Tosatti June 4, 2014, 5:52 p.m. UTC
Allowing addition of a link without keeping pointer-to-pointer.

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

---
 include/qom/object.h |   13 +++++++++
 qom/object.c         |   73 +++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 72 insertions(+), 14 deletions(-)

Comments

Paolo Bonzini June 5, 2014, 11:09 a.m. UTC | #1
Il 04/06/2014 19:52, mtosatti@redhat.com ha scritto:
> Allowing addition of a link without keeping pointer-to-pointer.
>
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
>
> ---
>  include/qom/object.h |   13 +++++++++
>  qom/object.c         |   73 +++++++++++++++++++++++++++++++++++++++++----------
>  2 files changed, 72 insertions(+), 14 deletions(-)
>
> Index: qemu/include/qom/object.h
> ===================================================================
> --- qemu.orig/include/qom/object.h	2014-06-02 23:12:51.875693325 -0300
> +++ qemu/include/qom/object.h	2014-06-02 23:14:13.045432426 -0300
> @@ -1073,6 +1073,19 @@
>  } ObjectPropertyLinkFlags;
>
>  /**
> + * object_property_add_alias:
> + * @obj: the object to add a property to
> + * @name: the name of the property
> + * @alias: the alias object
> + * @errp: if an error occurs, a pointer to an area to store the area
> + *
> + * Add a link under obj, named name, pointing to alias.
> + *
> + */
> +void object_property_add_alias(Object *obj, const char *name,
> +                               Object *alias, Error **errp);
> +
> +/**
>   * object_property_allow_set_link:
>   *
>   * The default implementation of the object_property_add_link() check()
> Index: qemu/qom/object.c
> ===================================================================
> --- qemu.orig/qom/object.c	2014-06-02 23:12:51.875693325 -0300
> +++ qemu/qom/object.c	2014-06-02 23:14:13.046432423 -0300
> @@ -1023,27 +1023,71 @@
>      g_free(type);
>  }
>
> +typedef struct {
> +    Object *child;
> +    Object **childp;

These field names are ugly... not your fault, but perhaps

     Object **linkp;
     Object *alias_dest;

would be better.  It would also avoid the mistake below:

> @@ -1096,7 +1140,7 @@
>  {
>      Error *local_err = NULL;
>      LinkProperty *prop = opaque;
> -    Object **child = prop->child;
> +    Object **child = prop->childp;
>      Object *old_target = *child;
>      Object *new_target = NULL;
>      char *path = NULL;

This is object_set_link_property.  It writes *child but not prop->child, 
and subsequent calls to object_get_link_property incorrect.

However, since a similar need arose recently in one of Peter 
Crosthwaite's patches, let's add a generic resolve mechanism.  I'll post 
a short series in a second, as soon as I finish testing it.

Paolo

> @@ -1133,8 +1177,8 @@
>  {
>      LinkProperty *prop = opaque;
>
> -    if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
> -        object_unref(*prop->child);
> +    if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && prop->child) {
> +        object_unref(prop->child);
>      }
>      g_free(prop);
>  }
> @@ -1150,7 +1194,8 @@
>      LinkProperty *prop = g_malloc(sizeof(*prop));
>      gchar *full_type;
>
> -    prop->child = child;
> +    prop->childp = child;
> +    prop->child = *child;
>      prop->check = check;
>      prop->flags = flags;
>
> @@ -1227,7 +1272,7 @@
>
>      if (object_property_is_link(prop)) {
>          LinkProperty *lprop = prop->opaque;
> -        return *lprop->child;
> +        return lprop->child;
>      } else if (object_property_is_child(prop)) {
>          return prop->opaque;
>      } else {
>
>
diff mbox

Patch

Index: qemu/include/qom/object.h
===================================================================
--- qemu.orig/include/qom/object.h	2014-06-02 23:12:51.875693325 -0300
+++ qemu/include/qom/object.h	2014-06-02 23:14:13.045432426 -0300
@@ -1073,6 +1073,19 @@ 
 } ObjectPropertyLinkFlags;
 
 /**
+ * object_property_add_alias:
+ * @obj: the object to add a property to
+ * @name: the name of the property
+ * @alias: the alias object
+ * @errp: if an error occurs, a pointer to an area to store the area
+ *
+ * Add a link under obj, named name, pointing to alias.
+ *
+ */
+void object_property_add_alias(Object *obj, const char *name,
+                               Object *alias, Error **errp);
+
+/**
  * object_property_allow_set_link:
  *
  * The default implementation of the object_property_add_link() check()
Index: qemu/qom/object.c
===================================================================
--- qemu.orig/qom/object.c	2014-06-02 23:12:51.875693325 -0300
+++ qemu/qom/object.c	2014-06-02 23:14:13.046432423 -0300
@@ -1023,27 +1023,71 @@ 
     g_free(type);
 }
 
+typedef struct {
+    Object *child;
+    Object **childp;
+    void (*check)(Object *, const char *, Object *, Error **);
+    ObjectPropertyLinkFlags flags;
+} LinkProperty;
+
+static void object_get_alias_property(Object *obj, Visitor *v, void *opaque,
+                                      const char *name, Error **errp)
+{
+    LinkProperty *prop = opaque;
+    Object *child = prop->child;
+    gchar *path;
+
+    path = object_get_canonical_path(child);
+    visit_type_str(v, &path, name, errp);
+    g_free(path);
+}
+
+static void object_release_alias_property(Object *obj, const char *name,
+                                         void *opaque)
+{
+    LinkProperty *prop = opaque;
+
+    g_free(prop);
+}
+
+void object_property_add_alias(Object *obj, const char *name,
+                               Object *alias, Error **errp)
+{
+    Error *local_err = NULL;
+    gchar *type;
+    LinkProperty *prop = g_malloc(sizeof(*prop));
+
+    type = g_strdup_printf("link<%s>", object_get_typename(OBJECT(alias)));
+
+    prop->child = alias;
+    prop->check = NULL;
+    prop->flags = 0;
+
+    object_property_add(obj, name, type, object_get_alias_property, NULL,
+                        object_release_alias_property, prop, &local_err);
+    if (local_err) {
+        g_free(prop);
+        error_propagate(errp, local_err);
+    }
+
+    g_free(type);
+}
+
 void object_property_allow_set_link(Object *obj, const char *name,
                                     Object *val, Error **errp)
 {
     /* Allow the link to be set, always */
 }
 
-typedef struct {
-    Object **child;
-    void (*check)(Object *, const char *, Object *, Error **);
-    ObjectPropertyLinkFlags flags;
-} LinkProperty;
-
 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
                                      const char *name, Error **errp)
 {
     LinkProperty *lprop = opaque;
-    Object **child = lprop->child;
+    Object *child = lprop->child;
     gchar *path;
 
-    if (*child) {
-        path = object_get_canonical_path(*child);
+    if (child) {
+        path = object_get_canonical_path(child);
         visit_type_str(v, &path, name, errp);
         g_free(path);
     } else {
@@ -1096,7 +1140,7 @@ 
 {
     Error *local_err = NULL;
     LinkProperty *prop = opaque;
-    Object **child = prop->child;
+    Object **child = prop->childp;
     Object *old_target = *child;
     Object *new_target = NULL;
     char *path = NULL;
@@ -1133,8 +1177,8 @@ 
 {
     LinkProperty *prop = opaque;
 
-    if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
-        object_unref(*prop->child);
+    if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && prop->child) {
+        object_unref(prop->child);
     }
     g_free(prop);
 }
@@ -1150,7 +1194,8 @@ 
     LinkProperty *prop = g_malloc(sizeof(*prop));
     gchar *full_type;
 
-    prop->child = child;
+    prop->childp = child;
+    prop->child = *child;
     prop->check = check;
     prop->flags = flags;
 
@@ -1227,7 +1272,7 @@ 
 
     if (object_property_is_link(prop)) {
         LinkProperty *lprop = prop->opaque;
-        return *lprop->child;
+        return lprop->child;
     } else if (object_property_is_child(prop)) {
         return prop->opaque;
     } else {