1 /*
2     Copyright (c) 2005-2020 Intel Corporation
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         http://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 __TBB_tbb_stddef_H
18 #define __TBB_tbb_stddef_H
19 
20 // Marketing-driven product version
21 #define TBB_VERSION_MAJOR 2020
22 #define TBB_VERSION_MINOR 3
23 
24 // Engineering-focused interface version
25 #define TBB_INTERFACE_VERSION 11103
26 #define TBB_INTERFACE_VERSION_MAJOR TBB_INTERFACE_VERSION/1000
27 
28 // The oldest major interface version still supported
29 // To be used in SONAME, manifests, etc.
30 #define TBB_COMPATIBLE_INTERFACE_VERSION 2
31 
32 #define __TBB_STRING_AUX(x) #x
33 #define __TBB_STRING(x) __TBB_STRING_AUX(x)
34 
35 // We do not need defines below for resource processing on windows
36 #if !defined RC_INVOKED
37 
38 // Define groups for Doxygen documentation
39 /**
40  * @defgroup algorithms         Algorithms
41  * @defgroup containers         Containers
42  * @defgroup memory_allocation  Memory Allocation
43  * @defgroup synchronization    Synchronization
44  * @defgroup timing             Timing
45  * @defgroup task_scheduling    Task Scheduling
46  */
47 
48 // Simple text that is displayed on the main page of Doxygen documentation.
49 /**
50  * \mainpage Main Page
51  *
52  * Click the tabs above for information about the
53  * - <a href="./modules.html">Modules</a> (groups of functionality) implemented by the library
54  * - <a href="./annotated.html">Classes</a> provided by the library
55  * - <a href="./files.html">Files</a> constituting the library.
56  * .
57  * Please note that significant part of TBB functionality is implemented in the form of
58  * template functions, descriptions of which are not accessible on the <a href="./annotated.html">Classes</a>
59  * tab. Use <a href="./modules.html">Modules</a> or <a href="./namespacemembers.html">Namespace/Namespace Members</a>
60  * tabs to find them.
61  *
62  * Additional pieces of information can be found here
63  * - \subpage concepts
64  * .
65  */
66 
67 /** \page concepts TBB concepts
68 
69     A concept is a set of requirements to a type, which are necessary and sufficient
70     for the type to model a particular behavior or a set of behaviors. Some concepts
71     are specific to a particular algorithm (e.g. algorithm body), while other ones
72     are common to several algorithms (e.g. range concept).
73 
74     All TBB algorithms make use of different classes implementing various concepts.
75     Implementation classes are supplied by the user as type arguments of template
76     parameters and/or as objects passed as function call arguments. The library
77     provides predefined  implementations of some concepts (e.g. several kinds of
78     \ref range_req "ranges"), while other ones must always be implemented by the user.
79 
80     TBB defines a set of minimal requirements each concept must conform to. Here is
81     the list of different concepts hyperlinked to the corresponding requirements specifications:
82     - \subpage range_req
83     - \subpage parallel_do_body_req
84     - \subpage parallel_for_body_req
85     - \subpage parallel_reduce_body_req
86     - \subpage parallel_scan_body_req
87     - \subpage parallel_sort_iter_req
88 **/
89 
90 // tbb_config.h should be included the first since it contains macro definitions used in other headers
91 #include "tbb_config.h"
92 
93 #if _MSC_VER >=1400
94     #define __TBB_EXPORTED_FUNC   __cdecl
95     #define __TBB_EXPORTED_METHOD __thiscall
96 #else
97     #define __TBB_EXPORTED_FUNC
98     #define __TBB_EXPORTED_METHOD
99 #endif
100 
101 #if __INTEL_COMPILER || _MSC_VER
102 #define __TBB_NOINLINE(decl) __declspec(noinline) decl
103 #elif __GNUC__
104 #define __TBB_NOINLINE(decl) decl __attribute__ ((noinline))
105 #else
106 #define __TBB_NOINLINE(decl) decl
107 #endif
108 
109 #if __TBB_NOEXCEPT_PRESENT
110 #define __TBB_NOEXCEPT(expression) noexcept(expression)
111 #else
112 #define __TBB_NOEXCEPT(expression)
113 #endif
114 
115 #include <cstddef>      /* Need size_t and ptrdiff_t */
116 
117 #if _MSC_VER
118     #define __TBB_tbb_windef_H
119     #include "internal/_tbb_windef.h"
120     #undef __TBB_tbb_windef_H
121 #endif
122 #if !defined(_MSC_VER) || _MSC_VER>=1600
123     #include <stdint.h>
124 #endif
125 
126 //! Type for an assertion handler
127 typedef void(*assertion_handler_type)( const char* filename, int line, const char* expression, const char * comment );
128 
129 #if __TBBMALLOC_BUILD
130 namespace rml { namespace internal {
131  #define __TBB_ASSERT_RELEASE(predicate,message) ((predicate)?((void)0) : rml::internal::assertion_failure(__FILE__,__LINE__,#predicate,message))
132 #else
133 namespace tbb {
134  #define __TBB_ASSERT_RELEASE(predicate,message) ((predicate)?((void)0) : tbb::assertion_failure(__FILE__,__LINE__,#predicate,message))
135 #endif
136 
137     //! Set assertion handler and return previous value of it.
138     assertion_handler_type __TBB_EXPORTED_FUNC set_assertion_handler( assertion_handler_type new_handler );
139 
140     //! Process an assertion failure.
141     /** Normally called from __TBB_ASSERT macro.
142         If assertion handler is null, print message for assertion failure and abort.
143         Otherwise call the assertion handler. */
144     void __TBB_EXPORTED_FUNC assertion_failure( const char* filename, int line, const char* expression, const char* comment );
145 
146 #if __TBBMALLOC_BUILD
147 }}  // namespace rml::internal
148 #else
149 } // namespace tbb
150 #endif
151 
152 #if TBB_USE_ASSERT
153 
154     //! Assert that predicate is true.
155     /** If predicate is false, print assertion failure message.
156         If the comment argument is not NULL, it is printed as part of the failure message.
157         The comment argument has no other effect. */
158     #define __TBB_ASSERT(predicate,message) __TBB_ASSERT_RELEASE(predicate,message)
159 
160     #define __TBB_ASSERT_EX __TBB_ASSERT
161 
162 #else /* !TBB_USE_ASSERT */
163 
164     //! No-op version of __TBB_ASSERT.
165     #define __TBB_ASSERT(predicate,comment) ((void)0)
166     //! "Extended" version is useful to suppress warnings if a variable is only used with an assert
167     #define __TBB_ASSERT_EX(predicate,comment) ((void)(1 && (predicate)))
168 
169 #endif /* !TBB_USE_ASSERT */
170 
171 //! The namespace tbb contains all components of the library.
172 namespace tbb {
173 
174     namespace internal {
175 #if _MSC_VER && _MSC_VER<1600
176         typedef __int8 int8_t;
177         typedef __int16 int16_t;
178         typedef __int32 int32_t;
179         typedef __int64 int64_t;
180         typedef unsigned __int8 uint8_t;
181         typedef unsigned __int16 uint16_t;
182         typedef unsigned __int32 uint32_t;
183         typedef unsigned __int64 uint64_t;
184 #else /* Posix */
185         using ::int8_t;
186         using ::int16_t;
187         using ::int32_t;
188         using ::int64_t;
189         using ::uint8_t;
190         using ::uint16_t;
191         using ::uint32_t;
192         using ::uint64_t;
193 #endif /* Posix */
194     } // namespace internal
195 
196     using std::size_t;
197     using std::ptrdiff_t;
198 
199 //! The function returns the interface version of the TBB shared library being used.
200 /**
201  * The version it returns is determined at runtime, not at compile/link time.
202  * So it can be different than the value of TBB_INTERFACE_VERSION obtained at compile time.
203  */
204 extern "C" int __TBB_EXPORTED_FUNC TBB_runtime_interface_version();
205 
206 /**
207  * @cond INTERNAL
208  * @brief Identifiers declared inside namespace internal should never be used directly by client code.
209  */
210 namespace internal {
211 
212 //! Compile-time constant that is upper bound on cache line/sector size.
213 /** It should be used only in situations where having a compile-time upper
214     bound is more useful than a run-time exact answer.
215     @ingroup memory_allocation */
216 const size_t NFS_MaxLineSize = 128;
217 
218 /** Label for data that may be accessed from different threads, and that may eventually become wrapped
219     in a formal atomic type.
220 
221     Note that no problems have yet been observed relating to the definition currently being empty,
222     even if at least "volatile" would seem to be in order to avoid data sometimes temporarily hiding
223     in a register (although "volatile" as a "poor man's atomic" lacks several other features of a proper
224     atomic, some of which are now provided instead through specialized functions).
225 
226     Note that usage is intentionally compatible with a definition as qualifier "volatile",
227     both as a way to have the compiler help enforce use of the label and to quickly rule out
228     one potential issue.
229 
230     Note however that, with some architecture/compiler combinations, e.g. on IA-64 architecture, "volatile"
231     also has non-portable memory semantics that are needlessly expensive for "relaxed" operations.
232 
233     Note that this must only be applied to data that will not change bit patterns when cast to/from
234     an integral type of the same length; tbb::atomic must be used instead for, e.g., floating-point types.
235 
236     TODO: apply wherever relevant **/
237 #define __TBB_atomic // intentionally empty, see above
238 
239 #if __TBB_OVERRIDE_PRESENT
240 #define __TBB_override override
241 #else
242 #define __TBB_override // formal comment only
243 #endif
244 
245 #if __TBB_CPP17_FALLTHROUGH_PRESENT
246 #define __TBB_fallthrough [[fallthrough]]
247 #elif __TBB_FALLTHROUGH_PRESENT
248 #define __TBB_fallthrough __attribute__ ((fallthrough))
249 #else
250 #define __TBB_fallthrough
251 #endif
252 
253 template<class T, size_t S, size_t R>
254 struct padded_base : T {
255     char pad[S - R];
256 };
257 template<class T, size_t S> struct padded_base<T, S, 0> : T {};
258 
259 //! Pads type T to fill out to a multiple of cache line size.
260 template<class T, size_t S = NFS_MaxLineSize>
261 struct padded : padded_base<T, S, sizeof(T) % S> {};
262 
263 //! Extended variant of the standard offsetof macro
264 /** The standard offsetof macro is not sufficient for TBB as it can be used for
265     POD-types only. The constant 0x1000 (not NULL) is necessary to appease GCC. **/
266 #define __TBB_offsetof(class_name, member_name) \
267     ((ptrdiff_t)&(reinterpret_cast<class_name*>(0x1000)->member_name) - 0x1000)
268 
269 //! Returns address of the object containing a member with the given name and address
270 #define __TBB_get_object_ref(class_name, member_name, member_addr) \
271     (*reinterpret_cast<class_name*>((char*)member_addr - __TBB_offsetof(class_name, member_name)))
272 
273 //! Throws std::runtime_error with what() returning error_code description prefixed with aux_info
274 void __TBB_EXPORTED_FUNC handle_perror( int error_code, const char* aux_info );
275 
276 #if TBB_USE_EXCEPTIONS
277     #define __TBB_TRY try
278     #define __TBB_CATCH(e) catch(e)
279     #define __TBB_THROW(e) throw e
280     #define __TBB_RETHROW() throw
281 #else /* !TBB_USE_EXCEPTIONS */
282     inline bool __TBB_false() { return false; }
283     #define __TBB_TRY
284     #define __TBB_CATCH(e) if ( tbb::internal::__TBB_false() )
285     #define __TBB_THROW(e) tbb::internal::suppress_unused_warning(e)
286     #define __TBB_RETHROW() ((void)0)
287 #endif /* !TBB_USE_EXCEPTIONS */
288 
289 //! Report a runtime warning.
290 void __TBB_EXPORTED_FUNC runtime_warning( const char* format, ... );
291 
292 #if TBB_USE_ASSERT
293 static void* const poisoned_ptr = reinterpret_cast<void*>(-1);
294 
295 //! Set p to invalid pointer value.
296 //  Also works for regular (non-__TBB_atomic) pointers.
297 template<typename T>
298 inline void poison_pointer( T* __TBB_atomic & p ) { p = reinterpret_cast<T*>(poisoned_ptr); }
299 
300 /** Expected to be used in assertions only, thus no empty form is defined. **/
301 template<typename T>
302 inline bool is_poisoned( T* p ) { return p == reinterpret_cast<T*>(poisoned_ptr); }
303 #else
304 template<typename T>
305 inline void poison_pointer( T* __TBB_atomic & ) {/*do nothing*/}
306 #endif /* !TBB_USE_ASSERT */
307 
308 //! Cast between unrelated pointer types.
309 /** This method should be used sparingly as a last resort for dealing with
310     situations that inherently break strict ISO C++ aliasing rules. */
311 // T is a pointer type because it will be explicitly provided by the programmer as a template argument;
312 // U is a referent type to enable the compiler to check that "ptr" is a pointer, deducing U in the process.
313 template<typename T, typename U>
314 inline T punned_cast( U* ptr ) {
315     uintptr_t x = reinterpret_cast<uintptr_t>(ptr);
316     return reinterpret_cast<T>(x);
317 }
318 
319 #if __TBB_DEFAULTED_AND_DELETED_FUNC_PRESENT
320 
321 //! Base class for types that should not be assigned.
322 class no_assign {
323 public:
324     void operator=( const no_assign& ) = delete;
325     no_assign( const no_assign& ) = default;
326     no_assign() = default;
327 };
328 
329 //! Base class for types that should not be copied or assigned.
330 class no_copy: no_assign {
331 public:
332     no_copy( const no_copy& ) = delete;
333     no_copy() = default;
334 };
335 
336 #else /*__TBB_DEFAULTED_AND_DELETED_FUNC_PRESENT*/
337 
338 //! Base class for types that should not be assigned.
339 class no_assign {
340     // Deny assignment
341     void operator=( const no_assign& );
342 public:
343 #if __GNUC__
344     //! Explicitly define default construction, because otherwise gcc issues gratuitous warning.
345     no_assign() {}
346 #endif /* __GNUC__ */
347 };
348 
349 //! Base class for types that should not be copied or assigned.
350 class no_copy: no_assign {
351     //! Deny copy construction
352     no_copy( const no_copy& );
353 public:
354     //! Allow default construction
355     no_copy() {}
356 };
357 
358 #endif /*__TBB_DEFAULTED_AND_DELETED_FUNC_PRESENT*/
359 
360 #if TBB_DEPRECATED_MUTEX_COPYING
361 class mutex_copy_deprecated_and_disabled {};
362 #else
363 // By default various implementations of mutexes are not copy constructible
364 // and not copy assignable.
365 class mutex_copy_deprecated_and_disabled : no_copy {};
366 #endif
367 
368 //! A function to check if passed in pointer is aligned on a specific border
369 template<typename T>
370 inline bool is_aligned(T* pointer, uintptr_t alignment) {
371     return 0==((uintptr_t)pointer & (alignment-1));
372 }
373 
374 //! A function to check if passed integer is a power of 2
375 template<typename integer_type>
376 inline bool is_power_of_two(integer_type arg) {
377     return arg && (0 == (arg & (arg - 1)));
378 }
379 
380 //! A function to compute arg modulo divisor where divisor is a power of 2.
381 template<typename argument_integer_type, typename divisor_integer_type>
382 inline argument_integer_type modulo_power_of_two(argument_integer_type arg, divisor_integer_type divisor) {
383     __TBB_ASSERT( is_power_of_two(divisor), "Divisor should be a power of two" );
384     return (arg & (divisor - 1));
385 }
386 
387 
388 //! A function to determine if arg is a power of 2 at least as big as another power of 2.
389 // i.e. for strictly positive i and j, with j being a power of 2,
390 // determines whether i==j<<k for some nonnegative k (so i==j yields true).
391 template<typename argument_integer_type, typename power2_integer_type>
392 inline bool is_power_of_two_at_least(argument_integer_type arg, power2_integer_type power2) {
393     __TBB_ASSERT( is_power_of_two(power2), "Divisor should be a power of two" );
394     return 0 == (arg & (arg - power2));
395 }
396 
397 //! Utility template function to prevent "unused" warnings by various compilers.
398 template<typename T1> void suppress_unused_warning( const T1& ) {}
399 template<typename T1, typename T2> void suppress_unused_warning( const T1&, const T2& ) {}
400 template<typename T1, typename T2, typename T3> void suppress_unused_warning( const T1&, const T2&, const T3& ) {}
401 
402 // Struct to be used as a version tag for inline functions.
403 /** Version tag can be necessary to prevent loader on Linux from using the wrong
404     symbol in debug builds (when inline functions are compiled as out-of-line). **/
405 struct version_tag_v3 {};
406 
407 typedef version_tag_v3 version_tag;
408 
409 } // internal
410 
411 //! Dummy type that distinguishes splitting constructor from copy constructor.
412 /**
413  * See description of parallel_for and parallel_reduce for example usages.
414  * @ingroup algorithms
415  */
416 class split {
417 };
418 
419 //! Type enables transmission of splitting proportion from partitioners to range objects
420 /**
421  * In order to make use of such facility Range objects must implement
422  * splitting constructor with this type passed and initialize static
423  * constant boolean field 'is_splittable_in_proportion' with the value
424  * of 'true'
425  */
426 class proportional_split: internal::no_assign {
427 public:
428     proportional_split(size_t _left = 1, size_t _right = 1) : my_left(_left), my_right(_right) { }
429 
430     size_t left() const { return my_left; }
431     size_t right() const { return my_right; }
432 
433     // used when range does not support proportional split
434     operator split() const { return split(); }
435 
436 #if __TBB_ENABLE_RANGE_FEEDBACK
437     void set_proportion(size_t _left, size_t _right) {
438         my_left = _left;
439         my_right = _right;
440     }
441 #endif
442 private:
443     size_t my_left, my_right;
444 };
445 
446 } // tbb
447 
448 // Following is a set of classes and functions typically used in compile-time "metaprogramming".
449 // TODO: move all that to a separate header
450 
451 #if __TBB_CPP11_SMART_POINTERS_PRESENT
452 #include <memory> // for unique_ptr
453 #endif
454 
455 #if __TBB_CPP11_RVALUE_REF_PRESENT || __TBB_CPP11_DECLTYPE_PRESENT || _LIBCPP_VERSION
456 #include <utility> // for std::move, std::forward, std::declval
457 #endif
458 
459 namespace tbb {
460 namespace internal {
461 
462 #if __TBB_CPP11_SMART_POINTERS_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT && __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
463     template<typename T, typename... Args>
464     std::unique_ptr<T> make_unique(Args&&... args) {
465         return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
466     }
467 #endif
468 
469 //! Class for determining type of std::allocator<T>::value_type.
470 template<typename T>
471 struct allocator_type {
472     typedef T value_type;
473 };
474 
475 #if _MSC_VER
476 //! Microsoft std::allocator has non-standard extension that strips const from a type.
477 template<typename T>
478 struct allocator_type<const T> {
479     typedef T value_type;
480 };
481 #endif
482 
483 // Ad-hoc implementation of true_type & false_type
484 // Intended strictly for internal use! For public APIs (traits etc), use C++11 analogues.
485 template <bool v>
486 struct bool_constant {
487     static /*constexpr*/ const bool value = v;
488 };
489 typedef bool_constant<true> true_type;
490 typedef bool_constant<false> false_type;
491 
492 //! A template to select either 32-bit or 64-bit constant as compile time, depending on machine word size.
493 template <unsigned u, unsigned long long ull >
494 struct select_size_t_constant {
495     //Explicit cast is needed to avoid compiler warnings about possible truncation.
496     //The value of the right size,   which is selected by ?:, is anyway not truncated or promoted.
497     static const size_t value = (size_t)((sizeof(size_t)==sizeof(u)) ? u : ull);
498 };
499 
500 #if __TBB_CPP11_RVALUE_REF_PRESENT
501 using std::move;
502 using std::forward;
503 #elif defined(_LIBCPP_NAMESPACE)
504 // libc++ defines "pre-C++11 move and forward" similarly to ours; use it to avoid name conflicts in some cases.
505 using std::_LIBCPP_NAMESPACE::move;
506 using std::_LIBCPP_NAMESPACE::forward;
507 #else
508 // It is assumed that cv qualifiers, if any, are part of the deduced type.
509 template <typename T>
510 T& move( T& x ) { return x; }
511 template <typename T>
512 T& forward( T& x ) { return x; }
513 #endif /* __TBB_CPP11_RVALUE_REF_PRESENT */
514 
515 // Helper macros to simplify writing templates working with both C++03 and C++11.
516 #if __TBB_CPP11_RVALUE_REF_PRESENT
517 #define  __TBB_FORWARDING_REF(A) A&&
518 #else
519 // It is assumed that cv qualifiers, if any, are part of a deduced type.
520 // Thus this macro should not be used in public interfaces.
521 #define  __TBB_FORWARDING_REF(A) A&
522 #endif
523 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT
524 #define __TBB_PARAMETER_PACK ...
525 #define __TBB_PACK_EXPANSION(A) A...
526 #else
527 #define __TBB_PARAMETER_PACK
528 #define __TBB_PACK_EXPANSION(A) A
529 #endif /* __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT */
530 
531 #if __TBB_CPP11_DECLTYPE_PRESENT
532 #if __TBB_CPP11_DECLVAL_BROKEN
533 // Ad-hoc implementation of std::declval
534 template <class T> __TBB_FORWARDING_REF(T) declval() /*noexcept*/;
535 #else
536 using std::declval;
537 #endif
538 #endif
539 
540 template <bool condition>
541 struct STATIC_ASSERTION_FAILED;
542 
543 template <>
544 struct STATIC_ASSERTION_FAILED<false> { enum {value=1};};
545 
546 template<>
547 struct STATIC_ASSERTION_FAILED<true>; //intentionally left undefined to cause compile time error
548 
549 //! @endcond
550 }} // namespace tbb::internal
551 
552 #if __TBB_STATIC_ASSERT_PRESENT
553 #define __TBB_STATIC_ASSERT(condition,msg) static_assert(condition,msg)
554 #else
555 //please note condition is intentionally inverted to get a bit more understandable error msg
556 #define __TBB_STATIC_ASSERT_IMPL1(condition,msg,line)       \
557     enum {static_assert_on_line_##line = tbb::internal::STATIC_ASSERTION_FAILED<!(condition)>::value}
558 
559 #define __TBB_STATIC_ASSERT_IMPL(condition,msg,line) __TBB_STATIC_ASSERT_IMPL1(condition,msg,line)
560 //! Verify condition, at compile time
561 #define __TBB_STATIC_ASSERT(condition,msg) __TBB_STATIC_ASSERT_IMPL(condition,msg,__LINE__)
562 #endif
563 
564 #endif /* RC_INVOKED */
565 #endif /* __TBB_tbb_stddef_H */
566