/* * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 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. * */ #ifndef WTF_HashTraits_h #define WTF_HashTraits_h #include "wtf/HashFunctions.h" #include "wtf/HashTableDeletedValueType.h" #include "wtf/StdLibExtras.h" #include "wtf/TypeTraits.h" #include #include // For memset. #include #include namespace WTF { class String; template struct GenericHashTraitsBase; template class OwnPtr; template class PassOwnPtr; template struct HashTraits; enum ShouldWeakPointersBeMarkedStrongly { WeakPointersActStrong, WeakPointersActWeak }; template struct GenericHashTraitsBase { // The emptyValueIsZero flag is used to optimize allocation of empty hash // tables with zeroed memory. static const 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 const bool hasIsEmptyValueFunction = false; // The starting table size. Can be overridden when we know beforehand that a // hash table will have at least N entries. #if defined(MEMORY_SANITIZER_INITIAL_SIZE) static const unsigned minimumTableSize = 1; #else static const unsigned minimumTableSize = 8; #endif template struct NeedsTracingLazily { static const bool value = NeedsTracing::value; }; // The NeedsToForbidGCOnMove flag is used to make the hash table move // operations safe when GC is enabled: if a move constructor invokes // an allocation triggering the GC then it should be invoked within GC // forbidden scope. template struct NeedsToForbidGCOnMove { // TODO(yutak): Consider using of std:::is_trivially_move_constructible // when it is accessible. static const bool value = !std::is_pod::value; }; static const WeakHandlingFlag weakHandlingFlag = IsWeak::value ? WeakHandlingInCollections : NoWeakHandlingInCollections; }; // Default integer traits disallow both 0 and -1 as keys (max value instead of // -1 for unsigned). template struct GenericHashTraitsBase : GenericHashTraitsBase { static const bool emptyValueIsZero = true; static void constructDeletedValue(T& slot, bool) { 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(); } // Type for functions that do not take ownership, such as contains. typedef const T& PeekInType; typedef T* IteratorGetType; typedef const T* IteratorConstGetType; typedef T& IteratorReferenceType; typedef const T& IteratorConstReferenceType; static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; } static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return *x; } // Type for functions that take ownership, such as add. // The store function either not be called or called once to store something // passed in. The value passed to the store function will be PassInType. typedef const T& PassInType; template static void store(IncomingValueType&& value, T& storage) { storage = std::forward(value); } // Type for return value of functions that transfer ownership, such as take. typedef T PassOutType; static T&& passOut(T& value) { return std::move(value); } static T&& passOut(T&& value) { return std::move(value); } // Type for return value of functions that do not transfer ownership, such // as get. // FIXME: We could change this type to const T& for better performance if we // figured out a way to handle the return value from emptyValue, which is a // temporary. typedef T PeekOutType; static const T& peek(const T& value) { return value; } }; template struct HashTraits : GenericHashTraits { }; template struct FloatHashTraits : GenericHashTraits { static T emptyValue() { return std::numeric_limits::infinity(); } static void constructDeletedValue(T& slot, bool) { 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 const bool emptyValueIsZero = false; static T emptyValue() { return std::numeric_limits::max(); } static void constructDeletedValue(T& slot, bool) { slot = std::numeric_limits::max() - 1; } static bool isDeletedValue(T value) { return value == std::numeric_limits::max() - 1; } }; template struct HashTraits : GenericHashTraits { static const bool emptyValueIsZero = true; static void constructDeletedValue(P*& slot, bool) { slot = reinterpret_cast(-1); } static bool isDeletedValue(P* value) { return value == reinterpret_cast(-1); } }; template struct SimpleClassHashTraits : GenericHashTraits { static const bool emptyValueIsZero = true; template struct NeedsToForbidGCOnMove { static const bool value = false; }; static void constructDeletedValue(T& slot, bool) { new (NotNull, &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 const bool hasIsEmptyValueFunction = true; static bool isEmptyValue(const OwnPtr

& value) { return !value; } typedef typename OwnPtr

::PtrType PeekInType; typedef PassOwnPtr

PassInType; static void store(PassOwnPtr

value, OwnPtr

& storage) { storage = value; } typedef PassOwnPtr

PassOutType; static PassOwnPtr

passOut(OwnPtr

& value) { return value.release(); } static PassOwnPtr

passOut(std::nullptr_t) { return nullptr; } typedef typename OwnPtr

::PtrType PeekOutType; static PeekOutType peek(const OwnPtr

& value) { return value.get(); } static PeekOutType peek(std::nullptr_t) { return 0; } }; template struct HashTraits> : SimpleClassHashTraits> { typedef std::nullptr_t EmptyValueType; static EmptyValueType emptyValue() { return nullptr; } static const bool hasIsEmptyValueFunction = true; static bool isEmptyValue(const RefPtr

& value) { return !value; } typedef RefPtrValuePeeker

PeekInType; typedef RefPtr

* IteratorGetType; typedef const RefPtr

* IteratorConstGetType; typedef RefPtr

& IteratorReferenceType; typedef const RefPtr

& IteratorConstReferenceType; static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; } static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return *x; } typedef PassRefPtr

PassInType; static void store(PassRefPtr

value, RefPtr

& storage) { storage = value; } typedef PassRefPtr

PassOutType; static PassOutType passOut(RefPtr

& value) { return value.release(); } static PassOutType passOut(std::nullptr_t) { return nullptr; } typedef P* PeekOutType; static PeekOutType peek(const RefPtr

& value) { return value.get(); } static PeekOutType peek(std::nullptr_t) { return 0; } }; template struct HashTraits> : HashTraits { }; template <> struct HashTraits : SimpleClassHashTraits { static const bool hasIsEmptyValueFunction = true; static bool isEmptyValue(const 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 PairHashTraits : GenericHashTraits> { typedef FirstTraitsArg FirstTraits; typedef SecondTraitsArg SecondTraits; typedef std::pair TraitType; typedef std::pair EmptyValueType; static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; static EmptyValueType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } static const unsigned minimumTableSize = FirstTraits::minimumTableSize; static void constructDeletedValue(TraitType& slot, bool zeroValue) { FirstTraits::constructDeletedValue(slot.first, zeroValue); // For GC collections the memory for the backing is zeroed when it is // allocated, and the constructors may take advantage of that, // especially if a GC occurs during insertion of an entry into the // table. This slot is being marked deleted, but If the slot is reused // at a later point, the same assumptions around memory zeroing must // hold as they did at the initial allocation. Therefore we zero the // value part of the slot here for GC collections. if (zeroValue) memset(reinterpret_cast(&slot.second), 0, sizeof(slot.second)); } static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); } }; template struct HashTraits> : public PairHashTraits, HashTraits> { }; template struct KeyValuePair { typedef KeyTypeArg KeyType; template KeyValuePair(IncomingKeyType&& key, IncomingValueType&& value) : key(std::forward(key)) , value(std::forward(value)) { } template KeyValuePair(KeyValuePair&& other) : key(std::move(other.key)) , value(std::move(other.value)) { } KeyTypeArg key; ValueTypeArg value; }; template struct KeyValuePairHashTraits : GenericHashTraits> { typedef KeyTraitsArg KeyTraits; typedef ValueTraitsArg ValueTraits; typedef KeyValuePair TraitType; typedef KeyValuePair EmptyValueType; static const bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTraits::emptyValueIsZero; static EmptyValueType emptyValue() { return KeyValuePair(KeyTraits::emptyValue(), ValueTraits::emptyValue()); } template struct NeedsTracingLazily { static const bool value = NeedsTracingTrait::value || NeedsTracingTrait::value; }; template struct NeedsToForbidGCOnMove { static const bool value = KeyTraits::template NeedsToForbidGCOnMove<>::value || ValueTraits::template NeedsToForbidGCOnMove<>::value; }; static const WeakHandlingFlag weakHandlingFlag = (KeyTraits::weakHandlingFlag == WeakHandlingInCollections || ValueTraits::weakHandlingFlag == WeakHandlingInCollections) ? WeakHandlingInCollections : NoWeakHandlingInCollections; static const unsigned minimumTableSize = KeyTraits::minimumTableSize; static void constructDeletedValue(TraitType& slot, bool zeroValue) { KeyTraits::constructDeletedValue(slot.key, zeroValue); // See similar code in this file for why we need to do this. if (zeroValue) memset(reinterpret_cast(&slot.value), 0, sizeof(slot.value)); } static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDeletedValue(value.key); } }; template struct HashTraits> : public KeyValuePairHashTraits, HashTraits> { }; template struct NullableHashTraits : public HashTraits { static const bool emptyValueIsZero = false; static T emptyValue() { return reinterpret_cast(1); } }; // This is for tracing inside collections that have special support for weak // pointers. The trait has a trace method which returns true if there are weak // pointers to things that have not (yet) been marked live. Returning true // indicates that the entry in the collection may yet be removed by weak // handling. Default implementation for non-weak types is to use the regular // non-weak TraceTrait. Default implementation for types with weakness is to // call traceInCollection on the type's trait. template struct TraceInCollectionTrait; } // namespace WTF using WTF::HashTraits; using WTF::PairHashTraits; using WTF::NullableHashTraits; using WTF::SimpleClassHashTraits; #endif // WTF_HashTraits_h