123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- /*
- * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
- * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
- * Copyright (C) 2012 Google 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"
- #include "AtomicString.h"
- #include "AtomicStringTable.h"
- #include "StringHash.h"
- #include <wtf/HashSet.h>
- #include <wtf/Threading.h>
- #include <wtf/WTFThreadData.h>
- #include <wtf/unicode/UTF8.h>
- #if USE(WEB_THREAD)
- #include <wtf/MainThread.h>
- #include <wtf/TCSpinLock.h>
- #endif
- namespace WTF {
- using namespace Unicode;
- COMPILE_ASSERT(sizeof(AtomicString) == sizeof(String), atomic_string_and_string_must_be_same_size);
- #if USE(WEB_THREAD)
- class AtomicStringTableLocker : public SpinLockHolder {
- WTF_MAKE_NONCOPYABLE(AtomicStringTableLocker);
- static SpinLock s_stringTableLock;
- public:
- AtomicStringTableLocker()
- : SpinLockHolder(&s_stringTableLock)
- {
- }
- };
- SpinLock AtomicStringTableLocker::s_stringTableLock = SPINLOCK_INITIALIZER;
- #else
- class AtomicStringTableLocker {
- WTF_MAKE_NONCOPYABLE(AtomicStringTableLocker);
- public:
- AtomicStringTableLocker() { }
- ~AtomicStringTableLocker() { }
- };
- #endif // USE(WEB_THREAD)
- static ALWAYS_INLINE HashSet<StringImpl*>& stringTable()
- {
- return wtfThreadData().atomicStringTable()->table();
- }
- template<typename T, typename HashTranslator>
- static inline PassRefPtr<StringImpl> addToStringTable(const T& value)
- {
- AtomicStringTableLocker locker;
- HashSet<StringImpl*>::AddResult addResult = stringTable().add<T, HashTranslator>(value);
- // 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.isNewEntry ? adoptRef(*addResult.iterator) : *addResult.iterator;
- }
- struct CStringTranslator {
- static unsigned hash(const LChar* c)
- {
- return StringHasher::computeHashAndMaskTop8Bits(c);
- }
- static inline bool equal(StringImpl* r, const LChar* s)
- {
- return WTF::equal(r, s);
- }
- static void translate(StringImpl*& location, const LChar* const& c, unsigned hash)
- {
- location = StringImpl::create(c).leakRef();
- location->setHash(hash);
- location->setIsAtomic(true);
- }
- };
- PassRefPtr<StringImpl> AtomicString::add(const LChar* c)
- {
- if (!c)
- return 0;
- if (!*c)
- return StringImpl::empty();
- return addToStringTable<const LChar*, CStringTranslator>(c);
- }
- template<typename CharacterType>
- struct HashTranslatorCharBuffer {
- const CharacterType* s;
- unsigned length;
- };
- typedef HashTranslatorCharBuffer<UChar> UCharBuffer;
- struct UCharBufferTranslator {
- static unsigned hash(const UCharBuffer& buf)
- {
- return StringHasher::computeHashAndMaskTop8Bits(buf.s, buf.length);
- }
- static bool equal(StringImpl* const& str, const UCharBuffer& buf)
- {
- return WTF::equal(str, buf.s, buf.length);
- }
- static void translate(StringImpl*& location, const UCharBuffer& buf, unsigned hash)
- {
- location = StringImpl::create8BitIfPossible(buf.s, buf.length).leakRef();
- location->setHash(hash);
- location->setIsAtomic(true);
- }
- };
- template<typename CharacterType>
- struct HashAndCharacters {
- unsigned hash;
- const CharacterType* characters;
- unsigned length;
- };
- template<typename CharacterType>
- struct HashAndCharactersTranslator {
- static unsigned hash(const HashAndCharacters<CharacterType>& buffer)
- {
- ASSERT(buffer.hash == StringHasher::computeHashAndMaskTop8Bits(buffer.characters, buffer.length));
- return buffer.hash;
- }
- static bool equal(StringImpl* const& string, const HashAndCharacters<CharacterType>& buffer)
- {
- return WTF::equal(string, buffer.characters, buffer.length);
- }
- static void translate(StringImpl*& location, const HashAndCharacters<CharacterType>& buffer, unsigned hash)
- {
- location = StringImpl::create(buffer.characters, buffer.length).leakRef();
- location->setHash(hash);
- location->setIsAtomic(true);
- }
- };
- struct HashAndUTF8Characters {
- unsigned hash;
- const char* characters;
- unsigned length;
- unsigned utf16Length;
- };
- struct HashAndUTF8CharactersTranslator {
- static unsigned hash(const HashAndUTF8Characters& buffer)
- {
- return buffer.hash;
- }
- static bool equal(StringImpl* const& string, const HashAndUTF8Characters& buffer)
- {
- if (buffer.utf16Length != string->length())
- return false;
- // If buffer contains only ASCII characters UTF-8 and UTF16 length are the same.
- if (buffer.utf16Length != buffer.length) {
- const UChar* stringCharacters = string->characters();
- return equalUTF16WithUTF8(stringCharacters, stringCharacters + string->length(), buffer.characters, buffer.characters + buffer.length);
- }
- if (string->is8Bit()) {
- const LChar* stringCharacters = string->characters8();
- for (unsigned i = 0; i < buffer.length; ++i) {
- ASSERT(isASCII(buffer.characters[i]));
- if (stringCharacters[i] != buffer.characters[i])
- return false;
- }
- return true;
- }
- const UChar* stringCharacters = string->characters16();
- for (unsigned i = 0; i < buffer.length; ++i) {
- ASSERT(isASCII(buffer.characters[i]));
- if (stringCharacters[i] != buffer.characters[i])
- return false;
- }
- return true;
- }
- static void translate(StringImpl*& location, const HashAndUTF8Characters& buffer, unsigned hash)
- {
- UChar* target;
- RefPtr<StringImpl> newString = StringImpl::createUninitialized(buffer.utf16Length, target);
- bool isAllASCII;
- const char* source = buffer.characters;
- if (convertUTF8ToUTF16(&source, source + buffer.length, &target, target + buffer.utf16Length, &isAllASCII) != conversionOK)
- ASSERT_NOT_REACHED();
- if (isAllASCII)
- newString = StringImpl::create(buffer.characters, buffer.length);
- location = newString.release().leakRef();
- location->setHash(hash);
- location->setIsAtomic(true);
- }
- };
- PassRefPtr<StringImpl> AtomicString::add(const UChar* s, unsigned length)
- {
- if (!s)
- return 0;
- if (!length)
- return StringImpl::empty();
-
- UCharBuffer buffer = { s, length };
- return addToStringTable<UCharBuffer, UCharBufferTranslator>(buffer);
- }
- PassRefPtr<StringImpl> AtomicString::add(const UChar* s, unsigned length, unsigned existingHash)
- {
- ASSERT(s);
- ASSERT(existingHash);
- if (!length)
- return StringImpl::empty();
- HashAndCharacters<UChar> buffer = { existingHash, s, length };
- return addToStringTable<HashAndCharacters<UChar>, HashAndCharactersTranslator<UChar> >(buffer);
- }
- PassRefPtr<StringImpl> AtomicString::add(const UChar* s)
- {
- if (!s)
- return 0;
- unsigned length = 0;
- while (s[length] != UChar(0))
- ++length;
- if (!length)
- return StringImpl::empty();
- UCharBuffer buffer = { s, length };
- return addToStringTable<UCharBuffer, UCharBufferTranslator>(buffer);
- }
- struct SubstringLocation {
- StringImpl* baseString;
- unsigned start;
- unsigned length;
- };
- struct SubstringTranslator {
- static unsigned hash(const SubstringLocation& buffer)
- {
- return StringHasher::computeHashAndMaskTop8Bits(buffer.baseString->characters() + buffer.start, buffer.length);
- }
- static bool equal(StringImpl* const& string, const SubstringLocation& buffer)
- {
- return WTF::equal(string, buffer.baseString->characters() + buffer.start, buffer.length);
- }
- static void translate(StringImpl*& location, const SubstringLocation& buffer, unsigned hash)
- {
- location = StringImpl::create(buffer.baseString, buffer.start, buffer.length).leakRef();
- location->setHash(hash);
- location->setIsAtomic(true);
- }
- };
- PassRefPtr<StringImpl> AtomicString::add(StringImpl* baseString, unsigned start, unsigned length)
- {
- if (!baseString)
- return 0;
- if (!length || start >= baseString->length())
- return StringImpl::empty();
- unsigned maxLength = baseString->length() - start;
- if (length >= maxLength) {
- if (!start)
- return add(baseString);
- length = maxLength;
- }
- SubstringLocation buffer = { baseString, start, length };
- return addToStringTable<SubstringLocation, SubstringTranslator>(buffer);
- }
-
- typedef HashTranslatorCharBuffer<LChar> LCharBuffer;
- struct LCharBufferTranslator {
- static unsigned hash(const LCharBuffer& buf)
- {
- return StringHasher::computeHashAndMaskTop8Bits(buf.s, buf.length);
- }
- static bool equal(StringImpl* const& str, const LCharBuffer& buf)
- {
- return WTF::equal(str, buf.s, buf.length);
- }
- static void translate(StringImpl*& location, const LCharBuffer& buf, unsigned hash)
- {
- location = StringImpl::create(buf.s, buf.length).leakRef();
- location->setHash(hash);
- location->setIsAtomic(true);
- }
- };
- typedef HashTranslatorCharBuffer<char> CharBuffer;
- struct CharBufferFromLiteralDataTranslator {
- static unsigned hash(const CharBuffer& buf)
- {
- return StringHasher::computeHashAndMaskTop8Bits(reinterpret_cast<const LChar*>(buf.s), buf.length);
- }
- static bool equal(StringImpl* const& str, const CharBuffer& buf)
- {
- return WTF::equal(str, buf.s, buf.length);
- }
- static void translate(StringImpl*& location, const CharBuffer& buf, unsigned hash)
- {
- location = StringImpl::createFromLiteral(buf.s, buf.length).leakRef();
- location->setHash(hash);
- location->setIsAtomic(true);
- }
- };
- PassRefPtr<StringImpl> AtomicString::add(const LChar* s, unsigned length)
- {
- if (!s)
- return 0;
- if (!length)
- return StringImpl::empty();
- LCharBuffer buffer = { s, length };
- return addToStringTable<LCharBuffer, LCharBufferTranslator>(buffer);
- }
- PassRefPtr<StringImpl> AtomicString::addFromLiteralData(const char* characters, unsigned length)
- {
- ASSERT(characters);
- ASSERT(length);
- CharBuffer buffer = { characters, length };
- return addToStringTable<CharBuffer, CharBufferFromLiteralDataTranslator>(buffer);
- }
- PassRefPtr<StringImpl> AtomicString::addSlowCase(StringImpl* r)
- {
- if (!r->length())
- return StringImpl::empty();
- AtomicStringTableLocker locker;
- StringImpl* result = *stringTable().add(r).iterator;
- if (result == r)
- r->setIsAtomic(true);
- return result;
- }
- template<typename CharacterType>
- static inline HashSet<StringImpl*>::iterator findString(const StringImpl* stringImpl)
- {
- HashAndCharacters<CharacterType> buffer = { stringImpl->existingHash(), stringImpl->getCharacters<CharacterType>(), stringImpl->length() };
- return stringTable().find<HashAndCharacters<CharacterType>, HashAndCharactersTranslator<CharacterType> >(buffer);
- }
- AtomicStringImpl* AtomicString::find(const StringImpl* stringImpl)
- {
- ASSERT(stringImpl);
- ASSERT(stringImpl->existingHash());
- if (!stringImpl->length())
- return static_cast<AtomicStringImpl*>(StringImpl::empty());
- AtomicStringTableLocker locker;
- HashSet<StringImpl*>::iterator iterator;
- if (stringImpl->is8Bit())
- iterator = findString<LChar>(stringImpl);
- else
- iterator = findString<UChar>(stringImpl);
- if (iterator == stringTable().end())
- return 0;
- return static_cast<AtomicStringImpl*>(*iterator);
- }
- void AtomicString::remove(StringImpl* r)
- {
- AtomicStringTableLocker locker;
- stringTable().remove(r);
- }
- AtomicString AtomicString::lower() const
- {
- // Note: This is a hot function in the Dromaeo benchmark.
- StringImpl* impl = this->impl();
- if (UNLIKELY(!impl))
- return *this;
- RefPtr<StringImpl> newImpl = impl->lower();
- if (LIKELY(newImpl == impl))
- return *this;
- return AtomicString(newImpl);
- }
- AtomicString AtomicString::fromUTF8Internal(const char* charactersStart, const char* charactersEnd)
- {
- HashAndUTF8Characters buffer;
- buffer.characters = charactersStart;
- buffer.hash = calculateStringHashAndLengthFromUTF8MaskingTop8Bits(charactersStart, charactersEnd, buffer.length, buffer.utf16Length);
- if (!buffer.hash)
- return nullAtom;
- AtomicString atomicString;
- atomicString.m_string = addToStringTable<HashAndUTF8Characters, HashAndUTF8CharactersTranslator>(buffer);
- return atomicString;
- }
- #ifndef NDEBUG
- void AtomicString::show() const
- {
- m_string.show();
- }
- #endif
- } // namespace WTF
|