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

Unified Diff: cc/base/contiguous_container.h

Issue 2122573002: Correct alignment in ContiguousContainer::appendByMoving() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 0u Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | cc/base/contiguous_container_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/base/contiguous_container.h
diff --git a/cc/base/contiguous_container.h b/cc/base/contiguous_container.h
index d032aa7f8c3bda9c36eb377c2308c02e8059b8be..2d877a16adc60e96a78d8fac937dd3e48333da9e 100644
--- a/cc/base/contiguous_container.h
+++ b/cc/base/contiguous_container.h
@@ -170,8 +170,7 @@ class ContiguousContainer : public ContiguousContainerBase {
DerivedElementType& AllocateAndConstruct(Args&&... args) {
static_assert(alignment % ALIGNOF(DerivedElementType) == 0,
"Derived type requires stronger alignment.");
- size_t alloc_size = Align(sizeof(DerivedElementType));
- return *new (Allocate(alloc_size))
+ return *new (AlignedAllocate(sizeof(DerivedElementType)))
DerivedElementType(std::forward<Args>(args)...);
}
@@ -198,14 +197,20 @@ class ContiguousContainer : public ContiguousContainerBase {
// element in its place. Use with care.
BaseElementType& AppendByMoving(BaseElementType* item, size_t size) {
DCHECK_GE(size, sizeof(BaseElementType));
- void* new_item = Allocate(size);
+ void* new_item = AlignedAllocate(size);
memcpy(new_item, static_cast<void*>(item), size);
new (item) BaseElementType;
return *static_cast<BaseElementType*>(new_item);
}
private:
- static size_t Align(size_t size) {
+ void* AlignedAllocate(size_t size) {
+ void* result = ContiguousContainerBase::Allocate(Align(size));
+ DCHECK_EQ(reinterpret_cast<intptr_t>(result) & (alignment - 1), 0u);
+ return result;
+ }
+
+ size_t Align(size_t size) {
size_t aligned_size = alignment * ((size + alignment - 1) / alignment);
DCHECK_EQ(aligned_size % alignment, 0u);
DCHECK_GE(aligned_size, size);
« no previous file with comments | « no previous file | cc/base/contiguous_container_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698