]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/SmallStrings.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / SmallStrings.cpp
CommitLineData
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"
9dae56ea 32#include <wtf/Noncopyable.h>
14957cd0 33#include <wtf/PassOwnPtr.h>
9dae56ea
A
34
35namespace JSC {
9dae56ea 36
6fe7ccc8 37static inline void finalize(JSString*& string)
f9bf01c6 38{
6fe7ccc8
A
39 if (!string || Heap::isMarked(string))
40 return;
41 string = 0;
f9bf01c6
A
42}
43
14957cd0
A
44class SmallStringsStorage {
45 WTF_MAKE_NONCOPYABLE(SmallStringsStorage); WTF_MAKE_FAST_ALLOCATED;
9dae56ea
A
46public:
47 SmallStringsStorage();
48
14957cd0
A
49 StringImpl* rep(unsigned char character)
50 {
51 return m_reps[character].get();
52 }
9dae56ea
A
53
54private:
14957cd0
A
55 static const unsigned singleCharacterStringCount = maxSingleCharacterString + 1;
56
57 RefPtr<StringImpl> m_reps[singleCharacterStringCount];
9dae56ea
A
58};
59
60SmallStringsStorage::SmallStringsStorage()
61{
6fe7ccc8 62 LChar* characterBuffer = 0;
14957cd0
A
63 RefPtr<StringImpl> baseString = StringImpl::createUninitialized(singleCharacterStringCount, characterBuffer);
64 for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
f9bf01c6 65 characterBuffer[i] = i;
14957cd0 66 m_reps[i] = StringImpl::create(baseString, i, 1);
9dae56ea
A
67 }
68}
69
70SmallStrings::SmallStrings()
6fe7ccc8
A
71 : m_emptyString(0)
72#define JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE(name) , m_##name(0)
73 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE)
74#undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE
9dae56ea 75{
14957cd0 76 COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
9dae56ea 77
6fe7ccc8
A
78 for (unsigned i = 0; i < singleCharacterStringCount; ++i)
79 m_singleCharacterStrings[i] = 0;
9dae56ea
A
80}
81
6fe7ccc8 82SmallStrings::~SmallStrings()
9dae56ea 83{
9dae56ea
A
84}
85
6fe7ccc8 86void SmallStrings::finalizeSmallStrings()
f9bf01c6 87{
6fe7ccc8 88 finalize(m_emptyString);
14957cd0 89 for (unsigned i = 0; i < singleCharacterStringCount; ++i)
6fe7ccc8
A
90 finalize(m_singleCharacterStrings[i]);
91#define JSC_COMMON_STRINGS_ATTRIBUTE_FINALIZE(name) finalize(m_##name);
92 JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_FINALIZE)
93#undef JSC_COMMON_STRINGS_ATTRIBUTE_FINALIZE
9dae56ea
A
94}
95
96void SmallStrings::createEmptyString(JSGlobalData* globalData)
97{
98 ASSERT(!m_emptyString);
6fe7ccc8 99 m_emptyString = JSString::createHasOtherOwner(*globalData, StringImpl::empty());
9dae56ea
A
100}
101
102void SmallStrings::createSingleCharacterString(JSGlobalData* globalData, unsigned char character)
103{
104 if (!m_storage)
14957cd0 105 m_storage = adoptPtr(new SmallStringsStorage);
9dae56ea 106 ASSERT(!m_singleCharacterStrings[character]);
6fe7ccc8 107 m_singleCharacterStrings[character] = JSString::createHasOtherOwner(*globalData, PassRefPtr<StringImpl>(m_storage->rep(character)));
9dae56ea
A
108}
109
14957cd0 110StringImpl* SmallStrings::singleCharacterStringRep(unsigned char character)
9dae56ea
A
111{
112 if (!m_storage)
14957cd0 113 m_storage = adoptPtr(new SmallStringsStorage);
9dae56ea
A
114 return m_storage->rep(character);
115}
116
6fe7ccc8
A
117void SmallStrings::initialize(JSGlobalData* globalData, JSString*& string, const char* value) const
118{
119 string = JSString::create(*globalData, StringImpl::create(value));
120}
121
9dae56ea 122} // namespace JSC