]> git.saurik.com Git - apple/javascriptcore.git/blame - parser/ParserArena.h
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / parser / ParserArena.h
CommitLineData
ba379fdc
A
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
40a37d08 29#include "CommonIdentifiers.h"
f9bf01c6 30#include "Identifier.h"
81345200 31#include <array>
f9bf01c6 32#include <wtf/SegmentedVector.h>
ba379fdc
A
33
34namespace JSC {
35
36 class ParserArenaDeletable;
37 class ParserArenaRefCounted;
38
14957cd0
A
39 class IdentifierArena {
40 WTF_MAKE_FAST_ALLOCATED;
ba379fdc 41 public:
14957cd0
A
42 IdentifierArena()
43 {
44 clear();
45 }
46
6fe7ccc8 47 template <typename T>
93a37866
A
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);
6fe7ccc8 50
93a37866 51 const Identifier& makeNumericIdentifier(VM*, double number);
f9bf01c6 52
6fe7ccc8
A
53 bool isEmpty() const { return m_identifiers.isEmpty(); }
54
55 public:
56 static const int MaximumCachableCharacter = 128;
57 typedef SegmentedVector<Identifier, 64> IdentifierVector;
14957cd0
A
58 void clear()
59 {
60 m_identifiers.clear();
6fe7ccc8 61 for (int i = 0; i < MaximumCachableCharacter; i++)
14957cd0 62 m_shortIdentifiers[i] = 0;
6fe7ccc8
A
63 for (int i = 0; i < MaximumCachableCharacter; i++)
64 m_recentIdentifiers[i] = 0;
14957cd0 65 }
f9bf01c6
A
66
67 private:
f9bf01c6 68 IdentifierVector m_identifiers;
81345200
A
69 std::array<Identifier*, MaximumCachableCharacter> m_shortIdentifiers;
70 std::array<Identifier*, MaximumCachableCharacter> m_recentIdentifiers;
f9bf01c6
A
71 };
72
6fe7ccc8 73 template <typename T>
93a37866 74 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifier(VM* vm, const T* characters, size_t length)
f9bf01c6 75 {
40a37d08
A
76 if (!length)
77 return vm->propertyNames->emptyIdentifier;
6fe7ccc8 78 if (characters[0] >= MaximumCachableCharacter) {
93a37866 79 m_identifiers.append(Identifier(vm, characters, length));
6fe7ccc8
A
80 return m_identifiers.last();
81 }
82 if (length == 1) {
14957cd0
A
83 if (Identifier* ident = m_shortIdentifiers[characters[0]])
84 return *ident;
93a37866 85 m_identifiers.append(Identifier(vm, characters, length));
14957cd0
A
86 m_shortIdentifiers[characters[0]] = &m_identifiers.last();
87 return m_identifiers.last();
88 }
6fe7ccc8
A
89 Identifier* ident = m_recentIdentifiers[characters[0]];
90 if (ident && Identifier::equal(ident->impl(), characters, length))
91 return *ident;
93a37866 92 m_identifiers.append(Identifier(vm, characters, length));
6fe7ccc8 93 m_recentIdentifiers[characters[0]] = &m_identifiers.last();
f9bf01c6
A
94 return m_identifiers.last();
95 }
96
93a37866 97 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifierLCharFromUChar(VM* vm, const UChar* characters, size_t length)
6fe7ccc8 98 {
40a37d08
A
99 if (!length)
100 return vm->propertyNames->emptyIdentifier;
6fe7ccc8 101 if (characters[0] >= MaximumCachableCharacter) {
93a37866 102 m_identifiers.append(Identifier::createLCharFromUChar(vm, characters, length));
6fe7ccc8
A
103 return m_identifiers.last();
104 }
105 if (length == 1) {
106 if (Identifier* ident = m_shortIdentifiers[characters[0]])
107 return *ident;
93a37866 108 m_identifiers.append(Identifier(vm, characters, length));
6fe7ccc8
A
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;
93a37866 115 m_identifiers.append(Identifier::createLCharFromUChar(vm, characters, length));
6fe7ccc8
A
116 m_recentIdentifiers[characters[0]] = &m_identifiers.last();
117 return m_identifiers.last();
118 }
119
93a37866 120 inline const Identifier& IdentifierArena::makeNumericIdentifier(VM* vm, double number)
f9bf01c6 121 {
93a37866 122 m_identifiers.append(Identifier(vm, String::numberToStringECMAScript(number)));
f9bf01c6
A
123 return m_identifiers.last();
124 }
125
14957cd0
A
126 class ParserArena {
127 WTF_MAKE_NONCOPYABLE(ParserArena);
f9bf01c6
A
128 public:
129 ParserArena();
130 ~ParserArena();
131
ba379fdc
A
132 void swap(ParserArena& otherArena)
133 {
f9bf01c6
A
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);
ba379fdc
A
138 m_deletableObjects.swap(otherArena.m_deletableObjects);
139 m_refCountedObjects.swap(otherArena.m_refCountedObjects);
140 }
ba379fdc 141
f9bf01c6
A
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 {
6fe7ccc8 157 ParserArenaDeletable* deletable = static_cast<ParserArenaDeletable*>(allocateFreeable(size));
f9bf01c6
A
158 m_deletableObjects.append(deletable);
159 return deletable;
160 }
ba379fdc 161
f9bf01c6 162 void derefWithArena(PassRefPtr<ParserArenaRefCounted>);
ba379fdc
A
163 bool contains(ParserArenaRefCounted*) const;
164 ParserArenaRefCounted* last() const;
165 void removeLast();
166
f9bf01c6 167 bool isEmpty() const;
6fe7ccc8 168 JS_EXPORT_PRIVATE void reset();
ba379fdc 169
6fe7ccc8
A
170 IdentifierArena& identifierArena()
171 {
172 if (UNLIKELY (!m_identifierArena))
173 m_identifierArena = adoptPtr(new IdentifierArena);
174 return *m_identifierArena;
175 }
f9bf01c6 176
ba379fdc 177 private:
f9bf01c6
A
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;
ba379fdc 194 Vector<ParserArenaDeletable*> m_deletableObjects;
81345200 195 Vector<RefPtr<ParserArenaRefCounted>> m_refCountedObjects;
ba379fdc
A
196 };
197
198}
199
200#endif