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 "Identifier.h"
30 #include <wtf/SegmentedVector.h>
34 class ParserArenaDeletable
;
35 class ParserArenaRefCounted
;
37 class IdentifierArena
{
38 WTF_MAKE_FAST_ALLOCATED
;
45 ALWAYS_INLINE
const Identifier
& makeIdentifier(JSGlobalData
*, const UChar
* characters
, size_t length
);
46 const Identifier
& makeNumericIdentifier(JSGlobalData
*, double number
);
50 m_identifiers
.clear();
51 for (unsigned i
= 0; i
< 128; i
++)
52 m_shortIdentifiers
[i
] = 0;
54 bool isEmpty() const { return m_identifiers
.isEmpty(); }
57 static const int MaximumCachableCharacter
= 128;
58 typedef SegmentedVector
<Identifier
, 64> IdentifierVector
;
59 IdentifierVector m_identifiers
;
60 FixedArray
<Identifier
*, MaximumCachableCharacter
> m_shortIdentifiers
;
63 ALWAYS_INLINE
const Identifier
& IdentifierArena::makeIdentifier(JSGlobalData
* globalData
, const UChar
* characters
, size_t length
)
65 if (length
== 1 && characters
[0] < MaximumCachableCharacter
) {
66 if (Identifier
* ident
= m_shortIdentifiers
[characters
[0]])
68 m_identifiers
.append(Identifier(globalData
, characters
, length
));
69 m_shortIdentifiers
[characters
[0]] = &m_identifiers
.last();
70 return m_identifiers
.last();
72 m_identifiers
.append(Identifier(globalData
, characters
, length
));
73 return m_identifiers
.last();
76 inline const Identifier
& IdentifierArena::makeNumericIdentifier(JSGlobalData
* globalData
, double number
)
78 m_identifiers
.append(Identifier(globalData
, UString::number(number
)));
79 return m_identifiers
.last();
83 WTF_MAKE_NONCOPYABLE(ParserArena
);
88 void swap(ParserArena
& otherArena
)
90 std::swap(m_freeableMemory
, otherArena
.m_freeableMemory
);
91 std::swap(m_freeablePoolEnd
, otherArena
.m_freeablePoolEnd
);
92 m_identifierArena
.swap(otherArena
.m_identifierArena
);
93 m_freeablePools
.swap(otherArena
.m_freeablePools
);
94 m_deletableObjects
.swap(otherArena
.m_deletableObjects
);
95 m_refCountedObjects
.swap(otherArena
.m_refCountedObjects
);
98 void* allocateFreeable(size_t size
)
101 ASSERT(size
<= freeablePoolSize
);
102 size_t alignedSize
= alignSize(size
);
103 ASSERT(alignedSize
<= freeablePoolSize
);
104 if (UNLIKELY(static_cast<size_t>(m_freeablePoolEnd
- m_freeableMemory
) < alignedSize
))
105 allocateFreeablePool();
106 void* block
= m_freeableMemory
;
107 m_freeableMemory
+= alignedSize
;
111 void* allocateDeletable(size_t size
)
113 ParserArenaDeletable
* deletable
= static_cast<ParserArenaDeletable
*>(fastMalloc(size
));
114 m_deletableObjects
.append(deletable
);
118 void derefWithArena(PassRefPtr
<ParserArenaRefCounted
>);
119 bool contains(ParserArenaRefCounted
*) const;
120 ParserArenaRefCounted
* last() const;
123 bool isEmpty() const;
126 IdentifierArena
& identifierArena() { return *m_identifierArena
; }
129 static const size_t freeablePoolSize
= 8000;
131 static size_t alignSize(size_t size
)
133 return (size
+ sizeof(WTF::AllocAlignmentInteger
) - 1) & ~(sizeof(WTF::AllocAlignmentInteger
) - 1);
136 void* freeablePool();
137 void allocateFreeablePool();
138 void deallocateObjects();
140 char* m_freeableMemory
;
141 char* m_freeablePoolEnd
;
143 OwnPtr
<IdentifierArena
> m_identifierArena
;
144 Vector
<void*> m_freeablePools
;
145 Vector
<ParserArenaDeletable
*> m_deletableObjects
;
146 Vector
<RefPtr
<ParserArenaRefCounted
> > m_refCountedObjects
;