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

Unified Diff: third_party/WebKit/Source/platform/graphics/ContiguousContainer.h

Issue 2119033003: Fix alignment issue of ContiguousContainer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/graphics/ContiguousContainer.h
diff --git a/third_party/WebKit/Source/platform/graphics/ContiguousContainer.h b/third_party/WebKit/Source/platform/graphics/ContiguousContainer.h
index a50431becefb31c3ab4a231c74212f66ea67bf33..fab626541f9f828934650f9993d35ca6b41ee64e 100644
--- a/third_party/WebKit/Source/platform/graphics/ContiguousContainer.h
+++ b/third_party/WebKit/Source/platform/graphics/ContiguousContainer.h
@@ -55,7 +55,7 @@ protected:
// These do not invoke constructors or destructors.
void reserveInitialCapacity(size_t, const char* typeName);
- void* allocate(size_t objectSize, const char* typeName);
+ void* allocate(size_t objectSize, size_t alignment, const char* typeName);
void removeLast();
void clear();
void swap(ContiguousContainerBase&);
@@ -72,13 +72,7 @@ private:
size_t m_maxObjectSize;
};
-// For most cases, no alignment stricter than pointer alignment is required. If
-// one of the derived classes has stronger alignment requirements (and the
-// static_assert fires), set alignment to the LCM of the derived class
-// alignments. For small structs without pointers, it may be possible to reduce
-// alignment for tighter packing.
-
-template <class BaseElementType, unsigned alignment = sizeof(void*)>
+template <class BaseElementType>
class ContiguousContainer : public ContiguousContainerBase {
private:
// Declares itself as a forward iterator, but also supports a few more
@@ -108,7 +102,7 @@ public:
using reverse_iterator = IteratorWrapper<Vector<void*>::reverse_iterator, BaseElementType>;
using const_reverse_iterator = IteratorWrapper<Vector<void*>::const_reverse_iterator, const BaseElementType>;
- explicit ContiguousContainer(size_t maxObjectSize) : ContiguousContainerBase(align(maxObjectSize)) {}
+ explicit ContiguousContainer(size_t maxObjectSize) : ContiguousContainerBase(maxObjectSize) {}
ContiguousContainer(size_t maxObjectSize, size_t initialSizeBytes)
: ContiguousContainer(maxObjectSize)
@@ -164,10 +158,8 @@ public:
{
static_assert(WTF::IsSubclass<DerivedElementType, BaseElementType>::value,
"Must use subclass of BaseElementType.");
- static_assert(alignment % WTF_ALIGN_OF(DerivedElementType) == 0,
- "Derived type requires stronger alignment.");
- size_t allocSize = align(sizeof(DerivedElementType));
- return *new (allocate(allocSize)) DerivedElementType(std::forward<Args>(args)...);
+ return *new (allocate(sizeof(DerivedElementType), WTF_ALIGN_OF(DerivedElementType)))
+ DerivedElementType(std::forward<Args>(args)...);
}
void removeLast()
@@ -190,28 +182,21 @@ public:
// Appends a new element using memcpy, then default-constructs a base
// element in its place. Use with care.
- BaseElementType& appendByMoving(BaseElementType& item, size_t size)
+ BaseElementType& appendByMoving(BaseElementType& item, size_t size, size_t alignment)
{
ASSERT(size >= sizeof(BaseElementType));
- void* newItem = allocate(size);
+ void* newItem = allocate(size, alignment);
memcpy(newItem, static_cast<void*>(&item), size);
new (&item) BaseElementType;
return *static_cast<BaseElementType*>(newItem);
}
private:
- void* allocate(size_t objectSize)
- {
- return ContiguousContainerBase::allocate(objectSize, WTF_HEAP_PROFILER_TYPE_NAME(BaseElementType));
- }
+ FRIEND_TEST_ALL_PREFIXES(ContiguousContainerTest, Alignment);
- static size_t align(size_t size)
+ void* allocate(size_t objectSize, size_t alignment)
{
- size_t alignedSize = alignment * ((size + alignment - 1) / alignment);
- ASSERT(alignedSize % alignment == 0);
- ASSERT(alignedSize >= size);
- ASSERT(alignedSize < size + alignment);
- return alignedSize;
+ return ContiguousContainerBase::allocate(objectSize, alignment, WTF_HEAP_PROFILER_TYPE_NAME(BaseElementType));
}
};

Powered by Google App Engine
This is Rietveld 408576698