/* * Copyright (C) 2005-2019 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #pragma once #include #include #include #include #include #include #ifdef __OBJC__ #include #endif namespace WTF { template struct GenericHashTraitsBase; template struct GenericHashTraitsBase { // The emptyValueIsZero flag is used to optimize allocation of empty hash tables with zeroed memory. static constexpr bool emptyValueIsZero = false; // The hasIsEmptyValueFunction flag allows the hash table to automatically generate code to check // for the empty value when it can be done with the equality operator, but allows custom functions // for cases like String that need them. static constexpr bool hasIsEmptyValueFunction = false; // Used by WeakPtr to indicate that the value may become deleted without being explicitly removed. static constexpr bool hasIsReleasedWeakValueFunction = false; // The starting table size. Can be overridden when we know beforehand that // a hash table will have at least N entries. static constexpr unsigned minimumTableSize = 8; }; // Default integer traits disallow both 0 and -1 as keys (max value instead of -1 for unsigned). template struct GenericHashTraitsBase : GenericHashTraitsBase { static constexpr bool emptyValueIsZero = true; static void constructDeletedValue(T& slot) { slot = static_cast(-1); } static bool isDeletedValue(T value) { return value == static_cast(-1); } }; template struct GenericHashTraits : GenericHashTraitsBase::value, T> { typedef T TraitType; typedef T EmptyValueType; static T emptyValue() { return T(); } template static void assignToEmpty(U& emptyValue, V&& value) { emptyValue = std::forward(value); } template static void constructEmptyValue(T& slot) { new (NotNull, std::addressof(slot)) T(Traits::emptyValue()); } // Type for return value of functions that do not transfer ownership, such as get. typedef T PeekType; template static U&& peek(U&& value) { return std::forward(value); } typedef T TakeType; template static TakeType take(U&& value) { return std::forward(value); } }; template struct HashTraits : GenericHashTraits { }; template struct FloatHashTraits : GenericHashTraits { static T emptyValue() { return std::numeric_limits::infinity(); } static void constructDeletedValue(T& slot) { slot = -std::numeric_limits::infinity(); } static bool isDeletedValue(T value) { return value == -std::numeric_limits::infinity(); } }; template<> struct HashTraits : FloatHashTraits { }; template<> struct HashTraits : FloatHashTraits { }; // Default unsigned traits disallow both 0 and max as keys -- use these traits to allow zero and disallow max - 1. template struct UnsignedWithZeroKeyHashTraits : GenericHashTraits { static constexpr bool emptyValueIsZero = false; static T emptyValue() { return std::numeric_limits::max(); } static void constructDeletedValue(T& slot) { slot = std::numeric_limits::max() - 1; } static bool isDeletedValue(T value) { return value == std::numeric_limits::max() - 1; } }; template struct SignedWithZeroKeyHashTraits : GenericHashTraits { static constexpr bool emptyValueIsZero = false; static T emptyValue() { return std::numeric_limits::min(); } static void constructDeletedValue(T& slot) { slot = std::numeric_limits::max(); } static bool isDeletedValue(T value) { return value == std::numeric_limits::max(); } }; // Can be used with strong enums, allows zero as key. template struct StrongEnumHashTraits : GenericHashTraits { using UnderlyingType = typename std::underlying_type::type; static constexpr bool emptyValueIsZero = false; static T emptyValue() { return static_cast(std::numeric_limits::max()); } static void constructDeletedValue(T& slot) { slot = static_cast(std::numeric_limits::max() - 1); } static bool isDeletedValue(T value) { return value == static_cast(std::numeric_limits::max() - 1); } }; template struct HashTraits : GenericHashTraits { static constexpr bool emptyValueIsZero = true; static void constructDeletedValue(P*& slot) { slot = reinterpret_cast(-1); } static bool isDeletedValue(P* value) { return value == reinterpret_cast(-1); } }; #ifdef __OBJC__ template<> struct HashTraits<__unsafe_unretained id> : GenericHashTraits<__unsafe_unretained id> { static constexpr bool emptyValueIsZero = true; static void constructDeletedValue(__unsafe_unretained id& slot) { slot = (__bridge __unsafe_unretained id)reinterpret_cast(-1); } static bool isDeletedValue(__unsafe_unretained id value) { return (__bridge CFTypeRef)value == reinterpret_cast(-1); } }; #endif template struct SimpleClassHashTraits : GenericHashTraits { static constexpr bool emptyValueIsZero = true; static void constructDeletedValue(T& slot) { new (NotNull, std::addressof(slot)) T(HashTableDeletedValue); } static bool isDeletedValue(const T& value) { return value.isHashTableDeletedValue(); } }; template struct HashTraits> : SimpleClassHashTraits> { typedef std::nullptr_t EmptyValueType; static EmptyValueType emptyValue() { return nullptr; } static void constructDeletedValue(std::unique_ptr& slot) { new (NotNull, std::addressof(slot)) std::unique_ptr { reinterpret_cast(-1) }; } static bool isDeletedValue(const std::unique_ptr& value) { return value.get() == reinterpret_cast(-1); } typedef T* PeekType; static T* peek(const std::unique_ptr& value) { return value.get(); } static T* peek(std::nullptr_t) { return nullptr; } static void customDeleteBucket(std::unique_ptr& value) { // The custom delete function exists to avoid a dead store before the value is destructed. // The normal destruction sequence of a bucket would be: // 1) Call the destructor of unique_ptr. // 2) unique_ptr store a zero for its internal pointer. // 3) unique_ptr destroys its value. // 4) Call constructDeletedValue() to set the bucket as destructed. // // The problem is the call in (3) prevents the compile from eliminating the dead store in (2) // becase a side effect of free() could be observing the value. // // This version of deleteBucket() ensures the dead 2 stores changing "value" // are on the same side of the function call. ASSERT(!isDeletedValue(value)); T* pointer = value.release(); constructDeletedValue(value); // The null case happens if a caller uses std::move() to remove the pointer before calling remove() // with an iterator. This is very uncommon. if (LIKELY(pointer)) Deleter()(pointer); } }; template struct HashTraits> : SimpleClassHashTraits> { typedef std::nullptr_t EmptyValueType; static EmptyValueType emptyValue() { return nullptr; } static void constructDeletedValue(UniqueRef& slot) { new (NotNull, std::addressof(slot)) UniqueRef { reinterpret_cast(-1) }; } static bool isDeletedValue(const UniqueRef& value) { return value.get() == reinterpret_cast(-1); } typedef T* PeekType; static const T* peek(const UniqueRef& value) { return &value.get(); } static T* peek(UniqueRef& value) { return &value.get(); } static T* peek(std::nullptr_t) { return nullptr; } using TakeType = std::unique_ptr; static TakeType take(UniqueRef&& value) { return value.moveToUniquePtr(); } static TakeType take(std::nullptr_t) { return nullptr; } }; template struct HashTraits> : SimpleClassHashTraits> { static P* emptyValue() { return nullptr; } typedef P* PeekType; static PeekType peek(const RefPtr

& value) { return value.get(); } static PeekType peek(P* value) { return value; } static void customDeleteBucket(RefPtr

& value) { // See unique_ptr's customDeleteBucket() for an explanation. ASSERT(!SimpleClassHashTraits>::isDeletedValue(value)); auto valueToBeDestroyed = WTFMove(value); SimpleClassHashTraits>::constructDeletedValue(value); } }; template struct RefHashTraits : SimpleClassHashTraits> { static constexpr bool emptyValueIsZero = true; static Ref

emptyValue() { return HashTableEmptyValue; } template static void constructEmptyValue(Ref

& slot) { new (NotNull, std::addressof(slot)) Ref

(HashTableEmptyValue); } static constexpr bool hasIsEmptyValueFunction = true; static bool isEmptyValue(const Ref

& value) { return value.isHashTableEmptyValue(); } using PeekType = P*; static PeekType peek(const Ref

& value) { return const_cast(value.ptrAllowingHashTableEmptyValue()); } static PeekType peek(P* value) { return value; } using TakeType = RefPtr

; static TakeType take(Ref

&& value) { return isEmptyValue(value) ? nullptr : RefPtr

(WTFMove(value)); } }; template struct HashTraits> : RefHashTraits

{ }; template struct HashTraits> : SimpleClassHashTraits> { static constexpr bool hasIsEmptyValueFunction = true; using TargetType = Packed; static_assert(TargetType::alignment < 4 * KB, "The first page is always unmapped since it includes nullptr."); static Packed emptyValue() { return nullptr; } static bool isEmptyValue(const TargetType& value) { return value.get() == nullptr; } using PeekType = P*; static PeekType peek(const TargetType& value) { return value.get(); } static PeekType peek(P* value) { return value; } }; template<> struct HashTraits : SimpleClassHashTraits { static constexpr bool hasIsEmptyValueFunction = true; static bool isEmptyValue(const String&); static void customDeleteBucket(String&); }; // This struct template is an implementation detail of the isHashTraitsEmptyValue function, // which selects either the emptyValue function or the isEmptyValue function to check for empty values. template struct HashTraitsEmptyValueChecker; template struct HashTraitsEmptyValueChecker { template static bool isEmptyValue(const T& value) { return Traits::isEmptyValue(value); } }; template struct HashTraitsEmptyValueChecker { template static bool isEmptyValue(const T& value) { return value == Traits::emptyValue(); } }; template inline bool isHashTraitsEmptyValue(const T& value) { return HashTraitsEmptyValueChecker::isEmptyValue(value); } template struct HashTraitsReleasedWeakValueChecker; template struct HashTraitsReleasedWeakValueChecker { template static bool isReleasedWeakValue(const T& value) { return Traits::isReleasedWeakValue(value); } }; template struct HashTraitsReleasedWeakValueChecker { template static bool isReleasedWeakValue(const T&) { return false; } }; template inline bool isHashTraitsReleasedWeakValue(const T& value) { return HashTraitsReleasedWeakValueChecker::isReleasedWeakValue(value); } template struct HashTraitHasCustomDelete { static T& bucketArg; template static std::true_type TestHasCustomDelete(X*, decltype(X::customDeleteBucket(bucketArg))* = nullptr); static std::false_type TestHasCustomDelete(...); typedef decltype(TestHasCustomDelete(static_cast(nullptr))) ResultType; static constexpr bool value = ResultType::value; }; template typename std::enable_if::value>::type hashTraitsDeleteBucket(T& value) { Traits::customDeleteBucket(value); } template typename std::enable_if::value>::type hashTraitsDeleteBucket(T& value) { value.~T(); Traits::constructDeletedValue(value); } template struct PairHashTraits : GenericHashTraits> { typedef FirstTraitsArg FirstTraits; typedef SecondTraitsArg SecondTraits; typedef std::pair TraitType; typedef std::pair EmptyValueType; static constexpr bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; static EmptyValueType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } static constexpr unsigned minimumTableSize = FirstTraits::minimumTableSize; static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); } static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); } }; template struct HashTraits> : public PairHashTraits, HashTraits> { }; template struct TupleHashTraits : GenericHashTraits> { typedef std::tuple TraitType; typedef std::tuple EmptyValueType; // We should use emptyValueIsZero = Traits::emptyValueIsZero &&... whenever we switch to C++17. We can't do anything // better here right now because GCC can't do C++. template static constexpr bool allTrue(BoolType value) { return value; } template static constexpr bool allTrue(BoolType value, BoolTypes... values) { return value && allTrue(values...); } static constexpr bool emptyValueIsZero = allTrue(FirstTrait::emptyValueIsZero, Traits::emptyValueIsZero...); static EmptyValueType emptyValue() { return std::make_tuple(FirstTrait::emptyValue(), Traits::emptyValue()...); } static constexpr unsigned minimumTableSize = FirstTrait::minimumTableSize; static void constructDeletedValue(TraitType& slot) { FirstTrait::constructDeletedValue(std::get<0>(slot)); } static bool isDeletedValue(const TraitType& value) { return FirstTrait::isDeletedValue(std::get<0>(value)); } }; template struct HashTraits> : public TupleHashTraits...> { }; template struct KeyValuePairHashTraits : GenericHashTraits> { typedef KeyTraitsArg KeyTraits; typedef ValueTraitsArg ValueTraits; typedef KeyValuePair TraitType; typedef KeyValuePair EmptyValueType; typedef typename ValueTraitsArg::TraitType ValueType; static constexpr bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTraits::emptyValueIsZero; static EmptyValueType emptyValue() { return KeyValuePair(KeyTraits::emptyValue(), ValueTraits::emptyValue()); } template static void constructEmptyValue(TraitType& slot) { KeyTraits::template constructEmptyValue(slot.key); ValueTraits::template constructEmptyValue(slot.value); } static constexpr unsigned minimumTableSize = KeyTraits::minimumTableSize; static void constructDeletedValue(TraitType& slot) { KeyTraits::constructDeletedValue(slot.key); } static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDeletedValue(value.key); } static void customDeleteBucket(TraitType& value) { static_assert(std::is_trivially_destructible>::value, "The wrapper itself has to be trivially destructible for customDeleteBucket() to make sense, since we do not destruct the wrapper itself."); hashTraitsDeleteBucket(value.key); value.value.~ValueType(); } }; template struct HashTraits> : public KeyValuePairHashTraits, HashTraits> { }; template struct NullableHashTraits : public HashTraits { static constexpr bool emptyValueIsZero = false; static T emptyValue() { return reinterpret_cast(1); } }; template struct HashTraits> : GenericHashTraits> { static constexpr bool emptyValueIsZero = !inlineCapacity; static void constructDeletedValue(Vector& slot) { new (NotNull, std::addressof(slot)) Vector(WTF::HashTableDeletedValue); } static bool isDeletedValue(const Vector& value) { return value.isHashTableDeletedValue(); } }; // Useful for classes that want complete control over what is empty and what is deleted, // and how to construct both. template struct CustomHashTraits : public GenericHashTraits { static constexpr bool emptyValueIsZero = false; static constexpr bool hasIsEmptyValueFunction = true; static void constructDeletedValue(T& slot) { new (NotNull, std::addressof(slot)) T(T::DeletedValue); } static bool isDeletedValue(const T& value) { return value.isDeletedValue(); } static T emptyValue() { return T(T::EmptyValue); } static bool isEmptyValue(const T& value) { return value.isEmptyValue(); } }; } // namespace WTF using WTF::HashTraits; using WTF::KeyValuePair; using WTF::PairHashTraits; using WTF::NullableHashTraits; using WTF::SimpleClassHashTraits;