Title: [154565] trunk/Source/WTF
Revision
154565
Author
[email protected]
Date
2013-08-24 21:39:20 -0700 (Sat, 24 Aug 2013)

Log Message

Save three bytes per CStringBuffer object
https://bugs.webkit.org/show_bug.cgi?id=120040

Reviewed by Darin Adler.

Merge https://chromium.googlesource.com/chromium/blink/+/894ae8eafdb64912aefd8f9c809f4ccda84f3b89

sizeof(CStringBuffer) was rounded up to 8 on account of struct size and
alignment rules. This is clearly not what was intended.

* wtf/text/CString.cpp:
(WTF::CStringBuffer::createUninitialized):
* wtf/text/CString.h:
(WTF::CStringBuffer::data):
(WTF::CStringBuffer::mutableData):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (154564 => 154565)


--- trunk/Source/WTF/ChangeLog	2013-08-25 04:33:16 UTC (rev 154564)
+++ trunk/Source/WTF/ChangeLog	2013-08-25 04:39:20 UTC (rev 154565)
@@ -1,3 +1,21 @@
+2013-08-24  Benjamin Poulain  <[email protected]>
+
+        Save three bytes per CStringBuffer object
+        https://bugs.webkit.org/show_bug.cgi?id=120040
+
+        Reviewed by Darin Adler.
+
+        Merge https://chromium.googlesource.com/chromium/blink/+/894ae8eafdb64912aefd8f9c809f4ccda84f3b89
+
+        sizeof(CStringBuffer) was rounded up to 8 on account of struct size and
+        alignment rules. This is clearly not what was intended.
+
+        * wtf/text/CString.cpp:
+        (WTF::CStringBuffer::createUninitialized):
+        * wtf/text/CString.h:
+        (WTF::CStringBuffer::data):
+        (WTF::CStringBuffer::mutableData):
+
 2013-08-24  Darin Adler  <[email protected]>
 
         RetainPtr lacks move constructor for case when argument is a RetainPtr of a different type

Modified: trunk/Source/WTF/wtf/text/CString.cpp (154564 => 154565)


--- trunk/Source/WTF/wtf/text/CString.cpp	2013-08-25 04:33:16 UTC (rev 154564)
+++ trunk/Source/WTF/wtf/text/CString.cpp	2013-08-25 04:39:20 UTC (rev 154565)
@@ -35,12 +35,10 @@
 
 PassRefPtr<CStringBuffer> CStringBuffer::createUninitialized(size_t length)
 {
-    if (length > (numeric_limits<size_t>::max() - sizeof(CStringBuffer)))
-        CRASH();
+    RELEASE_ASSERT(length < (numeric_limits<unsigned>::max() - sizeof(CStringBuffer)));
 
-    // CStringBuffer already has space for one character, we do not need to add +1 to the length
-    // to store the terminating zero.
-    size_t size = sizeof(CStringBuffer) + length;
+    // The +1 is for the terminating null character.
+    size_t size = sizeof(CStringBuffer) + length + 1;
     CStringBuffer* stringBuffer = static_cast<CStringBuffer*>(fastMalloc(size));
     return adoptRef(new (NotNull, stringBuffer) CStringBuffer(length));
 }

Modified: trunk/Source/WTF/wtf/text/CString.h (154564 => 154565)


--- trunk/Source/WTF/wtf/text/CString.h	2013-08-25 04:33:16 UTC (rev 154564)
+++ trunk/Source/WTF/wtf/text/CString.h	2013-08-25 04:39:20 UTC (rev 154565)
@@ -30,7 +30,6 @@
 #include <wtf/HashTraits.h>
 #include <wtf/PassRefPtr.h>
 #include <wtf/RefCounted.h>
-#include <wtf/Vector.h>
 
 namespace WTF {
 
@@ -38,7 +37,7 @@
 // The data is implicitly allocated 1 character longer than length(), as it is zero-terminated.
 class CStringBuffer : public RefCounted<CStringBuffer> {
 public:
-    const char* data() { return m_data; }
+    const char* data() { return mutableData(); }
     size_t length() const { return m_length; }
 
 private:
@@ -47,10 +46,9 @@
     static PassRefPtr<CStringBuffer> createUninitialized(size_t length);
 
     CStringBuffer(size_t length) : m_length(length) { }
-    char* mutableData() { return m_data; }
+    char* mutableData() { return reinterpret_cast_ptr<char*>(this + 1); }
 
     const size_t m_length;
-    char m_data[1];
 };
 
 // A container for a null-terminated char array supporting copy-on-write
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to