| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserv
ed. | 2 * Copyright (C) 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserv
ed. |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU Library General Public License | 14 * You should have received a copy of the GNU Library General Public License |
| 15 * along with this library; see the file COPYING.LIB. If not, write to | 15 * along with this library; see the file COPYING.LIB. If not, write to |
| 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
| 18 * | 18 * |
| 19 */ | 19 */ |
| 20 | 20 |
| 21 #ifndef WTF_HashTraits_h | 21 #ifndef WTF_HashTraits_h |
| 22 #define WTF_HashTraits_h | 22 #define WTF_HashTraits_h |
| 23 | 23 |
| 24 #include "wtf/HashFunctions.h" | 24 #include "wtf/HashFunctions.h" |
| 25 #include "wtf/HashTableDeletedValueType.h" | 25 #include "wtf/HashTableDeletedValueType.h" |
| 26 #include "wtf/StdLibExtras.h" | 26 #include "wtf/StdLibExtras.h" |
| 27 #include "wtf/TypeTraits.h" | 27 #include "wtf/TypeTraits.h" |
| 28 #include <limits> | 28 #include <limits> |
| 29 #include <string.h> // For memset. | 29 #include <string.h> // For memset. |
| 30 #include <utility> | 30 #include <utility> |
| 31 | 31 |
| 32 namespace WTF { | 32 namespace WTF { |
| 33 | 33 |
| 34 class String; | 34 class String; |
| 35 template <bool isInteger, typename T> struct GenericHashTraitsBase; | 35 template <bool isInteger, typename T> |
| 36 template <typename T> class OwnPtr; | 36 struct GenericHashTraitsBase; |
| 37 template <typename T> class PassOwnPtr; | 37 template <typename T> |
| 38 template <typename T> struct HashTraits; | 38 class OwnPtr; |
| 39 template <typename T> |
| 40 class PassOwnPtr; |
| 41 template <typename T> |
| 42 struct HashTraits; |
| 39 | 43 |
| 40 enum ShouldWeakPointersBeMarkedStrongly { | 44 enum ShouldWeakPointersBeMarkedStrongly { |
| 41 WeakPointersActStrong, | 45 WeakPointersActStrong, |
| 42 WeakPointersActWeak | 46 WeakPointersActWeak |
| 43 }; | 47 }; |
| 44 | 48 |
| 45 template <typename T> struct GenericHashTraitsBase<false, T> { | 49 template <typename T> |
| 46 // The emptyValueIsZero flag is used to optimize allocation of empty hash | 50 struct GenericHashTraitsBase<false, T> { |
| 47 // tables with zeroed memory. | 51 // The emptyValueIsZero flag is used to optimize allocation of empty hash |
| 48 static const bool emptyValueIsZero = false; | 52 // tables with zeroed memory. |
| 49 | 53 static const bool emptyValueIsZero = false; |
| 50 // The hasIsEmptyValueFunction flag allows the hash table to automatically | 54 |
| 51 // generate code to check for the empty value when it can be done with the | 55 // The hasIsEmptyValueFunction flag allows the hash table to automatically |
| 52 // equality operator, but allows custom functions for cases like String that | 56 // generate code to check for the empty value when it can be done with the |
| 53 // need them. | 57 // equality operator, but allows custom functions for cases like String that |
| 54 static const bool hasIsEmptyValueFunction = false; | 58 // need them. |
| 55 | 59 static const bool hasIsEmptyValueFunction = false; |
| 56 // The starting table size. Can be overridden when we know beforehand that a | 60 |
| 57 // hash table will have at least N entries. | 61 // The starting table size. Can be overridden when we know beforehand that a |
| 62 // hash table will have at least N entries. |
| 58 #if defined(MEMORY_SANITIZER_INITIAL_SIZE) | 63 #if defined(MEMORY_SANITIZER_INITIAL_SIZE) |
| 59 static const unsigned minimumTableSize = 1; | 64 static const unsigned minimumTableSize = 1; |
| 60 #else | 65 #else |
| 61 static const unsigned minimumTableSize = 8; | 66 static const unsigned minimumTableSize = 8; |
| 62 #endif | 67 #endif |
| 63 | 68 |
| 64 template <typename U = void> | 69 template <typename U = void> |
| 65 struct NeedsTracingLazily { | 70 struct NeedsTracingLazily { |
| 66 static const bool value = NeedsTracing<T>::value; | 71 static const bool value = NeedsTracing<T>::value; |
| 67 }; | 72 }; |
| 68 static const WeakHandlingFlag weakHandlingFlag = IsWeak<T>::value ? WeakHand
lingInCollections : NoWeakHandlingInCollections; | 73 static const WeakHandlingFlag weakHandlingFlag = IsWeak<T>::value ? WeakHandli
ngInCollections : NoWeakHandlingInCollections; |
| 69 }; | 74 }; |
| 70 | 75 |
| 71 // Default integer traits disallow both 0 and -1 as keys (max value instead of | 76 // Default integer traits disallow both 0 and -1 as keys (max value instead of |
| 72 // -1 for unsigned). | 77 // -1 for unsigned). |
| 73 template <typename T> struct GenericHashTraitsBase<true, T> : GenericHashTraitsB
ase<false, T> { | 78 template <typename T> |
| 74 static const bool emptyValueIsZero = true; | 79 struct GenericHashTraitsBase<true, T> : GenericHashTraitsBase<false, T> { |
| 75 static void constructDeletedValue(T& slot, bool) { slot = static_cast<T>(-1)
; } | 80 static const bool emptyValueIsZero = true; |
| 76 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } | 81 static void constructDeletedValue(T& slot, bool) { slot = static_cast<T>(-1);
} |
| 77 }; | 82 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } |
| 78 | 83 }; |
| 79 template <typename T> struct GenericHashTraits : GenericHashTraitsBase<IsInteger
<T>::value, T> { | 84 |
| 80 typedef T TraitType; | 85 template <typename T> |
| 81 typedef T EmptyValueType; | 86 struct GenericHashTraits : GenericHashTraitsBase<IsInteger<T>::value, T> { |
| 82 | 87 typedef T TraitType; |
| 83 static T emptyValue() { return T(); } | 88 typedef T EmptyValueType; |
| 84 | 89 |
| 85 // Type for functions that do not take ownership, such as contains. | 90 static T emptyValue() { return T(); } |
| 86 typedef const T& PeekInType; | 91 |
| 87 typedef T* IteratorGetType; | 92 // Type for functions that do not take ownership, such as contains. |
| 88 typedef const T* IteratorConstGetType; | 93 typedef const T& PeekInType; |
| 89 typedef T& IteratorReferenceType; | 94 typedef T* IteratorGetType; |
| 90 typedef const T& IteratorConstReferenceType; | 95 typedef const T* IteratorConstGetType; |
| 91 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { r
eturn *x; } | 96 typedef T& IteratorReferenceType; |
| 92 static IteratorConstReferenceType getToReferenceConstConversion(IteratorCons
tGetType x) { return *x; } | 97 typedef const T& IteratorConstReferenceType; |
| 93 // Type for functions that take ownership, such as add. | 98 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { ret
urn *x; } |
| 94 // The store function either not be called or called once to store something | 99 static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstG
etType x) { return *x; } |
| 95 // passed in. The value passed to the store function will be PassInType. | 100 // Type for functions that take ownership, such as add. |
| 96 typedef const T& PassInType; | 101 // The store function either not be called or called once to store something |
| 97 static void store(const T& value, T& storage) { storage = value; } | 102 // passed in. The value passed to the store function will be PassInType. |
| 98 | 103 typedef const T& PassInType; |
| 99 // Type for return value of functions that transfer ownership, such as take. | 104 static void store(const T& value, T& storage) { storage = value; } |
| 100 typedef T PassOutType; | 105 |
| 101 static const T& passOut(const T& value) { return value; } | 106 // Type for return value of functions that transfer ownership, such as take. |
| 102 | 107 typedef T PassOutType; |
| 103 // Type for return value of functions that do not transfer ownership, such | 108 static const T& passOut(const T& value) { return value; } |
| 104 // as get. | 109 |
| 105 // FIXME: We could change this type to const T& for better performance if we | 110 // Type for return value of functions that do not transfer ownership, such |
| 106 // figured out a way to handle the return value from emptyValue, which is a | 111 // as get. |
| 107 // temporary. | 112 // FIXME: We could change this type to const T& for better performance if we |
| 108 typedef T PeekOutType; | 113 // figured out a way to handle the return value from emptyValue, which is a |
| 109 static const T& peek(const T& value) { return value; } | 114 // temporary. |
| 110 }; | 115 typedef T PeekOutType; |
| 111 | 116 static const T& peek(const T& value) { return value; } |
| 112 template <typename T> struct HashTraits : GenericHashTraits<T> { }; | 117 }; |
| 113 | 118 |
| 114 template <typename T> struct FloatHashTraits : GenericHashTraits<T> { | 119 template <typename T> |
| 115 static T emptyValue() { return std::numeric_limits<T>::infinity(); } | 120 struct HashTraits : GenericHashTraits<T> {}; |
| 116 static void constructDeletedValue(T& slot, bool) { slot = -std::numeric_limi
ts<T>::infinity(); } | 121 |
| 117 static bool isDeletedValue(T value) { return value == -std::numeric_limits<T
>::infinity(); } | 122 template <typename T> |
| 118 }; | 123 struct FloatHashTraits : GenericHashTraits<T> { |
| 119 | 124 static T emptyValue() { return std::numeric_limits<T>::infinity(); } |
| 120 template <> struct HashTraits<float> : FloatHashTraits<float> { }; | 125 static void constructDeletedValue(T& slot, bool) { slot = -std::numeric_limits
<T>::infinity(); } |
| 121 template <> struct HashTraits<double> : FloatHashTraits<double> { }; | 126 static bool isDeletedValue(T value) { return value == -std::numeric_limits<T>:
:infinity(); } |
| 127 }; |
| 128 |
| 129 template <> |
| 130 struct HashTraits<float> : FloatHashTraits<float> {}; |
| 131 template <> |
| 132 struct HashTraits<double> : FloatHashTraits<double> {}; |
| 122 | 133 |
| 123 // Default unsigned traits disallow both 0 and max as keys -- use these traits | 134 // Default unsigned traits disallow both 0 and max as keys -- use these traits |
| 124 // to allow zero and disallow max - 1. | 135 // to allow zero and disallow max - 1. |
| 125 template <typename T> struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T
> { | 136 template <typename T> |
| 126 static const bool emptyValueIsZero = false; | 137 struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T> { |
| 127 static T emptyValue() { return std::numeric_limits<T>::max(); } | 138 static const bool emptyValueIsZero = false; |
| 128 static void constructDeletedValue(T& slot, bool) { slot = std::numeric_limit
s<T>::max() - 1; } | 139 static T emptyValue() { return std::numeric_limits<T>::max(); } |
| 129 static bool isDeletedValue(T value) { return value == std::numeric_limits<T>
::max() - 1; } | 140 static void constructDeletedValue(T& slot, bool) { slot = std::numeric_limits<
T>::max() - 1; } |
| 130 }; | 141 static bool isDeletedValue(T value) { return value == std::numeric_limits<T>::
max() - 1; } |
| 131 | 142 }; |
| 132 template <typename P> struct HashTraits<P*> : GenericHashTraits<P*> { | 143 |
| 133 static const bool emptyValueIsZero = true; | 144 template <typename P> |
| 134 static void constructDeletedValue(P*& slot, bool) { slot = reinterpret_cast<
P*>(-1); } | 145 struct HashTraits<P*> : GenericHashTraits<P*> { |
| 135 static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(
-1); } | 146 static const bool emptyValueIsZero = true; |
| 136 }; | 147 static void constructDeletedValue(P*& slot, bool) { slot = reinterpret_cast<P*
>(-1); } |
| 137 | 148 static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(-1
); } |
| 138 template <typename T> struct SimpleClassHashTraits : GenericHashTraits<T> { | 149 }; |
| 139 static const bool emptyValueIsZero = true; | 150 |
| 140 static void constructDeletedValue(T& slot, bool) { new (NotNull, &slot) T(Ha
shTableDeletedValue); } | 151 template <typename T> |
| 141 static bool isDeletedValue(const T& value) { return value.isHashTableDeleted
Value(); } | 152 struct SimpleClassHashTraits : GenericHashTraits<T> { |
| 142 }; | 153 static const bool emptyValueIsZero = true; |
| 143 | 154 static void constructDeletedValue(T& slot, bool) { new (NotNull, &slot) T(Hash
TableDeletedValue); } |
| 144 template <typename P> struct HashTraits<OwnPtr<P>> : SimpleClassHashTraits<OwnPt
r<P>> { | 155 static bool isDeletedValue(const T& value) { return value.isHashTableDeletedVa
lue(); } |
| 145 typedef std::nullptr_t EmptyValueType; | 156 }; |
| 146 | 157 |
| 147 static EmptyValueType emptyValue() { return nullptr; } | 158 template <typename P> |
| 148 | 159 struct HashTraits<OwnPtr<P>> : SimpleClassHashTraits<OwnPtr<P>> { |
| 149 static const bool hasIsEmptyValueFunction = true; | 160 typedef std::nullptr_t EmptyValueType; |
| 150 static bool isEmptyValue(const OwnPtr<P>& value) { return !value; } | 161 |
| 151 | 162 static EmptyValueType emptyValue() { return nullptr; } |
| 152 typedef typename OwnPtr<P>::PtrType PeekInType; | 163 |
| 153 | 164 static const bool hasIsEmptyValueFunction = true; |
| 154 typedef PassOwnPtr<P> PassInType; | 165 static bool isEmptyValue(const OwnPtr<P>& value) { return !value; } |
| 155 static void store(PassOwnPtr<P> value, OwnPtr<P>& storage) { storage = value
; } | 166 |
| 156 | 167 typedef typename OwnPtr<P>::PtrType PeekInType; |
| 157 typedef PassOwnPtr<P> PassOutType; | 168 |
| 158 static PassOwnPtr<P> passOut(OwnPtr<P>& value) { return value.release(); } | 169 typedef PassOwnPtr<P> PassInType; |
| 159 static PassOwnPtr<P> passOut(std::nullptr_t) { return nullptr; } | 170 static void store(PassOwnPtr<P> value, OwnPtr<P>& storage) { storage = value;
} |
| 160 | 171 |
| 161 typedef typename OwnPtr<P>::PtrType PeekOutType; | 172 typedef PassOwnPtr<P> PassOutType; |
| 162 static PeekOutType peek(const OwnPtr<P>& value) { return value.get(); } | 173 static PassOwnPtr<P> passOut(OwnPtr<P>& value) { return value.release(); } |
| 163 static PeekOutType peek(std::nullptr_t) { return 0; } | 174 static PassOwnPtr<P> passOut(std::nullptr_t) { return nullptr; } |
| 164 }; | 175 |
| 165 | 176 typedef typename OwnPtr<P>::PtrType PeekOutType; |
| 166 template <typename P> struct HashTraits<RefPtr<P>> : SimpleClassHashTraits<RefPt
r<P>> { | 177 static PeekOutType peek(const OwnPtr<P>& value) { return value.get(); } |
| 167 typedef std::nullptr_t EmptyValueType; | 178 static PeekOutType peek(std::nullptr_t) { return 0; } |
| 168 static EmptyValueType emptyValue() { return nullptr; } | 179 }; |
| 169 | 180 |
| 170 static const bool hasIsEmptyValueFunction = true; | 181 template <typename P> |
| 171 static bool isEmptyValue(const RefPtr<P>& value) { return !value; } | 182 struct HashTraits<RefPtr<P>> : SimpleClassHashTraits<RefPtr<P>> { |
| 172 | 183 typedef std::nullptr_t EmptyValueType; |
| 173 typedef RefPtrValuePeeker<P> PeekInType; | 184 static EmptyValueType emptyValue() { return nullptr; } |
| 174 typedef RefPtr<P>* IteratorGetType; | 185 |
| 175 typedef const RefPtr<P>* IteratorConstGetType; | 186 static const bool hasIsEmptyValueFunction = true; |
| 176 typedef RefPtr<P>& IteratorReferenceType; | 187 static bool isEmptyValue(const RefPtr<P>& value) { return !value; } |
| 177 typedef const RefPtr<P>& IteratorConstReferenceType; | 188 |
| 178 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { r
eturn *x; } | 189 typedef RefPtrValuePeeker<P> PeekInType; |
| 179 static IteratorConstReferenceType getToReferenceConstConversion(IteratorCons
tGetType x) { return *x; } | 190 typedef RefPtr<P>* IteratorGetType; |
| 180 | 191 typedef const RefPtr<P>* IteratorConstGetType; |
| 181 typedef PassRefPtr<P> PassInType; | 192 typedef RefPtr<P>& IteratorReferenceType; |
| 182 static void store(PassRefPtr<P> value, RefPtr<P>& storage) { storage = value
; } | 193 typedef const RefPtr<P>& IteratorConstReferenceType; |
| 183 | 194 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { ret
urn *x; } |
| 184 typedef PassRefPtr<P> PassOutType; | 195 static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstG
etType x) { return *x; } |
| 185 static PassOutType passOut(RefPtr<P>& value) { return value.release(); } | 196 |
| 186 static PassOutType passOut(std::nullptr_t) { return nullptr; } | 197 typedef PassRefPtr<P> PassInType; |
| 187 | 198 static void store(PassRefPtr<P> value, RefPtr<P>& storage) { storage = value;
} |
| 188 typedef P* PeekOutType; | 199 |
| 189 static PeekOutType peek(const RefPtr<P>& value) { return value.get(); } | 200 typedef PassRefPtr<P> PassOutType; |
| 190 static PeekOutType peek(std::nullptr_t) { return 0; } | 201 static PassOutType passOut(RefPtr<P>& value) { return value.release(); } |
| 191 }; | 202 static PassOutType passOut(std::nullptr_t) { return nullptr; } |
| 192 | 203 |
| 193 template <typename T> struct HashTraits<RawPtr<T>> : HashTraits<T*> { }; | 204 typedef P* PeekOutType; |
| 194 | 205 static PeekOutType peek(const RefPtr<P>& value) { return value.get(); } |
| 195 template <> struct HashTraits<String> : SimpleClassHashTraits<String> { | 206 static PeekOutType peek(std::nullptr_t) { return 0; } |
| 196 static const bool hasIsEmptyValueFunction = true; | 207 }; |
| 197 static bool isEmptyValue(const String&); | 208 |
| 209 template <typename T> |
| 210 struct HashTraits<RawPtr<T>> : HashTraits<T*> {}; |
| 211 |
| 212 template <> |
| 213 struct HashTraits<String> : SimpleClassHashTraits<String> { |
| 214 static const bool hasIsEmptyValueFunction = true; |
| 215 static bool isEmptyValue(const String&); |
| 198 }; | 216 }; |
| 199 | 217 |
| 200 // This struct template is an implementation detail of the | 218 // This struct template is an implementation detail of the |
| 201 // isHashTraitsEmptyValue function, which selects either the emptyValue function | 219 // isHashTraitsEmptyValue function, which selects either the emptyValue function |
| 202 // or the isEmptyValue function to check for empty values. | 220 // or the isEmptyValue function to check for empty values. |
| 203 template <typename Traits, bool hasEmptyValueFunction> struct HashTraitsEmptyVal
ueChecker; | 221 template <typename Traits, bool hasEmptyValueFunction> |
| 204 template <typename Traits> struct HashTraitsEmptyValueChecker<Traits, true> { | 222 struct HashTraitsEmptyValueChecker; |
| 205 template <typename T> static bool isEmptyValue(const T& value) { return Trai
ts::isEmptyValue(value); } | 223 template <typename Traits> |
| 206 }; | 224 struct HashTraitsEmptyValueChecker<Traits, true> { |
| 207 template <typename Traits> struct HashTraitsEmptyValueChecker<Traits, false> { | 225 template <typename T> |
| 208 template <typename T> static bool isEmptyValue(const T& value) { return valu
e == Traits::emptyValue(); } | 226 static bool isEmptyValue(const T& value) { return Traits::isEmptyValue(value);
} |
| 209 }; | 227 }; |
| 210 template <typename Traits, typename T> inline bool isHashTraitsEmptyValue(const
T& value) | 228 template <typename Traits> |
| 211 { | 229 struct HashTraitsEmptyValueChecker<Traits, false> { |
| 212 return HashTraitsEmptyValueChecker<Traits, Traits::hasIsEmptyValueFunction>:
:isEmptyValue(value); | 230 template <typename T> |
| 231 static bool isEmptyValue(const T& value) { return value == Traits::emptyValue(
); } |
| 232 }; |
| 233 template <typename Traits, typename T> |
| 234 inline bool isHashTraitsEmptyValue(const T& value) { |
| 235 return HashTraitsEmptyValueChecker<Traits, Traits::hasIsEmptyValueFunction>::i
sEmptyValue(value); |
| 213 } | 236 } |
| 214 | 237 |
| 215 template <typename FirstTraitsArg, typename SecondTraitsArg> | 238 template <typename FirstTraitsArg, typename SecondTraitsArg> |
| 216 struct PairHashTraits : GenericHashTraits<std::pair<typename FirstTraitsArg::Tra
itType, typename SecondTraitsArg::TraitType>> { | 239 struct PairHashTraits : GenericHashTraits<std::pair<typename FirstTraitsArg::Tra
itType, typename SecondTraitsArg::TraitType>> { |
| 217 typedef FirstTraitsArg FirstTraits; | 240 typedef FirstTraitsArg FirstTraits; |
| 218 typedef SecondTraitsArg SecondTraits; | 241 typedef SecondTraitsArg SecondTraits; |
| 219 typedef std::pair<typename FirstTraits::TraitType, typename SecondTraits::Tr
aitType> TraitType; | 242 typedef std::pair<typename FirstTraits::TraitType, typename SecondTraits::Trai
tType> TraitType; |
| 220 typedef std::pair<typename FirstTraits::EmptyValueType, typename SecondTrait
s::EmptyValueType> EmptyValueType; | 243 typedef std::pair<typename FirstTraits::EmptyValueType, typename SecondTraits:
:EmptyValueType> EmptyValueType; |
| 221 | 244 |
| 222 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && Second
Traits::emptyValueIsZero; | 245 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTr
aits::emptyValueIsZero; |
| 223 static EmptyValueType emptyValue() { return std::make_pair(FirstTraits::empt
yValue(), SecondTraits::emptyValue()); } | 246 static EmptyValueType emptyValue() { return std::make_pair(FirstTraits::emptyV
alue(), SecondTraits::emptyValue()); } |
| 224 | 247 |
| 225 static const unsigned minimumTableSize = FirstTraits::minimumTableSize; | 248 static const unsigned minimumTableSize = FirstTraits::minimumTableSize; |
| 226 | 249 |
| 227 static void constructDeletedValue(TraitType& slot, bool zeroValue) | 250 static void constructDeletedValue(TraitType& slot, bool zeroValue) { |
| 228 { | 251 FirstTraits::constructDeletedValue(slot.first, zeroValue); |
| 229 FirstTraits::constructDeletedValue(slot.first, zeroValue); | 252 // For GC collections the memory for the backing is zeroed when it is |
| 230 // For GC collections the memory for the backing is zeroed when it is | 253 // allocated, and the constructors may take advantage of that, |
| 231 // allocated, and the constructors may take advantage of that, | 254 // especially if a GC occurs during insertion of an entry into the |
| 232 // especially if a GC occurs during insertion of an entry into the | 255 // table. This slot is being marked deleted, but If the slot is reused |
| 233 // table. This slot is being marked deleted, but If the slot is reused | 256 // at a later point, the same assumptions around memory zeroing must |
| 234 // at a later point, the same assumptions around memory zeroing must | 257 // hold as they did at the initial allocation. Therefore we zero the |
| 235 // hold as they did at the initial allocation. Therefore we zero the | 258 // value part of the slot here for GC collections. |
| 236 // value part of the slot here for GC collections. | 259 if (zeroValue) |
| 237 if (zeroValue) | 260 memset(reinterpret_cast<void*>(&slot.second), 0, sizeof(slot.second)); |
| 238 memset(reinterpret_cast<void*>(&slot.second), 0, sizeof(slot.second)
); | 261 } |
| 239 } | 262 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDel
etedValue(value.first); } |
| 240 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isD
eletedValue(value.first); } | |
| 241 }; | 263 }; |
| 242 | 264 |
| 243 template <typename First, typename Second> | 265 template <typename First, typename Second> |
| 244 struct HashTraits<std::pair<First, Second>> : public PairHashTraits<HashTraits<F
irst>, HashTraits<Second>> { }; | 266 struct HashTraits<std::pair<First, Second>> : public PairHashTraits<HashTraits<F
irst>, HashTraits<Second>> {}; |
| 245 | 267 |
| 246 template <typename KeyTypeArg, typename ValueTypeArg> | 268 template <typename KeyTypeArg, typename ValueTypeArg> |
| 247 struct KeyValuePair { | 269 struct KeyValuePair { |
| 248 typedef KeyTypeArg KeyType; | 270 typedef KeyTypeArg KeyType; |
| 249 | 271 |
| 250 KeyValuePair(const KeyTypeArg& _key, const ValueTypeArg& _value) | 272 KeyValuePair(const KeyTypeArg& _key, const ValueTypeArg& _value) |
| 251 : key(_key) | 273 : key(_key), value(_value) { |
| 252 , value(_value) | 274 } |
| 253 { | 275 |
| 254 } | 276 template <typename OtherKeyType, typename OtherValueType> |
| 255 | 277 KeyValuePair(const KeyValuePair<OtherKeyType, OtherValueType>& other) |
| 256 template <typename OtherKeyType, typename OtherValueType> | 278 : key(other.key), value(other.value) { |
| 257 KeyValuePair(const KeyValuePair<OtherKeyType, OtherValueType>& other) | 279 } |
| 258 : key(other.key) | 280 |
| 259 , value(other.value) | 281 KeyTypeArg key; |
| 260 { | 282 ValueTypeArg value; |
| 261 } | |
| 262 | |
| 263 KeyTypeArg key; | |
| 264 ValueTypeArg value; | |
| 265 }; | 283 }; |
| 266 | 284 |
| 267 template <typename KeyTraitsArg, typename ValueTraitsArg> | 285 template <typename KeyTraitsArg, typename ValueTraitsArg> |
| 268 struct KeyValuePairHashTraits : GenericHashTraits<KeyValuePair<typename KeyTrait
sArg::TraitType, typename ValueTraitsArg::TraitType>> { | 286 struct KeyValuePairHashTraits : GenericHashTraits<KeyValuePair<typename KeyTrait
sArg::TraitType, typename ValueTraitsArg::TraitType>> { |
| 269 typedef KeyTraitsArg KeyTraits; | 287 typedef KeyTraitsArg KeyTraits; |
| 270 typedef ValueTraitsArg ValueTraits; | 288 typedef ValueTraitsArg ValueTraits; |
| 271 typedef KeyValuePair<typename KeyTraits::TraitType, typename ValueTraits::Tr
aitType> TraitType; | 289 typedef KeyValuePair<typename KeyTraits::TraitType, typename ValueTraits::Trai
tType> TraitType; |
| 272 typedef KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTrait
s::EmptyValueType> EmptyValueType; | 290 typedef KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTraits:
:EmptyValueType> EmptyValueType; |
| 273 | 291 |
| 274 static const bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTra
its::emptyValueIsZero; | 292 static const bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTrait
s::emptyValueIsZero; |
| 275 static EmptyValueType emptyValue() { return KeyValuePair<typename KeyTraits:
:EmptyValueType, typename ValueTraits::EmptyValueType>(KeyTraits::emptyValue(),
ValueTraits::emptyValue()); } | 293 static EmptyValueType emptyValue() { return KeyValuePair<typename KeyTraits::E
mptyValueType, typename ValueTraits::EmptyValueType>(KeyTraits::emptyValue(), Va
lueTraits::emptyValue()); } |
| 276 | 294 |
| 277 template <typename U = void> | 295 template <typename U = void> |
| 278 struct NeedsTracingLazily { | 296 struct NeedsTracingLazily { |
| 279 static const bool value = NeedsTracingTrait<KeyTraits>::value || NeedsTr
acingTrait<ValueTraits>::value; | 297 static const bool value = NeedsTracingTrait<KeyTraits>::value || NeedsTracin
gTrait<ValueTraits>::value; |
| 280 }; | 298 }; |
| 281 static const WeakHandlingFlag weakHandlingFlag = (KeyTraits::weakHandlingFla
g == WeakHandlingInCollections || ValueTraits::weakHandlingFlag == WeakHandlingI
nCollections) ? WeakHandlingInCollections : NoWeakHandlingInCollections; | 299 static const WeakHandlingFlag weakHandlingFlag = (KeyTraits::weakHandlingFlag
== WeakHandlingInCollections || ValueTraits::weakHandlingFlag == WeakHandlingInC
ollections) ? WeakHandlingInCollections : NoWeakHandlingInCollections; |
| 282 | 300 |
| 283 static const unsigned minimumTableSize = KeyTraits::minimumTableSize; | 301 static const unsigned minimumTableSize = KeyTraits::minimumTableSize; |
| 284 | 302 |
| 285 static void constructDeletedValue(TraitType& slot, bool zeroValue) | 303 static void constructDeletedValue(TraitType& slot, bool zeroValue) { |
| 286 { | 304 KeyTraits::constructDeletedValue(slot.key, zeroValue); |
| 287 KeyTraits::constructDeletedValue(slot.key, zeroValue); | 305 // See similar code in this file for why we need to do this. |
| 288 // See similar code in this file for why we need to do this. | 306 if (zeroValue) |
| 289 if (zeroValue) | 307 memset(reinterpret_cast<void*>(&slot.value), 0, sizeof(slot.value)); |
| 290 memset(reinterpret_cast<void*>(&slot.value), 0, sizeof(slot.value)); | 308 } |
| 291 } | 309 static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDelet
edValue(value.key); } |
| 292 static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDel
etedValue(value.key); } | |
| 293 }; | 310 }; |
| 294 | 311 |
| 295 template <typename Key, typename Value> | 312 template <typename Key, typename Value> |
| 296 struct HashTraits<KeyValuePair<Key, Value>> : public KeyValuePairHashTraits<Hash
Traits<Key>, HashTraits<Value>> { }; | 313 struct HashTraits<KeyValuePair<Key, Value>> : public KeyValuePairHashTraits<Hash
Traits<Key>, HashTraits<Value>> {}; |
| 297 | 314 |
| 298 template <typename T> | 315 template <typename T> |
| 299 struct NullableHashTraits : public HashTraits<T> { | 316 struct NullableHashTraits : public HashTraits<T> { |
| 300 static const bool emptyValueIsZero = false; | 317 static const bool emptyValueIsZero = false; |
| 301 static T emptyValue() { return reinterpret_cast<T>(1); } | 318 static T emptyValue() { return reinterpret_cast<T>(1); } |
| 302 }; | 319 }; |
| 303 | 320 |
| 304 // This is for tracing inside collections that have special support for weak | 321 // This is for tracing inside collections that have special support for weak |
| 305 // pointers. The trait has a trace method which returns true if there are weak | 322 // pointers. The trait has a trace method which returns true if there are weak |
| 306 // pointers to things that have not (yet) been marked live. Returning true | 323 // pointers to things that have not (yet) been marked live. Returning true |
| 307 // indicates that the entry in the collection may yet be removed by weak | 324 // indicates that the entry in the collection may yet be removed by weak |
| 308 // handling. Default implementation for non-weak types is to use the regular | 325 // handling. Default implementation for non-weak types is to use the regular |
| 309 // non-weak TraceTrait. Default implementation for types with weakness is to | 326 // non-weak TraceTrait. Default implementation for types with weakness is to |
| 310 // call traceInCollection on the type's trait. | 327 // call traceInCollection on the type's trait. |
| 311 template <WeakHandlingFlag weakHandlingFlag, ShouldWeakPointersBeMarkedStrongly
strongify, typename T, typename Traits> | 328 template <WeakHandlingFlag weakHandlingFlag, ShouldWeakPointersBeMarkedStrongly
strongify, typename T, typename Traits> |
| 312 struct TraceInCollectionTrait; | 329 struct TraceInCollectionTrait; |
| 313 | 330 |
| 314 } // namespace WTF | 331 } // namespace WTF |
| 315 | 332 |
| 316 using WTF::HashTraits; | 333 using WTF::HashTraits; |
| 317 using WTF::PairHashTraits; | 334 using WTF::PairHashTraits; |
| 318 using WTF::NullableHashTraits; | 335 using WTF::NullableHashTraits; |
| 319 using WTF::SimpleClassHashTraits; | 336 using WTF::SimpleClassHashTraits; |
| 320 | 337 |
| 321 #endif // WTF_HashTraits_h | 338 #endif // WTF_HashTraits_h |
| OLD | NEW |