AtomicString.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
  3. * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
  4. * Copyright (C) 2012 Google Inc. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this library; see the file COPYING.LIB. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. *
  21. */
  22. #include "config.h"
  23. #include "AtomicString.h"
  24. #include "AtomicStringTable.h"
  25. #include "StringHash.h"
  26. #include <wtf/HashSet.h>
  27. #include <wtf/Threading.h>
  28. #include <wtf/WTFThreadData.h>
  29. #include <wtf/unicode/UTF8.h>
  30. #if USE(WEB_THREAD)
  31. #include <wtf/MainThread.h>
  32. #include <wtf/TCSpinLock.h>
  33. #endif
  34. namespace WTF {
  35. using namespace Unicode;
  36. COMPILE_ASSERT(sizeof(AtomicString) == sizeof(String), atomic_string_and_string_must_be_same_size);
  37. #if USE(WEB_THREAD)
  38. class AtomicStringTableLocker : public SpinLockHolder {
  39. WTF_MAKE_NONCOPYABLE(AtomicStringTableLocker);
  40. static SpinLock s_stringTableLock;
  41. public:
  42. AtomicStringTableLocker()
  43. : SpinLockHolder(&s_stringTableLock)
  44. {
  45. }
  46. };
  47. SpinLock AtomicStringTableLocker::s_stringTableLock = SPINLOCK_INITIALIZER;
  48. #else
  49. class AtomicStringTableLocker {
  50. WTF_MAKE_NONCOPYABLE(AtomicStringTableLocker);
  51. public:
  52. AtomicStringTableLocker() { }
  53. ~AtomicStringTableLocker() { }
  54. };
  55. #endif // USE(WEB_THREAD)
  56. static ALWAYS_INLINE HashSet<StringImpl*>& stringTable()
  57. {
  58. return wtfThreadData().atomicStringTable()->table();
  59. }
  60. template<typename T, typename HashTranslator>
  61. static inline PassRefPtr<StringImpl> addToStringTable(const T& value)
  62. {
  63. AtomicStringTableLocker locker;
  64. HashSet<StringImpl*>::AddResult addResult = stringTable().add<T, HashTranslator>(value);
  65. // If the string is newly-translated, then we need to adopt it.
  66. // The boolean in the pair tells us if that is so.
  67. return addResult.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator;
  68. }
  69. struct CStringTranslator {
  70. static unsigned hash(const LChar* c)
  71. {
  72. return StringHasher::computeHashAndMaskTop8Bits(c);
  73. }
  74. static inline bool equal(StringImpl* r, const LChar* s)
  75. {
  76. return WTF::equal(r, s);
  77. }
  78. static void translate(StringImpl*& location, const LChar* const& c, unsigned hash)
  79. {
  80. location = StringImpl::create(c).leakRef();
  81. location->setHash(hash);
  82. location->setIsAtomic(true);
  83. }
  84. };
  85. PassRefPtr<StringImpl> AtomicString::add(const LChar* c)
  86. {
  87. if (!c)
  88. return 0;
  89. if (!*c)
  90. return StringImpl::empty();
  91. return addToStringTable<const LChar*, CStringTranslator>(c);
  92. }
  93. template<typename CharacterType>
  94. struct HashTranslatorCharBuffer {
  95. const CharacterType* s;
  96. unsigned length;
  97. };
  98. typedef HashTranslatorCharBuffer<UChar> UCharBuffer;
  99. struct UCharBufferTranslator {
  100. static unsigned hash(const UCharBuffer& buf)
  101. {
  102. return StringHasher::computeHashAndMaskTop8Bits(buf.s, buf.length);
  103. }
  104. static bool equal(StringImpl* const& str, const UCharBuffer& buf)
  105. {
  106. return WTF::equal(str, buf.s, buf.length);
  107. }
  108. static void translate(StringImpl*& location, const UCharBuffer& buf, unsigned hash)
  109. {
  110. location = StringImpl::create8BitIfPossible(buf.s, buf.length).leakRef();
  111. location->setHash(hash);
  112. location->setIsAtomic(true);
  113. }
  114. };
  115. template<typename CharacterType>
  116. struct HashAndCharacters {
  117. unsigned hash;
  118. const CharacterType* characters;
  119. unsigned length;
  120. };
  121. template<typename CharacterType>
  122. struct HashAndCharactersTranslator {
  123. static unsigned hash(const HashAndCharacters<CharacterType>& buffer)
  124. {
  125. ASSERT(buffer.hash == StringHasher::computeHashAndMaskTop8Bits(buffer.characters, buffer.length));
  126. return buffer.hash;
  127. }
  128. static bool equal(StringImpl* const& string, const HashAndCharacters<CharacterType>& buffer)
  129. {
  130. return WTF::equal(string, buffer.characters, buffer.length);
  131. }
  132. static void translate(StringImpl*& location, const HashAndCharacters<CharacterType>& buffer, unsigned hash)
  133. {
  134. location = StringImpl::create(buffer.characters, buffer.length).leakRef();
  135. location->setHash(hash);
  136. location->setIsAtomic(true);
  137. }
  138. };
  139. struct HashAndUTF8Characters {
  140. unsigned hash;
  141. const char* characters;
  142. unsigned length;
  143. unsigned utf16Length;
  144. };
  145. struct HashAndUTF8CharactersTranslator {
  146. static unsigned hash(const HashAndUTF8Characters& buffer)
  147. {
  148. return buffer.hash;
  149. }
  150. static bool equal(StringImpl* const& string, const HashAndUTF8Characters& buffer)
  151. {
  152. if (buffer.utf16Length != string->length())
  153. return false;
  154. // If buffer contains only ASCII characters UTF-8 and UTF16 length are the same.
  155. if (buffer.utf16Length != buffer.length) {
  156. const UChar* stringCharacters = string->characters();
  157. return equalUTF16WithUTF8(stringCharacters, stringCharacters + string->length(), buffer.characters, buffer.characters + buffer.length);
  158. }
  159. if (string->is8Bit()) {
  160. const LChar* stringCharacters = string->characters8();
  161. for (unsigned i = 0; i < buffer.length; ++i) {
  162. ASSERT(isASCII(buffer.characters[i]));
  163. if (stringCharacters[i] != buffer.characters[i])
  164. return false;
  165. }
  166. return true;
  167. }
  168. const UChar* stringCharacters = string->characters16();
  169. for (unsigned i = 0; i < buffer.length; ++i) {
  170. ASSERT(isASCII(buffer.characters[i]));
  171. if (stringCharacters[i] != buffer.characters[i])
  172. return false;
  173. }
  174. return true;
  175. }
  176. static void translate(StringImpl*& location, const HashAndUTF8Characters& buffer, unsigned hash)
  177. {
  178. UChar* target;
  179. RefPtr<StringImpl> newString = StringImpl::createUninitialized(buffer.utf16Length, target);
  180. bool isAllASCII;
  181. const char* source = buffer.characters;
  182. if (convertUTF8ToUTF16(&source, source + buffer.length, &target, target + buffer.utf16Length, &isAllASCII) != conversionOK)
  183. ASSERT_NOT_REACHED();
  184. if (isAllASCII)
  185. newString = StringImpl::create(buffer.characters, buffer.length);
  186. location = newString.release().leakRef();
  187. location->setHash(hash);
  188. location->setIsAtomic(true);
  189. }
  190. };
  191. PassRefPtr<StringImpl> AtomicString::add(const UChar* s, unsigned length)
  192. {
  193. if (!s)
  194. return 0;
  195. if (!length)
  196. return StringImpl::empty();
  197. UCharBuffer buffer = { s, length };
  198. return addToStringTable<UCharBuffer, UCharBufferTranslator>(buffer);
  199. }
  200. PassRefPtr<StringImpl> AtomicString::add(const UChar* s, unsigned length, unsigned existingHash)
  201. {
  202. ASSERT(s);
  203. ASSERT(existingHash);
  204. if (!length)
  205. return StringImpl::empty();
  206. HashAndCharacters<UChar> buffer = { existingHash, s, length };
  207. return addToStringTable<HashAndCharacters<UChar>, HashAndCharactersTranslator<UChar> >(buffer);
  208. }
  209. PassRefPtr<StringImpl> AtomicString::add(const UChar* s)
  210. {
  211. if (!s)
  212. return 0;
  213. unsigned length = 0;
  214. while (s[length] != UChar(0))
  215. ++length;
  216. if (!length)
  217. return StringImpl::empty();
  218. UCharBuffer buffer = { s, length };
  219. return addToStringTable<UCharBuffer, UCharBufferTranslator>(buffer);
  220. }
  221. struct SubstringLocation {
  222. StringImpl* baseString;
  223. unsigned start;
  224. unsigned length;
  225. };
  226. struct SubstringTranslator {
  227. static unsigned hash(const SubstringLocation& buffer)
  228. {
  229. return StringHasher::computeHashAndMaskTop8Bits(buffer.baseString->characters() + buffer.start, buffer.length);
  230. }
  231. static bool equal(StringImpl* const& string, const SubstringLocation& buffer)
  232. {
  233. return WTF::equal(string, buffer.baseString->characters() + buffer.start, buffer.length);
  234. }
  235. static void translate(StringImpl*& location, const SubstringLocation& buffer, unsigned hash)
  236. {
  237. location = StringImpl::create(buffer.baseString, buffer.start, buffer.length).leakRef();
  238. location->setHash(hash);
  239. location->setIsAtomic(true);
  240. }
  241. };
  242. PassRefPtr<StringImpl> AtomicString::add(StringImpl* baseString, unsigned start, unsigned length)
  243. {
  244. if (!baseString)
  245. return 0;
  246. if (!length || start >= baseString->length())
  247. return StringImpl::empty();
  248. unsigned maxLength = baseString->length() - start;
  249. if (length >= maxLength) {
  250. if (!start)
  251. return add(baseString);
  252. length = maxLength;
  253. }
  254. SubstringLocation buffer = { baseString, start, length };
  255. return addToStringTable<SubstringLocation, SubstringTranslator>(buffer);
  256. }
  257. typedef HashTranslatorCharBuffer<LChar> LCharBuffer;
  258. struct LCharBufferTranslator {
  259. static unsigned hash(const LCharBuffer& buf)
  260. {
  261. return StringHasher::computeHashAndMaskTop8Bits(buf.s, buf.length);
  262. }
  263. static bool equal(StringImpl* const& str, const LCharBuffer& buf)
  264. {
  265. return WTF::equal(str, buf.s, buf.length);
  266. }
  267. static void translate(StringImpl*& location, const LCharBuffer& buf, unsigned hash)
  268. {
  269. location = StringImpl::create(buf.s, buf.length).leakRef();
  270. location->setHash(hash);
  271. location->setIsAtomic(true);
  272. }
  273. };
  274. typedef HashTranslatorCharBuffer<char> CharBuffer;
  275. struct CharBufferFromLiteralDataTranslator {
  276. static unsigned hash(const CharBuffer& buf)
  277. {
  278. return StringHasher::computeHashAndMaskTop8Bits(reinterpret_cast<const LChar*>(buf.s), buf.length);
  279. }
  280. static bool equal(StringImpl* const& str, const CharBuffer& buf)
  281. {
  282. return WTF::equal(str, buf.s, buf.length);
  283. }
  284. static void translate(StringImpl*& location, const CharBuffer& buf, unsigned hash)
  285. {
  286. location = StringImpl::createFromLiteral(buf.s, buf.length).leakRef();
  287. location->setHash(hash);
  288. location->setIsAtomic(true);
  289. }
  290. };
  291. PassRefPtr<StringImpl> AtomicString::add(const LChar* s, unsigned length)
  292. {
  293. if (!s)
  294. return 0;
  295. if (!length)
  296. return StringImpl::empty();
  297. LCharBuffer buffer = { s, length };
  298. return addToStringTable<LCharBuffer, LCharBufferTranslator>(buffer);
  299. }
  300. PassRefPtr<StringImpl> AtomicString::addFromLiteralData(const char* characters, unsigned length)
  301. {
  302. ASSERT(characters);
  303. ASSERT(length);
  304. CharBuffer buffer = { characters, length };
  305. return addToStringTable<CharBuffer, CharBufferFromLiteralDataTranslator>(buffer);
  306. }
  307. PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* r)
  308. {
  309. if (!r->length())
  310. return StringImpl::empty();
  311. AtomicStringTableLocker locker;
  312. StringImpl* result = *stringTable().add(r).iterator;
  313. if (result == r)
  314. r->setIsAtomic(true);
  315. return result;
  316. }
  317. template<typename CharacterType>
  318. static inline HashSet<StringImpl*>::iterator findString(const StringImpl* stringImpl)
  319. {
  320. HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stringImpl->getCharacters<CharacterType>(), stringImpl->length() };
  321. return stringTable().find<HashAndCharacters<CharacterType>, HashAndCharactersTranslator<CharacterType> >(buffer);
  322. }
  323. AtomicStringImpl* AtomicString::find(const StringImpl* stringImpl)
  324. {
  325. ASSERT(stringImpl);
  326. ASSERT(stringImpl->existingHash());
  327. if (!stringImpl->length())
  328. return static_cast<AtomicStringImpl*>(StringImpl::empty());
  329. AtomicStringTableLocker locker;
  330. HashSet<StringImpl*>::iterator iterator;
  331. if (stringImpl->is8Bit())
  332. iterator = findString<LChar>(stringImpl);
  333. else
  334. iterator = findString<UChar>(stringImpl);
  335. if (iterator == stringTable().end())
  336. return 0;
  337. return static_cast<AtomicStringImpl*>(*iterator);
  338. }
  339. void AtomicString::remove(StringImpl* r)
  340. {
  341. AtomicStringTableLocker locker;
  342. stringTable().remove(r);
  343. }
  344. AtomicString AtomicString::lower() const
  345. {
  346. // Note: This is a hot function in the Dromaeo benchmark.
  347. StringImpl* impl = this->impl();
  348. if (UNLIKELY(!impl))
  349. return *this;
  350. RefPtr<StringImpl> newImpl = impl->lower();
  351. if (LIKELY(newImpl == impl))
  352. return *this;
  353. return AtomicString(newImpl);
  354. }
  355. AtomicString AtomicString::fromUTF8Internal(const char* charactersStart, const char* charactersEnd)
  356. {
  357. HashAndUTF8Characters buffer;
  358. buffer.characters = charactersStart;
  359. buffer.hash = calculateStringHashAndLengthFromUTF8MaskingTop8Bits(charactersStart, charactersEnd, buffer.length, buffer.utf16Length);
  360. if (!buffer.hash)
  361. return nullAtom;
  362. AtomicString atomicString;
  363. atomicString.m_string = addToStringTable<HashAndUTF8Characters, HashAndUTF8CharactersTranslator>(buffer);
  364. return atomicString;
  365. }
  366. #ifndef NDEBUG
  367. void AtomicString::show() const
  368. {
  369. m_string.show();
  370. }
  371. #endif
  372. } // namespace WTF