Changeset 152415 in webkit


Ignore:
Timestamp:
Jul 5, 2013 4:23:44 AM (11 years ago)
Author:
mikhail.pozdnyakov@intel.com
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:
Location:
trunk/Source/WTF
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r152381 r152415  
     12013-07-05  Mikhail Pozdnyakov  <mikhail.pozdnyakov@intel.com>
     2
     3        Remove code duplication from StringImpl create()/reallocate() methods
     4        https://bugs.webkit.org/show_bug.cgi?id=118355
     5
     6        Reviewed by Andreas Kling.
     7
     8        StringImpl create()/reallocate() methods accepting LChar and UChar used to have
     9        duplicated code. The code duplication is removed now via used templates.
     10
     11        * wtf/text/StringImpl.cpp:
     12        (WTF::StringImpl::constructInternal):
     13        (WTF::LChar):
     14        (WTF::StringImpl::createUninitializedInternal):
     15        (WTF::StringImpl::createUninitialized):
     16        (WTF::StringImpl::reallocateInternal):
     17        (WTF::StringImpl::reallocate):
     18        (WTF::StringImpl::createInternal):
     19        (WTF::StringImpl::create):
     20        * wtf/text/StringImpl.h:
     21
    1222013-07-03  Brent Fulgham  <bfulgham@apple.com>
    223
  • trunk/Source/WTF/wtf/text/StringImpl.cpp

    r152362 r152415  
    183183}
    184184
    185 PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, LChar*& data)
     185template <typename CharType>
     186inline PassRefPtr<StringImpl> StringImpl::createUninitializedInternal(unsigned length, CharType*& data)
    186187{
    187188    if (!length) {
     
    193194    // struct as well as the data which it contains. This removes one
    194195    // heap allocation from this call.
    195     if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(LChar)))
     196    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(CharType)))
    196197        CRASH();
    197     size_t size = sizeof(StringImpl) + length * sizeof(LChar);
     198    size_t size = sizeof(StringImpl) + length * sizeof(CharType);
    198199    StringImpl* string = static_cast<StringImpl*>(fastMalloc(size));
    199200
    200     data = reinterpret_cast<LChar*>(string + 1);
    201     return adoptRef(new (NotNull, string) StringImpl(length, Force8BitConstructor));
     201    data = reinterpret_cast<CharType*>(string + 1);
     202    return constructInternal<CharType>(string, length);
     203}
     204
     205PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, LChar*& data)
     206{
     207    return createUninitializedInternal(length, data);
    202208}
    203209
    204210PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, UChar*& data)
    205211{
     212    return createUninitializedInternal(length, data);
     213}
     214
     215template <typename CharType>
     216inline PassRefPtr<StringImpl> StringImpl::reallocateInternal(PassRefPtr<StringImpl> originalString, unsigned length, CharType*& data)
     217{   
     218    ASSERT(originalString->hasOneRef());
     219    ASSERT(originalString->bufferOwnership() == BufferInternal);
     220
    206221    if (!length) {
    207222        data = 0;
     
    209224    }
    210225
    211     // Allocate a single buffer large enough to contain the StringImpl
    212     // struct as well as the data which it contains. This removes one
    213     // heap allocation from this call.
    214     if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(UChar)))
     226    // Same as createUninitialized() except here we use fastRealloc.
     227    if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(CharType)))
    215228        CRASH();
    216     size_t size = sizeof(StringImpl) + length * sizeof(UChar);
    217     StringImpl* string = static_cast<StringImpl*>(fastMalloc(size));
    218 
    219     data = reinterpret_cast<UChar*>(string + 1);
    220     return adoptRef(new (NotNull, string) StringImpl(length));
    221 }
    222 
    223 PassRefPtr<StringImpl> StringImpl::reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data)
    224 {
    225     ASSERT(originalString->is8Bit());
    226     ASSERT(originalString->hasOneRef());
    227     ASSERT(originalString->bufferOwnership() == BufferInternal);
    228 
    229     if (!length) {
    230         data = 0;
    231         return empty();
    232     }
    233 
    234     // Same as createUninitialized() except here we use fastRealloc.
    235     if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(LChar)))
    236         CRASH();
    237     size_t size = sizeof(StringImpl) + length * sizeof(LChar);
     229    size_t size = sizeof(StringImpl) + length * sizeof(CharType);
    238230    originalString->~StringImpl();
    239231    StringImpl* string = static_cast<StringImpl*>(fastRealloc(originalString.leakRef(), size));
    240232
    241     data = reinterpret_cast<LChar*>(string + 1);
    242     return adoptRef(new (NotNull, string) StringImpl(length, Force8BitConstructor));
     233    data = reinterpret_cast<CharType*>(string + 1);
     234    return constructInternal<CharType>(string, length);
     235}
     236
     237PassRefPtr<StringImpl> StringImpl::reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data)
     238{
     239    ASSERT(originalString->is8Bit());
     240    return reallocateInternal(originalString, length, data);
    243241}
    244242
     
    246244{
    247245    ASSERT(!originalString->is8Bit());
    248     ASSERT(originalString->hasOneRef());
    249     ASSERT(originalString->bufferOwnership() == BufferInternal);
    250 
    251     if (!length) {
    252         data = 0;
    253         return empty();
    254     }
    255 
    256     // Same as createUninitialized() except here we use fastRealloc.
    257     if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(UChar)))
    258         CRASH();
    259     size_t size = sizeof(StringImpl) + length * sizeof(UChar);
    260     originalString->~StringImpl();
    261     StringImpl* string = static_cast<StringImpl*>(fastRealloc(originalString.leakRef(), size));
    262 
    263     data = reinterpret_cast<UChar*>(string + 1);
    264     return adoptRef(new (NotNull, string) StringImpl(length));
    265 }
    266 
    267 PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned length)
     246    return reallocateInternal(originalString, length, data);
     247}
     248
     249template <typename CharType>
     250inline PassRefPtr<StringImpl> StringImpl::createInternal(const CharType* characters, unsigned length)
    268251{
    269252    if (!characters || !length)
    270253        return empty();
    271254
    272     UChar* data;
     255    CharType* data;
    273256    RefPtr<StringImpl> string = createUninitialized(length, data);
    274     memcpy(data, characters, length * sizeof(UChar));
     257    memcpy(data, characters, length * sizeof(CharType));
    275258    return string.release();
    276259}
    277260
     261PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, unsigned length)
     262{
     263    return createInternal(characters, length);
     264}
     265
    278266PassRefPtr<StringImpl> StringImpl::create(const LChar* characters, unsigned length)
    279267{
    280     if (!characters || !length)
    281         return empty();
    282 
    283     LChar* data;
    284     RefPtr<StringImpl> string = createUninitialized(length, data);
    285     memcpy(data, characters, length * sizeof(LChar));
    286     return string.release();
     268    return createInternal(characters, length);
    287269}
    288270
  • trunk/Source/WTF/wtf/text/StringImpl.h

    r152362 r152415  
    444444        output = reinterpret_cast<T*>(resultImpl + 1);
    445445
    446         if (sizeof(T) == sizeof(char))
    447             return adoptRef(new (NotNull, resultImpl) StringImpl(length, Force8BitConstructor));
    448 
    449         return adoptRef(new (NotNull, resultImpl) StringImpl(length));
     446        return constructInternal<T>(resultImpl, length);
    450447    }
    451448
     
    771768    template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacters(UCharPredicate);
    772769    template <typename CharType, class UCharPredicate> PassRefPtr<StringImpl> simplifyMatchedCharactersToSpace(UCharPredicate);
     770    template <typename CharType> static PassRefPtr<StringImpl> constructInternal(StringImpl*, unsigned);
     771    template <typename CharType> static PassRefPtr<StringImpl> createUninitializedInternal(unsigned, CharType*&);
     772    template <typename CharType> static PassRefPtr<StringImpl> reallocateInternal(PassRefPtr<StringImpl>, unsigned, CharType*&);
     773    template <typename CharType> static PassRefPtr<StringImpl> createInternal(const CharType*, unsigned);
    773774    WTF_EXPORT_STRING_API NEVER_INLINE const UChar* getData16SlowCase() const;
    774775    WTF_EXPORT_PRIVATE NEVER_INLINE unsigned hashSlowCase() const;
     
    850851#endif
    851852
     853template <typename CharType>
     854ALWAYS_INLINE PassRefPtr<StringImpl> StringImpl::constructInternal(StringImpl* impl, unsigned length)
     855{
     856    if (sizeof(CharType) == sizeof(char))
     857        return adoptRef(new (NotNull, impl) StringImpl(length, Force8BitConstructor));
     858    return adoptRef(new (NotNull, impl) StringImpl(length));
     859}
     860
    852861template <>
    853862ALWAYS_INLINE const LChar* StringImpl::getCharacters<LChar>() const { return characters8(); }
Note: See TracChangeset for help on using the changeset viewer.