/* * Copyright (C) 2008 Apple Inc. All Rights Reserved. * Copyright (C) 2013 Patrick Gansterer * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE INC. 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. */ #ifndef WTF_StdLibExtras_h #define WTF_StdLibExtras_h #include #include #include #include // 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 // 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. #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; #endif // COMPILER(CLANG) #else #define DEFINE_DEBUG_ONLY_GLOBAL(type, name, arguments) #endif // NDEBUG // OBJECT_OFFSETOF: Like the C++ offsetof macro, but you can use it with classes. // The magic number 0x4000 is insignificant. We use it to avoid using NULL, since // NULL can cause compiler problems, especially in cases of multiple inheritance. #define OBJECT_OFFSETOF(class, field) (reinterpret_cast(&(reinterpret_cast(0x4000)->field)) - 0x4000) // STRINGIZE: Can convert any value to quoted string, even expandable macros #define STRINGIZE(exp) #exp #define STRINGIZE_VALUE_OF(exp) STRINGIZE(exp) /* * The reinterpret_cast([pointer to Type2]) expressions - where * sizeof(Type1) > sizeof(Type2) - cause the following warning on ARM with GCC: * increases required alignment of target type. * * An implicit or an extra static_cast bypasses the warning. * For more info see the following bugzilla entries: * - 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_OR_CLANG) template inline bool isPointerTypeAlignmentOkay(Type* ptr) { return !(reinterpret_cast(ptr) % __alignof__(Type)); } template inline TypePtr reinterpret_cast_ptr(void* ptr) { ASSERT(isPointerTypeAlignmentOkay(reinterpret_cast(ptr))); return reinterpret_cast(ptr); } template inline TypePtr reinterpret_cast_ptr(const void* ptr) { ASSERT(isPointerTypeAlignmentOkay(reinterpret_cast(ptr))); return reinterpret_cast(ptr); } #else template inline bool isPointerTypeAlignmentOkay(Type*) { return true; } #define reinterpret_cast_ptr reinterpret_cast #endif namespace WTF { enum CheckMoveParameterTag { CheckMoveParameter }; static const size_t KB = 1024; static const size_t MB = 1024 * 1024; inline bool isPointerAligned(void* p) { return !((intptr_t)(p) & (sizeof(char*) - 1)); } inline bool is8ByteAligned(void* p) { return !((uintptr_t)(p) & (sizeof(double) - 1)); } /* * C++'s idea of a reinterpret_cast lacks sufficient cojones. */ 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; } template inline ToType safeCast(FromType value) { ASSERT(isInBounds(value)); return static_cast(value); } // Returns a count of the number of bits set in 'bits'. inline size_t bitCount(unsigned bits) { bits = bits - ((bits >> 1) & 0x55555555); bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333); return (((bits + (bits >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; } inline size_t bitCount(uint64_t bits) { return bitCount(static_cast(bits)) + bitCount(static_cast(bits >> 32)); } // 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_OR_CLANG) template char (&ArrayLengthHelperFunction(T (&)[0]))[0]; #endif #define WTF_ARRAY_LENGTH(array) sizeof(::WTF::ArrayLengthHelperFunction(array)) // 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; } template inline size_t roundUpToMultipleOf(size_t x) { static_assert(divisor && !(divisor & (divisor - 1)), "divisor must be a power of two!"); return roundUpToMultipleOf(divisor, x); } enum BinarySearchMode { KeyMustBePresentInArray, KeyMightNotBePresentInArray, ReturnAdjacentElementIfKeyIsNotPresent }; template inline ArrayElementType* binarySearchImpl(ArrayType& array, size_t size, KeyType key, const ExtractKey& extractKey = ExtractKey()) { size_t offset = 0; while (size > 1) { size_t pos = (size - 1) >> 1; KeyType val = extractKey(&array[offset + pos]); if (val == key) return &array[offset + pos]; // The item we are looking for is smaller than the item being check; reduce the value of 'size', // chopping off the right hand half of the array. if (key < val) size = pos; // Discard all values in the left hand half of the array, up to and including the item at pos. else { size -= (pos + 1); offset += (pos + 1); } ASSERT(mode != KeyMustBePresentInArray || size); } if (mode == KeyMightNotBePresentInArray && !size) return 0; ArrayElementType* result = &array[offset]; if (mode == KeyMightNotBePresentInArray && key != extractKey(result)) return 0; if (mode == KeyMustBePresentInArray) { ASSERT(size == 1); ASSERT(key == extractKey(result)); } return result; } // If the element is not found, crash if asserts are enabled, and behave like approximateBinarySearch in release builds. template inline ArrayElementType* binarySearch(ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey()) { return binarySearchImpl(array, size, key, extractKey); } // Return zero if the element is not found. template inline ArrayElementType* tryBinarySearch(ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey()) { return binarySearchImpl(array, size, key, extractKey); } // Return the element that is either to the left, or the right, of where the element would have been found. template inline ArrayElementType* approximateBinarySearch(ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey()) { return binarySearchImpl(array, size, key, extractKey); } // Variants of the above that use const. template inline ArrayElementType* binarySearch(const ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey()) { return binarySearchImpl(const_cast(array), size, key, extractKey); } template inline ArrayElementType* tryBinarySearch(const ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey()) { return binarySearchImpl(const_cast(array), size, key, extractKey); } template inline ArrayElementType* approximateBinarySearch(const ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey()) { return binarySearchImpl(const_cast(array), size, key, extractKey); } template inline void insertIntoBoundedVector(VectorType& vector, size_t size, const ElementType& element, size_t index) { for (size_t i = size; i-- > index + 1;) vector[i] = vector[i - 1]; vector[index] = element; } // 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(); } // namespace WTF // This version of placement new omits a 0 check. enum NotNullTag { NotNull }; inline void* operator new(size_t, NotNullTag, void* location) { ASSERT(location); return location; } // This adds various C++14 features for versions of the STL that may not yet have them. namespace std { // MSVC 2013 supports std::make_unique already. #if !defined(_MSC_VER) || _MSC_VER < 1800 template struct _Unique_if { typedef unique_ptr _Single_object; }; template struct _Unique_if { typedef unique_ptr _Unknown_bound; }; template struct _Unique_if { typedef void _Known_bound; }; template inline typename _Unique_if::_Single_object make_unique(Args&&... args) { return unique_ptr(new T(std::forward(args)...)); } template inline typename _Unique_if::_Unknown_bound make_unique(size_t n) { typedef typename remove_extent::type U; return unique_ptr(new U[n]()); } template typename _Unique_if::_Known_bound make_unique(Args&&...) = delete; #endif // MSVC 2015 supports these functions. #if !COMPILER(MSVC) || _MSC_VER < 1900 // Compile-time integer sequences // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html // (Note that we only implement index_sequence, and not the more generic integer_sequence). template struct index_sequence { static size_t size() { return sizeof...(indexes); } }; template struct make_index_sequence_helper; template struct make_index_sequence_helper<0, indexes...> { typedef std::index_sequence type; }; template struct make_index_sequence_helper { typedef typename make_index_sequence_helper::type type; }; template struct make_index_sequence : public make_index_sequence_helper::type { }; // std::exchange template T exchange(T& t, U&& newValue) { T oldValue = std::move(t); t = std::forward(newValue); return oldValue; } #endif #if COMPILER_SUPPORTS(CXX_USER_LITERALS) // These literals are available in C++14, so once we require C++14 compilers we can get rid of them here. // (User-literals need to have a leading underscore so we add it here - the "real" literals don't have underscores). namespace literals { namespace chrono_literals { constexpr inline chrono::seconds operator"" _s(unsigned long long s) { return chrono::seconds(static_cast(s)); } constexpr chrono::milliseconds operator"" _ms(unsigned long long ms) { return chrono::milliseconds(static_cast(ms)); } } } #endif template ALWAYS_INLINE constexpr typename remove_reference::type&& move(T&& value) { static_assert(is_lvalue_reference::value, "T is not an lvalue reference; move() is unnecessary."); using NonRefQualifiedType = typename remove_reference::type; static_assert(!is_const::value, "T is const qualified."); return move(forward(value)); } } // namespace std #define WTFMove(value) std::move(value) using WTF::KB; using WTF::MB; using WTF::isCompilationThread; using WTF::insertIntoBoundedVector; using WTF::isPointerAligned; using WTF::is8ByteAligned; using WTF::binarySearch; using WTF::tryBinarySearch; using WTF::approximateBinarySearch; using WTF::bitwise_cast; using WTF::safeCast; #if COMPILER_SUPPORTS(CXX_USER_LITERALS) // We normally don't want to bring in entire std namespaces, but literals are an exception. using namespace std::literals::chrono_literals; #endif #endif // WTF_StdLibExtras_h