| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. |
| 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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "wtf/TypeTraits.h" | 26 #include "wtf/TypeTraits.h" |
| 27 #include <type_traits> | 27 #include <type_traits> |
| 28 #include <utility> | 28 #include <utility> |
| 29 | 29 |
| 30 namespace WTF { | 30 namespace WTF { |
| 31 | 31 |
| 32 class AtomicString; | 32 class AtomicString; |
| 33 | 33 |
| 34 template <typename T> | 34 template <typename T> |
| 35 struct VectorTraitsBase { | 35 struct VectorTraitsBase { |
| 36 static const bool needsDestruction = !IsTriviallyDestructible<T>::value; | 36 static const bool needsDestruction = !IsTriviallyDestructible<T>::value; |
| 37 | 37 |
| 38 static const bool canInitializeWithMemset = IsTriviallyDefaultConstructible<
T>::value; | 38 static const bool canInitializeWithMemset = |
| 39 // true iff memset(slot, 0, size) constructs an unused slot value that is | 39 IsTriviallyDefaultConstructible<T>::value; |
| 40 // valid for Oilpan to trace and if the value needs destruction, its | 40 // true iff memset(slot, 0, size) constructs an unused slot value that is |
| 41 // destructor can be invoked over. The zero'ed value representing an unused | 41 // valid for Oilpan to trace and if the value needs destruction, its |
| 42 // slot in the vector's backing storage; it does not have to be equal to | 42 // destructor can be invoked over. The zero'ed value representing an unused |
| 43 // what its constructor(s) would create, only be valid for those two uses. | 43 // slot in the vector's backing storage; it does not have to be equal to |
| 44 static const bool canClearUnusedSlotsWithMemset = IsTriviallyDefaultConstruc
tible<T>::value; | 44 // what its constructor(s) would create, only be valid for those two uses. |
| 45 static const bool canClearUnusedSlotsWithMemset = |
| 46 IsTriviallyDefaultConstructible<T>::value; |
| 45 | 47 |
| 46 static const bool canMoveWithMemcpy = IsTriviallyMoveAssignable<T>::value; | 48 static const bool canMoveWithMemcpy = IsTriviallyMoveAssignable<T>::value; |
| 47 static const bool canCopyWithMemcpy = IsTriviallyCopyAssignable<T>::value; | 49 static const bool canCopyWithMemcpy = IsTriviallyCopyAssignable<T>::value; |
| 48 static const bool canFillWithMemset = IsTriviallyDefaultConstructible<T>::va
lue && (sizeof(T) == sizeof(char)); | 50 static const bool canFillWithMemset = |
| 49 static const bool canCompareWithMemcmp = std::is_scalar<T>::value; // Types
without padding. | 51 IsTriviallyDefaultConstructible<T>::value && (sizeof(T) == sizeof(char)); |
| 50 template <typename U = void> | 52 static const bool canCompareWithMemcmp = |
| 51 struct NeedsTracingLazily { | 53 std::is_scalar<T>::value; // Types without padding. |
| 52 static const bool value = NeedsTracing<T>::value; | 54 template <typename U = void> |
| 53 }; | 55 struct NeedsTracingLazily { |
| 54 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollections
; // We don't support weak handling in vectors. | 56 static const bool value = NeedsTracing<T>::value; |
| 57 }; |
| 58 static const WeakHandlingFlag weakHandlingFlag = |
| 59 NoWeakHandlingInCollections; // We don't support weak handling in vectors
. |
| 55 }; | 60 }; |
| 56 | 61 |
| 57 template <typename T> | 62 template <typename T> |
| 58 struct VectorTraits : VectorTraitsBase<T> {}; | 63 struct VectorTraits : VectorTraitsBase<T> {}; |
| 59 | 64 |
| 60 // Classes marked with SimpleVectorTraits will use memmov, memcpy, memcmp | 65 // Classes marked with SimpleVectorTraits will use memmov, memcpy, memcmp |
| 61 // instead of constructors, copy operators, etc for initialization, move and | 66 // instead of constructors, copy operators, etc for initialization, move and |
| 62 // comparison. | 67 // comparison. |
| 63 template <typename T> | 68 template <typename T> |
| 64 struct SimpleClassVectorTraits : VectorTraitsBase<T> { | 69 struct SimpleClassVectorTraits : VectorTraitsBase<T> { |
| 65 static const bool canInitializeWithMemset = true; | 70 static const bool canInitializeWithMemset = true; |
| 66 static const bool canClearUnusedSlotsWithMemset = true; | 71 static const bool canClearUnusedSlotsWithMemset = true; |
| 67 static const bool canMoveWithMemcpy = true; | 72 static const bool canMoveWithMemcpy = true; |
| 68 static const bool canCompareWithMemcmp = true; | 73 static const bool canCompareWithMemcmp = true; |
| 69 }; | 74 }; |
| 70 | 75 |
| 71 // We know OwnPtr and RefPtr are simple enough that initializing to 0 and moving | 76 // We know OwnPtr and RefPtr are simple enough that initializing to 0 and moving |
| 72 // with memcpy (and then not destructing the original) will totally work. | 77 // with memcpy (and then not destructing the original) will totally work. |
| 73 template <typename P> | 78 template <typename P> |
| 74 struct VectorTraits<RefPtr<P>> : SimpleClassVectorTraits<RefPtr<P>> {}; | 79 struct VectorTraits<RefPtr<P>> : SimpleClassVectorTraits<RefPtr<P>> {}; |
| 75 | 80 |
| 76 template <typename P> | 81 template <typename P> |
| 77 struct VectorTraits<OwnPtr<P>> : SimpleClassVectorTraits<OwnPtr<P>> { | 82 struct VectorTraits<OwnPtr<P>> : SimpleClassVectorTraits<OwnPtr<P>> { |
| 78 // OwnPtr -> PassOwnPtr has a very particular structure that tricks the | 83 // OwnPtr -> PassOwnPtr has a very particular structure that tricks the |
| 79 // normal type traits into thinking that the class is "trivially copyable". | 84 // normal type traits into thinking that the class is "trivially copyable". |
| 80 static const bool canCopyWithMemcpy = false; | 85 static const bool canCopyWithMemcpy = false; |
| 81 }; | 86 }; |
| 82 static_assert(VectorTraits<RefPtr<int>>::canInitializeWithMemset, "inefficient R
efPtr Vector"); | 87 static_assert(VectorTraits<RefPtr<int>>::canInitializeWithMemset, |
| 83 static_assert(VectorTraits<RefPtr<int>>::canMoveWithMemcpy, "inefficient RefPtr
Vector"); | 88 "inefficient RefPtr Vector"); |
| 84 static_assert(VectorTraits<RefPtr<int>>::canCompareWithMemcmp, "inefficient RefP
tr Vector"); | 89 static_assert(VectorTraits<RefPtr<int>>::canMoveWithMemcpy, |
| 85 static_assert(VectorTraits<OwnPtr<int>>::canInitializeWithMemset, "inefficient O
wnPtr Vector"); | 90 "inefficient RefPtr Vector"); |
| 86 static_assert(VectorTraits<OwnPtr<int>>::canMoveWithMemcpy, "inefficient OwnPtr
Vector"); | 91 static_assert(VectorTraits<RefPtr<int>>::canCompareWithMemcmp, |
| 87 static_assert(VectorTraits<OwnPtr<int>>::canCompareWithMemcmp, "inefficient OwnP
tr Vector"); | 92 "inefficient RefPtr Vector"); |
| 93 static_assert(VectorTraits<OwnPtr<int>>::canInitializeWithMemset, |
| 94 "inefficient OwnPtr Vector"); |
| 95 static_assert(VectorTraits<OwnPtr<int>>::canMoveWithMemcpy, |
| 96 "inefficient OwnPtr Vector"); |
| 97 static_assert(VectorTraits<OwnPtr<int>>::canCompareWithMemcmp, |
| 98 "inefficient OwnPtr Vector"); |
| 88 | 99 |
| 89 template <typename First, typename Second> | 100 template <typename First, typename Second> |
| 90 struct VectorTraits<std::pair<First, Second>> { | 101 struct VectorTraits<std::pair<First, Second>> { |
| 91 typedef VectorTraits<First> FirstTraits; | 102 typedef VectorTraits<First> FirstTraits; |
| 92 typedef VectorTraits<Second> SecondTraits; | 103 typedef VectorTraits<Second> SecondTraits; |
| 93 | 104 |
| 94 static const bool needsDestruction = FirstTraits::needsDestruction || Second
Traits::needsDestruction; | 105 static const bool needsDestruction = |
| 95 static const bool canInitializeWithMemset = FirstTraits::canInitializeWithMe
mset && SecondTraits::canInitializeWithMemset; | 106 FirstTraits::needsDestruction || SecondTraits::needsDestruction; |
| 96 static const bool canMoveWithMemcpy = FirstTraits::canMoveWithMemcpy && Seco
ndTraits::canMoveWithMemcpy; | 107 static const bool canInitializeWithMemset = |
| 97 static const bool canCopyWithMemcpy = FirstTraits::canCopyWithMemcpy && Seco
ndTraits::canCopyWithMemcpy; | 108 FirstTraits::canInitializeWithMemset && |
| 98 static const bool canFillWithMemset = false; | 109 SecondTraits::canInitializeWithMemset; |
| 99 static const bool canCompareWithMemcmp = FirstTraits::canCompareWithMemcmp &
& SecondTraits::canCompareWithMemcmp; | 110 static const bool canMoveWithMemcpy = |
| 100 static const bool canClearUnusedSlotsWithMemset = FirstTraits::canClearUnuse
dSlotsWithMemset && SecondTraits::canClearUnusedSlotsWithMemset; | 111 FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy; |
| 101 template <typename U = void> | 112 static const bool canCopyWithMemcpy = |
| 102 struct NeedsTracingLazily { | 113 FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy; |
| 103 static const bool value = NeedsTracingTrait<FirstTraits>::value || Needs
TracingTrait<SecondTraits>::value; | 114 static const bool canFillWithMemset = false; |
| 104 }; | 115 static const bool canCompareWithMemcmp = |
| 105 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollections
; // We don't support weak handling in vectors. | 116 FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp; |
| 117 static const bool canClearUnusedSlotsWithMemset = |
| 118 FirstTraits::canClearUnusedSlotsWithMemset && |
| 119 SecondTraits::canClearUnusedSlotsWithMemset; |
| 120 template <typename U = void> |
| 121 struct NeedsTracingLazily { |
| 122 static const bool value = NeedsTracingTrait<FirstTraits>::value || |
| 123 NeedsTracingTrait<SecondTraits>::value; |
| 124 }; |
| 125 static const WeakHandlingFlag weakHandlingFlag = |
| 126 NoWeakHandlingInCollections; // We don't support weak handling in vectors
. |
| 106 }; | 127 }; |
| 107 | 128 |
| 108 } // namespace WTF | 129 } // namespace WTF |
| 109 | 130 |
| 110 #define WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(ClassName) \ | 131 #define WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(ClassName) \ |
| 111 namespace WTF { \ | 132 namespace WTF { \ |
| 112 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || !IsTrivially
MoveAssignable<ClassName>::value || !std::is_scalar<ClassName>::value, "macro no
t needed"); \ | 133 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || \ |
| 113 template <> \ | 134 !IsTriviallyMoveAssignable<ClassName>::value || \ |
| 114 struct VectorTraits<ClassName> : SimpleClassVectorTraits<ClassName> {}; \ | 135 !std::is_scalar<ClassName>::value, \ |
| 115 } | 136 "macro not needed"); \ |
| 137 template <> \ |
| 138 struct VectorTraits<ClassName> : SimpleClassVectorTraits<ClassName> {}; \ |
| 139 } |
| 116 | 140 |
| 117 #define WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(ClassName) \ | 141 #define WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(ClassName) \ |
| 118 namespace WTF { \ | 142 namespace WTF { \ |
| 119 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || !IsTrivially
MoveAssignable<ClassName>::value, "macro not needed"); \ | 143 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || \ |
| 120 template <> \ | 144 !IsTriviallyMoveAssignable<ClassName>::value, \ |
| 121 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \ | 145 "macro not needed"); \ |
| 122 { \ | 146 template <> \ |
| 123 static const bool canInitializeWithMemset = true; \ | 147 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> { \ |
| 124 static const bool canClearUnusedSlotsWithMemset = true; \ | 148 static const bool canInitializeWithMemset = true; \ |
| 125 static const bool canMoveWithMemcpy = true; \ | 149 static const bool canClearUnusedSlotsWithMemset = true; \ |
| 126 }; \ | 150 static const bool canMoveWithMemcpy = true; \ |
| 127 } | 151 }; \ |
| 152 } |
| 128 | 153 |
| 129 #define WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(ClassName) \ | 154 #define WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(ClassName) \ |
| 130 namespace WTF { \ | 155 namespace WTF { \ |
| 131 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, "macro not nee
ded"); \ | 156 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, \ |
| 132 template <> \ | 157 "macro not needed"); \ |
| 133 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \ | 158 template <> \ |
| 134 { \ | 159 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> { \ |
| 135 static const bool canInitializeWithMemset = true; \ | 160 static const bool canInitializeWithMemset = true; \ |
| 136 static const bool canClearUnusedSlotsWithMemset = true; \ | 161 static const bool canClearUnusedSlotsWithMemset = true; \ |
| 137 }; \ | 162 }; \ |
| 138 } | 163 } |
| 139 | 164 |
| 140 #define WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(ClassName) \ | 165 #define WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(ClassName) \ |
| 141 namespace WTF { \ | 166 namespace WTF { \ |
| 142 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, "macro not nee
ded"); \ | 167 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, \ |
| 143 template <> \ | 168 "macro not needed"); \ |
| 144 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> \ | 169 template <> \ |
| 145 { \ | 170 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> { \ |
| 146 static const bool canClearUnusedSlotsWithMemset = true; \ | 171 static const bool canClearUnusedSlotsWithMemset = true; \ |
| 147 }; \ | 172 }; \ |
| 148 } | 173 } |
| 149 | 174 |
| 150 using WTF::VectorTraits; | 175 using WTF::VectorTraits; |
| 151 using WTF::SimpleClassVectorTraits; | 176 using WTF::SimpleClassVectorTraits; |
| 152 | 177 |
| 153 #endif // WTF_VectorTraits_h | 178 #endif // WTF_VectorTraits_h |
| OLD | NEW |