| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * | 3 // found in the LICENSE file. |
| 4 * This library is free software; you can redistribute it and/or | |
| 5 * modify it under the terms of the GNU Library General Public | |
| 6 * License as published by the Free Software Foundation; either | |
| 7 * version 2 of the License, or (at your option) any later version. | |
| 8 * | |
| 9 * This library is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 12 * Library General Public License for more details. | |
| 13 * | |
| 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 | |
| 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 17 * Boston, MA 02110-1301, USA. | |
| 18 * | |
| 19 */ | |
| 20 | 4 |
| 21 #ifndef WTF_VectorTraits_h | 5 #include "platform/wtf/VectorTraits.h" |
| 22 #define WTF_VectorTraits_h | |
| 23 | 6 |
| 24 #include "wtf/RefPtr.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 25 #include "wtf/TypeTraits.h" | 8 // WTF migration project. See the following post for details: |
| 26 #include <memory> | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 27 #include <type_traits> | |
| 28 #include <utility> | |
| 29 | |
| 30 namespace WTF { | |
| 31 | |
| 32 template <typename T> | |
| 33 struct VectorTraitsBase { | |
| 34 static const bool needsDestruction = !IsTriviallyDestructible<T>::value; | |
| 35 | |
| 36 static const bool canInitializeWithMemset = | |
| 37 IsTriviallyDefaultConstructible<T>::value; | |
| 38 // true iff memset(slot, 0, size) constructs an unused slot value that is | |
| 39 // valid for Oilpan to trace and if the value needs destruction, its | |
| 40 // destructor can be invoked over. The zero'ed value representing an unused | |
| 41 // slot in the vector's backing storage; it does not have to be equal to | |
| 42 // what its constructor(s) would create, only be valid for those two uses. | |
| 43 static const bool canClearUnusedSlotsWithMemset = | |
| 44 IsTriviallyDefaultConstructible<T>::value; | |
| 45 | |
| 46 static const bool canMoveWithMemcpy = IsTriviallyMoveAssignable<T>::value; | |
| 47 static const bool canCopyWithMemcpy = IsTriviallyCopyAssignable<T>::value; | |
| 48 static const bool canFillWithMemset = | |
| 49 IsTriviallyDefaultConstructible<T>::value && (sizeof(T) == sizeof(char)); | |
| 50 static const bool canCompareWithMemcmp = | |
| 51 std::is_scalar<T>::value; // Types without padding. | |
| 52 | |
| 53 // Supports swapping elements using regular std::swap semantics. | |
| 54 static const bool canSwapUsingCopyOrMove = true; | |
| 55 | |
| 56 template <typename U = void> | |
| 57 struct IsTraceableInCollection { | |
| 58 static const bool value = IsTraceable<T>::value; | |
| 59 }; | |
| 60 // We don't support weak handling in vectors. | |
| 61 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollections; | |
| 62 }; | |
| 63 | |
| 64 template <typename T> | |
| 65 struct VectorTraits : VectorTraitsBase<T> {}; | |
| 66 | |
| 67 // Classes marked with SimpleVectorTraits will use memmov, memcpy, memcmp | |
| 68 // instead of constructors, copy operators, etc for initialization, move and | |
| 69 // comparison. | |
| 70 template <typename T> | |
| 71 struct SimpleClassVectorTraits : VectorTraitsBase<T> { | |
| 72 static const bool canInitializeWithMemset = true; | |
| 73 static const bool canClearUnusedSlotsWithMemset = true; | |
| 74 static const bool canMoveWithMemcpy = true; | |
| 75 static const bool canCompareWithMemcmp = true; | |
| 76 }; | |
| 77 | |
| 78 // We know std::unique_ptr and RefPtr are simple enough that initializing to 0 | |
| 79 // and moving with memcpy (and then not destructing the original) will totally | |
| 80 // work. | |
| 81 template <typename P> | |
| 82 struct VectorTraits<RefPtr<P>> : SimpleClassVectorTraits<RefPtr<P>> {}; | |
| 83 | |
| 84 template <typename P> | |
| 85 struct VectorTraits<std::unique_ptr<P>> | |
| 86 : SimpleClassVectorTraits<std::unique_ptr<P>> { | |
| 87 // std::unique_ptr -> std::unique_ptr has a very particular structure that | |
| 88 // tricks the normal type traits into thinking that the class is "trivially | |
| 89 // copyable". | |
| 90 static const bool canCopyWithMemcpy = false; | |
| 91 }; | |
| 92 static_assert(VectorTraits<RefPtr<int>>::canInitializeWithMemset, | |
| 93 "inefficient RefPtr Vector"); | |
| 94 static_assert(VectorTraits<RefPtr<int>>::canMoveWithMemcpy, | |
| 95 "inefficient RefPtr Vector"); | |
| 96 static_assert(VectorTraits<RefPtr<int>>::canCompareWithMemcmp, | |
| 97 "inefficient RefPtr Vector"); | |
| 98 static_assert(VectorTraits<std::unique_ptr<int>>::canInitializeWithMemset, | |
| 99 "inefficient std::unique_ptr Vector"); | |
| 100 static_assert(VectorTraits<std::unique_ptr<int>>::canMoveWithMemcpy, | |
| 101 "inefficient std::unique_ptr Vector"); | |
| 102 static_assert(VectorTraits<std::unique_ptr<int>>::canCompareWithMemcmp, | |
| 103 "inefficient std::unique_ptr Vector"); | |
| 104 | |
| 105 template <typename First, typename Second> | |
| 106 struct VectorTraits<std::pair<First, Second>> { | |
| 107 typedef VectorTraits<First> FirstTraits; | |
| 108 typedef VectorTraits<Second> SecondTraits; | |
| 109 | |
| 110 static const bool needsDestruction = | |
| 111 FirstTraits::needsDestruction || SecondTraits::needsDestruction; | |
| 112 static const bool canInitializeWithMemset = | |
| 113 FirstTraits::canInitializeWithMemset && | |
| 114 SecondTraits::canInitializeWithMemset; | |
| 115 static const bool canMoveWithMemcpy = | |
| 116 FirstTraits::canMoveWithMemcpy && SecondTraits::canMoveWithMemcpy; | |
| 117 static const bool canCopyWithMemcpy = | |
| 118 FirstTraits::canCopyWithMemcpy && SecondTraits::canCopyWithMemcpy; | |
| 119 static const bool canFillWithMemset = false; | |
| 120 static const bool canCompareWithMemcmp = | |
| 121 FirstTraits::canCompareWithMemcmp && SecondTraits::canCompareWithMemcmp; | |
| 122 static const bool canClearUnusedSlotsWithMemset = | |
| 123 FirstTraits::canClearUnusedSlotsWithMemset && | |
| 124 SecondTraits::canClearUnusedSlotsWithMemset; | |
| 125 // Supports swapping elements using regular std::swap semantics. | |
| 126 static const bool canSwapUsingCopyOrMove = true; | |
| 127 template <typename U = void> | |
| 128 struct IsTraceableInCollection { | |
| 129 static const bool value = | |
| 130 IsTraceableInCollectionTrait<FirstTraits>::value || | |
| 131 IsTraceableInCollectionTrait<SecondTraits>::value; | |
| 132 }; | |
| 133 // We don't support weak handling in vectors. | |
| 134 static const WeakHandlingFlag weakHandlingFlag = NoWeakHandlingInCollections; | |
| 135 }; | |
| 136 | |
| 137 } // namespace WTF | |
| 138 | |
| 139 #define WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(ClassName) \ | |
| 140 namespace WTF { \ | |
| 141 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || \ | |
| 142 !IsTriviallyMoveAssignable<ClassName>::value || \ | |
| 143 !std::is_scalar<ClassName>::value, \ | |
| 144 "macro not needed"); \ | |
| 145 template <> \ | |
| 146 struct VectorTraits<ClassName> : SimpleClassVectorTraits<ClassName> {}; \ | |
| 147 } | |
| 148 | |
| 149 #define WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(ClassName) \ | |
| 150 namespace WTF { \ | |
| 151 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value || \ | |
| 152 !IsTriviallyMoveAssignable<ClassName>::value, \ | |
| 153 "macro not needed"); \ | |
| 154 template <> \ | |
| 155 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> { \ | |
| 156 static const bool canInitializeWithMemset = true; \ | |
| 157 static const bool canClearUnusedSlotsWithMemset = true; \ | |
| 158 static const bool canMoveWithMemcpy = true; \ | |
| 159 }; \ | |
| 160 } | |
| 161 | |
| 162 #define WTF_ALLOW_INIT_WITH_MEM_FUNCTIONS(ClassName) \ | |
| 163 namespace WTF { \ | |
| 164 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, \ | |
| 165 "macro not needed"); \ | |
| 166 template <> \ | |
| 167 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> { \ | |
| 168 static const bool canInitializeWithMemset = true; \ | |
| 169 static const bool canClearUnusedSlotsWithMemset = true; \ | |
| 170 }; \ | |
| 171 } | |
| 172 | |
| 173 #define WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(ClassName) \ | |
| 174 namespace WTF { \ | |
| 175 static_assert(!IsTriviallyDefaultConstructible<ClassName>::value, \ | |
| 176 "macro not needed"); \ | |
| 177 template <> \ | |
| 178 struct VectorTraits<ClassName> : VectorTraitsBase<ClassName> { \ | |
| 179 static const bool canClearUnusedSlotsWithMemset = true; \ | |
| 180 }; \ | |
| 181 } | |
| 182 | |
| 183 using WTF::VectorTraits; | |
| 184 using WTF::SimpleClassVectorTraits; | |
| 185 | |
| 186 #endif // WTF_VectorTraits_h | |
| OLD | NEW |