]> xenbits.xensource.com Git - people/liuw/xtf.git/commitdiff
Introduce typesafe min()/max() helpers
authorAndrew Cooper <andrew.cooper3@citrix.com>
Fri, 27 May 2016 09:42:53 +0000 (10:42 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 12 Jul 2016 09:51:17 +0000 (10:51 +0100)
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
include/xtf/lib.h

index ddb9f982cb195c3b6542cf6c66de2d4bfd55f494..6bece8ead16588bbf9dc226bfc6c69b1a6ca9a83 100644 (file)
@@ -30,6 +30,22 @@ void __noreturn panic(const char *fmt, ...) __printf(1, 2);
 #define BUILD_BUG_ON(cond)                              \
     _Static_assert(!cond, "!(" #cond ")")
 
+#define min(a, b)                                       \
+    ({                                                  \
+        const typeof(a) _a = (a);                       \
+        const typeof(b) _b = (b);                       \
+        (void)(&_a == &_b);                             \
+        _a < _b ? _a : _b;                              \
+    })
+
+#define max(a, b)                                       \
+    ({                                                  \
+        const typeof(a) _a = (a);                       \
+        const typeof(b) _b = (b);                       \
+        (void)(&_a == &_b);                             \
+        _a > _b ? _a : _b;                              \
+    })
+
 void heapsort(void *base, size_t nmemb, size_t size,
               int (*compar)(const void *, const void *),
               void (*swap)(void *, void *));