1/*
2 Copyright 2005-2013 Intel Corporation. All Rights Reserved.
3
4 This file is part of Threading Building Blocks.
5
6 Threading Building Blocks is free software; you can redistribute it
7 and/or modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9
10 Threading Building Blocks is distributed in the hope that it will be
11 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Threading Building Blocks; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 As a special exception, you may use this file as part of a free software
20 library without restriction. Specifically, if other files instantiate
21 templates or use macros or inline functions from this file, or you compile
22 this file and link it with other files to produce an executable, this
23 file does not by itself cause the resulting executable to be covered by
24 the GNU General Public License. This exception does not however
25 invalidate any other reasons why the executable file might be covered by
26 the GNU General Public License.
27*/
28
29#ifndef __TBB_tbb_stddef_H
30#define __TBB_tbb_stddef_H
31
32// Marketing-driven product version
33#define TBB_VERSION_MAJOR 4
34#define TBB_VERSION_MINOR 2
35
36// Engineering-focused interface version
37#define TBB_INTERFACE_VERSION 7000
38#define TBB_INTERFACE_VERSION_MAJOR TBB_INTERFACE_VERSION/1000
39
40// The oldest major interface version still supported
41// To be used in SONAME, manifests, etc.
42#define TBB_COMPATIBLE_INTERFACE_VERSION 2
43
44#define __TBB_STRING_AUX(x) #x
45#define __TBB_STRING(x) __TBB_STRING_AUX(x)
46
47// We do not need defines below for resource processing on windows
48#if !defined RC_INVOKED
49
50// Define groups for Doxygen documentation
51/**
52 * @defgroup algorithms Algorithms
53 * @defgroup containers Containers
54 * @defgroup memory_allocation Memory Allocation
55 * @defgroup synchronization Synchronization
56 * @defgroup timing Timing
57 * @defgroup task_scheduling Task Scheduling
58 */
59
60// Simple text that is displayed on the main page of Doxygen documentation.
61/**
62 * \mainpage Main Page
63 *
64 * Click the tabs above for information about the
65 * - <a href="./modules.html">Modules</a> (groups of functionality) implemented by the library
66 * - <a href="./annotated.html">Classes</a> provided by the library
67 * - <a href="./files.html">Files</a> constituting the library.
68 * .
69 * Please note that significant part of TBB functionality is implemented in the form of
70 * template functions, descriptions of which are not accessible on the <a href="./annotated.html">Classes</a>
71 * tab. Use <a href="./modules.html">Modules</a> or <a href="./namespacemembers.html">Namespace/Namespace Members</a>
72 * tabs to find them.
73 *
74 * Additional pieces of information can be found here
75 * - \subpage concepts
76 * .
77 */
78
79/** \page concepts TBB concepts
80
81 A concept is a set of requirements to a type, which are necessary and sufficient
82 for the type to model a particular behavior or a set of behaviors. Some concepts
83 are specific to a particular algorithm (e.g. algorithm body), while other ones
84 are common to several algorithms (e.g. range concept).
85
86 All TBB algorithms make use of different classes implementing various concepts.
87 Implementation classes are supplied by the user as type arguments of template
88 parameters and/or as objects passed as function call arguments. The library
89 provides predefined implementations of some concepts (e.g. several kinds of
90 \ref range_req "ranges"), while other ones must always be implemented by the user.
91
92 TBB defines a set of minimal requirements each concept must conform to. Here is
93 the list of different concepts hyperlinked to the corresponding requirements specifications:
94 - \subpage range_req
95 - \subpage parallel_do_body_req
96 - \subpage parallel_for_body_req
97 - \subpage parallel_reduce_body_req
98 - \subpage parallel_scan_body_req
99 - \subpage parallel_sort_iter_req
100**/
101
102// tbb_config.h should be included the first since it contains macro definitions used in other headers
103#include "tbb_config.h"
104
105#if _MSC_VER >=1400
106 #define __TBB_EXPORTED_FUNC __cdecl
107 #define __TBB_EXPORTED_METHOD __thiscall
108#else
109 #define __TBB_EXPORTED_FUNC
110 #define __TBB_EXPORTED_METHOD
111#endif
112
113#if __INTEL_COMPILER || _MSC_VER
114#define __TBB_NOINLINE(decl) __declspec(noinline) decl
115#elif __GNUC__
116#define __TBB_NOINLINE(decl) decl __attribute__ ((noinline))
117#else
118#define __TBB_NOINLINE(decl) decl
119#endif
120
121#include <cstddef> /* Need size_t and ptrdiff_t */
122
123#if _MSC_VER
124 #define __TBB_tbb_windef_H
125 #include "internal/_tbb_windef.h"
126 #undef __TBB_tbb_windef_H
127#endif
128#if !defined(_MSC_VER) || _MSC_VER>=1600
129 #include <stdint.h>
130#endif
131
132//! Type for an assertion handler
133typedef void(*assertion_handler_type)( const char* filename, int line, const char* expression, const char * comment );
134
135#if TBB_USE_ASSERT
136
137 #define __TBB_ASSERT_NS(predicate,message,ns) ((predicate)?((void)0) : ns::assertion_failure(__FILE__,__LINE__,#predicate,message))
138 //! Assert that x is true.
139 /** If x is false, print assertion failure message.
140 If the comment argument is not NULL, it is printed as part of the failure message.
141 The comment argument has no other effect. */
142#if __TBBMALLOC_BUILD
143namespace rml { namespace internal {
144 #define __TBB_ASSERT(predicate,message) __TBB_ASSERT_NS(predicate,message,rml::internal)
145#else
146namespace tbb {
147 #define __TBB_ASSERT(predicate,message) __TBB_ASSERT_NS(predicate,message,tbb)
148#endif
149
150 #define __TBB_ASSERT_EX __TBB_ASSERT
151
152 //! Set assertion handler and return previous value of it.
153 assertion_handler_type __TBB_EXPORTED_FUNC set_assertion_handler( assertion_handler_type new_handler );
154
155 //! Process an assertion failure.
156 /** Normally called from __TBB_ASSERT macro.
157 If assertion handler is null, print message for assertion failure and abort.
158 Otherwise call the assertion handler. */
159 void __TBB_EXPORTED_FUNC assertion_failure( const char* filename, int line, const char* expression, const char* comment );
160
161#if __TBBMALLOC_BUILD
162}} // namespace rml::internal
163#else
164} // namespace tbb
165#endif
166#else /* !TBB_USE_ASSERT */
167
168 //! No-op version of __TBB_ASSERT.
169 #define __TBB_ASSERT(predicate,comment) ((void)0)
170 //! "Extended" version is useful to suppress warnings if a variable is only used with an assert
171 #define __TBB_ASSERT_EX(predicate,comment) ((void)(1 && (predicate)))
172
173#endif /* !TBB_USE_ASSERT */
174
175//! The namespace tbb contains all components of the library.
176namespace tbb {
177
178#if _MSC_VER && _MSC_VER<1600
179 namespace internal {
180 typedef __int8 int8_t;
181 typedef __int16 int16_t;
182 typedef __int32 int32_t;
183 typedef __int64 int64_t;
184 typedef unsigned __int8 uint8_t;
185 typedef unsigned __int16 uint16_t;
186 typedef unsigned __int32 uint32_t;
187 typedef unsigned __int64 uint64_t;
188 } // namespace internal
189#else /* Posix */
190 namespace internal {
191 using ::int8_t;
192 using ::int16_t;
193 using ::int32_t;
194 using ::int64_t;
195 using ::uint8_t;
196 using ::uint16_t;
197 using ::uint32_t;
198 using ::uint64_t;
199 } // namespace internal
200#endif /* Posix */
201
202 using std::size_t;
203 using std::ptrdiff_t;
204
205//! The function returns the interface version of the TBB shared library being used.
206/**
207 * The version it returns is determined at runtime, not at compile/link time.
208 * So it can be different than the value of TBB_INTERFACE_VERSION obtained at compile time.
209 */
210extern "C" int __TBB_EXPORTED_FUNC TBB_runtime_interface_version();
211
212//! Dummy type that distinguishes splitting constructor from copy constructor.
213/**
214 * See description of parallel_for and parallel_reduce for example usages.
215 * @ingroup algorithms
216 */
217class split {
218};
219
220/**
221 * @cond INTERNAL
222 * @brief Identifiers declared inside namespace internal should never be used directly by client code.
223 */
224namespace internal {
225
226//! Compile-time constant that is upper bound on cache line/sector size.
227/** It should be used only in situations where having a compile-time upper
228 bound is more useful than a run-time exact answer.
229 @ingroup memory_allocation */
230const size_t NFS_MaxLineSize = 128;
231
232/** Label for data that may be accessed from different threads, and that may eventually become wrapped
233 in a formal atomic type.
234
235 Note that no problems have yet been observed relating to the definition currently being empty,
236 even if at least "volatile" would seem to be in order to avoid data sometimes temporarily hiding
237 in a register (although "volatile" as a "poor man's atomic" lacks several other features of a proper
238 atomic, some of which are now provided instead through specialized functions).
239
240 Note that usage is intentionally compatible with a definition as qualifier "volatile",
241 both as a way to have the compiler help enforce use of the label and to quickly rule out
242 one potential issue.
243
244 Note however that, with some architecture/compiler combinations, e.g. on IA-64 architecture, "volatile"
245 also has non-portable memory semantics that are needlessly expensive for "relaxed" operations.
246
247 Note that this must only be applied to data that will not change bit patterns when cast to/from
248 an integral type of the same length; tbb::atomic must be used instead for, e.g., floating-point types.
249
250 TODO: apply wherever relevant **/
251#define __TBB_atomic // intentionally empty, see above
252
253template<class T, int S>
254struct padded_base : T {
255 char pad[NFS_MaxLineSize - sizeof(T) % NFS_MaxLineSize];
256};
257template<class T> struct padded_base<T, 0> : T {};
258
259//! Pads type T to fill out to a multiple of cache line size.
260template<class T>
261struct padded : padded_base<T, sizeof(T)> {};
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
274void __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) ((void)0)
286 #define __TBB_RETHROW() ((void)0)
287#endif /* !TBB_USE_EXCEPTIONS */
288
289//! Report a runtime warning.
290void __TBB_EXPORTED_FUNC runtime_warning( const char* format, ... );
291
292#if TBB_USE_ASSERT
293static void* const poisoned_ptr = reinterpret_cast<void*>(-1);
294
295//! Set p to invalid pointer value.
296template<typename T>
297inline void poison_pointer( T*& p ) { p = reinterpret_cast<T*>(poisoned_ptr); }
298
299/** Expected to be used in assertions only, thus no empty form is defined. **/
300template<typename T>
301inline bool is_poisoned( T* p ) { return p == reinterpret_cast<T*>(poisoned_ptr); }
302#else
303template<typename T>
304inline void poison_pointer( T* ) {/*do nothing*/}
305#endif /* !TBB_USE_ASSERT */
306
307//! Cast between unrelated pointer types.
308/** This method should be used sparingly as a last resort for dealing with
309 situations that inherently break strict ISO C++ aliasing rules. */
310// T is a pointer type because it will be explicitly provided by the programmer as a template argument;
311// U is a referent type to enable the compiler to check that "ptr" is a pointer, deducing U in the process.
312template<typename T, typename U>
313inline T punned_cast( U* ptr ) {
314 uintptr_t x = reinterpret_cast<uintptr_t>(ptr);
315 return reinterpret_cast<T>(x);
316}
317
318//! Base class for types that should not be assigned.
319class no_assign {
320 // Deny assignment
321 void operator=( const no_assign& );
322public:
323#if __GNUC__
324 //! Explicitly define default construction, because otherwise gcc issues gratuitous warning.
325 no_assign() {}
326#endif /* __GNUC__ */
327};
328
329//! Base class for types that should not be copied or assigned.
330class no_copy: no_assign {
331 //! Deny copy construction
332 no_copy( const no_copy& );
333public:
334 //! Allow default construction
335 no_copy() {}
336};
337
338//! Class for determining type of std::allocator<T>::value_type.
339template<typename T>
340struct allocator_type {
341 typedef T value_type;
342};
343
344#if _MSC_VER
345//! Microsoft std::allocator has non-standard extension that strips const from a type.
346template<typename T>
347struct allocator_type<const T> {
348 typedef T value_type;
349};
350#endif
351
352//! A template to select either 32-bit or 64-bit constant as compile time, depending on machine word size.
353template <unsigned u, unsigned long long ull >
354struct select_size_t_constant {
355 //Explicit cast is needed to avoid compiler warnings about possible truncation.
356 //The value of the right size, which is selected by ?:, is anyway not truncated or promoted.
357 static const size_t value = (size_t)((sizeof(size_t)==sizeof(u)) ? u : ull);
358};
359
360//! A function to check if passed in pointer is aligned on a specific border
361template<typename T>
362inline bool is_aligned(T* pointer, uintptr_t alignment) {
363 return 0==((uintptr_t)pointer & (alignment-1));
364}
365
366//! A function to check if passed integer is a power of 2
367template<typename integer_type>
368inline bool is_power_of_two(integer_type arg) {
369 return arg && (0 == (arg & (arg - 1)));
370}
371
372//! A function to compute arg modulo divisor where divisor is a power of 2.
373template<typename argument_integer_type, typename divisor_integer_type>
374inline argument_integer_type modulo_power_of_two(argument_integer_type arg, divisor_integer_type divisor) {
375 // Divisor is assumed to be a power of two (which is valid for current uses).
376 __TBB_ASSERT( is_power_of_two(divisor), "Divisor should be a power of two" );
377 return (arg & (divisor - 1));
378}
379
380
381//! A function to determine if "arg is a multiplication of a number and a power of 2".
382// i.e. for strictly positive i and j, with j a power of 2,
383// determines whether i==j<<k for some nonnegative k (so i==j yields true).
384template<typename argument_integer_type, typename divisor_integer_type>
385inline bool is_power_of_two_factor(argument_integer_type arg, divisor_integer_type divisor) {
386 // Divisor is assumed to be a power of two (which is valid for current uses).
387 __TBB_ASSERT( is_power_of_two(divisor), "Divisor should be a power of two" );
388 return 0 == (arg & (arg - divisor));
389}
390
391//! Utility template function to prevent "unused" warnings by various compilers.
392template<typename T>
393void suppress_unused_warning( const T& ) {}
394
395// Struct to be used as a version tag for inline functions.
396/** Version tag can be necessary to prevent loader on Linux from using the wrong
397 symbol in debug builds (when inline functions are compiled as out-of-line). **/
398struct version_tag_v3 {};
399
400typedef version_tag_v3 version_tag;
401
402} // internal
403//! @endcond
404
405} // tbb
406
407namespace tbb { namespace internal {
408template <bool condition>
409struct STATIC_ASSERTION_FAILED;
410
411template <>
412struct STATIC_ASSERTION_FAILED<false> { enum {value=1};};
413
414template<>
415struct STATIC_ASSERTION_FAILED<true>; //intentionally left undefined to cause compile time error
416}} // namespace tbb { namespace internal {
417
418#if __TBB_STATIC_ASSERT_PRESENT
419#define __TBB_STATIC_ASSERT(condition,msg) static_assert(condition,msg)
420#else
421//please note condition is intentionally inverted to get a bit more understandable error msg
422#define __TBB_STATIC_ASSERT_IMPL1(condition,msg,line) \
423 enum {static_assert_on_line_##line = tbb::internal::STATIC_ASSERTION_FAILED<!(condition)>::value}
424
425#define __TBB_STATIC_ASSERT_IMPL(condition,msg,line) __TBB_STATIC_ASSERT_IMPL1(condition,msg,line)
426//! Verify at compile time that passed in condition is hold
427#define __TBB_STATIC_ASSERT(condition,msg) __TBB_STATIC_ASSERT_IMPL(condition,msg,__LINE__)
428#endif
429
430#endif /* RC_INVOKED */
431#endif /* __TBB_tbb_stddef_H */
432