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

Side by Side Diff: cc/base/contiguous_container.h

Issue 2932053002: Use C++11 alignment primitives (Closed)
Patch Set: Fixes Created 3 years, 6 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CC_BASE_CONTIGUOUS_CONTAINER_H_ 5 #ifndef CC_BASE_CONTIGUOUS_CONTAINER_H_
6 #define CC_BASE_CONTIGUOUS_CONTAINER_H_ 6 #define CC_BASE_CONTIGUOUS_CONTAINER_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 const BaseElementType& first() const { return *begin(); } 156 const BaseElementType& first() const { return *begin(); }
157 BaseElementType& last() { return *rbegin(); } 157 BaseElementType& last() { return *rbegin(); }
158 const BaseElementType& last() const { return *rbegin(); } 158 const BaseElementType& last() const { return *rbegin(); }
159 BaseElementType& operator[](size_t index) { return *(begin() + index); } 159 BaseElementType& operator[](size_t index) { return *(begin() + index); }
160 const BaseElementType& operator[](size_t index) const { 160 const BaseElementType& operator[](size_t index) const {
161 return *(begin() + index); 161 return *(begin() + index);
162 } 162 }
163 163
164 template <class DerivedElementType, typename... Args> 164 template <class DerivedElementType, typename... Args>
165 DerivedElementType& AllocateAndConstruct(Args&&... args) { 165 DerivedElementType& AllocateAndConstruct(Args&&... args) {
166 static_assert(alignment % ALIGNOF(DerivedElementType) == 0, 166 static_assert(alignment % alignof(DerivedElementType) == 0,
167 "Derived type requires stronger alignment."); 167 "Derived type requires stronger alignment.");
168 return *new (AlignedAllocate(sizeof(DerivedElementType))) 168 return *new (AlignedAllocate(sizeof(DerivedElementType)))
169 DerivedElementType(std::forward<Args>(args)...); 169 DerivedElementType(std::forward<Args>(args)...);
170 } 170 }
171 171
172 private: 172 private:
173 void* AlignedAllocate(size_t size) { 173 void* AlignedAllocate(size_t size) {
174 void* result = ContiguousContainerBase::Allocate(Align(size)); 174 void* result = ContiguousContainerBase::Allocate(Align(size));
175 DCHECK_EQ(reinterpret_cast<intptr_t>(result) & (alignment - 1), 0u); 175 DCHECK_EQ(reinterpret_cast<intptr_t>(result) & (alignment - 1), 0u);
176 return result; 176 return result;
177 } 177 }
178 178
179 size_t Align(size_t size) { 179 size_t Align(size_t size) {
180 size_t aligned_size = alignment * ((size + alignment - 1) / alignment); 180 size_t aligned_size = alignment * ((size + alignment - 1) / alignment);
181 DCHECK_EQ(aligned_size % alignment, 0u); 181 DCHECK_EQ(aligned_size % alignment, 0u);
182 DCHECK_GE(aligned_size, size); 182 DCHECK_GE(aligned_size, size);
183 DCHECK_LT(aligned_size, size + alignment); 183 DCHECK_LT(aligned_size, size + alignment);
184 return aligned_size; 184 return aligned_size;
185 } 185 }
186 }; 186 };
187 187
188 } // namespace cc 188 } // namespace cc
189 189
190 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_ 190 #endif // CC_BASE_CONTIGUOUS_CONTAINER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698