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

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

Issue 2932053002: Use C++11 alignment primitives (Closed)
Patch Set: Fix merge 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
« no previous file with comments | « base/process/launch_posix.cc ('k') | cc/base/list_container_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_LIST_CONTAINER_H_ 5 #ifndef CC_BASE_LIST_CONTAINER_H_
6 #define CC_BASE_LIST_CONTAINER_H_ 6 #define CC_BASE_LIST_CONTAINER_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 return *Iterator(helper_.IteratorAt(index)); 94 return *Iterator(helper_.IteratorAt(index));
95 } 95 }
96 const BaseElementType* ElementAt(size_t index) const { 96 const BaseElementType* ElementAt(size_t index) const {
97 return *ConstIterator(helper_.IteratorAt(index)); 97 return *ConstIterator(helper_.IteratorAt(index));
98 } 98 }
99 99
100 // Take in derived element type and construct it at location generated by 100 // Take in derived element type and construct it at location generated by
101 // Allocate(). 101 // Allocate().
102 template <typename DerivedElementType> 102 template <typename DerivedElementType>
103 DerivedElementType* AllocateAndConstruct() { 103 DerivedElementType* AllocateAndConstruct() {
104 return new (helper_.Allocate(ALIGNOF(DerivedElementType), 104 return new (helper_.Allocate(alignof(DerivedElementType),
105 sizeof(DerivedElementType))) 105 sizeof(DerivedElementType)))
106 DerivedElementType; 106 DerivedElementType;
107 } 107 }
108 108
109 // Take in derived element type and copy construct it at location generated by 109 // Take in derived element type and copy construct it at location generated by
110 // Allocate(). 110 // Allocate().
111 template <typename DerivedElementType> 111 template <typename DerivedElementType>
112 DerivedElementType* AllocateAndCopyFrom(const DerivedElementType* source) { 112 DerivedElementType* AllocateAndCopyFrom(const DerivedElementType* source) {
113 return new (helper_.Allocate(ALIGNOF(DerivedElementType), 113 return new (helper_.Allocate(alignof(DerivedElementType),
114 sizeof(DerivedElementType))) 114 sizeof(DerivedElementType)))
115 DerivedElementType(*source); 115 DerivedElementType(*source);
116 } 116 }
117 117
118 // Construct a new element on top of an existing one. 118 // Construct a new element on top of an existing one.
119 template <typename DerivedElementType> 119 template <typename DerivedElementType>
120 DerivedElementType* ReplaceExistingElement(Iterator at) { 120 DerivedElementType* ReplaceExistingElement(Iterator at) {
121 at->~BaseElementType(); 121 at->~BaseElementType();
122 return new (at.item_iterator) DerivedElementType(); 122 return new (at.item_iterator) DerivedElementType();
123 } 123 }
(...skipping 23 matching lines...) Expand all
147 } 147 }
148 148
149 // Appends a new item without copying. The original item will not be 149 // Appends a new item without copying. The original item will not be
150 // destructed and will be replaced with a new DerivedElementType. The 150 // destructed and will be replaced with a new DerivedElementType. The
151 // DerivedElementType does not have to match the moved type as a full block 151 // DerivedElementType does not have to match the moved type as a full block
152 // of memory will be moved (up to MaxSizeForDerivedClass()). A pointer to 152 // of memory will be moved (up to MaxSizeForDerivedClass()). A pointer to
153 // the moved element is returned. 153 // the moved element is returned.
154 template <typename DerivedElementType> 154 template <typename DerivedElementType>
155 DerivedElementType* AppendByMoving(DerivedElementType* item) { 155 DerivedElementType* AppendByMoving(DerivedElementType* item) {
156 size_t max_size_for_derived_class = helper_.MaxSizeForDerivedClass(); 156 size_t max_size_for_derived_class = helper_.MaxSizeForDerivedClass();
157 void* new_item = helper_.Allocate(ALIGNOF(DerivedElementType), 157 void* new_item = helper_.Allocate(alignof(DerivedElementType),
158 max_size_for_derived_class); 158 max_size_for_derived_class);
159 memcpy(new_item, static_cast<void*>(item), max_size_for_derived_class); 159 memcpy(new_item, static_cast<void*>(item), max_size_for_derived_class);
160 // Construct a new element in-place so it can be destructed safely. 160 // Construct a new element in-place so it can be destructed safely.
161 new (item) DerivedElementType; 161 new (item) DerivedElementType;
162 return static_cast<DerivedElementType*>(new_item); 162 return static_cast<DerivedElementType*>(new_item);
163 } 163 }
164 164
165 size_t size() const { return helper_.size(); } 165 size_t size() const { return helper_.size(); }
166 bool empty() const { return helper_.empty(); } 166 bool empty() const { return helper_.empty(); }
167 size_t GetCapacityInBytes() const { return helper_.GetCapacityInBytes(); } 167 size_t GetCapacityInBytes() const { return helper_.GetCapacityInBytes(); }
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 336
337 private: 337 private:
338 ListContainerHelper helper_; 338 ListContainerHelper helper_;
339 339
340 DISALLOW_COPY_AND_ASSIGN(ListContainer); 340 DISALLOW_COPY_AND_ASSIGN(ListContainer);
341 }; 341 };
342 342
343 } // namespace cc 343 } // namespace cc
344 344
345 #endif // CC_BASE_LIST_CONTAINER_H_ 345 #endif // CC_BASE_LIST_CONTAINER_H_
OLDNEW
« no previous file with comments | « base/process/launch_posix.cc ('k') | cc/base/list_container_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698