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
|
/*
* Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#ifdef SKIP_STATIC_CONSTRUCTORS_ON_GCC
#define ATOMICSTRING_HIDE_GLOBALS 1
#endif
#include "AtomicString.h"
#include "StaticConstructors.h"
#include "StringHash.h"
#include "ThreadGlobalData.h"
#include <wtf/Threading.h>
#include <wtf/HashSet.h>
#if USE(JSC)
#include <runtime/Identifier.h>
using JSC::Identifier;
using JSC::UString;
#endif
namespace WebCore {
static inline HashSet<StringImpl*>& stringTable()
{
return threadGlobalData().atomicStringTable();
}
struct CStringTranslator {
static unsigned hash(const char* c)
{
return StringImpl::computeHash(c);
}
static bool equal(StringImpl* r, const char* s)
{
int length = r->length();
const UChar* d = r->characters();
for (int i = 0; i != length; ++i) {
unsigned char c = s[i];
if (d[i] != c)
return false;
}
return s[length] == 0;
}
static void translate(StringImpl*& location, const char* const& c, unsigned hash)
{
location = StringImpl::create(c).releaseRef();
location->setHash(hash);
location->setInTable();
}
};
bool operator==(const AtomicString& a, const char* b)
{
StringImpl* impl = a.impl();
if ((!impl || !impl->characters()) && !b)
return true;
if ((!impl || !impl->characters()) || !b)
return false;
return CStringTranslator::equal(impl, b);
}
PassRefPtr<StringImpl> AtomicString::add(const char* c)
{
if (!c)
return 0;
if (!*c)
return StringImpl::empty();
pair<HashSet<StringImpl*>::iterator, bool> addResult = stringTable().add<const char*, CStringTranslator>(c);
if (!addResult.second)
return *addResult.first;
return adoptRef(*addResult.first);
}
struct UCharBuffer {
const UChar* s;
unsigned length;
};
static inline bool equal(StringImpl* string, const UChar* characters, unsigned length)
{
if (string->length() != length)
return false;
// FIXME: perhaps we should have a more abstract macro that indicates when
// going 4 bytes at a time is unsafe
#if CPU(ARM) || CPU(SH4)
const UChar* stringCharacters = string->characters();
for (unsigned i = 0; i != length; ++i) {
if (*stringCharacters++ != *characters++)
return false;
}
return true;
#else
/* Do it 4-bytes-at-a-time on architectures where it's safe */
const uint32_t* stringCharacters = reinterpret_cast<const uint32_t*>(string->characters());
const uint32_t* bufferCharacters = reinterpret_cast<const uint32_t*>(characters);
unsigned halfLength = length >> 1;
for (unsigned i = 0; i != halfLength; ++i) {
if (*stringCharacters++ != *bufferCharacters++)
return false;
}
if (length & 1 && *reinterpret_cast<const uint16_t*>(stringCharacters) != *reinterpret_cast<const uint16_t*>(bufferCharacters))
return false;
return true;
#endif
}
struct UCharBufferTranslator {
static unsigned hash(const UCharBuffer& buf)
{
return StringImpl::computeHash(buf.s, buf.length);
}
static bool equal(StringImpl* const& str, const UCharBuffer& buf)
{
return WebCore::equal(str, buf.s, buf.length);
}
static void translate(StringImpl*& location, const UCharBuffer& buf, unsigned hash)
{
location = StringImpl::create(buf.s, buf.length).releaseRef();
location->setHash(hash);
location->setInTable();
}
};
struct HashAndCharacters {
unsigned hash;
const UChar* characters;
unsigned length;
};
struct HashAndCharactersTranslator {
static unsigned hash(const HashAndCharacters& buffer)
{
ASSERT(buffer.hash == StringImpl::computeHash(buffer.characters, buffer.length));
return buffer.hash;
}
static bool equal(StringImpl* const& string, const HashAndCharacters& buffer)
{
return WebCore::equal(string, buffer.characters, buffer.length);
}
static void translate(StringImpl*& location, const HashAndCharacters& buffer, unsigned hash)
{
location = StringImpl::create(buffer.characters, buffer.length).releaseRef();
location->setHash(hash);
location->setInTable();
}
};
PassRefPtr<StringImpl> AtomicString::add(const UChar* s, int length)
{
if (!s)
return 0;
if (length == 0)
return StringImpl::empty();
UCharBuffer buf = { s, length };
pair<HashSet<StringImpl*>::iterator, bool> addResult = stringTable().add<UCharBuffer, UCharBufferTranslator>(buf);
// If the string is newly-translated, then we need to adopt it.
// The boolean in the pair tells us if that is so.
return addResult.second ? adoptRef(*addResult.first) : *addResult.first;
}
PassRefPtr<StringImpl> AtomicString::add(const UChar* s)
{
if (!s)
return 0;
int length = 0;
while (s[length] != UChar(0))
length++;
if (length == 0)
return StringImpl::empty();
UCharBuffer buf = {s, length};
pair<HashSet<StringImpl*>::iterator, bool> addResult = stringTable().add<UCharBuffer, UCharBufferTranslator>(buf);
// If the string is newly-translated, then we need to adopt it.
// The boolean in the pair tells us if that is so.
return addResult.second ? adoptRef(*addResult.first) : *addResult.first;
}
PassRefPtr<StringImpl> AtomicString::add(StringImpl* r)
{
if (!r || r->inTable())
return r;
if (r->length() == 0)
return StringImpl::empty();
StringImpl* result = *stringTable().add(r).first;
if (result == r)
r->setInTable();
return result;
}
void AtomicString::remove(StringImpl* r)
{
stringTable().remove(r);
}
AtomicString AtomicString::lower() const
{
// Note: This is a hot function in the Dromaeo benchmark.
StringImpl* impl = this->impl();
RefPtr<StringImpl> newImpl = impl->lower();
if (LIKELY(newImpl == impl))
return *this;
return AtomicString(newImpl);
}
#if USE(JSC)
PassRefPtr<StringImpl> AtomicString::add(const JSC::Identifier& identifier)
{
if (identifier.isNull())
return 0;
UString::Rep* string = identifier.ustring().rep();
unsigned length = string->length();
if (!length)
return StringImpl::empty();
HashAndCharacters buffer = { string->existingHash(), string->data(), length };
pair<HashSet<StringImpl*>::iterator, bool> addResult = stringTable().add<HashAndCharacters, HashAndCharactersTranslator>(buffer);
if (!addResult.second)
return *addResult.first;
return adoptRef(*addResult.first);
}
PassRefPtr<StringImpl> AtomicString::add(const JSC::UString& ustring)
{
if (ustring.isNull())
return 0;
UString::Rep* string = ustring.rep();
unsigned length = string->length();
if (!length)
return StringImpl::empty();
HashAndCharacters buffer = { string->hash(), string->data(), length };
pair<HashSet<StringImpl*>::iterator, bool> addResult = stringTable().add<HashAndCharacters, HashAndCharactersTranslator>(buffer);
if (!addResult.second)
return *addResult.first;
return adoptRef(*addResult.first);
}
AtomicStringImpl* AtomicString::find(const JSC::Identifier& identifier)
{
if (identifier.isNull())
return 0;
UString::Rep* string = identifier.ustring().rep();
unsigned length = string->length();
if (!length)
return static_cast<AtomicStringImpl*>(StringImpl::empty());
HashAndCharacters buffer = { string->existingHash(), string->data(), length };
HashSet<StringImpl*>::iterator iterator = stringTable().find<HashAndCharacters, HashAndCharactersTranslator>(buffer);
if (iterator == stringTable().end())
return 0;
return static_cast<AtomicStringImpl*>(*iterator);
}
AtomicString::operator UString() const
{
return m_string;
}
#endif
DEFINE_GLOBAL(AtomicString, nullAtom)
DEFINE_GLOBAL(AtomicString, emptyAtom, "")
DEFINE_GLOBAL(AtomicString, textAtom, "#text")
DEFINE_GLOBAL(AtomicString, commentAtom, "#comment")
DEFINE_GLOBAL(AtomicString, starAtom, "*")
DEFINE_GLOBAL(AtomicString, xmlAtom, "xml")
DEFINE_GLOBAL(AtomicString, xmlnsAtom, "xmlns")
void AtomicString::init()
{
static bool initialized;
if (!initialized) {
// Initialization is not thread safe, so this function must be called from the main thread first.
ASSERT(isMainThread());
// Use placement new to initialize the globals.
new ((void*)&nullAtom) AtomicString;
new ((void*)&emptyAtom) AtomicString("");
new ((void*)&textAtom) AtomicString("#text");
new ((void*)&commentAtom) AtomicString("#comment");
new ((void*)&starAtom) AtomicString("*");
new ((void*)&xmlAtom) AtomicString("xml");
new ((void*)&xmlnsAtom) AtomicString("xmlns");
initialized = true;
}
}
}
|