1 | /*
|
---|
2 | * Copyright (C) 2008 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 "JSGlobalObject.h"
|
---|
30 | #include "JSString.h"
|
---|
31 |
|
---|
32 | #include <wtf/Noncopyable.h>
|
---|
33 |
|
---|
34 | namespace JSC {
|
---|
35 | static const unsigned numCharactersToStore = 0x100;
|
---|
36 |
|
---|
37 | class SmallStringsStorage : public Noncopyable {
|
---|
38 | public:
|
---|
39 | SmallStringsStorage();
|
---|
40 |
|
---|
41 | UString::Rep* rep(unsigned char character) { return &m_reps[character]; }
|
---|
42 |
|
---|
43 | private:
|
---|
44 | UString::Rep m_reps[numCharactersToStore];
|
---|
45 | };
|
---|
46 |
|
---|
47 | SmallStringsStorage::SmallStringsStorage()
|
---|
48 | {
|
---|
49 | UChar* characterBuffer = 0;
|
---|
50 | RefPtr<UStringImpl> baseString = UStringImpl::createUninitialized(numCharactersToStore, characterBuffer);
|
---|
51 | for (unsigned i = 0; i < numCharactersToStore; ++i) {
|
---|
52 | characterBuffer[i] = i;
|
---|
53 | new (&m_reps[i]) UString::Rep(&characterBuffer[i], 1, PassRefPtr<UStringImpl>(baseString));
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | SmallStrings::SmallStrings()
|
---|
58 | : m_emptyString(0)
|
---|
59 | , m_storage(0)
|
---|
60 | {
|
---|
61 | COMPILE_ASSERT(numCharactersToStore == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
|
---|
62 |
|
---|
63 | for (unsigned i = 0; i < numCharactersToStore; ++i)
|
---|
64 | m_singleCharacterStrings[i] = 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | SmallStrings::~SmallStrings()
|
---|
68 | {
|
---|
69 | }
|
---|
70 |
|
---|
71 | void SmallStrings::markChildren(MarkStack& markStack)
|
---|
72 | {
|
---|
73 | if (m_emptyString)
|
---|
74 | markStack.append(m_emptyString);
|
---|
75 | for (unsigned i = 0; i < numCharactersToStore; ++i) {
|
---|
76 | if (m_singleCharacterStrings[i])
|
---|
77 | markStack.append(m_singleCharacterStrings[i]);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | unsigned SmallStrings::count() const
|
---|
82 | {
|
---|
83 | unsigned count = 0;
|
---|
84 | if (m_emptyString)
|
---|
85 | ++count;
|
---|
86 | for (unsigned i = 0; i < numCharactersToStore; ++i) {
|
---|
87 | if (m_singleCharacterStrings[i])
|
---|
88 | ++count;
|
---|
89 | }
|
---|
90 | return count;
|
---|
91 | }
|
---|
92 |
|
---|
93 | void SmallStrings::createEmptyString(JSGlobalData* globalData)
|
---|
94 | {
|
---|
95 | ASSERT(!m_emptyString);
|
---|
96 | m_emptyString = new (globalData) JSString(globalData, "", JSString::HasOtherOwner);
|
---|
97 | }
|
---|
98 |
|
---|
99 | void SmallStrings::createSingleCharacterString(JSGlobalData* globalData, unsigned char character)
|
---|
100 | {
|
---|
101 | if (!m_storage)
|
---|
102 | m_storage.set(new SmallStringsStorage);
|
---|
103 | ASSERT(!m_singleCharacterStrings[character]);
|
---|
104 | m_singleCharacterStrings[character] = new (globalData) JSString(globalData, m_storage->rep(character), JSString::HasOtherOwner);
|
---|
105 | }
|
---|
106 |
|
---|
107 | UString::Rep* SmallStrings::singleCharacterStringRep(unsigned char character)
|
---|
108 | {
|
---|
109 | if (!m_storage)
|
---|
110 | m_storage.set(new SmallStringsStorage);
|
---|
111 | return m_storage->rep(character);
|
---|
112 | }
|
---|
113 |
|
---|
114 | } // namespace JSC
|
---|