summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrediano Ziglio <fziglio@redhat.com>2015-12-03 15:24:58 +0000
committerFrediano Ziglio <fziglio@redhat.com>2015-12-04 11:15:12 +0000
commitcffb8baee117710087445c968acc55654f74f508 (patch)
tree8280bb32530ad24c463134517965eeff48c8f5ed
parent95b5617874a17f13c3419f4de87dfd88a8eb832d (diff)
downloadspice-protocol-cffb8baee117710087445c968acc55654f74f508.tar.gz
spice-protocol-cffb8baee117710087445c968acc55654f74f508.tar.xz
spice-protocol-cffb8baee117710087445c968acc55654f74f508.zip
macros: make SPICE_CONTAINEROF more typesafe
Check the pointer given is the same type as member pointer. Signed-off-by: Frediano Ziglio <fziglio@redhat.com> Acked-by: Jonathon Jongsma <jjongsma@redhat.com>
-rw-r--r--spice/macros.h6
1 files changed, 6 insertions, 0 deletions
diff --git a/spice/macros.h b/spice/macros.h
index cd63528..2efbaff 100644
--- a/spice/macros.h
+++ b/spice/macros.h
@@ -143,8 +143,14 @@
((long) ((uint8_t*) &((struct_type*) 0)->member))
#endif
+#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
+#define SPICE_CONTAINEROF(ptr, struct_type, member) ({ \
+ const typeof( ((struct_type *)0)->member ) *__mptr = (ptr); \
+ ((struct_type *)(void *)((uint8_t *)(__mptr) - SPICE_OFFSETOF(struct_type, member))); })
+#else
#define SPICE_CONTAINEROF(ptr, struct_type, member) \
((struct_type *)(void *)((uint8_t *)(ptr) - SPICE_OFFSETOF(struct_type, member)))
+#endif
#define SPICE_MEMBER_P(struct_p, struct_offset) \
((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))