OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserv
ed. | 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserv
ed. |
3 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com> | 3 * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com> |
4 * Copyright (C) 2012 Google Inc. All rights reserved. | 4 * Copyright (C) 2012 Google Inc. All rights reserved. |
5 * | 5 * |
6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
10 * | 10 * |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "wtf/text/IntegerToStringConversion.h" | 28 #include "wtf/text/IntegerToStringConversion.h" |
29 #include "wtf/text/StringHash.h" | 29 #include "wtf/text/StringHash.h" |
30 #include "wtf/text/UTF8.h" | 30 #include "wtf/text/UTF8.h" |
31 | 31 |
32 namespace WTF { | 32 namespace WTF { |
33 | 33 |
34 using namespace Unicode; | 34 using namespace Unicode; |
35 | 35 |
36 static_assert(sizeof(AtomicString) == sizeof(String), "AtomicString and String m
ust be same size"); | 36 static_assert(sizeof(AtomicString) == sizeof(String), "AtomicString and String m
ust be same size"); |
37 | 37 |
38 class AtomicStringTable { | |
39 USING_FAST_MALLOC(AtomicStringTable); | |
40 WTF_MAKE_NONCOPYABLE(AtomicStringTable); | |
41 public: | |
42 static AtomicStringTable* create(WTFThreadData& data) | |
43 { | |
44 data.m_atomicStringTable = new AtomicStringTable; | |
45 data.m_atomicStringTableDestructor = AtomicStringTable::destroy; | |
46 data.m_atomicStringTable->addStaticStrings(); | |
47 return data.m_atomicStringTable; | |
48 } | |
49 | |
50 StringImpl* addStringImpl(StringImpl* string) | |
51 { | |
52 if (!string->length()) | |
53 return StringImpl::empty(); | |
54 | |
55 StringImpl* result = *m_table.add(string).storedValue; | |
56 | |
57 if (!result->isAtomic()) | |
58 result->setIsAtomic(true); | |
59 | |
60 ASSERT(!string->isStatic() || result->isStatic()); | |
61 return result; | |
62 } | |
63 | |
64 HashSet<StringImpl*>& table() | |
65 { | |
66 return m_table; | |
67 } | |
68 | |
69 private: | |
70 AtomicStringTable() { } | |
71 | |
72 void addStaticStrings() | |
73 { | |
74 const StaticStringsTable& staticStrings = StringImpl::allStaticStrings()
; | |
75 | |
76 StaticStringsTable::const_iterator it = staticStrings.begin(); | |
77 for (; it != staticStrings.end(); ++it) { | |
78 addStringImpl(it->value); | |
79 } | |
80 } | |
81 | |
82 static void destroy(AtomicStringTable* table) | |
83 { | |
84 HashSet<StringImpl*>::iterator end = table->m_table.end(); | |
85 for (HashSet<StringImpl*>::iterator iter = table->m_table.begin(); iter
!= end; ++iter) { | |
86 StringImpl* string = *iter; | |
87 if (!string->isStatic()) { | |
88 ASSERT(string->isAtomic()); | |
89 string->setIsAtomic(false); | |
90 } | |
91 } | |
92 delete table; | |
93 } | |
94 | |
95 HashSet<StringImpl*> m_table; | |
96 }; | |
97 | |
98 static inline AtomicStringTable& getAtomicStringTable() | |
99 { | |
100 // Once possible we should make this non-lazy (constructed in WTFThreadData'
s constructor). | |
101 WTFThreadData& data = wtfThreadData(); | |
102 AtomicStringTable* table = data.getAtomicStringTable(); | |
103 if (UNLIKELY(!table)) | |
104 table = AtomicStringTable::create(data); | |
105 return *table; | |
106 } | |
107 | |
108 static inline HashSet<StringImpl*>& atomicStrings() | 38 static inline HashSet<StringImpl*>& atomicStrings() |
109 { | 39 { |
110 return getAtomicStringTable().table(); | 40 return wtfThreadData().getAtomicStringTable().table(); |
111 } | 41 } |
112 | 42 |
113 void AtomicString::reserveTableCapacity(size_t size) | 43 void AtomicString::reserveTableCapacity(size_t size) |
114 { | 44 { |
115 getAtomicStringTable().table().reserveCapacityForSize(size); | 45 wtfThreadData().getAtomicStringTable().table().reserveCapacityForSize(size); |
116 } | 46 } |
117 | 47 |
118 template<typename T, typename HashTranslator> | 48 template<typename T, typename HashTranslator> |
119 static inline PassRefPtr<StringImpl> addToStringTable(const T& value) | 49 static inline PassRefPtr<StringImpl> addToStringTable(const T& value) |
120 { | 50 { |
121 HashSet<StringImpl*>::AddResult addResult = atomicStrings().addWithTranslato
r<HashTranslator>(value); | 51 HashSet<StringImpl*>::AddResult addResult = atomicStrings().addWithTranslato
r<HashTranslator>(value); |
122 | 52 |
123 // If the string is newly-translated, then we need to adopt it. | 53 // If the string is newly-translated, then we need to adopt it. |
124 // The boolean in the pair tells us if that is so. | 54 // The boolean in the pair tells us if that is so. |
125 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult.
storedValue; | 55 return addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult.
storedValue; |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 294 |
365 if (!length) | 295 if (!length) |
366 return StringImpl::empty(); | 296 return StringImpl::empty(); |
367 | 297 |
368 LCharBuffer buffer = { s, length }; | 298 LCharBuffer buffer = { s, length }; |
369 return addToStringTable<LCharBuffer, LCharBufferTranslator>(buffer); | 299 return addToStringTable<LCharBuffer, LCharBufferTranslator>(buffer); |
370 } | 300 } |
371 | 301 |
372 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string) | 302 PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* string) |
373 { | 303 { |
374 return getAtomicStringTable().addStringImpl(string); | 304 return wtfThreadData().getAtomicStringTable().addStringImpl(string); |
375 } | 305 } |
376 | 306 |
377 template<typename CharacterType> | 307 template<typename CharacterType> |
378 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string
Impl) | 308 static inline HashSet<StringImpl*>::iterator findString(const StringImpl* string
Impl) |
379 { | 309 { |
380 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri
ngImpl->getCharacters<CharacterType>(), stringImpl->length() }; | 310 HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stri
ngImpl->getCharacters<CharacterType>(), stringImpl->length() }; |
381 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff
er); | 311 return atomicStrings().find<HashAndCharactersTranslator<CharacterType>>(buff
er); |
382 } | 312 } |
383 | 313 |
384 StringImpl* AtomicString::find(const StringImpl* stringImpl) | 314 StringImpl* AtomicString::find(const StringImpl* stringImpl) |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 } | 427 } |
498 | 428 |
499 #ifndef NDEBUG | 429 #ifndef NDEBUG |
500 void AtomicString::show() const | 430 void AtomicString::show() const |
501 { | 431 { |
502 m_string.show(); | 432 m_string.show(); |
503 } | 433 } |
504 #endif | 434 #endif |
505 | 435 |
506 } // namespace WTF | 436 } // namespace WTF |
OLD | NEW |