[lttng-dev] [PATCH urcu] Fix uatomic sign cast
Mathieu Desnoyers
mathieu.desnoyers at efficios.com
Tue Mar 20 09:45:40 EDT 2012
Passing an unsigned int to uatomic_sub does not honor sign extend to
long, as we should be allowed by assume.
Fix this by introducing cast_long_keep_sign(), which casts either to
long or unsigned long depending on the signedness of the argument
received. It is used in uatomic_sub before applying the "-" operator,
since this operator needs to operate on the "long" type size (since sign
extension might not be performed if the argument received is unsigned).
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers at efficios.com>
---
diff --git a/tests/test_uatomic.c b/tests/test_uatomic.c
index 3faae66..4f48276 100644
--- a/tests/test_uatomic.c
+++ b/tests/test_uatomic.c
@@ -62,6 +62,10 @@ do { \
assert(uatomic_read(ptr) == 122); \
v = uatomic_sub_return(ptr, 1); \
assert(v == 121); \
+ uatomic_sub(ptr, (unsigned int) 2); \
+ assert(uatomic_read(ptr) == 119); \
+ uatomic_inc(ptr); \
+ uatomic_inc(ptr); \
assert(uatomic_read(ptr) == 121); \
uatomic_and(ptr, 129); \
assert(uatomic_read(ptr) == 1); \
diff --git a/urcu/compiler.h b/urcu/compiler.h
index 08bd9e9..5cc423a 100644
--- a/urcu/compiler.h
+++ b/urcu/compiler.h
@@ -86,4 +86,9 @@
#define URCU_FORCE_CAST(type, arg) ((type) (arg))
#endif
+#define caa_is_signed_type(type) (((type) (-1)) < 0)
+
+#define cast_long_keep_sign(v) \
+ (caa_is_signed_type(__typeof__(v)) ? (long) (v) : (unsigned long) (v))
+
#endif /* _URCU_COMPILER_H */
diff --git a/urcu/uatomic/generic.h b/urcu/uatomic/generic.h
index bf7cc6a..37db349 100644
--- a/urcu/uatomic/generic.h
+++ b/urcu/uatomic/generic.h
@@ -82,8 +82,9 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
#define uatomic_cmpxchg(addr, old, _new) \
- ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
- (unsigned long)(_new), \
+ ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
+ cast_long_keep_sign(old), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr))))
@@ -119,7 +120,7 @@ void _uatomic_and(void *addr, unsigned long val,
#define uatomic_and(addr, v) \
(_uatomic_and((addr), \
- (unsigned long)(v), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif
@@ -156,7 +157,7 @@ void _uatomic_or(void *addr, unsigned long val,
#define uatomic_or(addr, v) \
(_uatomic_or((addr), \
- (unsigned long)(v), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif
@@ -188,9 +189,9 @@ unsigned long _uatomic_add_return(void *addr, unsigned long val,
}
-#define uatomic_add_return(addr, v) \
- ((__typeof__(*(addr))) _uatomic_add_return((addr), \
- (unsigned long)(v), \
+#define uatomic_add_return(addr, v) \
+ ((__typeof__(*(addr))) _uatomic_add_return((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif /* #ifndef uatomic_add_return */
@@ -253,7 +254,8 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
}
#define uatomic_xchg(addr, v) \
- ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
+ ((__typeof__(*(addr))) _uatomic_exchange((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif /* #ifndef uatomic_xchg */
@@ -322,9 +324,9 @@ void _uatomic_and(void *addr, unsigned long val, int len)
_uatomic_link_error();
}
-#define uatomic_and(addr, v) \
- (_uatomic_and((addr), \
- (unsigned long)(v), \
+#define uatomic_and(addr, v) \
+ (_uatomic_and((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif /* #ifndef uatomic_and */
@@ -393,9 +395,9 @@ void _uatomic_or(void *addr, unsigned long val, int len)
_uatomic_link_error();
}
-#define uatomic_or(addr, v) \
- (_uatomic_or((addr), \
- (unsigned long)(v),\
+#define uatomic_or(addr, v) \
+ (_uatomic_or((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif /* #ifndef uatomic_or */
@@ -469,9 +471,9 @@ unsigned long _uatomic_add_return(void *addr, unsigned long val, int len)
return 0;
}
-#define uatomic_add_return(addr, v) \
- ((__typeof__(*(addr))) _uatomic_add_return((addr), \
- (unsigned long)(v), \
+#define uatomic_add_return(addr, v) \
+ ((__typeof__(*(addr))) _uatomic_add_return((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif /* #ifndef uatomic_add_return */
@@ -546,7 +548,8 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
}
#define uatomic_xchg(addr, v) \
- ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
+ ((__typeof__(*(addr))) _uatomic_exchange((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#endif /* #ifndef uatomic_xchg */
@@ -558,8 +561,10 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
#define uatomic_add(addr, v) (void)uatomic_add_return((addr), (v))
#endif
-#define uatomic_sub_return(addr, v) uatomic_add_return((addr), -(v))
-#define uatomic_sub(addr, v) uatomic_add((addr), -(v))
+#define uatomic_sub_return(addr, v) \
+ uatomic_add_return((addr), -(cast_long_keep_sign(v)))
+#define uatomic_sub(addr, v) \
+ uatomic_add((addr), -(cast_long_keep_sign(v)))
#ifndef uatomic_inc
#define uatomic_inc(addr) uatomic_add((addr), 1)
diff --git a/urcu/uatomic/ppc.h b/urcu/uatomic/ppc.h
index 3180750..0963a78 100644
--- a/urcu/uatomic/ppc.h
+++ b/urcu/uatomic/ppc.h
@@ -100,7 +100,8 @@ unsigned long _uatomic_exchange(void *addr, unsigned long val, int len)
}
#define uatomic_xchg(addr, v) \
- ((__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
+ ((__typeof__(*(addr))) _uatomic_exchange((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
/* cmpxchg */
@@ -160,8 +161,9 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
#define uatomic_cmpxchg(addr, old, _new) \
- ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
- (unsigned long)(_new), \
+ ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
+ cast_long_keep_sign(old), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr))))
/* uatomic_add_return */
@@ -215,9 +217,9 @@ unsigned long _uatomic_add_return(void *addr, unsigned long val,
}
-#define uatomic_add_return(addr, v) \
- ((__typeof__(*(addr))) _uatomic_add_return((addr), \
- (unsigned long)(v), \
+#define uatomic_add_return(addr, v) \
+ ((__typeof__(*(addr))) _uatomic_add_return((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#ifdef __cplusplus
diff --git a/urcu/uatomic/s390.h b/urcu/uatomic/s390.h
index b274c1c..7b08b82 100644
--- a/urcu/uatomic/s390.h
+++ b/urcu/uatomic/s390.h
@@ -106,8 +106,9 @@ unsigned long _uatomic_exchange(volatile void *addr, unsigned long val, int len)
}
#define uatomic_xchg(addr, v) \
- (__typeof__(*(addr))) _uatomic_exchange((addr), (unsigned long)(v), \
- sizeof(*(addr)))
+ (__typeof__(*(addr))) _uatomic_exchange((addr), \
+ cast_long_keep_sign(v), \
+ sizeof(*(addr)))
/* cmpxchg */
@@ -145,10 +146,10 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
return 0;
}
-#define uatomic_cmpxchg(addr, old, _new) \
- (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
- (unsigned long)(old), \
- (unsigned long)(_new), \
+#define uatomic_cmpxchg(addr, old, _new) \
+ (__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
+ cast_long_keep_sign(old), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr)))
#ifdef __cplusplus
diff --git a/urcu/uatomic/sparc64.h b/urcu/uatomic/sparc64.h
index d9ecada..fd973ef 100644
--- a/urcu/uatomic/sparc64.h
+++ b/urcu/uatomic/sparc64.h
@@ -67,8 +67,9 @@ unsigned long _uatomic_cmpxchg(void *addr, unsigned long old,
#define uatomic_cmpxchg(addr, old, _new) \
- ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), (unsigned long)(old),\
- (unsigned long)(_new), \
+ ((__typeof__(*(addr))) _uatomic_cmpxchg((addr), \
+ cast_long_keep_sign(old), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr))))
#ifdef __cplusplus
diff --git a/urcu/uatomic/x86.h b/urcu/uatomic/x86.h
index 9b67f16..dae5a10 100644
--- a/urcu/uatomic/x86.h
+++ b/urcu/uatomic/x86.h
@@ -102,8 +102,9 @@ unsigned long __uatomic_cmpxchg(void *addr, unsigned long old,
}
#define _uatomic_cmpxchg(addr, old, _new) \
- ((__typeof__(*(addr))) __uatomic_cmpxchg((addr), (unsigned long)(old),\
- (unsigned long)(_new), \
+ ((__typeof__(*(addr))) __uatomic_cmpxchg((addr), \
+ cast_long_keep_sign(old), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr))))
/* xchg */
@@ -163,7 +164,8 @@ unsigned long __uatomic_exchange(void *addr, unsigned long val, int len)
}
#define _uatomic_xchg(addr, v) \
- ((__typeof__(*(addr))) __uatomic_exchange((addr), (unsigned long)(v), \
+ ((__typeof__(*(addr))) __uatomic_exchange((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
/* uatomic_add_return */
@@ -226,9 +228,9 @@ unsigned long __uatomic_add_return(void *addr, unsigned long val,
return 0;
}
-#define _uatomic_add_return(addr, v) \
- ((__typeof__(*(addr))) __uatomic_add_return((addr), \
- (unsigned long)(v), \
+#define _uatomic_add_return(addr, v) \
+ ((__typeof__(*(addr))) __uatomic_add_return((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
/* uatomic_and */
@@ -283,7 +285,7 @@ void __uatomic_and(void *addr, unsigned long val, int len)
}
#define _uatomic_and(addr, v) \
- (__uatomic_and((addr), (unsigned long)(v), sizeof(*(addr))))
+ (__uatomic_and((addr), cast_long_keep_sign(v), sizeof(*(addr))))
/* uatomic_or */
@@ -337,7 +339,7 @@ void __uatomic_or(void *addr, unsigned long val, int len)
}
#define _uatomic_or(addr, v) \
- (__uatomic_or((addr), (unsigned long)(v), sizeof(*(addr))))
+ (__uatomic_or((addr), cast_long_keep_sign(v), sizeof(*(addr))))
/* uatomic_add */
@@ -391,7 +393,7 @@ void __uatomic_add(void *addr, unsigned long val, int len)
}
#define _uatomic_add(addr, v) \
- (__uatomic_add((addr), (unsigned long)(v), sizeof(*(addr))))
+ (__uatomic_add((addr), cast_long_keep_sign(v), sizeof(*(addr))))
/* uatomic_inc */
@@ -517,7 +519,7 @@ extern unsigned long _compat_uatomic_set(void *addr,
unsigned long _new, int len);
#define compat_uatomic_set(addr, _new) \
((__typeof__(*(addr))) _compat_uatomic_set((addr), \
- (unsigned long)(_new), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr))))
@@ -525,34 +527,34 @@ extern unsigned long _compat_uatomic_xchg(void *addr,
unsigned long _new, int len);
#define compat_uatomic_xchg(addr, _new) \
((__typeof__(*(addr))) _compat_uatomic_xchg((addr), \
- (unsigned long)(_new), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr))))
extern unsigned long _compat_uatomic_cmpxchg(void *addr, unsigned long old,
unsigned long _new, int len);
#define compat_uatomic_cmpxchg(addr, old, _new) \
((__typeof__(*(addr))) _compat_uatomic_cmpxchg((addr), \
- (unsigned long)(old), \
- (unsigned long)(_new), \
+ cast_long_keep_sign(old), \
+ cast_long_keep_sign(_new), \
sizeof(*(addr))))
extern void _compat_uatomic_and(void *addr, unsigned long _new, int len);
#define compat_uatomic_and(addr, v) \
(_compat_uatomic_and((addr), \
- (unsigned long)(v), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
extern void _compat_uatomic_or(void *addr, unsigned long _new, int len);
#define compat_uatomic_or(addr, v) \
(_compat_uatomic_or((addr), \
- (unsigned long)(v), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
extern unsigned long _compat_uatomic_add_return(void *addr,
unsigned long _new, int len);
-#define compat_uatomic_add_return(addr, v) \
- ((__typeof__(*(addr))) _compat_uatomic_add_return((addr), \
- (unsigned long)(v), \
+#define compat_uatomic_add_return(addr, v) \
+ ((__typeof__(*(addr))) _compat_uatomic_add_return((addr), \
+ cast_long_keep_sign(v), \
sizeof(*(addr))))
#define compat_uatomic_add(addr, v) \
--
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com
More information about the lttng-dev
mailing list