]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ArgList.h
JavaScriptCore-525.tar.gz
[apple/javascriptcore.git] / runtime / ArgList.h
1 /*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008 Apple Computer, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22 #ifndef ArgList_h
23 #define ArgList_h
24
25 #include "JSImmediate.h"
26 #include "Register.h"
27
28 #include <wtf/HashSet.h>
29 #include <wtf/Noncopyable.h>
30 #include <wtf/Vector.h>
31
32 namespace JSC {
33
34 class ArgList : Noncopyable {
35 private:
36 static const unsigned inlineCapacity = 8;
37 typedef Vector<Register, inlineCapacity> VectorType;
38 typedef HashSet<ArgList*> ListSet;
39
40 public:
41 typedef VectorType::iterator iterator;
42 typedef VectorType::const_iterator const_iterator;
43
44 // Constructor for a read-write list, to which you may append values.
45 // FIXME: Remove all clients of this API, then remove this API.
46 ArgList()
47 : m_markSet(0)
48 #ifndef NDEBUG
49 , m_isReadOnly(false)
50 #endif
51 {
52 m_buffer = m_vector.data();
53 m_size = 0;
54 }
55
56 // Constructor for a read-only list whose data has already been allocated elsewhere.
57 ArgList(Register* buffer, size_t size)
58 : m_buffer(buffer)
59 , m_size(size)
60 , m_markSet(0)
61 #ifndef NDEBUG
62 , m_isReadOnly(true)
63 #endif
64 {
65 }
66
67 void initialize(Register* buffer, size_t size)
68 {
69 ASSERT(!m_markSet);
70 ASSERT(isEmpty());
71
72 m_buffer = buffer;
73 m_size = size;
74 #ifndef NDEBUG
75 m_isReadOnly = true;
76 #endif
77 }
78
79 ~ArgList()
80 {
81 if (m_markSet)
82 m_markSet->remove(this);
83 }
84
85 size_t size() const { return m_size; }
86 bool isEmpty() const { return !m_size; }
87
88 JSValuePtr at(ExecState* exec, size_t i) const
89 {
90 if (i < m_size)
91 return m_buffer[i].jsValue(exec);
92 return jsUndefined();
93 }
94
95 void clear()
96 {
97 m_vector.clear();
98 m_buffer = 0;
99 m_size = 0;
100 }
101
102 void append(JSValuePtr v)
103 {
104 ASSERT(!m_isReadOnly);
105
106 if (m_size < inlineCapacity) {
107 m_vector.uncheckedAppend(v);
108 ++m_size;
109 } else {
110 // Putting this case all in one function measurably improves
111 // the performance of the fast "just append to inline buffer" case.
112 slowAppend(v);
113 ++m_size;
114 }
115 }
116
117 void getSlice(int startIndex, ArgList& result) const;
118
119 iterator begin() { return m_buffer; }
120 iterator end() { return m_buffer + m_size; }
121
122 const_iterator begin() const { return m_buffer; }
123 const_iterator end() const { return m_buffer + m_size; }
124
125 static void markLists(ListSet&);
126
127 private:
128 void slowAppend(JSValuePtr);
129
130 Register* m_buffer;
131 size_t m_size;
132
133 VectorType m_vector;
134 ListSet* m_markSet;
135 #ifndef NDEBUG
136 bool m_isReadOnly;
137 #endif
138
139 private:
140 // Prohibits new / delete, which would break GC.
141 friend class JSGlobalData;
142
143 void* operator new(size_t size)
144 {
145 return fastMalloc(size);
146 }
147 void operator delete(void* p)
148 {
149 fastFree(p);
150 }
151
152 void* operator new[](size_t);
153 void operator delete[](void*);
154
155 void* operator new(size_t, void*);
156 void operator delete(void*, size_t);
157 };
158
159 } // namespace JSC
160
161 #endif // ArgList_h