blob: a5ff86ea0b4e27343b7bdd4fb2e6fb81000d0a0c [file] [log] [blame]
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001// Copyright 2012 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"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000035#include "incremental-marking.h"
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +000036#include "list.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000037#include "mark-compact.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000038#include "objects-visiting.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000039#include "spaces.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000040#include "splay-tree-inl.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000041#include "store-buffer.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000042#include "v8-counters.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000043#include "v8globals.h"
ager@chromium.org18ad94b2009-09-02 08:22:29 +000044
kasperl@chromium.org71affb52009-05-26 05:44:31 +000045namespace v8 {
46namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
48// Defines all the roots in Heap.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +000049#define STRONG_ROOT_LIST(V) \
ager@chromium.org3811b432009-10-28 14:53:37 +000050 V(Map, byte_array_map, ByteArrayMap) \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000051 V(Map, free_space_map, FreeSpaceMap) \
ager@chromium.org3811b432009-10-28 14:53:37 +000052 V(Map, one_pointer_filler_map, OnePointerFillerMap) \
53 V(Map, two_pointer_filler_map, TwoPointerFillerMap) \
54 /* Cluster the most popular ones in a few cache lines here at the top. */ \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000055 V(Smi, store_buffer_top, StoreBufferTop) \
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +000056 V(Oddball, undefined_value, UndefinedValue) \
57 V(Oddball, the_hole_value, TheHoleValue) \
58 V(Oddball, null_value, NullValue) \
59 V(Oddball, true_value, TrueValue) \
60 V(Oddball, false_value, FalseValue) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000061 V(Map, global_property_cell_map, GlobalPropertyCellMap) \
62 V(Map, shared_function_info_map, SharedFunctionInfoMap) \
63 V(Map, meta_map, MetaMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000064 V(Map, heap_number_map, HeapNumberMap) \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +000065 V(Map, native_context_map, NativeContextMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000066 V(Map, fixed_array_map, FixedArrayMap) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000067 V(Map, code_map, CodeMap) \
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000068 V(Map, scope_info_map, ScopeInfoMap) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +000069 V(Map, fixed_cow_array_map, FixedCOWArrayMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000070 V(Map, fixed_double_array_map, FixedDoubleArrayMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000071 V(Object, no_interceptor_result_sentinel, NoInterceptorResultSentinel) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000072 V(Map, hash_table_map, HashTableMap) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000073 V(FixedArray, empty_fixed_array, EmptyFixedArray) \
74 V(ByteArray, empty_byte_array, EmptyByteArray) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000075 V(DescriptorArray, empty_descriptor_array, EmptyDescriptorArray) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000076 V(Smi, stack_limit, StackLimit) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000077 V(Oddball, arguments_marker, ArgumentsMarker) \
78 /* The first 32 roots above this line should be boring from a GC point of */ \
79 /* view. This means they are never in new space and never on a page that */ \
80 /* is being compacted. */ \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000081 V(FixedArray, number_string_cache, NumberStringCache) \
82 V(Object, instanceof_cache_function, InstanceofCacheFunction) \
83 V(Object, instanceof_cache_map, InstanceofCacheMap) \
84 V(Object, instanceof_cache_answer, InstanceofCacheAnswer) \
85 V(FixedArray, single_character_string_cache, SingleCharacterStringCache) \
jkummerow@chromium.org486075a2011-09-07 12:44:28 +000086 V(FixedArray, string_split_cache, StringSplitCache) \
jkummerow@chromium.org78502a92012-09-06 13:50:42 +000087 V(FixedArray, regexp_multiple_cache, RegExpMultipleCache) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000088 V(Object, termination_exception, TerminationException) \
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000089 V(Smi, hash_seed, HashSeed) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000090 V(Map, symbol_map, SymbolMap) \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000091 V(Map, string_map, StringMap) \
92 V(Map, ascii_string_map, AsciiStringMap) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000093 V(Map, cons_string_map, ConsStringMap) \
94 V(Map, cons_ascii_string_map, ConsAsciiStringMap) \
ricow@chromium.org4668a2c2011-08-29 10:41:00 +000095 V(Map, sliced_string_map, SlicedStringMap) \
96 V(Map, sliced_ascii_string_map, SlicedAsciiStringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000097 V(Map, external_string_map, ExternalStringMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000098 V(Map, external_string_with_ascii_data_map, ExternalStringWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000099 V(Map, external_ascii_string_map, ExternalAsciiStringMap) \
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000100 V(Map, short_external_string_map, ShortExternalStringMap) \
101 V(Map, \
102 short_external_string_with_ascii_data_map, \
103 ShortExternalStringWithAsciiDataMap) \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000104 V(Map, internalized_string_map, InternalizedStringMap) \
105 V(Map, ascii_internalized_string_map, AsciiInternalizedStringMap) \
106 V(Map, cons_internalized_string_map, ConsInternalizedStringMap) \
107 V(Map, cons_ascii_internalized_string_map, ConsAsciiInternalizedStringMap) \
108 V(Map, \
109 external_internalized_string_map, \
110 ExternalInternalizedStringMap) \
111 V(Map, \
112 external_internalized_string_with_ascii_data_map, \
113 ExternalInternalizedStringWithAsciiDataMap) \
114 V(Map, \
115 external_ascii_internalized_string_map, \
116 ExternalAsciiInternalizedStringMap) \
117 V(Map, \
118 short_external_internalized_string_map, \
119 ShortExternalInternalizedStringMap) \
120 V(Map, \
121 short_external_internalized_string_with_ascii_data_map, \
122 ShortExternalInternalizedStringWithAsciiDataMap) \
123 V(Map, \
124 short_external_ascii_internalized_string_map, \
125 ShortExternalAsciiInternalizedStringMap) \
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000126 V(Map, short_external_ascii_string_map, ShortExternalAsciiStringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000127 V(Map, undetectable_string_map, UndetectableStringMap) \
128 V(Map, undetectable_ascii_string_map, UndetectableAsciiStringMap) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000129 V(Map, external_pixel_array_map, ExternalPixelArrayMap) \
ager@chromium.org3811b432009-10-28 14:53:37 +0000130 V(Map, external_byte_array_map, ExternalByteArrayMap) \
131 V(Map, external_unsigned_byte_array_map, ExternalUnsignedByteArrayMap) \
132 V(Map, external_short_array_map, ExternalShortArrayMap) \
133 V(Map, external_unsigned_short_array_map, ExternalUnsignedShortArrayMap) \
134 V(Map, external_int_array_map, ExternalIntArrayMap) \
135 V(Map, external_unsigned_int_array_map, ExternalUnsignedIntArrayMap) \
136 V(Map, external_float_array_map, ExternalFloatArrayMap) \
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000137 V(Map, external_double_array_map, ExternalDoubleArrayMap) \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000138 V(Map, non_strict_arguments_elements_map, NonStrictArgumentsElementsMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000139 V(Map, function_context_map, FunctionContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000140 V(Map, catch_context_map, CatchContextMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000141 V(Map, with_context_map, WithContextMap) \
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000142 V(Map, block_context_map, BlockContextMap) \
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000143 V(Map, module_context_map, ModuleContextMap) \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000144 V(Map, global_context_map, GlobalContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000145 V(Map, oddball_map, OddballMap) \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000146 V(Map, message_object_map, JSMessageObjectMap) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000147 V(Map, foreign_map, ForeignMap) \
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000148 V(HeapNumber, nan_value, NanValue) \
149 V(HeapNumber, infinity_value, InfinityValue) \
150 V(HeapNumber, minus_zero_value, MinusZeroValue) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000151 V(Map, neander_map, NeanderMap) \
152 V(JSObject, message_listeners, MessageListeners) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000153 V(Foreign, prototype_accessors, PrototypeAccessors) \
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000154 V(UnseededNumberDictionary, code_stubs, CodeStubs) \
155 V(UnseededNumberDictionary, non_monomorphic_cache, NonMonomorphicCache) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000156 V(PolymorphicCodeCache, polymorphic_code_cache, PolymorphicCodeCache) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000157 V(Code, js_entry_code, JsEntryCode) \
158 V(Code, js_construct_entry_code, JsConstructEntryCode) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000159 V(FixedArray, natives_source_cache, NativesSourceCache) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000160 V(Object, last_script_id, LastScriptId) \
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000161 V(Script, empty_script, EmptyScript) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000162 V(Smi, real_stack_limit, RealStackLimit) \
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000163 V(StringDictionary, intrinsic_function_names, IntrinsicFunctionNames) \
ulan@chromium.org967e2702012-02-28 09:49:15 +0000164 V(Smi, arguments_adaptor_deopt_pc_offset, ArgumentsAdaptorDeoptPCOffset) \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000165 V(Smi, construct_stub_deopt_pc_offset, ConstructStubDeoptPCOffset) \
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +0000166 V(Smi, getter_stub_deopt_pc_offset, GetterStubDeoptPCOffset) \
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000167 V(Smi, setter_stub_deopt_pc_offset, SetterStubDeoptPCOffset) \
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000168 V(JSObject, observation_state, ObservationState) \
169 V(Map, external_map, ExternalMap)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171#define ROOT_LIST(V) \
172 STRONG_ROOT_LIST(V) \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000173 V(StringTable, string_table, StringTable)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000175#define INTERNALIZED_STRING_LIST(V) \
176 V(Array_string, "Array") \
177 V(Object_string, "Object") \
178 V(proto_string, "__proto__") \
179 V(StringImpl_string, "StringImpl") \
180 V(arguments_string, "arguments") \
181 V(Arguments_string, "Arguments") \
182 V(call_string, "call") \
183 V(apply_string, "apply") \
184 V(caller_string, "caller") \
185 V(boolean_string, "boolean") \
186 V(Boolean_string, "Boolean") \
187 V(callee_string, "callee") \
188 V(constructor_string, "constructor") \
189 V(code_string, ".code") \
190 V(result_string, ".result") \
191 V(dot_for_string, ".for.") \
192 V(catch_var_string, ".catch-var") \
193 V(empty_string, "") \
194 V(eval_string, "eval") \
195 V(function_string, "function") \
196 V(length_string, "length") \
197 V(module_string, "module") \
198 V(name_string, "name") \
199 V(native_string, "native") \
200 V(null_string, "null") \
201 V(number_string, "number") \
202 V(Number_string, "Number") \
203 V(nan_string, "NaN") \
204 V(RegExp_string, "RegExp") \
205 V(source_string, "source") \
206 V(global_string, "global") \
207 V(ignore_case_string, "ignoreCase") \
208 V(multiline_string, "multiline") \
209 V(input_string, "input") \
210 V(index_string, "index") \
211 V(last_index_string, "lastIndex") \
212 V(object_string, "object") \
213 V(prototype_string, "prototype") \
214 V(string_string, "string") \
215 V(String_string, "String") \
216 V(Date_string, "Date") \
217 V(this_string, "this") \
218 V(to_string_string, "toString") \
219 V(char_at_string, "CharAt") \
220 V(undefined_string, "undefined") \
221 V(value_of_string, "valueOf") \
222 V(stack_string, "stack") \
223 V(InitializeVarGlobal_string, "InitializeVarGlobal") \
224 V(InitializeConstGlobal_string, "InitializeConstGlobal") \
225 V(KeyedLoadElementMonomorphic_string, \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000226 "KeyedLoadElementMonomorphic") \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000227 V(KeyedStoreElementMonomorphic_string, \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000228 "KeyedStoreElementMonomorphic") \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000229 V(KeyedStoreAndGrowElementMonomorphic_string, \
ulan@chromium.org65a89c22012-02-14 11:46:07 +0000230 "KeyedStoreAndGrowElementMonomorphic") \
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000231 V(stack_overflow_string, "kStackOverflowBoilerplate") \
232 V(illegal_access_string, "illegal access") \
233 V(out_of_memory_string, "out-of-memory") \
234 V(illegal_execution_state_string, "illegal execution state") \
235 V(get_string, "get") \
236 V(set_string, "set") \
237 V(map_field_string, "%map") \
238 V(elements_field_string, "%elements") \
239 V(length_field_string, "%length") \
240 V(function_class_string, "Function") \
241 V(illegal_argument_string, "illegal argument") \
242 V(MakeReferenceError_string, "MakeReferenceError") \
243 V(MakeSyntaxError_string, "MakeSyntaxError") \
244 V(MakeTypeError_string, "MakeTypeError") \
245 V(invalid_lhs_in_assignment_string, "invalid_lhs_in_assignment") \
246 V(invalid_lhs_in_for_in_string, "invalid_lhs_in_for_in") \
247 V(invalid_lhs_in_postfix_op_string, "invalid_lhs_in_postfix_op") \
248 V(invalid_lhs_in_prefix_op_string, "invalid_lhs_in_prefix_op") \
249 V(illegal_return_string, "illegal_return") \
250 V(illegal_break_string, "illegal_break") \
251 V(illegal_continue_string, "illegal_continue") \
252 V(unknown_label_string, "unknown_label") \
253 V(redeclaration_string, "redeclaration") \
254 V(failure_string, "<failure>") \
255 V(space_string, " ") \
256 V(exec_string, "exec") \
257 V(zero_string, "0") \
258 V(global_eval_string, "GlobalEval") \
259 V(identity_hash_string, "v8::IdentityHash") \
260 V(closure_string, "(closure)") \
261 V(use_strict_string, "use strict") \
262 V(dot_string, ".") \
263 V(anonymous_function_string, "(anonymous function)") \
264 V(compare_ic_string, "==") \
265 V(strict_compare_ic_string, "===") \
266 V(infinity_string, "Infinity") \
267 V(minus_infinity_string, "-Infinity") \
268 V(hidden_stack_trace_string, "v8::hidden_stack_trace") \
269 V(query_colon_string, "(?:)") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000271// Forward declarations.
kasper.lund7276f142008-07-30 08:49:36 +0000272class GCTracer;
ager@chromium.org60121232009-12-03 11:25:37 +0000273class HeapStats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000274class Isolate;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000275class WeakObjectRetainer;
kasper.lund7276f142008-07-30 08:49:36 +0000276
277
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000278typedef String* (*ExternalStringTableUpdaterCallback)(Heap* heap,
279 Object** pointer);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000280
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000281class StoreBufferRebuilder {
282 public:
283 explicit StoreBufferRebuilder(StoreBuffer* store_buffer)
284 : store_buffer_(store_buffer) {
285 }
286
287 void Callback(MemoryChunk* page, StoreBufferEvent event);
288
289 private:
290 StoreBuffer* store_buffer_;
291
292 // We record in this variable how full the store buffer was when we started
293 // iterating over the current page, finding pointers to new space. If the
294 // store buffer overflows again we can exempt the page from the store buffer
295 // by rewinding to this point instead of having to search the store buffer.
296 Object*** start_of_current_page_;
297 // The current page we are scanning in the store buffer iterator.
298 MemoryChunk* current_page_;
299};
300
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000301
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000302
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000303// A queue of objects promoted during scavenge. Each object is accompanied
304// by it's size to avoid dereferencing a map pointer for scanning.
305class PromotionQueue {
306 public:
danno@chromium.orgc612e022011-11-10 11:38:15 +0000307 explicit PromotionQueue(Heap* heap)
308 : front_(NULL),
309 rear_(NULL),
310 limit_(NULL),
311 emergency_stack_(0),
312 heap_(heap) { }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000313
danno@chromium.orgc612e022011-11-10 11:38:15 +0000314 void Initialize();
315
316 void Destroy() {
317 ASSERT(is_empty());
318 delete emergency_stack_;
319 emergency_stack_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000320 }
321
danno@chromium.orgc612e022011-11-10 11:38:15 +0000322 inline void ActivateGuardIfOnTheSamePage();
323
324 Page* GetHeadPage() {
325 return Page::FromAllocationTop(reinterpret_cast<Address>(rear_));
326 }
327
328 void SetNewLimit(Address limit) {
329 if (!guard_) {
330 return;
331 }
332
333 ASSERT(GetHeadPage() == Page::FromAllocationTop(limit));
334 limit_ = reinterpret_cast<intptr_t*>(limit);
335
336 if (limit_ <= rear_) {
337 return;
338 }
339
340 RelocateQueueHead();
341 }
342
343 bool is_empty() {
344 return (front_ == rear_) &&
345 (emergency_stack_ == NULL || emergency_stack_->length() == 0);
346 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000347
348 inline void insert(HeapObject* target, int size);
349
350 void remove(HeapObject** target, int* size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000351 ASSERT(!is_empty());
danno@chromium.orgc612e022011-11-10 11:38:15 +0000352 if (front_ == rear_) {
353 Entry e = emergency_stack_->RemoveLast();
354 *target = e.obj_;
355 *size = e.size_;
356 return;
357 }
358
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000359 if (NewSpacePage::IsAtStart(reinterpret_cast<Address>(front_))) {
360 NewSpacePage* front_page =
361 NewSpacePage::FromAddress(reinterpret_cast<Address>(front_));
362 ASSERT(!front_page->prev_page()->is_anchor());
363 front_ =
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000364 reinterpret_cast<intptr_t*>(front_page->prev_page()->area_end());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000365 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000366 *target = reinterpret_cast<HeapObject*>(*(--front_));
367 *size = static_cast<int>(*(--front_));
368 // Assert no underflow.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000369 SemiSpace::AssertValidRange(reinterpret_cast<Address>(rear_),
370 reinterpret_cast<Address>(front_));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000371 }
372
373 private:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000374 // The front of the queue is higher in the memory page chain than the rear.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000375 intptr_t* front_;
376 intptr_t* rear_;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000377 intptr_t* limit_;
378
379 bool guard_;
380
381 static const int kEntrySizeInWords = 2;
382
383 struct Entry {
384 Entry(HeapObject* obj, int size) : obj_(obj), size_(size) { }
385
386 HeapObject* obj_;
387 int size_;
388 };
389 List<Entry>* emergency_stack_;
390
391 Heap* heap_;
392
393 void RelocateQueueHead();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000394
395 DISALLOW_COPY_AND_ASSIGN(PromotionQueue);
396};
397
398
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000399typedef void (*ScavengingCallback)(Map* map,
400 HeapObject** slot,
401 HeapObject* object);
402
403
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404// External strings table is a place where all external strings are
405// registered. We need to keep track of such strings to properly
406// finalize them.
407class ExternalStringTable {
408 public:
409 // Registers an external string.
410 inline void AddString(String* string);
411
412 inline void Iterate(ObjectVisitor* v);
413
414 // Restores internal invariant and gets rid of collected strings.
415 // Must be called after each Iterate() that modified the strings.
416 void CleanUp();
417
418 // Destroys all allocated memory.
419 void TearDown();
420
421 private:
422 ExternalStringTable() { }
423
424 friend class Heap;
425
426 inline void Verify();
427
428 inline void AddOldString(String* string);
429
430 // Notifies the table that only a prefix of the new list is valid.
431 inline void ShrinkNewStrings(int position);
432
433 // To speed up scavenge collections new space string are kept
434 // separate from old space strings.
435 List<Object*> new_space_strings_;
436 List<Object*> old_space_strings_;
437
438 Heap* heap_;
439
440 DISALLOW_COPY_AND_ASSIGN(ExternalStringTable);
441};
442
443
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000444// The stack property of an error object is implemented as a getter that
445// formats the attached raw stack trace into a string. This raw stack trace
446// keeps code and function objects alive until the getter is called the first
447// time. To release those objects, we call the getter after each GC for
448// newly tenured error objects that are kept in a list.
449class ErrorObjectList {
450 public:
451 inline void Add(JSObject* object);
452
453 inline void Iterate(ObjectVisitor* v);
454
455 void TearDown();
456
457 void RemoveUnmarked(Heap* heap);
458
459 void DeferredFormatStackTrace(Isolate* isolate);
460
461 void UpdateReferences();
462
463 void UpdateReferencesInNewSpace(Heap* heap);
464
465 private:
466 static const int kBudgetPerGC = 16;
467
468 ErrorObjectList() : nested_(false) { }
469
470 friend class Heap;
471
472 List<Object*> list_;
473 bool nested_;
474
475 DISALLOW_COPY_AND_ASSIGN(ErrorObjectList);
476};
477
478
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000479enum ArrayStorageAllocationMode {
480 DONT_INITIALIZE_ARRAY_ELEMENTS,
481 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE
482};
483
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484class Heap {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 public:
486 // Configure heap size before setup. Return false if the heap has been
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000487 // set up already.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000488 bool ConfigureHeap(int max_semispace_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000489 intptr_t max_old_gen_size,
490 intptr_t max_executable_size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000491 bool ConfigureHeapDefault();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000493 // Prepares the heap, setting up memory areas that are needed in the isolate
494 // without actually creating any objects.
495 bool SetUp();
496
497 // Bootstraps the object heap with the core set of objects required to run.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 // Returns whether it succeeded.
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000499 bool CreateHeapObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500
501 // Destroys all memory allocated by the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000502 void TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000504 // Set the stack limit in the roots_ array. Some architectures generate
505 // code that looks here, because it is faster than loading from the static
506 // jslimit_/real_jslimit_ variable in the StackGuard.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000507 void SetStackLimits();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000508
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000509 // Returns whether SetUp has been called.
510 bool HasBeenSetUp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511
ager@chromium.org3811b432009-10-28 14:53:37 +0000512 // Returns the maximum amount of memory reserved for the heap. For
513 // the young generation, we reserve 4 times the amount needed for a
514 // semi space. The young generation consists of two semi spaces and
515 // we reserve twice the amount needed for those in order to ensure
516 // that new space can be aligned to its size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000517 intptr_t MaxReserved() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000518 return 4 * reserved_semispace_size_ + max_old_generation_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000520 int MaxSemiSpaceSize() { return max_semispace_size_; }
521 int ReservedSemiSpaceSize() { return reserved_semispace_size_; }
522 int InitialSemiSpaceSize() { return initial_semispace_size_; }
523 intptr_t MaxOldGenerationSize() { return max_old_generation_size_; }
524 intptr_t MaxExecutableSize() { return max_executable_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525
526 // Returns the capacity of the heap in bytes w/o growing. Heap grows when
527 // more spaces are needed until it reaches the limit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000528 intptr_t Capacity();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529
ager@chromium.org3811b432009-10-28 14:53:37 +0000530 // Returns the amount of memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000531 intptr_t CommittedMemory();
ager@chromium.org3811b432009-10-28 14:53:37 +0000532
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000533 // Returns the amount of executable memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000534 intptr_t CommittedMemoryExecutable();
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000535
danno@chromium.org72204d52012-10-31 10:02:10 +0000536 // Returns the amount of phyical memory currently committed for the heap.
537 size_t CommittedPhysicalMemory();
538
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000539 // Returns the available bytes in space w/o growing.
540 // Heap doesn't guarantee that it can allocate an object that requires
541 // all available bytes. Check MaxHeapObjectSize() instead.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000542 intptr_t Available();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 // Returns of size of all objects residing in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000545 intptr_t SizeOfObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546
547 // Return the starting address and a mask for the new space. And-masking an
548 // address with the mask will result in the start address of the new space
549 // for all addresses in either semispace.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000550 Address NewSpaceStart() { return new_space_.start(); }
551 uintptr_t NewSpaceMask() { return new_space_.mask(); }
552 Address NewSpaceTop() { return new_space_.top(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000554 NewSpace* new_space() { return &new_space_; }
555 OldSpace* old_pointer_space() { return old_pointer_space_; }
556 OldSpace* old_data_space() { return old_data_space_; }
557 OldSpace* code_space() { return code_space_; }
558 MapSpace* map_space() { return map_space_; }
559 CellSpace* cell_space() { return cell_space_; }
560 LargeObjectSpace* lo_space() { return lo_space_; }
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000561 PagedSpace* paged_space(int idx) {
562 switch (idx) {
563 case OLD_POINTER_SPACE:
564 return old_pointer_space();
565 case OLD_DATA_SPACE:
566 return old_data_space();
567 case MAP_SPACE:
568 return map_space();
569 case CELL_SPACE:
570 return cell_space();
571 case CODE_SPACE:
572 return code_space();
573 case NEW_SPACE:
574 case LO_SPACE:
575 UNREACHABLE();
576 }
577 return NULL;
578 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000580 bool always_allocate() { return always_allocate_scope_depth_ != 0; }
581 Address always_allocate_scope_depth_address() {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000582 return reinterpret_cast<Address>(&always_allocate_scope_depth_);
583 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000584 bool linear_allocation() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000585 return linear_allocation_scope_depth_ != 0;
ager@chromium.org3811b432009-10-28 14:53:37 +0000586 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000587
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000588 Address* NewSpaceAllocationTopAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000589 return new_space_.allocation_top_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000591 Address* NewSpaceAllocationLimitAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000592 return new_space_.allocation_limit_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000593 }
594
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000595 // Uncommit unused semi space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000596 bool UncommitFromSpace() { return new_space_.UncommitFromSpace(); }
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000597
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 // Allocates and initializes a new JavaScript object based on a
599 // constructor.
600 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
601 // failed.
602 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000603 MUST_USE_RESULT MaybeObject* AllocateJSObject(
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000604 JSFunction* constructor,
605 PretenureFlag pretenure = NOT_TENURED);
606
607 MUST_USE_RESULT MaybeObject* AllocateJSObjectWithAllocationSite(
608 JSFunction* constructor,
609 Handle<Object> allocation_site_info_payload);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000611 MUST_USE_RESULT MaybeObject* AllocateJSModule(Context* context,
612 ScopeInfo* scope_info);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000613
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000614 // Allocate a JSArray with no elements
615 MUST_USE_RESULT MaybeObject* AllocateEmptyJSArray(
616 ElementsKind elements_kind,
617 PretenureFlag pretenure = NOT_TENURED) {
618 return AllocateJSArrayAndStorage(elements_kind, 0, 0,
619 DONT_INITIALIZE_ARRAY_ELEMENTS,
620 pretenure);
621 }
622
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000623 inline MUST_USE_RESULT MaybeObject* AllocateEmptyJSArrayWithAllocationSite(
624 ElementsKind elements_kind,
625 Handle<Object> allocation_site_payload);
626
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000627 // Allocate a JSArray with a specified length but elements that are left
628 // uninitialized.
629 MUST_USE_RESULT MaybeObject* AllocateJSArrayAndStorage(
630 ElementsKind elements_kind,
631 int length,
632 int capacity,
633 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
634 PretenureFlag pretenure = NOT_TENURED);
635
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000636 MUST_USE_RESULT MaybeObject* AllocateJSArrayAndStorageWithAllocationSite(
637 ElementsKind elements_kind,
638 int length,
639 int capacity,
640 Handle<Object> allocation_site_payload,
641 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
642
643 MUST_USE_RESULT MaybeObject* AllocateJSArrayStorage(
644 JSArray* array,
645 int length,
646 int capacity,
647 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS);
648
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000649 // Allocate a JSArray with no elements
650 MUST_USE_RESULT MaybeObject* AllocateJSArrayWithElements(
651 FixedArrayBase* array_base,
652 ElementsKind elements_kind,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000653 int length,
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000654 PretenureFlag pretenure = NOT_TENURED);
655
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000656 // Allocates and initializes a new global object based on a constructor.
657 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
658 // failed.
659 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000660 MUST_USE_RESULT MaybeObject* AllocateGlobalObject(JSFunction* constructor);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000661
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000662 // Returns a deep copy of the JavaScript object.
663 // Properties and elements are copied too.
664 // Returns failure if allocation failed.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000665 MUST_USE_RESULT MaybeObject* CopyJSObject(JSObject* source);
666
667 MUST_USE_RESULT MaybeObject* CopyJSObjectWithAllocationSite(JSObject* source);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000668
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669 // Allocates the function prototype.
670 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
671 // failed.
672 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000673 MUST_USE_RESULT MaybeObject* AllocateFunctionPrototype(JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674
lrn@chromium.org34e60782011-09-15 07:25:40 +0000675 // Allocates a Harmony proxy or function proxy.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000676 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
677 // failed.
678 // Please note this does not perform a garbage collection.
679 MUST_USE_RESULT MaybeObject* AllocateJSProxy(Object* handler,
680 Object* prototype);
681
lrn@chromium.org34e60782011-09-15 07:25:40 +0000682 MUST_USE_RESULT MaybeObject* AllocateJSFunctionProxy(Object* handler,
683 Object* call_trap,
684 Object* construct_trap,
685 Object* prototype);
686
687 // Reinitialize a JSReceiver into an (empty) JS object of respective type and
688 // size, but keeping the original prototype. The receiver must have at least
689 // the size of the new object. The object is reinitialized and behaves as an
690 // object that has been freshly allocated.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000691 // Returns failure if an error occured, otherwise object.
lrn@chromium.org34e60782011-09-15 07:25:40 +0000692 MUST_USE_RESULT MaybeObject* ReinitializeJSReceiver(JSReceiver* object,
693 InstanceType type,
694 int size);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000695
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000696 // Reinitialize an JSGlobalProxy based on a constructor. The object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000697 // must have the same size as objects allocated using the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000698 // constructor. The object is reinitialized and behaves as an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699 // object that has been freshly allocated using the constructor.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000700 MUST_USE_RESULT MaybeObject* ReinitializeJSGlobalProxy(
701 JSFunction* constructor, JSGlobalProxy* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702
703 // Allocates and initializes a new JavaScript object based on a map.
704 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
705 // failed.
706 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000707 MUST_USE_RESULT MaybeObject* AllocateJSObjectFromMap(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000708 Map* map, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000710 MUST_USE_RESULT MaybeObject* AllocateJSObjectFromMapWithAllocationSite(
711 Map* map, Handle<Object> allocation_site_info_payload);
712
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 // Allocates a heap object based on the map.
714 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
715 // failed.
716 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000717 MUST_USE_RESULT MaybeObject* Allocate(Map* map, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000719 MUST_USE_RESULT MaybeObject* AllocateWithAllocationSite(Map* map,
720 AllocationSpace space, Handle<Object> allocation_site_info_payload);
721
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722 // Allocates a JS Map in the heap.
723 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
724 // failed.
725 // Please note this function does not perform a garbage collection.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000726 MUST_USE_RESULT MaybeObject* AllocateMap(
727 InstanceType instance_type,
728 int instance_size,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000729 ElementsKind elements_kind = TERMINAL_FAST_ELEMENTS_KIND);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000730
731 // Allocates a partial map for bootstrapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000732 MUST_USE_RESULT MaybeObject* AllocatePartialMap(InstanceType instance_type,
733 int instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734
735 // Allocate a map for the specified function
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000736 MUST_USE_RESULT MaybeObject* AllocateInitialMap(JSFunction* fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000738 // Allocates an empty code cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000739 MUST_USE_RESULT MaybeObject* AllocateCodeCache();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000740
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000741 // Allocates a serialized scope info.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000742 MUST_USE_RESULT MaybeObject* AllocateScopeInfo(int length);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000743
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000744 // Allocates an External object for v8's external API.
745 MUST_USE_RESULT MaybeObject* AllocateExternal(void* value);
746
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000747 // Allocates an empty PolymorphicCodeCache.
748 MUST_USE_RESULT MaybeObject* AllocatePolymorphicCodeCache();
749
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000750 // Allocates a pre-tenured empty AccessorPair.
751 MUST_USE_RESULT MaybeObject* AllocateAccessorPair();
752
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000753 // Allocates an empty TypeFeedbackInfo.
754 MUST_USE_RESULT MaybeObject* AllocateTypeFeedbackInfo();
755
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000756 // Allocates an AliasedArgumentsEntry.
757 MUST_USE_RESULT MaybeObject* AllocateAliasedArgumentsEntry(int slot);
758
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000759 // Clear the Instanceof cache (used when a prototype changes).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000760 inline void ClearInstanceofCache();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000761
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000762 // For use during bootup.
763 void RepairFreeListsAfterBoot();
764
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765 // Allocates and fully initializes a String. There are two String
766 // encodings: ASCII and two byte. One should choose between the three string
767 // allocation functions based on the encoding of the string buffer used to
768 // initialized the string.
769 // - ...FromAscii initializes the string from a buffer that is ASCII
770 // encoded (it does not check that the buffer is ASCII encoded) and the
771 // result will be ASCII encoded.
772 // - ...FromUTF8 initializes the string from a buffer that is UTF-8
773 // encoded. If the characters are all single-byte characters, the
774 // result will be ASCII encoded, otherwise it will converted to two
775 // byte.
776 // - ...FromTwoByte initializes the string from a buffer that is two-byte
777 // encoded. If the characters are all single-byte characters, the
778 // result will be converted to ASCII, otherwise it will be left as
779 // two-byte.
780 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
781 // failed.
782 // Please note this does not perform a garbage collection.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000783 MUST_USE_RESULT MaybeObject* AllocateStringFromOneByte(
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000784 Vector<const uint8_t> str,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 PretenureFlag pretenure = NOT_TENURED);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000786 // TODO(dcarney): remove this function.
787 MUST_USE_RESULT inline MaybeObject* AllocateStringFromOneByte(
788 Vector<const char> str,
789 PretenureFlag pretenure = NOT_TENURED) {
790 return AllocateStringFromOneByte(Vector<const uint8_t>::cast(str),
791 pretenure);
792 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000793 MUST_USE_RESULT inline MaybeObject* AllocateStringFromUtf8(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000794 Vector<const char> str,
795 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000796 MUST_USE_RESULT MaybeObject* AllocateStringFromUtf8Slow(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 Vector<const char> str,
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000798 int non_ascii_start,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000800 MUST_USE_RESULT MaybeObject* AllocateStringFromTwoByte(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801 Vector<const uc16> str,
802 PretenureFlag pretenure = NOT_TENURED);
803
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000804 // Allocates an internalized string in old space based on the character
805 // stream. Returns Failure::RetryAfterGC(requested_bytes, space) if the
806 // allocation failed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 // Please note this function does not perform a garbage collection.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000808 MUST_USE_RESULT inline MaybeObject* AllocateInternalizedStringFromUtf8(
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000809 Vector<const char> str,
810 int chars,
811 uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000812
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000813 MUST_USE_RESULT inline MaybeObject* AllocateOneByteInternalizedString(
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000814 Vector<const uint8_t> str,
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000815 uint32_t hash_field);
816
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000817 MUST_USE_RESULT inline MaybeObject* AllocateTwoByteInternalizedString(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000818 Vector<const uc16> str,
819 uint32_t hash_field);
820
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000821 template<typename T>
822 static inline bool IsOneByte(T t, int chars);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000823
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000824 template<typename T>
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000825 MUST_USE_RESULT inline MaybeObject* AllocateInternalizedStringImpl(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000826 T t, int chars, uint32_t hash_field);
827
828 template<bool is_one_byte, typename T>
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000829 MUST_USE_RESULT MaybeObject* AllocateInternalizedStringImpl(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000830 T t, int chars, uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000831
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000832 // Allocates and partially initializes a String. There are two String
833 // encodings: ASCII and two byte. These functions allocate a string of the
834 // given length and set its map and length fields. The characters of the
835 // string are uninitialized.
836 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
837 // failed.
838 // Please note this does not perform a garbage collection.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000839 MUST_USE_RESULT MaybeObject* AllocateRawOneByteString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840 int length,
841 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000842 MUST_USE_RESULT MaybeObject* AllocateRawTwoByteString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843 int length,
844 PretenureFlag pretenure = NOT_TENURED);
845
846 // Computes a single character string where the character has code.
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000847 // A cache is used for ASCII codes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
849 // failed. Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000850 MUST_USE_RESULT MaybeObject* LookupSingleCharacterStringFromCode(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000851 uint16_t code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852
853 // Allocate a byte array of the specified length
854 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
855 // failed.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000856 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000857 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length,
858 PretenureFlag pretenure);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000859
860 // Allocate a non-tenured byte array of the specified length
861 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
862 // failed.
863 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000864 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865
ager@chromium.org3811b432009-10-28 14:53:37 +0000866 // Allocates an external array of the specified length and type.
867 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
868 // failed.
869 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000870 MUST_USE_RESULT MaybeObject* AllocateExternalArray(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000871 int length,
872 ExternalArrayType array_type,
873 void* external_pointer,
874 PretenureFlag pretenure);
ager@chromium.org3811b432009-10-28 14:53:37 +0000875
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000876 // Allocate a symbol.
877 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
878 // failed.
879 // Please note this does not perform a garbage collection.
880 MUST_USE_RESULT MaybeObject* AllocateSymbol(
881 PretenureFlag pretenure = NOT_TENURED);
882
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000883 // Allocate a tenured JS global property cell.
884 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
885 // failed.
886 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000887 MUST_USE_RESULT MaybeObject* AllocateJSGlobalPropertyCell(Object* value);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 // Allocates a fixed array initialized with undefined values
890 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
891 // failed.
892 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000893 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length,
894 PretenureFlag pretenure);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000895 // Allocates a fixed array initialized with undefined values
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000896 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000898 // Allocates an uninitialized fixed array. It must be filled by the caller.
899 //
900 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
901 // failed.
902 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000903 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedArray(int length);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000904
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000905 // Move len elements within a given array from src_index index to dst_index
906 // index.
907 void MoveElements(FixedArray* array, int dst_index, int src_index, int len);
908
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000909 // Make a copy of src and return it. Returns
910 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000911 MUST_USE_RESULT inline MaybeObject* CopyFixedArray(FixedArray* src);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000912
913 // Make a copy of src, set the map, and return the copy. Returns
914 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000915 MUST_USE_RESULT MaybeObject* CopyFixedArrayWithMap(FixedArray* src, Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000917 // Make a copy of src and return it. Returns
918 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
919 MUST_USE_RESULT inline MaybeObject* CopyFixedDoubleArray(
920 FixedDoubleArray* src);
921
922 // Make a copy of src, set the map, and return the copy. Returns
923 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
924 MUST_USE_RESULT MaybeObject* CopyFixedDoubleArrayWithMap(
925 FixedDoubleArray* src, Map* map);
926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 // Allocates a fixed array initialized with the hole values.
928 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
929 // failed.
930 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000931 MUST_USE_RESULT MaybeObject* AllocateFixedArrayWithHoles(
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000932 int length,
933 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000935 MUST_USE_RESULT MaybeObject* AllocateRawFixedDoubleArray(
936 int length,
937 PretenureFlag pretenure);
938
939 // Allocates a fixed double array with uninitialized values. Returns
940 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
941 // Please note this does not perform a garbage collection.
942 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedDoubleArray(
943 int length,
944 PretenureFlag pretenure = NOT_TENURED);
945
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000946 // Allocates a fixed double array with hole values. Returns
947 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
948 // Please note this does not perform a garbage collection.
949 MUST_USE_RESULT MaybeObject* AllocateFixedDoubleArrayWithHoles(
950 int length,
951 PretenureFlag pretenure = NOT_TENURED);
952
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000953 // AllocateHashTable is identical to AllocateFixedArray except
954 // that the resulting object has hash_table_map as map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000955 MUST_USE_RESULT MaybeObject* AllocateHashTable(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000956 int length, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000958 // Allocate a native (but otherwise uninitialized) context.
959 MUST_USE_RESULT MaybeObject* AllocateNativeContext();
960
961 // Allocate a global context.
962 MUST_USE_RESULT MaybeObject* AllocateGlobalContext(JSFunction* function,
963 ScopeInfo* scope_info);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000964
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000965 // Allocate a module context.
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000966 MUST_USE_RESULT MaybeObject* AllocateModuleContext(ScopeInfo* scope_info);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000967
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 // Allocate a function context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000969 MUST_USE_RESULT MaybeObject* AllocateFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000970 JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000972 // Allocate a catch context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000973 MUST_USE_RESULT MaybeObject* AllocateCatchContext(JSFunction* function,
974 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000975 String* name,
976 Object* thrown_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 // Allocate a 'with' context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000978 MUST_USE_RESULT MaybeObject* AllocateWithContext(JSFunction* function,
979 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000980 JSObject* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000982 // Allocate a block context.
983 MUST_USE_RESULT MaybeObject* AllocateBlockContext(JSFunction* function,
984 Context* previous,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000985 ScopeInfo* info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 // Allocates a new utility object in the old generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000988 MUST_USE_RESULT MaybeObject* AllocateStruct(InstanceType type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990 // Allocates a function initialized with a shared part.
991 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
992 // failed.
993 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000994 MUST_USE_RESULT MaybeObject* AllocateFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000995 Map* function_map,
996 SharedFunctionInfo* shared,
997 Object* prototype,
998 PretenureFlag pretenure = TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000999
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001000 // Arguments object size.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001001 static const int kArgumentsObjectSize =
1002 JSObject::kHeaderSize + 2 * kPointerSize;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001003 // Strict mode arguments has no callee so it is smaller.
1004 static const int kArgumentsObjectSizeStrict =
1005 JSObject::kHeaderSize + 1 * kPointerSize;
1006 // Indicies for direct access into argument objects.
1007 static const int kArgumentsLengthIndex = 0;
1008 // callee is only valid in non-strict mode.
1009 static const int kArgumentsCalleeIndex = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010
1011 // Allocates an arguments object - optionally with an elements array.
1012 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1013 // failed.
1014 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001015 MUST_USE_RESULT MaybeObject* AllocateArgumentsObject(
1016 Object* callee, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018 // Same as NewNumberFromDouble, but may return a preallocated/immutable
1019 // number object (e.g., minus_zero_value_, nan_value_)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001020 MUST_USE_RESULT MaybeObject* NumberFromDouble(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001021 double value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
1023 // Allocated a HeapNumber from value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001024 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(
lrn@chromium.org303ada72010-10-27 09:33:13 +00001025 double value,
1026 PretenureFlag pretenure);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001027 // pretenure = NOT_TENURED
1028 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(double value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029
1030 // Converts an int into either a Smi or a HeapNumber object.
1031 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1032 // failed.
1033 // Please note this does not perform a garbage collection.
erikcorry0ad885c2011-11-21 13:51:57 +00001034 MUST_USE_RESULT inline MaybeObject* NumberFromInt32(
1035 int32_t value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036
1037 // Converts an int into either a Smi or a HeapNumber object.
1038 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1039 // failed.
1040 // Please note this does not perform a garbage collection.
erikcorry0ad885c2011-11-21 13:51:57 +00001041 MUST_USE_RESULT inline MaybeObject* NumberFromUint32(
1042 uint32_t value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001044 // Allocates a new foreign object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1046 // failed.
1047 // Please note this does not perform a garbage collection.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001048 MUST_USE_RESULT MaybeObject* AllocateForeign(
1049 Address address, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001050
1051 // Allocates a new SharedFunctionInfo object.
1052 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1053 // failed.
1054 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001055 MUST_USE_RESULT MaybeObject* AllocateSharedFunctionInfo(Object* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001057 // Allocates a new JSMessageObject object.
1058 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1059 // failed.
1060 // Please note that this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001061 MUST_USE_RESULT MaybeObject* AllocateJSMessageObject(
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001062 String* type,
1063 JSArray* arguments,
1064 int start_position,
1065 int end_position,
1066 Object* script,
1067 Object* stack_trace,
1068 Object* stack_frames);
1069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 // Allocates a new cons string object.
1071 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1072 // failed.
1073 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001074 MUST_USE_RESULT MaybeObject* AllocateConsString(String* first,
1075 String* second);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 // Allocates a new sub string object which is a substring of an underlying
1078 // string buffer stretching from the index start (inclusive) to the index
1079 // end (exclusive).
1080 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1081 // failed.
1082 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001083 MUST_USE_RESULT MaybeObject* AllocateSubString(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001084 String* buffer,
1085 int start,
1086 int end,
1087 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088
1089 // Allocate a new external string object, which is backed by a string
1090 // resource that resides outside the V8 heap.
1091 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1092 // failed.
1093 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001094 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001095 const ExternalAsciiString::Resource* resource);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001096 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001097 const ExternalTwoByteString::Resource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001099 // Finalizes an external string by deleting the associated external
1100 // data and clearing the resource pointer.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001101 inline void FinalizeExternalString(String* string);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001102
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001103 // Allocates an uninitialized object. The memory is non-executable if the
1104 // hardware and OS allow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1106 // failed.
1107 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001108 MUST_USE_RESULT inline MaybeObject* AllocateRaw(int size_in_bytes,
1109 AllocationSpace space,
1110 AllocationSpace retry_space);
kasper.lund7276f142008-07-30 08:49:36 +00001111
ager@chromium.org6f10e412009-02-13 10:11:16 +00001112 // Initialize a filler object to keep the ability to iterate over the heap
1113 // when shortening objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001114 void CreateFillerObjectAt(Address addr, int size);
ager@chromium.org6f10e412009-02-13 10:11:16 +00001115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 // Makes a new native code object
1117 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001118 // failed. On success, the pointer to the Code object is stored in the
1119 // self_reference. This allows generated code to reference its own Code
1120 // object by containing this pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001122 MUST_USE_RESULT MaybeObject* CreateCode(const CodeDesc& desc,
1123 Code::Flags flags,
1124 Handle<Object> self_reference,
1125 bool immovable = false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001127 MUST_USE_RESULT MaybeObject* CopyCode(Code* code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001128
1129 // Copy the code and scope info part of the code object, but insert
1130 // the provided data as the relocation information.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001131 MUST_USE_RESULT MaybeObject* CopyCode(Code* code, Vector<byte> reloc_info);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001132
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001133 // Finds the internalized copy for string in the string table.
1134 // If not found, a new string is added to the table and returned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 // Returns Failure::RetryAfterGC(requested_bytes, space) if allocation
1136 // failed.
1137 // Please note this function does not perform a garbage collection.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001138 MUST_USE_RESULT MaybeObject* InternalizeUtf8String(Vector<const char> str);
1139 MUST_USE_RESULT MaybeObject* InternalizeUtf8String(const char* str) {
1140 return InternalizeUtf8String(CStrVector(str));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001142 MUST_USE_RESULT MaybeObject* InternalizeOneByteString(
1143 Vector<const uint8_t> str);
1144 MUST_USE_RESULT MaybeObject* InternalizeTwoByteString(Vector<const uc16> str);
1145 MUST_USE_RESULT MaybeObject* InternalizeString(String* str);
1146 MUST_USE_RESULT MaybeObject* InternalizeOneByteString(
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001147 Handle<SeqOneByteString> string, int from, int length);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001148
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001149 bool InternalizeStringIfExists(String* str, String** result);
1150 bool InternalizeTwoCharsStringIfExists(String* str, String** result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001152 // Compute the matching internalized string map for a string if possible.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153 // NULL is returned if string is in new space or not flattened.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001154 Map* InternalizedStringMapForString(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001156 // Tries to flatten a string before compare operation.
1157 //
1158 // Returns a failure in case it was decided that flattening was
1159 // necessary and failed. Note, if flattening is not necessary the
1160 // string might stay non-flat even when not a failure is returned.
1161 //
1162 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001163 MUST_USE_RESULT inline MaybeObject* PrepareForCompare(String* str);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 // Converts the given boolean condition to JavaScript boolean value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001166 inline Object* ToBoolean(bool condition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167
1168 // Code that should be run before and after each GC. Includes some
1169 // reporting/verification activities when compiled with DEBUG set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 void GarbageCollectionPrologue();
1171 void GarbageCollectionEpilogue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172
1173 // Performs garbage collection operation.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001174 // Returns whether there is a chance that another major GC could
1175 // collect more garbage.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001176 bool CollectGarbage(AllocationSpace space,
1177 GarbageCollector collector,
1178 const char* gc_reason,
1179 const char* collector_reason);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001180
1181 // Performs garbage collection operation.
1182 // Returns whether there is a chance that another major GC could
1183 // collect more garbage.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001184 inline bool CollectGarbage(AllocationSpace space,
1185 const char* gc_reason = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001187 static const int kNoGCFlags = 0;
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001188 static const int kSweepPreciselyMask = 1;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001189 static const int kReduceMemoryFootprintMask = 2;
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001190 static const int kAbortIncrementalMarkingMask = 4;
1191
1192 // Making the heap iterable requires us to sweep precisely and abort any
1193 // incremental marking as well.
1194 static const int kMakeHeapIterableMask =
1195 kSweepPreciselyMask | kAbortIncrementalMarkingMask;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001196
1197 // Performs a full garbage collection. If (flags & kMakeHeapIterableMask) is
1198 // non-zero, then the slower precise sweeper is used, which leaves the heap
1199 // in a state where we can iterate over the heap visiting all objects.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001200 void CollectAllGarbage(int flags, const char* gc_reason = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001201
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001202 // Last hope GC, should try to squeeze as much as possible.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001203 void CollectAllAvailableGarbage(const char* gc_reason = NULL);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001204
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001205 // Check whether the heap is currently iterable.
1206 bool IsHeapIterable();
1207
1208 // Ensure that we have swept all spaces in such a way that we can iterate
1209 // over all objects. May cause a GC.
1210 void EnsureHeapIsIterable();
1211
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001212 // Notify the heap that a context has been disposed.
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001213 int NotifyContextDisposed() {
1214 flush_monomorphic_ics_ = true;
1215 return ++contexts_disposed_;
1216 }
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001217
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 // Utility to invoke the scavenger. This is needed in test code to
1219 // ensure correct callback for weak global handles.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001220 void PerformScavenge();
1221
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001222 inline void increment_scan_on_scavenge_pages() {
1223 scan_on_scavenge_pages_++;
1224 if (FLAG_gc_verbose) {
1225 PrintF("Scan-on-scavenge pages: %d\n", scan_on_scavenge_pages_);
1226 }
1227 }
1228
1229 inline void decrement_scan_on_scavenge_pages() {
1230 scan_on_scavenge_pages_--;
1231 if (FLAG_gc_verbose) {
1232 PrintF("Scan-on-scavenge pages: %d\n", scan_on_scavenge_pages_);
1233 }
1234 }
1235
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001236 PromotionQueue* promotion_queue() { return &promotion_queue_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001238#ifdef DEBUG
1239 // Utility used with flag gc-greedy.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001240 void GarbageCollectionGreedyCheck();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001241#endif
1242
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001243 void AddGCPrologueCallback(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001244 GCPrologueCallback callback, GCType gc_type_filter);
1245 void RemoveGCPrologueCallback(GCPrologueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001246
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001247 void AddGCEpilogueCallback(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001248 GCEpilogueCallback callback, GCType gc_type_filter);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001249 void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001250
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001251 void SetGlobalGCPrologueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001252 ASSERT((callback == NULL) ^ (global_gc_prologue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 global_gc_prologue_callback_ = callback;
1254 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001255 void SetGlobalGCEpilogueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001256 ASSERT((callback == NULL) ^ (global_gc_epilogue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001257 global_gc_epilogue_callback_ = callback;
1258 }
1259
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001260 // Heap root getters. We have versions with and without type::cast() here.
1261 // You can't use type::cast during GC because the assert fails.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001262 // TODO(1490): Try removing the unchecked accessors, now that GC marking does
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001263 // not corrupt the map.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001264#define ROOT_ACCESSOR(type, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001265 type* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001266 return type::cast(roots_[k##camel_name##RootIndex]); \
1267 } \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001268 type* raw_unchecked_##name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001269 return reinterpret_cast<type*>(roots_[k##camel_name##RootIndex]); \
1270 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 ROOT_LIST(ROOT_ACCESSOR)
1272#undef ROOT_ACCESSOR
1273
1274// Utility type maps
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001275#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001276 Map* name##_map() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001277 return Map::cast(roots_[k##Name##MapRootIndex]); \
1278 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
1280#undef STRUCT_MAP_ACCESSOR
1281
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001282#define STRING_ACCESSOR(name, str) String* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001283 return String::cast(roots_[k##name##RootIndex]); \
1284 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001285 INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
1286#undef STRING_ACCESSOR
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001288 // The hidden_string is special because it is the empty string, but does
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001289 // not match the empty string.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001290 String* hidden_string() { return hidden_string_; }
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001291
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001292 void set_native_contexts_list(Object* object) {
1293 native_contexts_list_ = object;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001294 }
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001295 Object* native_contexts_list() { return native_contexts_list_; }
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001296
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001297 // Number of mark-sweeps.
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +00001298 unsigned int ms_count() { return ms_count_; }
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 // Iterates over all roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001301 void IterateRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302 // Iterates over all strong roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001303 void IterateStrongRoots(ObjectVisitor* v, VisitMode mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001304 // Iterates over all the other roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001305 void IterateWeakRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001307 // Iterate pointers to from semispace of new space found in memory interval
1308 // from start to end.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001309 void IterateAndMarkPointersToFromSpace(Address start,
1310 Address end,
1311 ObjectSlotCallback callback);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001312
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 // Returns whether the object resides in new space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001314 inline bool InNewSpace(Object* object);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001315 inline bool InNewSpace(Address addr);
1316 inline bool InNewSpacePage(Address addr);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001317 inline bool InFromSpace(Object* object);
1318 inline bool InToSpace(Object* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319
1320 // Checks whether an address/object in the heap (including auxiliary
1321 // area and unused area).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001322 bool Contains(Address addr);
1323 bool Contains(HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324
1325 // Checks whether an address/object in a space.
ager@chromium.org3811b432009-10-28 14:53:37 +00001326 // Currently used by tests, serialization and heap verification only.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001327 bool InSpace(Address addr, AllocationSpace space);
1328 bool InSpace(HeapObject* value, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329
kasper.lund7276f142008-07-30 08:49:36 +00001330 // Finds out which space an object should get promoted to based on its type.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001331 inline OldSpace* TargetSpace(HeapObject* object);
1332 inline AllocationSpace TargetSpaceId(InstanceType type);
kasper.lund7276f142008-07-30 08:49:36 +00001333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 // Sets the stub_cache_ (only used when expanding the dictionary).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001335 void public_set_code_stubs(UnseededNumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001336 roots_[kCodeStubsRootIndex] = value;
1337 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001339 // Support for computing object sizes for old objects during GCs. Returns
1340 // a function that is guaranteed to be safe for computing object sizes in
1341 // the current GC phase.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001342 HeapObjectCallback GcSafeSizeOfOldObjectFunction() {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001343 return gc_safe_size_of_old_object_;
1344 }
1345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 // Sets the non_monomorphic_cache_ (only used when expanding the dictionary).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001347 void public_set_non_monomorphic_cache(UnseededNumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001348 roots_[kNonMonomorphicCacheRootIndex] = value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 }
1350
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001351 void public_set_empty_script(Script* script) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001352 roots_[kEmptyScriptRootIndex] = script;
1353 }
1354
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001355 void public_set_store_buffer_top(Address* top) {
1356 roots_[kStoreBufferTopRootIndex] = reinterpret_cast<Smi*>(top);
1357 }
1358
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001359 // Update the next script id.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001360 inline void SetLastScriptId(Object* last_script_id);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001361
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001362 // Generated code can embed this address to get access to the roots.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001363 Object** roots_array_start() { return roots_; }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001364
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001365 Address* store_buffer_top_address() {
1366 return reinterpret_cast<Address*>(&roots_[kStoreBufferTopRootIndex]);
1367 }
1368
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001369 // Get address of native contexts list for serialization support.
1370 Object** native_contexts_list_address() {
1371 return &native_contexts_list_;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001372 }
1373
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001374#ifdef VERIFY_HEAP
1375 // Verify the heap is in its normal state before or after a GC.
1376 void Verify();
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00001377
1378
1379 bool weak_embedded_maps_verification_enabled() {
1380 return no_weak_embedded_maps_verification_scope_depth_ == 0;
1381 }
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001382#endif
1383
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001385 void Print();
1386 void PrintHandles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001388 void OldPointerSpaceCheckStoreBuffer();
1389 void MapSpaceCheckStoreBuffer();
1390 void LargeObjectSpaceCheckStoreBuffer();
1391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 // Report heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001393 void ReportHeapStatistics(const char* title);
1394 void ReportCodeStatistics(const char* title);
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001395#endif
1396
1397 // Zapping is needed for verify heap, and always done in debug builds.
1398 static inline bool ShouldZapGarbage() {
1399#ifdef DEBUG
1400 return true;
1401#else
1402#ifdef VERIFY_HEAP
1403 return FLAG_verify_heap;
1404#else
1405 return false;
1406#endif
1407#endif
1408 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409
1410 // Fill in bogus values in from space
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001411 void ZapFromSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001413 // Print short heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001414 void PrintShortHeapStatistics();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001415
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001416 // Makes a new internalized string object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1418 // failed.
1419 // Please note this function does not perform a garbage collection.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001420 MUST_USE_RESULT MaybeObject* CreateInternalizedString(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001421 const char* str, int length, int hash);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001422 MUST_USE_RESULT MaybeObject* CreateInternalizedString(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423
1424 // Write barrier support for address[offset] = o.
ulan@chromium.org2e04b582013-02-21 14:06:02 +00001425 INLINE(void RecordWrite(Address address, int offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001427 // Write barrier support for address[start : start + len[ = o.
ulan@chromium.org2e04b582013-02-21 14:06:02 +00001428 INLINE(void RecordWrites(Address address, int start, int len));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001429
kasper.lund7276f142008-07-30 08:49:36 +00001430 // Given an address occupied by a live code object, return that object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001431 Object* FindCodeObject(Address a);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 enum HeapState { NOT_IN_GC, SCAVENGE, MARK_COMPACT };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001434 inline HeapState gc_state() { return gc_state_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001436 inline bool IsInGCPostProcessing() { return gc_post_processing_depth_ > 0; }
1437
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001439 bool IsAllocationAllowed() { return allocation_allowed_; }
1440 inline bool allow_allocation(bool enable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001442 bool disallow_allocation_failure() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 return disallow_allocation_failure_;
1444 }
1445
ulan@chromium.orgea52b5f2012-07-30 13:05:33 +00001446 void TracePathToObjectFrom(Object* target, Object* root);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001447 void TracePathToObject(Object* target);
1448 void TracePathToGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449#endif
1450
ager@chromium.org32912102009-01-16 10:38:43 +00001451 // Callback function passed to Heap::Iterate etc. Copies an object if
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001452 // necessary, the object might be promoted to an old space. The caller must
1453 // ensure the precondition that the object is (a) a heap object and (b) in
1454 // the heap's from space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001455 static inline void ScavengePointer(HeapObject** p);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001456 static inline void ScavengeObject(HeapObject** p, HeapObject* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001458 // Commits from space if it is uncommitted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001459 void EnsureFromSpaceIsCommitted();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001460
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001461 // Support for partial snapshots. After calling this we have a linear
1462 // space to write objects in each space.
1463 void ReserveSpace(int *sizes, Address* addresses);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001464
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 //
1466 // Support for the API.
1467 //
1468
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001469 bool CreateApiObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470
1471 // Attempt to find the number in a small cache. If we finds it, return
1472 // the string representation of the number. Otherwise return undefined.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001473 Object* GetNumberStringCache(Object* number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474
1475 // Update the cache with a new number-string pair.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001476 void SetNumberStringCache(Object* number, String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477
kasper.lund7276f142008-07-30 08:49:36 +00001478 // Adjusts the amount of registered external memory.
1479 // Returns the adjusted value.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001480 inline intptr_t AdjustAmountOfExternalAllocatedMemory(
1481 intptr_t change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00001482
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001483 // Allocate uninitialized fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001484 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length);
1485 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length,
1486 PretenureFlag pretenure);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001487
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001488 inline intptr_t PromotedTotalSize() {
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001489 return PromotedSpaceSizeOfObjects() + PromotedExternalMemorySize();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001490 }
1491
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001492 // True if we have reached the allocation limit in the old generation that
1493 // should force the next GC (caused normally) to be a full one.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001494 inline bool OldGenerationPromotionLimitReached() {
1495 return PromotedTotalSize() > old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001496 }
1497
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001498 inline intptr_t OldGenerationSpaceAvailable() {
1499 return old_gen_allocation_limit_ - PromotedTotalSize();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001500 }
1501
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00001502 inline intptr_t OldGenerationCapacityAvailable() {
1503 return max_old_generation_size_ - PromotedTotalSize();
1504 }
1505
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00001506 static const intptr_t kMinimumPromotionLimit = 5 * Page::kPageSize;
1507 static const intptr_t kMinimumAllocationLimit =
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001508 8 * (Page::kPageSize > MB ? Page::kPageSize : MB);
1509
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00001510 intptr_t OldGenPromotionLimit(intptr_t old_gen_size) {
1511 const int divisor = FLAG_stress_compaction ? 10 : 3;
1512 intptr_t limit =
1513 Max(old_gen_size + old_gen_size / divisor, kMinimumPromotionLimit);
1514 limit += new_space_.Capacity();
1515 limit *= old_gen_limit_factor_;
1516 intptr_t halfway_to_the_max = (old_gen_size + max_old_generation_size_) / 2;
1517 return Min(limit, halfway_to_the_max);
1518 }
1519
1520 intptr_t OldGenAllocationLimit(intptr_t old_gen_size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001521 const int divisor = FLAG_stress_compaction ? 8 : 2;
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +00001522 intptr_t limit =
1523 Max(old_gen_size + old_gen_size / divisor, kMinimumAllocationLimit);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001524 limit += new_space_.Capacity();
1525 limit *= old_gen_limit_factor_;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001526 intptr_t halfway_to_the_max = (old_gen_size + max_old_generation_size_) / 2;
1527 return Min(limit, halfway_to_the_max);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001528 }
1529
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001530 // Implements the corresponding V8 API function.
1531 bool IdleNotification(int hint);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001532
1533 // Declare all the root indices.
1534 enum RootListIndex {
1535#define ROOT_INDEX_DECLARATION(type, name, camel_name) k##camel_name##RootIndex,
1536 STRONG_ROOT_LIST(ROOT_INDEX_DECLARATION)
1537#undef ROOT_INDEX_DECLARATION
1538
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001539#define STRING_INDEX_DECLARATION(name, str) k##name##RootIndex,
1540 INTERNALIZED_STRING_LIST(STRING_INDEX_DECLARATION)
1541#undef STRING_DECLARATION
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001542
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00001543 // Utility type maps
1544#define DECLARE_STRUCT_MAP(NAME, Name, name) k##Name##MapRootIndex,
1545 STRUCT_LIST(DECLARE_STRUCT_MAP)
1546#undef DECLARE_STRUCT_MAP
1547
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001548 kStringTableRootIndex,
1549 kStrongRootListLength = kStringTableRootIndex,
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001550 kRootListLength
1551 };
1552
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001553 STATIC_CHECK(kUndefinedValueRootIndex == Internals::kUndefinedValueRootIndex);
1554 STATIC_CHECK(kNullValueRootIndex == Internals::kNullValueRootIndex);
1555 STATIC_CHECK(kTrueValueRootIndex == Internals::kTrueValueRootIndex);
1556 STATIC_CHECK(kFalseValueRootIndex == Internals::kFalseValueRootIndex);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001557 STATIC_CHECK(kempty_stringRootIndex == Internals::kEmptyStringRootIndex);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001558
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001559 // Generated code can embed direct references to non-writable roots if
1560 // they are in new space.
1561 static bool RootCanBeWrittenAfterInitialization(RootListIndex root_index);
1562
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001563 MUST_USE_RESULT MaybeObject* NumberToString(
1564 Object* number, bool check_number_string_cache = true);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001565 MUST_USE_RESULT MaybeObject* Uint32ToString(
1566 uint32_t value, bool check_number_string_cache = true);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001567
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001568 Map* MapForExternalArrayType(ExternalArrayType array_type);
1569 RootListIndex RootIndexForExternalArrayType(
ager@chromium.org3811b432009-10-28 14:53:37 +00001570 ExternalArrayType array_type);
1571
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001572 void RecordStats(HeapStats* stats, bool take_snapshot = false);
ager@chromium.org60121232009-12-03 11:25:37 +00001573
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001574 // Copy block of memory from src to dst. Size of block should be aligned
1575 // by pointer size.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001576 static inline void CopyBlock(Address dst, Address src, int byte_size);
1577
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001578 // Optimized version of memmove for blocks with pointer size aligned sizes and
1579 // pointer size aligned addresses.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001580 static inline void MoveBlock(Address dst, Address src, int byte_size);
1581
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001582 // Check new space expansion criteria and expand semispaces if it was hit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001583 void CheckNewSpaceExpansionCriteria();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001584
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001585 inline void IncrementYoungSurvivorsCounter(int survived) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001586 ASSERT(survived >= 0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001587 young_survivors_after_last_gc_ = survived;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001588 survived_since_last_expansion_ += survived;
1589 }
1590
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001591 inline bool NextGCIsLikelyToBeFull() {
1592 if (FLAG_gc_global) return true;
1593
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001594 if (FLAG_stress_compaction && (gc_count_ & 1) != 0) return true;
1595
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001596 intptr_t total_promoted = PromotedTotalSize();
1597
1598 intptr_t adjusted_promotion_limit =
1599 old_gen_promotion_limit_ - new_space_.Capacity();
1600
1601 if (total_promoted >= adjusted_promotion_limit) return true;
1602
1603 intptr_t adjusted_allocation_limit =
1604 old_gen_allocation_limit_ - new_space_.Capacity() / 5;
1605
jkummerow@chromium.org212d9642012-05-11 15:02:09 +00001606 if (PromotedSpaceSizeOfObjects() >= adjusted_allocation_limit) return true;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001607
1608 return false;
1609 }
1610
1611
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001612 void UpdateNewSpaceReferencesInExternalStringTable(
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001613 ExternalStringTableUpdaterCallback updater_func);
1614
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001615 void UpdateReferencesInExternalStringTable(
1616 ExternalStringTableUpdaterCallback updater_func);
1617
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001618 void ProcessWeakReferences(WeakObjectRetainer* retainer);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001619
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001620 void VisitExternalResources(v8::ExternalResourceVisitor* visitor);
1621
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001622 // Helper function that governs the promotion policy from new space to
1623 // old. If the object's old address lies below the new space's age
1624 // mark or if we've already filled the bottom 1/16th of the to space,
1625 // we try to promote this object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001626 inline bool ShouldBePromoted(Address old_address, int object_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001627
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001628 int MaxObjectSizeInNewSpace() { return kMaxObjectSizeInNewSpace; }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001629
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001630 void ClearJSFunctionResultCaches();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001631
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001632 void ClearNormalizedMapCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00001633
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001634 GCTracer* tracer() { return tracer_; }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001635
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001636 // Returns the size of objects residing in non new spaces.
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001637 intptr_t PromotedSpaceSizeOfObjects();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001638
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001639 double total_regexp_code_generated() { return total_regexp_code_generated_; }
1640 void IncreaseTotalRegexpCodeGenerated(int size) {
1641 total_regexp_code_generated_ += size;
1642 }
1643
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001644 // Returns maximum GC pause.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001645 double get_max_gc_pause() { return max_gc_pause_; }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001646
1647 // Returns maximum size of objects alive after GC.
1648 intptr_t get_max_alive_after_gc() { return max_alive_after_gc_; }
1649
1650 // Returns minimal interval between two subsequent collections.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001651 double get_min_in_mutator() { return min_in_mutator_; }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001652
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001653 // TODO(hpayer): remove, should be handled by GCTracer
1654 void AddMarkingTime(double marking_time) {
1655 marking_time_ += marking_time;
1656 }
1657
1658 double marking_time() const {
1659 return marking_time_;
1660 }
1661
1662 // TODO(hpayer): remove, should be handled by GCTracer
1663 void AddSweepingTime(double sweeping_time) {
1664 sweeping_time_ += sweeping_time;
1665 }
1666
1667 double sweeping_time() const {
1668 return sweeping_time_;
1669 }
1670
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001671 MarkCompactCollector* mark_compact_collector() {
1672 return &mark_compact_collector_;
1673 }
1674
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001675 StoreBuffer* store_buffer() {
1676 return &store_buffer_;
1677 }
1678
1679 Marking* marking() {
1680 return &marking_;
1681 }
1682
1683 IncrementalMarking* incremental_marking() {
1684 return &incremental_marking_;
1685 }
1686
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001687 bool IsSweepingComplete() {
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00001688 return !mark_compact_collector()->IsConcurrentSweepingInProgress() &&
1689 old_data_space()->IsLazySweepingComplete() &&
1690 old_pointer_space()->IsLazySweepingComplete();
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001691 }
1692
1693 bool AdvanceSweepers(int step_size) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001694 ASSERT(!FLAG_parallel_sweeping && !FLAG_concurrent_sweeping);
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001695 bool sweeping_complete = old_data_space()->AdvanceSweeper(step_size);
1696 sweeping_complete &= old_pointer_space()->AdvanceSweeper(step_size);
1697 return sweeping_complete;
1698 }
1699
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001700 ExternalStringTable* external_string_table() {
1701 return &external_string_table_;
1702 }
1703
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001704 ErrorObjectList* error_object_list() {
1705 return &error_object_list_;
1706 }
1707
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001708 // Returns the current sweep generation.
1709 int sweep_generation() {
1710 return sweep_generation_;
1711 }
1712
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001713 inline Isolate* isolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001714
yangguo@chromium.org003650e2013-01-24 16:31:08 +00001715 void CallGCPrologueCallbacks(GCType gc_type);
1716 void CallGCEpilogueCallbacks(GCType gc_type);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001717
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001718 inline bool OldGenerationAllocationLimitReached();
1719
1720 inline void DoScavengeObject(Map* map, HeapObject** slot, HeapObject* obj) {
1721 scavenging_visitors_table_.GetVisitor(map)(map, slot, obj);
1722 }
1723
1724 void QueueMemoryChunkForFree(MemoryChunk* chunk);
1725 void FreeQueuedChunks();
1726
1727 // Completely clear the Instanceof cache (to stop it keeping objects alive
1728 // around a GC).
1729 inline void CompletelyClearInstanceofCache();
1730
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001731 // The roots that have an index less than this are always in old space.
1732 static const int kOldSpaceRoots = 0x20;
1733
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001734 uint32_t HashSeed() {
1735 uint32_t seed = static_cast<uint32_t>(hash_seed()->value());
1736 ASSERT(FLAG_randomize_hashes || seed == 0);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001737 return seed;
1738 }
1739
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001740 void SetArgumentsAdaptorDeoptPCOffset(int pc_offset) {
1741 ASSERT(arguments_adaptor_deopt_pc_offset() == Smi::FromInt(0));
1742 set_arguments_adaptor_deopt_pc_offset(Smi::FromInt(pc_offset));
1743 }
1744
ulan@chromium.org967e2702012-02-28 09:49:15 +00001745 void SetConstructStubDeoptPCOffset(int pc_offset) {
1746 ASSERT(construct_stub_deopt_pc_offset() == Smi::FromInt(0));
1747 set_construct_stub_deopt_pc_offset(Smi::FromInt(pc_offset));
1748 }
1749
mstarzinger@chromium.orgde886792012-09-11 13:22:37 +00001750 void SetGetterStubDeoptPCOffset(int pc_offset) {
1751 ASSERT(getter_stub_deopt_pc_offset() == Smi::FromInt(0));
1752 set_getter_stub_deopt_pc_offset(Smi::FromInt(pc_offset));
1753 }
1754
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001755 void SetSetterStubDeoptPCOffset(int pc_offset) {
1756 ASSERT(setter_stub_deopt_pc_offset() == Smi::FromInt(0));
1757 set_setter_stub_deopt_pc_offset(Smi::FromInt(pc_offset));
1758 }
1759
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001760 // For post mortem debugging.
1761 void RememberUnmappedPage(Address page, bool compacted);
1762
danno@chromium.org88aa0582012-03-23 15:11:57 +00001763 // Global inline caching age: it is incremented on some GCs after context
1764 // disposal. We use it to flush inline caches.
1765 int global_ic_age() {
1766 return global_ic_age_;
1767 }
1768
1769 void AgeInlineCaches() {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001770 global_ic_age_ = (global_ic_age_ + 1) & SharedFunctionInfo::ICAgeBits::kMax;
danno@chromium.org88aa0582012-03-23 15:11:57 +00001771 }
1772
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001773 bool flush_monomorphic_ics() { return flush_monomorphic_ics_; }
1774
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001775 intptr_t amount_of_external_allocated_memory() {
1776 return amount_of_external_allocated_memory_;
1777 }
1778
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001779 // ObjectStats are kept in two arrays, counts and sizes. Related stats are
1780 // stored in a contiguous linear buffer. Stats groups are stored one after
1781 // another.
1782 enum {
1783 FIRST_CODE_KIND_SUB_TYPE = LAST_TYPE + 1,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001784 FIRST_FIXED_ARRAY_SUB_TYPE =
1785 FIRST_CODE_KIND_SUB_TYPE + Code::LAST_CODE_KIND + 1,
1786 OBJECT_STATS_COUNT =
1787 FIRST_FIXED_ARRAY_SUB_TYPE + LAST_FIXED_ARRAY_SUB_TYPE + 1
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001788 };
1789
1790 void RecordObjectStats(InstanceType type, int sub_type, size_t size) {
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001791 ASSERT(type <= LAST_TYPE);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001792 if (sub_type < 0) {
1793 object_counts_[type]++;
1794 object_sizes_[type] += size;
1795 } else {
1796 if (type == CODE_TYPE) {
1797 ASSERT(sub_type <= Code::LAST_CODE_KIND);
1798 object_counts_[FIRST_CODE_KIND_SUB_TYPE + sub_type]++;
1799 object_sizes_[FIRST_CODE_KIND_SUB_TYPE + sub_type] += size;
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001800 } else if (type == FIXED_ARRAY_TYPE) {
1801 ASSERT(sub_type <= LAST_FIXED_ARRAY_SUB_TYPE);
1802 object_counts_[FIRST_FIXED_ARRAY_SUB_TYPE + sub_type]++;
1803 object_sizes_[FIRST_FIXED_ARRAY_SUB_TYPE + sub_type] += size;
verwaest@chromium.org753aee42012-07-17 16:15:42 +00001804 }
1805 }
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001806 }
1807
1808 void CheckpointObjectStats();
1809
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001810 // We don't use a ScopedLock here since we want to lock the heap
1811 // only when FLAG_parallel_recompilation is true.
1812 class RelocationLock {
1813 public:
1814 explicit RelocationLock(Heap* heap) : heap_(heap) {
1815 if (FLAG_parallel_recompilation) {
1816 heap_->relocation_mutex_->Lock();
1817 }
1818 }
1819 ~RelocationLock() {
1820 if (FLAG_parallel_recompilation) {
1821 heap_->relocation_mutex_->Unlock();
1822 }
1823 }
1824
1825 private:
1826 Heap* heap_;
1827 };
1828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001830 Heap();
1831
1832 // This can be calculated directly from a pointer to the heap; however, it is
1833 // more expedient to get at the isolate directly from within Heap methods.
1834 Isolate* isolate_;
1835
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001836 Object* roots_[kRootListLength];
1837
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001838 intptr_t code_range_size_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001839 int reserved_semispace_size_;
1840 int max_semispace_size_;
1841 int initial_semispace_size_;
1842 intptr_t max_old_generation_size_;
1843 intptr_t max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001845 // For keeping track of how much data has survived
1846 // scavenge since last new space expansion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001847 int survived_since_last_expansion_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001849 // For keeping track on when to flush RegExp code.
1850 int sweep_generation_;
1851
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001852 int always_allocate_scope_depth_;
1853 int linear_allocation_scope_depth_;
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00001854
1855 // For keeping track of context disposals.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001856 int contexts_disposed_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001857
danno@chromium.org88aa0582012-03-23 15:11:57 +00001858 int global_ic_age_;
1859
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +00001860 bool flush_monomorphic_ics_;
1861
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001862 int scan_on_scavenge_pages_;
1863
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001864#if defined(V8_TARGET_ARCH_X64)
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001865 static const int kMaxObjectSizeInNewSpace = 1024*KB;
lrn@chromium.org8541d772010-12-15 12:05:09 +00001866#else
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001867 static const int kMaxObjectSizeInNewSpace = 512*KB;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001868#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001869
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001870 NewSpace new_space_;
1871 OldSpace* old_pointer_space_;
1872 OldSpace* old_data_space_;
1873 OldSpace* code_space_;
1874 MapSpace* map_space_;
1875 CellSpace* cell_space_;
1876 LargeObjectSpace* lo_space_;
1877 HeapState gc_state_;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001878 int gc_post_processing_depth_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
kasper.lund7276f142008-07-30 08:49:36 +00001880 // Returns the amount of external memory registered since last global gc.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001881 intptr_t PromotedExternalMemorySize();
kasper.lund7276f142008-07-30 08:49:36 +00001882
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +00001883 unsigned int ms_count_; // how many mark-sweep collections happened
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001884 unsigned int gc_count_; // how many gc happened
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00001886 // For post mortem debugging.
1887 static const int kRememberedUnmappedPages = 128;
1888 int remembered_unmapped_pages_index_;
1889 Address remembered_unmapped_pages_[kRememberedUnmappedPages];
1890
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001891 // Total length of the strings we failed to flatten since the last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001892 int unflattened_strings_length_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001893
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001894#define ROOT_ACCESSOR(type, name, camel_name) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001895 inline void set_##name(type* value) { \
1896 /* The deserializer makes use of the fact that these common roots are */ \
1897 /* never in new space and never on a page that is being compacted. */ \
1898 ASSERT(k##camel_name##RootIndex >= kOldSpaceRoots || !InNewSpace(value)); \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001899 roots_[k##camel_name##RootIndex] = value; \
1900 }
1901 ROOT_LIST(ROOT_ACCESSOR)
1902#undef ROOT_ACCESSOR
1903
kasper.lund7276f142008-07-30 08:49:36 +00001904#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001905 bool allocation_allowed_;
kasper.lund7276f142008-07-30 08:49:36 +00001906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 // If the --gc-interval flag is set to a positive value, this
1908 // variable holds the value indicating the number of allocations
1909 // remain until the next failure and garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001910 int allocation_timeout_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911
1912 // Do we expect to be able to handle allocation failure at this
1913 // time?
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001914 bool disallow_allocation_failure_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915#endif // DEBUG
1916
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001917 // Indicates that the new space should be kept small due to high promotion
1918 // rates caused by the mutator allocating a lot of long-lived objects.
1919 bool new_space_high_promotion_mode_active_;
1920
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001921 // Limit that triggers a global GC on the next (normally caused) GC. This
1922 // is checked when we have already decided to do a GC to help determine
1923 // which collector to invoke.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001924 intptr_t old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001925
1926 // Limit that triggers a global GC as soon as is reasonable. This is
1927 // checked before expanding a paged space in the old generation and on
1928 // every allocation in large object space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001929 intptr_t old_gen_allocation_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001931 // Sometimes the heuristics dictate that those limits are increased. This
1932 // variable records that fact.
1933 int old_gen_limit_factor_;
1934
1935 // Used to adjust the limits that control the timing of the next GC.
1936 intptr_t size_of_old_gen_at_last_old_space_gc_;
1937
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001938 // Limit on the amount of externally allocated memory allowed
1939 // between global GCs. If reached a global GC is forced.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001940 intptr_t external_allocation_limit_;
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001941
kasper.lund7276f142008-07-30 08:49:36 +00001942 // The amount of external memory registered through the API kept alive
1943 // by global handles
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001944 intptr_t amount_of_external_allocated_memory_;
kasper.lund7276f142008-07-30 08:49:36 +00001945
1946 // Caches the amount of external memory registered at the last global gc.
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00001947 intptr_t amount_of_external_allocated_memory_at_last_global_gc_;
kasper.lund7276f142008-07-30 08:49:36 +00001948
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949 // Indicates that an allocation has failed in the old generation since the
1950 // last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001951 int old_gen_exhausted_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001953 Object* native_contexts_list_;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001954
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001955 StoreBufferRebuilder store_buffer_rebuilder_;
1956
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001957 struct StringTypeTable {
1958 InstanceType type;
1959 int size;
1960 RootListIndex index;
1961 };
1962
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001963 struct ConstantStringTable {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001964 const char* contents;
1965 RootListIndex index;
1966 };
1967
1968 struct StructTable {
1969 InstanceType type;
1970 int size;
1971 RootListIndex index;
1972 };
1973
1974 static const StringTypeTable string_type_table[];
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001975 static const ConstantStringTable constant_string_table[];
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001976 static const StructTable struct_table[];
1977
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001978 // The special hidden string which is an empty string, but does not match
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001979 // any string when looked up in properties.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001980 String* hidden_string_;
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001981
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001982 // GC callback function, called before and after mark-compact GC.
1983 // Allocations in the callback function are disallowed.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001984 struct GCPrologueCallbackPair {
1985 GCPrologueCallbackPair(GCPrologueCallback callback, GCType gc_type)
1986 : callback(callback), gc_type(gc_type) {
1987 }
1988 bool operator==(const GCPrologueCallbackPair& pair) const {
1989 return pair.callback == callback;
1990 }
1991 GCPrologueCallback callback;
1992 GCType gc_type;
1993 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001994 List<GCPrologueCallbackPair> gc_prologue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001995
1996 struct GCEpilogueCallbackPair {
1997 GCEpilogueCallbackPair(GCEpilogueCallback callback, GCType gc_type)
1998 : callback(callback), gc_type(gc_type) {
1999 }
2000 bool operator==(const GCEpilogueCallbackPair& pair) const {
2001 return pair.callback == callback;
2002 }
2003 GCEpilogueCallback callback;
2004 GCType gc_type;
2005 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002006 List<GCEpilogueCallbackPair> gc_epilogue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002007
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002008 GCCallback global_gc_prologue_callback_;
2009 GCCallback global_gc_epilogue_callback_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002011 // Support for computing object sizes during GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002012 HeapObjectCallback gc_safe_size_of_old_object_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002013 static int GcSafeSizeOfOldObject(HeapObject* object);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002014
2015 // Update the GC state. Called from the mark-compact collector.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002016 void MarkMapPointersAsEncoded(bool encoded) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002017 ASSERT(!encoded);
2018 gc_safe_size_of_old_object_ = &GcSafeSizeOfOldObject;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002019 }
2020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 // Checks whether a global GC is necessary
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002022 GarbageCollector SelectGarbageCollector(AllocationSpace space,
2023 const char** reason);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024
2025 // Performs garbage collection
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00002026 // Returns whether there is a chance another major GC could
2027 // collect more garbage.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002028 bool PerformGarbageCollection(GarbageCollector collector,
2029 GCTracer* tracer);
2030
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002031 inline void UpdateOldSpaceLimits();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002033 // Allocate an uninitialized object in map space. The behavior is identical
2034 // to Heap::AllocateRaw(size_in_bytes, MAP_SPACE), except that (a) it doesn't
2035 // have to test the allocation space argument and (b) can reduce code size
2036 // (since both AllocateRaw and AllocateRawMap are inlined).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002037 MUST_USE_RESULT inline MaybeObject* AllocateRawMap();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002038
2039 // Allocate an uninitialized object in the global property cell space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002040 MUST_USE_RESULT inline MaybeObject* AllocateRawCell();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 // Initializes a JSObject based on its map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002043 void InitializeJSObjectFromMap(JSObject* obj,
2044 FixedArray* properties,
2045 Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002047 bool CreateInitialMaps();
2048 bool CreateInitialObjects();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002049
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002050 // These five Create*EntryStub functions are here and forced to not be inlined
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +00002051 // because of a gcc-4.4 bug that assigns wrong vtable entries.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002052 NO_INLINE(void CreateJSEntryStub());
2053 NO_INLINE(void CreateJSConstructEntryStub());
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002054
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002055 void CreateFixedStubs();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002056
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +00002057 MUST_USE_RESULT MaybeObject* CreateOddball(const char* to_string,
2058 Object* to_number,
2059 byte kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002061 // Allocate a JSArray with no elements
2062 MUST_USE_RESULT MaybeObject* AllocateJSArray(
2063 ElementsKind elements_kind,
2064 PretenureFlag pretenure = NOT_TENURED);
2065
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002066 MUST_USE_RESULT MaybeObject* AllocateJSArrayWithAllocationSite(
2067 ElementsKind elements_kind,
2068 Handle<Object> allocation_site_info_payload);
2069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 // Allocate empty fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002071 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedArray();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002073 // Allocate empty fixed double array.
2074 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedDoubleArray();
2075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 // Performs a minor collection in new generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002077 void Scavenge();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002078
2079 static String* UpdateNewSpaceReferenceInExternalStringTableEntry(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002080 Heap* heap,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00002081 Object** pointer);
2082
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002083 Address DoScavenge(ObjectVisitor* scavenge_visitor, Address new_space_front);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002084 static void ScavengeStoreBufferCallback(Heap* heap,
2085 MemoryChunk* page,
2086 StoreBufferEvent event);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087
2088 // Performs a major collection in the whole heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002089 void MarkCompact(GCTracer* tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090
2091 // Code to be run before and after mark-compact.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002092 void MarkCompactPrologue();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 // Record statistics before and after garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002095 void ReportStatisticsBeforeGC();
2096 void ReportStatisticsAfterGC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002098 // Slow part of scavenge object.
2099 static void ScavengeObjectSlow(HeapObject** p, HeapObject* object);
2100
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002101 // Initializes a function with a shared part and prototype.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002102 // Note: this code was factored out of AllocateFunction such that
2103 // other parts of the VM could use it. Specifically, a function that creates
2104 // instances of type JS_FUNCTION_TYPE benefit from the use of this function.
2105 // Please note this does not perform a garbage collection.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002106 inline void InitializeFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002107 JSFunction* function,
2108 SharedFunctionInfo* shared,
2109 Object* prototype);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002110
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002111 // Total RegExp code ever generated
2112 double total_regexp_code_generated_;
2113
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002114 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002115
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002116 // Allocates a small number to string cache.
2117 MUST_USE_RESULT MaybeObject* AllocateInitialNumberStringCache();
2118 // Creates and installs the full-sized number string cache.
2119 void AllocateFullSizeNumberStringCache();
2120 // Get the length of the number to string cache based on the max semispace
2121 // size.
2122 int FullSizeNumberStringCacheLength();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002123 // Flush the number to string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002124 void FlushNumberStringCache();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002125
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002126 void UpdateSurvivalRateTrend(int start_new_space_size);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002127
2128 enum SurvivalRateTrend { INCREASING, STABLE, DECREASING, FLUCTUATING };
2129
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002130 static const int kYoungSurvivalRateHighThreshold = 90;
2131 static const int kYoungSurvivalRateLowThreshold = 10;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002132 static const int kYoungSurvivalRateAllowedDeviation = 15;
2133
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002134 int young_survivors_after_last_gc_;
2135 int high_survival_rate_period_length_;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002136 int low_survival_rate_period_length_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002137 double survival_rate_;
2138 SurvivalRateTrend previous_survival_rate_trend_;
2139 SurvivalRateTrend survival_rate_trend_;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002140
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002141 void set_survival_rate_trend(SurvivalRateTrend survival_rate_trend) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002142 ASSERT(survival_rate_trend != FLUCTUATING);
2143 previous_survival_rate_trend_ = survival_rate_trend_;
2144 survival_rate_trend_ = survival_rate_trend;
2145 }
2146
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002147 SurvivalRateTrend survival_rate_trend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002148 if (survival_rate_trend_ == STABLE) {
2149 return STABLE;
2150 } else if (previous_survival_rate_trend_ == STABLE) {
2151 return survival_rate_trend_;
2152 } else if (survival_rate_trend_ != previous_survival_rate_trend_) {
2153 return FLUCTUATING;
2154 } else {
2155 return survival_rate_trend_;
2156 }
2157 }
2158
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002159 bool IsStableOrIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002160 switch (survival_rate_trend()) {
2161 case STABLE:
2162 case INCREASING:
2163 return true;
2164 default:
2165 return false;
2166 }
2167 }
2168
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002169 bool IsStableOrDecreasingSurvivalTrend() {
2170 switch (survival_rate_trend()) {
2171 case STABLE:
2172 case DECREASING:
2173 return true;
2174 default:
2175 return false;
2176 }
2177 }
2178
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002179 bool IsIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002180 return survival_rate_trend() == INCREASING;
2181 }
2182
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002183 bool IsHighSurvivalRate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00002184 return high_survival_rate_period_length_ > 0;
2185 }
2186
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002187 bool IsLowSurvivalRate() {
2188 return low_survival_rate_period_length_ > 0;
2189 }
2190
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002191 void SelectScavengingVisitorsTable();
2192
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002193 void StartIdleRound() {
2194 mark_sweeps_since_idle_round_started_ = 0;
2195 ms_count_at_last_idle_notification_ = ms_count_;
2196 }
2197
2198 void FinishIdleRound() {
2199 mark_sweeps_since_idle_round_started_ = kMaxMarkSweepsInIdleRound;
2200 scavenges_since_last_idle_round_ = 0;
2201 }
2202
2203 bool EnoughGarbageSinceLastIdleRound() {
2204 return (scavenges_since_last_idle_round_ >= kIdleScavengeThreshold);
2205 }
2206
ulan@chromium.org6ff65142012-03-21 09:52:17 +00002207 // Estimates how many milliseconds a Mark-Sweep would take to complete.
2208 // In idle notification handler we assume that this function will return:
2209 // - a number less than 10 for small heaps, which are less than 8Mb.
2210 // - a number greater than 10 for large heaps, which are greater than 32Mb.
2211 int TimeMarkSweepWouldTakeInMs() {
2212 // Rough estimate of how many megabytes of heap can be processed in 1 ms.
2213 static const int kMbPerMs = 2;
2214
2215 int heap_size_mb = static_cast<int>(SizeOfObjects() / MB);
2216 return heap_size_mb / kMbPerMs;
2217 }
2218
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002219 // Returns true if no more GC work is left.
2220 bool IdleGlobalGC();
2221
ulan@chromium.org6ff65142012-03-21 09:52:17 +00002222 void AdvanceIdleIncrementalMarking(intptr_t step_size);
2223
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00002224 void ClearObjectStats(bool clear_last_time_stats = false);
ulan@chromium.org6ff65142012-03-21 09:52:17 +00002225
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002226 static const int kInitialStringTableSize = 2048;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002227 static const int kInitialEvalCacheSize = 64;
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002228 static const int kInitialNumberStringCacheSize = 256;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00002230 // Object counts and used memory by InstanceType
verwaest@chromium.org753aee42012-07-17 16:15:42 +00002231 size_t object_counts_[OBJECT_STATS_COUNT];
2232 size_t object_counts_last_time_[OBJECT_STATS_COUNT];
2233 size_t object_sizes_[OBJECT_STATS_COUNT];
2234 size_t object_sizes_last_time_[OBJECT_STATS_COUNT];
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00002235
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002236 // Maximum GC pause.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002237 double max_gc_pause_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002238
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002239 // Total time spent in GC.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002240 double total_gc_time_ms_;
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002241
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002242 // Maximum size of objects alive after GC.
2243 intptr_t max_alive_after_gc_;
2244
2245 // Minimal interval between two subsequent collections.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002246 double min_in_mutator_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002247
2248 // Size of objects alive after last GC.
2249 intptr_t alive_after_last_gc_;
2250
2251 double last_gc_end_timestamp_;
2252
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002253 // Cumulative GC time spent in marking
2254 double marking_time_;
2255
2256 // Cumulative GC time spent in sweeping
2257 double sweeping_time_;
2258
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002259 MarkCompactCollector mark_compact_collector_;
2260
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002261 StoreBuffer store_buffer_;
2262
2263 Marking marking_;
2264
2265 IncrementalMarking incremental_marking_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002266
2267 int number_idle_notifications_;
2268 unsigned int last_idle_notification_gc_count_;
2269 bool last_idle_notification_gc_count_init_;
2270
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002271 int mark_sweeps_since_idle_round_started_;
2272 int ms_count_at_last_idle_notification_;
2273 unsigned int gc_count_at_last_idle_gc_;
2274 int scavenges_since_last_idle_round_;
2275
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00002276#ifdef VERIFY_HEAP
2277 int no_weak_embedded_maps_verification_scope_depth_;
2278#endif
2279
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00002280 static const int kMaxMarkSweepsInIdleRound = 7;
2281 static const int kIdleScavengeThreshold = 5;
2282
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002283 // Shared state read by the scavenge collector and set by ScavengeObject.
2284 PromotionQueue promotion_queue_;
2285
2286 // Flag is set when the heap has been configured. The heap can be repeatedly
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002287 // configured through the API until it is set up.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002288 bool configured_;
2289
2290 ExternalStringTable external_string_table_;
2291
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002292 ErrorObjectList error_object_list_;
2293
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002294 VisitorDispatchTable<ScavengingCallback> scavenging_visitors_table_;
2295
2296 MemoryChunk* chunks_queued_for_free_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002297
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002298 Mutex* relocation_mutex_;
2299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 friend class Factory;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002301 friend class GCTracer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 friend class DisallowAllocationFailure;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002303 friend class AlwaysAllocateScope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002304 friend class Page;
2305 friend class Isolate;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002306 friend class MarkCompactCollector;
verwaest@chromium.orgb6d052d2012-07-27 08:03:27 +00002307 friend class MarkCompactMarkingVisitor;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002308 friend class MapCompact;
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00002309#ifdef VERIFY_HEAP
2310 friend class NoWeakEmbeddedMapsVerificationScope;
2311#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002312
2313 DISALLOW_COPY_AND_ASSIGN(Heap);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002314};
2315
2316
ager@chromium.org67368652009-12-03 11:40:49 +00002317class HeapStats {
2318 public:
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002319 static const int kStartMarker = 0xDECADE00;
2320 static const int kEndMarker = 0xDECADE01;
2321
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002322 int* start_marker; // 0
2323 int* new_space_size; // 1
2324 int* new_space_capacity; // 2
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002325 intptr_t* old_pointer_space_size; // 3
2326 intptr_t* old_pointer_space_capacity; // 4
2327 intptr_t* old_data_space_size; // 5
2328 intptr_t* old_data_space_capacity; // 6
2329 intptr_t* code_space_size; // 7
2330 intptr_t* code_space_capacity; // 8
2331 intptr_t* map_space_size; // 9
2332 intptr_t* map_space_capacity; // 10
2333 intptr_t* cell_space_size; // 11
2334 intptr_t* cell_space_capacity; // 12
2335 intptr_t* lo_space_size; // 13
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002336 int* global_handle_count; // 14
2337 int* weak_global_handle_count; // 15
2338 int* pending_global_handle_count; // 16
2339 int* near_death_global_handle_count; // 17
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002340 int* free_global_handle_count; // 18
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002341 intptr_t* memory_allocator_size; // 19
2342 intptr_t* memory_allocator_capacity; // 20
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002343 int* objects_per_type; // 21
2344 int* size_per_type; // 22
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002345 int* os_error; // 23
2346 int* end_marker; // 24
ager@chromium.org60121232009-12-03 11:25:37 +00002347};
2348
2349
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002350class DisallowAllocationFailure {
2351 public:
2352 inline DisallowAllocationFailure();
2353 inline ~DisallowAllocationFailure();
2354
2355#ifdef DEBUG
2356 private:
2357 bool old_state_;
2358#endif
2359};
2360
2361
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002362class AlwaysAllocateScope {
2363 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002364 inline AlwaysAllocateScope();
2365 inline ~AlwaysAllocateScope();
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002366
2367 private:
2368 // Implicitly disable artificial allocation failures.
2369 DisallowAllocationFailure disallow_allocation_failure_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370};
2371
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00002372#ifdef VERIFY_HEAP
2373class NoWeakEmbeddedMapsVerificationScope {
2374 public:
2375 inline NoWeakEmbeddedMapsVerificationScope();
2376 inline ~NoWeakEmbeddedMapsVerificationScope();
2377};
2378#endif
2379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002380
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002381// Visitor class to verify interior pointers in spaces that do not contain
2382// or care about intergenerational references. All heap object pointers have to
2383// point into the heap to a location that has a map pointer at its first word.
2384// Caveat: Heap::Contains is an approximation because it can return true for
2385// objects in a heap space but above the allocation pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386class VerifyPointersVisitor: public ObjectVisitor {
2387 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002388 inline void VisitPointers(Object** start, Object** end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390
2391
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002392// Space iterator for iterating over all spaces of the heap. Returns each space
2393// in turn, and null when it is done.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002394class AllSpaces BASE_EMBEDDED {
2395 public:
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002396 explicit AllSpaces(Heap* heap) : heap_(heap), counter_(FIRST_SPACE) {}
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002397 Space* next();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002398 private:
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002399 Heap* heap_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002400 int counter_;
2401};
2402
2403
2404// Space iterator for iterating over all old spaces of the heap: Old pointer
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002405// space, old data space and code space. Returns each space in turn, and null
2406// when it is done.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002407class OldSpaces BASE_EMBEDDED {
2408 public:
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002409 explicit OldSpaces(Heap* heap) : heap_(heap), counter_(OLD_POINTER_SPACE) {}
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002410 OldSpace* next();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002411 private:
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002412 Heap* heap_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002413 int counter_;
2414};
2415
2416
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002417// Space iterator for iterating over all the paged spaces of the heap: Map
2418// space, old pointer space, old data space, code space and cell space. Returns
2419// each space in turn, and null when it is done.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002420class PagedSpaces BASE_EMBEDDED {
2421 public:
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002422 explicit PagedSpaces(Heap* heap) : heap_(heap), counter_(OLD_POINTER_SPACE) {}
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002423 PagedSpace* next();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002424 private:
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002425 Heap* heap_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002426 int counter_;
2427};
2428
2429
2430// Space iterator for iterating over all spaces of the heap.
kasper.lund7276f142008-07-30 08:49:36 +00002431// For each space an object iterator is provided. The deallocation of the
2432// returned object iterators is handled by the space iterator.
kasper.lund7276f142008-07-30 08:49:36 +00002433class SpaceIterator : public Malloced {
2434 public:
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002435 explicit SpaceIterator(Heap* heap);
2436 SpaceIterator(Heap* heap, HeapObjectCallback size_func);
kasper.lund7276f142008-07-30 08:49:36 +00002437 virtual ~SpaceIterator();
2438
2439 bool has_next();
2440 ObjectIterator* next();
2441
2442 private:
2443 ObjectIterator* CreateIterator();
2444
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002445 Heap* heap_;
kasper.lund7276f142008-07-30 08:49:36 +00002446 int current_space_; // from enum AllocationSpace.
2447 ObjectIterator* iterator_; // object iterator for the current space.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002448 HeapObjectCallback size_func_;
kasper.lund7276f142008-07-30 08:49:36 +00002449};
2450
2451
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002452// A HeapIterator provides iteration over the whole heap. It
2453// aggregates the specific iterators for the different spaces as
2454// these can only iterate over one space only.
2455//
2456// HeapIterator can skip free list nodes (that is, de-allocated heap
2457// objects that still remain in the heap). As implementation of free
2458// nodes filtering uses GC marks, it can't be used during MS/MC GC
2459// phases. Also, it is forbidden to interrupt iteration in this mode,
2460// as this will leave heap objects marked (and thus, unusable).
whesse@chromium.org023421e2010-12-21 12:19:12 +00002461class HeapObjectsFilter;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462
2463class HeapIterator BASE_EMBEDDED {
2464 public:
whesse@chromium.org023421e2010-12-21 12:19:12 +00002465 enum HeapObjectsFiltering {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002466 kNoFiltering,
whesse@chromium.org023421e2010-12-21 12:19:12 +00002467 kFilterUnreachable
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002468 };
2469
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002470 explicit HeapIterator(Heap* heap);
2471 HeapIterator(Heap* heap, HeapObjectsFiltering filtering);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002472 ~HeapIterator();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474 HeapObject* next();
2475 void reset();
2476
2477 private:
2478 // Perform the initialization.
2479 void Init();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 // Perform all necessary shutdown (destruction) work.
2481 void Shutdown();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002482 HeapObject* NextObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002483
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00002484 Heap* heap_;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002485 HeapObjectsFiltering filtering_;
2486 HeapObjectsFilter* filter_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 // Space iterator for iterating all the spaces.
2488 SpaceIterator* space_iterator_;
2489 // Object iterator for the space currently being iterated.
2490 ObjectIterator* object_iterator_;
2491};
2492
2493
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002494// Cache for mapping (map, property name) into field offset.
2495// Cleared at startup and prior to mark sweep collection.
2496class KeyedLookupCache {
2497 public:
2498 // Lookup field offset for (map, name). If absent, -1 is returned.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002499 int Lookup(Map* map, String* name);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002500
2501 // Update an element in the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002502 void Update(Map* map, String* name, int field_offset);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002503
2504 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002505 void Clear();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002506
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002507 static const int kLength = 256;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002508 static const int kCapacityMask = kLength - 1;
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00002509 static const int kMapHashShift = 5;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002510 static const int kHashMask = -4; // Zero the last two bits.
2511 static const int kEntriesPerBucket = 4;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002512 static const int kNotFound = -1;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002513
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002514 // kEntriesPerBucket should be a power of 2.
2515 STATIC_ASSERT((kEntriesPerBucket & (kEntriesPerBucket - 1)) == 0);
2516 STATIC_ASSERT(kEntriesPerBucket == -kHashMask);
2517
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002518 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002519 KeyedLookupCache() {
2520 for (int i = 0; i < kLength; ++i) {
2521 keys_[i].map = NULL;
2522 keys_[i].name = NULL;
2523 field_offsets_[i] = kNotFound;
2524 }
2525 }
2526
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002527 static inline int Hash(Map* map, String* name);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002528
2529 // Get the address of the keys and field_offsets arrays. Used in
2530 // generated code to perform cache lookups.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002531 Address keys_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002532 return reinterpret_cast<Address>(&keys_);
2533 }
2534
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002535 Address field_offsets_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002536 return reinterpret_cast<Address>(&field_offsets_);
2537 }
2538
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002539 struct Key {
2540 Map* map;
2541 String* name;
2542 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002543
2544 Key keys_[kLength];
2545 int field_offsets_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002546
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002547 friend class ExternalReference;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002548 friend class Isolate;
2549 DISALLOW_COPY_AND_ASSIGN(KeyedLookupCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002550};
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002551
2552
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002553// Cache for mapping (map, property name) into descriptor index.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002554// The cache contains both positive and negative results.
2555// Descriptor index equals kNotFound means the property is absent.
2556// Cleared at startup and prior to any gc.
2557class DescriptorLookupCache {
2558 public:
2559 // Lookup descriptor index for (map, name).
2560 // If absent, kAbsent is returned.
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002561 int Lookup(Map* source, String* name) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002562 if (!StringShape(name).IsInternalized()) return kAbsent;
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002563 int index = Hash(source, name);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002564 Key& key = keys_[index];
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002565 if ((key.source == source) && (key.name == name)) return results_[index];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002566 return kAbsent;
2567 }
2568
2569 // Update an element in the cache.
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002570 void Update(Map* source, String* name, int result) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002571 ASSERT(result != kAbsent);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002572 if (StringShape(name).IsInternalized()) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002573 int index = Hash(source, name);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002574 Key& key = keys_[index];
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002575 key.source = source;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002576 key.name = name;
2577 results_[index] = result;
2578 }
2579 }
2580
2581 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002582 void Clear();
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002583
2584 static const int kAbsent = -2;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002585
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002586 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002587 DescriptorLookupCache() {
2588 for (int i = 0; i < kLength; ++i) {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002589 keys_[i].source = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002590 keys_[i].name = NULL;
2591 results_[i] = kAbsent;
2592 }
2593 }
2594
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002595 static int Hash(Object* source, String* name) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002596 // Uses only lower 32 bits if pointers are larger.
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002597 uint32_t source_hash =
2598 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(source))
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002599 >> kPointerSizeLog2;
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002600 uint32_t name_hash =
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +00002601 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name))
2602 >> kPointerSizeLog2;
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002603 return (source_hash ^ name_hash) % kLength;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002604 }
2605
2606 static const int kLength = 64;
2607 struct Key {
verwaest@chromium.org06ab2ec2012-10-09 17:00:13 +00002608 Map* source;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002609 String* name;
2610 };
2611
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002612 Key keys_[kLength];
2613 int results_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002614
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002615 friend class Isolate;
2616 DISALLOW_COPY_AND_ASSIGN(DescriptorLookupCache);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002617};
2618
2619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620// A helper class to document/test C++ scopes where we do not
2621// expect a GC. Usage:
2622//
2623// /* Allocation not allowed: we cannot handle a GC in this scope. */
2624// { AssertNoAllocation nogc;
2625// ...
2626// }
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002627class AssertNoAllocation {
2628 public:
2629 inline AssertNoAllocation();
2630 inline ~AssertNoAllocation();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631
2632#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 private:
2634 bool old_state_;
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002635 bool active_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636#endif
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002637};
2638
2639
2640class DisableAssertNoAllocation {
2641 public:
2642 inline DisableAssertNoAllocation();
2643 inline ~DisableAssertNoAllocation();
2644
2645#ifdef DEBUG
2646 private:
2647 bool old_state_;
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002648 bool active_;
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002649#endif
2650};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651
kasper.lund7276f142008-07-30 08:49:36 +00002652// GCTracer collects and prints ONE line after each garbage collector
2653// invocation IFF --trace_gc is used.
2654
2655class GCTracer BASE_EMBEDDED {
2656 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002657 class Scope BASE_EMBEDDED {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002658 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002659 enum ScopeId {
2660 EXTERNAL,
2661 MC_MARK,
2662 MC_SWEEP,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002663 MC_SWEEP_NEWSPACE,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002664 MC_EVACUATE_PAGES,
2665 MC_UPDATE_NEW_TO_NEW_POINTERS,
2666 MC_UPDATE_ROOT_TO_NEW_POINTERS,
2667 MC_UPDATE_OLD_TO_NEW_POINTERS,
2668 MC_UPDATE_POINTERS_TO_EVACUATED,
2669 MC_UPDATE_POINTERS_BETWEEN_EVACUATED,
2670 MC_UPDATE_MISC_POINTERS,
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00002671 MC_FLUSH_CODE,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002672 kNumberOfScopes
2673 };
2674
2675 Scope(GCTracer* tracer, ScopeId scope)
2676 : tracer_(tracer),
2677 scope_(scope) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002678 start_time_ = OS::TimeCurrentMillis();
2679 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002680
2681 ~Scope() {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002682 ASSERT(scope_ < kNumberOfScopes); // scope_ is unsigned.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002683 tracer_->scopes_[scope_] += OS::TimeCurrentMillis() - start_time_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002684 }
kasper.lund7276f142008-07-30 08:49:36 +00002685
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002686 private:
2687 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002688 ScopeId scope_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002689 double start_time_;
2690 };
2691
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002692 explicit GCTracer(Heap* heap,
2693 const char* gc_reason,
2694 const char* collector_reason);
kasper.lund7276f142008-07-30 08:49:36 +00002695 ~GCTracer();
2696
2697 // Sets the collector.
2698 void set_collector(GarbageCollector collector) { collector_ = collector; }
2699
2700 // Sets the GC count.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002701 void set_gc_count(unsigned int count) { gc_count_ = count; }
kasper.lund7276f142008-07-30 08:49:36 +00002702
2703 // Sets the full GC count.
2704 void set_full_gc_count(int count) { full_gc_count_ = count; }
2705
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002706 void increment_promoted_objects_size(int object_size) {
2707 promoted_objects_size_ += object_size;
2708 }
2709
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00002710 void increment_nodes_died_in_new_space() {
2711 nodes_died_in_new_space_++;
2712 }
2713
2714 void increment_nodes_copied_in_new_space() {
2715 nodes_copied_in_new_space_++;
2716 }
2717
2718 void increment_nodes_promoted() {
2719 nodes_promoted_++;
2720 }
2721
kasper.lund7276f142008-07-30 08:49:36 +00002722 private:
2723 // Returns a string matching the collector.
2724 const char* CollectorString();
2725
2726 // Returns size of object in heap (in MB).
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002727 inline double SizeOfHeapObjects();
kasper.lund7276f142008-07-30 08:49:36 +00002728
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00002729 // Timestamp set in the constructor.
2730 double start_time_;
2731
2732 // Size of objects in heap set in constructor.
2733 intptr_t start_object_size_;
2734
2735 // Size of memory allocated from OS set in constructor.
2736 intptr_t start_memory_size_;
2737
2738 // Type of collector.
2739 GarbageCollector collector_;
kasper.lund7276f142008-07-30 08:49:36 +00002740
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002741 // A count (including this one, e.g. the first collection is 1) of the
kasper.lund7276f142008-07-30 08:49:36 +00002742 // number of garbage collections.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002743 unsigned int gc_count_;
kasper.lund7276f142008-07-30 08:49:36 +00002744
2745 // A count (including this one) of the number of full garbage collections.
2746 int full_gc_count_;
2747
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002748 // Amounts of time spent in different scopes during GC.
2749 double scopes_[Scope::kNumberOfScopes];
2750
2751 // Total amount of space either wasted or contained in one of free lists
2752 // before the current GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002753 intptr_t in_free_list_or_wasted_before_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002754
2755 // Difference between space used in the heap at the beginning of the current
2756 // collection and the end of the previous collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002757 intptr_t allocated_since_last_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002758
2759 // Amount of time spent in mutator that is time elapsed between end of the
2760 // previous collection and the beginning of the current one.
2761 double spent_in_mutator_;
2762
2763 // Size of objects promoted during the current collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002764 intptr_t promoted_objects_size_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002765
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +00002766 // Number of died nodes in the new space.
2767 int nodes_died_in_new_space_;
2768
2769 // Number of copied nodes to the new space.
2770 int nodes_copied_in_new_space_;
2771
2772 // Number of promoted nodes to the old space.
2773 int nodes_promoted_;
2774
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002775 // Incremental marking steps counters.
2776 int steps_count_;
2777 double steps_took_;
2778 double longest_step_;
2779 int steps_count_since_last_gc_;
2780 double steps_took_since_last_gc_;
2781
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002782 Heap* heap_;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002783
2784 const char* gc_reason_;
2785 const char* collector_reason_;
kasper.lund7276f142008-07-30 08:49:36 +00002786};
2787
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002788
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002789class RegExpResultsCache {
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002790 public:
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002791 enum ResultsCacheType { REGEXP_MULTIPLE_INDICES, STRING_SPLIT_SUBSTRINGS };
2792
2793 // Attempt to retrieve a cached result. On failure, 0 is returned as a Smi.
2794 // On success, the returned result is guaranteed to be a COW-array.
2795 static Object* Lookup(Heap* heap,
2796 String* key_string,
2797 Object* key_pattern,
2798 ResultsCacheType type);
2799 // Attempt to add value_array to the cache specified by type. On success,
2800 // value_array is turned into a COW-array.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002801 static void Enter(Heap* heap,
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002802 String* key_string,
2803 Object* key_pattern,
2804 FixedArray* value_array,
2805 ResultsCacheType type);
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002806 static void Clear(FixedArray* cache);
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00002807 static const int kRegExpResultsCacheSize = 0x100;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002808
2809 private:
2810 static const int kArrayEntriesPerCacheEntry = 4;
2811 static const int kStringOffset = 0;
2812 static const int kPatternOffset = 1;
2813 static const int kArrayOffset = 2;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002814};
2815
2816
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002817class TranscendentalCache {
2818 public:
2819 enum Type {ACOS, ASIN, ATAN, COS, EXP, LOG, SIN, TAN, kNumberOfCaches};
whesse@chromium.org023421e2010-12-21 12:19:12 +00002820 static const int kTranscendentalTypeBits = 3;
2821 STATIC_ASSERT((1 << kTranscendentalTypeBits) >= kNumberOfCaches);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002822
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002823 // Returns a heap number with f(input), where f is a math function specified
2824 // by the 'type' argument.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002825 MUST_USE_RESULT inline MaybeObject* Get(Type type, double input);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002826
2827 // The cache contains raw Object pointers. This method disposes of
2828 // them before a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002829 void Clear();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002830
2831 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002832 class SubCache {
2833 static const int kCacheSize = 512;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002834
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002835 explicit SubCache(Type t);
2836
2837 MUST_USE_RESULT inline MaybeObject* Get(double input);
2838
2839 inline double Calculate(double input);
2840
2841 struct Element {
2842 uint32_t in[2];
2843 Object* output;
2844 };
2845
2846 union Converter {
2847 double dbl;
2848 uint32_t integers[2];
2849 };
2850
2851 inline static int Hash(const Converter& c) {
2852 uint32_t hash = (c.integers[0] ^ c.integers[1]);
2853 hash ^= static_cast<int32_t>(hash) >> 16;
2854 hash ^= static_cast<int32_t>(hash) >> 8;
2855 return (hash & (kCacheSize - 1));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002856 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002857
2858 Element elements_[kCacheSize];
2859 Type type_;
2860 Isolate* isolate_;
2861
2862 // Allow access to the caches_ array as an ExternalReference.
2863 friend class ExternalReference;
2864 // Inline implementation of the cache.
2865 friend class TranscendentalCacheStub;
2866 // For evaluating value.
2867 friend class TranscendentalCache;
2868
2869 DISALLOW_COPY_AND_ASSIGN(SubCache);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002870 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002871
2872 TranscendentalCache() {
2873 for (int i = 0; i < kNumberOfCaches; ++i) caches_[i] = NULL;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002874 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002875
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002876 // Used to create an external reference.
2877 inline Address cache_array_address();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002878
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002879 // Instantiation
2880 friend class Isolate;
2881 // Inline implementation of the caching.
2882 friend class TranscendentalCacheStub;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002883 // Allow access to the caches_ array as an ExternalReference.
2884 friend class ExternalReference;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002885
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002886 SubCache* caches_[kNumberOfCaches];
2887 DISALLOW_COPY_AND_ASSIGN(TranscendentalCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002888};
2889
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002890
2891// Abstract base class for checking whether a weak object should be retained.
2892class WeakObjectRetainer {
2893 public:
2894 virtual ~WeakObjectRetainer() {}
2895
2896 // Return whether this object should be retained. If NULL is returned the
2897 // object has no references. Otherwise the address of the retained object
2898 // should be returned as in some GC situations the object has been moved.
2899 virtual Object* RetainAs(Object* object) = 0;
2900};
2901
2902
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002903// Intrusive object marking uses least significant bit of
2904// heap object's map word to mark objects.
2905// Normally all map words have least significant bit set
2906// because they contain tagged map pointer.
2907// If the bit is not set object is marked.
2908// All objects should be unmarked before resuming
2909// JavaScript execution.
2910class IntrusiveMarking {
2911 public:
2912 static bool IsMarked(HeapObject* object) {
2913 return (object->map_word().ToRawValue() & kNotMarkedBit) == 0;
2914 }
2915
2916 static void ClearMark(HeapObject* object) {
2917 uintptr_t map_word = object->map_word().ToRawValue();
2918 object->set_map_word(MapWord::FromRawValue(map_word | kNotMarkedBit));
2919 ASSERT(!IsMarked(object));
2920 }
2921
2922 static void SetMark(HeapObject* object) {
2923 uintptr_t map_word = object->map_word().ToRawValue();
2924 object->set_map_word(MapWord::FromRawValue(map_word & ~kNotMarkedBit));
2925 ASSERT(IsMarked(object));
2926 }
2927
2928 static Map* MapOfMarkedObject(HeapObject* object) {
2929 uintptr_t map_word = object->map_word().ToRawValue();
2930 return MapWord::FromRawValue(map_word | kNotMarkedBit).ToMap();
2931 }
2932
2933 static int SizeOfMarkedObject(HeapObject* object) {
2934 return object->SizeFromMap(MapOfMarkedObject(object));
2935 }
2936
2937 private:
2938 static const uintptr_t kNotMarkedBit = 0x1;
2939 STATIC_ASSERT((kHeapObjectTag & kNotMarkedBit) != 0);
2940};
2941
2942
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002943#ifdef DEBUG
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002944// Helper class for tracing paths to a search target Object from all roots.
2945// The TracePathFrom() method can be used to trace paths from a specific
2946// object to the search target object.
2947class PathTracer : public ObjectVisitor {
2948 public:
2949 enum WhatToFind {
2950 FIND_ALL, // Will find all matches.
2951 FIND_FIRST // Will stop the search after first match.
2952 };
2953
2954 // For the WhatToFind arg, if FIND_FIRST is specified, tracing will stop
2955 // after the first match. If FIND_ALL is specified, then tracing will be
2956 // done for all matches.
2957 PathTracer(Object* search_target,
2958 WhatToFind what_to_find,
2959 VisitMode visit_mode)
2960 : search_target_(search_target),
2961 found_target_(false),
2962 found_target_in_trace_(false),
2963 what_to_find_(what_to_find),
2964 visit_mode_(visit_mode),
2965 object_stack_(20),
2966 no_alloc() {}
2967
2968 virtual void VisitPointers(Object** start, Object** end);
2969
2970 void Reset();
2971 void TracePathFrom(Object** root);
2972
2973 bool found() const { return found_target_; }
2974
2975 static Object* const kAnyGlobalObject;
2976
2977 protected:
2978 class MarkVisitor;
2979 class UnmarkVisitor;
2980
2981 void MarkRecursively(Object** p, MarkVisitor* mark_visitor);
2982 void UnmarkRecursively(Object** p, UnmarkVisitor* unmark_visitor);
2983 virtual void ProcessResults();
2984
2985 // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject.
2986 static const int kMarkTag = 2;
2987
2988 Object* search_target_;
2989 bool found_target_;
2990 bool found_target_in_trace_;
2991 WhatToFind what_to_find_;
2992 VisitMode visit_mode_;
2993 List<Object*> object_stack_;
2994
2995 AssertNoAllocation no_alloc; // i.e. no gc allowed.
2996
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00002997 private:
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002998 DISALLOW_IMPLICIT_CONSTRUCTORS(PathTracer);
2999};
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00003000#endif // DEBUG
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003001
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002} } // namespace v8::internal
3003
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003004#endif // V8_HEAP_H_