Add a typecheck for the endianness conversions
authorJens Axboe <axboe@fb.com>
Tue, 1 Jul 2014 14:46:44 +0000 (08:46 -0600)
committerJens Axboe <axboe@fb.com>
Tue, 1 Jul 2014 14:46:44 +0000 (08:46 -0600)
Signed-off-by: Jens Axboe <axboe@fb.com>
compiler/compiler.h
os/os.h

index 0a0213b60fca5204cb9d0294ffb01139baa23083..e1afcb424b8efa06380311cd1e865df3fb6f0d64 100644 (file)
 
 #define fio_unlikely(x)        __builtin_expect(!!(x), 0)
 
 
 #define fio_unlikely(x)        __builtin_expect(!!(x), 0)
 
+/*
+ * Check at compile time that something is of a particular type.
+ * Always evaluates to 1 so you may use it easily in comparisons.
+ */
+#define typecheck(type,x) \
+({     type __dummy; \
+       typeof(x) __dummy2; \
+       (void)(&__dummy == &__dummy2); \
+       1; \
+})
+
 #endif
 #endif
diff --git a/os/os.h b/os/os.h
index b8eee66a14501fd2d083dda2920684940b7cb815..df706ab7be4150d8589b2130e96e5c4ef1b09bb6 100644 (file)
--- a/os/os.h
+++ b/os/os.h
@@ -202,23 +202,29 @@ static inline uint64_t fio_swap64(uint64_t val)
 
 #ifdef FIO_INTERNAL
 #define le16_to_cpu(val) ({                    \
 
 #ifdef FIO_INTERNAL
 #define le16_to_cpu(val) ({                    \
+       typecheck(uint16_t, val);               \
        __le16_to_cpu(val);                     \
 })
 #define le32_to_cpu(val) ({                    \
        __le16_to_cpu(val);                     \
 })
 #define le32_to_cpu(val) ({                    \
+       typecheck(uint32_t, val);               \
        __le32_to_cpu(val);                     \
 })
 #define le64_to_cpu(val) ({                    \
        __le32_to_cpu(val);                     \
 })
 #define le64_to_cpu(val) ({                    \
-       __le64_to_cpu(val);                             \
+       typecheck(uint64_t, val);               \
+       __le64_to_cpu(val);                     \
 })
 #endif
 
 #define cpu_to_le16(val) ({                    \
 })
 #endif
 
 #define cpu_to_le16(val) ({                    \
+       typecheck(uint16_t, val);               \
        __cpu_to_le16(val);                     \
 })
 #define cpu_to_le32(val) ({                    \
        __cpu_to_le16(val);                     \
 })
 #define cpu_to_le32(val) ({                    \
+       typecheck(uint32_t, val);               \
        __cpu_to_le32(val);                     \
 })
 #define cpu_to_le64(val) ({                    \
        __cpu_to_le32(val);                     \
 })
 #define cpu_to_le64(val) ({                    \
+       typecheck(uint64_t, val);               \
        __cpu_to_le64(val);                     \
 })
 
        __cpu_to_le64(val);                     \
 })