| 1 | /* |
| 2 | * Copyright (C) 2006 Apple Inc. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <wtf/text/UniquedStringImpl.h> |
| 24 | |
| 25 | namespace WTF { |
| 26 | |
| 27 | class AtomicStringTable; |
| 28 | |
| 29 | class AtomicStringImpl : public UniquedStringImpl { |
| 30 | public: |
| 31 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> lookUp(const LChar*, unsigned length); |
| 32 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> lookUp(const UChar*, unsigned length); |
| 33 | static RefPtr<AtomicStringImpl> lookUp(StringImpl* string) |
| 34 | { |
| 35 | if (!string || string->isAtomic()) |
| 36 | return static_cast<AtomicStringImpl*>(string); |
| 37 | return lookUpSlowCase(*string); |
| 38 | } |
| 39 | |
| 40 | static void remove(AtomicStringImpl*); |
| 41 | |
| 42 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> add(const LChar*); |
| 43 | ALWAYS_INLINE static RefPtr<AtomicStringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); }; |
| 44 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> add(const LChar*, unsigned length); |
| 45 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> add(const UChar*, unsigned length); |
| 46 | ALWAYS_INLINE static RefPtr<AtomicStringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); }; |
| 47 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> add(const UChar*); |
| 48 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> add(StringImpl*, unsigned offset, unsigned length); |
| 49 | ALWAYS_INLINE static RefPtr<AtomicStringImpl> add(StringImpl* string) |
| 50 | { |
| 51 | if (!string) |
| 52 | return static_cast<AtomicStringImpl*>(string); |
| 53 | return add(*string); |
| 54 | } |
| 55 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> add(const StaticStringImpl*); |
| 56 | WTF_EXPORT_PRIVATE static Ref<AtomicStringImpl> addLiteral(const char* characters, unsigned length); |
| 57 | |
| 58 | // Returns null if the input data contains an invalid UTF-8 sequence. |
| 59 | static RefPtr<AtomicStringImpl> addUTF8(const char* start, const char* end); |
| 60 | |
| 61 | #if USE(CF) |
| 62 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> add(CFStringRef); |
| 63 | #endif |
| 64 | |
| 65 | template<typename StringTableProvider> |
| 66 | ALWAYS_INLINE static RefPtr<AtomicStringImpl> addWithStringTableProvider(StringTableProvider& stringTableProvider, StringImpl* string) |
| 67 | { |
| 68 | if (!string) |
| 69 | return nullptr; |
| 70 | return add(*stringTableProvider.atomicStringTable(), *string); |
| 71 | } |
| 72 | |
| 73 | #if !ASSERT_DISABLED |
| 74 | WTF_EXPORT_PRIVATE static bool isInAtomicStringTable(StringImpl*); |
| 75 | #endif |
| 76 | |
| 77 | private: |
| 78 | AtomicStringImpl() = delete; |
| 79 | |
| 80 | ALWAYS_INLINE static Ref<AtomicStringImpl> add(StringImpl& string) |
| 81 | { |
| 82 | if (string.isAtomic()) { |
| 83 | ASSERT_WITH_MESSAGE(!string.length() || isInAtomicStringTable(&string), "The atomic string comes from an other thread!" ); |
| 84 | return static_cast<AtomicStringImpl&>(string); |
| 85 | } |
| 86 | return addSlowCase(string); |
| 87 | } |
| 88 | |
| 89 | ALWAYS_INLINE static Ref<AtomicStringImpl> add(AtomicStringTable& stringTable, StringImpl& string) |
| 90 | { |
| 91 | if (string.isAtomic()) { |
| 92 | ASSERT_WITH_MESSAGE(!string.length() || isInAtomicStringTable(&string), "The atomic string comes from an other thread!" ); |
| 93 | return static_cast<AtomicStringImpl&>(string); |
| 94 | } |
| 95 | return addSlowCase(stringTable, string); |
| 96 | } |
| 97 | |
| 98 | WTF_EXPORT_PRIVATE static Ref<AtomicStringImpl> addSlowCase(StringImpl&); |
| 99 | WTF_EXPORT_PRIVATE static Ref<AtomicStringImpl> addSlowCase(AtomicStringTable&, StringImpl&); |
| 100 | |
| 101 | WTF_EXPORT_PRIVATE static RefPtr<AtomicStringImpl> lookUpSlowCase(StringImpl&); |
| 102 | }; |
| 103 | |
| 104 | #if !ASSERT_DISABLED |
| 105 | // AtomicStringImpls created from StaticStringImpl will ASSERT |
| 106 | // in the generic ValueCheck<T>::checkConsistency |
| 107 | // as they are not allocated by fastMalloc. |
| 108 | // We don't currently have any way to detect that case |
| 109 | // so we ignore the consistency check for all AtomicStringImpls*. |
| 110 | template<> struct |
| 111 | ValueCheck<AtomicStringImpl*> { |
| 112 | static void checkConsistency(const AtomicStringImpl*) { } |
| 113 | }; |
| 114 | |
| 115 | template<> struct |
| 116 | ValueCheck<const AtomicStringImpl*> { |
| 117 | static void checkConsistency(const AtomicStringImpl*) { } |
| 118 | }; |
| 119 | #endif |
| 120 | |
| 121 | } |
| 122 | |
| 123 | using WTF::AtomicStringImpl; |
| 124 | |