[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RfC PATCH 1/2] qdev/prop: macros for creating typechecked
From: |
Gerd Hoffmann |
Subject: |
[Qemu-devel] [RfC PATCH 1/2] qdev/prop: macros for creating typechecked properties. |
Date: |
Fri, 31 Jul 2009 15:04:58 +0200 |
There are DEFINE_PROP_$TYPE("name", struct, field, default) macros for
each property type. These macros link the qdev_prop_$name struct to the
type used by that property.
typeof(struct->field) is verifyed to be the correct one for the given
property. This uses gcc extentions, but as it is a check only it could
easily be turned off for non-gcc compilers if needed.
The error message given by the compiler isn't that great, I didn't
manage to make the compiler print something more helpful than
"error: initializer element is not constant". Ideally I'd like to
somehow generate a message containing the variable referenced by the
macro reference in the failure case, so we could use the variable name
to transport the message ...
Signed-off-by: Gerd Hoffmann <address@hidden>
---
hw/qdev-addr.h | 3 +++
hw/qdev.h | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+), 0 deletions(-)
diff --git a/hw/qdev-addr.h b/hw/qdev-addr.h
index f02bd7a..9568c27 100644
--- a/hw/qdev-addr.h
+++ b/hw/qdev-addr.h
@@ -1,2 +1,5 @@
+#define DEFINE_PROP_TADDR(_n, _s, _f) \
+ DEFINE_PROP(_n, _s, _f, qdev_prop_taddr, target_phys_addr_t)
+
extern PropertyInfo qdev_prop_taddr;
void qdev_prop_set_taddr(DeviceState *dev, const char *name,
target_phys_addr_t value);
diff --git a/hw/qdev.h b/hw/qdev.h
index b342afb..4fe534e 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -156,6 +156,46 @@ extern PropertyInfo qdev_prop_ptr;
extern PropertyInfo qdev_prop_macaddr;
extern PropertyInfo qdev_prop_pci_devfn;
+extern int qdev_property_type_check_failed; /* undefined */
+#define typeof_field(type, field) typeof(((type *)0)->field)
+#define type_check(t1,t2) __builtin_choose_expr( \
+ __builtin_types_compatible_p(t1,t2),0,qdev_property_type_check_failed)
+
+#define DEFINE_PROP(_name, _state, _field, _prop, _type) { \
+ .name = (_name), \
+ .info = &(_prop), \
+ .offset = offsetof(_state, _field) \
+ + type_check(_type,typeof_field(_state, _field)), \
+ }
+#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
+ .name = (_name), \
+ .info = &(_prop), \
+ .offset = offsetof(_state, _field) \
+ + type_check(_type,typeof_field(_state, _field)), \
+ .defval = (_type[]) { _defval }, \
+ }
+
+#define DEFINE_PROP_UINT16(_n, _s, _f, _d) \
+ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint16, uint16_t)
+#define DEFINE_PROP_UINT32(_n, _s, _f, _d) \
+ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint32, uint32_t)
+#define DEFINE_PROP_UINT64(_n, _s, _f, _d) \
+ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_uint64, uint64_t)
+#define DEFINE_PROP_HEX32(_n, _s, _f, _d) \
+ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
+#define DEFINE_PROP_HEX64(_n, _s, _f, _d) \
+ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex64, uint64_t)
+#define DEFINE_PROP_PCI_DEVFN(_n, _s, _f, _d) \
+ DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_pci_devfn, uint32_t)
+
+#define DEFINE_PROP_PTR(_n, _s, _f) \
+ DEFINE_PROP(_n, _s, _f, qdev_prop_ptr, void*)
+#define DEFINE_PROP_MACADDR(_n, _s, _f) \
+ DEFINE_PROP(_n, _s, _f, qdev_prop_macaddr, uint8_t[6])
+
+#define DEFINE_PROP_END_OF_LIST() \
+ {}
+
/* Set properties between creation and init. */
void *qdev_get_prop_ptr(DeviceState *dev, Property *prop);
int qdev_prop_parse(DeviceState *dev, const char *name, const char *value);
--
1.6.2.5