1 | /*
|
---|
2 | * Copyright (C) 2009 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 UStringImpl_h
|
---|
27 | #define UStringImpl_h
|
---|
28 |
|
---|
29 | #include <limits>
|
---|
30 | #include <wtf/CrossThreadRefCounted.h>
|
---|
31 | #include <wtf/OwnFastMallocPtr.h>
|
---|
32 | #include <wtf/PossiblyNull.h>
|
---|
33 | #include <wtf/StringHashFunctions.h>
|
---|
34 | #include <wtf/Vector.h>
|
---|
35 | #include <wtf/unicode/Unicode.h>
|
---|
36 |
|
---|
37 | namespace JSC {
|
---|
38 |
|
---|
39 | class IdentifierTable;
|
---|
40 |
|
---|
41 | typedef OwnFastMallocPtr<const UChar> SharableUChar;
|
---|
42 | typedef CrossThreadRefCounted<SharableUChar> SharedUChar;
|
---|
43 |
|
---|
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 {
|
---|
109 | friend struct CStringTranslator;
|
---|
110 | friend struct UCharBufferTranslator;
|
---|
111 | friend class JIT;
|
---|
112 | friend class SmallStringsStorage;
|
---|
113 | friend void initializeUString();
|
---|
114 | private:
|
---|
115 | // For SmallStringStorage, which allocates an array and uses an in-place new.
|
---|
116 | UStringImpl() { }
|
---|
117 |
|
---|
118 | // Used to construct static strings, which have an special refCount that can never hit zero.
|
---|
119 | // This means that the static string will never be destroyed, which is important because
|
---|
120 | // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
|
---|
121 | UStringImpl(const UChar* characters, unsigned length, StaticStringConstructType)
|
---|
122 | : UStringImplBase(length, ConstructStaticString)
|
---|
123 | , m_data(characters)
|
---|
124 | , m_buffer(0)
|
---|
125 | , m_hash(0)
|
---|
126 | {
|
---|
127 | hash();
|
---|
128 | }
|
---|
129 |
|
---|
130 | // Create a normal string with internal storage (BufferInternal)
|
---|
131 | UStringImpl(unsigned length)
|
---|
132 | : UStringImplBase(length, BufferInternal)
|
---|
133 | , m_data(reinterpret_cast<UChar*>(this + 1))
|
---|
134 | , m_buffer(0)
|
---|
135 | , m_hash(0)
|
---|
136 | {
|
---|
137 | ASSERT(m_data);
|
---|
138 | ASSERT(m_length);
|
---|
139 | }
|
---|
140 |
|
---|
141 | // Create a UStringImpl adopting ownership of the provided buffer (BufferOwned)
|
---|
142 | UStringImpl(const UChar* characters, unsigned length)
|
---|
143 | : UStringImplBase(length, BufferOwned)
|
---|
144 | , m_data(characters)
|
---|
145 | , m_buffer(0)
|
---|
146 | , m_hash(0)
|
---|
147 | {
|
---|
148 | ASSERT(m_data);
|
---|
149 | ASSERT(m_length);
|
---|
150 | }
|
---|
151 |
|
---|
152 | // Used to create new strings that are a substring of an existing UStringImpl (BufferSubstring)
|
---|
153 | UStringImpl(const UChar* characters, unsigned length, PassRefPtr<UStringImpl> base)
|
---|
154 | : UStringImplBase(length, BufferSubstring)
|
---|
155 | , m_data(characters)
|
---|
156 | , m_substringBuffer(base.releaseRef())
|
---|
157 | , m_hash(0)
|
---|
158 | {
|
---|
159 | ASSERT(m_data);
|
---|
160 | ASSERT(m_length);
|
---|
161 | ASSERT(m_substringBuffer->bufferOwnership() != BufferSubstring);
|
---|
162 | }
|
---|
163 |
|
---|
164 | // Used to construct new strings sharing an existing SharedUChar (BufferShared)
|
---|
165 | UStringImpl(const UChar* characters, unsigned length, PassRefPtr<SharedUChar> sharedBuffer)
|
---|
166 | : UStringImplBase(length, BufferShared)
|
---|
167 | , m_data(characters)
|
---|
168 | , m_sharedBuffer(sharedBuffer.releaseRef())
|
---|
169 | , m_hash(0)
|
---|
170 | {
|
---|
171 | ASSERT(m_data);
|
---|
172 | ASSERT(m_length);
|
---|
173 | }
|
---|
174 |
|
---|
175 | // For use only by Identifier's XXXTranslator helpers.
|
---|
176 | void setHash(unsigned hash)
|
---|
177 | {
|
---|
178 | ASSERT(!isStatic());
|
---|
179 | ASSERT(!m_hash);
|
---|
180 | ASSERT(hash == computeHash(m_data, m_length));
|
---|
181 | m_hash = hash;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public:
|
---|
185 | ~UStringImpl();
|
---|
186 |
|
---|
187 | static PassRefPtr<UStringImpl> create(const UChar*, unsigned length);
|
---|
188 | static PassRefPtr<UStringImpl> create(const char*, unsigned length);
|
---|
189 | static PassRefPtr<UStringImpl> create(const char*);
|
---|
190 | static PassRefPtr<UStringImpl> create(const UChar*, unsigned length, PassRefPtr<SharedUChar>);
|
---|
191 | static PassRefPtr<UStringImpl> create(PassRefPtr<UStringImpl> rep, unsigned offset, unsigned length)
|
---|
192 | {
|
---|
193 | ASSERT(rep);
|
---|
194 | ASSERT(length <= rep->length());
|
---|
195 |
|
---|
196 | if (!length)
|
---|
197 | return empty();
|
---|
198 |
|
---|
199 | UStringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->m_substringBuffer : rep.get();
|
---|
200 | return adoptRef(new UStringImpl(rep->m_data + offset, length, ownerRep));
|
---|
201 | }
|
---|
202 |
|
---|
203 | static PassRefPtr<UStringImpl> createUninitialized(unsigned length, UChar*& output);
|
---|
204 | static PassRefPtr<UStringImpl> tryCreateUninitialized(unsigned length, UChar*& output)
|
---|
205 | {
|
---|
206 | if (!length) {
|
---|
207 | output = 0;
|
---|
208 | return empty();
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (length > ((std::numeric_limits<size_t>::max() - sizeof(UStringImpl)) / sizeof(UChar)))
|
---|
212 | return 0;
|
---|
213 | UStringImpl* resultImpl;
|
---|
214 | if (!tryFastMalloc(sizeof(UChar) * length + sizeof(UStringImpl)).getValue(resultImpl))
|
---|
215 | return 0;
|
---|
216 | output = reinterpret_cast<UChar*>(resultImpl + 1);
|
---|
217 | return adoptRef(new(resultImpl) UStringImpl(length));
|
---|
218 | }
|
---|
219 |
|
---|
220 | template<size_t inlineCapacity>
|
---|
221 | static PassRefPtr<UStringImpl> adopt(Vector<UChar, inlineCapacity>& vector)
|
---|
222 | {
|
---|
223 | if (size_t size = vector.size()) {
|
---|
224 | ASSERT(vector.data());
|
---|
225 | return adoptRef(new UStringImpl(vector.releaseBuffer(), size));
|
---|
226 | }
|
---|
227 | return empty();
|
---|
228 | }
|
---|
229 |
|
---|
230 | SharedUChar* sharedBuffer();
|
---|
231 | const UChar* characters() const { return m_data; }
|
---|
232 |
|
---|
233 | size_t cost()
|
---|
234 | {
|
---|
235 | // For substrings, return the cost of the base string.
|
---|
236 | if (bufferOwnership() == BufferSubstring)
|
---|
237 | return m_substringBuffer->cost();
|
---|
238 |
|
---|
239 | if (m_refCountAndFlags & s_refCountFlagShouldReportedCost) {
|
---|
240 | m_refCountAndFlags &= ~s_refCountFlagShouldReportedCost;
|
---|
241 | return m_length;
|
---|
242 | }
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | bool isIdentifier() const { return m_refCountAndFlags & s_refCountFlagIsIdentifier; }
|
---|
247 | void setIsIdentifier(bool isIdentifier)
|
---|
248 | {
|
---|
249 | ASSERT(!isStatic());
|
---|
250 | if (isIdentifier)
|
---|
251 | m_refCountAndFlags |= s_refCountFlagIsIdentifier;
|
---|
252 | else
|
---|
253 | m_refCountAndFlags &= ~s_refCountFlagIsIdentifier;
|
---|
254 | }
|
---|
255 |
|
---|
256 | unsigned hash() const { if (!m_hash) m_hash = computeHash(m_data, m_length); return m_hash; }
|
---|
257 | unsigned existingHash() const { ASSERT(m_hash); return m_hash; }
|
---|
258 | static unsigned computeHash(const UChar* data, unsigned length) { return WTF::stringHash(data, length); }
|
---|
259 | static unsigned computeHash(const char* data, unsigned length) { return WTF::stringHash(data, length); }
|
---|
260 | static unsigned computeHash(const char* data) { return WTF::stringHash(data); }
|
---|
261 |
|
---|
262 | ALWAYS_INLINE void deref() { m_refCountAndFlags -= s_refCountIncrement; if (!(m_refCountAndFlags & (s_refCountMask | s_refCountFlagStatic))) delete this; }
|
---|
263 |
|
---|
264 | static UStringImpl* empty();
|
---|
265 |
|
---|
266 | static void copyChars(UChar* destination, const UChar* source, unsigned numCharacters)
|
---|
267 | {
|
---|
268 | if (numCharacters <= s_copyCharsInlineCutOff) {
|
---|
269 | for (unsigned i = 0; i < numCharacters; ++i)
|
---|
270 | destination[i] = source[i];
|
---|
271 | } else
|
---|
272 | memcpy(destination, source, numCharacters * sizeof(UChar));
|
---|
273 | }
|
---|
274 |
|
---|
275 | private:
|
---|
276 | // This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
|
---|
277 | static const unsigned s_copyCharsInlineCutOff = 20;
|
---|
278 |
|
---|
279 | BufferOwnership bufferOwnership() const { return static_cast<BufferOwnership>(m_refCountAndFlags & s_refCountMaskBufferOwnership); }
|
---|
280 | bool isStatic() const { return m_refCountAndFlags & s_refCountFlagStatic; }
|
---|
281 |
|
---|
282 | const UChar* m_data;
|
---|
283 | union {
|
---|
284 | void* m_buffer;
|
---|
285 | UStringImpl* m_substringBuffer;
|
---|
286 | SharedUChar* m_sharedBuffer;
|
---|
287 | };
|
---|
288 | mutable unsigned m_hash;
|
---|
289 | };
|
---|
290 |
|
---|
291 | bool equal(const UStringImpl*, const UStringImpl*);
|
---|
292 |
|
---|
293 | }
|
---|
294 |
|
---|
295 | #endif
|
---|