/usr/lib/gcc/x86_64-linux-gnu/5/include/cilk/metaprogramming.h is in libgcc-5-dev 5.5.0-12ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | /* metaprogramming.h -*- C++ -*-
*
* @copyright
* Copyright (C) 2012-2013, Intel Corporation
* All rights reserved.
*
* @copyright
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* @copyright
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/** @file metaprogramming.h
*
* @brief Defines metaprogramming utility classes used in the Cilk library.
*
* @ingroup common
*/
#ifndef METAPROGRAMMING_H_INCLUDED
#define METAPROGRAMMING_H_INCLUDED
#ifdef __cplusplus
#include <functional>
#include <new>
#include <cstdlib>
#ifdef _WIN32
#include <malloc.h>
#endif
#include <algorithm>
namespace cilk {
namespace internal {
/** Test if a class is empty.
*
* If @a Class is an empty (and therefore necessarily stateless) class, then
* the “empty base-class optimization” guarantees that
* `sizeof(check_for_empty_class<Class>) == sizeof(char)`. Conversely, if
* `sizeof(check_for_empty_class<Class>) > sizeof(char)`, then @a Class is not
* empty, and we must discriminate distinct instances of @a Class.
*
* Typical usage:
*
* // General definition of A<B> for non-empty B:
* template <typename B, bool BIsEmpty = class_is_empty<B>::value> >
* class A { ... };
*
* // Specialized definition of A<B> for empty B:
* template <typename B>
* class A<B, true> { ... };
*
* @tparam Class The class to be tested for emptiness.
*
* @result The `value` member will be `true` if @a Class is empty,
* `false` otherwise.
*
* @ingroup common
*/
template <class Class>
class class_is_empty {
class check_for_empty_class : public Class
{
char m_data;
public:
// Declared but not defined
check_for_empty_class();
check_for_empty_class(const check_for_empty_class&);
check_for_empty_class& operator=(const check_for_empty_class&);
~check_for_empty_class();
};
public:
/** Constant is true if and only if @a Class is empty.
*/
static const bool value = (sizeof(check_for_empty_class) == sizeof(char));
};
/** Get the alignment of a type.
*
* For example:
*
* align_of<double>::value == 8
*
* @tparam Tp The type whose alignment is to be computed.
*
* @result The `value` member of an instantiation of this class template
* will hold the integral alignment requirement of @a Tp.
*
* @pre @a Tp shall be a complete type.
*
* @ingroup common
*/
template <typename Tp>
struct align_of
{
private:
struct imp {
char m_padding;
Tp m_val;
// The following declarations exist to suppress compiler-generated
// definitions, in case @a Tp does not have a public default
// constructor, copy constructor, or destructor.
imp(const imp&); // Declared but not defined
~imp(); // Declared but not defined
};
public:
/// The integral alignment requirement of @a Tp.
static const std::size_t value = (sizeof(imp) - sizeof(Tp));
};
/** A class containing raw bytes with a specified alignment and size.
*
* An object of type `aligned_storage<S, A>` will have alignment `A` and
* size at least `S`. Its contents will be uninitialized bytes.
*
* @tparam Size The required minimum size of the resulting class.
* @tparam Alignment The required alignment of the resulting class.
*
* @pre @a Alignment shall be a power of 2 no greater then 64.
*
* @note This is implemented using the `CILK_ALIGNAS` macro, which uses
* the non-standard, implementation-specific features
* `__declspec(align(N))` on Windows, and
* `__attribute__((__aligned__(N)))` on Unix. The `gcc` implementation
* of `__attribute__((__aligned__(N)))` requires a numeric literal `N`
* (_not_ an arbitrary compile-time constant expression). Therefore,
* this class is implemented using specialization on the required
* alignment.
*
* @note The template class is specialized only for the supported
* alignments. An attempt to instantiate it for an unsupported
* alignment will result in a compilation error.
*/
template <std::size_t Size, std::size_t Alignment>
struct aligned_storage;
template<std::size_t Size> class aligned_storage<Size, 1>
{ CILK_ALIGNAS( 1) char m_bytes[Size]; };
template<std::size_t Size> class aligned_storage<Size, 2>
{ CILK_ALIGNAS( 2) char m_bytes[Size]; };
template<std::size_t Size> class aligned_storage<Size, 4>
{ CILK_ALIGNAS( 4) char m_bytes[Size]; };
template<std::size_t Size> class aligned_storage<Size, 8>
{ CILK_ALIGNAS( 8) char m_bytes[Size]; };
template<std::size_t Size> class aligned_storage<Size, 16>
{ CILK_ALIGNAS(16) char m_bytes[Size]; };
template<std::size_t Size> class aligned_storage<Size, 32>
{ CILK_ALIGNAS(32) char m_bytes[Size]; };
template<std::size_t Size> class aligned_storage<Size, 64>
{ CILK_ALIGNAS(64) char m_bytes[Size]; };
/** A buffer of uninitialized bytes with the same size and alignment as a
* specified type.
*
* The class `storage_for_object<Type>` will have the same size and alignment
* properties as `Type`, but it will contain only raw (uninitialized) bytes.
* This allows the definition of a data member which can contain a `Type`
* object which is initialized explicitly under program control, rather
* than implicitly as part of the initialization of the containing class.
* For example:
*
* class C {
* storage_for_object<MemberClass> _member;
* public:
* C() ... // Does NOT initialize _member
* void initialize(args)
* { new (_member.pointer()) MemberClass(args); }
* const MemberClass& member() const { return _member.object(); }
* MemberClass& member() { return _member.object(); }
*
* @tparam Type The type whose size and alignment are to be reflected
* by this class.
*/
template <typename Type>
class storage_for_object :
aligned_storage< sizeof(Type), align_of<Type>::value >
{
public:
/// Return a typed reference to the buffer.
const Type& object() const { return *reinterpret_cast<Type*>(this); }
Type& object() { return *reinterpret_cast<Type*>(this); }
};
/** Get the functor class corresponding to a binary function type.
*
* The `binary_functor` template class class can be instantiated with a binary
* functor class or with a real binary function, and will yield an equivalent
* binary functor class class in either case.
*
* @tparam F A binary functor class, a binary function type, or a pointer to
* binary function type.
*
* @result `binary_functor<F>::%type` will be the same as @a F if @a F is
* a class. It will be a `std::pointer_to_binary_function` wrapper
* if @a F is a binary function or binary function pointer type.
* (It will _not_ necessarily be an `Adaptable Binary Function`
* class, since @a F might be a non-adaptable binary functor
* class.)
*
* @ingroup common
*/
template <typename F>
struct binary_functor {
/// The binary functor class equivalent to @a F.
typedef F type;
};
/// @copydoc binary_functor
/// Specialization for binary function.
template <typename R, typename A, typename B>
struct binary_functor<R(A,B)> {
/// The binary functor class equivalent to @a F.
typedef std::pointer_to_binary_function<A, B, R> type;
};
/// @copydoc binary_functor
/// Specialization for pointer to binary function.
template <typename R, typename A, typename B>
struct binary_functor<R(*)(A,B)> {
/// The binary functor class equivalent to @a F.
typedef std::pointer_to_binary_function<A, B, R> type;
};
/** Indirect binary function class with specified types.
*
* `typed_indirect_binary_function<F>` is an `Adaptable Binary Function` class
* based on an existing binary functor class or binary function type @a F. If
* @a F is a stateless class, then this class will be empty, and its
* `operator()` will invoke @a F’s `operator()`. Otherwise, an object of this
* class will hold a pointer to an object of type @a F, and will refer its
* `operator()` calls to the pointed-to @a F object.
*
* That is, suppose that we have the declarations:
*
* F *p;
* typed_indirect_binary_function<F, int, int, bool> ibf(p);
*
* Then:
*
* - `ibf(x, y) == (*p)(x, y)`.
* - `ibf(x, y)` will not do a pointer dereference if `F` is an empty class.
*
* @note Just to repeat: if `F` is an empty class, then
* `typed_indirect_binary_function\<F\>' is also an empty class.
* This is critical for its use in the @ref min_max::view_base
* "min/max reducer view classes", where it allows the view to
* call a comparison functor in the monoid without actually
* having to allocate a pointer in the view class when the
* comparison class is empty.
*
* @note If you have an `Adaptable Binary Function` class or a binary
* function type, then you can use the
* @ref indirect_binary_function class, which derives the
* argument and result types parameter type instead of requiring
* you to specify them as template arguments.
*
* @tparam F A binary functor class, a binary function type, or a pointer to
* binary function type.
* @param A1 The first argument type.
* @param A2 The second argument type.
* @param R The result type.
*
* @see min_max::comparator_base
* @see indirect_binary_function
*
* @ingroup common
*/
template < typename F
, typename A1
, typename A2
, typename R
, typename Functor = typename binary_functor<F>::type
, bool FunctorIsEmpty = class_is_empty<Functor>::value
>
class typed_indirect_binary_function : std::binary_function<A1, A2, R>
{
const F* f;
public:
/// Constructor captures a pointer to the wrapped function.
typed_indirect_binary_function(const F* f) : f(f) {}
/// Return the comparator pointer, or `NULL` if the comparator is stateless.
const F* pointer() const { return f; }
/// Apply the pointed-to functor to the arguments.
R operator()(const A1& a1, const A2& a2) const { return (*f)(a1, a2); }
};
/// @copydoc typed_indirect_binary_function
/// Specialization for an empty functor class. (This is only possible if @a F
/// itself is an empty class. If @a F is a function or pointer-to-function
/// type, then the functor will contain a pointer.)
template <typename F, typename A1, typename A2, typename R, typename Functor>
class typed_indirect_binary_function<F, A1, A2, R, Functor, true> :
std::binary_function<A1, A2, R>
{
public:
/// Return `NULL` for the comparator pointer of a stateless comparator.
const F* pointer() const { return 0; }
/// Constructor discards the pointer to a stateless functor class.
typed_indirect_binary_function(const F* f) {}
/// Create an instance of the stateless functor class and apply it to the arguments.
R operator()(const A1& a1, const A2& a2) const { return F()(a1, a2); }
};
/** Indirect binary function class with inferred types.
*
* This is identical to @ref typed_indirect_binary_function, except that it
* derives the binary function argument and result types from the parameter
* type @a F instead of taking them as additional template parameters. If @a F
* is a class type, then it must be an `Adaptable Binary Function`.
*
* @see typed_indirect_binary_function
*
* @ingroup common
*/
template <typename F, typename Functor = typename binary_functor<F>::type>
class indirect_binary_function :
typed_indirect_binary_function< F
, typename Functor::first_argument_type
, typename Functor::second_argument_type
, typename Functor::result_type
>
{
typedef typed_indirect_binary_function< F
, typename Functor::first_argument_type
, typename Functor::second_argument_type
, typename Functor::result_type
>
base;
public:
indirect_binary_function(const F* f) : base(f) {} ///< Constructor
};
/** Choose a type based on a boolean constant.
*
* This metafunction is identical to C++11’s condition metafunction.
* It needs to be here until we can reasonably assume that users will be
* compiling with C++11.
*
* @tparam Cond A boolean constant.
* @tparam IfTrue A type.
* @tparam IfFalse A type.
* @result The `type` member will be a typedef of @a IfTrue if @a Cond
* is true, and a typedef of @a IfFalse if @a Cond is false.
*
* @ingroup common
*/
template <bool Cond, typename IfTrue, typename IfFalse>
struct condition
{
typedef IfTrue type; ///< The type selected by the condition.
};
/// @copydoc condition
/// Specialization for @a Cond == `false`.
template <typename IfTrue, typename IfFalse>
struct condition<false, IfTrue, IfFalse>
{
typedef IfFalse type; ///< The type selected by the condition.
};
/** @def __CILKRTS_STATIC_ASSERT
*
* @brief Compile-time assertion.
*
* Causes a compilation error if a compile-time constant expression is false.
*
* @par Usage example.
* This assertion is used in reducer_min_max.h to avoid defining
* legacy reducer classes that would not be binary-compatible with the
* same classes compiled with earlier versions of the reducer library.
*
* __CILKRTS_STATIC_ASSERT(
* internal::class_is_empty< internal::binary_functor<Compare> >::value,
* "cilk::reducer_max<Value, Compare> only works with an empty Compare class");
*
* @note In a C++11 compiler, this is just the language predefined
* `static_assert` macro.
*
* @note In a non-C++11 compiler, the @a Msg string is not directly included
* in the compiler error message, but it may appear if the compiler
* prints the source line that the error occurred on.
*
* @param Cond The expression to test.
* @param Msg A string explaining the failure.
*
* @ingroup common
*/
#if defined(__INTEL_CXX11_MODE__) || defined(__GXX_EXPERIMENTAL_CXX0X__)
# define __CILKRTS_STATIC_ASSERT(Cond, Msg) static_assert(Cond, Msg)
#else
# define __CILKRTS_STATIC_ASSERT(Cond, Msg) \
typedef int __CILKRTS_STATIC_ASSERT_DUMMY_TYPE \
[::cilk::internal::static_assert_failure<(Cond)>::Success]
/// @cond internal
template <bool> struct static_assert_failure { };
template <> struct static_assert_failure<true> { enum { Success = 1 }; };
# define __CILKRTS_STATIC_ASSERT_DUMMY_TYPE \
__CILKRTS_STATIC_ASSERT_DUMMY_TYPE1(__cilkrts_static_assert_, __LINE__)
# define __CILKRTS_STATIC_ASSERT_DUMMY_TYPE1(a, b) \
__CILKRTS_STATIC_ASSERT_DUMMY_TYPE2(a, b)
# define __CILKRTS_STATIC_ASSERT_DUMMY_TYPE2(a, b) a ## b
/// @endcond
#endif
/// @cond internal
/** @name Aligned heap management.
*/
//@{
/** Implementation-specific aligned memory allocation function.
*
* @param size The minimum number of bytes to allocate.
* @param alignment The required alignment (must be a power of 2).
* @return The address of a block of memory of at least @a size
* bytes. The address will be a multiple of @a alignment.
* `NULL` if the allocation fails.
*
* @see deallocate_aligned()
*/
inline void* allocate_aligned(std::size_t size, std::size_t alignment)
{
#ifdef _WIN32
return _aligned_malloc(size, alignment);
#else
#if defined(__ANDROID__)
return memalign(std::max(alignment, sizeof(void*)), size);
#else
void* ptr;
return (posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size) == 0) ? ptr : 0;
#endif
#endif
}
/** Implementation-specific aligned memory deallocation function.
*
* @param ptr A pointer which was returned by a call to alloc_aligned().
*/
inline void deallocate_aligned(void* ptr)
{
#ifdef _WIN32
_aligned_free(ptr);
#else
std::free(ptr);
#endif
}
/** Class to allocate and guard an aligned pointer.
*
* A new_aligned_pointer object allocates aligned heap-allocated memory when
* it is created, and automatically deallocates it when it is destroyed
* unless its `ok()` function is called.
*
* @tparam T The type of the object to allocate on the heap. The allocated
* will have the size and alignment of an object of type T.
*/
template <typename T>
class new_aligned_pointer {
void* m_ptr;
public:
/// Constructor allocates the pointer.
new_aligned_pointer() :
m_ptr(allocate_aligned(sizeof(T), internal::align_of<T>::value)) {}
/// Destructor deallocates the pointer.
~new_aligned_pointer() { if (m_ptr) deallocate_aligned(m_ptr); }
/// Get the pointer.
operator void*() { return m_ptr; }
/// Return the pointer and release the guard.
T* ok() {
T* ptr = static_cast<T*>(m_ptr);
m_ptr = 0;
return ptr;
}
};
//@}
/// @endcond
} // namespace internal
//@{
/** Allocate an aligned data structure on the heap.
*
* `cilk::aligned_new<T>([args])` is equivalent to `new T([args])`, except
* that it guarantees that the returned pointer will be at least as aligned
* as the alignment requirements of type `T`.
*
* @ingroup common
*/
template <typename T>
T* aligned_new()
{
internal::new_aligned_pointer<T> ptr;
new (ptr) T();
return ptr.ok();
}
template <typename T, typename T1>
T* aligned_new(const T1& x1)
{
internal::new_aligned_pointer<T> ptr;
new (ptr) T(x1);
return ptr.ok();
}
template <typename T, typename T1, typename T2>
T* aligned_new(const T1& x1, const T2& x2)
{
internal::new_aligned_pointer<T> ptr;
new (ptr) T(x1, x2);
return ptr.ok();
}
template <typename T, typename T1, typename T2, typename T3>
T* aligned_new(const T1& x1, const T2& x2, const T3& x3)
{
internal::new_aligned_pointer<T> ptr;
new (ptr) T(x1, x2, x3);
return ptr.ok();
}
template <typename T, typename T1, typename T2, typename T3, typename T4>
T* aligned_new(const T1& x1, const T2& x2, const T3& x3, const T4& x4)
{
internal::new_aligned_pointer<T> ptr;
new (ptr) T(x1, x2, x3, x4);
return ptr.ok();
}
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
T* aligned_new(const T1& x1, const T2& x2, const T3& x3, const T4& x4, const T5& x5)
{
internal::new_aligned_pointer<T> ptr;
new (ptr) T(x1, x2, x3, x4, x5);
return ptr.ok();
}
//@}
/** Deallocate an aligned data structure on the heap.
*
* `cilk::aligned_delete(ptr)` is equivalent to `delete ptr`, except that it
* operates on a pointer that was allocated by aligned_new().
*
* @ingroup common
*/
template <typename T>
void aligned_delete(const T* ptr)
{
ptr->~T();
internal::deallocate_aligned((void*)ptr);
}
} // namespace cilk
#endif // __cplusplus
#endif // METAPROGRAMMING_H_INCLUDED
|