]> git.saurik.com Git - apple/javascriptcore.git/blame - parser/ParserArena.h
JavaScriptCore-903.5.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
f9bf01c6
A
29#include "Identifier.h"
30#include <wtf/SegmentedVector.h>
ba379fdc
A
31
32namespace JSC {
33
34 class ParserArenaDeletable;
35 class ParserArenaRefCounted;
36
14957cd0
A
37 class IdentifierArena {
38 WTF_MAKE_FAST_ALLOCATED;
ba379fdc 39 public:
14957cd0
A
40 IdentifierArena()
41 {
42 clear();
43 }
44
f9bf01c6
A
45 ALWAYS_INLINE const Identifier& makeIdentifier(JSGlobalData*, const UChar* characters, size_t length);
46 const Identifier& makeNumericIdentifier(JSGlobalData*, double number);
47
14957cd0
A
48 void clear()
49 {
50 m_identifiers.clear();
51 for (unsigned i = 0; i < 128; i++)
52 m_shortIdentifiers[i] = 0;
53 }
f9bf01c6
A
54 bool isEmpty() const { return m_identifiers.isEmpty(); }
55
56 private:
14957cd0 57 static const int MaximumCachableCharacter = 128;
f9bf01c6
A
58 typedef SegmentedVector<Identifier, 64> IdentifierVector;
59 IdentifierVector m_identifiers;
14957cd0 60 FixedArray<Identifier*, MaximumCachableCharacter> m_shortIdentifiers;
f9bf01c6
A
61 };
62
63 ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifier(JSGlobalData* globalData, const UChar* characters, size_t length)
64 {
14957cd0
A
65 if (length == 1 && characters[0] < MaximumCachableCharacter) {
66 if (Identifier* ident = m_shortIdentifiers[characters[0]])
67 return *ident;
68 m_identifiers.append(Identifier(globalData, characters, length));
69 m_shortIdentifiers[characters[0]] = &m_identifiers.last();
70 return m_identifiers.last();
71 }
f9bf01c6
A
72 m_identifiers.append(Identifier(globalData, characters, length));
73 return m_identifiers.last();
74 }
75
76 inline const Identifier& IdentifierArena::makeNumericIdentifier(JSGlobalData* globalData, double number)
77 {
14957cd0 78 m_identifiers.append(Identifier(globalData, UString::number(number)));
f9bf01c6
A
79 return m_identifiers.last();
80 }
81
14957cd0
A
82 class ParserArena {
83 WTF_MAKE_NONCOPYABLE(ParserArena);
f9bf01c6
A
84 public:
85 ParserArena();
86 ~ParserArena();
87
ba379fdc
A
88 void swap(ParserArena& otherArena)
89 {
f9bf01c6
A
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);
ba379fdc
A
94 m_deletableObjects.swap(otherArena.m_deletableObjects);
95 m_refCountedObjects.swap(otherArena.m_refCountedObjects);
96 }
ba379fdc 97
f9bf01c6
A
98 void* allocateFreeable(size_t size)
99 {
100 ASSERT(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;
108 return block;
109 }
110
111 void* allocateDeletable(size_t size)
112 {
113 ParserArenaDeletable* deletable = static_cast<ParserArenaDeletable*>(fastMalloc(size));
114 m_deletableObjects.append(deletable);
115 return deletable;
116 }
ba379fdc 117
f9bf01c6 118 void derefWithArena(PassRefPtr<ParserArenaRefCounted>);
ba379fdc
A
119 bool contains(ParserArenaRefCounted*) const;
120 ParserArenaRefCounted* last() const;
121 void removeLast();
122
f9bf01c6 123 bool isEmpty() const;
ba379fdc
A
124 void reset();
125
f9bf01c6
A
126 IdentifierArena& identifierArena() { return *m_identifierArena; }
127
ba379fdc 128 private:
f9bf01c6
A
129 static const size_t freeablePoolSize = 8000;
130
131 static size_t alignSize(size_t size)
132 {
133 return (size + sizeof(WTF::AllocAlignmentInteger) - 1) & ~(sizeof(WTF::AllocAlignmentInteger) - 1);
134 }
135
136 void* freeablePool();
137 void allocateFreeablePool();
138 void deallocateObjects();
139
140 char* m_freeableMemory;
141 char* m_freeablePoolEnd;
142
143 OwnPtr<IdentifierArena> m_identifierArena;
144 Vector<void*> m_freeablePools;
ba379fdc
A
145 Vector<ParserArenaDeletable*> m_deletableObjects;
146 Vector<RefPtr<ParserArenaRefCounted> > m_refCountedObjects;
147 };
148
149}
150
151#endif