]>
Commit | Line | Data |
---|---|---|
9dae56ea | 1 | /* |
14957cd0 | 2 | * Copyright (C) 2008, 2010 Apple Inc. All Rights Reserved. |
9dae56ea A |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 | */ | |
25 | ||
26 | #include "config.h" | |
27 | #include "SmallStrings.h" | |
28 | ||
6fe7ccc8 | 29 | #include "HeapRootVisitor.h" |
9dae56ea A |
30 | #include "JSGlobalObject.h" |
31 | #include "JSString.h" | |
93a37866 | 32 | #include "Operations.h" |
9dae56ea | 33 | #include <wtf/Noncopyable.h> |
14957cd0 | 34 | #include <wtf/PassOwnPtr.h> |
93a37866 | 35 | #include <wtf/text/StringImpl.h> |
9dae56ea A |
36 | |
37 | namespace JSC { | |
9dae56ea | 38 | |
6fe7ccc8 | 39 | static inline void finalize(JSString*& string) |
f9bf01c6 | 40 | { |
6fe7ccc8 A |
41 | if (!string || Heap::isMarked(string)) |
42 | return; | |
43 | string = 0; | |
f9bf01c6 A |
44 | } |
45 | ||
14957cd0 A |
46 | class SmallStringsStorage { |
47 | WTF_MAKE_NONCOPYABLE(SmallStringsStorage); WTF_MAKE_FAST_ALLOCATED; | |
9dae56ea A |
48 | public: |
49 | SmallStringsStorage(); | |
50 | ||
14957cd0 A |
51 | StringImpl* rep(unsigned char character) |
52 | { | |
53 | return m_reps[character].get(); | |
54 | } | |
9dae56ea A |
55 | |
56 | private: | |
14957cd0 A |
57 | static const unsigned singleCharacterStringCount = maxSingleCharacterString + 1; |
58 | ||
59 | RefPtr<StringImpl> m_reps[singleCharacterStringCount]; | |
9dae56ea A |
60 | }; |
61 | ||
62 | SmallStringsStorage::SmallStringsStorage() | |
63 | { | |
6fe7ccc8 | 64 | LChar* characterBuffer = 0; |
14957cd0 A |
65 | RefPtr<StringImpl> baseString = StringImpl::createUninitialized(singleCharacterStringCount, characterBuffer); |
66 | for (unsigned i = 0; i < singleCharacterStringCount; ++i) { | |
f9bf01c6 | 67 | characterBuffer[i] = i; |
14957cd0 | 68 | m_reps[i] = StringImpl::create(baseString, i, 1); |
9dae56ea A |
69 | } |
70 | } | |
71 | ||
72 | SmallStrings::SmallStrings() | |
6fe7ccc8 A |
73 | : m_emptyString(0) |
74 | #define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) , m_##name(0) | |
75 | JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE) | |
76 | #undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE | |
9dae56ea | 77 | { |
14957cd0 | 78 | COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage); |
9dae56ea | 79 | |
6fe7ccc8 A |
80 | for (unsigned i = 0; i < singleCharacterStringCount; ++i) |
81 | m_singleCharacterStrings[i] = 0; | |
9dae56ea A |
82 | } |
83 | ||
93a37866 A |
84 | void SmallStrings::initializeCommonStrings(VM& vm) |
85 | { | |
86 | createEmptyString(&vm); | |
87 | #define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) initialize(&vm, m_##name, #name); | |
88 | JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE) | |
89 | #undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE | |
90 | } | |
91 | ||
92 | void SmallStrings::visitStrongReferences(SlotVisitor& visitor) | |
93 | { | |
94 | visitor.appendUnbarrieredPointer(&m_emptyString); | |
95 | #define JSC_COMMON_STRINGS_ATTRIBUTE_VISIT(name) visitor.appendUnbarrieredPointer(&m_##name); | |
96 | JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_VISIT) | |
97 | #undef JSC_COMMON_STRINGS_ATTRIBUTE_VISIT | |
98 | } | |
99 | ||
6fe7ccc8 | 100 | SmallStrings::~SmallStrings() |
9dae56ea | 101 | { |
9dae56ea A |
102 | } |
103 | ||
6fe7ccc8 | 104 | void SmallStrings::finalizeSmallStrings() |
f9bf01c6 | 105 | { |
6fe7ccc8 | 106 | finalize(m_emptyString); |
14957cd0 | 107 | for (unsigned i = 0; i < singleCharacterStringCount; ++i) |
6fe7ccc8 | 108 | finalize(m_singleCharacterStrings[i]); |
9dae56ea A |
109 | } |
110 | ||
93a37866 | 111 | void SmallStrings::createEmptyString(VM* vm) |
9dae56ea A |
112 | { |
113 | ASSERT(!m_emptyString); | |
93a37866 | 114 | m_emptyString = JSString::createHasOtherOwner(*vm, StringImpl::empty()); |
9dae56ea A |
115 | } |
116 | ||
93a37866 | 117 | void SmallStrings::createSingleCharacterString(VM* vm, unsigned char character) |
9dae56ea A |
118 | { |
119 | if (!m_storage) | |
14957cd0 | 120 | m_storage = adoptPtr(new SmallStringsStorage); |
9dae56ea | 121 | ASSERT(!m_singleCharacterStrings[character]); |
93a37866 | 122 | m_singleCharacterStrings[character] = JSString::createHasOtherOwner(*vm, PassRefPtr<StringImpl>(m_storage->rep(character))); |
9dae56ea A |
123 | } |
124 | ||
14957cd0 | 125 | StringImpl* SmallStrings::singleCharacterStringRep(unsigned char character) |
9dae56ea A |
126 | { |
127 | if (!m_storage) | |
14957cd0 | 128 | m_storage = adoptPtr(new SmallStringsStorage); |
9dae56ea A |
129 | return m_storage->rep(character); |
130 | } | |
131 | ||
93a37866 | 132 | void SmallStrings::initialize(VM* vm, JSString*& string, const char* value) const |
6fe7ccc8 | 133 | { |
93a37866 | 134 | string = JSString::create(*vm, StringImpl::create(value)); |
6fe7ccc8 A |
135 | } |
136 | ||
9dae56ea | 137 | } // namespace JSC |