2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef StringImplBase_h
27 #define StringImplBase_h
29 #include <wtf/Noncopyable.h>
30 #include <wtf/unicode/Unicode.h>
34 class StringImplBase
: public Noncopyable
{
36 bool isStringImpl() { return (m_refCountAndFlags
& s_refCountInvalidForStringImpl
) != s_refCountInvalidForStringImpl
; }
37 unsigned length() const { return m_length
; }
38 void ref() { m_refCountAndFlags
+= s_refCountIncrement
; }
41 enum BufferOwnership
{
48 using Noncopyable::operator new;
49 void* operator new(size_t, void* inPlace
) { ASSERT(inPlace
); return inPlace
; }
51 // For SmallStringStorage, which allocates an array and uses an in-place new.
54 StringImplBase(unsigned length
, BufferOwnership ownership
)
55 : m_refCountAndFlags(s_refCountIncrement
| s_refCountFlagShouldReportedCost
| ownership
)
58 ASSERT(isStringImpl());
61 enum StaticStringConstructType
{ ConstructStaticString
};
62 StringImplBase(unsigned length
, StaticStringConstructType
)
63 : m_refCountAndFlags(s_refCountFlagStatic
| s_refCountFlagIsIdentifier
| BufferOwned
)
66 ASSERT(isStringImpl());
69 // This constructor is not used when creating StringImpl objects,
70 // and sets the flags into a state marking the object as such.
71 enum NonStringImplConstructType
{ ConstructNonStringImpl
};
72 StringImplBase(NonStringImplConstructType
)
73 : m_refCountAndFlags(s_refCountIncrement
| s_refCountInvalidForStringImpl
)
76 ASSERT(!isStringImpl());
79 // The bottom 7 bits hold flags, the top 25 bits hold the ref count.
80 // When dereferencing StringImpls we check for the ref count AND the
81 // static bit both being zero - static strings are never deleted.
82 static const unsigned s_refCountMask
= 0xFFFFFF80;
83 static const unsigned s_refCountIncrement
= 0x80;
84 static const unsigned s_refCountFlagStatic
= 0x40;
85 static const unsigned s_refCountFlagHasTerminatingNullCharacter
= 0x20;
86 static const unsigned s_refCountFlagIsAtomic
= 0x10;
87 static const unsigned s_refCountFlagShouldReportedCost
= 0x8;
88 static const unsigned s_refCountFlagIsIdentifier
= 0x4;
89 static const unsigned s_refCountMaskBufferOwnership
= 0x3;
90 // An invalid permutation of flags (static & shouldReportedCost - static strings do not
91 // set shouldReportedCost in the constructor, and this bit is only ever cleared, not set).
92 // Used by "ConstructNonStringImpl" constructor, above.
93 static const unsigned s_refCountInvalidForStringImpl
= s_refCountFlagStatic
| s_refCountFlagShouldReportedCost
;
95 unsigned m_refCountAndFlags
;
101 using WTF::StringImplBase
;