From patchwork Fri Feb 16 13:45:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 128591 Delivered-To: patches@linaro.org Received: by 10.46.124.24 with SMTP id x24csp578997ljc; Fri, 16 Feb 2018 05:45:18 -0800 (PST) X-Google-Smtp-Source: AH8x226iLbkYKasZkNwhTUi5Nnoxwudz5kyAzTJnk1tSnsWjp9jrenykIiBYoWDBT8GC7dnd6qyf X-Received: by 10.28.113.83 with SMTP id m80mr4553287wmc.74.1518788718599; Fri, 16 Feb 2018 05:45:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518788718; cv=none; d=google.com; s=arc-20160816; b=Mwgs4ym86+zZOB3X0omNaw9YnwGTxv1rfpXfifjeNH3WHyjt+qLLn/8PO79fqqSj9c IJBj1qlK1t1+JRqtAuodEOQDIVbN2SqlmGwak86t2//XLc4dbpPUnFL+u2X+B3hxRRb5 R7edNuP1VsMCi5Rb6uxjs1LC+AVIX/XwWjCjvAcAu1it4aBhypuMPrVVMStvRWsUFvok HjxU5bHSufAPnEyoE6TamlxLEIb+1cpIS6yx76TBpC1LBIy9FtbdnrI3FlAJdsi8ZPOJ swgtJRq2ugVVyCQCAjfHAoSTuh22O+PYR00ZcG63jw/wL5sDIL+6C/orR7Vw6eAdj3zo ocQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=p+Xis/I9H3LgBalEwczFftJYXM8wse7+Q8zsf+x2NPc=; b=W5p5S9111qF9/cdzYV3ggNn/nctaFaKrKNxWV6diP9M9UY6nmqRR83beWrDQiU03zr YQ3BYpLLdwwClnBPptyp/tde1cNdxB857enUm0e2VQbvTSO9A8T3NPZyqlTZXBth4oTs xozVdDNRsEXzh+wbpTmZzOFA7jbo54WFu0UMOYb0z4QsAhLYeAs2FZyhfjGA/vU2RhCQ 0fyJ92tg7Pifgo97GZB1f90XpcnZXdd2S+hFcQlXVDj0csidu9dxzuEY4kHbfUvqVO4N SgUvBTP+TQ4+Mj5r9R0JmmPxsxZqTMCWbsHZ35jTZ4cCcx/n5KB27wXNtzskjKt+CqSF RgLA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) smtp.mailfrom=pm215@archaic.org.uk; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by mx.google.com with ESMTPS id v30si9085727wrc.492.2018.02.16.05.45.18 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 16 Feb 2018 05:45:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) client-ip=2001:8b0:1d0::2; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 2001:8b0:1d0::2 as permitted sender) smtp.mailfrom=pm215@archaic.org.uk; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from pm215 by orth.archaic.org.uk with local (Exim 4.89) (envelope-from ) id 1emgKD-0002xn-Il; Fri, 16 Feb 2018 13:45:17 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org, Paolo Bonzini , Stefan Hajnoczi Subject: [PATCH 1/2] hw/sysbus.h: New sysbus_init_child() helper function Date: Fri, 16 Feb 2018 13:45:15 +0000 Message-Id: <20180216134516.6269-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180216134516.6269-1-peter.maydell@linaro.org> References: <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 --- include/hw/sysbus.h | 12 ++++++++++++ hw/core/sysbus.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+) -- 2.16.1 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);