From 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 27 Jun 2017 06:07:23 +0000 Subject: webkitgtk-2.16.5 --- Source/WTF/wtf/StdLibExtras.h | 286 +++++++++++++++++++++++++++--------------- 1 file changed, 188 insertions(+), 98 deletions(-) (limited to 'Source/WTF/wtf/StdLibExtras.h') diff --git a/Source/WTF/wtf/StdLibExtras.h b/Source/WTF/wtf/StdLibExtras.h index 2e35031da..716661fad 100644 --- a/Source/WTF/wtf/StdLibExtras.h +++ b/Source/WTF/wtf/StdLibExtras.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * Copyright (C) 2008, 2016 Apple Inc. All Rights Reserved. * Copyright (C) 2013 Patrick Gansterer * * Redistribution and use in source and binary forms, with or without @@ -28,35 +28,33 @@ #define WTF_StdLibExtras_h #include +#include #include #include #include +#include -// Use these to declare and define a static local variable (static T;) so that -// it is leaked so that its destructors are not called at exit. Using this -// macro also allows workarounds a compiler bug present in Apple's version of GCC 4.0.1. -#ifndef DEFINE_STATIC_LOCAL -#if COMPILER(GCC) && defined(__APPLE_CC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 0 && __GNUC_PATCHLEVEL__ == 1 -#define DEFINE_STATIC_LOCAL(type, name, arguments) \ - static type* name##Ptr = new type arguments; \ - type& name = *name##Ptr -#else -#define DEFINE_STATIC_LOCAL(type, name, arguments) \ +// This was used to declare and define a static local variable (static T;) so that +// it was leaked so that its destructors were not called at exit. +// Newly written code should use static NeverDestroyed instead. +#ifndef DEPRECATED_DEFINE_STATIC_LOCAL +#define DEPRECATED_DEFINE_STATIC_LOCAL(type, name, arguments) \ static type& name = *new type arguments #endif -#endif // Use this macro to declare and define a debug-only global variable that may have a // non-trivial constructor and destructor. When building with clang, this will suppress // warnings about global constructors and exit-time destructors. -#ifndef NDEBUG -#if COMPILER(CLANG) -#define DEFINE_DEBUG_ONLY_GLOBAL(type, name, arguments) \ +#define DEFINE_GLOBAL_FOR_LOGGING(type, name, arguments) \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Wglobal-constructors\"") \ _Pragma("clang diagnostic ignored \"-Wexit-time-destructors\"") \ static type name arguments; \ _Pragma("clang diagnostic pop") + +#ifndef NDEBUG +#if COMPILER(CLANG) +#define DEFINE_DEBUG_ONLY_GLOBAL(type, name, arguments) DEFINE_GLOBAL_FOR_LOGGING(type, name, arguments) #else #define DEFINE_DEBUG_ONLY_GLOBAL(type, name, arguments) \ static type name arguments; @@ -70,10 +68,17 @@ // NULL can cause compiler problems, especially in cases of multiple inheritance. #define OBJECT_OFFSETOF(class, field) (reinterpret_cast(&(reinterpret_cast(0x4000)->field)) - 0x4000) +#define CAST_OFFSET(from, to) (reinterpret_cast(static_cast((reinterpret_cast(0x4000)))) - 0x4000) + // STRINGIZE: Can convert any value to quoted string, even expandable macros #define STRINGIZE(exp) #exp #define STRINGIZE_VALUE_OF(exp) STRINGIZE(exp) +// WTF_CONCAT: concatenate two symbols into one, even expandable macros +#define WTF_CONCAT_INTERNAL_DONT_USE(a, b) a ## b +#define WTF_CONCAT(a, b) WTF_CONCAT_INTERNAL_DONT_USE(a, b) + + /* * The reinterpret_cast([pointer to Type2]) expressions - where * sizeof(Type1) > sizeof(Type2) - cause the following warning on ARM with GCC: @@ -84,7 +89,7 @@ * - https://bugs.webkit.org/show_bug.cgi?id=38045 * - http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43976 */ -#if (CPU(ARM) || CPU(MIPS)) && COMPILER(GCC) +#if (CPU(ARM) || CPU(MIPS)) && COMPILER(GCC_OR_CLANG) template inline bool isPointerTypeAlignmentOkay(Type* ptr) { @@ -115,8 +120,11 @@ inline bool isPointerTypeAlignmentOkay(Type*) namespace WTF { +enum CheckMoveParameterTag { CheckMoveParameter }; + static const size_t KB = 1024; static const size_t MB = 1024 * 1024; +static const size_t GB = 1024 * 1024 * 1024; inline bool isPointerAligned(void* p) { @@ -135,18 +143,20 @@ template inline ToType bitwise_cast(FromType from) { static_assert(sizeof(FromType) == sizeof(ToType), "bitwise_cast size of FromType and ToType must be equal!"); - union { - FromType from; - ToType to; - } u; - u.from = from; - return u.to; +#if COMPILER_SUPPORTS(BUILTIN_IS_TRIVIALLY_COPYABLE) + // Not all recent STL implementations support the std::is_trivially_copyable type trait. Work around this by only checking on toolchains which have the equivalent compiler intrinsic. + static_assert(__is_trivially_copyable(ToType), "bitwise_cast of non-trivially-copyable type!"); + static_assert(__is_trivially_copyable(FromType), "bitwise_cast of non-trivially-copyable type!"); +#endif + typename std::remove_const::type to { }; + std::memcpy(&to, &from, sizeof(to)); + return to; } template inline ToType safeCast(FromType value) { - ASSERT(isInBounds(value)); + RELEASE_ASSERT(isInBounds(value)); return static_cast(value); } @@ -166,23 +176,32 @@ inline size_t bitCount(uint64_t bits) // Macro that returns a compile time constant with the length of an array, but gives an error if passed a non-array. template char (&ArrayLengthHelperFunction(T (&)[Size]))[Size]; // GCC needs some help to deduce a 0 length array. -#if COMPILER(GCC) +#if COMPILER(GCC_OR_CLANG) template char (&ArrayLengthHelperFunction(T (&)[0]))[0]; #endif #define WTF_ARRAY_LENGTH(array) sizeof(::WTF::ArrayLengthHelperFunction(array)) +ALWAYS_INLINE constexpr size_t roundUpToMultipleOfImpl0(size_t remainderMask, size_t x) +{ + return (x + remainderMask) & ~remainderMask; +} + +ALWAYS_INLINE constexpr size_t roundUpToMultipleOfImpl(size_t divisor, size_t x) +{ + return roundUpToMultipleOfImpl0(divisor - 1, x); +} + // Efficient implementation that takes advantage of powers of two. inline size_t roundUpToMultipleOf(size_t divisor, size_t x) { ASSERT(divisor && !(divisor & (divisor - 1))); - size_t remainderMask = divisor - 1; - return (x + remainderMask) & ~remainderMask; + return roundUpToMultipleOfImpl(divisor, x); } -template inline size_t roundUpToMultipleOf(size_t x) +template inline constexpr size_t roundUpToMultipleOf(size_t x) { static_assert(divisor && !(divisor & (divisor - 1)), "divisor must be a power of two!"); - return roundUpToMultipleOf(divisor, x); + return roundUpToMultipleOfImpl(divisor, x); } enum BinarySearchMode { @@ -276,32 +295,120 @@ inline void insertIntoBoundedVector(VectorType& vector, size_t size, const Eleme vector[index] = element; } -} // namespace WTF +// This is here instead of CompilationThread.h to prevent that header from being included +// everywhere. The fact that this method, and that header, exist outside of JSC is a bug. +// https://bugs.webkit.org/show_bug.cgi?id=131815 +WTF_EXPORT_PRIVATE bool isCompilationThread(); + +template +bool isStatelessLambda() +{ + return std::is_empty::value; +} -#if OS(WINCE) -// Windows CE CRT has does not implement bsearch(). -inline void* wtf_bsearch(const void* key, const void* base, size_t count, size_t size, int (*compare)(const void *, const void *)) -{ - const char* first = static_cast(base); - - while (count) { - size_t pos = (count - 1) >> 1; - const char* item = first + pos * size; - int compareResult = compare(item, key); - if (!compareResult) - return const_cast(item); - if (compareResult < 0) { - count -= (pos + 1); - first += (pos + 1) * size; - } else - count = pos; +template +ResultType callStatelessLambda(ArgumentTypes&&... arguments) +{ + uint64_t data[(sizeof(Func) + sizeof(uint64_t) - 1) / sizeof(uint64_t)]; + memset(data, 0, sizeof(data)); + return (*bitwise_cast(data))(std::forward(arguments)...); +} + +template +bool checkAndSet(T& left, U right) +{ + if (left == right) + return false; + left = right; + return true; +} + +template +bool findBitInWord(T word, size_t& index, size_t endIndex, bool value) +{ + static_assert(std::is_unsigned::value, "Type used in findBitInWord must be unsigned"); + + word >>= index; + + while (index < endIndex) { + if ((word & 1) == static_cast(value)) + return true; + index++; + word >>= 1; + } + + index = endIndex; + return false; +} + +// Visitor adapted from http://stackoverflow.com/questions/25338795/is-there-a-name-for-this-tuple-creation-idiom + +template +struct Visitor : Visitor, Visitor { + Visitor(A a, B... b) + : Visitor(a) + , Visitor(b...) + { + } + + using Visitor::operator (); + using Visitor::operator (); +}; + +template +struct Visitor : A { + Visitor(A a) + : A(a) + { } - return 0; + using A::operator(); +}; + +template +Visitor makeVisitor(F... f) +{ + return Visitor(f...); } -#define bsearch(key, base, count, size, compare) wtf_bsearch(key, base, count, size, compare) -#endif +namespace Detail +{ + template class> + struct IsTemplate_ : std::false_type + { + }; + + template class C> + struct IsTemplate_, C> : std::true_type + { + }; +} + +template class Template> +struct IsTemplate : public std::integral_constant::value> {}; + +namespace Detail +{ + template