Ignore:
Timestamp:
Apr 20, 2010, 3:34:52 PM (15 years ago)
Author:
[email protected]
Message:

JavaScriptCore: Bug 37895 - Share common code from UStringImplBase with StringImpl

Reviewed by Oliver Hunt.

The implementation of StringImpl & UStringImpl is very similar. Restructure
StringImpl to match UStringImpl, moving the flags and length into a base class,
so that this can be shared between both string types to increase code reuse.

(JSC::RopeImpl::RopeImpl):

  • runtime/UStringImpl.h:

(JSC::UStringImpl::UStringImpl):

  • wtf/text/StringImpl.h:

(WebCore::StringImpl::StringImpl):
(WebCore::StringImpl::characters):

  • wtf/text/StringImplBase.h: Copied from JavaScriptCore/runtime/UStringImpl.h.

(WTF::StringImplBase::length):
(WTF::StringImplBase::operator new):
(WTF::StringImplBase::StringImplBase):

JavaScriptGlue: Bug 37895 - Share common code from UStringImplBase with StringImpl
Add forwarding header.

Reviewed by Oliver Hunt.

  • ForwardingHeaders/wtf/text/StringImplBase.h: Added.

WebCore: Bug 37895 - Share common code from UStringImplBase with StringImpl
Add forwarding header.

Reviewed by Oliver Hunt.

  • ForwardingHeaders/wtf/text/StringImplBase.h: Added.
File:
1 edited

Legend:

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

    r57912 r57932  
    3434#include <wtf/Vector.h>
    3535#include <wtf/unicode/Unicode.h>
     36#include <wtf/text/StringImplBase.h>
    3637
    3738namespace JSC {
     
    4243typedef CrossThreadRefCounted<SharableUChar> SharedUChar;
    4344
    44 class UStringImplBase : public Noncopyable {
    45 public:
    46     bool isStringImpl() { return (m_refCountAndFlags & s_refCountInvalidForStringImpl) != s_refCountInvalidForStringImpl; }
    47     unsigned length() const { return m_length; }
    48 
    49     void ref() { m_refCountAndFlags += s_refCountIncrement; }
    50 
    51 protected:
    52     enum BufferOwnership {
    53         BufferInternal,
    54         BufferOwned,
    55         BufferSubstring,
    56         BufferShared,
    57     };
    58 
    59     using Noncopyable::operator new;
    60     void* operator new(size_t, void* inPlace) { return inPlace; }
    61 
    62     // For SmallStringStorage, which allocates an array and uses an in-place new.
    63     UStringImplBase() { }
    64 
    65     UStringImplBase(unsigned length, BufferOwnership ownership)
    66         : m_refCountAndFlags(s_refCountIncrement | s_refCountFlagShouldReportedCost | ownership)
    67         , m_length(length)
    68     {
    69         ASSERT(isStringImpl());
    70     }
    71 
    72     enum StaticStringConstructType { ConstructStaticString };
    73     UStringImplBase(unsigned length, StaticStringConstructType)
    74         : m_refCountAndFlags(s_refCountFlagStatic | s_refCountFlagIsIdentifier | BufferOwned)
    75         , m_length(length)
    76     {
    77         ASSERT(isStringImpl());
    78     }
    79 
    80     // This constructor is not used when creating UStringImpl objects,
    81     // and sets the flags into a state marking the object as such.
    82     enum NonStringImplConstructType { ConstructNonStringImpl };
    83     UStringImplBase(NonStringImplConstructType)
    84         : m_refCountAndFlags(s_refCountIncrement | s_refCountInvalidForStringImpl)
    85         , m_length(0)
    86     {
    87         ASSERT(!isStringImpl());
    88     }
    89 
    90     // The bottom 5 bits hold flags, the top 27 bits hold the ref count.
    91     // When dereferencing UStringImpls we check for the ref count AND the
    92     // static bit both being zero - static strings are never deleted.
    93     static const unsigned s_refCountMask = 0xFFFFFFE0;
    94     static const unsigned s_refCountIncrement = 0x20;
    95     static const unsigned s_refCountFlagStatic = 0x10;
    96     static const unsigned s_refCountFlagShouldReportedCost = 0x8;
    97     static const unsigned s_refCountFlagIsIdentifier = 0x4;
    98     static const unsigned s_refCountMaskBufferOwnership = 0x3;
    99     // An invalid permutation of flags (static & shouldReportedCost - static strings do not
    100     // set shouldReportedCost in the constructor, and this bit is only ever cleared, not set).
    101     // Used by "ConstructNonStringImpl" constructor, above.
    102     static const unsigned s_refCountInvalidForStringImpl = s_refCountFlagStatic | s_refCountFlagShouldReportedCost;
    103 
    104     unsigned m_refCountAndFlags;
    105     unsigned m_length;
    106 };
    107 
    108 class UStringImpl : public UStringImplBase {
     45class UStringImpl : public StringImplBase {
    10946    friend struct CStringTranslator;
    11047    friend struct UCharBufferTranslator;
     
    12057    // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
    12158    UStringImpl(const UChar* characters, unsigned length, StaticStringConstructType)
    122         : UStringImplBase(length, ConstructStaticString)
     59        : StringImplBase(length, ConstructStaticString)
    12360        , m_data(characters)
    12461        , m_buffer(0)
     
    13067    // Create a normal string with internal storage (BufferInternal)
    13168    UStringImpl(unsigned length)
    132         : UStringImplBase(length, BufferInternal)
     69        : StringImplBase(length, BufferInternal)
    13370        , m_data(reinterpret_cast<UChar*>(this + 1))
    13471        , m_buffer(0)
     
    14178    // Create a UStringImpl adopting ownership of the provided buffer (BufferOwned)
    14279    UStringImpl(const UChar* characters, unsigned length)
    143         : UStringImplBase(length, BufferOwned)
     80        : StringImplBase(length, BufferOwned)
    14481        , m_data(characters)
    14582        , m_buffer(0)
     
    15289    // Used to create new strings that are a substring of an existing UStringImpl (BufferSubstring)
    15390    UStringImpl(const UChar* characters, unsigned length, PassRefPtr<UStringImpl> base)
    154         : UStringImplBase(length, BufferSubstring)
     91        : StringImplBase(length, BufferSubstring)
    15592        , m_data(characters)
    15693        , m_substringBuffer(base.releaseRef())
     
    164101    // Used to construct new strings sharing an existing SharedUChar (BufferShared)
    165102    UStringImpl(const UChar* characters, unsigned length, PassRefPtr<SharedUChar> sharedBuffer)
    166         : UStringImplBase(length, BufferShared)
     103        : StringImplBase(length, BufferShared)
    167104        , m_data(characters)
    168105        , m_sharedBuffer(sharedBuffer.releaseRef())
Note: See TracChangeset for help on using the changeset viewer.