From 6c34f77f85ac22d598b72a6b8ba5f0753e83eaba Mon Sep 17 00:00:00 2001 From: "akling@apple.com" Date: Fri, 15 Feb 2013 21:30:17 +0000 Subject: [PATCH] ShareableElementData should use zero-length array for storage. Reviewed by Anders Carlsson. Use a zero-length Attribute array instead of always casting from void* to an array. It was done this way originally because I didn't know we could sidestep the MSVC build error with some #pragma hackery. * dom/DocumentSharedObjectPool.cpp: (WebCore::DocumentSharedObjectPool::cachedShareableElementDataWithAttributes): * dom/Element.cpp: (WebCore::sizeForShareableElementDataWithAttributeCount): (WebCore::ShareableElementData::ShareableElementData): (WebCore::ShareableElementData::~ShareableElementData): (WebCore::UniqueElementData::UniqueElementData): * dom/Element.h: (ShareableElementData): (WebCore::ElementData::attributeItem): git-svn-id: https://svn.webkit.org/repository/webkit/trunk@143044 268f45cc-cd09-0410-ab3c-d52691b4dbfc --- Source/WebCore/ChangeLog | 22 +++++++++++++++++++ .../WebCore/dom/DocumentSharedObjectPool.cpp | 2 +- Source/WebCore/dom/Element.cpp | 10 ++++----- Source/WebCore/dom/Element.h | 15 +++++++++---- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog index b16449b73674..f129a1bbec46 100644 --- a/Source/WebCore/ChangeLog +++ b/Source/WebCore/ChangeLog @@ -1,3 +1,25 @@ +2013-02-15 Andreas Kling + + ShareableElementData should use zero-length array for storage. + + + Reviewed by Anders Carlsson. + + Use a zero-length Attribute array instead of always casting from void* to an array. + It was done this way originally because I didn't know we could sidestep the MSVC + build error with some #pragma hackery. + + * dom/DocumentSharedObjectPool.cpp: + (WebCore::DocumentSharedObjectPool::cachedShareableElementDataWithAttributes): + * dom/Element.cpp: + (WebCore::sizeForShareableElementDataWithAttributeCount): + (WebCore::ShareableElementData::ShareableElementData): + (WebCore::ShareableElementData::~ShareableElementData): + (WebCore::UniqueElementData::UniqueElementData): + * dom/Element.h: + (ShareableElementData): + (WebCore::ElementData::attributeItem): + 2013-02-14 Ojan Vafai Implement RenderGrid::computeIntrinsicLogicalWidths diff --git a/Source/WebCore/dom/DocumentSharedObjectPool.cpp b/Source/WebCore/dom/DocumentSharedObjectPool.cpp index 0301000e90e8..997e8668d15b 100644 --- a/Source/WebCore/dom/DocumentSharedObjectPool.cpp +++ b/Source/WebCore/dom/DocumentSharedObjectPool.cpp @@ -86,7 +86,7 @@ PassRefPtr DocumentSharedObjectPool::cachedShareableElemen if (!cacheHash || cacheIterator->value) return elementData.release(); - cacheIterator->value = adoptPtr(new ShareableElementDataCacheEntry(ShareableElementDataCacheKey(elementData->immutableAttributeArray(), elementData->length()), elementData)); + cacheIterator->value = adoptPtr(new ShareableElementDataCacheEntry(ShareableElementDataCacheKey(elementData->m_attributeArray, elementData->length()), elementData)); return elementData.release(); } diff --git a/Source/WebCore/dom/Element.cpp b/Source/WebCore/dom/Element.cpp index a589750e282e..025aba111f04 100644 --- a/Source/WebCore/dom/Element.cpp +++ b/Source/WebCore/dom/Element.cpp @@ -2863,7 +2863,7 @@ COMPILE_ASSERT(sizeof(ElementData) == sizeof(SameSizeAsElementData), element_att static size_t sizeForShareableElementDataWithAttributeCount(unsigned count) { - return sizeof(ShareableElementData) - sizeof(void*) + sizeof(Attribute) * count; + return sizeof(ShareableElementData) + sizeof(Attribute) * count; } PassRefPtr ShareableElementData::createWithAttributes(const Vector& attributes) @@ -2881,13 +2881,13 @@ ShareableElementData::ShareableElementData(const Vector& attributes) : ElementData(attributes.size()) { for (unsigned i = 0; i < m_arraySize; ++i) - new (&reinterpret_cast(&m_attributeArray)[i]) Attribute(attributes[i]); + new (&m_attributeArray[i]) Attribute(attributes[i]); } ShareableElementData::~ShareableElementData() { for (unsigned i = 0; i < m_arraySize; ++i) - (reinterpret_cast(&m_attributeArray)[i]).~Attribute(); + m_attributeArray[i].~Attribute(); } ShareableElementData::ShareableElementData(const UniqueElementData& other) @@ -2901,7 +2901,7 @@ ShareableElementData::ShareableElementData(const UniqueElementData& other) } for (unsigned i = 0; i < m_arraySize; ++i) - new (&reinterpret_cast(&m_attributeArray)[i]) Attribute(other.m_attributeVector.at(i)); + new (&m_attributeArray[i]) Attribute(other.m_attributeVector.at(i)); } ElementData::ElementData(const ElementData& other, bool isUnique) @@ -2939,7 +2939,7 @@ UniqueElementData::UniqueElementData(const ShareableElementData& other) m_attributeVector.reserveCapacity(other.length()); for (unsigned i = 0; i < other.length(); ++i) - m_attributeVector.uncheckedAppend(other.immutableAttributeArray()[i]); + m_attributeVector.uncheckedAppend(other.m_attributeArray[i]); } PassRefPtr ElementData::makeUniqueCopy() const diff --git a/Source/WebCore/dom/Element.h b/Source/WebCore/dom/Element.h index f1d6bfe3cb8a..df0125c9f0cb 100644 --- a/Source/WebCore/dom/Element.h +++ b/Source/WebCore/dom/Element.h @@ -119,19 +119,26 @@ private: PassRefPtr makeUniqueCopy() const; }; +#if COMPILER(MSVC) +#pragma warning(push) +#pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning +#endif + class ShareableElementData : public ElementData { public: static PassRefPtr createWithAttributes(const Vector&); - const Attribute* immutableAttributeArray() const { return reinterpret_cast(&m_attributeArray); } - explicit ShareableElementData(const Vector&); explicit ShareableElementData(const UniqueElementData&); ~ShareableElementData(); - void* m_attributeArray; + Attribute m_attributeArray[0]; }; +#if COMPILER(MSVC) +#pragma warning(pop) +#endif + class UniqueElementData : public ElementData { public: static PassRefPtr create(); @@ -1034,7 +1041,7 @@ inline const Attribute* ElementData::attributeItem(unsigned index) const ASSERT_WITH_SECURITY_IMPLICATION(index < length()); if (m_isUnique) return &static_cast(this)->m_attributeVector.at(index); - return &static_cast(this)->immutableAttributeArray()[index]; + return &static_cast(this)->m_attributeArray[index]; } } // namespace -- 2.36.0