]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/text/StringImplBase.h
65676725eb583158a7569d0d1a0bb685b83bd019
[apple/javascriptcore.git] / wtf / text / StringImplBase.h
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 void* operator new(size_t, void* inPlace) { ASSERT(inPlace); return inPlace; }
50
51 // For SmallStringStorage, which allocates an array and uses an in-place new.
52 StringImplBase() { }
53
54 StringImplBase(unsigned length, BufferOwnership ownership)
55 : m_refCountAndFlags(s_refCountIncrement | s_refCountFlagShouldReportedCost | ownership)
56 , m_length(length)
57 {
58 ASSERT(isStringImpl());
59 }
60
61 enum StaticStringConstructType { ConstructStaticString };
62 StringImplBase(unsigned length, StaticStringConstructType)
63 : m_refCountAndFlags(s_refCountFlagStatic | s_refCountFlagIsIdentifier | BufferOwned)
64 , m_length(length)
65 {
66 ASSERT(isStringImpl());
67 }
68
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)
74 , m_length(0)
75 {
76 ASSERT(!isStringImpl());
77 }
78
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;
94
95 unsigned m_refCountAndFlags;
96 unsigned m_length;
97 };
98
99 } // namespace WTF
100
101 using WTF::StringImplBase;
102
103 #endif