| 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 <type_traits> | 30 #include <type_traits> |
| 31 #include <utility> | 31 #include <utility> |
| 32 | 32 |
| 33 namespace WTF { | 33 namespace WTF { |
| 34 | 34 |
| 35 class String; | 35 class String; |
| 36 template <bool isInteger, typename T> struct GenericHashTraitsBase; | 36 template <bool isInteger, typename T> |
| 37 template <typename T> class OwnPtr; | 37 struct GenericHashTraitsBase; |
| 38 template <typename T> class PassOwnPtr; | 38 template <typename T> |
| 39 template <typename T> struct HashTraits; | 39 class OwnPtr; |
| 40 template <typename T> |
| 41 class PassOwnPtr; |
| 42 template <typename T> |
| 43 struct HashTraits; |
| 40 | 44 |
| 41 enum ShouldWeakPointersBeMarkedStrongly { | 45 enum ShouldWeakPointersBeMarkedStrongly { |
| 42 WeakPointersActStrong, | 46 WeakPointersActStrong, |
| 43 WeakPointersActWeak | 47 WeakPointersActWeak |
| 44 }; | 48 }; |
| 45 | 49 |
| 46 template <typename T> struct GenericHashTraitsBase<false, T> { | 50 template <typename T> |
| 47 // The emptyValueIsZero flag is used to optimize allocation of empty hash | 51 struct GenericHashTraitsBase<false, T> { |
| 48 // tables with zeroed memory. | 52 // The emptyValueIsZero flag is used to optimize allocation of empty hash |
| 49 static const bool emptyValueIsZero = false; | 53 // tables with zeroed memory. |
| 50 | 54 static const bool emptyValueIsZero = false; |
| 51 // The hasIsEmptyValueFunction flag allows the hash table to automatically | 55 |
| 52 // generate code to check for the empty value when it can be done with the | 56 // The hasIsEmptyValueFunction flag allows the hash table to automatically |
| 53 // equality operator, but allows custom functions for cases like String that | 57 // generate code to check for the empty value when it can be done with the |
| 54 // need them. | 58 // equality operator, but allows custom functions for cases like String that |
| 55 static const bool hasIsEmptyValueFunction = false; | 59 // need them. |
| 56 | 60 static const bool hasIsEmptyValueFunction = false; |
| 57 // The starting table size. Can be overridden when we know beforehand that a | 61 |
| 58 // hash table will have at least N entries. | 62 // The starting table size. Can be overridden when we know beforehand that a |
| 63 // hash table will have at least N entries. |
| 59 #if defined(MEMORY_SANITIZER_INITIAL_SIZE) | 64 #if defined(MEMORY_SANITIZER_INITIAL_SIZE) |
| 60 static const unsigned minimumTableSize = 1; | 65 static const unsigned minimumTableSize = 1; |
| 61 #else | 66 #else |
| 62 static const unsigned minimumTableSize = 8; | 67 static const unsigned minimumTableSize = 8; |
| 63 #endif | 68 #endif |
| 64 | 69 |
| 65 template <typename U = void> | 70 template <typename U = void> |
| 66 struct NeedsTracingLazily { | 71 struct NeedsTracingLazily { |
| 67 static const bool value = NeedsTracing<T>::value; | 72 static const bool value = NeedsTracing<T>::value; |
| 68 }; | 73 }; |
| 69 static const WeakHandlingFlag weakHandlingFlag = IsWeak<T>::value ? WeakHand
lingInCollections : NoWeakHandlingInCollections; | 74 static const WeakHandlingFlag weakHandlingFlag = |
| 75 IsWeak<T>::value ? WeakHandlingInCollections |
| 76 : NoWeakHandlingInCollections; |
| 70 }; | 77 }; |
| 71 | 78 |
| 72 // Default integer traits disallow both 0 and -1 as keys (max value instead of | 79 // Default integer traits disallow both 0 and -1 as keys (max value instead of |
| 73 // -1 for unsigned). | 80 // -1 for unsigned). |
| 74 template <typename T> struct GenericHashTraitsBase<true, T> : GenericHashTraitsB
ase<false, T> { | 81 template <typename T> |
| 75 static const bool emptyValueIsZero = true; | 82 struct GenericHashTraitsBase<true, T> : GenericHashTraitsBase<false, T> { |
| 76 static void constructDeletedValue(T& slot, bool) { slot = static_cast<T>(-1)
; } | 83 static const bool emptyValueIsZero = true; |
| 77 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } | 84 static void constructDeletedValue(T& slot, bool) { |
| 78 }; | 85 slot = static_cast<T>(-1); |
| 79 | 86 } |
| 80 template <typename T> struct GenericHashTraits : GenericHashTraitsBase<std::is_i
ntegral<T>::value, T> { | 87 static bool isDeletedValue(T value) { return value == static_cast<T>(-1); } |
| 81 typedef T TraitType; | 88 }; |
| 82 typedef T EmptyValueType; | 89 |
| 83 | 90 template <typename T> |
| 84 static T emptyValue() { return T(); } | 91 struct GenericHashTraits |
| 85 | 92 : GenericHashTraitsBase<std::is_integral<T>::value, T> { |
| 86 // Type for functions that do not take ownership, such as contains. | 93 typedef T TraitType; |
| 87 typedef const T& PeekInType; | 94 typedef T EmptyValueType; |
| 88 typedef T* IteratorGetType; | 95 |
| 89 typedef const T* IteratorConstGetType; | 96 static T emptyValue() { return T(); } |
| 90 typedef T& IteratorReferenceType; | 97 |
| 91 typedef const T& IteratorConstReferenceType; | 98 // Type for functions that do not take ownership, such as contains. |
| 92 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { r
eturn *x; } | 99 typedef const T& PeekInType; |
| 93 static IteratorConstReferenceType getToReferenceConstConversion(IteratorCons
tGetType x) { return *x; } | 100 typedef T* IteratorGetType; |
| 94 // Type for functions that take ownership, such as add. | 101 typedef const T* IteratorConstGetType; |
| 95 // The store function either not be called or called once to store something | 102 typedef T& IteratorReferenceType; |
| 96 // passed in. The value passed to the store function will be PassInType. | 103 typedef const T& IteratorConstReferenceType; |
| 97 typedef const T& PassInType; | 104 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { |
| 98 static void store(const T& value, T& storage) { storage = value; } | 105 return *x; |
| 99 | 106 } |
| 100 // Type for return value of functions that transfer ownership, such as take. | 107 static IteratorConstReferenceType getToReferenceConstConversion( |
| 101 typedef T PassOutType; | 108 IteratorConstGetType x) { |
| 102 static const T& passOut(const T& value) { return value; } | 109 return *x; |
| 103 | 110 } |
| 104 // Type for return value of functions that do not transfer ownership, such | 111 // Type for functions that take ownership, such as add. |
| 105 // as get. | 112 // The store function either not be called or called once to store something |
| 106 // FIXME: We could change this type to const T& for better performance if we | 113 // passed in. The value passed to the store function will be PassInType. |
| 107 // figured out a way to handle the return value from emptyValue, which is a | 114 typedef const T& PassInType; |
| 108 // temporary. | 115 static void store(const T& value, T& storage) { storage = value; } |
| 109 typedef T PeekOutType; | 116 |
| 110 static const T& peek(const T& value) { return value; } | 117 // Type for return value of functions that transfer ownership, such as take. |
| 111 }; | 118 typedef T PassOutType; |
| 112 | 119 static const T& passOut(const T& value) { return value; } |
| 113 template <typename T> struct HashTraits : GenericHashTraits<T> { }; | 120 |
| 114 | 121 // Type for return value of functions that do not transfer ownership, such |
| 115 template <typename T> struct FloatHashTraits : GenericHashTraits<T> { | 122 // as get. |
| 116 static T emptyValue() { return std::numeric_limits<T>::infinity(); } | 123 // FIXME: We could change this type to const T& for better performance if we |
| 117 static void constructDeletedValue(T& slot, bool) { slot = -std::numeric_limi
ts<T>::infinity(); } | 124 // figured out a way to handle the return value from emptyValue, which is a |
| 118 static bool isDeletedValue(T value) { return value == -std::numeric_limits<T
>::infinity(); } | 125 // temporary. |
| 119 }; | 126 typedef T PeekOutType; |
| 120 | 127 static const T& peek(const T& value) { return value; } |
| 121 template <> struct HashTraits<float> : FloatHashTraits<float> { }; | 128 }; |
| 122 template <> struct HashTraits<double> : FloatHashTraits<double> { }; | 129 |
| 130 template <typename T> |
| 131 struct HashTraits : GenericHashTraits<T> {}; |
| 132 |
| 133 template <typename T> |
| 134 struct FloatHashTraits : GenericHashTraits<T> { |
| 135 static T emptyValue() { return std::numeric_limits<T>::infinity(); } |
| 136 static void constructDeletedValue(T& slot, bool) { |
| 137 slot = -std::numeric_limits<T>::infinity(); |
| 138 } |
| 139 static bool isDeletedValue(T value) { |
| 140 return value == -std::numeric_limits<T>::infinity(); |
| 141 } |
| 142 }; |
| 143 |
| 144 template <> |
| 145 struct HashTraits<float> : FloatHashTraits<float> {}; |
| 146 template <> |
| 147 struct HashTraits<double> : FloatHashTraits<double> {}; |
| 123 | 148 |
| 124 // Default unsigned traits disallow both 0 and max as keys -- use these traits | 149 // Default unsigned traits disallow both 0 and max as keys -- use these traits |
| 125 // to allow zero and disallow max - 1. | 150 // to allow zero and disallow max - 1. |
| 126 template <typename T> struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T
> { | 151 template <typename T> |
| 127 static const bool emptyValueIsZero = false; | 152 struct UnsignedWithZeroKeyHashTraits : GenericHashTraits<T> { |
| 128 static T emptyValue() { return std::numeric_limits<T>::max(); } | 153 static const bool emptyValueIsZero = false; |
| 129 static void constructDeletedValue(T& slot, bool) { slot = std::numeric_limit
s<T>::max() - 1; } | 154 static T emptyValue() { return std::numeric_limits<T>::max(); } |
| 130 static bool isDeletedValue(T value) { return value == std::numeric_limits<T>
::max() - 1; } | 155 static void constructDeletedValue(T& slot, bool) { |
| 131 }; | 156 slot = std::numeric_limits<T>::max() - 1; |
| 132 | 157 } |
| 133 template <typename P> struct HashTraits<P*> : GenericHashTraits<P*> { | 158 static bool isDeletedValue(T value) { |
| 134 static const bool emptyValueIsZero = true; | 159 return value == std::numeric_limits<T>::max() - 1; |
| 135 static void constructDeletedValue(P*& slot, bool) { slot = reinterpret_cast<
P*>(-1); } | 160 } |
| 136 static bool isDeletedValue(P* value) { return value == reinterpret_cast<P*>(
-1); } | 161 }; |
| 137 }; | 162 |
| 138 | 163 template <typename P> |
| 139 template <typename T> struct SimpleClassHashTraits : GenericHashTraits<T> { | 164 struct HashTraits<P*> : GenericHashTraits<P*> { |
| 140 static const bool emptyValueIsZero = true; | 165 static const bool emptyValueIsZero = true; |
| 141 static void constructDeletedValue(T& slot, bool) { new (NotNull, &slot) T(Ha
shTableDeletedValue); } | 166 static void constructDeletedValue(P*& slot, bool) { |
| 142 static bool isDeletedValue(const T& value) { return value.isHashTableDeleted
Value(); } | 167 slot = reinterpret_cast<P*>(-1); |
| 143 }; | 168 } |
| 144 | 169 static bool isDeletedValue(P* value) { |
| 145 template <typename P> struct HashTraits<OwnPtr<P>> : SimpleClassHashTraits<OwnPt
r<P>> { | 170 return value == reinterpret_cast<P*>(-1); |
| 146 typedef std::nullptr_t EmptyValueType; | 171 } |
| 147 | 172 }; |
| 148 static EmptyValueType emptyValue() { return nullptr; } | 173 |
| 149 | 174 template <typename T> |
| 150 static const bool hasIsEmptyValueFunction = true; | 175 struct SimpleClassHashTraits : GenericHashTraits<T> { |
| 151 static bool isEmptyValue(const OwnPtr<P>& value) { return !value; } | 176 static const bool emptyValueIsZero = true; |
| 152 | 177 static void constructDeletedValue(T& slot, bool) { |
| 153 typedef typename OwnPtr<P>::PtrType PeekInType; | 178 new (NotNull, &slot) T(HashTableDeletedValue); |
| 154 | 179 } |
| 155 typedef PassOwnPtr<P> PassInType; | 180 static bool isDeletedValue(const T& value) { |
| 156 static void store(PassOwnPtr<P> value, OwnPtr<P>& storage) { storage = value
; } | 181 return value.isHashTableDeletedValue(); |
| 157 | 182 } |
| 158 typedef PassOwnPtr<P> PassOutType; | 183 }; |
| 159 static PassOwnPtr<P> passOut(OwnPtr<P>& value) { return value.release(); } | 184 |
| 160 static PassOwnPtr<P> passOut(std::nullptr_t) { return nullptr; } | 185 template <typename P> |
| 161 | 186 struct HashTraits<OwnPtr<P>> : SimpleClassHashTraits<OwnPtr<P>> { |
| 162 typedef typename OwnPtr<P>::PtrType PeekOutType; | 187 typedef std::nullptr_t EmptyValueType; |
| 163 static PeekOutType peek(const OwnPtr<P>& value) { return value.get(); } | 188 |
| 164 static PeekOutType peek(std::nullptr_t) { return 0; } | 189 static EmptyValueType emptyValue() { return nullptr; } |
| 165 }; | 190 |
| 166 | 191 static const bool hasIsEmptyValueFunction = true; |
| 167 template <typename P> struct HashTraits<RefPtr<P>> : SimpleClassHashTraits<RefPt
r<P>> { | 192 static bool isEmptyValue(const OwnPtr<P>& value) { return !value; } |
| 168 typedef std::nullptr_t EmptyValueType; | 193 |
| 169 static EmptyValueType emptyValue() { return nullptr; } | 194 typedef typename OwnPtr<P>::PtrType PeekInType; |
| 170 | 195 |
| 171 static const bool hasIsEmptyValueFunction = true; | 196 typedef PassOwnPtr<P> PassInType; |
| 172 static bool isEmptyValue(const RefPtr<P>& value) { return !value; } | 197 static void store(PassOwnPtr<P> value, OwnPtr<P>& storage) { |
| 173 | 198 storage = value; |
| 174 typedef RefPtrValuePeeker<P> PeekInType; | 199 } |
| 175 typedef RefPtr<P>* IteratorGetType; | 200 |
| 176 typedef const RefPtr<P>* IteratorConstGetType; | 201 typedef PassOwnPtr<P> PassOutType; |
| 177 typedef RefPtr<P>& IteratorReferenceType; | 202 static PassOwnPtr<P> passOut(OwnPtr<P>& value) { return value.release(); } |
| 178 typedef const RefPtr<P>& IteratorConstReferenceType; | 203 static PassOwnPtr<P> passOut(std::nullptr_t) { return nullptr; } |
| 179 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { r
eturn *x; } | 204 |
| 180 static IteratorConstReferenceType getToReferenceConstConversion(IteratorCons
tGetType x) { return *x; } | 205 typedef typename OwnPtr<P>::PtrType PeekOutType; |
| 181 | 206 static PeekOutType peek(const OwnPtr<P>& value) { return value.get(); } |
| 182 typedef PassRefPtr<P> PassInType; | 207 static PeekOutType peek(std::nullptr_t) { return 0; } |
| 183 static void store(PassRefPtr<P> value, RefPtr<P>& storage) { storage = value
; } | 208 }; |
| 184 | 209 |
| 185 typedef PassRefPtr<P> PassOutType; | 210 template <typename P> |
| 186 static PassOutType passOut(RefPtr<P>& value) { return value.release(); } | 211 struct HashTraits<RefPtr<P>> : SimpleClassHashTraits<RefPtr<P>> { |
| 187 static PassOutType passOut(std::nullptr_t) { return nullptr; } | 212 typedef std::nullptr_t EmptyValueType; |
| 188 | 213 static EmptyValueType emptyValue() { return nullptr; } |
| 189 typedef P* PeekOutType; | 214 |
| 190 static PeekOutType peek(const RefPtr<P>& value) { return value.get(); } | 215 static const bool hasIsEmptyValueFunction = true; |
| 191 static PeekOutType peek(std::nullptr_t) { return 0; } | 216 static bool isEmptyValue(const RefPtr<P>& value) { return !value; } |
| 192 }; | 217 |
| 193 | 218 typedef RefPtrValuePeeker<P> PeekInType; |
| 194 template <typename T> struct HashTraits<RawPtr<T>> : HashTraits<T*> { }; | 219 typedef RefPtr<P>* IteratorGetType; |
| 195 | 220 typedef const RefPtr<P>* IteratorConstGetType; |
| 196 template <> struct HashTraits<String> : SimpleClassHashTraits<String> { | 221 typedef RefPtr<P>& IteratorReferenceType; |
| 197 static const bool hasIsEmptyValueFunction = true; | 222 typedef const RefPtr<P>& IteratorConstReferenceType; |
| 198 static bool isEmptyValue(const String&); | 223 static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { |
| 224 return *x; |
| 225 } |
| 226 static IteratorConstReferenceType getToReferenceConstConversion( |
| 227 IteratorConstGetType x) { |
| 228 return *x; |
| 229 } |
| 230 |
| 231 typedef PassRefPtr<P> PassInType; |
| 232 static void store(PassRefPtr<P> value, RefPtr<P>& storage) { |
| 233 storage = value; |
| 234 } |
| 235 |
| 236 typedef PassRefPtr<P> PassOutType; |
| 237 static PassOutType passOut(RefPtr<P>& value) { return value.release(); } |
| 238 static PassOutType passOut(std::nullptr_t) { return nullptr; } |
| 239 |
| 240 typedef P* PeekOutType; |
| 241 static PeekOutType peek(const RefPtr<P>& value) { return value.get(); } |
| 242 static PeekOutType peek(std::nullptr_t) { return 0; } |
| 243 }; |
| 244 |
| 245 template <typename T> |
| 246 struct HashTraits<RawPtr<T>> : HashTraits<T*> {}; |
| 247 |
| 248 template <> |
| 249 struct HashTraits<String> : SimpleClassHashTraits<String> { |
| 250 static const bool hasIsEmptyValueFunction = true; |
| 251 static bool isEmptyValue(const String&); |
| 199 }; | 252 }; |
| 200 | 253 |
| 201 // This struct template is an implementation detail of the | 254 // This struct template is an implementation detail of the |
| 202 // isHashTraitsEmptyValue function, which selects either the emptyValue function | 255 // isHashTraitsEmptyValue function, which selects either the emptyValue function |
| 203 // or the isEmptyValue function to check for empty values. | 256 // or the isEmptyValue function to check for empty values. |
| 204 template <typename Traits, bool hasEmptyValueFunction> struct HashTraitsEmptyVal
ueChecker; | 257 template <typename Traits, bool hasEmptyValueFunction> |
| 205 template <typename Traits> struct HashTraitsEmptyValueChecker<Traits, true> { | 258 struct HashTraitsEmptyValueChecker; |
| 206 template <typename T> static bool isEmptyValue(const T& value) { return Trai
ts::isEmptyValue(value); } | 259 template <typename Traits> |
| 207 }; | 260 struct HashTraitsEmptyValueChecker<Traits, true> { |
| 208 template <typename Traits> struct HashTraitsEmptyValueChecker<Traits, false> { | 261 template <typename T> |
| 209 template <typename T> static bool isEmptyValue(const T& value) { return valu
e == Traits::emptyValue(); } | 262 static bool isEmptyValue(const T& value) { |
| 210 }; | 263 return Traits::isEmptyValue(value); |
| 211 template <typename Traits, typename T> inline bool isHashTraitsEmptyValue(const
T& value) | 264 } |
| 212 { | 265 }; |
| 213 return HashTraitsEmptyValueChecker<Traits, Traits::hasIsEmptyValueFunction>:
:isEmptyValue(value); | 266 template <typename Traits> |
| 267 struct HashTraitsEmptyValueChecker<Traits, false> { |
| 268 template <typename T> |
| 269 static bool isEmptyValue(const T& value) { |
| 270 return value == Traits::emptyValue(); |
| 271 } |
| 272 }; |
| 273 template <typename Traits, typename T> |
| 274 inline bool isHashTraitsEmptyValue(const T& value) { |
| 275 return HashTraitsEmptyValueChecker< |
| 276 Traits, Traits::hasIsEmptyValueFunction>::isEmptyValue(value); |
| 214 } | 277 } |
| 215 | 278 |
| 216 template <typename FirstTraitsArg, typename SecondTraitsArg> | 279 template <typename FirstTraitsArg, typename SecondTraitsArg> |
| 217 struct PairHashTraits : GenericHashTraits<std::pair<typename FirstTraitsArg::Tra
itType, typename SecondTraitsArg::TraitType>> { | 280 struct PairHashTraits |
| 218 typedef FirstTraitsArg FirstTraits; | 281 : GenericHashTraits<std::pair<typename FirstTraitsArg::TraitType, |
| 219 typedef SecondTraitsArg SecondTraits; | 282 typename SecondTraitsArg::TraitType>> { |
| 220 typedef std::pair<typename FirstTraits::TraitType, typename SecondTraits::Tr
aitType> TraitType; | 283 typedef FirstTraitsArg FirstTraits; |
| 221 typedef std::pair<typename FirstTraits::EmptyValueType, typename SecondTrait
s::EmptyValueType> EmptyValueType; | 284 typedef SecondTraitsArg SecondTraits; |
| 222 | 285 typedef std::pair<typename FirstTraits::TraitType, |
| 223 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && Second
Traits::emptyValueIsZero; | 286 typename SecondTraits::TraitType> |
| 224 static EmptyValueType emptyValue() { return std::make_pair(FirstTraits::empt
yValue(), SecondTraits::emptyValue()); } | 287 TraitType; |
| 225 | 288 typedef std::pair<typename FirstTraits::EmptyValueType, |
| 226 static const unsigned minimumTableSize = FirstTraits::minimumTableSize; | 289 typename SecondTraits::EmptyValueType> |
| 227 | 290 EmptyValueType; |
| 228 static void constructDeletedValue(TraitType& slot, bool zeroValue) | 291 |
| 229 { | 292 static const bool emptyValueIsZero = |
| 230 FirstTraits::constructDeletedValue(slot.first, zeroValue); | 293 FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; |
| 231 // For GC collections the memory for the backing is zeroed when it is | 294 static EmptyValueType emptyValue() { |
| 232 // allocated, and the constructors may take advantage of that, | 295 return std::make_pair(FirstTraits::emptyValue(), |
| 233 // especially if a GC occurs during insertion of an entry into the | 296 SecondTraits::emptyValue()); |
| 234 // table. This slot is being marked deleted, but If the slot is reused | 297 } |
| 235 // at a later point, the same assumptions around memory zeroing must | 298 |
| 236 // hold as they did at the initial allocation. Therefore we zero the | 299 static const unsigned minimumTableSize = FirstTraits::minimumTableSize; |
| 237 // value part of the slot here for GC collections. | 300 |
| 238 if (zeroValue) | 301 static void constructDeletedValue(TraitType& slot, bool zeroValue) { |
| 239 memset(reinterpret_cast<void*>(&slot.second), 0, sizeof(slot.second)
); | 302 FirstTraits::constructDeletedValue(slot.first, zeroValue); |
| 240 } | 303 // For GC collections the memory for the backing is zeroed when it is |
| 241 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isD
eletedValue(value.first); } | 304 // allocated, and the constructors may take advantage of that, |
| 305 // especially if a GC occurs during insertion of an entry into the |
| 306 // table. This slot is being marked deleted, but If the slot is reused |
| 307 // at a later point, the same assumptions around memory zeroing must |
| 308 // hold as they did at the initial allocation. Therefore we zero the |
| 309 // value part of the slot here for GC collections. |
| 310 if (zeroValue) |
| 311 memset(reinterpret_cast<void*>(&slot.second), 0, sizeof(slot.second)); |
| 312 } |
| 313 static bool isDeletedValue(const TraitType& value) { |
| 314 return FirstTraits::isDeletedValue(value.first); |
| 315 } |
| 242 }; | 316 }; |
| 243 | 317 |
| 244 template <typename First, typename Second> | 318 template <typename First, typename Second> |
| 245 struct HashTraits<std::pair<First, Second>> : public PairHashTraits<HashTraits<F
irst>, HashTraits<Second>> { }; | 319 struct HashTraits<std::pair<First, Second>> |
| 320 : public PairHashTraits<HashTraits<First>, HashTraits<Second>> {}; |
| 246 | 321 |
| 247 template <typename KeyTypeArg, typename ValueTypeArg> | 322 template <typename KeyTypeArg, typename ValueTypeArg> |
| 248 struct KeyValuePair { | 323 struct KeyValuePair { |
| 249 typedef KeyTypeArg KeyType; | 324 typedef KeyTypeArg KeyType; |
| 250 | 325 |
| 251 KeyValuePair(const KeyTypeArg& _key, const ValueTypeArg& _value) | 326 KeyValuePair(const KeyTypeArg& _key, const ValueTypeArg& _value) |
| 252 : key(_key) | 327 : key(_key), value(_value) {} |
| 253 , value(_value) | 328 |
| 254 { | 329 template <typename OtherKeyType, typename OtherValueType> |
| 255 } | 330 KeyValuePair(const KeyValuePair<OtherKeyType, OtherValueType>& other) |
| 256 | 331 : key(other.key), value(other.value) {} |
| 257 template <typename OtherKeyType, typename OtherValueType> | 332 |
| 258 KeyValuePair(const KeyValuePair<OtherKeyType, OtherValueType>& other) | 333 KeyTypeArg key; |
| 259 : key(other.key) | 334 ValueTypeArg value; |
| 260 , value(other.value) | |
| 261 { | |
| 262 } | |
| 263 | |
| 264 KeyTypeArg key; | |
| 265 ValueTypeArg value; | |
| 266 }; | 335 }; |
| 267 | 336 |
| 268 template <typename KeyTraitsArg, typename ValueTraitsArg> | 337 template <typename KeyTraitsArg, typename ValueTraitsArg> |
| 269 struct KeyValuePairHashTraits : GenericHashTraits<KeyValuePair<typename KeyTrait
sArg::TraitType, typename ValueTraitsArg::TraitType>> { | 338 struct KeyValuePairHashTraits |
| 270 typedef KeyTraitsArg KeyTraits; | 339 : GenericHashTraits<KeyValuePair<typename KeyTraitsArg::TraitType, |
| 271 typedef ValueTraitsArg ValueTraits; | 340 typename ValueTraitsArg::TraitType>> { |
| 272 typedef KeyValuePair<typename KeyTraits::TraitType, typename ValueTraits::Tr
aitType> TraitType; | 341 typedef KeyTraitsArg KeyTraits; |
| 273 typedef KeyValuePair<typename KeyTraits::EmptyValueType, typename ValueTrait
s::EmptyValueType> EmptyValueType; | 342 typedef ValueTraitsArg ValueTraits; |
| 274 | 343 typedef KeyValuePair<typename KeyTraits::TraitType, |
| 275 static const bool emptyValueIsZero = KeyTraits::emptyValueIsZero && ValueTra
its::emptyValueIsZero; | 344 typename ValueTraits::TraitType> |
| 276 static EmptyValueType emptyValue() { return KeyValuePair<typename KeyTraits:
:EmptyValueType, typename ValueTraits::EmptyValueType>(KeyTraits::emptyValue(),
ValueTraits::emptyValue()); } | 345 TraitType; |
| 277 | 346 typedef KeyValuePair<typename KeyTraits::EmptyValueType, |
| 278 template <typename U = void> | 347 typename ValueTraits::EmptyValueType> |
| 279 struct NeedsTracingLazily { | 348 EmptyValueType; |
| 280 static const bool value = NeedsTracingTrait<KeyTraits>::value || NeedsTr
acingTrait<ValueTraits>::value; | 349 |
| 281 }; | 350 static const bool emptyValueIsZero = |
| 282 static const WeakHandlingFlag weakHandlingFlag = (KeyTraits::weakHandlingFla
g == WeakHandlingInCollections || ValueTraits::weakHandlingFlag == WeakHandlingI
nCollections) ? WeakHandlingInCollections : NoWeakHandlingInCollections; | 351 KeyTraits::emptyValueIsZero && ValueTraits::emptyValueIsZero; |
| 283 | 352 static EmptyValueType emptyValue() { |
| 284 static const unsigned minimumTableSize = KeyTraits::minimumTableSize; | 353 return KeyValuePair<typename KeyTraits::EmptyValueType, |
| 285 | 354 typename ValueTraits::EmptyValueType>( |
| 286 static void constructDeletedValue(TraitType& slot, bool zeroValue) | 355 KeyTraits::emptyValue(), ValueTraits::emptyValue()); |
| 287 { | 356 } |
| 288 KeyTraits::constructDeletedValue(slot.key, zeroValue); | 357 |
| 289 // See similar code in this file for why we need to do this. | 358 template <typename U = void> |
| 290 if (zeroValue) | 359 struct NeedsTracingLazily { |
| 291 memset(reinterpret_cast<void*>(&slot.value), 0, sizeof(slot.value)); | 360 static const bool value = NeedsTracingTrait<KeyTraits>::value || |
| 292 } | 361 NeedsTracingTrait<ValueTraits>::value; |
| 293 static bool isDeletedValue(const TraitType& value) { return KeyTraits::isDel
etedValue(value.key); } | 362 }; |
| 363 static const WeakHandlingFlag weakHandlingFlag = |
| 364 (KeyTraits::weakHandlingFlag == WeakHandlingInCollections || |
| 365 ValueTraits::weakHandlingFlag == WeakHandlingInCollections) |
| 366 ? WeakHandlingInCollections |
| 367 : NoWeakHandlingInCollections; |
| 368 |
| 369 static const unsigned minimumTableSize = KeyTraits::minimumTableSize; |
| 370 |
| 371 static void constructDeletedValue(TraitType& slot, bool zeroValue) { |
| 372 KeyTraits::constructDeletedValue(slot.key, zeroValue); |
| 373 // See similar code in this file for why we need to do this. |
| 374 if (zeroValue) |
| 375 memset(reinterpret_cast<void*>(&slot.value), 0, sizeof(slot.value)); |
| 376 } |
| 377 static bool isDeletedValue(const TraitType& value) { |
| 378 return KeyTraits::isDeletedValue(value.key); |
| 379 } |
| 294 }; | 380 }; |
| 295 | 381 |
| 296 template <typename Key, typename Value> | 382 template <typename Key, typename Value> |
| 297 struct HashTraits<KeyValuePair<Key, Value>> : public KeyValuePairHashTraits<Hash
Traits<Key>, HashTraits<Value>> { }; | 383 struct HashTraits<KeyValuePair<Key, Value>> |
| 384 : public KeyValuePairHashTraits<HashTraits<Key>, HashTraits<Value>> {}; |
| 298 | 385 |
| 299 template <typename T> | 386 template <typename T> |
| 300 struct NullableHashTraits : public HashTraits<T> { | 387 struct NullableHashTraits : public HashTraits<T> { |
| 301 static const bool emptyValueIsZero = false; | 388 static const bool emptyValueIsZero = false; |
| 302 static T emptyValue() { return reinterpret_cast<T>(1); } | 389 static T emptyValue() { return reinterpret_cast<T>(1); } |
| 303 }; | 390 }; |
| 304 | 391 |
| 305 // This is for tracing inside collections that have special support for weak | 392 // This is for tracing inside collections that have special support for weak |
| 306 // pointers. The trait has a trace method which returns true if there are weak | 393 // pointers. The trait has a trace method which returns true if there are weak |
| 307 // pointers to things that have not (yet) been marked live. Returning true | 394 // pointers to things that have not (yet) been marked live. Returning true |
| 308 // indicates that the entry in the collection may yet be removed by weak | 395 // indicates that the entry in the collection may yet be removed by weak |
| 309 // handling. Default implementation for non-weak types is to use the regular | 396 // handling. Default implementation for non-weak types is to use the regular |
| 310 // non-weak TraceTrait. Default implementation for types with weakness is to | 397 // non-weak TraceTrait. Default implementation for types with weakness is to |
| 311 // call traceInCollection on the type's trait. | 398 // call traceInCollection on the type's trait. |
| 312 template <WeakHandlingFlag weakHandlingFlag, ShouldWeakPointersBeMarkedStrongly
strongify, typename T, typename Traits> | 399 template <WeakHandlingFlag weakHandlingFlag, |
| 400 ShouldWeakPointersBeMarkedStrongly strongify, |
| 401 typename T, |
| 402 typename Traits> |
| 313 struct TraceInCollectionTrait; | 403 struct TraceInCollectionTrait; |
| 314 | 404 |
| 315 } // namespace WTF | 405 } // namespace WTF |
| 316 | 406 |
| 317 using WTF::HashTraits; | 407 using WTF::HashTraits; |
| 318 using WTF::PairHashTraits; | 408 using WTF::PairHashTraits; |
| 319 using WTF::NullableHashTraits; | 409 using WTF::NullableHashTraits; |
| 320 using WTF::SimpleClassHashTraits; | 410 using WTF::SimpleClassHashTraits; |
| 321 | 411 |
| 322 #endif // WTF_HashTraits_h | 412 #endif // WTF_HashTraits_h |
| OLD | NEW |