2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "CommonIdentifiers.h"
30 #include "Identifier.h"
32 #include <wtf/SegmentedVector.h>
36 class ParserArenaDeletable
;
37 class ParserArenaRefCounted
;
39 class IdentifierArena
{
40 WTF_MAKE_FAST_ALLOCATED
;
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
);
51 const Identifier
& makeNumericIdentifier(VM
*, double number
);
53 bool isEmpty() const { return m_identifiers
.isEmpty(); }
56 static const int MaximumCachableCharacter
= 128;
57 typedef SegmentedVector
<Identifier
, 64> IdentifierVector
;
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;
68 IdentifierVector m_identifiers
;
69 std::array
<Identifier
*, MaximumCachableCharacter
> m_shortIdentifiers
;
70 std::array
<Identifier
*, MaximumCachableCharacter
> m_recentIdentifiers
;
74 ALWAYS_INLINE
const Identifier
& IdentifierArena::makeIdentifier(VM
* vm
, const T
* characters
, size_t length
)
77 return vm
->propertyNames
->emptyIdentifier
;
78 if (characters
[0] >= MaximumCachableCharacter
) {
79 m_identifiers
.append(Identifier(vm
, characters
, length
));
80 return m_identifiers
.last();
83 if (Identifier
* ident
= m_shortIdentifiers
[characters
[0]])
85 m_identifiers
.append(Identifier(vm
, characters
, length
));
86 m_shortIdentifiers
[characters
[0]] = &m_identifiers
.last();
87 return m_identifiers
.last();
89 Identifier
* ident
= m_recentIdentifiers
[characters
[0]];
90 if (ident
&& Identifier::equal(ident
->impl(), characters
, length
))
92 m_identifiers
.append(Identifier(vm
, characters
, length
));
93 m_recentIdentifiers
[characters
[0]] = &m_identifiers
.last();
94 return m_identifiers
.last();
97 ALWAYS_INLINE
const Identifier
& IdentifierArena::makeIdentifierLCharFromUChar(VM
* vm
, const UChar
* characters
, size_t 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();
106 if (Identifier
* ident
= m_shortIdentifiers
[characters
[0]])
108 m_identifiers
.append(Identifier(vm
, characters
, length
));
109 m_shortIdentifiers
[characters
[0]] = &m_identifiers
.last();
110 return m_identifiers
.last();
112 Identifier
* ident
= m_recentIdentifiers
[characters
[0]];
113 if (ident
&& Identifier::equal(ident
->impl(), characters
, length
))
115 m_identifiers
.append(Identifier::createLCharFromUChar(vm
, characters
, length
));
116 m_recentIdentifiers
[characters
[0]] = &m_identifiers
.last();
117 return m_identifiers
.last();
120 inline const Identifier
& IdentifierArena::makeNumericIdentifier(VM
* vm
, double number
)
122 m_identifiers
.append(Identifier(vm
, String::numberToStringECMAScript(number
)));
123 return m_identifiers
.last();
127 WTF_MAKE_NONCOPYABLE(ParserArena
);
132 void swap(ParserArena
& otherArena
)
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
);
142 void* allocateFreeable(size_t 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
;
155 void* allocateDeletable(size_t size
)
157 ParserArenaDeletable
* deletable
= static_cast<ParserArenaDeletable
*>(allocateFreeable(size
));
158 m_deletableObjects
.append(deletable
);
162 void derefWithArena(PassRefPtr
<ParserArenaRefCounted
>);
163 bool contains(ParserArenaRefCounted
*) const;
164 ParserArenaRefCounted
* last() const;
167 bool isEmpty() const;
168 JS_EXPORT_PRIVATE
void reset();
170 IdentifierArena
& identifierArena()
172 if (UNLIKELY (!m_identifierArena
))
173 m_identifierArena
= adoptPtr(new IdentifierArena
);
174 return *m_identifierArena
;
178 static const size_t freeablePoolSize
= 8000;
180 static size_t alignSize(size_t size
)
182 return (size
+ sizeof(WTF::AllocAlignmentInteger
) - 1) & ~(sizeof(WTF::AllocAlignmentInteger
) - 1);
185 void* freeablePool();
186 void allocateFreeablePool();
187 void deallocateObjects();
189 char* m_freeableMemory
;
190 char* m_freeablePoolEnd
;
192 OwnPtr
<IdentifierArena
> m_identifierArena
;
193 Vector
<void*> m_freeablePools
;
194 Vector
<ParserArenaDeletable
*> m_deletableObjects
;
195 Vector
<RefPtr
<ParserArenaRefCounted
>> m_refCountedObjects
;