Title: [152415] trunk/Source/WTF
Revision
152415
Author
mikhail.pozdnya...@intel.com
Date
2013-07-05 04:23:44 -0700 (Fri, 05 Jul 2013)

Log Message

Remove code duplication from StringImpl create()/reallocate() methods
https://bugs.webkit.org/show_bug.cgi?id=118355

Reviewed by Andreas Kling.

StringImpl create()/reallocate() methods accepting LChar and UChar used to have
duplicated code. The code duplication is removed now via used templates.

* wtf/text/StringImpl.cpp:
(WTF::StringImpl::constructInternal):
(WTF::LChar):
(WTF::StringImpl::createUninitializedInternal):
(WTF::StringImpl::createUninitialized):
(WTF::StringImpl::reallocateInternal):
(WTF::StringImpl::reallocate):
(WTF::StringImpl::createInternal):
(WTF::StringImpl::create):
* wtf/text/StringImpl.h:

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (152414 => 152415)


--- trunk/Source/WTF/ChangeLog	2013-07-05 10:29:12 UTC (rev 152414)
+++ trunk/Source/WTF/ChangeLog	2013-07-05 11:23:44 UTC (rev 152415)
@@ -1,3 +1,24 @@
+2013-07-05  Mikhail Pozdnyakov  <mikhail.pozdnya...@intel.com>
+
+        Remove code duplication from StringImpl create()/reallocate() methods
+        https://bugs.webkit.org/show_bug.cgi?id=118355
+
+        Reviewed by Andreas Kling.
+
+        StringImpl create()/reallocate() methods accepting LChar and UChar used to have
+        duplicated code. The code duplication is removed now via used templates.
+
+        * wtf/text/StringImpl.cpp:
+        (WTF::StringImpl::constructInternal):
+        (WTF::LChar):
+        (WTF::StringImpl::createUninitializedInternal):
+        (WTF::StringImpl::createUninitialized):
+        (WTF::StringImpl::reallocateInternal):
+        (WTF::StringImpl::reallocate):
+        (WTF::StringImpl::createInternal):
+        (WTF::StringImpl::create):
+        * wtf/text/StringImpl.h:
+
 2013-07-03  Brent Fulgham  <bfulg...@apple.com>
 
         [Windows] Unreviewed build correction.

Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (152414 => 152415)


--- trunk/Source/WTF/wtf/text/StringImpl.cpp	2013-07-05 10:29:12 UTC (rev 152414)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp	2013-07-05 11:23:44 UTC (rev 152415)
@@ -182,7 +182,8 @@
     return adoptRef(new StringImpl(characters, length, ConstructWithoutCopying));
 }
 
-PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, LChar*& data)
+template <typename CharType>
+inline PassRefPtr<StringImpl> StringImpl::createUninitializedInternal(unsigned length, CharType*& data)
 {
     if (!length) {
         data = ""
@@ -192,37 +193,28 @@
     // Allocate a single buffer large enough to contain the StringImpl
     // struct as well as the data which it contains. This removes one
     // heap allocation from this call.
-    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(LChar)))
+    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(CharType)))
         CRASH();
-    size_t size = sizeof(StringImpl) + length * sizeof(LChar);
+    size_t size = sizeof(StringImpl) + length * sizeof(CharType);
     StringImpl* string = static_cast<StringImpl*>(fastMalloc(size));
 
-    data = "" + 1);
-    return adoptRef(new (NotNull, string) StringImpl(length, Force8BitConstructor));
+    data = "" + 1);
+    return constructInternal<CharType>(string, length);
 }
 
-PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, UChar*& data)
+PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, LChar*& data)
 {
-    if (!length) {
-        data = ""
-        return empty();
-    }
-
-    // Allocate a single buffer large enough to contain the StringImpl
-    // struct as well as the data which it contains. This removes one 
-    // heap allocation from this call.
-    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(UChar)))
-        CRASH();
-    size_t size = sizeof(StringImpl) + length * sizeof(UChar);
-    StringImpl* string = static_cast<StringImpl*>(fastMalloc(size));
-
-    data = "" + 1);
-    return adoptRef(new (NotNull, string) StringImpl(length));
+    return createUninitializedInternal(length, data);
 }
 
-PassRefPtr<StringImpl> StringImpl::reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data)
+PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, UChar*& data)
 {
-    ASSERT(originalString->is8Bit());
+    return createUninitializedInternal(length, data);
+}
+
+template <typename CharType>
+inline PassRefPtr<StringImpl> StringImpl::reallocateInternal(PassRefPtr<StringImpl> originalString, unsigned length, CharType*& data)
+{   
     ASSERT(originalString->hasOneRef());
     ASSERT(originalString->bufferOwnership() == BufferInternal);
 
@@ -232,58 +224,48 @@
     }
 
     // Same as createUninitialized() except here we use fastRealloc.
-    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(LChar)))
+    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(CharType)))
         CRASH();
-    size_t size = sizeof(StringImpl) + length * sizeof(LChar);
+    size_t size = sizeof(StringImpl) + length * sizeof(CharType);
     originalString->~StringImpl();
     StringImpl* string = static_cast<StringImpl*>(fastRealloc(originalString.leakRef(), size));
 
-    data = "" + 1);
-    return adoptRef(new (NotNull, string) StringImpl(length, Force8BitConstructor));
+    data = "" + 1);
+    return constructInternal<CharType>(string, length);
 }
 
+PassRefPtr<StringImpl> StringImpl::reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data)
+{
+    ASSERT(originalString->is8Bit());
+    return reallocateInternal(originalString, length, data);
+}
+
 PassRefPtr<StringImpl> StringImpl::reallocate(PassRefPtr<StringImpl> originalString, unsigned length, UChar*& data)
 {
     ASSERT(!originalString->is8Bit());
-    ASSERT(originalString->hasOneRef());
-    ASSERT(originalString->bufferOwnership() == BufferInternal);
-
-    if (!length) {
-        data = ""
-        return empty();
-    }
-
-    // Same as createUninitialized() except here we use fastRealloc.
-    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(UChar)))
-        CRASH();
-    size_t size = sizeof(StringImpl) + length * sizeof(UChar);
-    originalString->~StringImpl();
-    StringImpl* string = static_cast<StringImpl*>(fastRealloc(originalString.leakRef(), size));
-
-    data = "" + 1);
-    return adoptRef(new (NotNull, string) StringImpl(length));
+    return reallocateInternal(originalString, length, data);
 }
 
-PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned length)
+template <typename CharType>
+inline PassRefPtr<StringImpl> StringImpl::createInternal(const CharType* characters, unsigned length)
 {
     if (!characters || !length)
         return empty();
 
-    UChar* data;
+    CharType* data;
     RefPtr<StringImpl> string = createUninitialized(length, data);
-    memcpy(data, characters, length * sizeof(UChar));
+    memcpy(data, characters, length * sizeof(CharType));
     return string.release();
 }
 
-PassRefPtr<StringImpl> StringImpl::create(const LChar* characters, unsigned length)
+PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned length)
 {
-    if (!characters || !length)
-        return empty();
+    return createInternal(characters, length);
+}
 
-    LChar* data;
-    RefPtr<StringImpl> string = createUninitialized(length, data);
-    memcpy(data, characters, length * sizeof(LChar));
-    return string.release();
+PassRefPtr<StringImpl> StringImpl::create(const LChar* characters, unsigned length)
+{
+    return createInternal(characters, length);
 }
 
 PassRefPtr<StringImpl> StringImpl::create8BitIfPossible(const UChar* characters, unsigned length)

Modified: trunk/Source/WTF/wtf/text/StringImpl.h (152414 => 152415)


--- trunk/Source/WTF/wtf/text/StringImpl.h	2013-07-05 10:29:12 UTC (rev 152414)
+++ trunk/Source/WTF/wtf/text/StringImpl.h	2013-07-05 11:23:44 UTC (rev 152415)
@@ -443,10 +443,7 @@
         }
         output = reinterpret_cast<T*>(resultImpl + 1);
 
-        if (sizeof(T) == sizeof(char))
-            return adoptRef(new (NotNull, resultImpl) StringImpl(length, Force8BitConstructor));
-
-        return adoptRef(new (NotNull, resultImpl) StringImpl(length));
+        return constructInternal<T>(resultImpl, length);
     }
 
     static PassRefPtr<StringImpl> createEmptyUnique()
@@ -770,6 +767,10 @@
     bool isStatic() const { return m_refCount & s_refCountFlagIsStaticString; }
     template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacters(UCharPredicate);
     template <typename CharType, class UCharPredicate> PassRefPtr<StringImpl> simplifyMatchedCharactersToSpace(UCharPredicate);
+    template <typename CharType> static PassRefPtr<StringImpl> constructInternal(StringImpl*, unsigned);
+    template <typename CharType> static PassRefPtr<StringImpl> createUninitializedInternal(unsigned, CharType*&);
+    template <typename CharType> static PassRefPtr<StringImpl> reallocateInternal(PassRefPtr<StringImpl>, unsigned, CharType*&);
+    template <typename CharType> static PassRefPtr<StringImpl> createInternal(const CharType*, unsigned);
     WTF_EXPORT_STRING_API NEVER_INLINE const UChar* getData16SlowCase() const;
     WTF_EXPORT_PRIVATE NEVER_INLINE unsigned hashSlowCase() const;
 
@@ -849,6 +850,14 @@
 };
 #endif
 
+template <typename CharType>
+ALWAYS_INLINE PassRefPtr<StringImpl> StringImpl::constructInternal(StringImpl* impl, unsigned length)
+{
+    if (sizeof(CharType) == sizeof(char))
+        return adoptRef(new (NotNull, impl) StringImpl(length, Force8BitConstructor));
+    return adoptRef(new (NotNull, impl) StringImpl(length));
+}
+
 template <>
 ALWAYS_INLINE const LChar* StringImpl::getCharacters<LChar>() const { return characters8(); }
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to