[RFC 3/3] kernel.h: use new typechecking macros in min()/max() and friends

From: Sasha Levin
Date: Sat Apr 14 2012 - 15:15:55 EST


Convert to using new typechecking macros in kernel.h. This prevents code
duplication and makes the modified macros (easily) readable again.

Signed-off-by: Sasha Levin <levinsasha928@xxxxxxxxx>
---
include/linux/kernel.h | 47 +++++++++++++++--------------------------------
1 files changed, 15 insertions(+), 32 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 645231c..ec509cc 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -555,34 +555,22 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
* "unnecessary" pointer comparison.
*/
#define min(x, y) ({ \
- typeof(x) _min1 = (x); \
- typeof(y) _min2 = (y); \
- (void) (&_min1 == &_min2); \
- _min1 < _min2 ? _min1 : _min2; })
+ typecmp2((x), (y)); \
+ (x) < (y) ? (x) : (y); })

#define max(x, y) ({ \
- typeof(x) _max1 = (x); \
- typeof(y) _max2 = (y); \
- (void) (&_max1 == &_max2); \
- _max1 > _max2 ? _max1 : _max2; })
+ typecmp2((x), (y)); \
+ (x) > (y) ? (x) : (y); })

#define min3(x, y, z) ({ \
- typeof(x) _min1 = (x); \
- typeof(y) _min2 = (y); \
- typeof(z) _min3 = (z); \
- (void) (&_min1 == &_min2); \
- (void) (&_min1 == &_min3); \
- _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
- (_min2 < _min3 ? _min2 : _min3); })
+ typecmp3((x), (y), (z)); \
+ (x) < (y) ? ((x) < (z) ? (x) : (z)) : \
+ ((y) < (z) ? (y) : (z)); })

#define max3(x, y, z) ({ \
- typeof(x) _max1 = (x); \
- typeof(y) _max2 = (y); \
- typeof(z) _max3 = (z); \
- (void) (&_max1 == &_max2); \
- (void) (&_max1 == &_max3); \
- _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
- (_max2 > _max3 ? _max2 : _max3); })
+ typecmp3((x), (y), (z)); \
+ (x) > (y) ? ((x) > (z) ? (x) : (z)) : \
+ ((y) > (z) ? (y) : (z)); })

/**
* min_not_zero - return the minimum that is _not_ zero, unless both are zero
@@ -590,9 +578,8 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
* @y: value2
*/
#define min_not_zero(x, y) ({ \
- typeof(x) __x = (x); \
- typeof(y) __y = (y); \
- __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
+ typecmp2((x), (y)); \
+ (x) == 0 ? (y) : ((y) == 0 ? (x) : min((x), (y))); })

/**
* clamp - return a value clamped to a given range with strict typechecking
@@ -604,13 +591,9 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
* same type as val. See the unnecessary pointer comparisons.
*/
#define clamp(val, min, max) ({ \
- typeof(val) __val = (val); \
- typeof(min) __min = (min); \
- typeof(max) __max = (max); \
- (void) (&__val == &__min); \
- (void) (&__val == &__max); \
- __val = __val < __min ? __min: __val; \
- __val > __max ? __max: __val; })
+ typecmp3((val), (min), (max)); \
+ ((val) > (max) ? (max) : (val)) < (min) ? \
+ (min) : ((val) > (max) ? (max) : (val)); })

/*
* ..and if you can't take the strict
--
1.7.8.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/