From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Cc: patches@linaro.org, Paolo Bonzini <pbonzini@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: [Qemu-devel] [PATCH 1/2] hw/sysbus.h: New sysbus_init_child() helper function
Date: Fri, 16 Feb 2018 13:45:15 +0000 [thread overview]
Message-ID: <20180216134516.6269-2-peter.maydell@linaro.org> (raw)
In-Reply-To: <20180216134516.6269-1-peter.maydell@linaro.org>
If you're using the increasingly-common QOM style of
having container devices create their child objects
in-place, you end up with a lot of boilerplate in the
container's init function:
object_initialize() to init the child
object_property_add_child() to make the child a
child of the parent
qdev_set_parent_bus() to put the child on the
sysbus default bus
If you forget the second of these then things sort of
work but trying to add a child to the child will segfault;
if you forget the third then the device won't get reset.
Provide a helper function sysbus_init_child() which
does all these things for you, reducing the boilerplate
and making it harder to get wrong.
Code that used to look like this:
object_initialize(&s->ic, sizeof(s->ic), TYPE_BCM2835_IC);
object_property_add_child(obj, "ic", OBJECT(&s->ic), NULL);
qdev_set_parent_bus(DEVICE(&s->ic), sysbus_get_default());
can now look like this:
sysbus_init_child(obj, "ic", &s->ic, sizeof(s->ic), TYPE_BCM2835_IC);
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
include/hw/sysbus.h | 12 ++++++++++++
hw/core/sysbus.c | 14 ++++++++++++++
2 files changed, 26 insertions(+)
diff --git a/include/hw/sysbus.h b/include/hw/sysbus.h
index e88bb6dae0..6095e24e86 100644
--- a/include/hw/sysbus.h
+++ b/include/hw/sysbus.h
@@ -118,4 +118,16 @@ static inline DeviceState *sysbus_try_create_simple(const char *name,
return sysbus_try_create_varargs(name, addr, irq, NULL);
}
+/**
+ * sysbus_init_child: in-place initialize and parent a SysBus device
+ * @parent: object to parent the device to
+ * @childname: name to use as the child property name
+ * @child: child object
+ * @childsize: size of the storage for the object
+ * @childtype: type name of the child
+ */
+void sysbus_init_child(Object *parent, const char *childname,
+ void *child, size_t childsize,
+ const char *childtype);
+
#endif /* HW_SYSBUS_H */
diff --git a/hw/core/sysbus.c b/hw/core/sysbus.c
index 5d0887f499..acfa52dc68 100644
--- a/hw/core/sysbus.c
+++ b/hw/core/sysbus.c
@@ -21,6 +21,7 @@
#include "hw/sysbus.h"
#include "monitor/monitor.h"
#include "exec/address-spaces.h"
+#include "qapi/error.h"
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
static char *sysbus_get_fw_dev_path(DeviceState *dev);
@@ -372,6 +373,19 @@ BusState *sysbus_get_default(void)
return main_system_bus;
}
+void sysbus_init_child(Object *parent, const char *childname,
+ void *child, size_t childsize,
+ const char *childtype)
+{
+ object_initialize(child, childsize, childtype);
+ /* error_abort is fine here because this can only fail for
+ * programming-error reasons: child already parented, or
+ * parent already has a child with the given name.
+ */
+ object_property_add_child(parent, childname, OBJECT(child), &error_abort);
+ qdev_set_parent_bus(DEVICE(child), sysbus_get_default());
+}
+
static void sysbus_register_types(void)
{
type_register_static(&system_bus_info);
--
2.16.1
next prev parent reply other threads:[~2018-02-16 13:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-16 13:45 [Qemu-devel] [PATCH 0/2] Reduce QOM boilerplate with sysbus_init_child() helper Peter Maydell
2018-02-16 13:45 ` Peter Maydell [this message]
2018-02-16 16:28 ` [Qemu-devel] [PATCH 1/2] hw/sysbus.h: New sysbus_init_child() helper function Igor Mammedov
2018-02-16 17:40 ` Philippe Mathieu-Daudé
2018-02-20 12:13 ` Paolo Bonzini
2018-02-20 13:23 ` Igor Mammedov
2018-03-24 15:35 ` Philippe Mathieu-Daudé
2018-03-26 12:41 ` Igor Mammedov
2018-02-16 13:45 ` [Qemu-devel] [PATCH 2/2] hw/arm/bcm2835_peripherals: Use sysbus_init_child() Peter Maydell
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=20180216134516.6269-2-peter.maydell@linaro.org \
--to=peter.maydell@linaro.org \
--cc=patches@linaro.org \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).