All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: Anthony Liguori <aliguori@us.ibm.com>
Subject: [Qemu-devel] [PATCH 05/21] plug: add Plug property type
Date: Sun, 24 Jul 2011 20:44:37 -0500	[thread overview]
Message-ID: <1311558293-5855-6-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1311558293-5855-1-git-send-email-aliguori@us.ibm.com>

Plug properties types allow composition within the object model.  A plug is an
object that is directly created from the current object, usually using the same
memory as the current object.

Realize state is propagated to any plugs assocatied with the object.  Lifecycle
is also propagated such that when a plug is added, its automatically initialized
and the plug is finalized when the parent object is finalized.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 include/qemu/plug.h |    4 ++
 qom/plug.c          |  104 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/include/qemu/plug.h b/include/qemu/plug.h
index f6948a0..fdefa04 100644
--- a/include/qemu/plug.h
+++ b/include/qemu/plug.h
@@ -68,6 +68,10 @@ bool plug_get_realized(Plug *plug);
 void plug_realize_all(Plug *plug);
 void plug_unrealize_all(Plug *plug);
 
+void plug_add_property_plug(Plug *plug, Plug *value, const char *typename, const char *name, ...);
+
+Plug *plug_get_property_plug(Plug *plug, Error **errp, const char *name, ...);
+
 #include "qemu/plug-proptypes.h"
 
 #endif
diff --git a/qom/plug.c b/qom/plug.c
index 4a5f2fb..725cac2 100644
--- a/qom/plug.c
+++ b/qom/plug.c
@@ -159,12 +159,115 @@ bool plug_get_realized(Plug *plug)
     return plug->realized;
 }
 
+static char *plug_get_property_str(Plug *plug, const char *name, Error **errp)
+{
+    StringOutputVisitor sov;
+
+    string_output_visitor_init(&sov);
+    plug_get_property(plug, name, &sov.parent, errp);
+
+    return qemu_strdup(sov.value);
+}
+
+static void plug_propagate_realized(Plug *plug, const char *name,
+                                    const char *typename, int flags,
+                                    void *opaque)
+{
+    if (strstart(typename, "plug<", NULL)) {
+        char *child_name;
+        Plug *child_plug;
+
+        child_name = plug_get_property_str(plug, name, NULL);
+        child_plug = PLUG(type_find_by_id(child_name));
+
+        plug_set_realized(child_plug, plug_get_realized(plug));
+
+        qemu_free(child_name);
+    }
+}
+
+typedef struct PlugData
+{
+    const char *typename;
+    Plug *value;
+} PlugData;
+
+static void plug_get_property__plug(Plug *plug, const char *name, Visitor *v, void *opaque, Error **errp)
+{
+    PlugData *data = opaque;
+    char *value;
+
+    value = (char *)TYPE_INSTANCE(data->value)->id;
+    visit_type_str(v, &value, name, errp);
+}
+
+static void plug_del_property__plug(Plug *plug, const char *name, void *opaque)
+{
+    PlugData *data = opaque;
+
+    type_finalize(data->value);
+    qemu_free(data);
+}
+
+void plug_add_property_plug(Plug *plug, Plug *value, const char *typename,
+                            const char *name, ...)
+{
+    PlugData *data = qemu_mallocz(sizeof(*data));
+    char fullid[MAX_NAME];
+    char fulltype[MAX_TYPENAME];
+    size_t off;
+    va_list ap;
+
+    data->typename = typename;
+    data->value = value;
+
+    snprintf(fulltype, sizeof(fulltype), "plug<%s>", typename);
+
+    va_start(ap, name);
+    off = snprintf(fullid, sizeof(fullid), "%s::",
+                   type_get_id(TYPE_INSTANCE(plug)));
+    vsnprintf(&fullid[off], sizeof(fullid) - off, name, ap);
+    va_end(ap);
+
+    type_initialize(plug, typename, fullid);
+
+    plug_add_property_full(plug, name,
+                           plug_get_property__plug,
+                           NULL,
+                           plug_del_property__plug,
+                           data, fulltype, PROP_F_READ);
+}
+
+Plug *plug_get_property_plug(Plug *plug, Error **errp, const char *name, ...)
+{
+    char fullname[MAX_NAME];
+    char *plugname;
+    Plug *value;
+    va_list ap;
+
+    va_start(ap, name);
+    vsnprintf(fullname, sizeof(fullname), name, ap);
+    va_end(ap);
+
+    plugname = plug_get_property_str(plug, fullname, errp);
+    if (error_is_set(errp)) {
+        return NULL;
+    }
+
+    value = PLUG(type_find_by_id(plugname));
+
+    qemu_free(plugname);
+
+    return value;
+}
+
 void plug_realize_all(Plug *plug)
 {
     /* This doesn't loop infinitely because the callbacks are only called when
      * the state changes. */
     plug_set_realized(plug, true);
     plug_lock_all_properties(plug);
+    plug_foreach_property(plug, plug_propagate_realized, NULL);
 }
 
 void plug_unrealize_all(Plug *plug)
@@ -173,6 +276,7 @@ void plug_unrealize_all(Plug *plug)
      * the state changes. */
     plug_set_realized(plug, false);
     plug_unlock_all_properties(plug);
+    plug_foreach_property(plug, plug_propagate_realized, NULL);
 }
 
 static void plug_class_initfn(TypeClass *base_class)
-- 
1.7.4.1

  parent reply	other threads:[~2011-07-25  1:45 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-25  1:44 [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 01/21] qom: add make infrastructure Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 02/21] qom: convert QAPI to use Qconfig build system Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 03/21] qom: Add core type system Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 04/21] qom: add Plug class Anthony Liguori
2011-07-25  1:44 ` Anthony Liguori [this message]
2011-07-25  1:44 ` [Qemu-devel] [PATCH 06/21] plug: add socket property type Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 07/21] plug: add generated property types Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 08/21] qom: add plug_create QMP command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 09/21] qom: add plug_list " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 10/21] qom: add plug_get " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 11/21] qom: add plug_set " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 12/21] qom: add plug_list_props " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 13/21] qom: add plug_destroy command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 14/21] qom: add example qsh command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 15/21] qom: add Device class Anthony Liguori
2011-07-27 15:10   ` Peter Maydell
2011-07-27 16:07     ` Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 16/21] qom-devices: add a Pin class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 17/21] qom: add CharDriver class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 18/21] qom-chrdrv: add memory character driver Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 19/21] qom-chrdrv: add Socket base class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 20/21] qom-chrdrv: add TCPServer class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 21/21] qom-chrdrv: add UnixServer Anthony Liguori
2011-07-25 11:21 ` [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Kevin Wolf
2011-07-25 12:45   ` Anthony Liguori
2011-07-25 13:08     ` Kevin Wolf
2011-07-25 13:10       ` Anthony Liguori
2011-07-26 12:59 ` Paolo Bonzini
2011-07-26 14:02   ` Anthony Liguori
2011-07-26 14:35     ` Paolo Bonzini
2011-07-26 15:34       ` Anthony Liguori
2011-07-26 18:26         ` Paolo Bonzini
2011-07-26 19:23           ` Anthony Liguori
2011-07-27  8:55             ` Paolo Bonzini
2011-07-27 12:48               ` Anthony Liguori
2011-07-27 15:33                 ` Paolo Bonzini
2011-07-27 16:19                   ` Anthony Liguori
2011-07-27 16:28                   ` Anthony Liguori
2011-07-27 18:51                     ` Paolo Bonzini
2011-07-27 20:01                       ` Anthony Liguori
2011-07-28  7:36                         ` Paolo Bonzini
2011-07-28 12:46                           ` Anthony Liguori
2011-07-28 13:50                             ` Paolo Bonzini
2011-07-28 14:03                               ` Anthony Liguori
2011-07-28 14:41                                 ` Paolo Bonzini
2011-07-28 15:04                                   ` Anthony Liguori
2011-07-28 15:47                                     ` Paolo Bonzini
2011-07-28 17:59                                       ` Anthony Liguori
2011-07-29  7:19                                         ` Paolo Bonzini
2011-07-27 21:33     ` Peter Maydell
2011-07-27 22:31       ` Anthony Liguori

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=1311558293-5855-6-git-send-email-aliguori@us.ibm.com \
    --to=aliguori@us.ibm.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.