]> git.saurik.com Git - apple/javascriptcore.git/blame - parser/ParserArena.h
JavaScriptCore-7601.1.46.3.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;
ba379fdc 37
14957cd0
A
38 class IdentifierArena {
39 WTF_MAKE_FAST_ALLOCATED;
ba379fdc 40 public:
14957cd0
A
41 IdentifierArena()
42 {
43 clear();
44 }
45
6fe7ccc8 46 template <typename T>
93a37866 47 ALWAYS_INLINE const Identifier& makeIdentifier(VM*, const T* characters, size_t length);
ed1e77d3 48 ALWAYS_INLINE const Identifier& makeEmptyIdentifier(VM*);
93a37866 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 public:
54 static const int MaximumCachableCharacter = 128;
55 typedef SegmentedVector<Identifier, 64> IdentifierVector;
14957cd0
A
56 void clear()
57 {
58 m_identifiers.clear();
6fe7ccc8 59 for (int i = 0; i < MaximumCachableCharacter; i++)
14957cd0 60 m_shortIdentifiers[i] = 0;
6fe7ccc8
A
61 for (int i = 0; i < MaximumCachableCharacter; i++)
62 m_recentIdentifiers[i] = 0;
14957cd0 63 }
f9bf01c6
A
64
65 private:
f9bf01c6 66 IdentifierVector m_identifiers;
81345200
A
67 std::array<Identifier*, MaximumCachableCharacter> m_shortIdentifiers;
68 std::array<Identifier*, MaximumCachableCharacter> m_recentIdentifiers;
f9bf01c6
A
69 };
70
6fe7ccc8 71 template <typename T>
93a37866 72 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifier(VM* vm, const T* characters, size_t length)
f9bf01c6 73 {
40a37d08
A
74 if (!length)
75 return vm->propertyNames->emptyIdentifier;
6fe7ccc8 76 if (characters[0] >= MaximumCachableCharacter) {
ed1e77d3 77 m_identifiers.append(Identifier::fromString(vm, characters, length));
6fe7ccc8
A
78 return m_identifiers.last();
79 }
80 if (length == 1) {
14957cd0
A
81 if (Identifier* ident = m_shortIdentifiers[characters[0]])
82 return *ident;
ed1e77d3 83 m_identifiers.append(Identifier::fromString(vm, characters, length));
14957cd0
A
84 m_shortIdentifiers[characters[0]] = &m_identifiers.last();
85 return m_identifiers.last();
86 }
6fe7ccc8
A
87 Identifier* ident = m_recentIdentifiers[characters[0]];
88 if (ident && Identifier::equal(ident->impl(), characters, length))
89 return *ident;
ed1e77d3 90 m_identifiers.append(Identifier::fromString(vm, characters, length));
6fe7ccc8 91 m_recentIdentifiers[characters[0]] = &m_identifiers.last();
f9bf01c6
A
92 return m_identifiers.last();
93 }
94
ed1e77d3
A
95 ALWAYS_INLINE const Identifier& IdentifierArena::makeEmptyIdentifier(VM* vm)
96 {
97 return vm->propertyNames->emptyIdentifier;
98 }
99
93a37866 100 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifierLCharFromUChar(VM* vm, const UChar* characters, size_t length)
6fe7ccc8 101 {
40a37d08
A
102 if (!length)
103 return vm->propertyNames->emptyIdentifier;
6fe7ccc8 104 if (characters[0] >= MaximumCachableCharacter) {
93a37866 105 m_identifiers.append(Identifier::createLCharFromUChar(vm, characters, length));
6fe7ccc8
A
106 return m_identifiers.last();
107 }
108 if (length == 1) {
109 if (Identifier* ident = m_shortIdentifiers[characters[0]])
110 return *ident;
ed1e77d3 111 m_identifiers.append(Identifier::fromString(vm, characters, length));
6fe7ccc8
A
112 m_shortIdentifiers[characters[0]] = &m_identifiers.last();
113 return m_identifiers.last();
114 }
115 Identifier* ident = m_recentIdentifiers[characters[0]];
116 if (ident && Identifier::equal(ident->impl(), characters, length))
117 return *ident;
93a37866 118 m_identifiers.append(Identifier::createLCharFromUChar(vm, characters, length));
6fe7ccc8
A
119 m_recentIdentifiers[characters[0]] = &m_identifiers.last();
120 return m_identifiers.last();
121 }
122
93a37866 123 inline const Identifier& IdentifierArena::makeNumericIdentifier(VM* vm, double number)
f9bf01c6 124 {
ed1e77d3 125 m_identifiers.append(Identifier::fromString(vm, String::numberToStringECMAScript(number)));
f9bf01c6
A
126 return m_identifiers.last();
127 }
128
14957cd0
A
129 class ParserArena {
130 WTF_MAKE_NONCOPYABLE(ParserArena);
f9bf01c6
A
131 public:
132 ParserArena();
133 ~ParserArena();
134
ba379fdc
A
135 void swap(ParserArena& otherArena)
136 {
f9bf01c6
A
137 std::swap(m_freeableMemory, otherArena.m_freeableMemory);
138 std::swap(m_freeablePoolEnd, otherArena.m_freeablePoolEnd);
139 m_identifierArena.swap(otherArena.m_identifierArena);
140 m_freeablePools.swap(otherArena.m_freeablePools);
ba379fdc 141 m_deletableObjects.swap(otherArena.m_deletableObjects);
ba379fdc 142 }
ba379fdc 143
f9bf01c6
A
144 void* allocateFreeable(size_t size)
145 {
146 ASSERT(size);
147 ASSERT(size <= freeablePoolSize);
148 size_t alignedSize = alignSize(size);
149 ASSERT(alignedSize <= freeablePoolSize);
150 if (UNLIKELY(static_cast<size_t>(m_freeablePoolEnd - m_freeableMemory) < alignedSize))
151 allocateFreeablePool();
152 void* block = m_freeableMemory;
153 m_freeableMemory += alignedSize;
154 return block;
155 }
156
157 void* allocateDeletable(size_t size)
158 {
6fe7ccc8 159 ParserArenaDeletable* deletable = static_cast<ParserArenaDeletable*>(allocateFreeable(size));
f9bf01c6
A
160 m_deletableObjects.append(deletable);
161 return deletable;
162 }
ba379fdc 163
6fe7ccc8
A
164 IdentifierArena& identifierArena()
165 {
166 if (UNLIKELY (!m_identifierArena))
ed1e77d3 167 m_identifierArena = std::make_unique<IdentifierArena>();
6fe7ccc8
A
168 return *m_identifierArena;
169 }
f9bf01c6 170
ba379fdc 171 private:
f9bf01c6
A
172 static const size_t freeablePoolSize = 8000;
173
174 static size_t alignSize(size_t size)
175 {
176 return (size + sizeof(WTF::AllocAlignmentInteger) - 1) & ~(sizeof(WTF::AllocAlignmentInteger) - 1);
177 }
178
179 void* freeablePool();
180 void allocateFreeablePool();
181 void deallocateObjects();
182
183 char* m_freeableMemory;
184 char* m_freeablePoolEnd;
185
ed1e77d3 186 std::unique_ptr<IdentifierArena> m_identifierArena;
f9bf01c6 187 Vector<void*> m_freeablePools;
ba379fdc 188 Vector<ParserArenaDeletable*> m_deletableObjects;
ba379fdc
A
189 };
190
191}
192
193#endif