Add type checking to 16/32/64 endianness converters
authorJens Axboe <axboe@kernel.dk>
Mon, 3 Oct 2011 12:45:27 +0000 (14:45 +0200)
committerJens Axboe <axboe@kernel.dk>
Mon, 3 Oct 2011 12:45:27 +0000 (14:45 +0200)
Signed-off-by: Jens Axboe <axboe@kernel.dk>
client.c
server.c
server.h
stat.h

index 459c300b18fe73b77dfc1f5f912647e46c949915..c6588664ecd3613501154b21810a2c0f98a5c69a 100644 (file)
--- a/client.c
+++ b/client.c
@@ -208,8 +208,8 @@ static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
        dst->min_val    = le64_to_cpu(src->min_val);
        dst->samples    = le64_to_cpu(src->samples);
        /* FIXME */
-       dst->mean       = le64_to_cpu(src->mean);
-       dst->S          = le64_to_cpu(src->S);
+       dst->mean       = __le64_to_cpu(src->mean);
+       dst->S          = __le64_to_cpu(src->S);
 }
 
 static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
@@ -267,8 +267,8 @@ static void convert_ts(struct thread_stat *dst, struct thread_stat *src)
        dst->total_run_time     = le64_to_cpu(src->total_run_time);
        dst->continue_on_error  = le16_to_cpu(src->continue_on_error);
        dst->total_err_count    = le64_to_cpu(src->total_err_count);
-       dst->first_error        = le64_to_cpu(src->first_error);
-       dst->kb_base            = le64_to_cpu(src->kb_base);
+       dst->first_error        = le32_to_cpu(src->first_error);
+       dst->kb_base            = le32_to_cpu(src->kb_base);
 }
 
 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
index 72761c562fa938a6fe70867e60765dbd329c5698..92d37bd9757872b98aa873312974217c0dfed553 100644 (file)
--- a/server.c
+++ b/server.c
@@ -180,11 +180,11 @@ void fio_net_cmd_crc(struct fio_net_cmd *cmd)
 {
        uint32_t pdu_len;
 
-       cmd->cmd_crc16 = cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
+       cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
 
        pdu_len = le32_to_cpu(cmd->pdu_len);
        if (pdu_len)
-               cmd->pdu_crc16 = cpu_to_le16(crc16(cmd->payload, pdu_len));
+               cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
 }
 
 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size)
@@ -203,7 +203,7 @@ int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size)
                fio_init_net_cmd(cmd, opcode, buf, this_len);
 
                if (this_len < size)
-                       cmd->flags = cpu_to_le32(FIO_NET_CMD_F_MORE);
+                       cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
 
                fio_net_cmd_crc(cmd);
 
@@ -219,7 +219,7 @@ int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size)
 static int send_simple_command(int sk, uint16_t opcode, uint64_t serial)
 {
        struct fio_net_cmd cmd = {
-               .version        = cpu_to_le16(FIO_SERVER_VER1),
+               .version        = __cpu_to_le16(FIO_SERVER_VER1),
                .opcode         = cpu_to_le16(opcode),
                .serial         = cpu_to_le64(serial),
        };
@@ -442,8 +442,8 @@ static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
        dst->min_val    = cpu_to_le64(src->min_val);
        dst->samples    = cpu_to_le64(src->samples);
        /* FIXME */
-       dst->mean       = cpu_to_le64(src->mean);
-       dst->S          = cpu_to_le64(src->S);
+       dst->mean       = __cpu_to_le64(src->mean);
+       dst->S          = __cpu_to_le64(src->S);
 }
 
 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
@@ -476,9 +476,9 @@ void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
        strcpy(p.ts.verror, ts->verror);
        strcpy(p.ts.description, ts->description);
 
-       p.ts.error              = cpu_to_le32(ts->error);
+       p.ts.error      = cpu_to_le32(ts->error);
        p.ts.groupid    = cpu_to_le32(ts->groupid);
-       p.ts.pid                = cpu_to_le32(ts->pid);
+       p.ts.pid        = cpu_to_le32(ts->pid);
        p.ts.members    = cpu_to_le32(ts->members);
 
        for (i = 0; i < 2; i++) {
@@ -527,8 +527,8 @@ void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
        p.ts.total_run_time     = cpu_to_le64(ts->total_run_time);
        p.ts.continue_on_error  = cpu_to_le16(ts->continue_on_error);
        p.ts.total_err_count    = cpu_to_le64(ts->total_err_count);
-       p.ts.first_error        = cpu_to_le64(ts->first_error);
-       p.ts.kb_base            = cpu_to_le64(ts->kb_base);
+       p.ts.first_error        = cpu_to_le32(ts->first_error);
+       p.ts.kb_base            = cpu_to_le32(ts->kb_base);
 
        convert_gs(&p.rs, rs);
 
index 10c9006e4339c1ec57a6f12eed5da26493ddc680..9aa91de139a58d950cd0c8a5fbae0f4a5b7bfbd3 100644 (file)
--- a/server.h
+++ b/server.h
@@ -76,29 +76,54 @@ extern int exit_backend;
 extern int fio_net_port;
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-#define le16_to_cpu(x)         (x)
-#define le32_to_cpu(x)         (x)
-#define le64_to_cpu(x)         (x)
-#define cpu_to_le16(x)         (x)
-#define cpu_to_le32(x)         (x)
-#define cpu_to_le64(x)         (x)
+#define __le16_to_cpu(x)               (x)
+#define __le32_to_cpu(x)               (x)
+#define __le64_to_cpu(x)               (x)
+#define __cpu_to_le16(x)               (x)
+#define __cpu_to_le32(x)               (x)
+#define __cpu_to_le64(x)               (x)
 #elif __BYTE_ORDER == __BIG_ENDIAN
-#define le16_to_cpu(x)         __bswap_16(x)
-#define le32_to_cpu(x)         __bswap_32(x)
-#define le64_to_cpu(x)         __bswap_64(x)
-#define cpu_to_le16(x)         __bswap_16(x)
-#define cpu_to_le32(x)         __bswap_32(x)
-#define cpu_to_le64(x)         __bswap_64(x)
+#define __le16_to_cpu(x)               __bswap_16(x)
+#define __le32_to_cpu(x)               __bswap_32(x)
+#define __le64_to_cpu(x)               __bswap_64(x)
+#define __cpu_to_le16(x)               __bswap_16(x)
+#define __cpu_to_le32(x)               __bswap_32(x)
+#define __cpu_to_le64(x)               __bswap_64(x)
 #else
 #error "Endianness not detected"
 #endif
 
+#define le16_to_cpu(val) ({                    \
+       uint16_t *__val = &(val);               \
+       __le16_to_cpu(*__val);                  \
+})
+#define le32_to_cpu(val) ({                    \
+       uint32_t *__val = &(val);               \
+       __le32_to_cpu(*__val);                  \
+})
+#define le64_to_cpu(val) ({                    \
+       uint64_t *__val = &(val);               \
+       __le64_to_cpu(*__val);                  \
+})
+#define cpu_to_le16(val) ({                    \
+       uint16_t *__val = &(val);               \
+       __cpu_to_le16(*__val);                  \
+})
+#define cpu_to_le32(val) ({                    \
+       uint32_t *__val = &(val);               \
+       __cpu_to_le32(*__val);                  \
+})
+#define cpu_to_le64(val) ({                    \
+       uint64_t *__val = &(val);               \
+       __cpu_to_le64(*__val);                  \
+})
+
 static inline void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
                                    const void *pdu, uint32_t pdu_len)
 {
        memset(cmd, 0, sizeof(*cmd));
 
-       cmd->version    = cpu_to_le16(FIO_SERVER_VER1);
+       cmd->version    = __cpu_to_le16(FIO_SERVER_VER1);
        cmd->opcode     = cpu_to_le16(opcode);
 
        if (pdu) {
diff --git a/stat.h b/stat.h
index 507a76a9689c703883a84d54535671a217c91a91..cba871458cf78292eb42b3097aa781a45a954bd9 100644 (file)
--- a/stat.h
+++ b/stat.h
@@ -115,8 +115,8 @@ struct group_run_stats {
 struct thread_stat {
        char name[FIO_JOBNAME_SIZE];
        char verror[FIO_VERROR_SIZE];
-       int32_t error;
-       int32_t groupid;
+       uint32_t error;
+       uint32_t groupid;
        uint32_t pid;
        char description[FIO_JOBNAME_SIZE];
        uint32_t members;
@@ -163,7 +163,7 @@ struct thread_stat {
         */
        uint16_t continue_on_error;
        uint64_t total_err_count;
-       int32_t first_error;
+       uint32_t first_error;
 
        uint32_t kb_base;
 };