/src/LPM/external.protobuf/include/google/protobuf/port.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Protocol Buffers - Google's data interchange format |
2 | | // Copyright 2008 Google Inc. All rights reserved. |
3 | | // https://developers.google.com/protocol-buffers/ |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without |
6 | | // modification, are permitted provided that the following conditions are |
7 | | // met: |
8 | | // |
9 | | // * Redistributions of source code must retain the above copyright |
10 | | // notice, this list of conditions and the following disclaimer. |
11 | | // * Redistributions in binary form must reproduce the above |
12 | | // copyright notice, this list of conditions and the following disclaimer |
13 | | // in the documentation and/or other materials provided with the |
14 | | // distribution. |
15 | | // * Neither the name of Google Inc. nor the names of its |
16 | | // contributors may be used to endorse or promote products derived from |
17 | | // this software without specific prior written permission. |
18 | | // |
19 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | | |
31 | | // A common header that is included across all protobuf headers. We do our best |
32 | | // to avoid #defining any macros here; instead we generally put macros in |
33 | | // port_def.inc and port_undef.inc so they are not visible from outside of |
34 | | // protobuf. |
35 | | |
36 | | #ifndef GOOGLE_PROTOBUF_PORT_H__ |
37 | | #define GOOGLE_PROTOBUF_PORT_H__ |
38 | | |
39 | | #include <cassert> |
40 | | #include <cstddef> |
41 | | #include <new> |
42 | | #include <string> |
43 | | #include <type_traits> |
44 | | |
45 | | #if PROTOBUF_RTTI |
46 | | #include <typeinfo> |
47 | | #endif |
48 | | |
49 | | |
50 | | #include "absl/meta/type_traits.h" |
51 | | #include "absl/strings/string_view.h" |
52 | | #include "absl/types/optional.h" |
53 | | |
54 | | // must be last |
55 | | #include "google/protobuf/port_def.inc" |
56 | | |
57 | | |
58 | | namespace google { |
59 | | namespace protobuf { |
60 | | |
61 | | class MessageLite; |
62 | | |
63 | | namespace internal { |
64 | | |
65 | | |
66 | | // See comments on `AllocateAtLeast` for information on size returning new. |
67 | | struct SizedPtr { |
68 | | void* p; |
69 | | size_t n; |
70 | | }; |
71 | | |
72 | | // Debug hook allowing setting up test scenarios for AllocateAtLeast usage. |
73 | | using AllocateAtLeastHookFn = SizedPtr (*)(size_t, void*); |
74 | | |
75 | | // `AllocAtLeastHook` API |
76 | | constexpr bool HaveAllocateAtLeastHook(); |
77 | | void SetAllocateAtLeastHook(AllocateAtLeastHookFn fn, void* context = nullptr); |
78 | | |
79 | | #if !defined(NDEBUG) && defined(ABSL_HAVE_THREAD_LOCAL) && \ |
80 | | defined(__cpp_inline_variables) |
81 | | |
82 | | // Hook data for current thread. These vars must not be accessed directly, use |
83 | | // the 'HaveAllocateAtLeastHook()` and `SetAllocateAtLeastHook()` API instead. |
84 | | inline thread_local AllocateAtLeastHookFn allocate_at_least_hook = nullptr; |
85 | | inline thread_local void* allocate_at_least_hook_context = nullptr; |
86 | | |
87 | | constexpr bool HaveAllocateAtLeastHook() { return true; } |
88 | | inline void SetAllocateAtLeastHook(AllocateAtLeastHookFn fn, void* context) { |
89 | | allocate_at_least_hook = fn; |
90 | | allocate_at_least_hook_context = context; |
91 | | } |
92 | | |
93 | | #else // !NDEBUG && ABSL_HAVE_THREAD_LOCAL && __cpp_inline_variables |
94 | | |
95 | 0 | constexpr bool HaveAllocateAtLeastHook() { return false; } |
96 | 0 | inline void SetAllocateAtLeastHook(AllocateAtLeastHookFn fn, void* context) {} |
97 | | |
98 | | #endif // !NDEBUG && ABSL_HAVE_THREAD_LOCAL && __cpp_inline_variables |
99 | | |
100 | | // Allocates at least `size` bytes. This function follows the c++ language |
101 | | // proposal from D0901R10 (http://wg21.link/D0901R10) and will be implemented |
102 | | // in terms of the new operator new semantics when available. The allocated |
103 | | // memory should be released by a call to `SizedDelete` or `::operator delete`. |
104 | 0 | inline SizedPtr AllocateAtLeast(size_t size) { |
105 | 0 | #if !defined(NDEBUG) && defined(ABSL_HAVE_THREAD_LOCAL) && \ |
106 | 0 | defined(__cpp_inline_variables) |
107 | 0 | if (allocate_at_least_hook != nullptr) { |
108 | 0 | return allocate_at_least_hook(size, allocate_at_least_hook_context); |
109 | 0 | } |
110 | 0 | #endif // !NDEBUG && ABSL_HAVE_THREAD_LOCAL && __cpp_inline_variables |
111 | 0 | return {::operator new(size), size}; |
112 | 0 | } |
113 | | |
114 | 0 | inline void SizedDelete(void* p, size_t size) { |
115 | 0 | #if defined(__cpp_sized_deallocation) |
116 | 0 | ::operator delete(p, size); |
117 | 0 | #else |
118 | 0 | // Avoid -Wunused-parameter |
119 | 0 | (void)size; |
120 | 0 | ::operator delete(p); |
121 | 0 | #endif |
122 | 0 | } |
123 | 0 | inline void SizedArrayDelete(void* p, size_t size) { |
124 | 0 | #if defined(__cpp_sized_deallocation) |
125 | 0 | ::operator delete[](p, size); |
126 | 0 | #else |
127 | 0 | // Avoid -Wunused-parameter |
128 | 0 | (void)size; |
129 | 0 | ::operator delete[](p); |
130 | 0 | #endif |
131 | 0 | } |
132 | | |
133 | | // Tag type used to invoke the constinit constructor overload of classes |
134 | | // such as ArenaStringPtr and MapFieldBase. Such constructors are internal |
135 | | // implementation details of the library. |
136 | | struct ConstantInitialized { |
137 | | explicit ConstantInitialized() = default; |
138 | | }; |
139 | | |
140 | | // Tag type used to invoke the arena constructor overload of classes such |
141 | | // as ExtensionSet and MapFieldLite in aggregate initialization. These |
142 | | // classes typically don't have move/copy constructors, which rules out |
143 | | // explicit initialization in pre-C++17. |
144 | | struct ArenaInitialized { |
145 | | explicit ArenaInitialized() = default; |
146 | | }; |
147 | | |
148 | | template <typename To, typename From> |
149 | | inline To DownCast(From* f) { |
150 | | static_assert( |
151 | | std::is_base_of<From, typename std::remove_pointer<To>::type>::value, |
152 | | "illegal DownCast"); |
153 | | |
154 | | #if PROTOBUF_RTTI |
155 | | // RTTI: debug mode only! |
156 | | assert(f == nullptr || dynamic_cast<To>(f) != nullptr); |
157 | | #endif |
158 | | return static_cast<To>(f); |
159 | | } |
160 | | |
161 | | template <typename ToRef, typename From> |
162 | | inline ToRef DownCast(From& f) { |
163 | | using To = typename std::remove_reference<ToRef>::type; |
164 | | static_assert(std::is_base_of<From, To>::value, "illegal DownCast"); |
165 | | |
166 | | #if PROTOBUF_RTTI |
167 | | // RTTI: debug mode only! |
168 | | assert(dynamic_cast<To*>(&f) != nullptr); |
169 | | #endif |
170 | | return *static_cast<To*>(&f); |
171 | | } |
172 | | |
173 | | // Looks up the name of `T` via RTTI, if RTTI is available. |
174 | | template <typename T> |
175 | | inline absl::optional<absl::string_view> RttiTypeName() { |
176 | | #if PROTOBUF_RTTI |
177 | | return typeid(T).name(); |
178 | | #else |
179 | | return absl::nullopt; |
180 | | #endif |
181 | | } |
182 | | |
183 | | // Helpers for identifying our supported types. |
184 | | template <typename T> |
185 | | struct is_supported_integral_type |
186 | | : absl::disjunction<std::is_same<T, int32_t>, std::is_same<T, uint32_t>, |
187 | | std::is_same<T, int64_t>, std::is_same<T, uint64_t>, |
188 | | std::is_same<T, bool>> {}; |
189 | | |
190 | | template <typename T> |
191 | | struct is_supported_floating_point_type |
192 | | : absl::disjunction<std::is_same<T, float>, std::is_same<T, double>> {}; |
193 | | |
194 | | template <typename T> |
195 | | struct is_supported_string_type |
196 | | : absl::disjunction<std::is_same<T, std::string>> {}; |
197 | | |
198 | | template <typename T> |
199 | | struct is_supported_scalar_type |
200 | | : absl::disjunction<is_supported_integral_type<T>, |
201 | | is_supported_floating_point_type<T>, |
202 | | is_supported_string_type<T>> {}; |
203 | | |
204 | | template <typename T> |
205 | | struct is_supported_message_type |
206 | | : absl::disjunction<std::is_base_of<MessageLite, T>> { |
207 | | static constexpr auto force_complete_type = sizeof(T); |
208 | | }; |
209 | | |
210 | | // To prevent sharing cache lines between threads |
211 | | #ifdef __cpp_aligned_new |
212 | | enum { kCacheAlignment = 64 }; |
213 | | #else |
214 | | enum { kCacheAlignment = alignof(max_align_t) }; // do the best we can |
215 | | #endif |
216 | | |
217 | | // The maximum byte alignment we support. |
218 | | enum { kMaxMessageAlignment = 8 }; |
219 | | |
220 | | } // namespace internal |
221 | | } // namespace protobuf |
222 | | } // namespace google |
223 | | |
224 | | #include "google/protobuf/port_undef.inc" |
225 | | |
226 | | #endif // GOOGLE_PROTOBUF_PORT_H__ |