urcu-pointer: implement URCU_FORCE_CAST for C++ compatibility of urcu-pointer.h
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 13 Sep 2011 21:00:08 +0000 (17:00 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 13 Sep 2011 21:02:44 +0000 (17:02 -0400)
We need to be careful with those, so we do not break aliasing. Our
use-case is to cast back and forth between the same type and a void *
(or void **) type when we pass pointers to C functions. As we cast back
to the same type when the pointer is returned from the function,
aliasing should still work.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
urcu-pointer.h
urcu/compiler.h

index 027a18fd4d0b57159a096e5a575d1618ddcfe364..67ee381686903f2ab59de27362748730d61919a9 100644 (file)
@@ -67,37 +67,40 @@ extern "C" {
 extern void *rcu_dereference_sym(void *p);
 #define rcu_dereference(p)                                                  \
        ({                                                                   \
-               typeof(p) _________p1 =                                      \
-                       rcu_dereference_sym((void *)(p));                    \
+               typeof(p) _________p1 = URCU_FORCE_CAST(typeof(p),           \
+                       rcu_dereference_sym(URCU_FORCE_CAST(void *, p)));    \
                (_________p1);                                               \
        })
 
 extern void *rcu_cmpxchg_pointer_sym(void **p, void *old, void *_new);
 #define rcu_cmpxchg_pointer(p, old, _new)                                   \
        ({                                                                   \
-               typeof(*p) _________pold = (old);                            \
-               typeof(*p) _________pnew = (_new);                           \
-               typeof(*p) _________p1 =                                     \
-                       rcu_cmpxchg_pointer_sym((void **)(p), _________pold, \
-                                               _________pnew);              \
+               typeof(*(p)) _________pold = (old);                          \
+               typeof(*(p)) _________pnew = (_new);                         \
+               typeof(*(p)) _________p1 = URCU_FORCE_CAST(typeof(*(p)),     \
+                       rcu_cmpxchg_pointer_sym(URCU_FORCE_CAST(void **, p),\
+                                               _________pold,               \
+                                               _________pnew));             \
                (_________p1);                                               \
        })
 
 extern void *rcu_xchg_pointer_sym(void **p, void *v);
 #define rcu_xchg_pointer(p, v)                                              \
        ({                                                                   \
-               typeof(*p) _________pv = (v);                                \
-               typeof(*p) _________p1 =                                     \
-                       rcu_xchg_pointer_sym((void **)(p), _________pv);     \
+               typeof(*(p)) _________pv = (v);                              \
+               typeof(*(p)) _________p1 = URCU_FORCE_CAST(typeof(*(p)),     \
+                       rcu_xchg_pointer_sym(URCU_FORCE_CAST(void **, p),    \
+                                            _________pv));                  \
                (_________p1);                                               \
        })
 
 extern void *rcu_set_pointer_sym(void **p, void *v);
 #define rcu_set_pointer(p, v)                                               \
        ({                                                                   \
-               typeof(*p) _________pv = (v);                                \
-               typeof(*p) _________p1 =                                     \
-                       rcu_set_pointer_sym((void **)(p), _________pv);      \
+               typeof(*(p)) _________pv = (v);                              \
+               typeof(*(p)) _________p1 = URCU_FORCE_CAST(typeof(*(p)),     \
+                       rcu_set_pointer_sym(URCU_FORCE_CAST(void **, p),     \
+                                           _________pv));                   \
        })
 
 #endif /* !_LGPL_SOURCE */
index 4bced2a9849846a25b8c1fd59a15cd2b03af0060..6db803e9319845a38d847f6ccc339c2230f55c91 100644 (file)
  */
 #define __rcu
 
+#ifdef __cplusplus
+#define URCU_FORCE_CAST(type, arg)     (reinterpret_cast<type>(arg))
+#else
+#define URCU_FORCE_CAST(type, arg)     ((type) (arg))
+#endif
+
 #endif /* _URCU_COMPILER_H */
This page took 0.026717 seconds and 4 git commands to generate.