]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/crc32c: refactor a bit
authorSage Weil <sage@inktank.com>
Tue, 20 Aug 2013 18:55:10 +0000 (11:55 -0700)
committerSage Weil <sage@inktank.com>
Tue, 20 Aug 2013 23:42:53 +0000 (16:42 -0700)
- the generic function without the _le suffix (useless)
- use a static global so that detection only happens once
- make the structure a bit cleaner to plug in new implementations

Signed-off-by: Sage Weil <sage@inktank.com>
src/Makefile.am
src/common/crc32c.cc [new file with mode: 0644]
src/common/sctp_crc32.c
src/common/sctp_crc32.h [new file with mode: 0644]
src/include/buffer.h
src/include/crc32c.h
src/msg/Message.h
src/msg/Pipe.cc

index 5a0f3472080acebdcd7f2342ee7bd7358ef78df8..9a5f2a8ae0f5e27a35c9768297c84207fabe2286 100644 (file)
@@ -1534,6 +1534,7 @@ libcommon_files = \
        common/Finisher.cc \
        common/environment.cc\
        common/sctp_crc32.c\
+       common/crc32c.cc\
        common/crc32c-intel.c\
        common/assert.cc \
         common/run_cmd.cc \
@@ -1885,6 +1886,7 @@ noinst_HEADERS = \
        common/utf8.h\
        common/mime.h\
        common/pick_address.h\
+       common/sctp_crc32.h\
        common/secret.h\
        common/strtol.h\
        common/static_assert.h\
diff --git a/src/common/crc32c.cc b/src/common/crc32c.cc
new file mode 100644 (file)
index 0000000..84a5ef4
--- /dev/null
@@ -0,0 +1,24 @@
+#include "include/crc32c.h"
+
+#include "common/sctp_crc32.h"
+
+/*
+ * choose best implementation based on the CPU architecture.
+ */
+ceph_crc32c_func_t ceph_choose_crc32(void)
+{
+       /* default */
+       return ceph_crc32c_sctp;
+}
+
+/*
+ * static global
+ *
+ * This is a bit of a no-no for shared libraries, but we don't care.
+ * It is effectively constant for the executing process as the value
+ * depends on the CPU architecture.
+ *
+ * We initialize it during program init using the magic of C++.
+ */
+ceph_crc32c_func_t ceph_crc32c_func = ceph_choose_crc32();
+
index b11bb48dd87ff2bf04f9090ec76261b65eaece8c..7e2678a2b7ceba7815fdb2444cbe821a83c78e61 100644 (file)
@@ -728,7 +728,7 @@ sctp_csum_finalize(uint32_t crc32c)
 }
 #endif
 
-uint32_t ceph_crc32c_le_generic(uint32_t crc, unsigned char const *data, unsigned length)
+uint32_t ceph_crc32c_sctp(uint32_t crc, unsigned char const *data, unsigned length)
 {
        return update_crc32(crc, data, length);
 }
diff --git a/src/common/sctp_crc32.h b/src/common/sctp_crc32.h
new file mode 100644 (file)
index 0000000..92d20bc
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef CEPH_COMMON_SCTP_CRC32_H
+#define CEPH_COMMON_SCTP_CRC32_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern uint32_t ceph_crc32c_sctp(uint32_t crc, unsigned char const *data, unsigned length);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
index 8c4dfb56e174d4cf8cdeeac07390ea300dea4fb7..8e637d658c51d06e723c1c6ddc9169ec2b15ac63 100644 (file)
@@ -425,7 +425,7 @@ public:
           it != _buffers.end(); 
           ++it)
        if (it->length())
-         crc = ceph_crc32c_le(crc, (unsigned char*)it->c_str(), it->length());
+         crc = ceph_crc32c(crc, (unsigned char*)it->c_str(), it->length());
       return crc;
     }
 
index 3fd209efb02b301b2aed6760aac6e2e292c840cc..d5f7388be565cbb7a0b5c76903ace2eacc594e92 100644 (file)
@@ -1,25 +1,25 @@
 #ifndef CEPH_CRC32C_H
 #define CEPH_CRC32C_H
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
+#include "include/inttypes.h"
 #include <string.h>
 
-extern int ceph_have_crc32c_intel(void);
-extern uint32_t ceph_crc32c_le_generic(uint32_t crc, unsigned char const *data, unsigned length);
-extern uint32_t ceph_crc32c_le_intel(uint32_t crc, unsigned char const *data, unsigned length);
+typedef uint32_t (*ceph_crc32c_func_t)(uint32_t crc, unsigned char const *data, unsigned length);
 
-static inline uint32_t ceph_crc32c_le(uint32_t crc, unsigned char const *data, unsigned length) {
-       if (ceph_have_crc32c_intel()) //__builtin_cpu_supports("sse4.2"))
-               return ceph_crc32c_le_intel(crc, data, length);
-       else
-               return ceph_crc32c_le_generic(crc, data, length);
-}
+/*
+ * this is a static global with the chosen crc32c implementation for
+ * the given architecture.
+ */
+extern ceph_crc32c_func_t ceph_crc32c_func;
 
-#ifdef __cplusplus
+extern ceph_crc32c_func_t ceph_choose_crc32(void);
+
+/*
+ * common entry point; use this!
+ */
+static inline uint32_t ceph_crc32c(uint32_t crc, unsigned char const *data, unsigned length)
+{
+       return ceph_crc32c_func(crc, data, length);
 }
-#endif
 
 #endif
index 3ed8ee667d242c07b00e50450901f39c36429b7c..f345e7adaabec8e80c50b073ae90295ed21490a2 100644 (file)
@@ -449,8 +449,8 @@ public:
   const utime_t& get_recv_complete_stamp() const { return recv_complete_stamp; }
 
   void calc_header_crc() {
-    header.crc = ceph_crc32c_le(0, (unsigned char*)&header,
-                          sizeof(header) - sizeof(header.crc));
+    header.crc = ceph_crc32c(0, (unsigned char*)&header,
+                            sizeof(header) - sizeof(header.crc));
   }
   void calc_front_crc() {
     footer.front_crc = payload.crc32c(0);
index 6f271c812f386324e27f96e60ab8616c813e05d5..50656fee53b9d0762692a7c186f35deb339d60a7 100644 (file)
@@ -1684,7 +1684,7 @@ int Pipe::read_message(Message **pm)
   if (connection_state->has_feature(CEPH_FEATURE_NOSRCADDR)) {
     if (tcp_read((char*)&header, sizeof(header)) < 0)
       return -1;
-    header_crc = ceph_crc32c_le(0, (unsigned char *)&header, sizeof(header) - sizeof(header.crc));
+    header_crc = ceph_crc32c(0, (unsigned char *)&header, sizeof(header) - sizeof(header.crc));
   } else {
     ceph_msg_header_old oldheader;
     if (tcp_read((char*)&oldheader, sizeof(oldheader)) < 0)
@@ -1694,7 +1694,7 @@ int Pipe::read_message(Message **pm)
     header.src = oldheader.src.name;
     header.reserved = oldheader.reserved;
     header.crc = oldheader.crc;
-    header_crc = ceph_crc32c_le(0, (unsigned char *)&oldheader, sizeof(oldheader) - sizeof(oldheader.crc));
+    header_crc = ceph_crc32c(0, (unsigned char *)&oldheader, sizeof(oldheader) - sizeof(oldheader.crc));
   }
 
   ldout(msgr->cct,20) << "reader got envelope type=" << header.type
@@ -2028,8 +2028,8 @@ int Pipe::write_message(ceph_msg_header& header, ceph_msg_footer& footer, buffer
     oldheader.src.addr = connection_state->get_peer_addr();
     oldheader.orig_src = oldheader.src;
     oldheader.reserved = header.reserved;
-    oldheader.crc = ceph_crc32c_le(0, (unsigned char*)&oldheader,
-                             sizeof(oldheader) - sizeof(oldheader.crc));
+    oldheader.crc = ceph_crc32c(0, (unsigned char*)&oldheader,
+                               sizeof(oldheader) - sizeof(oldheader.crc));
     msgvec[msg.msg_iovlen].iov_base = (char*)&oldheader;
     msgvec[msg.msg_iovlen].iov_len = sizeof(oldheader);
     msglen += sizeof(oldheader);