JavaScriptCore/wtf/text/StringImplBase.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Apple Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
       
    14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
       
    17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    24  */
       
    25 
       
    26 #ifndef StringImplBase_h
       
    27 #define StringImplBase_h
       
    28 
       
    29 #include <wtf/Noncopyable.h>
       
    30 #include <wtf/unicode/Unicode.h>
       
    31 
       
    32 namespace WTF {
       
    33 
       
    34 class StringImplBase : public Noncopyable {
       
    35 public:
       
    36     bool isStringImpl() { return (m_refCountAndFlags & s_refCountInvalidForStringImpl) != s_refCountInvalidForStringImpl; }
       
    37     unsigned length() const { return m_length; }
       
    38     void ref() { m_refCountAndFlags += s_refCountIncrement; }
       
    39 
       
    40 protected:
       
    41     enum BufferOwnership {
       
    42         BufferInternal,
       
    43         BufferOwned,
       
    44         BufferSubstring,
       
    45         BufferShared,
       
    46     };
       
    47 
       
    48     using Noncopyable::operator new;
       
    49 
       
    50 #if !COMPILER(WINSCW)
       
    51     void* operator new(size_t, void* inPlace) { ASSERT(inPlace); return inPlace; }
       
    52 #endif
       
    53 
       
    54     // For SmallStringStorage, which allocates an array and uses an in-place new.
       
    55     StringImplBase() { }
       
    56 
       
    57     StringImplBase(unsigned length, BufferOwnership ownership)
       
    58         : m_refCountAndFlags(s_refCountIncrement | s_refCountFlagShouldReportedCost | ownership)
       
    59         , m_length(length)
       
    60     {
       
    61         ASSERT(isStringImpl());
       
    62     }
       
    63 
       
    64     enum StaticStringConstructType { ConstructStaticString };
       
    65     StringImplBase(unsigned length, StaticStringConstructType)
       
    66         : m_refCountAndFlags(s_refCountFlagStatic | s_refCountFlagIsIdentifier | BufferOwned)
       
    67         , m_length(length)
       
    68     {
       
    69         ASSERT(isStringImpl());
       
    70     }
       
    71 
       
    72     // This constructor is not used when creating StringImpl objects,
       
    73     // and sets the flags into a state marking the object as such.
       
    74     enum NonStringImplConstructType { ConstructNonStringImpl };
       
    75     StringImplBase(NonStringImplConstructType)
       
    76         : m_refCountAndFlags(s_refCountIncrement | s_refCountInvalidForStringImpl)
       
    77         , m_length(0)
       
    78     {
       
    79         ASSERT(!isStringImpl());
       
    80     }
       
    81 
       
    82     // The bottom 7 bits hold flags, the top 25 bits hold the ref count.
       
    83     // When dereferencing StringImpls we check for the ref count AND the
       
    84     // static bit both being zero - static strings are never deleted.
       
    85     static const unsigned s_refCountMask = 0xFFFFFF80;
       
    86     static const unsigned s_refCountIncrement = 0x80;
       
    87     static const unsigned s_refCountFlagStatic = 0x40;
       
    88     static const unsigned s_refCountFlagHasTerminatingNullCharacter = 0x20;
       
    89     static const unsigned s_refCountFlagIsAtomic = 0x10;
       
    90     static const unsigned s_refCountFlagShouldReportedCost = 0x8;
       
    91     static const unsigned s_refCountFlagIsIdentifier = 0x4;
       
    92     static const unsigned s_refCountMaskBufferOwnership = 0x3;
       
    93     // An invalid permutation of flags (static & shouldReportedCost - static strings do not
       
    94     // set shouldReportedCost in the constructor, and this bit is only ever cleared, not set).
       
    95     // Used by "ConstructNonStringImpl" constructor, above.
       
    96     static const unsigned s_refCountInvalidForStringImpl = s_refCountFlagStatic | s_refCountFlagShouldReportedCost;
       
    97 
       
    98     unsigned m_refCountAndFlags;
       
    99     unsigned m_length;
       
   100 };
       
   101 
       
   102 } // namespace WTF
       
   103 
       
   104 using WTF::StringImplBase;
       
   105 
       
   106 #endif