Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: Source/wtf/Vector.h

Issue 178313002: Vector::insert and Vector::append may use memcpy for POD types (Closed) Base URL: https://chromium.googlesource.com/chromium/blink@master
Patch Set: Corrections after review Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 memmove(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpre t_cast<const char*>(src)); 155 memmove(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpre t_cast<const char*>(src));
156 } 156 }
157 }; 157 };
158 158
159 template <bool canCopyWithMemcpy, typename T> 159 template <bool canCopyWithMemcpy, typename T>
160 struct VectorCopier; 160 struct VectorCopier;
161 161
162 template<typename T> 162 template<typename T>
163 struct VectorCopier<false, T> 163 struct VectorCopier<false, T>
164 { 164 {
165 static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) 165 template<typename U>
166 static void uninitializedCopy(const U* src, const U* srcEnd, T* dst)
166 { 167 {
167 while (src != srcEnd) { 168 while (src != srcEnd) {
168 new (NotNull, dst) T(*src); 169 new (NotNull, dst) T(*src);
169 ++dst; 170 ++dst;
170 ++src; 171 ++src;
171 } 172 }
172 } 173 }
173 }; 174 };
174 175
175 template<typename T> 176 template<typename T>
176 struct VectorCopier<true, T> 177 struct VectorCopier<true, T>
177 { 178 {
178 static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) 179 static void uninitializedCopy(const T* src, const T* srcEnd, T* dst)
179 { 180 {
180 memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret _cast<const char*>(src)); 181 memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret _cast<const char*>(src));
181 } 182 }
183 template<typename U>
184 static void uninitializedCopy(const U* src, const U* srcEnd, T* dst)
185 {
186 VectorCopier<false, T>::uninitializedCopy(src, srcEnd, dst);
187 }
182 }; 188 };
183 189
184 template <bool canFillWithMemset, typename T> 190 template <bool canFillWithMemset, typename T>
185 struct VectorFiller; 191 struct VectorFiller;
186 192
187 template<typename T> 193 template<typename T>
188 struct VectorFiller<false, T> 194 struct VectorFiller<false, T>
189 { 195 {
190 static void uninitializedFill(T* dst, T* dstEnd, const T& val) 196 static void uninitializedFill(T* dst, T* dstEnd, const T& val)
191 { 197 {
(...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U> 962 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U>
957 void Vector<T, inlineCapacity, Allocator>::append(const U* data, size_t data Size) 963 void Vector<T, inlineCapacity, Allocator>::append(const U* data, size_t data Size)
958 { 964 {
959 size_t newSize = m_size + dataSize; 965 size_t newSize = m_size + dataSize;
960 if (newSize > capacity()) { 966 if (newSize > capacity()) {
961 data = expandCapacity(newSize, data); 967 data = expandCapacity(newSize, data);
962 ASSERT(begin()); 968 ASSERT(begin());
963 } 969 }
964 RELEASE_ASSERT(newSize >= m_size); 970 RELEASE_ASSERT(newSize >= m_size);
965 T* dest = end(); 971 T* dest = end();
966 for (size_t i = 0; i < dataSize; ++i) 972 VectorCopier<VectorTraits<T>::canCopyWithMemcpy, T>::uninitializedCopy(d ata, &data[dataSize], dest);
967 new (NotNull, &dest[i]) T(data[i]);
968 m_size = newSize; 973 m_size = newSize;
969 } 974 }
970 975
971 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U> 976 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U>
972 ALWAYS_INLINE void Vector<T, inlineCapacity, Allocator>::append(const U& val ) 977 ALWAYS_INLINE void Vector<T, inlineCapacity, Allocator>::append(const U& val )
973 { 978 {
974 if (LIKELY(size() != capacity())) { 979 if (LIKELY(size() != capacity())) {
975 new (NotNull, end()) T(val); 980 new (NotNull, end()) T(val);
976 ++m_size; 981 ++m_size;
977 return; 982 return;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 { 1021 {
1017 RELEASE_ASSERT(position <= size()); 1022 RELEASE_ASSERT(position <= size());
1018 size_t newSize = m_size + dataSize; 1023 size_t newSize = m_size + dataSize;
1019 if (newSize > capacity()) { 1024 if (newSize > capacity()) {
1020 data = expandCapacity(newSize, data); 1025 data = expandCapacity(newSize, data);
1021 ASSERT(begin()); 1026 ASSERT(begin());
1022 } 1027 }
1023 RELEASE_ASSERT(newSize >= m_size); 1028 RELEASE_ASSERT(newSize >= m_size);
1024 T* spot = begin() + position; 1029 T* spot = begin() + position;
1025 TypeOperations::moveOverlapping(spot, end(), spot + dataSize); 1030 TypeOperations::moveOverlapping(spot, end(), spot + dataSize);
1026 for (size_t i = 0; i < dataSize; ++i) 1031 VectorCopier<VectorTraits<T>::canCopyWithMemcpy, T>::uninitializedCopy(d ata, &data[dataSize], spot);
1027 new (NotNull, &spot[i]) T(data[i]);
1028 m_size = newSize; 1032 m_size = newSize;
1029 } 1033 }
1030 1034
1031 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U> 1035 template<typename T, size_t inlineCapacity, typename Allocator> template<typ ename U>
1032 inline void Vector<T, inlineCapacity, Allocator>::insert(size_t position, co nst U& val) 1036 inline void Vector<T, inlineCapacity, Allocator>::insert(size_t position, co nst U& val)
1033 { 1037 {
1034 RELEASE_ASSERT(position <= size()); 1038 RELEASE_ASSERT(position <= size());
1035 const U* data = &val; 1039 const U* data = &val;
1036 if (size() == capacity()) { 1040 if (size() == capacity()) {
1037 data = expandCapacity(size() + 1, data); 1041 data = expandCapacity(size() + 1, data);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1142 } 1146 }
1143 if (this->hasOutOfLineBuffer()) 1147 if (this->hasOutOfLineBuffer())
1144 Allocator::markNoTracing(visitor, buffer()); 1148 Allocator::markNoTracing(visitor, buffer());
1145 } 1149 }
1146 1150
1147 } // namespace WTF 1151 } // namespace WTF
1148 1152
1149 using WTF::Vector; 1153 using WTF::Vector;
1150 1154
1151 #endif // WTF_Vector_h 1155 #endif // WTF_Vector_h
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698