]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commit
blobhash: do not use cast for unaligned access
authorKefu Chai <kchai@redhat.com>
Wed, 15 May 2019 04:46:10 +0000 (12:46 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 28 May 2019 13:22:26 +0000 (21:22 +0800)
commitec4065af4d777b1d48a6c3bf4e0b8c39c818d0bb
tree91f046e370d7d6130e550849038d4905a1242ef2
parentf1ea02970b4bae5a5e5d1bccacb3c8ab45372a5e
blobhash: do not use cast for unaligned access

* remove the uncorrect comment. as std::hash<> does not apply to a
  customized type. see https://en.cppreference.com/w/cpp/utility/hash
* do not use cast for accessing an uint32_t by dereferencing (void *)
  or (char *) pointer. because the alignment requirement of `uint32_t`
  is stricter than that of `void*` or `char *`. we need to do an
  explicit memcpy() for accessing the uint32_t pointed by the pointer.
  see also https://www.kernel.org/doc/Documentation/unaligned-memory-access.txt
* instead of using a loop for mixing the last few bytes. use a switch-
  case. so GCC-9 won't complain with
```
../src/include/blobhash.h:38:10: warning: iteration 4 invokes undefined
behavior [-Waggressive-loop-optimizations]
   38 |       sh += 8;
      |       ~~~^~~~
../src/include/blobhash.h:36:12: note: within this loop
   36 |     while (len) {
      |            ^~~
```
  at seeing `sh += 8;`
* instead of mixing the last bits repeatly by derefencing `p` using
  `uint32_t`, while move it forwards with step size of `1`, mix
  the bits byte-wise.
* include <cstdint>. this header file is supposed to be self-contained.
* use `std::uint32_t` instead of using `uint32_t`. we cannot assume
  somebody is `using namespace std` or `using std::uint32_t` for us.
* mark the operator() `const noexcept`. see
  https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c89-make-a-hash-noexcept
  for the rationale behind this

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/blobhash.h
src/msg/msg_types.h