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);
48 bool isEmpty() const { return m_identifiers.isEmpty(); }
51 static const int MaximumCachableCharacter = 128;
52 typedef SegmentedVector<Identifier, 64> IdentifierVector;
55 m_identifiers.clear();
56 for (int i = 0; i < MaximumCachableCharacter; i++)
57 m_shortIdentifiers[i] = 0;
58 for (int i = 0; i < MaximumCachableCharacter; i++)
59 m_recentIdentifiers[i] = 0;
63 IdentifierVector m_identifiers;
64 FixedArray<Identifier*, MaximumCachableCharacter> m_shortIdentifiers;
65 FixedArray<Identifier*, MaximumCachableCharacter> m_recentIdentifiers;
68 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifier(JSGlobalData* globalData, const UChar* characters, size_t length)
70 if (characters[0] >= MaximumCachableCharacter) {
71 m_identifiers.append(Identifier(globalData, characters, length));
72 return m_identifiers.last();
75 if (Identifier* ident = m_shortIdentifiers[characters[0]])
77 m_identifiers.append(Identifier(globalData, characters, length));
78 m_shortIdentifiers[characters[0]] = &m_identifiers.last();
79 return m_identifiers.last();
81 Identifier* ident = m_recentIdentifiers[characters[0]];
82 if (ident && Identifier::equal(ident->impl(), characters, length))
84 m_identifiers.append(Identifier(globalData, characters, length));
85 m_recentIdentifiers[characters[0]] = &m_identifiers.last();
86 return m_identifiers.last();
89 inline const Identifier& IdentifierArena::makeNumericIdentifier(JSGlobalData* globalData, double number)
91 m_identifiers.append(Identifier(globalData, UString::number(number)));
92 return m_identifiers.last();
96 WTF_MAKE_NONCOPYABLE(ParserArena);
101 void swap(ParserArena& otherArena)
103 std::swap(m_freeableMemory, otherArena.m_freeableMemory);
104 std::swap(m_freeablePoolEnd, otherArena.m_freeablePoolEnd);
105 m_identifierArena.swap(otherArena.m_identifierArena);
106 m_freeablePools.swap(otherArena.m_freeablePools);
107 m_deletableObjects.swap(otherArena.m_deletableObjects);
108 m_refCountedObjects.swap(otherArena.m_refCountedObjects);
111 void* allocateFreeable(size_t size)
114 ASSERT(size <= freeablePoolSize);
115 size_t alignedSize = alignSize(size);
116 ASSERT(alignedSize <= freeablePoolSize);
117 if (UNLIKELY(static_cast<size_t>(m_freeablePoolEnd - m_freeableMemory) < alignedSize))
118 allocateFreeablePool();
119 void* block = m_freeableMemory;
120 m_freeableMemory += alignedSize;
124 void* allocateDeletable(size_t size)
126 ParserArenaDeletable* deletable = static_cast<ParserArenaDeletable*>(fastMalloc(size));
127 m_deletableObjects.append(deletable);
131 void derefWithArena(PassRefPtr<ParserArenaRefCounted>);
132 bool contains(ParserArenaRefCounted*) const;
133 ParserArenaRefCounted* last() const;
136 bool isEmpty() const;
139 IdentifierArena& identifierArena() { return *m_identifierArena; }
142 static const size_t freeablePoolSize = 8000;
144 static size_t alignSize(size_t size)
146 return (size + sizeof(WTF::AllocAlignmentInteger) - 1) & ~(sizeof(WTF::AllocAlignmentInteger) - 1);
149 void* freeablePool();
150 void allocateFreeablePool();
151 void deallocateObjects();
153 char* m_freeableMemory;
154 char* m_freeablePoolEnd;
156 OwnPtr<IdentifierArena> m_identifierArena;
157 Vector<void*> m_freeablePools;
158 Vector<ParserArenaDeletable*> m_deletableObjects;
159 Vector<RefPtr<ParserArenaRefCounted> > m_refCountedObjects;