ShareableElementData should use zero-length array for storage.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Feb 2013 21:30:17 +0000 (21:30 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 15 Feb 2013 21:30:17 +0000 (21:30 +0000)
<http://webkit.org/b/109959>

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
Source/WebCore/dom/DocumentSharedObjectPool.cpp
Source/WebCore/dom/Element.cpp
Source/WebCore/dom/Element.h

index b16449b736743b6043e71bd0c2ae867665209fbc..f129a1bbec46776d899aeaa08906e5976ee11d80 100644 (file)
@@ -1,3 +1,25 @@
+2013-02-15  Andreas Kling  <akling@apple.com>
+
+        ShareableElementData should use zero-length array for storage.
+        <http://webkit.org/b/109959>
+
+        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  <ojan@chromium.org>
 
         Implement RenderGrid::computeIntrinsicLogicalWidths
index 0301000e90e8c82c0370a962ee17b376a8e770d7..997e8668d15b505c51386d41ad2bc230d7bd2b07 100644 (file)
@@ -86,7 +86,7 @@ PassRefPtr<ShareableElementData> 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();
 }
index a589750e282e871e3013e0b07f17cb811f82e0c1..025aba111f04628651652a8e3f6eb74d3a64a887 100644 (file)
@@ -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> ShareableElementData::createWithAttributes(const Vector<Attribute>& attributes)
@@ -2881,13 +2881,13 @@ ShareableElementData::ShareableElementData(const Vector<Attribute>& attributes)
     : ElementData(attributes.size())
 {
     for (unsigned i = 0; i < m_arraySize; ++i)
-        new (&reinterpret_cast<Attribute*>(&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<Attribute*>(&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<Attribute*>(&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<UniqueElementData> ElementData::makeUniqueCopy() const
index f1d6bfe3cb8a4d96df13c4b400b08e6f8cedfae3..df0125c9f0cbb1737b3f91205ea55d5103dfa070 100644 (file)
@@ -119,19 +119,26 @@ private:
     PassRefPtr<UniqueElementData> 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<ShareableElementData> createWithAttributes(const Vector<Attribute>&);
 
-    const Attribute* immutableAttributeArray() const { return reinterpret_cast<const Attribute*>(&m_attributeArray); }
-
     explicit ShareableElementData(const Vector<Attribute>&);
     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<UniqueElementData> 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<const UniqueElementData*>(this)->m_attributeVector.at(index);
-    return &static_cast<const ShareableElementData*>(this)->immutableAttributeArray()[index];
+    return &static_cast<const ShareableElementData*>(this)->m_attributeArray[index];
 }
 
 } // namespace