#ifndef WTF_StdLibExtras_h
#define WTF_StdLibExtras_h
#include <chrono>
#include <memory>
#include <string.h>
#include <wtf/Assertions.h>
#include <wtf/CheckedArithmetic.h>
#ifndef DEPRECATED_DEFINE_STATIC_LOCAL
#define DEPRECATED_DEFINE_STATIC_LOCAL(type, name, arguments) \
static type& name = *new type arguments
#endif
#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
#define OBJECT_OFFSETOF(class, field) (reinterpret_cast<ptrdiff_t>(&(reinterpret_cast<class*>(0x4000)->field)) - 0x4000)
#define STRINGIZE(exp) #exp
#define STRINGIZE_VALUE_OF(exp) STRINGIZE(exp)
#if OS(WINDOWS) && !defined(PRId64)
#define PRId64 "lld"
#endif
#if (CPU(ARM) || CPU(MIPS)) && COMPILER(GCC_OR_CLANG)
template<typename Type>
inline bool isPointerTypeAlignmentOkay(Type* ptr)
{
return !(reinterpret_cast<intptr_t>(ptr) % __alignof__(Type));
}
template<typename TypePtr>
inline TypePtr reinterpret_cast_ptr(void* ptr)
{
ASSERT(isPointerTypeAlignmentOkay(reinterpret_cast<TypePtr>(ptr)));
return reinterpret_cast<TypePtr>(ptr);
}
template<typename TypePtr>
inline TypePtr reinterpret_cast_ptr(const void* ptr)
{
ASSERT(isPointerTypeAlignmentOkay(reinterpret_cast<TypePtr>(ptr)));
return reinterpret_cast<TypePtr>(ptr);
}
#else
template<typename Type>
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;
static const size_t GB = 1024 * 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));
}
template<typename ToType, typename FromType>
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<typename ToType, typename FromType>
inline ToType safeCast(FromType value)
{
ASSERT(isInBounds<ToType>(value));
return static_cast<ToType>(value);
}
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<unsigned>(bits)) + bitCount(static_cast<unsigned>(bits >> 32));
}
template<typename T, size_t Size> char (&ArrayLengthHelperFunction(T (&)[Size]))[Size];
#if COMPILER(GCC_OR_CLANG)
template<typename T> char (&ArrayLengthHelperFunction(T (&)[0]))[0];
#endif
#define WTF_ARRAY_LENGTH(array) sizeof(::WTF::ArrayLengthHelperFunction(array))
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<size_t divisor> 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<typename ArrayElementType, typename KeyType, typename ArrayType, typename ExtractKey, BinarySearchMode mode>
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];
if (key < val)
size = 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;
}
template<typename ArrayElementType, typename KeyType, typename ArrayType, typename ExtractKey>
inline ArrayElementType* binarySearch(ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey())
{
return binarySearchImpl<ArrayElementType, KeyType, ArrayType, ExtractKey, KeyMustBePresentInArray>(array, size, key, extractKey);
}
template<typename ArrayElementType, typename KeyType, typename ArrayType, typename ExtractKey>
inline ArrayElementType* tryBinarySearch(ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey())
{
return binarySearchImpl<ArrayElementType, KeyType, ArrayType, ExtractKey, KeyMightNotBePresentInArray>(array, size, key, extractKey);
}
template<typename ArrayElementType, typename KeyType, typename ArrayType, typename ExtractKey>
inline ArrayElementType* approximateBinarySearch(ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey())
{
return binarySearchImpl<ArrayElementType, KeyType, ArrayType, ExtractKey, ReturnAdjacentElementIfKeyIsNotPresent>(array, size, key, extractKey);
}
template<typename ArrayElementType, typename KeyType, typename ArrayType, typename ExtractKey>
inline ArrayElementType* binarySearch(const ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey())
{
return binarySearchImpl<ArrayElementType, KeyType, ArrayType, ExtractKey, KeyMustBePresentInArray>(const_cast<ArrayType&>(array), size, key, extractKey);
}
template<typename ArrayElementType, typename KeyType, typename ArrayType, typename ExtractKey>
inline ArrayElementType* tryBinarySearch(const ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey())
{
return binarySearchImpl<ArrayElementType, KeyType, ArrayType, ExtractKey, KeyMightNotBePresentInArray>(const_cast<ArrayType&>(array), size, key, extractKey);
}
template<typename ArrayElementType, typename KeyType, typename ArrayType, typename ExtractKey>
inline ArrayElementType* approximateBinarySearch(const ArrayType& array, size_t size, KeyType key, ExtractKey extractKey = ExtractKey())
{
return binarySearchImpl<ArrayElementType, KeyType, ArrayType, ExtractKey, ReturnAdjacentElementIfKeyIsNotPresent>(const_cast<ArrayType&>(array), size, key, extractKey);
}
template<typename VectorType, typename ElementType>
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;
}
WTF_EXPORT_PRIVATE bool isCompilationThread();
template<typename Func>
bool isStatelessLambda()
{
return std::is_empty<Func>::value;
}
template<typename ResultType, typename Func, typename... ArgumentTypes>
ResultType callStatelessLambda(ArgumentTypes&&... arguments)
{
uint64_t data[(sizeof(Func) + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
memset(data, 0, sizeof(data));
return (*bitwise_cast<Func*>(data))(std::forward<ArgumentTypes>(arguments)...);
}
template<typename T, typename U>
bool checkAndSet(T& left, U right)
{
if (left == right)
return false;
left = right;
return true;
}
}
enum NotNullTag { NotNull };
inline void* operator new(size_t, NotNullTag, void* location)
{
ASSERT(location);
return location;
}
namespace std {
#if COMPILER(CLANG) && __cplusplus < 201400L
template<class T> struct _Unique_if {
typedef unique_ptr<T> _Single_object;
};
template<class T> struct _Unique_if<T[]> {
typedef unique_ptr<T[]> _Unknown_bound;
};
template<class T, size_t N> struct _Unique_if<T[N]> {
typedef void _Known_bound;
};
template<class T, class... Args> inline typename _Unique_if<T>::_Single_object
make_unique(Args&&... args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T> inline typename _Unique_if<T>::_Unknown_bound
make_unique(size_t n)
{
typedef typename remove_extent<T>::type U;
return unique_ptr<T>(new U[n]());
}
template<class T, class... Args> typename _Unique_if<T>::_Known_bound
make_unique(Args&&...) = delete;
template<class T, class U = T>
T exchange(T& t, U&& newValue)
{
T oldValue = std::move(t);
t = std::forward<U>(newValue);
return oldValue;
}
#endif
template<WTF::CheckMoveParameterTag, typename T>
ALWAYS_INLINE constexpr typename remove_reference<T>::type&& move(T&& value)
{
static_assert(is_lvalue_reference<T>::value, "T is not an lvalue reference; move() is unnecessary.");
using NonRefQualifiedType = typename remove_reference<T>::type;
static_assert(!is_const<NonRefQualifiedType>::value, "T is const qualified.");
return move(forward<T>(value));
}
}
#define WTFMove(value) std::move<WTF::CheckMoveParameter>(value)
using WTF::KB;
using WTF::MB;
using WTF::approximateBinarySearch;
using WTF::binarySearch;
using WTF::bitwise_cast;
using WTF::callStatelessLambda;
using WTF::checkAndSet;
using WTF::insertIntoBoundedVector;
using WTF::isCompilationThread;
using WTF::isPointerAligned;
using WTF::isStatelessLambda;
using WTF::is8ByteAligned;
using WTF::safeCast;
using WTF::tryBinarySearch;
#if !COMPILER(CLANG) || __cplusplus >= 201400L
using namespace std::literals::chrono_literals;
#endif
#endif // WTF_StdLibExtras_h