Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
00018 #define ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_
00019
00020 #include <string.h>
00021 #include <cstdint>
00022
00023 #include "absl/base/attributes.h"
00024
00025
00026
00027
00028
00029
00030
00031 #if defined(__cplusplus)
00032
00033 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) ||\
00034 defined(MEMORY_SANITIZER)
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include <stdint.h>
00048
00049 extern "C" {
00050 uint16_t __sanitizer_unaligned_load16(const void *p);
00051 uint32_t __sanitizer_unaligned_load32(const void *p);
00052 uint64_t __sanitizer_unaligned_load64(const void *p);
00053 void __sanitizer_unaligned_store16(void *p, uint16_t v);
00054 void __sanitizer_unaligned_store32(void *p, uint32_t v);
00055 void __sanitizer_unaligned_store64(void *p, uint64_t v);
00056 }
00057
00058 namespace absl {
00059 namespace base_internal {
00060
00061 inline uint16_t UnalignedLoad16(const void *p) {
00062 return __sanitizer_unaligned_load16(p);
00063 }
00064
00065 inline uint32_t UnalignedLoad32(const void *p) {
00066 return __sanitizer_unaligned_load32(p);
00067 }
00068
00069 inline uint64_t UnalignedLoad64(const void *p) {
00070 return __sanitizer_unaligned_load64(p);
00071 }
00072
00073 inline void UnalignedStore16(void *p, uint16_t v) {
00074 __sanitizer_unaligned_store16(p, v);
00075 }
00076
00077 inline void UnalignedStore32(void *p, uint32_t v) {
00078 __sanitizer_unaligned_store32(p, v);
00079 }
00080
00081 inline void UnalignedStore64(void *p, uint64_t v) {
00082 __sanitizer_unaligned_store64(p, v);
00083 }
00084
00085 }
00086 }
00087
00088 #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
00089 (absl::base_internal::UnalignedLoad16(_p))
00090 #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
00091 (absl::base_internal::UnalignedLoad32(_p))
00092 #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
00093 (absl::base_internal::UnalignedLoad64(_p))
00094
00095 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
00096 (absl::base_internal::UnalignedStore16(_p, _val))
00097 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
00098 (absl::base_internal::UnalignedStore32(_p, _val))
00099 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
00100 (absl::base_internal::UnalignedStore64(_p, _val))
00101
00102 #else
00103
00104 namespace absl {
00105 namespace base_internal {
00106
00107 inline uint16_t UnalignedLoad16(const void *p) {
00108 uint16_t t;
00109 memcpy(&t, p, sizeof t);
00110 return t;
00111 }
00112
00113 inline uint32_t UnalignedLoad32(const void *p) {
00114 uint32_t t;
00115 memcpy(&t, p, sizeof t);
00116 return t;
00117 }
00118
00119 inline uint64_t UnalignedLoad64(const void *p) {
00120 uint64_t t;
00121 memcpy(&t, p, sizeof t);
00122 return t;
00123 }
00124
00125 inline void UnalignedStore16(void *p, uint16_t v) { memcpy(p, &v, sizeof v); }
00126
00127 inline void UnalignedStore32(void *p, uint32_t v) { memcpy(p, &v, sizeof v); }
00128
00129 inline void UnalignedStore64(void *p, uint64_t v) { memcpy(p, &v, sizeof v); }
00130
00131 }
00132 }
00133
00134 #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \
00135 (absl::base_internal::UnalignedLoad16(_p))
00136 #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \
00137 (absl::base_internal::UnalignedLoad32(_p))
00138 #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \
00139 (absl::base_internal::UnalignedLoad64(_p))
00140
00141 #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \
00142 (absl::base_internal::UnalignedStore16(_p, _val))
00143 #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \
00144 (absl::base_internal::UnalignedStore32(_p, _val))
00145 #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \
00146 (absl::base_internal::UnalignedStore64(_p, _val))
00147
00148 #endif
00149
00150 #endif // defined(__cplusplus), end of unaligned API
00151
00152 #endif // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_