initial import
[vuplus_webkit] / Source / JavaScriptCore / 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 <wtf/Noncopyable.h>
33 #include <wtf/PassOwnPtr.h>
34
35 namespace JSC {
36
37 static inline void finalize(JSString*& string)
38 {
39     if (!string || Heap::isMarked(string))
40         return;
41     string = 0;
42 }
43
44 class SmallStringsStorage {
45     WTF_MAKE_NONCOPYABLE(SmallStringsStorage); WTF_MAKE_FAST_ALLOCATED;
46 public:
47     SmallStringsStorage();
48
49     StringImpl* rep(unsigned char character)
50     {
51         return m_reps[character].get();
52     }
53
54 private:
55     static const unsigned singleCharacterStringCount = maxSingleCharacterString + 1;
56
57     RefPtr<StringImpl> m_reps[singleCharacterStringCount];
58 };
59
60 SmallStringsStorage::SmallStringsStorage()
61 {
62     UChar* characterBuffer = 0;
63     RefPtr<StringImpl> baseString = StringImpl::createUninitialized(singleCharacterStringCount, characterBuffer);
64     for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
65         characterBuffer[i] = i;
66         m_reps[i] = StringImpl::create(baseString, i, 1);
67     }
68 }
69
70 SmallStrings::SmallStrings()
71 {
72     COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
73     clear();
74 }
75
76 SmallStrings::~SmallStrings()
77 {
78 }
79
80 void SmallStrings::finalizeSmallStrings()
81 {
82     finalize(m_emptyString);
83     for (unsigned i = 0; i < singleCharacterStringCount; ++i)
84         finalize(m_singleCharacterStrings[i]);
85 }
86
87 void SmallStrings::clear()
88 {
89     m_emptyString = 0;
90     for (unsigned i = 0; i < singleCharacterStringCount; ++i)
91         m_singleCharacterStrings[i] = 0;
92 }
93
94 unsigned SmallStrings::count() const
95 {
96     unsigned count = 0;
97     if (m_emptyString)
98         ++count;
99     for (unsigned i = 0; i < singleCharacterStringCount; ++i) {
100         if (m_singleCharacterStrings[i])
101             ++count;
102     }
103     return count;
104 }
105
106 void SmallStrings::createEmptyString(JSGlobalData* globalData)
107 {
108     ASSERT(!m_emptyString);
109     m_emptyString = JSString::createHasOtherOwner(*globalData, "");
110 }
111
112 void SmallStrings::createSingleCharacterString(JSGlobalData* globalData, unsigned char character)
113 {
114     if (!m_storage)
115         m_storage = adoptPtr(new SmallStringsStorage);
116     ASSERT(!m_singleCharacterStrings[character]);
117     m_singleCharacterStrings[character] = JSString::createHasOtherOwner(*globalData, PassRefPtr<StringImpl>(m_storage->rep(character)));
118 }
119
120 StringImpl* SmallStrings::singleCharacterStringRep(unsigned char character)
121 {
122     if (!m_storage)
123         m_storage = adoptPtr(new SmallStringsStorage);
124     return m_storage->rep(character);
125 }
126
127 } // namespace JSC