/
UStringImpl.h
364 lines (306 loc) · 13.6 KB
/
UStringImpl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright (C) 2009 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef UStringImpl_h
#define UStringImpl_h
#include <limits>
#include <wtf/CrossThreadRefCounted.h>
#include <wtf/OwnFastMallocPtr.h>
#include <wtf/PossiblyNull.h>
#include <wtf/StringHashFunctions.h>
#include <wtf/Vector.h>
#include <wtf/unicode/Unicode.h>
namespace JSC {
class IdentifierTable;
typedef CrossThreadRefCounted<OwnFastMallocPtr<UChar> > SharedUChar;
class UStringOrRopeImpl : public Noncopyable {
public:
bool isRope() { return (m_refCountAndFlags & s_refCountIsRope) == s_refCountIsRope; }
unsigned length() const { return m_length; }
void ref() { m_refCountAndFlags += s_refCountIncrement; }
inline void deref();
protected:
enum BufferOwnership {
BufferInternal,
BufferOwned,
BufferSubstring,
BufferShared,
};
using Noncopyable::operator new;
void* operator new(size_t, void* inPlace) { return inPlace; }
// For SmallStringStorage, which allocates an array and uses an in-place new.
UStringOrRopeImpl() { }
UStringOrRopeImpl(unsigned length, BufferOwnership ownership)
: m_refCountAndFlags(s_refCountIncrement | s_refCountFlagShouldReportedCost | ownership)
, m_length(length)
{
ASSERT(!isRope());
}
enum StaticStringConstructType { ConstructStaticString };
UStringOrRopeImpl(unsigned length, StaticStringConstructType)
: m_refCountAndFlags(s_refCountFlagStatic | BufferOwned)
, m_length(length)
{
ASSERT(!isRope());
}
enum RopeConstructType { ConstructRope };
UStringOrRopeImpl(RopeConstructType)
: m_refCountAndFlags(s_refCountIncrement | s_refCountIsRope)
, m_length(0)
{
ASSERT(isRope());
}
// The bottom 5 bits hold flags, the top 27 bits hold the ref count.
// When dereferencing UStringImpls we check for the ref count AND the
// static bit both being zero - static strings are never deleted.
static const unsigned s_refCountMask = 0xFFFFFFE0;
static const unsigned s_refCountIncrement = 0x20;
static const unsigned s_refCountFlagStatic = 0x10;
static const unsigned s_refCountFlagShouldReportedCost = 0x8;
static const unsigned s_refCountFlagIsIdentifier = 0x4;
static const unsigned s_refCountMaskBufferOwnership = 0x3;
// Use an otherwise invalid permutation of flags (static & shouldReportedCost -
// static strings do not set shouldReportedCost in the constructor, and this bit
// is only ever cleared, not set) to identify objects that are ropes.
static const unsigned s_refCountIsRope = s_refCountFlagStatic | s_refCountFlagShouldReportedCost;
unsigned m_refCountAndFlags;
unsigned m_length;
};
class UStringImpl : public UStringOrRopeImpl {
public:
template<size_t inlineCapacity>
static PassRefPtr<UStringImpl> adopt(Vector<UChar, inlineCapacity>& vector)
{
if (unsigned length = vector.size()) {
ASSERT(vector.data());
return adoptRef(new UStringImpl(vector.releaseBuffer(), length));
}
return &empty();
}
static PassRefPtr<UStringImpl> create(const char* c);
static PassRefPtr<UStringImpl> create(const char* c, unsigned length);
static PassRefPtr<UStringImpl> create(const UChar* buffer, unsigned length);
static PassRefPtr<UStringImpl> create(PassRefPtr<UStringImpl> rep, unsigned offset, unsigned length)
{
ASSERT(rep);
rep->checkConsistency();
return adoptRef(new UStringImpl(rep->m_data + offset, length, rep->bufferOwnerString()));
}
static PassRefPtr<UStringImpl> create(PassRefPtr<SharedUChar> sharedBuffer, UChar* buffer, unsigned length)
{
return adoptRef(new UStringImpl(buffer, length, sharedBuffer));
}
static PassRefPtr<UStringImpl> createUninitialized(unsigned length, UChar*& output)
{
if (!length) {
output = 0;
return &empty();
}
if (length > ((std::numeric_limits<size_t>::max() - sizeof(UStringImpl)) / sizeof(UChar)))
CRASH();
UStringImpl* resultImpl = static_cast<UStringImpl*>(fastMalloc(sizeof(UChar) * length + sizeof(UStringImpl)));
output = reinterpret_cast<UChar*>(resultImpl + 1);
return adoptRef(new(resultImpl) UStringImpl(length));
}
static PassRefPtr<UStringImpl> tryCreateUninitialized(unsigned length, UChar*& output)
{
if (!length) {
output = 0;
return &empty();
}
if (length > ((std::numeric_limits<size_t>::max() - sizeof(UStringImpl)) / sizeof(UChar)))
return 0;
UStringImpl* resultImpl;
if (!tryFastMalloc(sizeof(UChar) * length + sizeof(UStringImpl)).getValue(resultImpl))
return 0;
output = reinterpret_cast<UChar*>(resultImpl + 1);
return adoptRef(new(resultImpl) UStringImpl(length));
}
SharedUChar* sharedBuffer();
UChar* data() const { return m_data; }
size_t cost()
{
// For substrings, return the cost of the base string.
if (bufferOwnership() == BufferSubstring)
return m_bufferSubstring->cost();
if (m_refCountAndFlags & s_refCountFlagShouldReportedCost) {
m_refCountAndFlags &= ~s_refCountFlagShouldReportedCost;
return m_length;
}
return 0;
}
unsigned hash() const { if (!m_hash) m_hash = computeHash(data(), m_length); return m_hash; }
unsigned existingHash() const { ASSERT(m_hash); return m_hash; } // fast path for Identifiers
void setHash(unsigned hash) { ASSERT(hash == computeHash(data(), m_length)); m_hash = hash; } // fast path for Identifiers
bool isIdentifier() const { return m_refCountAndFlags & s_refCountFlagIsIdentifier; }
void setIsIdentifier(bool isIdentifier)
{
if (isIdentifier)
m_refCountAndFlags |= s_refCountFlagIsIdentifier;
else
m_refCountAndFlags &= ~s_refCountFlagIsIdentifier;
}
ALWAYS_INLINE void deref() { m_refCountAndFlags -= s_refCountIncrement; if (!(m_refCountAndFlags & (s_refCountMask | s_refCountFlagStatic))) delete this; }
static void copyChars(UChar* destination, const UChar* source, unsigned numCharacters)
{
if (numCharacters <= s_copyCharsInlineCutOff) {
for (unsigned i = 0; i < numCharacters; ++i)
destination[i] = source[i];
} else
memcpy(destination, source, numCharacters * sizeof(UChar));
}
static unsigned computeHash(const UChar* s, unsigned length) { return WTF::stringHash(s, length); }
static unsigned computeHash(const char* s, unsigned length) { return WTF::stringHash(s, length); }
static unsigned computeHash(const char* s) { return WTF::stringHash(s); }
static UStringImpl& empty() { return *s_empty; }
ALWAYS_INLINE void checkConsistency() const
{
// There is no recursion of substrings.
ASSERT(bufferOwnerString()->bufferOwnership() != BufferSubstring);
// Static strings cannot be put in identifier tables, because they are globally shared.
ASSERT(!isStatic() || !isIdentifier());
}
PassRefPtr<UStringImpl> singleCharacterSubstring(unsigned index) { ASSERT(index < length()); return create(this, index, 1); }
private:
// For SmallStringStorage, which allocates an array and uses an in-place new.
UStringImpl() { }
// Used to construct normal strings with an internal buffer.
UStringImpl(unsigned length)
: UStringOrRopeImpl(length, BufferInternal)
, m_data(reinterpret_cast<UChar*>(this + 1))
, m_buffer(0)
, m_hash(0)
{
checkConsistency();
}
// Used to construct normal strings with an external buffer.
UStringImpl(UChar* data, unsigned length)
: UStringOrRopeImpl(length, BufferOwned)
, m_data(data)
, m_buffer(0)
, m_hash(0)
{
checkConsistency();
}
// Used to construct static strings, which have an special refCount that can never hit zero.
// This means that the static string will never be destroyed, which is important because
// static strings will be shared across threads & ref-counted in a non-threadsafe manner.
UStringImpl(UChar* data, unsigned length, StaticStringConstructType)
: UStringOrRopeImpl(length, ConstructStaticString)
, m_data(data)
, m_buffer(0)
, m_hash(0)
{
checkConsistency();
}
// Used to create new strings that are a substring of an existing string.
UStringImpl(UChar* data, unsigned length, PassRefPtr<UStringImpl> base)
: UStringOrRopeImpl(length, BufferSubstring)
, m_data(data)
, m_bufferSubstring(base.releaseRef())
, m_hash(0)
{
// Do use static strings as a base for substrings; UntypedPtrAndBitfield assumes
// that all pointers will be at least 8-byte aligned, we cannot guarantee that of
// UStringImpls that are not heap allocated.
ASSERT(m_bufferSubstring->length());
ASSERT(!m_bufferSubstring->isStatic());
checkConsistency();
}
// Used to construct new strings sharing an existing shared buffer.
UStringImpl(UChar* data, unsigned length, PassRefPtr<SharedUChar> sharedBuffer)
: UStringOrRopeImpl(length, BufferShared)
, m_data(data)
, m_bufferShared(sharedBuffer.releaseRef())
, m_hash(0)
{
checkConsistency();
}
~UStringImpl();
// This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
static const unsigned s_minLengthToShare = 10;
static const unsigned s_copyCharsInlineCutOff = 20;
UStringImpl* bufferOwnerString() { return (bufferOwnership() == BufferSubstring) ? m_bufferSubstring : this; }
const UStringImpl* bufferOwnerString() const { return (bufferOwnership() == BufferSubstring) ? m_bufferSubstring : this; }
SharedUChar* baseSharedBuffer();
unsigned bufferOwnership() const { return m_refCountAndFlags & s_refCountMaskBufferOwnership; }
bool isStatic() const { return m_refCountAndFlags & s_refCountFlagStatic; }
// unshared data
UChar* m_data;
union {
void* m_buffer;
UStringImpl* m_bufferSubstring;
SharedUChar* m_bufferShared;
};
mutable unsigned m_hash;
JS_EXPORTDATA static UStringImpl* s_empty;
friend class JIT;
friend class SmallStringsStorage;
friend class UStringOrRopeImpl;
friend void initializeUString();
};
class URopeImpl : public UStringOrRopeImpl {
public:
// A URopeImpl is composed from a set of smaller strings called Fibers.
// Each Fiber in a rope is either UStringImpl or another URopeImpl.
typedef UStringOrRopeImpl* Fiber;
// Creates a URopeImpl comprising of 'fiberCount' Fibers.
// The URopeImpl is constructed in an uninitialized state - initialize must be called for each Fiber in the URopeImpl.
static PassRefPtr<URopeImpl> tryCreateUninitialized(unsigned fiberCount)
{
void* allocation;
if (tryFastMalloc(sizeof(URopeImpl) + (fiberCount - 1) * sizeof(Fiber)).getValue(allocation))
return adoptRef(new (allocation) URopeImpl(fiberCount));
return 0;
}
void initializeFiber(unsigned &index, Fiber fiber)
{
m_fibers[index++] = fiber;
fiber->ref();
m_length += fiber->length();
}
unsigned fiberCount() { return m_fiberCount; }
Fiber& fibers(unsigned index) { return m_fibers[index]; }
ALWAYS_INLINE void deref() { m_refCountAndFlags -= s_refCountIncrement; if (!(m_refCountAndFlags & s_refCountMask)) destructNonRecursive(); }
private:
URopeImpl(unsigned fiberCount) : UStringOrRopeImpl(ConstructRope), m_fiberCount(fiberCount) {}
void destructNonRecursive();
void derefFibersNonRecursive(Vector<URopeImpl*, 32>& workQueue);
bool hasOneRef() { return (m_refCountAndFlags & s_refCountMask) == s_refCountIncrement; }
unsigned m_fiberCount;
Fiber m_fibers[1];
friend class UStringOrRopeImpl;
friend PassRefPtr<UStringImpl> singleCharacterSubstring(UStringOrRopeImpl* ropeoid, unsigned index);
};
inline void UStringOrRopeImpl::deref()
{
if (isRope())
static_cast<URopeImpl*>(this)->deref();
else
static_cast<UStringImpl*>(this)->deref();
}
bool equal(const UStringImpl*, const UStringImpl*);
PassRefPtr<UStringImpl> singleCharacterSubstring(UStringOrRopeImpl* ropeoid, unsigned index);
}
#endif