unaligned_access.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "absl/base/nullability.h"
  23. // unaligned APIs
  24. // Portable handling of unaligned loads, stores, and copies.
  25. // The unaligned API is C++ only. The declarations use C++ features
  26. // (namespaces, inline) which are absent or incompatible in C.
  27. #if defined(__cplusplus)
  28. namespace absl {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace base_internal {
  31. inline uint16_t UnalignedLoad16(absl::Nonnull<const void *> p) {
  32. uint16_t t;
  33. memcpy(&t, p, sizeof t);
  34. return t;
  35. }
  36. inline uint32_t UnalignedLoad32(absl::Nonnull<const void *> p) {
  37. uint32_t t;
  38. memcpy(&t, p, sizeof t);
  39. return t;
  40. }
  41. inline uint64_t UnalignedLoad64(absl::Nonnull<const void *> p) {
  42. uint64_t t;
  43. memcpy(&t, p, sizeof t);
  44. return t;
  45. }
  46. inline void UnalignedStore16(absl::Nonnull<void *> p, uint16_t v) {
  47. memcpy(p, &v, sizeof v);
  48. }
  49. inline void UnalignedStore32(absl::Nonnull<void *> p, uint32_t v) {
  50. memcpy(p, &v, sizeof v);
  51. }
  52. inline void UnalignedStore64(absl::Nonnull<void *> p, uint64_t v) {
  53. memcpy(p, &v, sizeof v);
  54. }
  55. } // namespace base_internal
  56. ABSL_NAMESPACE_END
  57. } // namespace absl
  58. #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
  59. (absl::base_internal::UnalignedLoad16(_p))
  60. #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
  61. (absl::base_internal::UnalignedLoad32(_p))
  62. #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
  63. (absl::base_internal::UnalignedLoad64(_p))
  64. #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
  65. (absl::base_internal::UnalignedStore16(_p, _val))
  66. #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
  67. (absl::base_internal::UnalignedStore32(_p, _val))
  68. #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
  69. (absl::base_internal::UnalignedStore64(_p, _val))
  70. #endif // defined(__cplusplus), end of unaligned API
  71. #endif // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_