/src/abseil-cpp/absl/base/internal/unaligned_access.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
17 | | #ifndef ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_ |
18 | | #define ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_ |
19 | | |
20 | | #include <string.h> |
21 | | |
22 | | #include <cstdint> |
23 | | |
24 | | #include "absl/base/attributes.h" |
25 | | #include "absl/base/config.h" |
26 | | #include "absl/base/nullability.h" |
27 | | |
28 | | // unaligned APIs |
29 | | |
30 | | // Portable handling of unaligned loads, stores, and copies. |
31 | | |
32 | | // The unaligned API is C++ only. The declarations use C++ features |
33 | | // (namespaces, inline) which are absent or incompatible in C. |
34 | | #if defined(__cplusplus) |
35 | | namespace absl { |
36 | | ABSL_NAMESPACE_BEGIN |
37 | | namespace base_internal { |
38 | | |
39 | 0 | inline uint16_t UnalignedLoad16(absl::Nonnull<const void *> p) { |
40 | 0 | uint16_t t; |
41 | 0 | memcpy(&t, p, sizeof t); |
42 | 0 | return t; |
43 | 0 | } |
44 | | |
45 | 72 | inline uint32_t UnalignedLoad32(absl::Nonnull<const void *> p) { |
46 | 72 | uint32_t t; |
47 | 72 | memcpy(&t, p, sizeof t); |
48 | 72 | return t; |
49 | 72 | } |
50 | | |
51 | 158 | inline uint64_t UnalignedLoad64(absl::Nonnull<const void *> p) { |
52 | 158 | uint64_t t; |
53 | 158 | memcpy(&t, p, sizeof t); |
54 | 158 | return t; |
55 | 158 | } |
56 | | |
57 | 3.79M | inline void UnalignedStore16(absl::Nonnull<void *> p, uint16_t v) { |
58 | 3.79M | memcpy(p, &v, sizeof v); |
59 | 3.79M | } |
60 | | |
61 | 0 | inline void UnalignedStore32(absl::Nonnull<void *> p, uint32_t v) { |
62 | 0 | memcpy(p, &v, sizeof v); |
63 | 0 | } |
64 | | |
65 | 15.1M | inline void UnalignedStore64(absl::Nonnull<void *> p, uint64_t v) { |
66 | 15.1M | memcpy(p, &v, sizeof v); |
67 | 15.1M | } |
68 | | |
69 | | } // namespace base_internal |
70 | | ABSL_NAMESPACE_END |
71 | | } // namespace absl |
72 | | |
73 | | #define ABSL_INTERNAL_UNALIGNED_LOAD16(_p) \ |
74 | 0 | (absl::base_internal::UnalignedLoad16(_p)) |
75 | | #define ABSL_INTERNAL_UNALIGNED_LOAD32(_p) \ |
76 | | (absl::base_internal::UnalignedLoad32(_p)) |
77 | | #define ABSL_INTERNAL_UNALIGNED_LOAD64(_p) \ |
78 | 6 | (absl::base_internal::UnalignedLoad64(_p)) |
79 | | |
80 | | #define ABSL_INTERNAL_UNALIGNED_STORE16(_p, _val) \ |
81 | 3.79M | (absl::base_internal::UnalignedStore16(_p, _val)) |
82 | | #define ABSL_INTERNAL_UNALIGNED_STORE32(_p, _val) \ |
83 | 0 | (absl::base_internal::UnalignedStore32(_p, _val)) |
84 | | #define ABSL_INTERNAL_UNALIGNED_STORE64(_p, _val) \ |
85 | 15.1M | (absl::base_internal::UnalignedStore64(_p, _val)) |
86 | | |
87 | | #endif // defined(__cplusplus), end of unaligned API |
88 | | |
89 | | #endif // ABSL_BASE_INTERNAL_UNALIGNED_ACCESS_H_ |