You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

unaligned_access.h 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #ifndef ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
  17. #define ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
  18. #include <string.h>
  19. #include <cstdint>
  20. #include "absl/base/attributes.h"
  21. #include "absl/base/config.h"
  22. // unaligned APIs
  23. // Portable handling of unaligned loads, stores, and copies.
  24. // The unaligned API is C++ only. The declarations use C++ features
  25. // (namespaces, inline) which are absent or incompatible in C.
  26. #if defined(__cplusplus)
  27. namespace absl
  28. {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace base_internal
  31. {
  32. inline uint16_t UnalignedLoad16(const void* p)
  33. {
  34. uint16_t t;
  35. memcpy(&t, p, sizeof t);
  36. return t;
  37. }
  38. inline uint32_t UnalignedLoad32(const void* p)
  39. {
  40. uint32_t t;
  41. memcpy(&t, p, sizeof t);
  42. return t;
  43. }
  44. inline uint64_t UnalignedLoad64(const void* p)
  45. {
  46. uint64_t t;
  47. memcpy(&t, p, sizeof t);
  48. return t;
  49. }
  50. inline void UnalignedStore16(void* p, uint16_t v)
  51. {
  52. memcpy(p, &v, sizeof v);
  53. }
  54. inline void UnalignedStore32(void* p, uint32_t v)
  55. {
  56. memcpy(p, &v, sizeof v);
  57. }
  58. inline void UnalignedStore64(void* p, uint64_t v)
  59. {
  60. memcpy(p, &v, sizeof v);
  61. }
  62. } // namespace base_internal
  63. ABSL_NAMESPACE_END
  64. } // namespace absl
  65. #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
  66. (absl::base_internal::UnalignedLoad16(_p))
  67. #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
  68. (absl::base_internal::UnalignedLoad32(_p))
  69. #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
  70. (absl::base_internal::UnalignedLoad64(_p))
  71. #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
  72. (absl::base_internal::UnalignedStore16(_p, _val))
  73. #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
  74. (absl::base_internal::UnalignedStore32(_p, _val))
  75. #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
  76. (absl::base_internal::UnalignedStore64(_p, _val))
  77. #endif // defined(__cplusplus), end of unaligned API
  78. #endif // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_