Ignore:
Timestamp:
Mar 11, 2010, 7:14:17 PM (15 years ago)
Author:
[email protected]
Message:

https://bugs.webkit.org/show_bug.cgi?id=36041
Remove unnecessary differences in common code between WebCore::StringImpl & JSC::UStringImpl

Reviewed by Oliver Hunt.

Much of the code in WebCore::StringImpl and JSC::UStringImpl is now very similar,
but has trivial and unnecessary formatting differences, such as the exact wording
of comments, missing ASSERTs, functions implemented in the .h vs .cpp etc.

JavaScriptCore:

  • runtime/Identifier.cpp:

(JSC::Identifier::add): UStringImpl::empty() now automatically hashes, uas per WebCore strings.
(JSC::Identifier::addSlowCase): UStringImpl::empty() now automatically hashes, uas per WebCore strings.

  • runtime/UStringImpl.cpp:

(JSC::UStringImpl::~UStringImpl): Only call bufferOwnership() once, add missing ASSERTs.
(JSC::UStringImpl::createUninitialized): Move from .h, not commonly called, no need to inline.
(JSC::UStringImpl::create): Move from .h, not commonly called, no need to inline.
(JSC::UStringImpl::sharedBuffer): Rewritten to more closely match WebCore implementation, remove need for separate baseSharedBuffer() method.

  • runtime/UStringImpl.h:

(JSC::UStringImpl::UStringImpl): Automatically hash static strings, ASSERT m_data & m_length are non-null/non-zero in non-static strings.
(JSC::UStringImpl::setHash): Add missing ASSERT.
(JSC::UStringImpl::create): Moved to .cpp / added missing check for empty string creation.
(JSC::UStringImpl::adopt): Vector.size() returns size_t, not unsigned.
(JSC::UStringImpl::cost): Renamed m_bufferSubstring -> m_substringBuffer
(JSC::UStringImpl::hash): Reordered in file.
(JSC::UStringImpl::existingHash): Reordered in file.
(JSC::UStringImpl::computeHash): Reordered in file, renamed parameter.
(JSC::UStringImpl::checkConsistency): rewrote ASSERT.
(JSC::UStringImpl::bufferOwnership): Return type should be BufferOwnership.
(JSC::UStringImpl::): Moved friends to head of class.

WebCore:

  • platform/text/StringImpl.cpp:

(WebCore::StringImpl::empty): Reordered in file, made empty()->characters() return a non-null value to match JSC.
(WebCore::StringImpl::createUninitialized): Added overflow check.
(WebCore::StringImpl::create): Reordered in file.
(WebCore::StringImpl::sharedBuffer): Reordered in file.

  • platform/text/StringImpl.h:

(WebCore::StringImpl::): Remove ThreadGlobalData as friend, move SharableUChar & SharedUChar to WebCore namespace.
(WebCore::StringImpl::StringImpl): Made static constructor method (used to create empty string) take arguments, to match JSC & prevent accidental use.
(WebCore::StringImpl::setHash): Added missing ASSERT.
(WebCore::StringImpl::adopt): Make adpot work with Vectors with a non-zero inline capacity.
(WebCore::StringImpl::characters): Mark as const to match JSC.
(WebCore::StringImpl::hash): Use !m_hash instead of m_hash == 0.
(WebCore::StringImpl::computeHash): Remove redundant 'inline'.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/UStringImpl.cpp

    r55825 r55878  
    3737namespace JSC {
    3838
     39static const unsigned minLengthToShare = 20;
     40
     41UStringImpl::~UStringImpl()
     42{
     43    ASSERT(!isStatic());
     44    checkConsistency();
     45
     46    if (isIdentifier())
     47        Identifier::remove(this);
     48
     49    BufferOwnership ownership = bufferOwnership();
     50    if (ownership != BufferInternal) {
     51        if (ownership == BufferOwned) {
     52            ASSERT(!m_sharedBuffer);
     53            ASSERT(m_data);
     54            fastFree(const_cast<UChar*>(m_data));
     55        } else if (ownership == BufferSubstring) {
     56            ASSERT(m_substringBuffer);
     57            m_substringBuffer->deref();
     58        } else {
     59            ASSERT(ownership == BufferShared);
     60            ASSERT(m_sharedBuffer);
     61            m_sharedBuffer->deref();
     62        }
     63    }
     64}
     65
    3966UStringImpl* UStringImpl::empty()
    4067{
     
    4471    DEFINE_STATIC_LOCAL(UStringImpl, emptyString, (invalidNonNullUCharPtr, 0, ConstructStaticString));
    4572    return &emptyString;
     73}
     74
     75PassRefPtr<UStringImpl> UStringImpl::createUninitialized(unsigned length, UChar*& data)
     76{
     77    if (!length) {
     78        data = 0;
     79        return empty();
     80    }
     81
     82    // Allocate a single buffer large enough to contain the StringImpl
     83    // struct as well as the data which it contains. This removes one
     84    // heap allocation from this call.
     85    if (length > ((std::numeric_limits<size_t>::max() - sizeof(UStringImpl)) / sizeof(UChar)))
     86        CRASH();
     87    size_t size = sizeof(UStringImpl) + length * sizeof(UChar);
     88    UStringImpl* string = static_cast<UStringImpl*>(fastMalloc(size));
     89
     90    data = reinterpret_cast<UChar*>(string + 1);
     91    return adoptRef(new (string) UStringImpl(length));
    4692}
    4793
     
    78124}
    79125
    80 SharedUChar* UStringImpl::baseSharedBuffer()
     126PassRefPtr<UStringImpl> UStringImpl::create(PassRefPtr<SharedUChar> sharedBuffer, const UChar* buffer, unsigned length)
    81127{
    82     ASSERT((bufferOwnership() == BufferShared)
    83         || ((bufferOwnership() == BufferOwned) && !m_buffer));
    84 
    85     if (bufferOwnership() != BufferShared) {
    86         m_refCountAndFlags = (m_refCountAndFlags & ~s_refCountMaskBufferOwnership) | BufferShared;
    87         m_bufferShared = SharedUChar::create(new SharableUChar(m_data)).releaseRef();
    88     }
    89 
    90     return m_bufferShared;
     128    if (!length)
     129        return empty();
     130    return adoptRef(new UStringImpl(buffer, length, sharedBuffer));
    91131}
    92132
    93133SharedUChar* UStringImpl::sharedBuffer()
    94134{
    95     if (m_length < s_minLengthToShare)
     135    if (m_length < minLengthToShare)
    96136        return 0;
     137    // All static strings are smaller that the minimim length to share.
    97138    ASSERT(!isStatic());
    98139
    99     UStringImpl* owner = bufferOwnerString();
    100     if (owner->bufferOwnership() == BufferInternal)
     140    BufferOwnership ownership = bufferOwnership();
     141
     142    if (ownership == BufferInternal)
    101143        return 0;
     144    if (ownership == BufferSubstring)
     145        return m_substringBuffer->sharedBuffer();
     146    if (ownership == BufferOwned) {
     147        ASSERT(!m_sharedBuffer);
     148        m_sharedBuffer = SharedUChar::create(new SharableUChar(m_data)).releaseRef();
     149        m_refCountAndFlags = (m_refCountAndFlags & ~s_refCountMaskBufferOwnership) | BufferShared;
     150    }
    102151
    103     return owner->baseSharedBuffer();
    104 }
    105 
    106 UStringImpl::~UStringImpl()
    107 {
    108     ASSERT(!isStatic());
    109     checkConsistency();
    110 
    111     if (isIdentifier())
    112         Identifier::remove(this);
    113 
    114     if (bufferOwnership() != BufferInternal) {
    115         if (bufferOwnership() == BufferOwned)
    116             fastFree(const_cast<UChar*>(m_data));
    117         else if (bufferOwnership() == BufferSubstring)
    118             m_bufferSubstring->deref();
    119         else {
    120             ASSERT(bufferOwnership() == BufferShared);
    121             m_bufferShared->deref();
    122         }
    123     }
     152    ASSERT(bufferOwnership() == BufferShared);
     153    ASSERT(m_sharedBuffer);
     154    return m_sharedBuffer;
    124155}
    125156
Note: See TracChangeset for help on using the changeset viewer.