]> git.saurik.com Git - apple/javascriptcore.git/blob - parser/ParserArena.h
d8bc2cffa427314564fd1b0415c82475ce52b235
[apple/javascriptcore.git] / parser / ParserArena.h
1 /*
2 * Copyright (C) 2009 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 #ifndef ParserArena_h
27 #define ParserArena_h
28
29 #include "CommonIdentifiers.h"
30 #include "Identifier.h"
31 #include <array>
32 #include <wtf/SegmentedVector.h>
33
34 namespace JSC {
35
36 class ParserArenaDeletable;
37 class ParserArenaRefCounted;
38
39 class IdentifierArena {
40 WTF_MAKE_FAST_ALLOCATED;
41 public:
42 IdentifierArena()
43 {
44 clear();
45 }
46
47 template <typename T>
48 ALWAYS_INLINE const Identifier& makeIdentifier(VM*, const T* characters, size_t length);
49 ALWAYS_INLINE const Identifier& makeIdentifierLCharFromUChar(VM*, const UChar* characters, size_t length);
50
51 const Identifier& makeNumericIdentifier(VM*, double number);
52
53 bool isEmpty() const { return m_identifiers.isEmpty(); }
54
55 public:
56 static const int MaximumCachableCharacter = 128;
57 typedef SegmentedVector<Identifier, 64> IdentifierVector;
58 void clear()
59 {
60 m_identifiers.clear();
61 for (int i = 0; i < MaximumCachableCharacter; i++)
62 m_shortIdentifiers[i] = 0;
63 for (int i = 0; i < MaximumCachableCharacter; i++)
64 m_recentIdentifiers[i] = 0;
65 }
66
67 private:
68 IdentifierVector m_identifiers;
69 std::array<Identifier*, MaximumCachableCharacter> m_shortIdentifiers;
70 std::array<Identifier*, MaximumCachableCharacter> m_recentIdentifiers;
71 };
72
73 template <typename T>
74 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifier(VM* vm, const T* characters, size_t length)
75 {
76 if (!length)
77 return vm->propertyNames->emptyIdentifier;
78 if (characters[0] >= MaximumCachableCharacter) {
79 m_identifiers.append(Identifier(vm, characters, length));
80 return m_identifiers.last();
81 }
82 if (length == 1) {
83 if (Identifier* ident = m_shortIdentifiers[characters[0]])
84 return *ident;
85 m_identifiers.append(Identifier(vm, characters, length));
86 m_shortIdentifiers[characters[0]] = &m_identifiers.last();
87 return m_identifiers.last();
88 }
89 Identifier* ident = m_recentIdentifiers[characters[0]];
90 if (ident && Identifier::equal(ident->impl(), characters, length))
91 return *ident;
92 m_identifiers.append(Identifier(vm, characters, length));
93 m_recentIdentifiers[characters[0]] = &m_identifiers.last();
94 return m_identifiers.last();
95 }
96
97 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifierLCharFromUChar(VM* vm, const UChar* characters, size_t length)
98 {
99 if (!length)
100 return vm->propertyNames->emptyIdentifier;
101 if (characters[0] >= MaximumCachableCharacter) {
102 m_identifiers.append(Identifier::createLCharFromUChar(vm, characters, length));
103 return m_identifiers.last();
104 }
105 if (length == 1) {
106 if (Identifier* ident = m_shortIdentifiers[characters[0]])
107 return *ident;
108 m_identifiers.append(Identifier(vm, characters, length));
109 m_shortIdentifiers[characters[0]] = &m_identifiers.last();
110 return m_identifiers.last();
111 }
112 Identifier* ident = m_recentIdentifiers[characters[0]];
113 if (ident && Identifier::equal(ident->impl(), characters, length))
114 return *ident;
115 m_identifiers.append(Identifier::createLCharFromUChar(vm, characters, length));
116 m_recentIdentifiers[characters[0]] = &m_identifiers.last();
117 return m_identifiers.last();
118 }
119
120 inline const Identifier& IdentifierArena::makeNumericIdentifier(VM* vm, double number)
121 {
122 m_identifiers.append(Identifier(vm, String::numberToStringECMAScript(number)));
123 return m_identifiers.last();
124 }
125
126 class ParserArena {
127 WTF_MAKE_NONCOPYABLE(ParserArena);
128 public:
129 ParserArena();
130 ~ParserArena();
131
132 void swap(ParserArena& otherArena)
133 {
134 std::swap(m_freeableMemory, otherArena.m_freeableMemory);
135 std::swap(m_freeablePoolEnd, otherArena.m_freeablePoolEnd);
136 m_identifierArena.swap(otherArena.m_identifierArena);
137 m_freeablePools.swap(otherArena.m_freeablePools);
138 m_deletableObjects.swap(otherArena.m_deletableObjects);
139 m_refCountedObjects.swap(otherArena.m_refCountedObjects);
140 }
141
142 void* allocateFreeable(size_t size)
143 {
144 ASSERT(size);
145 ASSERT(size <= freeablePoolSize);
146 size_t alignedSize = alignSize(size);
147 ASSERT(alignedSize <= freeablePoolSize);
148 if (UNLIKELY(static_cast<size_t>(m_freeablePoolEnd - m_freeableMemory) < alignedSize))
149 allocateFreeablePool();
150 void* block = m_freeableMemory;
151 m_freeableMemory += alignedSize;
152 return block;
153 }
154
155 void* allocateDeletable(size_t size)
156 {
157 ParserArenaDeletable* deletable = static_cast<ParserArenaDeletable*>(allocateFreeable(size));
158 m_deletableObjects.append(deletable);
159 return deletable;
160 }
161
162 void derefWithArena(PassRefPtr<ParserArenaRefCounted>);
163 bool contains(ParserArenaRefCounted*) const;
164 ParserArenaRefCounted* last() const;
165 void removeLast();
166
167 bool isEmpty() const;
168 JS_EXPORT_PRIVATE void reset();
169
170 IdentifierArena& identifierArena()
171 {
172 if (UNLIKELY (!m_identifierArena))
173 m_identifierArena = adoptPtr(new IdentifierArena);
174 return *m_identifierArena;
175 }
176
177 private:
178 static const size_t freeablePoolSize = 8000;
179
180 static size_t alignSize(size_t size)
181 {
182 return (size + sizeof(WTF::AllocAlignmentInteger) - 1) & ~(sizeof(WTF::AllocAlignmentInteger) - 1);
183 }
184
185 void* freeablePool();
186 void allocateFreeablePool();
187 void deallocateObjects();
188
189 char* m_freeableMemory;
190 char* m_freeablePoolEnd;
191
192 OwnPtr<IdentifierArena> m_identifierArena;
193 Vector<void*> m_freeablePools;
194 Vector<ParserArenaDeletable*> m_deletableObjects;
195 Vector<RefPtr<ParserArenaRefCounted>> m_refCountedObjects;
196 };
197
198 }
199
200 #endif