ONE - On-device Neural Engine
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 Google Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef FLATBUFFERS_BASE_H_
18#define FLATBUFFERS_BASE_H_
19
20// clang-format off
21
22// If activate should be declared and included first.
23#if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && \
24 defined(_MSC_VER) && defined(_DEBUG)
25 // The _CRTDBG_MAP_ALLOC inside <crtdbg.h> will replace
26 // calloc/free (etc) to its debug version using #define directives.
27 #define _CRTDBG_MAP_ALLOC
28 #include <stdlib.h>
29 #include <crtdbg.h>
30 // Replace operator new by trace-enabled version.
31 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
32 #define new DEBUG_NEW
33#endif
34
35#if !defined(FLATBUFFERS_ASSERT)
36#include <assert.h>
37#define FLATBUFFERS_ASSERT assert
38#elif defined(FLATBUFFERS_ASSERT_INCLUDE)
39// Include file with forward declaration
40#include FLATBUFFERS_ASSERT_INCLUDE
41#endif
42
43#ifndef ARDUINO
44#include <cstdint>
45#endif
46
47#include <cstddef>
48#include <cstdlib>
49#include <cstring>
50
51#if defined(ARDUINO) && !defined(ARDUINOSTL_M_H)
52 #include <utility.h>
53#else
54 #include <utility>
55#endif
56
57#include <string>
58#include <type_traits>
59#include <vector>
60#include <set>
61#include <algorithm>
62#include <iterator>
63#include <memory>
64
65#if defined(__unix__) && !defined(FLATBUFFERS_LOCALE_INDEPENDENT)
66 #include <unistd.h>
67#endif
68
69#ifdef _STLPORT_VERSION
70 #define FLATBUFFERS_CPP98_STL
71#endif
72
73#ifdef __ANDROID__
74 #include <android/api-level.h>
75#endif
76
77#if defined(__ICCARM__)
78#include <intrinsics.h>
79#endif
80
81// Note the __clang__ check is needed, because clang presents itself
82// as an older GNUC compiler (4.2).
83// Clang 3.3 and later implement all of the ISO C++ 2011 standard.
84// Clang 3.4 and later implement all of the ISO C++ 2014 standard.
85// http://clang.llvm.org/cxx_status.html
86
87// Note the MSVC value '__cplusplus' may be incorrect:
88// The '__cplusplus' predefined macro in the MSVC stuck at the value 199711L,
89// indicating (erroneously!) that the compiler conformed to the C++98 Standard.
90// This value should be correct starting from MSVC2017-15.7-Preview-3.
91// The '__cplusplus' will be valid only if MSVC2017-15.7-P3 and the `/Zc:__cplusplus` switch is set.
92// Workaround (for details see MSDN):
93// Use the _MSC_VER and _MSVC_LANG definition instead of the __cplusplus for compatibility.
94// The _MSVC_LANG macro reports the Standard version regardless of the '/Zc:__cplusplus' switch.
95
96#if defined(__GNUC__) && !defined(__clang__)
97 #define FLATBUFFERS_GCC (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
98#else
99 #define FLATBUFFERS_GCC 0
100#endif
101
102#if defined(__clang__)
103 #define FLATBUFFERS_CLANG (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
104#else
105 #define FLATBUFFERS_CLANG 0
106#endif
107
109#if __cplusplus <= 199711L && \
110 (!defined(_MSC_VER) || _MSC_VER < 1600) && \
111 (!defined(__GNUC__) || \
112 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400))
113 #error A C++11 compatible compiler with support for the auto typing is \
114 required for FlatBuffers.
115 #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__
116#endif
117
118#if !defined(__clang__) && \
119 defined(__GNUC__) && \
120 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600)
121 // Backwards compatibility for g++ 4.4, and 4.5 which don't have the nullptr
122 // and constexpr keywords. Note the __clang__ check is needed, because clang
123 // presents itself as an older GNUC compiler.
124 #ifndef nullptr_t
125 const class nullptr_t {
126 public:
127 template<class T> inline operator T*() const { return 0; }
128 private:
129 void operator&() const;
130 } nullptr = {};
131 #endif
132 #ifndef constexpr
133 #define constexpr const
134 #endif
135#endif
136
137// The wire format uses a little endian encoding (since that's efficient for
138// the common platforms).
139#if defined(__s390x__)
140 #define FLATBUFFERS_LITTLEENDIAN 0
141#endif // __s390x__
142#if !defined(FLATBUFFERS_LITTLEENDIAN)
143 #if defined(__GNUC__) || defined(__clang__) || defined(__ICCARM__)
144 #if (defined(__BIG_ENDIAN__) || \
145 (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
146 #define FLATBUFFERS_LITTLEENDIAN 0
147 #else
148 #define FLATBUFFERS_LITTLEENDIAN 1
149 #endif // __BIG_ENDIAN__
150 #elif defined(_MSC_VER)
151 #if defined(_M_PPC)
152 #define FLATBUFFERS_LITTLEENDIAN 0
153 #else
154 #define FLATBUFFERS_LITTLEENDIAN 1
155 #endif
156 #else
157 #error Unable to determine endianness, define FLATBUFFERS_LITTLEENDIAN.
158 #endif
159#endif // !defined(FLATBUFFERS_LITTLEENDIAN)
160
161#define FLATBUFFERS_VERSION_MAJOR 2
162#define FLATBUFFERS_VERSION_MINOR 0
163#define FLATBUFFERS_VERSION_REVISION 0
164#define FLATBUFFERS_STRING_EXPAND(X) #X
165#define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X)
166namespace flatbuffers {
167 // Returns version as string "MAJOR.MINOR.REVISION".
168 const char* FLATBUFFERS_VERSION();
169}
170
171#if (!defined(_MSC_VER) || _MSC_VER > 1600) && \
172 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) || \
173 defined(__clang__)
174 #define FLATBUFFERS_FINAL_CLASS final
175 #define FLATBUFFERS_OVERRIDE override
176 #define FLATBUFFERS_EXPLICIT_CPP11 explicit
177 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE : flatbuffers::voffset_t
178#else
179 #define FLATBUFFERS_FINAL_CLASS
180 #define FLATBUFFERS_OVERRIDE
181 #define FLATBUFFERS_EXPLICIT_CPP11
182 #define FLATBUFFERS_VTABLE_UNDERLYING_TYPE
183#endif
184
185#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
186 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
187 (defined(__cpp_constexpr) && __cpp_constexpr >= 200704)
188 #define FLATBUFFERS_CONSTEXPR constexpr
189 #define FLATBUFFERS_CONSTEXPR_CPP11 constexpr
190 #define FLATBUFFERS_CONSTEXPR_DEFINED
191#else
192 #define FLATBUFFERS_CONSTEXPR const
193 #define FLATBUFFERS_CONSTEXPR_CPP11
194#endif
195
196#if (defined(__cplusplus) && __cplusplus >= 201402L) || \
197 (defined(__cpp_constexpr) && __cpp_constexpr >= 201304)
198 #define FLATBUFFERS_CONSTEXPR_CPP14 FLATBUFFERS_CONSTEXPR_CPP11
199#else
200 #define FLATBUFFERS_CONSTEXPR_CPP14
201#endif
202
203#if (defined(__GXX_EXPERIMENTAL_CXX0X__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) || \
204 (defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 190023026)) || \
205 defined(__clang__)
206 #define FLATBUFFERS_NOEXCEPT noexcept
207#else
208 #define FLATBUFFERS_NOEXCEPT
209#endif
210
211// NOTE: the FLATBUFFERS_DELETE_FUNC macro may change the access mode to
212// private, so be sure to put it at the end or reset access mode explicitly.
213#if (!defined(_MSC_VER) || _MSC_FULL_VER >= 180020827) && \
214 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)) || \
215 defined(__clang__)
216 #define FLATBUFFERS_DELETE_FUNC(func) func = delete
217#else
218 #define FLATBUFFERS_DELETE_FUNC(func) private: func
219#endif
220
221#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
222 (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
223 defined(__clang__)
224 #define FLATBUFFERS_DEFAULT_DECLARATION
225#endif
226
227// Check if we can use template aliases
228// Not possible if Microsoft Compiler before 2012
229// Possible is the language feature __cpp_alias_templates is defined well
230// Or possible if the C++ std is C+11 or newer
231#if (defined(_MSC_VER) && _MSC_VER > 1700 /* MSVC2012 */) \
232 || (defined(__cpp_alias_templates) && __cpp_alias_templates >= 200704) \
233 || (defined(__cplusplus) && __cplusplus >= 201103L)
234 #define FLATBUFFERS_TEMPLATES_ALIASES
235#endif
236
237#ifndef FLATBUFFERS_HAS_STRING_VIEW
238 // Only provide flatbuffers::string_view if __has_include can be used
239 // to detect a header that provides an implementation
240 #if defined(__has_include)
241 // Check for std::string_view (in c++17)
242 #if __has_include(<string_view>) && (__cplusplus >= 201606 || (defined(_HAS_CXX17) && _HAS_CXX17))
243 #include <string_view>
244 namespace flatbuffers {
245 typedef std::string_view string_view;
246 }
247 #define FLATBUFFERS_HAS_STRING_VIEW 1
248 // Check for std::experimental::string_view (in c++14, compiler-dependent)
249 #elif __has_include(<experimental/string_view>) && (__cplusplus >= 201411)
250 #include <experimental/string_view>
251 namespace flatbuffers {
252 typedef std::experimental::string_view string_view;
253 }
254 #define FLATBUFFERS_HAS_STRING_VIEW 1
255 // Check for absl::string_view
256 #elif __has_include("absl/strings/string_view.h")
257 #include "absl/strings/string_view.h"
258 namespace flatbuffers {
259 typedef absl::string_view string_view;
260 }
261 #define FLATBUFFERS_HAS_STRING_VIEW 1
262 #endif
263 #endif // __has_include
264#endif // !FLATBUFFERS_HAS_STRING_VIEW
265
266#ifndef FLATBUFFERS_HAS_NEW_STRTOD
267 // Modern (C++11) strtod and strtof functions are available for use.
268 // 1) nan/inf strings as argument of strtod;
269 // 2) hex-float as argument of strtod/strtof.
270 #if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
271 (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)) || \
272 (defined(__clang__))
273 #define FLATBUFFERS_HAS_NEW_STRTOD 1
274 #endif
275#endif // !FLATBUFFERS_HAS_NEW_STRTOD
276
277#ifndef FLATBUFFERS_LOCALE_INDEPENDENT
278 // Enable locale independent functions {strtof_l, strtod_l,strtoll_l, strtoull_l}.
279 #if ((defined(_MSC_VER) && _MSC_VER >= 1800) || \
280 (defined(_XOPEN_VERSION) && (_XOPEN_VERSION>=700)) && (!defined(__ANDROID_API__) || (defined(__ANDROID_API__) && (__ANDROID_API__>=21))))
281 #define FLATBUFFERS_LOCALE_INDEPENDENT 1
282 #else
283 #define FLATBUFFERS_LOCALE_INDEPENDENT 0
284 #endif
285#endif // !FLATBUFFERS_LOCALE_INDEPENDENT
286
287// Suppress Undefined Behavior Sanitizer (recoverable only). Usage:
288// - __supress_ubsan__("undefined")
289// - __supress_ubsan__("signed-integer-overflow")
290#if defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))
291 #define __supress_ubsan__(type) __attribute__((no_sanitize(type)))
292#elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 409)
293 #define __supress_ubsan__(type) __attribute__((no_sanitize_undefined))
294#else
295 #define __supress_ubsan__(type)
296#endif
297
298// This is constexpr function used for checking compile-time constants.
299// Avoid `#pragma warning(disable: 4127) // C4127: expression is constant`.
300template<typename T> FLATBUFFERS_CONSTEXPR inline bool IsConstTrue(T t) {
301 return !!t;
302}
303
304// Enable C++ attribute [[]] if std:c++17 or higher.
305#if ((__cplusplus >= 201703L) \
306 || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
307 // All attributes unknown to an implementation are ignored without causing an error.
308 #define FLATBUFFERS_ATTRIBUTE(attr) [[attr]]
309
310 #define FLATBUFFERS_FALLTHROUGH() [[fallthrough]]
311#else
312 #define FLATBUFFERS_ATTRIBUTE(attr)
313
314 #if FLATBUFFERS_CLANG >= 30800
315 #define FLATBUFFERS_FALLTHROUGH() [[clang::fallthrough]]
316 #elif FLATBUFFERS_GCC >= 70300
317 #define FLATBUFFERS_FALLTHROUGH() [[gnu::fallthrough]]
318 #else
319 #define FLATBUFFERS_FALLTHROUGH()
320 #endif
321#endif
322
324
326namespace flatbuffers {
327
329// Our default offset / size type, 32bit on purpose on 64bit systems.
330// Also, using a consistent offset type maintains compatibility of serialized
331// offset values between 32bit and 64bit systems.
332typedef uint32_t uoffset_t;
333
334// Signed offsets for references that can go in both directions.
335typedef int32_t soffset_t;
336
337// Offset/index used in v-tables, can be changed to uint8_t in
338// format forks to save a bit of space if desired.
339typedef uint16_t voffset_t;
340
341typedef uintmax_t largest_scalar_t;
342
343// In 32bits, this evaluates to 2GB - 1
344#define FLATBUFFERS_MAX_BUFFER_SIZE ((1ULL << (sizeof(::flatbuffers::soffset_t) * 8 - 1)) - 1)
345
346// We support aligning the contents of buffers up to this size.
347#define FLATBUFFERS_MAX_ALIGNMENT 16
348
349inline bool VerifyAlignmentRequirements(size_t align, size_t min_align = 1) {
350 return (min_align <= align) && (align <= (FLATBUFFERS_MAX_ALIGNMENT)) &&
351 (align & (align - 1)) == 0; // must be power of 2
352}
353
354#if defined(_MSC_VER)
355 #pragma warning(disable: 4351) // C4351: new behavior: elements of array ... will be default initialized
356 #pragma warning(push)
357 #pragma warning(disable: 4127) // C4127: conditional expression is constant
358#endif
359
360template<typename T> T EndianSwap(T t) {
361 #if defined(_MSC_VER)
362 #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
363 #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
364 #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
365 #elif defined(__ICCARM__)
366 #define FLATBUFFERS_BYTESWAP16 __REV16
367 #define FLATBUFFERS_BYTESWAP32 __REV
368 #define FLATBUFFERS_BYTESWAP64(x) \
369 ((__REV(static_cast<uint32_t>(x >> 32U))) | (static_cast<uint64_t>(__REV(static_cast<uint32_t>(x)))) << 32U)
370 #else
371 #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408 && !defined(__clang__)
372 // __builtin_bswap16 was missing prior to GCC 4.8.
373 #define FLATBUFFERS_BYTESWAP16(x) \
374 static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
375 #else
376 #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
377 #endif
378 #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
379 #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
380 #endif
381 if (sizeof(T) == 1) { // Compile-time if-then's.
382 return t;
383 } else if (sizeof(T) == 2) {
384 union { T t; uint16_t i; } u = { t };
385 u.i = FLATBUFFERS_BYTESWAP16(u.i);
386 return u.t;
387 } else if (sizeof(T) == 4) {
388 union { T t; uint32_t i; } u = { t };
389 u.i = FLATBUFFERS_BYTESWAP32(u.i);
390 return u.t;
391 } else if (sizeof(T) == 8) {
392 union { T t; uint64_t i; } u = { t };
393 u.i = FLATBUFFERS_BYTESWAP64(u.i);
394 return u.t;
395 } else {
397 return t;
398 }
399}
400
401#if defined(_MSC_VER)
402 #pragma warning(pop)
403#endif
404
405
406template<typename T> T EndianScalar(T t) {
407 #if FLATBUFFERS_LITTLEENDIAN
408 return t;
409 #else
410 return EndianSwap(t);
411 #endif
412}
413
414template<typename T>
415// UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
416__supress_ubsan__("alignment")
417T ReadScalar(const void *p) {
418 return EndianScalar(*reinterpret_cast<const T *>(p));
419}
420
421// See https://github.com/google/flatbuffers/issues/5950
422
423#if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
424 #pragma GCC diagnostic push
425 #pragma GCC diagnostic ignored "-Wstringop-overflow"
426#endif
427
428template<typename T>
429// UBSAN: C++ aliasing type rules, see std::bit_cast<> for details.
430__supress_ubsan__("alignment")
431void WriteScalar(void *p, T t) {
432 *reinterpret_cast<T *>(p) = EndianScalar(t);
433}
434
435template<typename T> struct Offset;
436template<typename T> __supress_ubsan__("alignment") void WriteScalar(void *p, Offset<T> t) {
437 *reinterpret_cast<uoffset_t *>(p) = EndianScalar(t.o);
438}
439
440#if (FLATBUFFERS_GCC >= 100000) && (FLATBUFFERS_GCC < 110000)
441 #pragma GCC diagnostic pop
442#endif
443
444// Computes how many bytes you'd have to pad to be able to write an
445// "scalar_size" scalar if the buffer had grown to "buf_size" (downwards in
446// memory).
447__supress_ubsan__("unsigned-integer-overflow")
448inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) {
449 return ((~buf_size) + 1) & (scalar_size - 1);
450}
451
452} // namespace flatbuffers
453#endif // FLATBUFFERS_BASE_H_
int Offset(const Dims< 4 > &dims, int i0, int i1, int i2, int i3)
Definition Dims.h:64
#define FLATBUFFERS_ASSERT
Definition base.h:37
__supress_ubsan__("float-cast-overflow") inline void strtoval_impl(float *val