blob: 4f4ef14ddfe297eccfd9e8bf1a6f28403480e6a0 [file] [log] [blame]
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_HEAP_H_
29#define V8_HEAP_H_
30
ager@chromium.org18ad94b2009-09-02 08:22:29 +000031#include <math.h>
32
lrn@chromium.org1c092762011-05-09 09:42:16 +000033#include "allocation.h"
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +000034#include "globals.h"
35#include "list.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000036#include "mark-compact.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000037#include "spaces.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000038#include "splay-tree-inl.h"
39#include "v8-counters.h"
ager@chromium.org18ad94b2009-09-02 08:22:29 +000040
kasperl@chromium.org71affb52009-05-26 05:44:31 +000041namespace v8 {
42namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000044// TODO(isolates): remove HEAP here
45#define HEAP (_inline_get_heap_())
46class Heap;
47inline Heap* _inline_get_heap_();
48
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000049
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050// Defines all the roots in Heap.
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +000051#define STRONG_ROOT_LIST(V) \
ager@chromium.org3811b432009-10-28 14:53:37 +000052 /* Put the byte array map early. We need it to be in place by the time */ \
53 /* the deserializer hits the next page, since it wants to put a byte */ \
54 /* array in the unused space at the end of the page. */ \
55 V(Map, byte_array_map, ByteArrayMap) \
56 V(Map, one_pointer_filler_map, OnePointerFillerMap) \
57 V(Map, two_pointer_filler_map, TwoPointerFillerMap) \
58 /* Cluster the most popular ones in a few cache lines here at the top. */ \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000059 V(Object, undefined_value, UndefinedValue) \
60 V(Object, the_hole_value, TheHoleValue) \
61 V(Object, null_value, NullValue) \
62 V(Object, true_value, TrueValue) \
63 V(Object, false_value, FalseValue) \
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +000064 V(Object, arguments_marker, ArgumentsMarker) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000065 V(Map, heap_number_map, HeapNumberMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000066 V(Map, global_context_map, GlobalContextMap) \
67 V(Map, fixed_array_map, FixedArrayMap) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +000068 V(Map, fixed_cow_array_map, FixedCOWArrayMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000069 V(Map, fixed_double_array_map, FixedDoubleArrayMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000070 V(Object, no_interceptor_result_sentinel, NoInterceptorResultSentinel) \
71 V(Map, meta_map, MetaMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000072 V(Map, hash_table_map, HashTableMap) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000073 V(Smi, stack_limit, StackLimit) \
74 V(FixedArray, number_string_cache, NumberStringCache) \
75 V(Object, instanceof_cache_function, InstanceofCacheFunction) \
76 V(Object, instanceof_cache_map, InstanceofCacheMap) \
77 V(Object, instanceof_cache_answer, InstanceofCacheAnswer) \
78 V(FixedArray, single_character_string_cache, SingleCharacterStringCache) \
79 V(Object, termination_exception, TerminationException) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000080 V(FixedArray, empty_fixed_array, EmptyFixedArray) \
kasperl@chromium.orga5551262010-12-07 12:49:48 +000081 V(ByteArray, empty_byte_array, EmptyByteArray) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000082 V(FixedDoubleArray, empty_fixed_double_array, EmptyFixedDoubleArray) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000083 V(String, empty_string, EmptyString) \
84 V(DescriptorArray, empty_descriptor_array, EmptyDescriptorArray) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000085 V(Map, string_map, StringMap) \
86 V(Map, ascii_string_map, AsciiStringMap) \
87 V(Map, symbol_map, SymbolMap) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000088 V(Map, cons_string_map, ConsStringMap) \
89 V(Map, cons_ascii_string_map, ConsAsciiStringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000090 V(Map, ascii_symbol_map, AsciiSymbolMap) \
91 V(Map, cons_symbol_map, ConsSymbolMap) \
92 V(Map, cons_ascii_symbol_map, ConsAsciiSymbolMap) \
93 V(Map, external_symbol_map, ExternalSymbolMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000094 V(Map, external_symbol_with_ascii_data_map, ExternalSymbolWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000095 V(Map, external_ascii_symbol_map, ExternalAsciiSymbolMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000096 V(Map, external_string_map, ExternalStringMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000097 V(Map, external_string_with_ascii_data_map, ExternalStringWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000098 V(Map, external_ascii_string_map, ExternalAsciiStringMap) \
99 V(Map, undetectable_string_map, UndetectableStringMap) \
100 V(Map, undetectable_ascii_string_map, UndetectableAsciiStringMap) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000101 V(Map, external_pixel_array_map, ExternalPixelArrayMap) \
ager@chromium.org3811b432009-10-28 14:53:37 +0000102 V(Map, external_byte_array_map, ExternalByteArrayMap) \
103 V(Map, external_unsigned_byte_array_map, ExternalUnsignedByteArrayMap) \
104 V(Map, external_short_array_map, ExternalShortArrayMap) \
105 V(Map, external_unsigned_short_array_map, ExternalUnsignedShortArrayMap) \
106 V(Map, external_int_array_map, ExternalIntArrayMap) \
107 V(Map, external_unsigned_int_array_map, ExternalUnsignedIntArrayMap) \
108 V(Map, external_float_array_map, ExternalFloatArrayMap) \
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000109 V(Map, external_double_array_map, ExternalDoubleArrayMap) \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000110 V(Map, non_strict_arguments_elements_map, NonStrictArgumentsElementsMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000111 V(Map, function_context_map, FunctionContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000112 V(Map, catch_context_map, CatchContextMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000113 V(Map, with_context_map, WithContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000114 V(Map, code_map, CodeMap) \
115 V(Map, oddball_map, OddballMap) \
116 V(Map, global_property_cell_map, GlobalPropertyCellMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000117 V(Map, shared_function_info_map, SharedFunctionInfoMap) \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000118 V(Map, message_object_map, JSMessageObjectMap) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000119 V(Map, foreign_map, ForeignMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000120 V(Object, nan_value, NanValue) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000121 V(Object, minus_zero_value, MinusZeroValue) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000122 V(Map, neander_map, NeanderMap) \
123 V(JSObject, message_listeners, MessageListeners) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000124 V(Foreign, prototype_accessors, PrototypeAccessors) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000125 V(NumberDictionary, code_stubs, CodeStubs) \
126 V(NumberDictionary, non_monomorphic_cache, NonMonomorphicCache) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000127 V(PolymorphicCodeCache, polymorphic_code_cache, PolymorphicCodeCache) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000128 V(Code, js_entry_code, JsEntryCode) \
129 V(Code, js_construct_entry_code, JsConstructEntryCode) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000130 V(FixedArray, natives_source_cache, NativesSourceCache) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000131 V(Object, last_script_id, LastScriptId) \
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000132 V(Script, empty_script, EmptyScript) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000133 V(Smi, real_stack_limit, RealStackLimit) \
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000134 V(StringDictionary, intrinsic_function_names, IntrinsicFunctionNames) \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136#define ROOT_LIST(V) \
137 STRONG_ROOT_LIST(V) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000138 V(SymbolTable, symbol_table, SymbolTable)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139
140#define SYMBOL_LIST(V) \
141 V(Array_symbol, "Array") \
142 V(Object_symbol, "Object") \
143 V(Proto_symbol, "__proto__") \
144 V(StringImpl_symbol, "StringImpl") \
145 V(arguments_symbol, "arguments") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000146 V(Arguments_symbol, "Arguments") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 V(call_symbol, "call") \
148 V(apply_symbol, "apply") \
149 V(caller_symbol, "caller") \
150 V(boolean_symbol, "boolean") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000151 V(Boolean_symbol, "Boolean") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 V(callee_symbol, "callee") \
153 V(constructor_symbol, "constructor") \
154 V(code_symbol, ".code") \
155 V(result_symbol, ".result") \
156 V(catch_var_symbol, ".catch-var") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 V(empty_symbol, "") \
158 V(eval_symbol, "eval") \
159 V(function_symbol, "function") \
160 V(length_symbol, "length") \
161 V(name_symbol, "name") \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000162 V(native_symbol, "native") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 V(number_symbol, "number") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000164 V(Number_symbol, "Number") \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000165 V(nan_symbol, "NaN") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000166 V(RegExp_symbol, "RegExp") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000167 V(source_symbol, "source") \
168 V(global_symbol, "global") \
169 V(ignore_case_symbol, "ignoreCase") \
170 V(multiline_symbol, "multiline") \
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000171 V(input_symbol, "input") \
172 V(index_symbol, "index") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000173 V(last_index_symbol, "lastIndex") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 V(object_symbol, "object") \
175 V(prototype_symbol, "prototype") \
176 V(string_symbol, "string") \
ager@chromium.org7c537e22008-10-16 08:43:32 +0000177 V(String_symbol, "String") \
178 V(Date_symbol, "Date") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 V(this_symbol, "this") \
180 V(to_string_symbol, "toString") \
181 V(char_at_symbol, "CharAt") \
182 V(undefined_symbol, "undefined") \
183 V(value_of_symbol, "valueOf") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 V(InitializeVarGlobal_symbol, "InitializeVarGlobal") \
185 V(InitializeConstGlobal_symbol, "InitializeConstGlobal") \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000186 V(KeyedLoadElementMonomorphic_symbol, \
187 "KeyedLoadElementMonomorphic") \
188 V(KeyedLoadElementPolymorphic_symbol, \
189 "KeyedLoadElementPolymorphic") \
190 V(KeyedStoreElementMonomorphic_symbol, \
191 "KeyedStoreElementMonomorphic") \
192 V(KeyedStoreElementPolymorphic_symbol, \
193 "KeyedStoreElementPolymorphic") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 V(stack_overflow_symbol, "kStackOverflowBoilerplate") \
195 V(illegal_access_symbol, "illegal access") \
196 V(out_of_memory_symbol, "out-of-memory") \
197 V(illegal_execution_state_symbol, "illegal execution state") \
198 V(get_symbol, "get") \
199 V(set_symbol, "set") \
200 V(function_class_symbol, "Function") \
201 V(illegal_argument_symbol, "illegal argument") \
202 V(MakeReferenceError_symbol, "MakeReferenceError") \
203 V(MakeSyntaxError_symbol, "MakeSyntaxError") \
204 V(MakeTypeError_symbol, "MakeTypeError") \
205 V(invalid_lhs_in_assignment_symbol, "invalid_lhs_in_assignment") \
206 V(invalid_lhs_in_for_in_symbol, "invalid_lhs_in_for_in") \
207 V(invalid_lhs_in_postfix_op_symbol, "invalid_lhs_in_postfix_op") \
208 V(invalid_lhs_in_prefix_op_symbol, "invalid_lhs_in_prefix_op") \
209 V(illegal_return_symbol, "illegal_return") \
210 V(illegal_break_symbol, "illegal_break") \
211 V(illegal_continue_symbol, "illegal_continue") \
212 V(unknown_label_symbol, "unknown_label") \
213 V(redeclaration_symbol, "redeclaration") \
214 V(failure_symbol, "<failure>") \
215 V(space_symbol, " ") \
216 V(exec_symbol, "exec") \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000217 V(zero_symbol, "0") \
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000218 V(global_eval_symbol, "GlobalEval") \
ager@chromium.org3811b432009-10-28 14:53:37 +0000219 V(identity_hash_symbol, "v8::IdentityHash") \
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000220 V(closure_symbol, "(closure)") \
ager@chromium.org04921a82011-06-27 13:21:41 +0000221 V(use_strict, "use strict") \
222 V(dot_symbol, ".") \
223 V(anonymous_function_symbol, "(anonymous function)")
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000225// Forward declarations.
kasper.lund7276f142008-07-30 08:49:36 +0000226class GCTracer;
ager@chromium.org60121232009-12-03 11:25:37 +0000227class HeapStats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000228class Isolate;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000229class WeakObjectRetainer;
kasper.lund7276f142008-07-30 08:49:36 +0000230
231
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000232typedef String* (*ExternalStringTableUpdaterCallback)(Heap* heap,
233 Object** pointer);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000234
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235typedef bool (*DirtyRegionCallback)(Heap* heap,
236 Address start,
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000237 Address end,
238 ObjectSlotCallback copy_object_func);
239
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000240
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241// The all static Heap captures the interface to the global object heap.
242// All JavaScript contexts by this process share the same object heap.
243
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000244#ifdef DEBUG
245class HeapDebugUtils;
246#endif
247
248
249// A queue of objects promoted during scavenge. Each object is accompanied
250// by it's size to avoid dereferencing a map pointer for scanning.
251class PromotionQueue {
252 public:
253 PromotionQueue() : front_(NULL), rear_(NULL) { }
254
255 void Initialize(Address start_address) {
256 front_ = rear_ = reinterpret_cast<intptr_t*>(start_address);
257 }
258
259 bool is_empty() { return front_ <= rear_; }
260
261 inline void insert(HeapObject* target, int size);
262
263 void remove(HeapObject** target, int* size) {
264 *target = reinterpret_cast<HeapObject*>(*(--front_));
265 *size = static_cast<int>(*(--front_));
266 // Assert no underflow.
267 ASSERT(front_ >= rear_);
268 }
269
270 private:
271 // The front of the queue is higher in memory than the rear.
272 intptr_t* front_;
273 intptr_t* rear_;
274
275 DISALLOW_COPY_AND_ASSIGN(PromotionQueue);
276};
277
278
279// External strings table is a place where all external strings are
280// registered. We need to keep track of such strings to properly
281// finalize them.
282class ExternalStringTable {
283 public:
284 // Registers an external string.
285 inline void AddString(String* string);
286
287 inline void Iterate(ObjectVisitor* v);
288
289 // Restores internal invariant and gets rid of collected strings.
290 // Must be called after each Iterate() that modified the strings.
291 void CleanUp();
292
293 // Destroys all allocated memory.
294 void TearDown();
295
296 private:
297 ExternalStringTable() { }
298
299 friend class Heap;
300
301 inline void Verify();
302
303 inline void AddOldString(String* string);
304
305 // Notifies the table that only a prefix of the new list is valid.
306 inline void ShrinkNewStrings(int position);
307
308 // To speed up scavenge collections new space string are kept
309 // separate from old space strings.
310 List<Object*> new_space_strings_;
311 List<Object*> old_space_strings_;
312
313 Heap* heap_;
314
315 DISALLOW_COPY_AND_ASSIGN(ExternalStringTable);
316};
317
318
319class Heap {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000320 public:
321 // Configure heap size before setup. Return false if the heap has been
322 // setup already.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000323 bool ConfigureHeap(int max_semispace_size,
324 int max_old_gen_size,
325 int max_executable_size);
326 bool ConfigureHeapDefault();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327
328 // Initializes the global object heap. If create_heap_objects is true,
329 // also creates the basic non-mutable objects.
330 // Returns whether it succeeded.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000331 bool Setup(bool create_heap_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000332
333 // Destroys all memory allocated by the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000334 void TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000335
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000336 // Set the stack limit in the roots_ array. Some architectures generate
337 // code that looks here, because it is faster than loading from the static
338 // jslimit_/real_jslimit_ variable in the StackGuard.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000339 void SetStackLimits();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000340
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000341 // Returns whether Setup has been called.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 bool HasBeenSetup();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343
ager@chromium.org3811b432009-10-28 14:53:37 +0000344 // Returns the maximum amount of memory reserved for the heap. For
345 // the young generation, we reserve 4 times the amount needed for a
346 // semi space. The young generation consists of two semi spaces and
347 // we reserve twice the amount needed for those in order to ensure
348 // that new space can be aligned to its size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000349 intptr_t MaxReserved() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000350 return 4 * reserved_semispace_size_ + max_old_generation_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000352 int MaxSemiSpaceSize() { return max_semispace_size_; }
353 int ReservedSemiSpaceSize() { return reserved_semispace_size_; }
354 int InitialSemiSpaceSize() { return initial_semispace_size_; }
355 intptr_t MaxOldGenerationSize() { return max_old_generation_size_; }
356 intptr_t MaxExecutableSize() { return max_executable_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357
358 // Returns the capacity of the heap in bytes w/o growing. Heap grows when
359 // more spaces are needed until it reaches the limit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000360 intptr_t Capacity();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000361
ager@chromium.org3811b432009-10-28 14:53:37 +0000362 // Returns the amount of memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000363 intptr_t CommittedMemory();
ager@chromium.org3811b432009-10-28 14:53:37 +0000364
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000365 // Returns the amount of executable memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000366 intptr_t CommittedMemoryExecutable();
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000367
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000368 // Returns the available bytes in space w/o growing.
369 // Heap doesn't guarantee that it can allocate an object that requires
370 // all available bytes. Check MaxHeapObjectSize() instead.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000371 intptr_t Available();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000372
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000373 // Returns the maximum object size in paged space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000374 inline int MaxObjectSizeInPagedSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000375
376 // Returns of size of all objects residing in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377 intptr_t SizeOfObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378
379 // Return the starting address and a mask for the new space. And-masking an
380 // address with the mask will result in the start address of the new space
381 // for all addresses in either semispace.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000382 Address NewSpaceStart() { return new_space_.start(); }
383 uintptr_t NewSpaceMask() { return new_space_.mask(); }
384 Address NewSpaceTop() { return new_space_.top(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 NewSpace* new_space() { return &new_space_; }
387 OldSpace* old_pointer_space() { return old_pointer_space_; }
388 OldSpace* old_data_space() { return old_data_space_; }
389 OldSpace* code_space() { return code_space_; }
390 MapSpace* map_space() { return map_space_; }
391 CellSpace* cell_space() { return cell_space_; }
392 LargeObjectSpace* lo_space() { return lo_space_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394 bool always_allocate() { return always_allocate_scope_depth_ != 0; }
395 Address always_allocate_scope_depth_address() {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000396 return reinterpret_cast<Address>(&always_allocate_scope_depth_);
397 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000398 bool linear_allocation() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000399 return linear_allocation_scope_depth_ != 0;
ager@chromium.org3811b432009-10-28 14:53:37 +0000400 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000401
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 Address* NewSpaceAllocationTopAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000403 return new_space_.allocation_top_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 Address* NewSpaceAllocationLimitAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000406 return new_space_.allocation_limit_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000407 }
408
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000409 // Uncommit unused semi space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000410 bool UncommitFromSpace() { return new_space_.UncommitFromSpace(); }
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000411
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 // Allocates and initializes a new JavaScript object based on a
413 // constructor.
414 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
415 // failed.
416 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 MUST_USE_RESULT MaybeObject* AllocateJSObject(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000418 JSFunction* constructor, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000420 // Allocates and initializes a new global object based on a constructor.
421 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
422 // failed.
423 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000424 MUST_USE_RESULT MaybeObject* AllocateGlobalObject(JSFunction* constructor);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000425
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000426 // Returns a deep copy of the JavaScript object.
427 // Properties and elements are copied too.
428 // Returns failure if allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000429 MUST_USE_RESULT MaybeObject* CopyJSObject(JSObject* source);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000430
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431 // Allocates the function prototype.
432 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
433 // failed.
434 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000435 MUST_USE_RESULT MaybeObject* AllocateFunctionPrototype(JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000437 // Allocates a Harmony Proxy.
438 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
439 // failed.
440 // Please note this does not perform a garbage collection.
441 MUST_USE_RESULT MaybeObject* AllocateJSProxy(Object* handler,
442 Object* prototype);
443
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000444 // Reinitialize a JSProxy into an (empty) JSObject. The receiver
445 // must have the same size as an empty object. The object is reinitialized
446 // and behaves as an object that has been freshly allocated.
447 MUST_USE_RESULT MaybeObject* ReinitializeJSProxyAsJSObject(JSProxy* object);
448
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000449 // Reinitialize an JSGlobalProxy based on a constructor. The object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450 // must have the same size as objects allocated using the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000451 // constructor. The object is reinitialized and behaves as an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452 // object that has been freshly allocated using the constructor.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000453 MUST_USE_RESULT MaybeObject* ReinitializeJSGlobalProxy(
454 JSFunction* constructor, JSGlobalProxy* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455
456 // Allocates and initializes a new JavaScript object based on a map.
457 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
458 // failed.
459 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000460 MUST_USE_RESULT MaybeObject* AllocateJSObjectFromMap(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000461 Map* map, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462
463 // Allocates a heap object based on the map.
464 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
465 // failed.
466 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000467 MUST_USE_RESULT MaybeObject* Allocate(Map* map, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468
469 // Allocates a JS Map in the heap.
470 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
471 // failed.
472 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000473 MUST_USE_RESULT MaybeObject* AllocateMap(InstanceType instance_type,
474 int instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
476 // Allocates a partial map for bootstrapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000477 MUST_USE_RESULT MaybeObject* AllocatePartialMap(InstanceType instance_type,
478 int instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479
480 // Allocate a map for the specified function
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 MUST_USE_RESULT MaybeObject* AllocateInitialMap(JSFunction* fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000482
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000483 // Allocates an empty code cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 MUST_USE_RESULT MaybeObject* AllocateCodeCache();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000485
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000486 // Allocates an empty PolymorphicCodeCache.
487 MUST_USE_RESULT MaybeObject* AllocatePolymorphicCodeCache();
488
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000489 // Clear the Instanceof cache (used when a prototype changes).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000490 inline void ClearInstanceofCache();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000491
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 // Allocates and fully initializes a String. There are two String
493 // encodings: ASCII and two byte. One should choose between the three string
494 // allocation functions based on the encoding of the string buffer used to
495 // initialized the string.
496 // - ...FromAscii initializes the string from a buffer that is ASCII
497 // encoded (it does not check that the buffer is ASCII encoded) and the
498 // result will be ASCII encoded.
499 // - ...FromUTF8 initializes the string from a buffer that is UTF-8
500 // encoded. If the characters are all single-byte characters, the
501 // result will be ASCII encoded, otherwise it will converted to two
502 // byte.
503 // - ...FromTwoByte initializes the string from a buffer that is two-byte
504 // encoded. If the characters are all single-byte characters, the
505 // result will be converted to ASCII, otherwise it will be left as
506 // two-byte.
507 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
508 // failed.
509 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000510 MUST_USE_RESULT MaybeObject* AllocateStringFromAscii(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 Vector<const char> str,
512 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000513 MUST_USE_RESULT inline MaybeObject* AllocateStringFromUtf8(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000514 Vector<const char> str,
515 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000516 MUST_USE_RESULT MaybeObject* AllocateStringFromUtf8Slow(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517 Vector<const char> str,
518 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000519 MUST_USE_RESULT MaybeObject* AllocateStringFromTwoByte(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 Vector<const uc16> str,
521 PretenureFlag pretenure = NOT_TENURED);
522
523 // Allocates a symbol in old space based on the character stream.
524 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
525 // failed.
526 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000527 MUST_USE_RESULT inline MaybeObject* AllocateSymbol(Vector<const char> str,
528 int chars,
529 uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000530
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000531 MUST_USE_RESULT inline MaybeObject* AllocateAsciiSymbol(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000532 Vector<const char> str,
533 uint32_t hash_field);
534
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000535 MUST_USE_RESULT inline MaybeObject* AllocateTwoByteSymbol(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000536 Vector<const uc16> str,
537 uint32_t hash_field);
538
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000539 MUST_USE_RESULT MaybeObject* AllocateInternalSymbol(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000540 unibrow::CharacterStream* buffer, int chars, uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000541
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000542 MUST_USE_RESULT MaybeObject* AllocateExternalSymbol(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000543 Vector<const char> str,
544 int chars);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000545
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 // Allocates and partially initializes a String. There are two String
547 // encodings: ASCII and two byte. These functions allocate a string of the
548 // given length and set its map and length fields. The characters of the
549 // string are uninitialized.
550 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
551 // failed.
552 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000553 MUST_USE_RESULT MaybeObject* AllocateRawAsciiString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 int length,
555 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000556 MUST_USE_RESULT MaybeObject* AllocateRawTwoByteString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 int length,
558 PretenureFlag pretenure = NOT_TENURED);
559
560 // Computes a single character string where the character has code.
561 // A cache is used for ascii codes.
562 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
563 // failed. Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000564 MUST_USE_RESULT MaybeObject* LookupSingleCharacterStringFromCode(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000565 uint16_t code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566
567 // Allocate a byte array of the specified length
568 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
569 // failed.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000570 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000571 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length,
572 PretenureFlag pretenure);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000573
574 // Allocate a non-tenured byte array of the specified length
575 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
576 // failed.
577 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000578 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579
ager@chromium.org3811b432009-10-28 14:53:37 +0000580 // Allocates an external array of the specified length and type.
581 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
582 // failed.
583 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000584 MUST_USE_RESULT MaybeObject* AllocateExternalArray(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000585 int length,
586 ExternalArrayType array_type,
587 void* external_pointer,
588 PretenureFlag pretenure);
ager@chromium.org3811b432009-10-28 14:53:37 +0000589
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000590 // Allocate a tenured JS global property cell.
591 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
592 // failed.
593 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000594 MUST_USE_RESULT MaybeObject* AllocateJSGlobalPropertyCell(Object* value);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 // Allocates a fixed array initialized with undefined values
597 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
598 // failed.
599 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000600 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length,
601 PretenureFlag pretenure);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000602 // Allocates a fixed array initialized with undefined values
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000603 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000605 // Allocates an uninitialized fixed array. It must be filled by the caller.
606 //
607 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
608 // failed.
609 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000610 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedArray(int length);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000611
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000612 // Make a copy of src and return it. Returns
613 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000614 MUST_USE_RESULT inline MaybeObject* CopyFixedArray(FixedArray* src);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000615
616 // Make a copy of src, set the map, and return the copy. Returns
617 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000618 MUST_USE_RESULT MaybeObject* CopyFixedArrayWithMap(FixedArray* src, Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000620 // Make a copy of src and return it. Returns
621 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
622 MUST_USE_RESULT inline MaybeObject* CopyFixedDoubleArray(
623 FixedDoubleArray* src);
624
625 // Make a copy of src, set the map, and return the copy. Returns
626 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
627 MUST_USE_RESULT MaybeObject* CopyFixedDoubleArrayWithMap(
628 FixedDoubleArray* src, Map* map);
629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 // Allocates a fixed array initialized with the hole values.
631 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
632 // failed.
633 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000634 MUST_USE_RESULT MaybeObject* AllocateFixedArrayWithHoles(
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000635 int length,
636 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000638 MUST_USE_RESULT MaybeObject* AllocateRawFixedDoubleArray(
639 int length,
640 PretenureFlag pretenure);
641
642 // Allocates a fixed double array with uninitialized values. Returns
643 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
644 // Please note this does not perform a garbage collection.
645 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedDoubleArray(
646 int length,
647 PretenureFlag pretenure = NOT_TENURED);
648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 // AllocateHashTable is identical to AllocateFixedArray except
650 // that the resulting object has hash_table_map as map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000651 MUST_USE_RESULT MaybeObject* AllocateHashTable(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000652 int length, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
654 // Allocate a global (but otherwise uninitialized) context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000655 MUST_USE_RESULT MaybeObject* AllocateGlobalContext();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656
657 // Allocate a function context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000658 MUST_USE_RESULT MaybeObject* AllocateFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000659 JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000661 // Allocate a catch context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000662 MUST_USE_RESULT MaybeObject* AllocateCatchContext(JSFunction* function,
663 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000664 String* name,
665 Object* thrown_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 // Allocate a 'with' context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000667 MUST_USE_RESULT MaybeObject* AllocateWithContext(JSFunction* function,
668 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000669 JSObject* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670
671 // Allocates a new utility object in the old generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000672 MUST_USE_RESULT MaybeObject* AllocateStruct(InstanceType type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 // Allocates a function initialized with a shared part.
675 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
676 // failed.
677 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000678 MUST_USE_RESULT MaybeObject* AllocateFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000679 Map* function_map,
680 SharedFunctionInfo* shared,
681 Object* prototype,
682 PretenureFlag pretenure = TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000684 // Arguments object size.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000685 static const int kArgumentsObjectSize =
686 JSObject::kHeaderSize + 2 * kPointerSize;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000687 // Strict mode arguments has no callee so it is smaller.
688 static const int kArgumentsObjectSizeStrict =
689 JSObject::kHeaderSize + 1 * kPointerSize;
690 // Indicies for direct access into argument objects.
691 static const int kArgumentsLengthIndex = 0;
692 // callee is only valid in non-strict mode.
693 static const int kArgumentsCalleeIndex = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694
695 // Allocates an arguments object - optionally with an elements array.
696 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
697 // failed.
698 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000699 MUST_USE_RESULT MaybeObject* AllocateArgumentsObject(
700 Object* callee, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 // Same as NewNumberFromDouble, but may return a preallocated/immutable
703 // number object (e.g., minus_zero_value_, nan_value_)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000704 MUST_USE_RESULT MaybeObject* NumberFromDouble(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000705 double value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
707 // Allocated a HeapNumber from value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000708 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000709 double value,
710 PretenureFlag pretenure);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000711 // pretenure = NOT_TENURED
712 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(double value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713
714 // Converts an int into either a Smi or a HeapNumber object.
715 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
716 // failed.
717 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000718 MUST_USE_RESULT inline MaybeObject* NumberFromInt32(int32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719
720 // Converts an int into either a Smi or a HeapNumber object.
721 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
722 // failed.
723 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000724 MUST_USE_RESULT inline MaybeObject* NumberFromUint32(uint32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000726 // Allocates a new foreign object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
728 // failed.
729 // Please note this does not perform a garbage collection.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000730 MUST_USE_RESULT MaybeObject* AllocateForeign(
731 Address address, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732
733 // Allocates a new SharedFunctionInfo object.
734 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
735 // failed.
736 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000737 MUST_USE_RESULT MaybeObject* AllocateSharedFunctionInfo(Object* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000739 // Allocates a new JSMessageObject object.
740 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
741 // failed.
742 // Please note that this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000743 MUST_USE_RESULT MaybeObject* AllocateJSMessageObject(
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000744 String* type,
745 JSArray* arguments,
746 int start_position,
747 int end_position,
748 Object* script,
749 Object* stack_trace,
750 Object* stack_frames);
751
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 // Allocates a new cons string object.
753 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
754 // failed.
755 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000756 MUST_USE_RESULT MaybeObject* AllocateConsString(String* first,
757 String* second);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 // Allocates a new sub string object which is a substring of an underlying
760 // string buffer stretching from the index start (inclusive) to the index
761 // end (exclusive).
762 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
763 // failed.
764 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000765 MUST_USE_RESULT MaybeObject* AllocateSubString(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000766 String* buffer,
767 int start,
768 int end,
769 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770
771 // Allocate a new external string object, which is backed by a string
772 // resource that resides outside the V8 heap.
773 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
774 // failed.
775 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000776 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromAscii(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 ExternalAsciiString::Resource* resource);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000778 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromTwoByte(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 ExternalTwoByteString::Resource* resource);
780
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000781 // Finalizes an external string by deleting the associated external
782 // data and clearing the resource pointer.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000783 inline void FinalizeExternalString(String* string);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000784
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000785 // Allocates an uninitialized object. The memory is non-executable if the
786 // hardware and OS allow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
788 // failed.
789 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000790 MUST_USE_RESULT inline MaybeObject* AllocateRaw(int size_in_bytes,
791 AllocationSpace space,
792 AllocationSpace retry_space);
kasper.lund7276f142008-07-30 08:49:36 +0000793
ager@chromium.org6f10e412009-02-13 10:11:16 +0000794 // Initialize a filler object to keep the ability to iterate over the heap
795 // when shortening objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000796 void CreateFillerObjectAt(Address addr, int size);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000797
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 // Makes a new native code object
799 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000800 // failed. On success, the pointer to the Code object is stored in the
801 // self_reference. This allows generated code to reference its own Code
802 // object by containing this pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000804 MUST_USE_RESULT MaybeObject* CreateCode(const CodeDesc& desc,
805 Code::Flags flags,
806 Handle<Object> self_reference,
807 bool immovable = false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000809 MUST_USE_RESULT MaybeObject* CopyCode(Code* code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000810
811 // Copy the code and scope info part of the code object, but insert
812 // the provided data as the relocation information.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000813 MUST_USE_RESULT MaybeObject* CopyCode(Code* code, Vector<byte> reloc_info);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000814
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 // Finds the symbol for string in the symbol table.
816 // If not found, a new symbol is added to the table and returned.
817 // Returns Failure::RetryAfterGC(requested_bytes, space) if allocation
818 // failed.
819 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000820 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str);
821 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str);
822 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000823 Vector<const uc16> str);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000824 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(const char* str) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825 return LookupSymbol(CStrVector(str));
826 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000827 MUST_USE_RESULT MaybeObject* LookupSymbol(String* str);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000828 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Handle<SeqAsciiString> string,
829 int from,
830 int length);
831
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000832 bool LookupSymbolIfExists(String* str, String** symbol);
833 bool LookupTwoCharsSymbolIfExists(String* str, String** symbol);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000834
835 // Compute the matching symbol map for a string if possible.
836 // NULL is returned if string is in new space or not flattened.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000837 Map* SymbolMapForString(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000838
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000839 // Tries to flatten a string before compare operation.
840 //
841 // Returns a failure in case it was decided that flattening was
842 // necessary and failed. Note, if flattening is not necessary the
843 // string might stay non-flat even when not a failure is returned.
844 //
845 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000846 MUST_USE_RESULT inline MaybeObject* PrepareForCompare(String* str);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000847
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 // Converts the given boolean condition to JavaScript boolean value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000849 inline Object* ToBoolean(bool condition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850
851 // Code that should be run before and after each GC. Includes some
852 // reporting/verification activities when compiled with DEBUG set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000853 void GarbageCollectionPrologue();
854 void GarbageCollectionEpilogue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
856 // Performs garbage collection operation.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000857 // Returns whether there is a chance that another major GC could
858 // collect more garbage.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000859 bool CollectGarbage(AllocationSpace space, GarbageCollector collector);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000860
861 // Performs garbage collection operation.
862 // Returns whether there is a chance that another major GC could
863 // collect more garbage.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000864 inline bool CollectGarbage(AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000866 // Performs a full garbage collection. Force compaction if the
867 // parameter is true.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000868 void CollectAllGarbage(bool force_compaction);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000869
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000870 // Last hope GC, should try to squeeze as much as possible.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000871 void CollectAllAvailableGarbage();
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000872
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000873 // Notify the heap that a context has been disposed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000874 int NotifyContextDisposed() { return ++contexts_disposed_; }
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000875
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 // Utility to invoke the scavenger. This is needed in test code to
877 // ensure correct callback for weak global handles.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000878 void PerformScavenge();
879
880 PromotionQueue* promotion_queue() { return &promotion_queue_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000882#ifdef DEBUG
883 // Utility used with flag gc-greedy.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000884 void GarbageCollectionGreedyCheck();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000885#endif
886
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000887 void AddGCPrologueCallback(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000888 GCEpilogueCallback callback, GCType gc_type_filter);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000889 void RemoveGCPrologueCallback(GCEpilogueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000890
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000891 void AddGCEpilogueCallback(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000892 GCEpilogueCallback callback, GCType gc_type_filter);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000893 void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000894
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000895 void SetGlobalGCPrologueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000896 ASSERT((callback == NULL) ^ (global_gc_prologue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 global_gc_prologue_callback_ = callback;
898 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000899 void SetGlobalGCEpilogueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000900 ASSERT((callback == NULL) ^ (global_gc_epilogue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 global_gc_epilogue_callback_ = callback;
902 }
903
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000904 // Heap root getters. We have versions with and without type::cast() here.
905 // You can't use type::cast during GC because the assert fails.
906#define ROOT_ACCESSOR(type, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000907 type* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000908 return type::cast(roots_[k##camel_name##RootIndex]); \
909 } \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000910 type* raw_unchecked_##name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000911 return reinterpret_cast<type*>(roots_[k##camel_name##RootIndex]); \
912 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 ROOT_LIST(ROOT_ACCESSOR)
914#undef ROOT_ACCESSOR
915
916// Utility type maps
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000917#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000918 Map* name##_map() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000919 return Map::cast(roots_[k##Name##MapRootIndex]); \
920 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
922#undef STRUCT_MAP_ACCESSOR
923
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000924#define SYMBOL_ACCESSOR(name, str) String* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000925 return String::cast(roots_[k##name##RootIndex]); \
926 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 SYMBOL_LIST(SYMBOL_ACCESSOR)
928#undef SYMBOL_ACCESSOR
929
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000930 // The hidden_symbol is special because it is the empty string, but does
931 // not match the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000932 String* hidden_symbol() { return hidden_symbol_; }
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000933
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000934 void set_global_contexts_list(Object* object) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000935 global_contexts_list_ = object;
936 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000937 Object* global_contexts_list() { return global_contexts_list_; }
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000938
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 // Iterates over all roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000940 void IterateRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 // Iterates over all strong roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000942 void IterateStrongRoots(ObjectVisitor* v, VisitMode mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000943 // Iterates over all the other roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000944 void IterateWeakRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000946 enum ExpectedPageWatermarkState {
947 WATERMARK_SHOULD_BE_VALID,
948 WATERMARK_CAN_BE_INVALID
949 };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000951 // For each dirty region on a page in use from an old space call
952 // visit_dirty_region callback.
953 // If either visit_dirty_region or callback can cause an allocation
954 // in old space and changes in allocation watermark then
955 // can_preallocate_during_iteration should be set to true.
956 // All pages will be marked as having invalid watermark upon
957 // iteration completion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000958 void IterateDirtyRegions(
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000959 PagedSpace* space,
960 DirtyRegionCallback visit_dirty_region,
961 ObjectSlotCallback callback,
962 ExpectedPageWatermarkState expected_page_watermark_state);
963
964 // Interpret marks as a bitvector of dirty marks for regions of size
965 // Page::kRegionSize aligned by Page::kRegionAlignmentMask and covering
966 // memory interval from start to top. For each dirty region call a
967 // visit_dirty_region callback. Return updated bitvector of dirty marks.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000968 uint32_t IterateDirtyRegions(uint32_t marks,
969 Address start,
970 Address end,
971 DirtyRegionCallback visit_dirty_region,
972 ObjectSlotCallback callback);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000973
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000974 // Iterate pointers to from semispace of new space found in memory interval
975 // from start to end.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000976 // Update dirty marks for page containing start address.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000977 void IterateAndMarkPointersToFromSpace(Address start,
978 Address end,
979 ObjectSlotCallback callback);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000980
981 // Iterate pointers to new space found in memory interval from start to end.
982 // Return true if pointers to new space was found.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000983 static bool IteratePointersInDirtyRegion(Heap* heap,
984 Address start,
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000985 Address end,
986 ObjectSlotCallback callback);
987
988
989 // Iterate pointers to new space found in memory interval from start to end.
990 // This interval is considered to belong to the map space.
991 // Return true if pointers to new space was found.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000992 static bool IteratePointersInDirtyMapsRegion(Heap* heap,
993 Address start,
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000994 Address end,
995 ObjectSlotCallback callback);
996
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000997
998 // Returns whether the object resides in new space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000999 inline bool InNewSpace(Object* object);
1000 inline bool InFromSpace(Object* object);
1001 inline bool InToSpace(Object* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002
1003 // Checks whether an address/object in the heap (including auxiliary
1004 // area and unused area).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001005 bool Contains(Address addr);
1006 bool Contains(HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007
1008 // Checks whether an address/object in a space.
ager@chromium.org3811b432009-10-28 14:53:37 +00001009 // Currently used by tests, serialization and heap verification only.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001010 bool InSpace(Address addr, AllocationSpace space);
1011 bool InSpace(HeapObject* value, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012
kasper.lund7276f142008-07-30 08:49:36 +00001013 // Finds out which space an object should get promoted to based on its type.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001014 inline OldSpace* TargetSpace(HeapObject* object);
1015 inline AllocationSpace TargetSpaceId(InstanceType type);
kasper.lund7276f142008-07-30 08:49:36 +00001016
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 // Sets the stub_cache_ (only used when expanding the dictionary).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001018 void public_set_code_stubs(NumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001019 roots_[kCodeStubsRootIndex] = value;
1020 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001022 // Support for computing object sizes for old objects during GCs. Returns
1023 // a function that is guaranteed to be safe for computing object sizes in
1024 // the current GC phase.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001025 HeapObjectCallback GcSafeSizeOfOldObjectFunction() {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001026 return gc_safe_size_of_old_object_;
1027 }
1028
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 // Sets the non_monomorphic_cache_ (only used when expanding the dictionary).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001030 void public_set_non_monomorphic_cache(NumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001031 roots_[kNonMonomorphicCacheRootIndex] = value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 }
1033
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001034 void public_set_empty_script(Script* script) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001035 roots_[kEmptyScriptRootIndex] = script;
1036 }
1037
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001038 // Update the next script id.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001039 inline void SetLastScriptId(Object* last_script_id);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001040
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001041 // Generated code can embed this address to get access to the roots.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001042 Object** roots_address() { return roots_; }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001043
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001044 // Get address of global contexts list for serialization support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001045 Object** global_contexts_list_address() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001046 return &global_contexts_list_;
1047 }
1048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001050 void Print();
1051 void PrintHandles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052
1053 // Verify the heap is in its normal state before or after a GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001054 void Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055
1056 // Report heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001057 void ReportHeapStatistics(const char* title);
1058 void ReportCodeStatistics(const char* title);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059
1060 // Fill in bogus values in from space
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001061 void ZapFromSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062#endif
1063
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001064 // Print short heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001065 void PrintShortHeapStatistics();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 // Makes a new symbol object
1068 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1069 // failed.
1070 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001071 MUST_USE_RESULT MaybeObject* CreateSymbol(
1072 const char* str, int length, int hash);
1073 MUST_USE_RESULT MaybeObject* CreateSymbol(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074
1075 // Write barrier support for address[offset] = o.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001076 inline void RecordWrite(Address address, int offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001078 // Write barrier support for address[start : start + len[ = o.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001079 inline void RecordWrites(Address address, int start, int len);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001080
kasper.lund7276f142008-07-30 08:49:36 +00001081 // Given an address occupied by a live code object, return that object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001082 Object* FindCodeObject(Address a);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083
1084 // Invoke Shrink on shrinkable spaces.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001085 void Shrink();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086
1087 enum HeapState { NOT_IN_GC, SCAVENGE, MARK_COMPACT };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001088 inline HeapState gc_state() { return gc_state_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001090 inline bool IsInGCPostProcessing() { return gc_post_processing_depth_ > 0; }
1091
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001093 bool IsAllocationAllowed() { return allocation_allowed_; }
1094 inline bool allow_allocation(bool enable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001096 bool disallow_allocation_failure() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097 return disallow_allocation_failure_;
1098 }
1099
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001100 void TracePathToObject(Object* target);
1101 void TracePathToGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102#endif
1103
ager@chromium.org32912102009-01-16 10:38:43 +00001104 // Callback function passed to Heap::Iterate etc. Copies an object if
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 // necessary, the object might be promoted to an old space. The caller must
1106 // ensure the precondition that the object is (a) a heap object and (b) in
1107 // the heap's from space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001108 static inline void ScavengePointer(HeapObject** p);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001109 static inline void ScavengeObject(HeapObject** p, HeapObject* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001111 // Commits from space if it is uncommitted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001112 void EnsureFromSpaceIsCommitted();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001113
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001114 // Support for partial snapshots. After calling this we can allocate a
1115 // certain number of bytes using only linear allocation (with a
1116 // LinearAllocationScope and an AlwaysAllocateScope) without using freelists
1117 // or causing a GC. It returns true of space was reserved or false if a GC is
1118 // needed. For paged spaces the space requested must include the space wasted
1119 // at the end of each page when allocating linearly.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001120 void ReserveSpace(
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001121 int new_space_size,
1122 int pointer_space_size,
1123 int data_space_size,
1124 int code_space_size,
1125 int map_space_size,
1126 int cell_space_size,
1127 int large_object_size);
1128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 //
1130 // Support for the API.
1131 //
1132
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 bool CreateApiObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134
1135 // Attempt to find the number in a small cache. If we finds it, return
1136 // the string representation of the number. Otherwise return undefined.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001137 Object* GetNumberStringCache(Object* number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138
1139 // Update the cache with a new number-string pair.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001140 void SetNumberStringCache(Object* number, String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141
kasper.lund7276f142008-07-30 08:49:36 +00001142 // Adjusts the amount of registered external memory.
1143 // Returns the adjusted value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001144 inline int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00001145
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001146 // Allocate uninitialized fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001147 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length);
1148 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length,
1149 PretenureFlag pretenure);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001150
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001151 // True if we have reached the allocation limit in the old generation that
1152 // should force the next GC (caused normally) to be a full one.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001153 bool OldGenerationPromotionLimitReached() {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001154 return (PromotedSpaceSize() + PromotedExternalMemorySize())
1155 > old_gen_promotion_limit_;
1156 }
1157
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001158 intptr_t OldGenerationSpaceAvailable() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001159 return old_gen_allocation_limit_ -
1160 (PromotedSpaceSize() + PromotedExternalMemorySize());
1161 }
1162
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001163 // True if we have reached the allocation limit in the old generation that
1164 // should artificially cause a GC right now.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001165 bool OldGenerationAllocationLimitReached() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001166 return OldGenerationSpaceAvailable() < 0;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001167 }
1168
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001169 // Can be called when the embedding application is idle.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 bool IdleNotification();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001171
1172 // Declare all the root indices.
1173 enum RootListIndex {
1174#define ROOT_INDEX_DECLARATION(type, name, camel_name) k##camel_name##RootIndex,
1175 STRONG_ROOT_LIST(ROOT_INDEX_DECLARATION)
1176#undef ROOT_INDEX_DECLARATION
1177
1178// Utility type maps
1179#define DECLARE_STRUCT_MAP(NAME, Name, name) k##Name##MapRootIndex,
1180 STRUCT_LIST(DECLARE_STRUCT_MAP)
1181#undef DECLARE_STRUCT_MAP
1182
1183#define SYMBOL_INDEX_DECLARATION(name, str) k##name##RootIndex,
1184 SYMBOL_LIST(SYMBOL_INDEX_DECLARATION)
1185#undef SYMBOL_DECLARATION
1186
1187 kSymbolTableRootIndex,
1188 kStrongRootListLength = kSymbolTableRootIndex,
1189 kRootListLength
1190 };
1191
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001192 MUST_USE_RESULT MaybeObject* NumberToString(
1193 Object* number, bool check_number_string_cache = true);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001194
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001195 Map* MapForExternalArrayType(ExternalArrayType array_type);
1196 RootListIndex RootIndexForExternalArrayType(
ager@chromium.org3811b432009-10-28 14:53:37 +00001197 ExternalArrayType array_type);
1198
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001199 void RecordStats(HeapStats* stats, bool take_snapshot = false);
ager@chromium.org60121232009-12-03 11:25:37 +00001200
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001201 // Copy block of memory from src to dst. Size of block should be aligned
1202 // by pointer size.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001203 static inline void CopyBlock(Address dst, Address src, int byte_size);
1204
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001205 inline void CopyBlockToOldSpaceAndUpdateRegionMarks(Address dst,
1206 Address src,
1207 int byte_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001208
1209 // Optimized version of memmove for blocks with pointer size aligned sizes and
1210 // pointer size aligned addresses.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001211 static inline void MoveBlock(Address dst, Address src, int byte_size);
1212
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001213 inline void MoveBlockToOldSpaceAndUpdateRegionMarks(Address dst,
1214 Address src,
1215 int byte_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001216
1217 // Check new space expansion criteria and expand semispaces if it was hit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001218 void CheckNewSpaceExpansionCriteria();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001219
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001220 inline void IncrementYoungSurvivorsCounter(int survived) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001221 young_survivors_after_last_gc_ = survived;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001222 survived_since_last_expansion_ += survived;
1223 }
1224
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001225 void UpdateNewSpaceReferencesInExternalStringTable(
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001226 ExternalStringTableUpdaterCallback updater_func);
1227
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001228 void ProcessWeakReferences(WeakObjectRetainer* retainer);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001229
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001230 // Helper function that governs the promotion policy from new space to
1231 // old. If the object's old address lies below the new space's age
1232 // mark or if we've already filled the bottom 1/16th of the to space,
1233 // we try to promote this object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001234 inline bool ShouldBePromoted(Address old_address, int object_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001235
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001236 int MaxObjectSizeInNewSpace() { return kMaxObjectSizeInNewSpace; }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001237
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001238 void ClearJSFunctionResultCaches();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001239
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001240 void ClearNormalizedMapCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00001241
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001242 GCTracer* tracer() { return tracer_; }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001243
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001244 double total_regexp_code_generated() { return total_regexp_code_generated_; }
1245 void IncreaseTotalRegexpCodeGenerated(int size) {
1246 total_regexp_code_generated_ += size;
1247 }
1248
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001249 // Returns maximum GC pause.
1250 int get_max_gc_pause() { return max_gc_pause_; }
1251
1252 // Returns maximum size of objects alive after GC.
1253 intptr_t get_max_alive_after_gc() { return max_alive_after_gc_; }
1254
1255 // Returns minimal interval between two subsequent collections.
1256 int get_min_in_mutator() { return min_in_mutator_; }
1257
1258 MarkCompactCollector* mark_compact_collector() {
1259 return &mark_compact_collector_;
1260 }
1261
1262 ExternalStringTable* external_string_table() {
1263 return &external_string_table_;
1264 }
1265
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001266 // Returns the current sweep generation.
1267 int sweep_generation() {
1268 return sweep_generation_;
1269 }
1270
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001271 inline Isolate* isolate();
1272 bool is_safe_to_read_maps() { return is_safe_to_read_maps_; }
1273
1274 void CallGlobalGCPrologueCallback() {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001275 if (global_gc_prologue_callback_ != NULL) global_gc_prologue_callback_();
1276 }
1277
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001278 void CallGlobalGCEpilogueCallback() {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001279 if (global_gc_epilogue_callback_ != NULL) global_gc_epilogue_callback_();
1280 }
1281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001283 Heap();
1284
1285 // This can be calculated directly from a pointer to the heap; however, it is
1286 // more expedient to get at the isolate directly from within Heap methods.
1287 Isolate* isolate_;
1288
1289 int reserved_semispace_size_;
1290 int max_semispace_size_;
1291 int initial_semispace_size_;
1292 intptr_t max_old_generation_size_;
1293 intptr_t max_executable_size_;
1294 intptr_t code_range_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001296 // For keeping track of how much data has survived
1297 // scavenge since last new space expansion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001298 int survived_since_last_expansion_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001300 // For keeping track on when to flush RegExp code.
1301 int sweep_generation_;
1302
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001303 int always_allocate_scope_depth_;
1304 int linear_allocation_scope_depth_;
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00001305
1306 // For keeping track of context disposals.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001307 int contexts_disposed_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001308
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001309#if defined(V8_TARGET_ARCH_X64)
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001310 static const int kMaxObjectSizeInNewSpace = 1024*KB;
lrn@chromium.org8541d772010-12-15 12:05:09 +00001311#else
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001312 static const int kMaxObjectSizeInNewSpace = 512*KB;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001313#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001314
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001315 NewSpace new_space_;
1316 OldSpace* old_pointer_space_;
1317 OldSpace* old_data_space_;
1318 OldSpace* code_space_;
1319 MapSpace* map_space_;
1320 CellSpace* cell_space_;
1321 LargeObjectSpace* lo_space_;
1322 HeapState gc_state_;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001323 int gc_post_processing_depth_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324
1325 // Returns the size of object residing in non new spaces.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001326 intptr_t PromotedSpaceSize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327
kasper.lund7276f142008-07-30 08:49:36 +00001328 // Returns the amount of external memory registered since last global gc.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001329 int PromotedExternalMemorySize();
kasper.lund7276f142008-07-30 08:49:36 +00001330
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001331 int mc_count_; // how many mark-compact collections happened
1332 int ms_count_; // how many mark-sweep collections happened
1333 unsigned int gc_count_; // how many gc happened
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001335 // Total length of the strings we failed to flatten since the last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001336 int unflattened_strings_length_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001337
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001338#define ROOT_ACCESSOR(type, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001339 inline void set_##name(type* value) { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001340 roots_[k##camel_name##RootIndex] = value; \
1341 }
1342 ROOT_LIST(ROOT_ACCESSOR)
1343#undef ROOT_ACCESSOR
1344
kasper.lund7276f142008-07-30 08:49:36 +00001345#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001346 bool allocation_allowed_;
kasper.lund7276f142008-07-30 08:49:36 +00001347
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 // If the --gc-interval flag is set to a positive value, this
1349 // variable holds the value indicating the number of allocations
1350 // remain until the next failure and garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001351 int allocation_timeout_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352
1353 // Do we expect to be able to handle allocation failure at this
1354 // time?
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001355 bool disallow_allocation_failure_;
1356
1357 HeapDebugUtils* debug_utils_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358#endif // DEBUG
1359
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001360 // Limit that triggers a global GC on the next (normally caused) GC. This
1361 // is checked when we have already decided to do a GC to help determine
1362 // which collector to invoke.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001363 intptr_t old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001364
1365 // Limit that triggers a global GC as soon as is reasonable. This is
1366 // checked before expanding a paged space in the old generation and on
1367 // every allocation in large object space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001368 intptr_t old_gen_allocation_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001370 // Limit on the amount of externally allocated memory allowed
1371 // between global GCs. If reached a global GC is forced.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001372 intptr_t external_allocation_limit_;
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001373
kasper.lund7276f142008-07-30 08:49:36 +00001374 // The amount of external memory registered through the API kept alive
1375 // by global handles
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001376 int amount_of_external_allocated_memory_;
kasper.lund7276f142008-07-30 08:49:36 +00001377
1378 // Caches the amount of external memory registered at the last global gc.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001379 int amount_of_external_allocated_memory_at_last_global_gc_;
kasper.lund7276f142008-07-30 08:49:36 +00001380
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 // Indicates that an allocation has failed in the old generation since the
1382 // last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001383 int old_gen_exhausted_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001385 Object* roots_[kRootListLength];
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001386
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001387 Object* global_contexts_list_;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001388
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001389 struct StringTypeTable {
1390 InstanceType type;
1391 int size;
1392 RootListIndex index;
1393 };
1394
1395 struct ConstantSymbolTable {
1396 const char* contents;
1397 RootListIndex index;
1398 };
1399
1400 struct StructTable {
1401 InstanceType type;
1402 int size;
1403 RootListIndex index;
1404 };
1405
1406 static const StringTypeTable string_type_table[];
1407 static const ConstantSymbolTable constant_symbol_table[];
1408 static const StructTable struct_table[];
1409
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001410 // The special hidden symbol which is an empty string, but does not match
1411 // any string when looked up in properties.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001412 String* hidden_symbol_;
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001413
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001414 // GC callback function, called before and after mark-compact GC.
1415 // Allocations in the callback function are disallowed.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001416 struct GCPrologueCallbackPair {
1417 GCPrologueCallbackPair(GCPrologueCallback callback, GCType gc_type)
1418 : callback(callback), gc_type(gc_type) {
1419 }
1420 bool operator==(const GCPrologueCallbackPair& pair) const {
1421 return pair.callback == callback;
1422 }
1423 GCPrologueCallback callback;
1424 GCType gc_type;
1425 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001426 List<GCPrologueCallbackPair> gc_prologue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001427
1428 struct GCEpilogueCallbackPair {
1429 GCEpilogueCallbackPair(GCEpilogueCallback callback, GCType gc_type)
1430 : callback(callback), gc_type(gc_type) {
1431 }
1432 bool operator==(const GCEpilogueCallbackPair& pair) const {
1433 return pair.callback == callback;
1434 }
1435 GCEpilogueCallback callback;
1436 GCType gc_type;
1437 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001438 List<GCEpilogueCallbackPair> gc_epilogue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001439
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001440 GCCallback global_gc_prologue_callback_;
1441 GCCallback global_gc_epilogue_callback_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001443 // Support for computing object sizes during GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001444 HeapObjectCallback gc_safe_size_of_old_object_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001445 static int GcSafeSizeOfOldObject(HeapObject* object);
1446 static int GcSafeSizeOfOldObjectWithEncodedMap(HeapObject* object);
1447
1448 // Update the GC state. Called from the mark-compact collector.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001449 void MarkMapPointersAsEncoded(bool encoded) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001450 gc_safe_size_of_old_object_ = encoded
1451 ? &GcSafeSizeOfOldObjectWithEncodedMap
1452 : &GcSafeSizeOfOldObject;
1453 }
1454
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 // Checks whether a global GC is necessary
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001456 GarbageCollector SelectGarbageCollector(AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457
1458 // Performs garbage collection
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001459 // Returns whether there is a chance another major GC could
1460 // collect more garbage.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001461 bool PerformGarbageCollection(GarbageCollector collector,
1462 GCTracer* tracer);
1463
1464 static const intptr_t kMinimumPromotionLimit = 2 * MB;
1465 static const intptr_t kMinimumAllocationLimit = 8 * MB;
1466
1467 inline void UpdateOldSpaceLimits();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001469 // Allocate an uninitialized object in map space. The behavior is identical
1470 // to Heap::AllocateRaw(size_in_bytes, MAP_SPACE), except that (a) it doesn't
1471 // have to test the allocation space argument and (b) can reduce code size
1472 // (since both AllocateRaw and AllocateRawMap are inlined).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001473 MUST_USE_RESULT inline MaybeObject* AllocateRawMap();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001474
1475 // Allocate an uninitialized object in the global property cell space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001476 MUST_USE_RESULT inline MaybeObject* AllocateRawCell();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001478 // Initializes a JSObject based on its map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001479 void InitializeJSObjectFromMap(JSObject* obj,
1480 FixedArray* properties,
1481 Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001483 bool CreateInitialMaps();
1484 bool CreateInitialObjects();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001485
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001486 // These five Create*EntryStub functions are here and forced to not be inlined
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +00001487 // because of a gcc-4.4 bug that assigns wrong vtable entries.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001488 NO_INLINE(void CreateJSEntryStub());
1489 NO_INLINE(void CreateJSConstructEntryStub());
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001490
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001491 void CreateFixedStubs();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001492
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001493 MaybeObject* CreateOddball(const char* to_string,
1494 Object* to_number,
1495 byte kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496
1497 // Allocate empty fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001498 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedArray();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001500 // Allocate empty fixed double array.
1501 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedDoubleArray();
1502
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001503 void SwitchScavengingVisitorsTableIfProfilingWasEnabled();
1504
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 // Performs a minor collection in new generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001506 void Scavenge();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001507
1508 static String* UpdateNewSpaceReferenceInExternalStringTableEntry(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001509 Heap* heap,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001510 Object** pointer);
1511
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001512 Address DoScavenge(ObjectVisitor* scavenge_visitor, Address new_space_front);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513
1514 // Performs a major collection in the whole heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001515 void MarkCompact(GCTracer* tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516
1517 // Code to be run before and after mark-compact.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001518 void MarkCompactPrologue(bool is_compacting);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001520 // Completely clear the Instanceof cache (to stop it keeping objects alive
1521 // around a GC).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001522 inline void CompletelyClearInstanceofCache();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001523
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524 // Record statistics before and after garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001525 void ReportStatisticsBeforeGC();
1526 void ReportStatisticsAfterGC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001528 // Slow part of scavenge object.
1529 static void ScavengeObjectSlow(HeapObject** p, HeapObject* object);
1530
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001531 // Initializes a function with a shared part and prototype.
1532 // Returns the function.
1533 // Note: this code was factored out of AllocateFunction such that
1534 // other parts of the VM could use it. Specifically, a function that creates
1535 // instances of type JS_FUNCTION_TYPE benefit from the use of this function.
1536 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001537 MUST_USE_RESULT inline MaybeObject* InitializeFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001538 JSFunction* function,
1539 SharedFunctionInfo* shared,
1540 Object* prototype);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001541
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001542 // Total RegExp code ever generated
1543 double total_regexp_code_generated_;
1544
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001545 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001546
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001547
1548 // Initializes the number to string cache based on the max semispace size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001549 MUST_USE_RESULT MaybeObject* InitializeNumberStringCache();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001550 // Flush the number to string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001551 void FlushNumberStringCache();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001552
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001553 void UpdateSurvivalRateTrend(int start_new_space_size);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001554
1555 enum SurvivalRateTrend { INCREASING, STABLE, DECREASING, FLUCTUATING };
1556
1557 static const int kYoungSurvivalRateThreshold = 90;
1558 static const int kYoungSurvivalRateAllowedDeviation = 15;
1559
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001560 int young_survivors_after_last_gc_;
1561 int high_survival_rate_period_length_;
1562 double survival_rate_;
1563 SurvivalRateTrend previous_survival_rate_trend_;
1564 SurvivalRateTrend survival_rate_trend_;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001565
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001566 void set_survival_rate_trend(SurvivalRateTrend survival_rate_trend) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001567 ASSERT(survival_rate_trend != FLUCTUATING);
1568 previous_survival_rate_trend_ = survival_rate_trend_;
1569 survival_rate_trend_ = survival_rate_trend;
1570 }
1571
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001572 SurvivalRateTrend survival_rate_trend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001573 if (survival_rate_trend_ == STABLE) {
1574 return STABLE;
1575 } else if (previous_survival_rate_trend_ == STABLE) {
1576 return survival_rate_trend_;
1577 } else if (survival_rate_trend_ != previous_survival_rate_trend_) {
1578 return FLUCTUATING;
1579 } else {
1580 return survival_rate_trend_;
1581 }
1582 }
1583
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001584 bool IsStableOrIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001585 switch (survival_rate_trend()) {
1586 case STABLE:
1587 case INCREASING:
1588 return true;
1589 default:
1590 return false;
1591 }
1592 }
1593
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001594 bool IsIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001595 return survival_rate_trend() == INCREASING;
1596 }
1597
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001598 bool IsHighSurvivalRate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001599 return high_survival_rate_period_length_ > 0;
1600 }
1601
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 static const int kInitialSymbolTableSize = 2048;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001603 static const int kInitialEvalCacheSize = 64;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001605 // Maximum GC pause.
1606 int max_gc_pause_;
1607
1608 // Maximum size of objects alive after GC.
1609 intptr_t max_alive_after_gc_;
1610
1611 // Minimal interval between two subsequent collections.
1612 int min_in_mutator_;
1613
1614 // Size of objects alive after last GC.
1615 intptr_t alive_after_last_gc_;
1616
1617 double last_gc_end_timestamp_;
1618
1619 MarkCompactCollector mark_compact_collector_;
1620
1621 // This field contains the meaning of the WATERMARK_INVALIDATED flag.
1622 // Instead of clearing this flag from all pages we just flip
1623 // its meaning at the beginning of a scavenge.
1624 intptr_t page_watermark_invalidated_mark_;
1625
1626 int number_idle_notifications_;
1627 unsigned int last_idle_notification_gc_count_;
1628 bool last_idle_notification_gc_count_init_;
1629
1630 // Shared state read by the scavenge collector and set by ScavengeObject.
1631 PromotionQueue promotion_queue_;
1632
1633 // Flag is set when the heap has been configured. The heap can be repeatedly
1634 // configured through the API until it is setup.
1635 bool configured_;
1636
1637 ExternalStringTable external_string_table_;
1638
1639 bool is_safe_to_read_maps_;
1640
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 friend class Factory;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001642 friend class GCTracer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 friend class DisallowAllocationFailure;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001644 friend class AlwaysAllocateScope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001645 friend class LinearAllocationScope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001646 friend class Page;
1647 friend class Isolate;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001648 friend class MarkCompactCollector;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001649 friend class StaticMarkingVisitor;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001650 friend class MapCompact;
1651
1652 DISALLOW_COPY_AND_ASSIGN(Heap);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001653};
1654
1655
ager@chromium.org67368652009-12-03 11:40:49 +00001656class HeapStats {
1657 public:
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001658 static const int kStartMarker = 0xDECADE00;
1659 static const int kEndMarker = 0xDECADE01;
1660
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001661 int* start_marker; // 0
1662 int* new_space_size; // 1
1663 int* new_space_capacity; // 2
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001664 intptr_t* old_pointer_space_size; // 3
1665 intptr_t* old_pointer_space_capacity; // 4
1666 intptr_t* old_data_space_size; // 5
1667 intptr_t* old_data_space_capacity; // 6
1668 intptr_t* code_space_size; // 7
1669 intptr_t* code_space_capacity; // 8
1670 intptr_t* map_space_size; // 9
1671 intptr_t* map_space_capacity; // 10
1672 intptr_t* cell_space_size; // 11
1673 intptr_t* cell_space_capacity; // 12
1674 intptr_t* lo_space_size; // 13
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001675 int* global_handle_count; // 14
1676 int* weak_global_handle_count; // 15
1677 int* pending_global_handle_count; // 16
1678 int* near_death_global_handle_count; // 17
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001679 int* free_global_handle_count; // 18
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001680 intptr_t* memory_allocator_size; // 19
1681 intptr_t* memory_allocator_capacity; // 20
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001682 int* objects_per_type; // 21
1683 int* size_per_type; // 22
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001684 int* os_error; // 23
1685 int* end_marker; // 24
ager@chromium.org60121232009-12-03 11:25:37 +00001686};
1687
1688
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001689class AlwaysAllocateScope {
1690 public:
1691 AlwaysAllocateScope() {
1692 // We shouldn't hit any nested scopes, because that requires
1693 // non-handle code to call handle code. The code still works but
1694 // performance will degrade, so we want to catch this situation
1695 // in debug mode.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001696 ASSERT(HEAP->always_allocate_scope_depth_ == 0);
1697 HEAP->always_allocate_scope_depth_++;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001698 }
1699
1700 ~AlwaysAllocateScope() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001701 HEAP->always_allocate_scope_depth_--;
1702 ASSERT(HEAP->always_allocate_scope_depth_ == 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001703 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704};
1705
1706
ager@chromium.org3811b432009-10-28 14:53:37 +00001707class LinearAllocationScope {
1708 public:
1709 LinearAllocationScope() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001710 HEAP->linear_allocation_scope_depth_++;
ager@chromium.org3811b432009-10-28 14:53:37 +00001711 }
1712
1713 ~LinearAllocationScope() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001714 HEAP->linear_allocation_scope_depth_--;
1715 ASSERT(HEAP->linear_allocation_scope_depth_ >= 0);
ager@chromium.org3811b432009-10-28 14:53:37 +00001716 }
1717};
1718
1719
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720#ifdef DEBUG
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001721// Visitor class to verify interior pointers in spaces that do not contain
1722// or care about intergenerational references. All heap object pointers have to
1723// point into the heap to a location that has a map pointer at its first word.
1724// Caveat: Heap::Contains is an approximation because it can return true for
1725// objects in a heap space but above the allocation pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001726class VerifyPointersVisitor: public ObjectVisitor {
1727 public:
1728 void VisitPointers(Object** start, Object** end) {
1729 for (Object** current = start; current < end; current++) {
1730 if ((*current)->IsHeapObject()) {
1731 HeapObject* object = HeapObject::cast(*current);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001732 ASSERT(HEAP->Contains(object));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 ASSERT(object->map()->IsMap());
1734 }
1735 }
1736 }
1737};
1738
1739
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001740// Visitor class to verify interior pointers in spaces that use region marks
1741// to keep track of intergenerational references.
1742// As VerifyPointersVisitor but also checks that dirty marks are set
1743// for regions covering intergenerational references.
1744class VerifyPointersAndDirtyRegionsVisitor: public ObjectVisitor {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 public:
1746 void VisitPointers(Object** start, Object** end) {
1747 for (Object** current = start; current < end; current++) {
1748 if ((*current)->IsHeapObject()) {
1749 HeapObject* object = HeapObject::cast(*current);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001750 ASSERT(HEAP->Contains(object));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 ASSERT(object->map()->IsMap());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001752 if (HEAP->InNewSpace(object)) {
1753 ASSERT(HEAP->InToSpace(object));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001754 Address addr = reinterpret_cast<Address>(current);
1755 ASSERT(Page::FromAddress(addr)->IsRegionDirty(addr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 }
1757 }
1758 }
1759 }
1760};
1761#endif
1762
1763
kasper.lund7276f142008-07-30 08:49:36 +00001764// Space iterator for iterating over all spaces of the heap.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001765// Returns each space in turn, and null when it is done.
1766class AllSpaces BASE_EMBEDDED {
1767 public:
1768 Space* next();
1769 AllSpaces() { counter_ = FIRST_SPACE; }
1770 private:
1771 int counter_;
1772};
1773
1774
1775// Space iterator for iterating over all old spaces of the heap: Old pointer
1776// space, old data space and code space.
1777// Returns each space in turn, and null when it is done.
1778class OldSpaces BASE_EMBEDDED {
1779 public:
1780 OldSpace* next();
1781 OldSpaces() { counter_ = OLD_POINTER_SPACE; }
1782 private:
1783 int counter_;
1784};
1785
1786
1787// Space iterator for iterating over all the paged spaces of the heap:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001788// Map space, old pointer space, old data space, code space and cell space.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001789// Returns each space in turn, and null when it is done.
1790class PagedSpaces BASE_EMBEDDED {
1791 public:
1792 PagedSpace* next();
1793 PagedSpaces() { counter_ = OLD_POINTER_SPACE; }
1794 private:
1795 int counter_;
1796};
1797
1798
1799// Space iterator for iterating over all spaces of the heap.
kasper.lund7276f142008-07-30 08:49:36 +00001800// For each space an object iterator is provided. The deallocation of the
1801// returned object iterators is handled by the space iterator.
kasper.lund7276f142008-07-30 08:49:36 +00001802class SpaceIterator : public Malloced {
1803 public:
1804 SpaceIterator();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001805 explicit SpaceIterator(HeapObjectCallback size_func);
kasper.lund7276f142008-07-30 08:49:36 +00001806 virtual ~SpaceIterator();
1807
1808 bool has_next();
1809 ObjectIterator* next();
1810
1811 private:
1812 ObjectIterator* CreateIterator();
1813
1814 int current_space_; // from enum AllocationSpace.
1815 ObjectIterator* iterator_; // object iterator for the current space.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001816 HeapObjectCallback size_func_;
kasper.lund7276f142008-07-30 08:49:36 +00001817};
1818
1819
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001820// A HeapIterator provides iteration over the whole heap. It
1821// aggregates the specific iterators for the different spaces as
1822// these can only iterate over one space only.
1823//
1824// HeapIterator can skip free list nodes (that is, de-allocated heap
1825// objects that still remain in the heap). As implementation of free
1826// nodes filtering uses GC marks, it can't be used during MS/MC GC
1827// phases. Also, it is forbidden to interrupt iteration in this mode,
1828// as this will leave heap objects marked (and thus, unusable).
whesse@chromium.org023421e2010-12-21 12:19:12 +00001829class HeapObjectsFilter;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830
1831class HeapIterator BASE_EMBEDDED {
1832 public:
whesse@chromium.org023421e2010-12-21 12:19:12 +00001833 enum HeapObjectsFiltering {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001834 kNoFiltering,
whesse@chromium.org023421e2010-12-21 12:19:12 +00001835 kFilterFreeListNodes,
1836 kFilterUnreachable
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001837 };
1838
1839 HeapIterator();
whesse@chromium.org023421e2010-12-21 12:19:12 +00001840 explicit HeapIterator(HeapObjectsFiltering filtering);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001841 ~HeapIterator();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 HeapObject* next();
1844 void reset();
1845
1846 private:
1847 // Perform the initialization.
1848 void Init();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 // Perform all necessary shutdown (destruction) work.
1850 void Shutdown();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001851 HeapObject* NextObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852
whesse@chromium.org023421e2010-12-21 12:19:12 +00001853 HeapObjectsFiltering filtering_;
1854 HeapObjectsFilter* filter_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 // Space iterator for iterating all the spaces.
1856 SpaceIterator* space_iterator_;
1857 // Object iterator for the space currently being iterated.
1858 ObjectIterator* object_iterator_;
1859};
1860
1861
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001862// Cache for mapping (map, property name) into field offset.
1863// Cleared at startup and prior to mark sweep collection.
1864class KeyedLookupCache {
1865 public:
1866 // Lookup field offset for (map, name). If absent, -1 is returned.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001867 int Lookup(Map* map, String* name);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001868
1869 // Update an element in the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001870 void Update(Map* map, String* name, int field_offset);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001871
1872 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001873 void Clear();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001874
1875 static const int kLength = 64;
1876 static const int kCapacityMask = kLength - 1;
1877 static const int kMapHashShift = 2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001878 static const int kNotFound = -1;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001879
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001880 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001881 KeyedLookupCache() {
1882 for (int i = 0; i < kLength; ++i) {
1883 keys_[i].map = NULL;
1884 keys_[i].name = NULL;
1885 field_offsets_[i] = kNotFound;
1886 }
1887 }
1888
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001889 static inline int Hash(Map* map, String* name);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001890
1891 // Get the address of the keys and field_offsets arrays. Used in
1892 // generated code to perform cache lookups.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001893 Address keys_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001894 return reinterpret_cast<Address>(&keys_);
1895 }
1896
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001897 Address field_offsets_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001898 return reinterpret_cast<Address>(&field_offsets_);
1899 }
1900
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001901 struct Key {
1902 Map* map;
1903 String* name;
1904 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001905
1906 Key keys_[kLength];
1907 int field_offsets_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001908
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001909 friend class ExternalReference;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001910 friend class Isolate;
1911 DISALLOW_COPY_AND_ASSIGN(KeyedLookupCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001912};
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001913
1914
1915// Cache for mapping (array, property name) into descriptor index.
1916// The cache contains both positive and negative results.
1917// Descriptor index equals kNotFound means the property is absent.
1918// Cleared at startup and prior to any gc.
1919class DescriptorLookupCache {
1920 public:
1921 // Lookup descriptor index for (map, name).
1922 // If absent, kAbsent is returned.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001923 int Lookup(DescriptorArray* array, String* name) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001924 if (!StringShape(name).IsSymbol()) return kAbsent;
1925 int index = Hash(array, name);
1926 Key& key = keys_[index];
1927 if ((key.array == array) && (key.name == name)) return results_[index];
1928 return kAbsent;
1929 }
1930
1931 // Update an element in the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001932 void Update(DescriptorArray* array, String* name, int result) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001933 ASSERT(result != kAbsent);
1934 if (StringShape(name).IsSymbol()) {
1935 int index = Hash(array, name);
1936 Key& key = keys_[index];
1937 key.array = array;
1938 key.name = name;
1939 results_[index] = result;
1940 }
1941 }
1942
1943 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001944 void Clear();
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001945
1946 static const int kAbsent = -2;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001947
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001948 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001949 DescriptorLookupCache() {
1950 for (int i = 0; i < kLength; ++i) {
1951 keys_[i].array = NULL;
1952 keys_[i].name = NULL;
1953 results_[i] = kAbsent;
1954 }
1955 }
1956
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001957 static int Hash(DescriptorArray* array, String* name) {
1958 // Uses only lower 32 bits if pointers are larger.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00001959 uint32_t array_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001960 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(array)) >> 2;
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00001961 uint32_t name_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001962 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name)) >> 2;
1963 return (array_hash ^ name_hash) % kLength;
1964 }
1965
1966 static const int kLength = 64;
1967 struct Key {
1968 DescriptorArray* array;
1969 String* name;
1970 };
1971
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001972 Key keys_[kLength];
1973 int results_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001974
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001975 friend class Isolate;
1976 DISALLOW_COPY_AND_ASSIGN(DescriptorLookupCache);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977};
1978
1979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980// A helper class to document/test C++ scopes where we do not
1981// expect a GC. Usage:
1982//
1983// /* Allocation not allowed: we cannot handle a GC in this scope. */
1984// { AssertNoAllocation nogc;
1985// ...
1986// }
1987
1988#ifdef DEBUG
1989
1990class DisallowAllocationFailure {
1991 public:
1992 DisallowAllocationFailure() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001993 old_state_ = HEAP->disallow_allocation_failure_;
1994 HEAP->disallow_allocation_failure_ = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 }
1996 ~DisallowAllocationFailure() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001997 HEAP->disallow_allocation_failure_ = old_state_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 }
1999 private:
2000 bool old_state_;
2001};
2002
2003class AssertNoAllocation {
2004 public:
2005 AssertNoAllocation() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002006 old_state_ = HEAP->allow_allocation(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 }
2008
2009 ~AssertNoAllocation() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002010 HEAP->allow_allocation(old_state_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 }
2012
2013 private:
2014 bool old_state_;
2015};
2016
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002017class DisableAssertNoAllocation {
2018 public:
2019 DisableAssertNoAllocation() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002020 old_state_ = HEAP->allow_allocation(true);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002021 }
2022
2023 ~DisableAssertNoAllocation() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002024 HEAP->allow_allocation(old_state_);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002025 }
2026
2027 private:
2028 bool old_state_;
2029};
2030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031#else // ndef DEBUG
2032
2033class AssertNoAllocation {
2034 public:
2035 AssertNoAllocation() { }
2036 ~AssertNoAllocation() { }
2037};
2038
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002039class DisableAssertNoAllocation {
2040 public:
2041 DisableAssertNoAllocation() { }
2042 ~DisableAssertNoAllocation() { }
2043};
2044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045#endif
2046
kasper.lund7276f142008-07-30 08:49:36 +00002047// GCTracer collects and prints ONE line after each garbage collector
2048// invocation IFF --trace_gc is used.
2049
2050class GCTracer BASE_EMBEDDED {
2051 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002052 class Scope BASE_EMBEDDED {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002053 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002054 enum ScopeId {
2055 EXTERNAL,
2056 MC_MARK,
2057 MC_SWEEP,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002058 MC_SWEEP_NEWSPACE,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002059 MC_COMPACT,
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00002060 MC_FLUSH_CODE,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002061 kNumberOfScopes
2062 };
2063
2064 Scope(GCTracer* tracer, ScopeId scope)
2065 : tracer_(tracer),
2066 scope_(scope) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002067 start_time_ = OS::TimeCurrentMillis();
2068 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002069
2070 ~Scope() {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002071 ASSERT(scope_ < kNumberOfScopes); // scope_ is unsigned.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002072 tracer_->scopes_[scope_] += OS::TimeCurrentMillis() - start_time_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002073 }
kasper.lund7276f142008-07-30 08:49:36 +00002074
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002075 private:
2076 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002077 ScopeId scope_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002078 double start_time_;
2079 };
2080
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002081 explicit GCTracer(Heap* heap);
kasper.lund7276f142008-07-30 08:49:36 +00002082 ~GCTracer();
2083
2084 // Sets the collector.
2085 void set_collector(GarbageCollector collector) { collector_ = collector; }
2086
2087 // Sets the GC count.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002088 void set_gc_count(unsigned int count) { gc_count_ = count; }
kasper.lund7276f142008-07-30 08:49:36 +00002089
2090 // Sets the full GC count.
2091 void set_full_gc_count(int count) { full_gc_count_ = count; }
2092
2093 // Sets the flag that this is a compacting full GC.
2094 void set_is_compacting() { is_compacting_ = true; }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002095 bool is_compacting() const { return is_compacting_; }
kasper.lund7276f142008-07-30 08:49:36 +00002096
2097 // Increment and decrement the count of marked objects.
2098 void increment_marked_count() { ++marked_count_; }
2099 void decrement_marked_count() { --marked_count_; }
2100
2101 int marked_count() { return marked_count_; }
2102
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002103 void increment_promoted_objects_size(int object_size) {
2104 promoted_objects_size_ += object_size;
2105 }
2106
kasper.lund7276f142008-07-30 08:49:36 +00002107 private:
2108 // Returns a string matching the collector.
2109 const char* CollectorString();
2110
2111 // Returns size of object in heap (in MB).
2112 double SizeOfHeapObjects() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002113 return (static_cast<double>(HEAP->SizeOfObjects())) / MB;
kasper.lund7276f142008-07-30 08:49:36 +00002114 }
2115
2116 double start_time_; // Timestamp set in the constructor.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002117 intptr_t start_size_; // Size of objects in heap set in constructor.
kasper.lund7276f142008-07-30 08:49:36 +00002118 GarbageCollector collector_; // Type of collector.
2119
2120 // A count (including this one, eg, the first collection is 1) of the
2121 // number of garbage collections.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002122 unsigned int gc_count_;
kasper.lund7276f142008-07-30 08:49:36 +00002123
2124 // A count (including this one) of the number of full garbage collections.
2125 int full_gc_count_;
2126
2127 // True if the current GC is a compacting full collection, false
2128 // otherwise.
2129 bool is_compacting_;
2130
2131 // True if the *previous* full GC cwas a compacting collection (will be
2132 // false if there has not been a previous full GC).
2133 bool previous_has_compacted_;
2134
2135 // On a full GC, a count of the number of marked objects. Incremented
2136 // when an object is marked and decremented when an object's mark bit is
2137 // cleared. Will be zero on a scavenge collection.
2138 int marked_count_;
2139
2140 // The count from the end of the previous full GC. Will be zero if there
2141 // was no previous full GC.
2142 int previous_marked_count_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002143
2144 // Amounts of time spent in different scopes during GC.
2145 double scopes_[Scope::kNumberOfScopes];
2146
2147 // Total amount of space either wasted or contained in one of free lists
2148 // before the current GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002149 intptr_t in_free_list_or_wasted_before_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002150
2151 // Difference between space used in the heap at the beginning of the current
2152 // collection and the end of the previous collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002153 intptr_t allocated_since_last_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002154
2155 // Amount of time spent in mutator that is time elapsed between end of the
2156 // previous collection and the beginning of the current one.
2157 double spent_in_mutator_;
2158
2159 // Size of objects promoted during the current collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002160 intptr_t promoted_objects_size_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002161
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002162 Heap* heap_;
kasper.lund7276f142008-07-30 08:49:36 +00002163};
2164
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002165
2166class TranscendentalCache {
2167 public:
2168 enum Type {ACOS, ASIN, ATAN, COS, EXP, LOG, SIN, TAN, kNumberOfCaches};
whesse@chromium.org023421e2010-12-21 12:19:12 +00002169 static const int kTranscendentalTypeBits = 3;
2170 STATIC_ASSERT((1 << kTranscendentalTypeBits) >= kNumberOfCaches);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002171
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002172 // Returns a heap number with f(input), where f is a math function specified
2173 // by the 'type' argument.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002174 MUST_USE_RESULT inline MaybeObject* Get(Type type, double input);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002175
2176 // The cache contains raw Object pointers. This method disposes of
2177 // them before a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002178 void Clear();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002179
2180 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002181 class SubCache {
2182 static const int kCacheSize = 512;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002183
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002184 explicit SubCache(Type t);
2185
2186 MUST_USE_RESULT inline MaybeObject* Get(double input);
2187
2188 inline double Calculate(double input);
2189
2190 struct Element {
2191 uint32_t in[2];
2192 Object* output;
2193 };
2194
2195 union Converter {
2196 double dbl;
2197 uint32_t integers[2];
2198 };
2199
2200 inline static int Hash(const Converter& c) {
2201 uint32_t hash = (c.integers[0] ^ c.integers[1]);
2202 hash ^= static_cast<int32_t>(hash) >> 16;
2203 hash ^= static_cast<int32_t>(hash) >> 8;
2204 return (hash & (kCacheSize - 1));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002205 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002206
2207 Element elements_[kCacheSize];
2208 Type type_;
2209 Isolate* isolate_;
2210
2211 // Allow access to the caches_ array as an ExternalReference.
2212 friend class ExternalReference;
2213 // Inline implementation of the cache.
2214 friend class TranscendentalCacheStub;
2215 // For evaluating value.
2216 friend class TranscendentalCache;
2217
2218 DISALLOW_COPY_AND_ASSIGN(SubCache);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002219 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002220
2221 TranscendentalCache() {
2222 for (int i = 0; i < kNumberOfCaches; ++i) caches_[i] = NULL;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002223 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002224
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002225 // Used to create an external reference.
2226 inline Address cache_array_address();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002227
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002228 // Instantiation
2229 friend class Isolate;
2230 // Inline implementation of the caching.
2231 friend class TranscendentalCacheStub;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002232 // Allow access to the caches_ array as an ExternalReference.
2233 friend class ExternalReference;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002234
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002235 SubCache* caches_[kNumberOfCaches];
2236 DISALLOW_COPY_AND_ASSIGN(TranscendentalCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002237};
2238
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002239
2240// Abstract base class for checking whether a weak object should be retained.
2241class WeakObjectRetainer {
2242 public:
2243 virtual ~WeakObjectRetainer() {}
2244
2245 // Return whether this object should be retained. If NULL is returned the
2246 // object has no references. Otherwise the address of the retained object
2247 // should be returned as in some GC situations the object has been moved.
2248 virtual Object* RetainAs(Object* object) = 0;
2249};
2250
2251
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002252#if defined(DEBUG) || defined(LIVE_OBJECT_LIST)
2253// Helper class for tracing paths to a search target Object from all roots.
2254// The TracePathFrom() method can be used to trace paths from a specific
2255// object to the search target object.
2256class PathTracer : public ObjectVisitor {
2257 public:
2258 enum WhatToFind {
2259 FIND_ALL, // Will find all matches.
2260 FIND_FIRST // Will stop the search after first match.
2261 };
2262
2263 // For the WhatToFind arg, if FIND_FIRST is specified, tracing will stop
2264 // after the first match. If FIND_ALL is specified, then tracing will be
2265 // done for all matches.
2266 PathTracer(Object* search_target,
2267 WhatToFind what_to_find,
2268 VisitMode visit_mode)
2269 : search_target_(search_target),
2270 found_target_(false),
2271 found_target_in_trace_(false),
2272 what_to_find_(what_to_find),
2273 visit_mode_(visit_mode),
2274 object_stack_(20),
2275 no_alloc() {}
2276
2277 virtual void VisitPointers(Object** start, Object** end);
2278
2279 void Reset();
2280 void TracePathFrom(Object** root);
2281
2282 bool found() const { return found_target_; }
2283
2284 static Object* const kAnyGlobalObject;
2285
2286 protected:
2287 class MarkVisitor;
2288 class UnmarkVisitor;
2289
2290 void MarkRecursively(Object** p, MarkVisitor* mark_visitor);
2291 void UnmarkRecursively(Object** p, UnmarkVisitor* unmark_visitor);
2292 virtual void ProcessResults();
2293
2294 // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject.
2295 static const int kMarkTag = 2;
2296
2297 Object* search_target_;
2298 bool found_target_;
2299 bool found_target_in_trace_;
2300 WhatToFind what_to_find_;
2301 VisitMode visit_mode_;
2302 List<Object*> object_stack_;
2303
2304 AssertNoAllocation no_alloc; // i.e. no gc allowed.
2305
2306 DISALLOW_IMPLICIT_CONSTRUCTORS(PathTracer);
2307};
2308#endif // DEBUG || LIVE_OBJECT_LIST
2309
2310
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311} } // namespace v8::internal
2312
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002313#undef HEAP
2314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315#endif // V8_HEAP_H_