]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/SmallStrings.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / SmallStrings.cpp
1 /*
2 * Copyright (C) 2008, 2010 Apple Inc. All Rights Reserved.
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
29 #include "HeapRootVisitor.h"
30 #include "JSGlobalObject.h"
31 #include "JSString.h"
32 #include "JSCInlines.h"
33 #include <wtf/Noncopyable.h>
34 #include <wtf/text/StringImpl.h>
35
36 namespace JSC {
37
38 class SmallStringsStorage {
39 WTF_MAKE_NONCOPYABLE(SmallStringsStorage); WTF_MAKE_FAST_ALLOCATED;
40 public:
41 SmallStringsStorage();
42
43 StringImpl* rep(unsigned char character)
44 {
45 return m_reps[character].get();
46 }
47
48 private:
49 static const unsigned singleCharacterStringCount = maxSingleCharacterString + 1;
50
51 RefPtr<StringImpl> m_reps[singleCharacterStringCount];
52 };
53
54 SmallStringsStorage::SmallStringsStorage()
55 {
56 LChar* characterBuffer = 0;
57 RefPtr<StringImpl> baseString = StringImpl::createUninitialized(singleCharacterStringCount, characterBuffer);
58 for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
59 characterBuffer[i] = i;
60 m_reps[i] = AtomicStringImpl::add(PassRefPtr<StringImpl>(StringImpl::createSubstringSharingImpl(baseString, i, 1)).get());
61 }
62 }
63
64 SmallStrings::SmallStrings()
65 : m_emptyString(0)
66 #define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) , m_##name(0)
67 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE)
68 #undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE
69 , m_nullObjectString(nullptr)
70 , m_undefinedObjectString(nullptr)
71 , m_needsToBeVisited(true)
72 {
73 COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
74
75 for (unsigned i = 0; i < singleCharacterStringCount; ++i)
76 m_singleCharacterStrings[i] = 0;
77 }
78
79 void SmallStrings::initializeCommonStrings(VM& vm)
80 {
81 createEmptyString(&vm);
82 for (unsigned i = 0; i <= maxSingleCharacterString; ++i)
83 createSingleCharacterString(&vm, i);
84 #define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) initialize(&vm, m_##name, #name);
85 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE)
86 #undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE
87 initialize(&vm, m_nullObjectString, "[object Null]");
88 initialize(&vm, m_undefinedObjectString, "[object Undefined]");
89 }
90
91 void SmallStrings::visitStrongReferences(SlotVisitor& visitor)
92 {
93 m_needsToBeVisited = false;
94 visitor.appendUnbarrieredPointer(&m_emptyString);
95 for (unsigned i = 0; i <= maxSingleCharacterString; ++i)
96 visitor.appendUnbarrieredPointer(m_singleCharacterStrings + i);
97 #define JSC_COMMON_STRINGS_ATTRIBUTE_VISIT(name) visitor.appendUnbarrieredPointer(&m_##name);
98 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_VISIT)
99 #undef JSC_COMMON_STRINGS_ATTRIBUTE_VISIT
100 visitor.appendUnbarrieredPointer(&m_nullObjectString);
101 visitor.appendUnbarrieredPointer(&m_undefinedObjectString);
102 }
103
104 SmallStrings::~SmallStrings()
105 {
106 }
107
108 void SmallStrings::createEmptyString(VM* vm)
109 {
110 ASSERT(!m_emptyString);
111 m_emptyString = JSString::createHasOtherOwner(*vm, StringImpl::empty());
112 m_needsToBeVisited = true;
113 }
114
115 void SmallStrings::createSingleCharacterString(VM* vm, unsigned char character)
116 {
117 if (!m_storage)
118 m_storage = std::make_unique<SmallStringsStorage>();
119 ASSERT(!m_singleCharacterStrings[character]);
120 m_singleCharacterStrings[character] = JSString::createHasOtherOwner(*vm, PassRefPtr<StringImpl>(m_storage->rep(character)));
121 m_needsToBeVisited = true;
122 }
123
124 StringImpl* SmallStrings::singleCharacterStringRep(unsigned char character)
125 {
126 if (!m_storage)
127 m_storage = std::make_unique<SmallStringsStorage>();
128 return m_storage->rep(character);
129 }
130
131 void SmallStrings::initialize(VM* vm, JSString*& string, const char* value)
132 {
133 string = JSString::create(*vm, Identifier::fromString(vm, value).impl());
134 m_needsToBeVisited = true;
135 }
136
137 } // namespace JSC