initial import
[vuplus_webkit] / Source / JavaScriptCore / parser / ParserArena.h
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
29 #include "Identifier.h"
30 #include <wtf/SegmentedVector.h>
31
32 namespace JSC {
33
34     class ParserArenaDeletable;
35     class ParserArenaRefCounted;
36
37     class IdentifierArena {
38         WTF_MAKE_FAST_ALLOCATED;
39     public:
40         IdentifierArena()
41         {
42             clear();
43         }
44
45         ALWAYS_INLINE const Identifier& makeIdentifier(JSGlobalData*, const UChar* characters, size_t length);
46         const Identifier& makeNumericIdentifier(JSGlobalData*, double number);
47
48         bool isEmpty() const { return m_identifiers.isEmpty(); }
49
50     public:
51         static const int MaximumCachableCharacter = 128;
52         typedef SegmentedVector<Identifier, 64> IdentifierVector;
53         void clear()
54         {
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;
60         }
61
62     private:
63         IdentifierVector m_identifiers;
64         FixedArray<Identifier*, MaximumCachableCharacter> m_shortIdentifiers;
65         FixedArray<Identifier*, MaximumCachableCharacter> m_recentIdentifiers;
66     };
67
68     ALWAYS_INLINE const Identifier& IdentifierArena::makeIdentifier(JSGlobalData* globalData, const UChar* characters, size_t length)
69     {
70         if (characters[0] >= MaximumCachableCharacter) {
71             m_identifiers.append(Identifier(globalData, characters, length));
72             return m_identifiers.last();
73         }
74         if (length == 1) {
75             if (Identifier* ident = m_shortIdentifiers[characters[0]])
76                 return *ident;
77             m_identifiers.append(Identifier(globalData, characters, length));
78             m_shortIdentifiers[characters[0]] = &m_identifiers.last();
79             return m_identifiers.last();
80         }
81         Identifier* ident = m_recentIdentifiers[characters[0]];
82         if (ident && Identifier::equal(ident->impl(), characters, length))
83             return *ident;
84         m_identifiers.append(Identifier(globalData, characters, length));
85         m_recentIdentifiers[characters[0]] = &m_identifiers.last();
86         return m_identifiers.last();
87     }
88
89     inline const Identifier& IdentifierArena::makeNumericIdentifier(JSGlobalData* globalData, double number)
90     {
91         m_identifiers.append(Identifier(globalData, UString::number(number)));
92         return m_identifiers.last();
93     }
94
95     class ParserArena {
96         WTF_MAKE_NONCOPYABLE(ParserArena);
97     public:
98         ParserArena();
99         ~ParserArena();
100
101         void swap(ParserArena& otherArena)
102         {
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);
109         }
110
111         void* allocateFreeable(size_t size)
112         {
113             ASSERT(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;
121             return block;
122         }
123
124         void* allocateDeletable(size_t size)
125         {
126             ParserArenaDeletable* deletable = static_cast<ParserArenaDeletable*>(fastMalloc(size));
127             m_deletableObjects.append(deletable);
128             return deletable;
129         }
130
131         void derefWithArena(PassRefPtr<ParserArenaRefCounted>);
132         bool contains(ParserArenaRefCounted*) const;
133         ParserArenaRefCounted* last() const;
134         void removeLast();
135
136         bool isEmpty() const;
137         void reset();
138
139         IdentifierArena& identifierArena() { return *m_identifierArena; }
140
141     private:
142         static const size_t freeablePoolSize = 8000;
143
144         static size_t alignSize(size_t size)
145         {
146             return (size + sizeof(WTF::AllocAlignmentInteger) - 1) & ~(sizeof(WTF::AllocAlignmentInteger) - 1);
147         }
148
149         void* freeablePool();
150         void allocateFreeablePool();
151         void deallocateObjects();
152
153         char* m_freeableMemory;
154         char* m_freeablePoolEnd;
155
156         OwnPtr<IdentifierArena> m_identifierArena;
157         Vector<void*> m_freeablePools;
158         Vector<ParserArenaDeletable*> m_deletableObjects;
159         Vector<RefPtr<ParserArenaRefCounted> > m_refCountedObjects;
160     };
161
162 }
163
164 #endif