qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Gerd Hoffmann <kraxel@redhat.com>
To: Paul Brook <paul@codesourcery.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [RfC PATCH 0/2] qdev/prop: type checking.
Date: Mon, 13 Jul 2009 21:19:55 +0200	[thread overview]
Message-ID: <4A5B88DB.2020707@redhat.com> (raw)
In-Reply-To: <200907131636.45711.paul@codesourcery.com>

[-- Attachment #1: Type: text/plain, Size: 858 bytes --]

On 07/13/09 17:36, Paul Brook wrote:
>> The second helps a bunch of helper macros to help creating property
>> declarations and converts pci.c as example.  I'm not that happy with
>> that one yet.  Especially I'd like to check somehow that
>> typeof(_state->_field) == _type.  But couldn't figure out a way to do
>> so.  As we are setting up static data structures we are quite limited in
>> what we can do here.  The typechecking trick used by the linux kernel
>> min/max macros can't be used for example.
>
> Could we use a union rather than an opaque pointer for the default value?

See patch (not functional, just to show the macros).

Improves things only a little bit.  We still don't have any type 
checking for the struct element we are writing to.  We have the field 
offset and the type it is supposed to be.  But no actual check.

cheers,
   Gerd

[-- Attachment #2: 0001-qdev-prop-helper-macros.patch --]
[-- Type: text/plain, Size: 3401 bytes --]

>From a770efe5a41de0bc841e935bf0a102ba643db031 Mon Sep 17 00:00:00 2001
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Mon, 13 Jul 2009 14:00:18 +0200
Subject: [PATCH] qdev/prop: helper macros.

---
 hw/pci.c       |    5 +++++
 hw/qdev-addr.h |    3 +++
 hw/qdev.h      |   32 +++++++++++++++++++++++++++++++-
 3 files changed, 39 insertions(+), 1 deletions(-)

diff --git a/hw/pci.c b/hw/pci.c
index 4c61d63..e689d35 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -56,6 +56,7 @@ static struct BusInfo pci_bus_info = {
     .print_dev  = pcibus_dev_print,
     .add_dev    = pci_create,
     .props      = (Property[]) {
+#if 0
         {
             .name   = "devfn",
             .info   = &qdev_prop_uint32,
@@ -63,6 +64,10 @@ static struct BusInfo pci_bus_info = {
             .defval = (uint32_t[]) { -1 },
         },
         {/* end of list */}
+#else
+        DEFINE_PROP_UINT32("devfn", PCIDevice, devfn, -1),
+        DEFINE_PROP_END_OF_LIST()
+#endif
     }
 };
 
diff --git a/hw/qdev-addr.h b/hw/qdev-addr.h
index f02bd7a..e135a96 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)
+
 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 ed0ae83..e7f2367 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -51,7 +51,12 @@ struct Property {
     const char   *name;
     PropertyInfo *info;
     int          offset;
-    void         *defval;
+    void         *defval; /* FIXME: delete */
+    union {
+        uint16_t *uint16_t;
+        uint32_t *uint32_t;
+        void     *any;
+    } def;
 };
 
 enum PropertyType {
@@ -144,6 +149,31 @@ extern PropertyInfo qdev_prop_hex32;
 extern PropertyInfo qdev_prop_ptr;
 extern PropertyInfo qdev_prop_mac;
 
+#define DEFINE_PROP(_name, _state, _field, _prop) { \
+        .name      = (_name),                                    \
+        .info      = &(_prop),                                   \
+        .offset    = offsetof(_state, _field),                   \
+        }
+#define DEFINE_PROP_DEFAULT(_name, _state, _field, _defval, _prop, _type) { \
+        .name      = (_name),                                           \
+        .info      = &(_prop),                                          \
+        .offset    = offsetof(_state, _field),                          \
+        .def._type = (_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_HEX32(_n, _s, _f, _d)                       \
+    DEFINE_PROP_DEFAULT(_n, _s, _f, _d, qdev_prop_hex32, uint32_t)
+#define DEFINE_PROP_PTR(_n, _s, _f)             \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_ptr)
+#define DEFINE_PROP_MAC(_n, _s, _f)             \
+    DEFINE_PROP(_n, _s, _f, qdev_prop_mac)
+#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


      reply	other threads:[~2009-07-13 19:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-13 13:33 [Qemu-devel] [RfC PATCH 0/2] qdev/prop: type checking Gerd Hoffmann
2009-07-13 13:33 ` [Qemu-devel] [PATCH 1/2] qdev/prop: add property type Gerd Hoffmann
2009-07-13 13:33 ` [Qemu-devel] [PATCH 2/2] qdev/prop: helper macros Gerd Hoffmann
2009-07-13 15:36 ` [Qemu-devel] [RfC PATCH 0/2] qdev/prop: type checking Paul Brook
2009-07-13 19:19   ` Gerd Hoffmann [this message]

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=4A5B88DB.2020707@redhat.com \
    --to=kraxel@redhat.com \
    --cc=paul@codesourcery.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 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).