Skip to contentSkip to conversation

Allow QOM struct fields to be marked as private

PATCH In review Peter Maydell 3 participants 11 messages May 9, 2014 – May 23, 2014

lore
Share & tools
lore
#0 lore raw

v2 cover Allow QOM struct fields to be marked as private

This patch series provides infrastructure and documentation for marking QOM struct fields as private to the class implementation.

Patch 1 is the implementation (which is a trivial five lines!) and documentation of the code pattern that it's intended to be used with.

Patches 2, 3 and 4 are examples of its use: patch 2 deals with the ARM GIC classes, as an example of a largish class with some subclasses. Patches 3 and 4 are conversions of much simpler and smaller devices, so might be easier to look at first.

A .h file which uses this pattern ends up with half a dozen extra lines of boilerplate, which is slightly sad but not too awful. It would be pretty easy to autogenerate (along with the type macros themselves) if we decided to do that in future, though.

Example of the compiler message if you try to touch a field
which is private:
arm_gic_kvm.c:559:5: error: ‘iomem’ is deprecated (declared at /root/qemu/include/hw/intc/arm_gic_common.h:105): this field is private [-Werror=deprecated-declarations]

Changes since the RFC back in July last year:
* name the macro 'qom_private' rather than '__private'
* redid patch 2 as the GIC code has been rearranged somewhat
* patches 3 and 4 are new

Peter Maydell (4):
Provide infrastructure for marking private QOM struct fields
arm_gic: Use new qom_private macro to mark private fields
a9scu: Use qom_private to mark private fields
arm11scu: Use qom_private to mark private fields

 hw/intc/arm_gic.c                |  3 ++
 hw/intc/arm_gic_common.c         |  2 ++
 hw/intc/arm_gic_kvm.c            |  2 ++
 hw/intc/armv7m_nvic.c            |  2 ++
 hw/misc/a9scu.c                  |  2 ++
 hw/misc/arm11scu.c               |  2 ++
 include/hw/intc/arm_gic.h        | 12 ++++++--
 include/hw/intc/arm_gic_common.h | 62 +++++++++++++++++++++++-----------------
 include/hw/misc/a9scu.h          | 16 +++++++----
 include/hw/misc/arm11scu.h       | 14 ++++++---
 include/qemu/compiler.h          | 10 +++++++
 include/qom/object.h             | 47 ++++++++++++++++++++++++++++++
 12 files changed, 136 insertions(+), 38 deletions(-)
Signature · 1 line
1.9.2
#1 lore raw

PATCH v2 1/4 Provide infrastructure for marking private QOM struct fields

Provide infrastructure for marking private QOM struct fields, so that a compiler warning is generated when a user of the QOM object attempts to access them directly.

This is implemented using GCC's 'deprecated' attribute; preprocessor macros arrange that when compiling the class implementation, no attribute is applied to the fields; when compiling a user of the class the fields are marked deprecated.

This allows us to have a single simple C struct defining the object, and for users of the QOM object to be able to embed instances of it into other structs, but still to have a guard against users accidentally touching parts of the structure they should not be accessing.

---
 include/qemu/compiler.h | 10 ++++++++++
 include/qom/object.h    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
include/qemu/compiler.h +10 -0
index 155b358..d7cc153 100644
@@ -52,4 +52,14 @@
 #define GCC_FMT_ATTR(n, m)
 #endif
 
+/* An attribute usable to mark structure fields as private to the
+ * implementation; since this is only a diagnostic to catch programming
+ * errors, it's OK if it expands to nothing on non-gcc compilers.
+ */
+#if defined __GNUC__
+# define QEMU_PRIVATE_ATTR __attribute__((deprecated("this field is private")))
+#else
+# define QEMU_PRIVATE_ATTR
+#endif
+
 #endif /* COMPILER_H */
include/qom/object.h +47 -0

53 lines not on this page · raw message

Signature · 1 line
1.9.2
#2 lore raw

PATCH v2 2/4 arm_gic: Use new qom_private macro to mark private fields

In reply to Peter Maydell

Use the new qom_private macro infrastructure to mark private fields for the arm_gic classes.

---
 hw/intc/arm_gic.c                |  3 ++
 hw/intc/arm_gic_common.c         |  2 ++
 hw/intc/arm_gic_kvm.c            |  2 ++
 hw/intc/armv7m_nvic.c            |  2 ++
 include/hw/intc/arm_gic.h        | 12 ++++++--
 include/hw/intc/arm_gic_common.h | 62 +++++++++++++++++++++++-----------------
 6 files changed, 54 insertions(+), 29 deletions(-)
hw/intc/arm_gic.c +3 -0
index 1532ef9..afe2179 100644
@@ -18,6 +18,9 @@
  *  armv7m_nvic device.
  */
 
+#define IMPLEMENTING_ARM_GIC
+#define IMPLEMENTING_ARM_GIC_COMMON
+
 #include "hw/sysbus.h"
 #include "gic_internal.h"
 #include "qom/cpu.h"
hw/intc/arm_gic_common.c +2 -0
index 6d884ec..8a2c434 100644
@@ -18,6 +18,8 @@
  * with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#define IMPLEMENTING_ARM_GIC_COMMON
+
 #include "gic_internal.h"
 
 static void gic_pre_save(void *opaque)
hw/intc/arm_gic_kvm.c +2 -0
index 5038885..792073e 100644
@@ -19,6 +19,8 @@
  * with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#define IMPLEMENTING_ARM_GIC_COMMON
+
 #include "hw/sysbus.h"
 #include "sysemu/kvm.h"
 #include "kvm_arm.h"
hw/intc/armv7m_nvic.c +2 -0
index 9aa8ab2..d68b286 100644
@@ -10,6 +10,8 @@
  * NVIC.  Much of that is also implemented here.
  */
 
+#define IMPLEMENTING_ARM_GIC_COMMON
+
 #include "hw/sysbus.h"
 #include "qemu/timer.h"
 #include "hw/arm/arm.h"
include/hw/intc/arm_gic.h +10 -2
index 0971e37..8d69e02 100644
@@ -31,12 +31,20 @@
 #define ARM_GIC_GET_CLASS(obj) \
      OBJECT_GET_CLASS(ARMGICClass, (obj), TYPE_ARM_GIC)
 
+#ifdef IMPLEMENTING_ARM_GIC
+#define qom_private
+#else
+#define qom_private QEMU_PRIVATE_ATTR
+#endif
+
 typedef struct ARMGICClass {
     /*< private >*/
-    ARMGICCommonClass parent_class;
+    qom_private ARMGICCommonClass parent_class;
     /*< public >*/
 
-    DeviceRealize parent_realize;
+    qom_private DeviceRealize parent_realize;
 } ARMGICClass;
 
+#undef qom_private
+
 #endif
include/hw/intc/arm_gic_common.h +35 -27

104 lines not on this page · raw message

Signature · 1 line
1.9.2
#3 lore raw

PATCH v2 3/4 a9scu: Use qom_private to mark private fields

In reply to Peter Maydell

Use the new qom_private infrastructure to mark private fields in the QOM device struct.

---
 hw/misc/a9scu.c         |  2 ++
 include/hw/misc/a9scu.h | 16 +++++++++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)
hw/misc/a9scu.c +2 -0
index 4434945..81587b6 100644
@@ -8,6 +8,8 @@
  * This code is licensed under the GPL.
  */
 
+#define IMPLEMENTING_A9_SCU
+
 #include "hw/misc/a9scu.h"
 
 static uint64_t a9_scu_read(void *opaque, hwaddr offset,
include/hw/misc/a9scu.h +11 -5
index efb0c30..86dd21f 100644
@@ -14,15 +14,21 @@
 
 /* A9MP private memory region.  */
 
+#ifdef IMPLEMENTING_A9_SCU
+#define qom_private
+#else
+#define qom_private QEMU_PRIVATE_ATTR
+#endif
+
 typedef struct A9SCUState {
     /*< private >*/
-    SysBusDevice parent_obj;
+    qom_private SysBusDevice parent_obj;
     /*< public >*/
 
-    MemoryRegion iomem;
-    uint32_t control;
-    uint32_t status;
-    uint32_t num_cpu;
+    qom_private MemoryRegion iomem;
+    qom_private uint32_t control;
+    qom_private uint32_t status;
+    qom_private uint32_t num_cpu;
 } A9SCUState;
 
 #define TYPE_A9_SCU "a9-scu"
Signature · 1 line
1.9.2
#4 lore raw

PATCH v2 4/4 arm11scu: Use qom_private to mark private fields

In reply to Peter Maydell

Mark the arm11scu private fields as qom_private.

---
 hw/misc/arm11scu.c         |  2 ++
 include/hw/misc/arm11scu.h | 14 ++++++++++----
 2 files changed, 12 insertions(+), 4 deletions(-)
hw/misc/arm11scu.c +2 -0
index a791675..d8d5b2b 100644
@@ -8,6 +8,8 @@
  * This code is licensed under the GPL.
  */
 
+#define IMPLEMENTING_ARM11_SCU
+
 #include "hw/misc/arm11scu.h"
 
 static uint64_t mpcore_scu_read(void *opaque, hwaddr offset,
include/hw/misc/arm11scu.h +10 -4
index 5ad0f3d..4de1eb1 100644
@@ -16,14 +16,20 @@
 #define TYPE_ARM11_SCU "arm11-scu"
 #define ARM11_SCU(obj) OBJECT_CHECK(ARM11SCUState, (obj), TYPE_ARM11_SCU)
 
+#ifdef IMPLEMENTING_ARM11_SCU
+#define qom_private
+#else
+#define qom_private QEMU_PRIVATE_ATTR
+#endif
+
 typedef struct ARM11SCUState {
     /*< private >*/
-    SysBusDevice parent_obj;
+    qom_private SysBusDevice parent_obj;
     /*< public >*/
 
-    uint32_t control;
-    uint32_t num_cpu;
-    MemoryRegion iomem;
+    qom_private uint32_t control;
+    qom_private uint32_t num_cpu;
+    qom_private MemoryRegion iomem;
 } ARM11SCUState;
 
 #endif
Signature · 1 line
1.9.2
#5 lore raw
In reply to Peter Maydell
Quoted 11 lines · Peter Maydell

This patch series provides infrastructure and documentation for marking QOM struct fields as private to the class implementation.

Patch 1 is the implementation (which is a trivial five lines!) and documentation of the code pattern that it's intended to be used with.

Patches 2, 3 and 4 are examples of its use: patch 2 deals with the ARM GIC classes, as an example of a largish class with some subclasses. Patches 3 and 4 are conversions of much simpler and smaller devices, so might be easier to look at first.

Quoted 8 lines

A .h file which uses this pattern ends up with half a dozen extra lines of boilerplate, which is slightly sad but not too awful. It would be pretty easy to autogenerate (along with the type macros themselves) if we decided to do that in future, though.

Example of the compiler message if you try to touch a field
which is private:
arm_gic_kvm.c:559:5: error: ‘iomem’ is deprecated (declared at /root/qemu/include/hw/intc/arm_gic_common.h:105): this field is private [-Werror=deprecated-declarations]

Ping?

thanks
-- PMM

#6 lore raw

Am 23.05.2014 13:13, schrieb Peter Maydell:

Quoted 17 lines · Peter Maydell
Quoted 16 lines

This patch series provides infrastructure and documentation for marking QOM struct fields as private to the class implementation.

Patch 1 is the implementation (which is a trivial five lines!) and documentation of the code pattern that it's intended to be used with.

Patches 2, 3 and 4 are examples of its use: patch 2 deals with the ARM GIC classes, as an example of a largish class with some subclasses. Patches 3 and 4 are conversions of much simpler and smaller devices, so might be easier to look at first.

A .h file which uses this pattern ends up with half a dozen extra lines of boilerplate, which is slightly sad but not too awful. It would be pretty easy to autogenerate (along with the type macros themselves) if we decided to do that in future, though.

Quoted 5 lines
Quoted 3 lines

Example of the compiler message if you try to touch a field
which is private:
arm_gic_kvm.c:559:5: error: ‘iomem’ is deprecated (declared at /root/qemu/include/hw/intc/arm_gic_common.h:105): this field is private [-Werror=deprecated-declarations]

Ping?

I believe I remarked that in the example
typedef struct Foo {
would be more in line with our Coding Style and majority of users.

Other than that, I have no objections and assumed you'll take it through your arm queue.

Cheers,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#7 lore raw
Quoted 9 lines · Andreas Färber

Am 23.05.2014 13:13, schrieb Peter Maydell:

Quoted 1 line

Ping?

I believe I remarked that in the example
typedef struct Foo {
would be more in line with our Coding Style and majority of users.

Other than that, I have no objections and assumed you'll take it through your arm queue.

Oops, yes, I'd forgotten that conversation. I'll remove the newline preceding the '{' and am happy to put this through the target-arm queue.

thanks
-- PMM

#8 lore raw

Il 23/05/2014 13:50, Peter Maydell ha scritto:

Quoted 8 lines · Andreas Färber
Quoted 7 lines

Am 23.05.2014 13:13, schrieb Peter Maydell:

Quoted 1 line

Ping?

I believe I remarked that in the example
typedef struct Foo {
would be more in line with our Coding Style and majority of users.

Quoted 6 lines
Quoted 2 lines

Other than that, I have no objections and assumed you'll take it through your arm queue.

Oops, yes, I'd forgotten that conversation. I'll remove the newline preceding the '{' and am happy to put this through the target-arm queue.

Semi-serious proposal:

    #define comma_if_empty_ ,
    #define CPP_IFEMPTY(macro, t, f)      CPP_IFEMPTY1(macro, t, f)
    #define CPP_IFEMPTY1(value, t, f)     CPP_IF2(comma_if_empty_##value, t, f)
    #define CPP_IF2(comma_if_true, t, f)  CPP_IF3(comma_if_true t, f)
    #define CPP_IF3(_, v, ...) v

    #define qom_private(macro) CPP_IFEMPTY(IMPLEMENTING_##macro,, QEMU_PRIVATE_ATTR)

To be used as:

    qom_private(A9_SCU) MemoryRegion iomem;

Credit: https://plus.google.com/u/0/+LinusTorvalds/posts/9gntjh57dXt

Paolo

#9 lore raw
Quoted 26 lines · Paolo Bonzini

Il 23/05/2014 13:50, Peter Maydell ha scritto:

Quoted 8 lines · Andreas Färber
Quoted 7 lines

Am 23.05.2014 13:13, schrieb Peter Maydell:

Quoted 1 line

Ping?

I believe I remarked that in the example
typedef struct Foo {
would be more in line with our Coding Style and majority of users.

Quoted 6 lines
Quoted 2 lines

Other than that, I have no objections and assumed you'll take it through your arm queue.

Oops, yes, I'd forgotten that conversation. I'll remove the newline preceding the '{' and am happy to put this through the target-arm queue.

Semi-serious proposal:

    #define comma_if_empty_ ,
    #define CPP_IFEMPTY(macro, t, f)      CPP_IFEMPTY1(macro, t, f)
    #define CPP_IFEMPTY1(value, t, f)     CPP_IF2(comma_if_empty_##value, t, f)
    #define CPP_IF2(comma_if_true, t, f)  CPP_IF3(comma_if_true t, f)
    #define CPP_IF3(_, v, ...) v

    #define qom_private(macro) CPP_IFEMPTY(IMPLEMENTING_##macro,, QEMU_PRIVATE_ATTR)
Quoted 3 lines

To be used as:

    qom_private(A9_SCU) MemoryRegion iomem;

Interesting. That means we can avoid the irritating boilerplate in each include file to define and undefine qom_private, at the cost of the tag on each field in the struct being a bit longer. I'm not entirely sure which I prefer...

thanks
-- PMM

#10 lore raw

Il 23/05/2014 18:21, Peter Maydell ha scritto:

Quoted 29 lines · Paolo Bonzini
Quoted 28 lines

Il 23/05/2014 13:50, Peter Maydell ha scritto:

Quoted 8 lines · Andreas Färber
Quoted 7 lines

Am 23.05.2014 13:13, schrieb Peter Maydell:

Quoted 1 line

Ping?

I believe I remarked that in the example
typedef struct Foo {
would be more in line with our Coding Style and majority of users.

Quoted 6 lines
Quoted 2 lines

Other than that, I have no objections and assumed you'll take it through your arm queue.

Oops, yes, I'd forgotten that conversation. I'll remove the newline preceding the '{' and am happy to put this through the target-arm queue.

Semi-serious proposal:

    #define comma_if_empty_ ,
    #define CPP_IFEMPTY(macro, t, f)      CPP_IFEMPTY1(macro, t, f)
    #define CPP_IFEMPTY1(value, t, f)     CPP_IF2(comma_if_empty_##value, t, f)
    #define CPP_IF2(comma_if_true, t, f)  CPP_IF3(comma_if_true t, f)
    #define CPP_IF3(_, v, ...) v

    #define qom_private(macro) CPP_IFEMPTY(IMPLEMENTING_##macro,, QEMU_PRIVATE_ATTR)

To be used as:

Quoted 7 lines
Quoted 1 line
    qom_private(A9_SCU) MemoryRegion iomem;

Interesting. That means we can avoid the irritating boilerplate in each include file to define and undefine qom_private, at the cost of the tag on each field in the struct being a bit longer. I'm not entirely sure which I prefer...

You could also

#define a9_scu_private qom_private(A9_SCU)

a9_scu_private MemoryRegion iommu;

where the difference becomes a wash.

Paolo