blob: 32bf7a84b6f49124b29083654b75bb83884bae1e [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) \
64 V(Map, ascii_symbol_map, AsciiSymbolMap) \
65 V(Map, ascii_string_map, AsciiStringMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000066 V(Map, heap_number_map, HeapNumberMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000067 V(Map, global_context_map, GlobalContextMap) \
68 V(Map, fixed_array_map, FixedArrayMap) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000069 V(Map, code_map, CodeMap) \
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +000070 V(Map, scope_info_map, ScopeInfoMap) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +000071 V(Map, fixed_cow_array_map, FixedCOWArrayMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +000072 V(Map, fixed_double_array_map, FixedDoubleArrayMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000073 V(Object, no_interceptor_result_sentinel, NoInterceptorResultSentinel) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000074 V(Map, hash_table_map, HashTableMap) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000075 V(FixedArray, empty_fixed_array, EmptyFixedArray) \
76 V(ByteArray, empty_byte_array, EmptyByteArray) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000077 V(String, empty_string, EmptyString) \
78 V(DescriptorArray, empty_descriptor_array, EmptyDescriptorArray) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000079 V(Smi, stack_limit, StackLimit) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000080 V(Oddball, frame_alignment_marker, FrameAlignmentMarker) \
81 V(Oddball, arguments_marker, ArgumentsMarker) \
82 /* The first 32 roots above this line should be boring from a GC point of */ \
83 /* view. This means they are never in new space and never on a page that */ \
84 /* is being compacted. */ \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000085 V(FixedArray, number_string_cache, NumberStringCache) \
86 V(Object, instanceof_cache_function, InstanceofCacheFunction) \
87 V(Object, instanceof_cache_map, InstanceofCacheMap) \
88 V(Object, instanceof_cache_answer, InstanceofCacheAnswer) \
89 V(FixedArray, single_character_string_cache, SingleCharacterStringCache) \
jkummerow@chromium.org486075a2011-09-07 12:44:28 +000090 V(FixedArray, string_split_cache, StringSplitCache) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000091 V(Object, termination_exception, TerminationException) \
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000092 V(Smi, hash_seed, HashSeed) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000093 V(Map, string_map, StringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000094 V(Map, symbol_map, SymbolMap) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000095 V(Map, cons_string_map, ConsStringMap) \
96 V(Map, cons_ascii_string_map, ConsAsciiStringMap) \
ricow@chromium.org4668a2c2011-08-29 10:41:00 +000097 V(Map, sliced_string_map, SlicedStringMap) \
98 V(Map, sliced_ascii_string_map, SlicedAsciiStringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000099 V(Map, cons_symbol_map, ConsSymbolMap) \
100 V(Map, cons_ascii_symbol_map, ConsAsciiSymbolMap) \
101 V(Map, external_symbol_map, ExternalSymbolMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000102 V(Map, external_symbol_with_ascii_data_map, ExternalSymbolWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000103 V(Map, external_ascii_symbol_map, ExternalAsciiSymbolMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000104 V(Map, external_string_map, ExternalStringMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000105 V(Map, external_string_with_ascii_data_map, ExternalStringWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000106 V(Map, external_ascii_string_map, ExternalAsciiStringMap) \
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000107 V(Map, short_external_symbol_map, ShortExternalSymbolMap) \
108 V(Map, \
109 short_external_symbol_with_ascii_data_map, \
110 ShortExternalSymbolWithAsciiDataMap) \
111 V(Map, short_external_ascii_symbol_map, ShortExternalAsciiSymbolMap) \
112 V(Map, short_external_string_map, ShortExternalStringMap) \
113 V(Map, \
114 short_external_string_with_ascii_data_map, \
115 ShortExternalStringWithAsciiDataMap) \
116 V(Map, short_external_ascii_string_map, ShortExternalAsciiStringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000117 V(Map, undetectable_string_map, UndetectableStringMap) \
118 V(Map, undetectable_ascii_string_map, UndetectableAsciiStringMap) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000119 V(Map, external_pixel_array_map, ExternalPixelArrayMap) \
ager@chromium.org3811b432009-10-28 14:53:37 +0000120 V(Map, external_byte_array_map, ExternalByteArrayMap) \
121 V(Map, external_unsigned_byte_array_map, ExternalUnsignedByteArrayMap) \
122 V(Map, external_short_array_map, ExternalShortArrayMap) \
123 V(Map, external_unsigned_short_array_map, ExternalUnsignedShortArrayMap) \
124 V(Map, external_int_array_map, ExternalIntArrayMap) \
125 V(Map, external_unsigned_int_array_map, ExternalUnsignedIntArrayMap) \
126 V(Map, external_float_array_map, ExternalFloatArrayMap) \
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000127 V(Map, external_double_array_map, ExternalDoubleArrayMap) \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000128 V(Map, non_strict_arguments_elements_map, NonStrictArgumentsElementsMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000129 V(Map, function_context_map, FunctionContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000130 V(Map, catch_context_map, CatchContextMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000131 V(Map, with_context_map, WithContextMap) \
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000132 V(Map, block_context_map, BlockContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000133 V(Map, oddball_map, OddballMap) \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000134 V(Map, message_object_map, JSMessageObjectMap) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000135 V(Map, foreign_map, ForeignMap) \
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000136 V(HeapNumber, nan_value, NanValue) \
137 V(HeapNumber, infinity_value, InfinityValue) \
138 V(HeapNumber, minus_zero_value, MinusZeroValue) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000139 V(Map, neander_map, NeanderMap) \
140 V(JSObject, message_listeners, MessageListeners) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000141 V(Foreign, prototype_accessors, PrototypeAccessors) \
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000142 V(UnseededNumberDictionary, code_stubs, CodeStubs) \
143 V(UnseededNumberDictionary, non_monomorphic_cache, NonMonomorphicCache) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000144 V(PolymorphicCodeCache, polymorphic_code_cache, PolymorphicCodeCache) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000145 V(Code, js_entry_code, JsEntryCode) \
146 V(Code, js_construct_entry_code, JsConstructEntryCode) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000147 V(FixedArray, natives_source_cache, NativesSourceCache) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000148 V(Object, last_script_id, LastScriptId) \
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000149 V(Script, empty_script, EmptyScript) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000150 V(Smi, real_stack_limit, RealStackLimit) \
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000151 V(StringDictionary, intrinsic_function_names, IntrinsicFunctionNames) \
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000152 V(Smi, arguments_adaptor_deopt_pc_offset, ArgumentsAdaptorDeoptPCOffset)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154#define ROOT_LIST(V) \
155 STRONG_ROOT_LIST(V) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000156 V(SymbolTable, symbol_table, SymbolTable)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157
158#define SYMBOL_LIST(V) \
159 V(Array_symbol, "Array") \
160 V(Object_symbol, "Object") \
161 V(Proto_symbol, "__proto__") \
162 V(StringImpl_symbol, "StringImpl") \
163 V(arguments_symbol, "arguments") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000164 V(Arguments_symbol, "Arguments") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 V(call_symbol, "call") \
166 V(apply_symbol, "apply") \
167 V(caller_symbol, "caller") \
168 V(boolean_symbol, "boolean") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000169 V(Boolean_symbol, "Boolean") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 V(callee_symbol, "callee") \
171 V(constructor_symbol, "constructor") \
172 V(code_symbol, ".code") \
173 V(result_symbol, ".result") \
174 V(catch_var_symbol, ".catch-var") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 V(empty_symbol, "") \
176 V(eval_symbol, "eval") \
177 V(function_symbol, "function") \
178 V(length_symbol, "length") \
179 V(name_symbol, "name") \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000180 V(native_symbol, "native") \
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000181 V(null_symbol, "null") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 V(number_symbol, "number") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000183 V(Number_symbol, "Number") \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000184 V(nan_symbol, "NaN") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000185 V(RegExp_symbol, "RegExp") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000186 V(source_symbol, "source") \
187 V(global_symbol, "global") \
188 V(ignore_case_symbol, "ignoreCase") \
189 V(multiline_symbol, "multiline") \
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000190 V(input_symbol, "input") \
191 V(index_symbol, "index") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000192 V(last_index_symbol, "lastIndex") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 V(object_symbol, "object") \
194 V(prototype_symbol, "prototype") \
195 V(string_symbol, "string") \
ager@chromium.org7c537e22008-10-16 08:43:32 +0000196 V(String_symbol, "String") \
197 V(Date_symbol, "Date") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 V(this_symbol, "this") \
199 V(to_string_symbol, "toString") \
200 V(char_at_symbol, "CharAt") \
201 V(undefined_symbol, "undefined") \
202 V(value_of_symbol, "valueOf") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 V(InitializeVarGlobal_symbol, "InitializeVarGlobal") \
204 V(InitializeConstGlobal_symbol, "InitializeConstGlobal") \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000205 V(KeyedLoadElementMonomorphic_symbol, \
206 "KeyedLoadElementMonomorphic") \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000207 V(KeyedStoreElementMonomorphic_symbol, \
208 "KeyedStoreElementMonomorphic") \
ulan@chromium.org65a89c22012-02-14 11:46:07 +0000209 V(KeyedStoreAndGrowElementMonomorphic_symbol, \
210 "KeyedStoreAndGrowElementMonomorphic") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 V(stack_overflow_symbol, "kStackOverflowBoilerplate") \
212 V(illegal_access_symbol, "illegal access") \
213 V(out_of_memory_symbol, "out-of-memory") \
214 V(illegal_execution_state_symbol, "illegal execution state") \
215 V(get_symbol, "get") \
216 V(set_symbol, "set") \
217 V(function_class_symbol, "Function") \
218 V(illegal_argument_symbol, "illegal argument") \
219 V(MakeReferenceError_symbol, "MakeReferenceError") \
220 V(MakeSyntaxError_symbol, "MakeSyntaxError") \
221 V(MakeTypeError_symbol, "MakeTypeError") \
222 V(invalid_lhs_in_assignment_symbol, "invalid_lhs_in_assignment") \
223 V(invalid_lhs_in_for_in_symbol, "invalid_lhs_in_for_in") \
224 V(invalid_lhs_in_postfix_op_symbol, "invalid_lhs_in_postfix_op") \
225 V(invalid_lhs_in_prefix_op_symbol, "invalid_lhs_in_prefix_op") \
226 V(illegal_return_symbol, "illegal_return") \
227 V(illegal_break_symbol, "illegal_break") \
228 V(illegal_continue_symbol, "illegal_continue") \
229 V(unknown_label_symbol, "unknown_label") \
230 V(redeclaration_symbol, "redeclaration") \
231 V(failure_symbol, "<failure>") \
232 V(space_symbol, " ") \
233 V(exec_symbol, "exec") \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000234 V(zero_symbol, "0") \
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000235 V(global_eval_symbol, "GlobalEval") \
ager@chromium.org3811b432009-10-28 14:53:37 +0000236 V(identity_hash_symbol, "v8::IdentityHash") \
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000237 V(closure_symbol, "(closure)") \
ager@chromium.org04921a82011-06-27 13:21:41 +0000238 V(use_strict, "use strict") \
239 V(dot_symbol, ".") \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000240 V(anonymous_function_symbol, "(anonymous function)") \
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +0000241 V(compare_ic_symbol, ".compare_ic") \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000242 V(infinity_symbol, "Infinity") \
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +0000243 V(minus_infinity_symbol, "-Infinity") \
244 V(hidden_stack_trace_symbol, "v8::hidden_stack_trace")
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000246// Forward declarations.
kasper.lund7276f142008-07-30 08:49:36 +0000247class GCTracer;
ager@chromium.org60121232009-12-03 11:25:37 +0000248class HeapStats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000249class Isolate;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000250class WeakObjectRetainer;
kasper.lund7276f142008-07-30 08:49:36 +0000251
252
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000253typedef String* (*ExternalStringTableUpdaterCallback)(Heap* heap,
254 Object** pointer);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000255
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000256class StoreBufferRebuilder {
257 public:
258 explicit StoreBufferRebuilder(StoreBuffer* store_buffer)
259 : store_buffer_(store_buffer) {
260 }
261
262 void Callback(MemoryChunk* page, StoreBufferEvent event);
263
264 private:
265 StoreBuffer* store_buffer_;
266
267 // We record in this variable how full the store buffer was when we started
268 // iterating over the current page, finding pointers to new space. If the
269 // store buffer overflows again we can exempt the page from the store buffer
270 // by rewinding to this point instead of having to search the store buffer.
271 Object*** start_of_current_page_;
272 // The current page we are scanning in the store buffer iterator.
273 MemoryChunk* current_page_;
274};
275
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000276
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278// The all static Heap captures the interface to the global object heap.
279// All JavaScript contexts by this process share the same object heap.
280
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000281#ifdef DEBUG
282class HeapDebugUtils;
283#endif
284
285
286// A queue of objects promoted during scavenge. Each object is accompanied
287// by it's size to avoid dereferencing a map pointer for scanning.
288class PromotionQueue {
289 public:
danno@chromium.orgc612e022011-11-10 11:38:15 +0000290 explicit PromotionQueue(Heap* heap)
291 : front_(NULL),
292 rear_(NULL),
293 limit_(NULL),
294 emergency_stack_(0),
295 heap_(heap) { }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000296
danno@chromium.orgc612e022011-11-10 11:38:15 +0000297 void Initialize();
298
299 void Destroy() {
300 ASSERT(is_empty());
301 delete emergency_stack_;
302 emergency_stack_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000303 }
304
danno@chromium.orgc612e022011-11-10 11:38:15 +0000305 inline void ActivateGuardIfOnTheSamePage();
306
307 Page* GetHeadPage() {
308 return Page::FromAllocationTop(reinterpret_cast<Address>(rear_));
309 }
310
311 void SetNewLimit(Address limit) {
312 if (!guard_) {
313 return;
314 }
315
316 ASSERT(GetHeadPage() == Page::FromAllocationTop(limit));
317 limit_ = reinterpret_cast<intptr_t*>(limit);
318
319 if (limit_ <= rear_) {
320 return;
321 }
322
323 RelocateQueueHead();
324 }
325
326 bool is_empty() {
327 return (front_ == rear_) &&
328 (emergency_stack_ == NULL || emergency_stack_->length() == 0);
329 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000330
331 inline void insert(HeapObject* target, int size);
332
333 void remove(HeapObject** target, int* size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000334 ASSERT(!is_empty());
danno@chromium.orgc612e022011-11-10 11:38:15 +0000335 if (front_ == rear_) {
336 Entry e = emergency_stack_->RemoveLast();
337 *target = e.obj_;
338 *size = e.size_;
339 return;
340 }
341
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000342 if (NewSpacePage::IsAtStart(reinterpret_cast<Address>(front_))) {
343 NewSpacePage* front_page =
344 NewSpacePage::FromAddress(reinterpret_cast<Address>(front_));
345 ASSERT(!front_page->prev_page()->is_anchor());
346 front_ =
347 reinterpret_cast<intptr_t*>(front_page->prev_page()->body_limit());
348 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000349 *target = reinterpret_cast<HeapObject*>(*(--front_));
350 *size = static_cast<int>(*(--front_));
351 // Assert no underflow.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000352 SemiSpace::AssertValidRange(reinterpret_cast<Address>(rear_),
353 reinterpret_cast<Address>(front_));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000354 }
355
356 private:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000357 // The front of the queue is higher in the memory page chain than the rear.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000358 intptr_t* front_;
359 intptr_t* rear_;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000360 intptr_t* limit_;
361
362 bool guard_;
363
364 static const int kEntrySizeInWords = 2;
365
366 struct Entry {
367 Entry(HeapObject* obj, int size) : obj_(obj), size_(size) { }
368
369 HeapObject* obj_;
370 int size_;
371 };
372 List<Entry>* emergency_stack_;
373
374 Heap* heap_;
375
376 void RelocateQueueHead();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377
378 DISALLOW_COPY_AND_ASSIGN(PromotionQueue);
379};
380
381
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000382typedef void (*ScavengingCallback)(Map* map,
383 HeapObject** slot,
384 HeapObject* object);
385
386
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000387// External strings table is a place where all external strings are
388// registered. We need to keep track of such strings to properly
389// finalize them.
390class ExternalStringTable {
391 public:
392 // Registers an external string.
393 inline void AddString(String* string);
394
395 inline void Iterate(ObjectVisitor* v);
396
397 // Restores internal invariant and gets rid of collected strings.
398 // Must be called after each Iterate() that modified the strings.
399 void CleanUp();
400
401 // Destroys all allocated memory.
402 void TearDown();
403
404 private:
405 ExternalStringTable() { }
406
407 friend class Heap;
408
409 inline void Verify();
410
411 inline void AddOldString(String* string);
412
413 // Notifies the table that only a prefix of the new list is valid.
414 inline void ShrinkNewStrings(int position);
415
416 // To speed up scavenge collections new space string are kept
417 // separate from old space strings.
418 List<Object*> new_space_strings_;
419 List<Object*> old_space_strings_;
420
421 Heap* heap_;
422
423 DISALLOW_COPY_AND_ASSIGN(ExternalStringTable);
424};
425
426
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000427enum ArrayStorageAllocationMode {
428 DONT_INITIALIZE_ARRAY_ELEMENTS,
429 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE
430};
431
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432class Heap {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 public:
434 // Configure heap size before setup. Return false if the heap has been
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000435 // set up already.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000436 bool ConfigureHeap(int max_semispace_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000437 intptr_t max_old_gen_size,
438 intptr_t max_executable_size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000439 bool ConfigureHeapDefault();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440
441 // Initializes the global object heap. If create_heap_objects is true,
442 // also creates the basic non-mutable objects.
443 // Returns whether it succeeded.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000444 bool SetUp(bool create_heap_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
446 // Destroys all memory allocated by the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000447 void TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000448
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000449 // Set the stack limit in the roots_ array. Some architectures generate
450 // code that looks here, because it is faster than loading from the static
451 // jslimit_/real_jslimit_ variable in the StackGuard.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000452 void SetStackLimits();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000453
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000454 // Returns whether SetUp has been called.
455 bool HasBeenSetUp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456
ager@chromium.org3811b432009-10-28 14:53:37 +0000457 // Returns the maximum amount of memory reserved for the heap. For
458 // the young generation, we reserve 4 times the amount needed for a
459 // semi space. The young generation consists of two semi spaces and
460 // we reserve twice the amount needed for those in order to ensure
461 // that new space can be aligned to its size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000462 intptr_t MaxReserved() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000463 return 4 * reserved_semispace_size_ + max_old_generation_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000465 int MaxSemiSpaceSize() { return max_semispace_size_; }
466 int ReservedSemiSpaceSize() { return reserved_semispace_size_; }
467 int InitialSemiSpaceSize() { return initial_semispace_size_; }
468 intptr_t MaxOldGenerationSize() { return max_old_generation_size_; }
469 intptr_t MaxExecutableSize() { return max_executable_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000470
471 // Returns the capacity of the heap in bytes w/o growing. Heap grows when
472 // more spaces are needed until it reaches the limit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000473 intptr_t Capacity();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474
ager@chromium.org3811b432009-10-28 14:53:37 +0000475 // Returns the amount of memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000476 intptr_t CommittedMemory();
ager@chromium.org3811b432009-10-28 14:53:37 +0000477
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000478 // Returns the amount of executable memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000479 intptr_t CommittedMemoryExecutable();
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000480
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000481 // Returns the available bytes in space w/o growing.
482 // Heap doesn't guarantee that it can allocate an object that requires
483 // all available bytes. Check MaxHeapObjectSize() instead.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 intptr_t Available();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000486 // Returns the maximum object size in paged space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000487 inline int MaxObjectSizeInPagedSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488
489 // Returns of size of all objects residing in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000490 intptr_t SizeOfObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491
492 // Return the starting address and a mask for the new space. And-masking an
493 // address with the mask will result in the start address of the new space
494 // for all addresses in either semispace.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000495 Address NewSpaceStart() { return new_space_.start(); }
496 uintptr_t NewSpaceMask() { return new_space_.mask(); }
497 Address NewSpaceTop() { return new_space_.top(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000499 NewSpace* new_space() { return &new_space_; }
500 OldSpace* old_pointer_space() { return old_pointer_space_; }
501 OldSpace* old_data_space() { return old_data_space_; }
502 OldSpace* code_space() { return code_space_; }
503 MapSpace* map_space() { return map_space_; }
504 CellSpace* cell_space() { return cell_space_; }
505 LargeObjectSpace* lo_space() { return lo_space_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000507 bool always_allocate() { return always_allocate_scope_depth_ != 0; }
508 Address always_allocate_scope_depth_address() {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000509 return reinterpret_cast<Address>(&always_allocate_scope_depth_);
510 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000511 bool linear_allocation() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000512 return linear_allocation_scope_depth_ != 0;
ager@chromium.org3811b432009-10-28 14:53:37 +0000513 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000514
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 Address* NewSpaceAllocationTopAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000516 return new_space_.allocation_top_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000518 Address* NewSpaceAllocationLimitAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000519 return new_space_.allocation_limit_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 }
521
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000522 // Uncommit unused semi space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 bool UncommitFromSpace() { return new_space_.UncommitFromSpace(); }
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000524
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000525 // Allocates and initializes a new JavaScript object based on a
526 // constructor.
527 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
528 // failed.
529 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000530 MUST_USE_RESULT MaybeObject* AllocateJSObject(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000531 JSFunction* constructor, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000533 // Allocate a JSArray with no elements
534 MUST_USE_RESULT MaybeObject* AllocateEmptyJSArray(
535 ElementsKind elements_kind,
536 PretenureFlag pretenure = NOT_TENURED) {
537 return AllocateJSArrayAndStorage(elements_kind, 0, 0,
538 DONT_INITIALIZE_ARRAY_ELEMENTS,
539 pretenure);
540 }
541
542 // Allocate a JSArray with a specified length but elements that are left
543 // uninitialized.
544 MUST_USE_RESULT MaybeObject* AllocateJSArrayAndStorage(
545 ElementsKind elements_kind,
546 int length,
547 int capacity,
548 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
549 PretenureFlag pretenure = NOT_TENURED);
550
551 // Allocate a JSArray with no elements
552 MUST_USE_RESULT MaybeObject* AllocateJSArrayWithElements(
553 FixedArrayBase* array_base,
554 ElementsKind elements_kind,
555 PretenureFlag pretenure = NOT_TENURED);
556
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000557 // Allocates and initializes a new global object based on a constructor.
558 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
559 // failed.
560 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000561 MUST_USE_RESULT MaybeObject* AllocateGlobalObject(JSFunction* constructor);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000562
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000563 // Returns a deep copy of the JavaScript object.
564 // Properties and elements are copied too.
565 // Returns failure if allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000566 MUST_USE_RESULT MaybeObject* CopyJSObject(JSObject* source);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000567
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 // Allocates the function prototype.
569 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
570 // failed.
571 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000572 MUST_USE_RESULT MaybeObject* AllocateFunctionPrototype(JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573
lrn@chromium.org34e60782011-09-15 07:25:40 +0000574 // Allocates a Harmony proxy or function proxy.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000575 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
576 // failed.
577 // Please note this does not perform a garbage collection.
578 MUST_USE_RESULT MaybeObject* AllocateJSProxy(Object* handler,
579 Object* prototype);
580
lrn@chromium.org34e60782011-09-15 07:25:40 +0000581 MUST_USE_RESULT MaybeObject* AllocateJSFunctionProxy(Object* handler,
582 Object* call_trap,
583 Object* construct_trap,
584 Object* prototype);
585
586 // Reinitialize a JSReceiver into an (empty) JS object of respective type and
587 // size, but keeping the original prototype. The receiver must have at least
588 // the size of the new object. The object is reinitialized and behaves as an
589 // object that has been freshly allocated.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000590 // Returns failure if an error occured, otherwise object.
lrn@chromium.org34e60782011-09-15 07:25:40 +0000591 MUST_USE_RESULT MaybeObject* ReinitializeJSReceiver(JSReceiver* object,
592 InstanceType type,
593 int size);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000594
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000595 // Reinitialize an JSGlobalProxy based on a constructor. The object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 // must have the same size as objects allocated using the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000597 // constructor. The object is reinitialized and behaves as an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 // object that has been freshly allocated using the constructor.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000599 MUST_USE_RESULT MaybeObject* ReinitializeJSGlobalProxy(
600 JSFunction* constructor, JSGlobalProxy* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
602 // Allocates and initializes a new JavaScript object based on a map.
603 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
604 // failed.
605 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606 MUST_USE_RESULT MaybeObject* AllocateJSObjectFromMap(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000607 Map* map, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608
609 // Allocates a heap object based on the map.
610 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
611 // failed.
612 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000613 MUST_USE_RESULT MaybeObject* Allocate(Map* map, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614
615 // Allocates a JS Map in the heap.
616 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
617 // failed.
618 // Please note this function does not perform a garbage collection.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000619 MUST_USE_RESULT MaybeObject* AllocateMap(
620 InstanceType instance_type,
621 int instance_size,
622 ElementsKind elements_kind = FAST_ELEMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623
624 // Allocates a partial map for bootstrapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000625 MUST_USE_RESULT MaybeObject* AllocatePartialMap(InstanceType instance_type,
626 int instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627
628 // Allocate a map for the specified function
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000629 MUST_USE_RESULT MaybeObject* AllocateInitialMap(JSFunction* fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000631 // Allocates an empty code cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000632 MUST_USE_RESULT MaybeObject* AllocateCodeCache();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000633
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000634 // Allocates a serialized scope info.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000635 MUST_USE_RESULT MaybeObject* AllocateScopeInfo(int length);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000636
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000637 // Allocates an empty PolymorphicCodeCache.
638 MUST_USE_RESULT MaybeObject* AllocatePolymorphicCodeCache();
639
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000640 // Allocates a pre-tenured empty AccessorPair.
641 MUST_USE_RESULT MaybeObject* AllocateAccessorPair();
642
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000643 // Clear the Instanceof cache (used when a prototype changes).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000644 inline void ClearInstanceofCache();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 // Allocates and fully initializes a String. There are two String
647 // encodings: ASCII and two byte. One should choose between the three string
648 // allocation functions based on the encoding of the string buffer used to
649 // initialized the string.
650 // - ...FromAscii initializes the string from a buffer that is ASCII
651 // encoded (it does not check that the buffer is ASCII encoded) and the
652 // result will be ASCII encoded.
653 // - ...FromUTF8 initializes the string from a buffer that is UTF-8
654 // encoded. If the characters are all single-byte characters, the
655 // result will be ASCII encoded, otherwise it will converted to two
656 // byte.
657 // - ...FromTwoByte initializes the string from a buffer that is two-byte
658 // encoded. If the characters are all single-byte characters, the
659 // result will be converted to ASCII, otherwise it will be left as
660 // two-byte.
661 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
662 // failed.
663 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000664 MUST_USE_RESULT MaybeObject* AllocateStringFromAscii(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 Vector<const char> str,
666 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000667 MUST_USE_RESULT inline MaybeObject* AllocateStringFromUtf8(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000668 Vector<const char> str,
669 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000670 MUST_USE_RESULT MaybeObject* AllocateStringFromUtf8Slow(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 Vector<const char> str,
672 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000673 MUST_USE_RESULT MaybeObject* AllocateStringFromTwoByte(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 Vector<const uc16> str,
675 PretenureFlag pretenure = NOT_TENURED);
676
677 // Allocates a symbol in old space based on the character stream.
678 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
679 // failed.
680 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000681 MUST_USE_RESULT inline MaybeObject* AllocateSymbol(Vector<const char> str,
682 int chars,
683 uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000684
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000685 MUST_USE_RESULT inline MaybeObject* AllocateAsciiSymbol(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000686 Vector<const char> str,
687 uint32_t hash_field);
688
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000689 MUST_USE_RESULT inline MaybeObject* AllocateTwoByteSymbol(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000690 Vector<const uc16> str,
691 uint32_t hash_field);
692
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000693 MUST_USE_RESULT MaybeObject* AllocateInternalSymbol(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000694 unibrow::CharacterStream* buffer, int chars, uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000695
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000696 MUST_USE_RESULT MaybeObject* AllocateExternalSymbol(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000697 Vector<const char> str,
698 int chars);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000699
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700 // Allocates and partially initializes a String. There are two String
701 // encodings: ASCII and two byte. These functions allocate a string of the
702 // given length and set its map and length fields. The characters of the
703 // string are uninitialized.
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* AllocateRawAsciiString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 int length,
709 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000710 MUST_USE_RESULT MaybeObject* AllocateRawTwoByteString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 int length,
712 PretenureFlag pretenure = NOT_TENURED);
713
714 // Computes a single character string where the character has code.
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000715 // A cache is used for ASCII codes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
717 // failed. Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000718 MUST_USE_RESULT MaybeObject* LookupSingleCharacterStringFromCode(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000719 uint16_t code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720
721 // Allocate a byte array of the specified length
722 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
723 // failed.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000724 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000725 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length,
726 PretenureFlag pretenure);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000727
728 // Allocate a non-tenured byte array of the specified length
729 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
730 // failed.
731 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000732 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733
ager@chromium.org3811b432009-10-28 14:53:37 +0000734 // Allocates an external array of the specified length and type.
735 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
736 // failed.
737 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000738 MUST_USE_RESULT MaybeObject* AllocateExternalArray(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000739 int length,
740 ExternalArrayType array_type,
741 void* external_pointer,
742 PretenureFlag pretenure);
ager@chromium.org3811b432009-10-28 14:53:37 +0000743
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000744 // Allocate a tenured JS global property cell.
745 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
746 // failed.
747 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000748 MUST_USE_RESULT MaybeObject* AllocateJSGlobalPropertyCell(Object* value);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000749
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 // Allocates a fixed array initialized with undefined values
751 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
752 // failed.
753 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000754 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length,
755 PretenureFlag pretenure);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000756 // Allocates a fixed array initialized with undefined values
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000757 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000759 // Allocates an uninitialized fixed array. It must be filled by the caller.
760 //
761 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
762 // failed.
763 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000764 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedArray(int length);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000765
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000766 // Make a copy of src and return it. Returns
767 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000768 MUST_USE_RESULT inline MaybeObject* CopyFixedArray(FixedArray* src);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000769
770 // Make a copy of src, set the map, and return the copy. Returns
771 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000772 MUST_USE_RESULT MaybeObject* CopyFixedArrayWithMap(FixedArray* src, Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000774 // Make a copy of src and return it. Returns
775 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
776 MUST_USE_RESULT inline MaybeObject* CopyFixedDoubleArray(
777 FixedDoubleArray* src);
778
779 // Make a copy of src, set the map, and return the copy. Returns
780 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
781 MUST_USE_RESULT MaybeObject* CopyFixedDoubleArrayWithMap(
782 FixedDoubleArray* src, Map* map);
783
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000784 // Allocates a fixed array initialized with the hole values.
785 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
786 // failed.
787 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000788 MUST_USE_RESULT MaybeObject* AllocateFixedArrayWithHoles(
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000789 int length,
790 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000792 MUST_USE_RESULT MaybeObject* AllocateRawFixedDoubleArray(
793 int length,
794 PretenureFlag pretenure);
795
796 // Allocates a fixed double array with uninitialized values. Returns
797 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
798 // Please note this does not perform a garbage collection.
799 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedDoubleArray(
800 int length,
801 PretenureFlag pretenure = NOT_TENURED);
802
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000803 // Allocates a fixed double array with hole values. Returns
804 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
805 // Please note this does not perform a garbage collection.
806 MUST_USE_RESULT MaybeObject* AllocateFixedDoubleArrayWithHoles(
807 int length,
808 PretenureFlag pretenure = NOT_TENURED);
809
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810 // AllocateHashTable is identical to AllocateFixedArray except
811 // that the resulting object has hash_table_map as map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000812 MUST_USE_RESULT MaybeObject* AllocateHashTable(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000813 int length, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000814
815 // Allocate a global (but otherwise uninitialized) context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000816 MUST_USE_RESULT MaybeObject* AllocateGlobalContext();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000817
818 // Allocate a function context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000819 MUST_USE_RESULT MaybeObject* AllocateFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000820 JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000821
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000822 // Allocate a catch context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000823 MUST_USE_RESULT MaybeObject* AllocateCatchContext(JSFunction* function,
824 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000825 String* name,
826 Object* thrown_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827 // Allocate a 'with' context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000828 MUST_USE_RESULT MaybeObject* AllocateWithContext(JSFunction* function,
829 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000830 JSObject* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000832 // Allocate a block context.
833 MUST_USE_RESULT MaybeObject* AllocateBlockContext(JSFunction* function,
834 Context* previous,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000835 ScopeInfo* info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000836
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000837 // Allocates a new utility object in the old generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000838 MUST_USE_RESULT MaybeObject* AllocateStruct(InstanceType type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840 // Allocates a function initialized with a shared part.
841 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
842 // failed.
843 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000844 MUST_USE_RESULT MaybeObject* AllocateFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000845 Map* function_map,
846 SharedFunctionInfo* shared,
847 Object* prototype,
848 PretenureFlag pretenure = TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000850 // Arguments object size.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000851 static const int kArgumentsObjectSize =
852 JSObject::kHeaderSize + 2 * kPointerSize;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000853 // Strict mode arguments has no callee so it is smaller.
854 static const int kArgumentsObjectSizeStrict =
855 JSObject::kHeaderSize + 1 * kPointerSize;
856 // Indicies for direct access into argument objects.
857 static const int kArgumentsLengthIndex = 0;
858 // callee is only valid in non-strict mode.
859 static const int kArgumentsCalleeIndex = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860
861 // Allocates an arguments object - optionally with an elements array.
862 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
863 // failed.
864 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000865 MUST_USE_RESULT MaybeObject* AllocateArgumentsObject(
866 Object* callee, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 // Same as NewNumberFromDouble, but may return a preallocated/immutable
869 // number object (e.g., minus_zero_value_, nan_value_)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000870 MUST_USE_RESULT MaybeObject* NumberFromDouble(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000871 double value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
873 // Allocated a HeapNumber from value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000874 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000875 double value,
876 PretenureFlag pretenure);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000877 // pretenure = NOT_TENURED
878 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(double value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
880 // Converts an int into either a Smi or a HeapNumber object.
881 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
882 // failed.
883 // Please note this does not perform a garbage collection.
erikcorry0ad885c2011-11-21 13:51:57 +0000884 MUST_USE_RESULT inline MaybeObject* NumberFromInt32(
885 int32_t value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886
887 // Converts an int into either a Smi or a HeapNumber object.
888 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
889 // failed.
890 // Please note this does not perform a garbage collection.
erikcorry0ad885c2011-11-21 13:51:57 +0000891 MUST_USE_RESULT inline MaybeObject* NumberFromUint32(
892 uint32_t value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000894 // Allocates a new foreign object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
896 // failed.
897 // Please note this does not perform a garbage collection.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000898 MUST_USE_RESULT MaybeObject* AllocateForeign(
899 Address address, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
901 // Allocates a new SharedFunctionInfo object.
902 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
903 // failed.
904 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000905 MUST_USE_RESULT MaybeObject* AllocateSharedFunctionInfo(Object* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000906
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000907 // Allocates a new JSMessageObject object.
908 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
909 // failed.
910 // Please note that this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000911 MUST_USE_RESULT MaybeObject* AllocateJSMessageObject(
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000912 String* type,
913 JSArray* arguments,
914 int start_position,
915 int end_position,
916 Object* script,
917 Object* stack_trace,
918 Object* stack_frames);
919
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 // Allocates a new cons string object.
921 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
922 // failed.
923 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000924 MUST_USE_RESULT MaybeObject* AllocateConsString(String* first,
925 String* second);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000927 // Allocates a new sub string object which is a substring of an underlying
928 // string buffer stretching from the index start (inclusive) to the index
929 // end (exclusive).
930 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
931 // failed.
932 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000933 MUST_USE_RESULT MaybeObject* AllocateSubString(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000934 String* buffer,
935 int start,
936 int end,
937 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938
939 // Allocate a new external string object, which is backed by a string
940 // resource that resides outside the V8 heap.
941 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
942 // failed.
943 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000944 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000945 const ExternalAsciiString::Resource* resource);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000946 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000947 const ExternalTwoByteString::Resource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000949 // Finalizes an external string by deleting the associated external
950 // data and clearing the resource pointer.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000951 inline void FinalizeExternalString(String* string);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000952
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000953 // Allocates an uninitialized object. The memory is non-executable if the
954 // hardware and OS allow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
956 // failed.
957 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000958 MUST_USE_RESULT inline MaybeObject* AllocateRaw(int size_in_bytes,
959 AllocationSpace space,
960 AllocationSpace retry_space);
kasper.lund7276f142008-07-30 08:49:36 +0000961
ager@chromium.org6f10e412009-02-13 10:11:16 +0000962 // Initialize a filler object to keep the ability to iterate over the heap
963 // when shortening objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000964 void CreateFillerObjectAt(Address addr, int size);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000965
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 // Makes a new native code object
967 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000968 // failed. On success, the pointer to the Code object is stored in the
969 // self_reference. This allows generated code to reference its own Code
970 // object by containing this pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000972 MUST_USE_RESULT MaybeObject* CreateCode(const CodeDesc& desc,
973 Code::Flags flags,
974 Handle<Object> self_reference,
975 bool immovable = false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000977 MUST_USE_RESULT MaybeObject* CopyCode(Code* code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000978
979 // Copy the code and scope info part of the code object, but insert
980 // the provided data as the relocation information.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000981 MUST_USE_RESULT MaybeObject* CopyCode(Code* code, Vector<byte> reloc_info);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000982
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 // Finds the symbol for string in the symbol table.
984 // If not found, a new symbol is added to the table and returned.
985 // Returns Failure::RetryAfterGC(requested_bytes, space) if allocation
986 // failed.
987 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000988 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str);
989 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000990 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000991 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(const char* str) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992 return LookupSymbol(CStrVector(str));
993 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000994 MUST_USE_RESULT MaybeObject* LookupSymbol(String* str);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000995 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Handle<SeqAsciiString> string,
996 int from,
997 int length);
998
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000999 bool LookupSymbolIfExists(String* str, String** symbol);
1000 bool LookupTwoCharsSymbolIfExists(String* str, String** symbol);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001
1002 // Compute the matching symbol map for a string if possible.
1003 // NULL is returned if string is in new space or not flattened.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001004 Map* SymbolMapForString(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001006 // Tries to flatten a string before compare operation.
1007 //
1008 // Returns a failure in case it was decided that flattening was
1009 // necessary and failed. Note, if flattening is not necessary the
1010 // string might stay non-flat even when not a failure is returned.
1011 //
1012 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001013 MUST_USE_RESULT inline MaybeObject* PrepareForCompare(String* str);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001014
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 // Converts the given boolean condition to JavaScript boolean value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001016 inline Object* ToBoolean(bool condition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017
1018 // Code that should be run before and after each GC. Includes some
1019 // reporting/verification activities when compiled with DEBUG set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001020 void GarbageCollectionPrologue();
1021 void GarbageCollectionEpilogue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022
1023 // Performs garbage collection operation.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001024 // Returns whether there is a chance that another major GC could
1025 // collect more garbage.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001026 bool CollectGarbage(AllocationSpace space,
1027 GarbageCollector collector,
1028 const char* gc_reason,
1029 const char* collector_reason);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001030
1031 // Performs garbage collection operation.
1032 // Returns whether there is a chance that another major GC could
1033 // collect more garbage.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001034 inline bool CollectGarbage(AllocationSpace space,
1035 const char* gc_reason = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001037 static const int kNoGCFlags = 0;
1038 static const int kMakeHeapIterableMask = 1;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001039 static const int kReduceMemoryFootprintMask = 2;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001040
1041 // Performs a full garbage collection. If (flags & kMakeHeapIterableMask) is
1042 // non-zero, then the slower precise sweeper is used, which leaves the heap
1043 // in a state where we can iterate over the heap visiting all objects.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001044 void CollectAllGarbage(int flags, const char* gc_reason = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001045
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001046 // Last hope GC, should try to squeeze as much as possible.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001047 void CollectAllAvailableGarbage(const char* gc_reason = NULL);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001048
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001049 // Check whether the heap is currently iterable.
1050 bool IsHeapIterable();
1051
1052 // Ensure that we have swept all spaces in such a way that we can iterate
1053 // over all objects. May cause a GC.
1054 void EnsureHeapIsIterable();
1055
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001056 // Notify the heap that a context has been disposed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001057 int NotifyContextDisposed() { return ++contexts_disposed_; }
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001058
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 // Utility to invoke the scavenger. This is needed in test code to
1060 // ensure correct callback for weak global handles.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001061 void PerformScavenge();
1062
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001063 inline void increment_scan_on_scavenge_pages() {
1064 scan_on_scavenge_pages_++;
1065 if (FLAG_gc_verbose) {
1066 PrintF("Scan-on-scavenge pages: %d\n", scan_on_scavenge_pages_);
1067 }
1068 }
1069
1070 inline void decrement_scan_on_scavenge_pages() {
1071 scan_on_scavenge_pages_--;
1072 if (FLAG_gc_verbose) {
1073 PrintF("Scan-on-scavenge pages: %d\n", scan_on_scavenge_pages_);
1074 }
1075 }
1076
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001077 PromotionQueue* promotion_queue() { return &promotion_queue_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001079#ifdef DEBUG
1080 // Utility used with flag gc-greedy.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001081 void GarbageCollectionGreedyCheck();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001082#endif
1083
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001084 void AddGCPrologueCallback(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001085 GCEpilogueCallback callback, GCType gc_type_filter);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001086 void RemoveGCPrologueCallback(GCEpilogueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001087
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001088 void AddGCEpilogueCallback(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001089 GCEpilogueCallback callback, GCType gc_type_filter);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001090 void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001091
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001092 void SetGlobalGCPrologueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001093 ASSERT((callback == NULL) ^ (global_gc_prologue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 global_gc_prologue_callback_ = callback;
1095 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001096 void SetGlobalGCEpilogueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001097 ASSERT((callback == NULL) ^ (global_gc_epilogue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 global_gc_epilogue_callback_ = callback;
1099 }
1100
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001101 // Heap root getters. We have versions with and without type::cast() here.
1102 // You can't use type::cast during GC because the assert fails.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001103 // TODO(1490): Try removing the unchecked accessors, now that GC marking does
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001104 // not corrupt the map.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001105#define ROOT_ACCESSOR(type, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001106 type* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001107 return type::cast(roots_[k##camel_name##RootIndex]); \
1108 } \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001109 type* raw_unchecked_##name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001110 return reinterpret_cast<type*>(roots_[k##camel_name##RootIndex]); \
1111 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 ROOT_LIST(ROOT_ACCESSOR)
1113#undef ROOT_ACCESSOR
1114
1115// Utility type maps
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001116#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001117 Map* name##_map() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001118 return Map::cast(roots_[k##Name##MapRootIndex]); \
1119 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
1121#undef STRUCT_MAP_ACCESSOR
1122
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001123#define SYMBOL_ACCESSOR(name, str) String* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001124 return String::cast(roots_[k##name##RootIndex]); \
1125 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126 SYMBOL_LIST(SYMBOL_ACCESSOR)
1127#undef SYMBOL_ACCESSOR
1128
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001129 // The hidden_symbol is special because it is the empty string, but does
1130 // not match the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001131 String* hidden_symbol() { return hidden_symbol_; }
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001132
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 void set_global_contexts_list(Object* object) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001134 global_contexts_list_ = object;
1135 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001136 Object* global_contexts_list() { return global_contexts_list_; }
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001137
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001138 // Number of mark-sweeps.
1139 int ms_count() { return ms_count_; }
1140
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 // Iterates over all roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001142 void IterateRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 // Iterates over all strong roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001144 void IterateStrongRoots(ObjectVisitor* v, VisitMode mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001145 // Iterates over all the other roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001146 void IterateWeakRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001147
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001148 // Iterate pointers to from semispace of new space found in memory interval
1149 // from start to end.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001150 void IterateAndMarkPointersToFromSpace(Address start,
1151 Address end,
1152 ObjectSlotCallback callback);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154 // Returns whether the object resides in new space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001155 inline bool InNewSpace(Object* object);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001156 inline bool InNewSpace(Address addr);
1157 inline bool InNewSpacePage(Address addr);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001158 inline bool InFromSpace(Object* object);
1159 inline bool InToSpace(Object* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001160
1161 // Checks whether an address/object in the heap (including auxiliary
1162 // area and unused area).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001163 bool Contains(Address addr);
1164 bool Contains(HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165
1166 // Checks whether an address/object in a space.
ager@chromium.org3811b432009-10-28 14:53:37 +00001167 // Currently used by tests, serialization and heap verification only.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001168 bool InSpace(Address addr, AllocationSpace space);
1169 bool InSpace(HeapObject* value, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170
kasper.lund7276f142008-07-30 08:49:36 +00001171 // Finds out which space an object should get promoted to based on its type.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001172 inline OldSpace* TargetSpace(HeapObject* object);
1173 inline AllocationSpace TargetSpaceId(InstanceType type);
kasper.lund7276f142008-07-30 08:49:36 +00001174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 // Sets the stub_cache_ (only used when expanding the dictionary).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001176 void public_set_code_stubs(UnseededNumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001177 roots_[kCodeStubsRootIndex] = value;
1178 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001180 // Support for computing object sizes for old objects during GCs. Returns
1181 // a function that is guaranteed to be safe for computing object sizes in
1182 // the current GC phase.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001183 HeapObjectCallback GcSafeSizeOfOldObjectFunction() {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001184 return gc_safe_size_of_old_object_;
1185 }
1186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 // Sets the non_monomorphic_cache_ (only used when expanding the dictionary).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001188 void public_set_non_monomorphic_cache(UnseededNumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001189 roots_[kNonMonomorphicCacheRootIndex] = value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 }
1191
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001192 void public_set_empty_script(Script* script) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001193 roots_[kEmptyScriptRootIndex] = script;
1194 }
1195
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001196 void public_set_store_buffer_top(Address* top) {
1197 roots_[kStoreBufferTopRootIndex] = reinterpret_cast<Smi*>(top);
1198 }
1199
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001200 // Update the next script id.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001201 inline void SetLastScriptId(Object* last_script_id);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001202
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001203 // Generated code can embed this address to get access to the roots.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001204 Object** roots_array_start() { return roots_; }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001205
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001206 Address* store_buffer_top_address() {
1207 return reinterpret_cast<Address*>(&roots_[kStoreBufferTopRootIndex]);
1208 }
1209
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001210 // Get address of global contexts list for serialization support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001211 Object** global_contexts_list_address() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001212 return &global_contexts_list_;
1213 }
1214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001216 void Print();
1217 void PrintHandles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218
1219 // Verify the heap is in its normal state before or after a GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001220 void Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001222 void OldPointerSpaceCheckStoreBuffer();
1223 void MapSpaceCheckStoreBuffer();
1224 void LargeObjectSpaceCheckStoreBuffer();
1225
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226 // Report heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001227 void ReportHeapStatistics(const char* title);
1228 void ReportCodeStatistics(const char* title);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229
1230 // Fill in bogus values in from space
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001231 void ZapFromSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232#endif
1233
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001234 // Print short heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001235 void PrintShortHeapStatistics();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001236
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 // Makes a new symbol object
1238 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1239 // failed.
1240 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001241 MUST_USE_RESULT MaybeObject* CreateSymbol(
1242 const char* str, int length, int hash);
1243 MUST_USE_RESULT MaybeObject* CreateSymbol(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244
1245 // Write barrier support for address[offset] = o.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001246 inline void RecordWrite(Address address, int offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001248 // Write barrier support for address[start : start + len[ = o.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001249 inline void RecordWrites(Address address, int start, int len);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001250
kasper.lund7276f142008-07-30 08:49:36 +00001251 // Given an address occupied by a live code object, return that object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001252 Object* FindCodeObject(Address a);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253
1254 // Invoke Shrink on shrinkable spaces.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001255 void Shrink();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256
1257 enum HeapState { NOT_IN_GC, SCAVENGE, MARK_COMPACT };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001258 inline HeapState gc_state() { return gc_state_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001260 inline bool IsInGCPostProcessing() { return gc_post_processing_depth_ > 0; }
1261
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001263 bool IsAllocationAllowed() { return allocation_allowed_; }
1264 inline bool allow_allocation(bool enable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001266 bool disallow_allocation_failure() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 return disallow_allocation_failure_;
1268 }
1269
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001270 void TracePathToObject(Object* target);
1271 void TracePathToGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272#endif
1273
ager@chromium.org32912102009-01-16 10:38:43 +00001274 // Callback function passed to Heap::Iterate etc. Copies an object if
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 // necessary, the object might be promoted to an old space. The caller must
1276 // ensure the precondition that the object is (a) a heap object and (b) in
1277 // the heap's from space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001278 static inline void ScavengePointer(HeapObject** p);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001279 static inline void ScavengeObject(HeapObject** p, HeapObject* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001281 // Commits from space if it is uncommitted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001282 void EnsureFromSpaceIsCommitted();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001283
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001284 // Support for partial snapshots. After calling this we can allocate a
1285 // certain number of bytes using only linear allocation (with a
1286 // LinearAllocationScope and an AlwaysAllocateScope) without using freelists
1287 // or causing a GC. It returns true of space was reserved or false if a GC is
1288 // needed. For paged spaces the space requested must include the space wasted
1289 // at the end of each page when allocating linearly.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001290 void ReserveSpace(
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001291 int new_space_size,
1292 int pointer_space_size,
1293 int data_space_size,
1294 int code_space_size,
1295 int map_space_size,
1296 int cell_space_size,
1297 int large_object_size);
1298
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 //
1300 // Support for the API.
1301 //
1302
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001303 bool CreateApiObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304
1305 // Attempt to find the number in a small cache. If we finds it, return
1306 // the string representation of the number. Otherwise return undefined.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001307 Object* GetNumberStringCache(Object* number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308
1309 // Update the cache with a new number-string pair.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001310 void SetNumberStringCache(Object* number, String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311
kasper.lund7276f142008-07-30 08:49:36 +00001312 // Adjusts the amount of registered external memory.
1313 // Returns the adjusted value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001314 inline int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00001315
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001316 // Allocate uninitialized fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001317 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length);
1318 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length,
1319 PretenureFlag pretenure);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001320
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001321 inline intptr_t PromotedTotalSize() {
1322 return PromotedSpaceSize() + PromotedExternalMemorySize();
1323 }
1324
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001325 // True if we have reached the allocation limit in the old generation that
1326 // should force the next GC (caused normally) to be a full one.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001327 inline bool OldGenerationPromotionLimitReached() {
1328 return PromotedTotalSize() > old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001329 }
1330
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001331 inline intptr_t OldGenerationSpaceAvailable() {
1332 return old_gen_allocation_limit_ - PromotedTotalSize();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001333 }
1334
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001335 static const intptr_t kMinimumPromotionLimit = 5 * Page::kPageSize;
1336 static const intptr_t kMinimumAllocationLimit =
1337 8 * (Page::kPageSize > MB ? Page::kPageSize : MB);
1338
1339 // When we sweep lazily we initially guess that there is no garbage on the
1340 // heap and set the limits for the next GC accordingly. As we sweep we find
1341 // out that some of the pages contained garbage and we have to adjust
1342 // downwards the size of the heap. This means the limits that control the
1343 // timing of the next GC also need to be adjusted downwards.
1344 void LowerOldGenLimits(intptr_t adjustment) {
1345 size_of_old_gen_at_last_old_space_gc_ -= adjustment;
1346 old_gen_promotion_limit_ =
1347 OldGenPromotionLimit(size_of_old_gen_at_last_old_space_gc_);
1348 old_gen_allocation_limit_ =
1349 OldGenAllocationLimit(size_of_old_gen_at_last_old_space_gc_);
1350 }
1351
1352 intptr_t OldGenPromotionLimit(intptr_t old_gen_size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001353 const int divisor = FLAG_stress_compaction ? 10 : 3;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001354 intptr_t limit =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001355 Max(old_gen_size + old_gen_size / divisor, kMinimumPromotionLimit);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001356 limit += new_space_.Capacity();
1357 limit *= old_gen_limit_factor_;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001358 intptr_t halfway_to_the_max = (old_gen_size + max_old_generation_size_) / 2;
1359 return Min(limit, halfway_to_the_max);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001360 }
1361
1362 intptr_t OldGenAllocationLimit(intptr_t old_gen_size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001363 const int divisor = FLAG_stress_compaction ? 8 : 2;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001364 intptr_t limit =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001365 Max(old_gen_size + old_gen_size / divisor, kMinimumAllocationLimit);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001366 limit += new_space_.Capacity();
1367 limit *= old_gen_limit_factor_;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001368 intptr_t halfway_to_the_max = (old_gen_size + max_old_generation_size_) / 2;
1369 return Min(limit, halfway_to_the_max);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001370 }
1371
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001372 // Implements the corresponding V8 API function.
1373 bool IdleNotification(int hint);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001374
1375 // Declare all the root indices.
1376 enum RootListIndex {
1377#define ROOT_INDEX_DECLARATION(type, name, camel_name) k##camel_name##RootIndex,
1378 STRONG_ROOT_LIST(ROOT_INDEX_DECLARATION)
1379#undef ROOT_INDEX_DECLARATION
1380
1381// Utility type maps
1382#define DECLARE_STRUCT_MAP(NAME, Name, name) k##Name##MapRootIndex,
1383 STRUCT_LIST(DECLARE_STRUCT_MAP)
1384#undef DECLARE_STRUCT_MAP
1385
1386#define SYMBOL_INDEX_DECLARATION(name, str) k##name##RootIndex,
1387 SYMBOL_LIST(SYMBOL_INDEX_DECLARATION)
1388#undef SYMBOL_DECLARATION
1389
1390 kSymbolTableRootIndex,
1391 kStrongRootListLength = kSymbolTableRootIndex,
1392 kRootListLength
1393 };
1394
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001395 MUST_USE_RESULT MaybeObject* NumberToString(
1396 Object* number, bool check_number_string_cache = true);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001397 MUST_USE_RESULT MaybeObject* Uint32ToString(
1398 uint32_t value, bool check_number_string_cache = true);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001399
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001400 Map* MapForExternalArrayType(ExternalArrayType array_type);
1401 RootListIndex RootIndexForExternalArrayType(
ager@chromium.org3811b432009-10-28 14:53:37 +00001402 ExternalArrayType array_type);
1403
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001404 void RecordStats(HeapStats* stats, bool take_snapshot = false);
ager@chromium.org60121232009-12-03 11:25:37 +00001405
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001406 // Copy block of memory from src to dst. Size of block should be aligned
1407 // by pointer size.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001408 static inline void CopyBlock(Address dst, Address src, int byte_size);
1409
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001410 // Optimized version of memmove for blocks with pointer size aligned sizes and
1411 // pointer size aligned addresses.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001412 static inline void MoveBlock(Address dst, Address src, int byte_size);
1413
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001414 // Check new space expansion criteria and expand semispaces if it was hit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001415 void CheckNewSpaceExpansionCriteria();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001416
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001417 inline void IncrementYoungSurvivorsCounter(int survived) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001418 ASSERT(survived >= 0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001419 young_survivors_after_last_gc_ = survived;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001420 survived_since_last_expansion_ += survived;
1421 }
1422
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001423 inline bool NextGCIsLikelyToBeFull() {
1424 if (FLAG_gc_global) return true;
1425
1426 intptr_t total_promoted = PromotedTotalSize();
1427
1428 intptr_t adjusted_promotion_limit =
1429 old_gen_promotion_limit_ - new_space_.Capacity();
1430
1431 if (total_promoted >= adjusted_promotion_limit) return true;
1432
1433 intptr_t adjusted_allocation_limit =
1434 old_gen_allocation_limit_ - new_space_.Capacity() / 5;
1435
1436 if (PromotedSpaceSize() >= adjusted_allocation_limit) return true;
1437
1438 return false;
1439 }
1440
1441
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001442 void UpdateNewSpaceReferencesInExternalStringTable(
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001443 ExternalStringTableUpdaterCallback updater_func);
1444
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001445 void UpdateReferencesInExternalStringTable(
1446 ExternalStringTableUpdaterCallback updater_func);
1447
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001448 void ProcessWeakReferences(WeakObjectRetainer* retainer);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001449
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001450 void VisitExternalResources(v8::ExternalResourceVisitor* visitor);
1451
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001452 // Helper function that governs the promotion policy from new space to
1453 // old. If the object's old address lies below the new space's age
1454 // mark or if we've already filled the bottom 1/16th of the to space,
1455 // we try to promote this object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001456 inline bool ShouldBePromoted(Address old_address, int object_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001457
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001458 int MaxObjectSizeInNewSpace() { return kMaxObjectSizeInNewSpace; }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001459
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001460 void ClearJSFunctionResultCaches();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001461
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001462 void ClearNormalizedMapCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00001463
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001464 GCTracer* tracer() { return tracer_; }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001465
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001466 // Returns the size of objects residing in non new spaces.
1467 intptr_t PromotedSpaceSize();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001468 intptr_t PromotedSpaceSizeOfObjects();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001469
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001470 double total_regexp_code_generated() { return total_regexp_code_generated_; }
1471 void IncreaseTotalRegexpCodeGenerated(int size) {
1472 total_regexp_code_generated_ += size;
1473 }
1474
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001475 // Returns maximum GC pause.
1476 int get_max_gc_pause() { return max_gc_pause_; }
1477
1478 // Returns maximum size of objects alive after GC.
1479 intptr_t get_max_alive_after_gc() { return max_alive_after_gc_; }
1480
1481 // Returns minimal interval between two subsequent collections.
1482 int get_min_in_mutator() { return min_in_mutator_; }
1483
1484 MarkCompactCollector* mark_compact_collector() {
1485 return &mark_compact_collector_;
1486 }
1487
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001488 StoreBuffer* store_buffer() {
1489 return &store_buffer_;
1490 }
1491
1492 Marking* marking() {
1493 return &marking_;
1494 }
1495
1496 IncrementalMarking* incremental_marking() {
1497 return &incremental_marking_;
1498 }
1499
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001500 bool IsSweepingComplete() {
1501 return old_data_space()->IsSweepingComplete() &&
1502 old_pointer_space()->IsSweepingComplete();
1503 }
1504
1505 bool AdvanceSweepers(int step_size) {
1506 bool sweeping_complete = old_data_space()->AdvanceSweeper(step_size);
1507 sweeping_complete &= old_pointer_space()->AdvanceSweeper(step_size);
1508 return sweeping_complete;
1509 }
1510
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001511 ExternalStringTable* external_string_table() {
1512 return &external_string_table_;
1513 }
1514
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001515 // Returns the current sweep generation.
1516 int sweep_generation() {
1517 return sweep_generation_;
1518 }
1519
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001520 inline Isolate* isolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001521
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001522 inline void CallGlobalGCPrologueCallback() {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001523 if (global_gc_prologue_callback_ != NULL) global_gc_prologue_callback_();
1524 }
1525
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001526 inline void CallGlobalGCEpilogueCallback() {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001527 if (global_gc_epilogue_callback_ != NULL) global_gc_epilogue_callback_();
1528 }
1529
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001530 inline bool OldGenerationAllocationLimitReached();
1531
1532 inline void DoScavengeObject(Map* map, HeapObject** slot, HeapObject* obj) {
1533 scavenging_visitors_table_.GetVisitor(map)(map, slot, obj);
1534 }
1535
1536 void QueueMemoryChunkForFree(MemoryChunk* chunk);
1537 void FreeQueuedChunks();
1538
1539 // Completely clear the Instanceof cache (to stop it keeping objects alive
1540 // around a GC).
1541 inline void CompletelyClearInstanceofCache();
1542
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001543 // The roots that have an index less than this are always in old space.
1544 static const int kOldSpaceRoots = 0x20;
1545
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001546 bool idle_notification_will_schedule_next_gc() {
1547 return idle_notification_will_schedule_next_gc_;
1548 }
1549
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001550 uint32_t HashSeed() {
1551 uint32_t seed = static_cast<uint32_t>(hash_seed()->value());
1552 ASSERT(FLAG_randomize_hashes || seed == 0);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001553 return seed;
1554 }
1555
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001556 void SetArgumentsAdaptorDeoptPCOffset(int pc_offset) {
1557 ASSERT(arguments_adaptor_deopt_pc_offset() == Smi::FromInt(0));
1558 set_arguments_adaptor_deopt_pc_offset(Smi::FromInt(pc_offset));
1559 }
1560
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001561 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001562 Heap();
1563
1564 // This can be calculated directly from a pointer to the heap; however, it is
1565 // more expedient to get at the isolate directly from within Heap methods.
1566 Isolate* isolate_;
1567
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001568 intptr_t code_range_size_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001569 int reserved_semispace_size_;
1570 int max_semispace_size_;
1571 int initial_semispace_size_;
1572 intptr_t max_old_generation_size_;
1573 intptr_t max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001575 // For keeping track of how much data has survived
1576 // scavenge since last new space expansion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001577 int survived_since_last_expansion_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001579 // For keeping track on when to flush RegExp code.
1580 int sweep_generation_;
1581
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001582 int always_allocate_scope_depth_;
1583 int linear_allocation_scope_depth_;
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00001584
1585 // For keeping track of context disposals.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001586 int contexts_disposed_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001587
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001588 int scan_on_scavenge_pages_;
1589
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001590#if defined(V8_TARGET_ARCH_X64)
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001591 static const int kMaxObjectSizeInNewSpace = 1024*KB;
lrn@chromium.org8541d772010-12-15 12:05:09 +00001592#else
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001593 static const int kMaxObjectSizeInNewSpace = 512*KB;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001594#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001595
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001596 NewSpace new_space_;
1597 OldSpace* old_pointer_space_;
1598 OldSpace* old_data_space_;
1599 OldSpace* code_space_;
1600 MapSpace* map_space_;
1601 CellSpace* cell_space_;
1602 LargeObjectSpace* lo_space_;
1603 HeapState gc_state_;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001604 int gc_post_processing_depth_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605
kasper.lund7276f142008-07-30 08:49:36 +00001606 // Returns the amount of external memory registered since last global gc.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001607 int PromotedExternalMemorySize();
kasper.lund7276f142008-07-30 08:49:36 +00001608
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001609 int ms_count_; // how many mark-sweep collections happened
1610 unsigned int gc_count_; // how many gc happened
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001612 // Total length of the strings we failed to flatten since the last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001613 int unflattened_strings_length_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001614
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001615#define ROOT_ACCESSOR(type, name, camel_name) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001616 inline void set_##name(type* value) { \
1617 /* The deserializer makes use of the fact that these common roots are */ \
1618 /* never in new space and never on a page that is being compacted. */ \
1619 ASSERT(k##camel_name##RootIndex >= kOldSpaceRoots || !InNewSpace(value)); \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001620 roots_[k##camel_name##RootIndex] = value; \
1621 }
1622 ROOT_LIST(ROOT_ACCESSOR)
1623#undef ROOT_ACCESSOR
1624
kasper.lund7276f142008-07-30 08:49:36 +00001625#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001626 bool allocation_allowed_;
kasper.lund7276f142008-07-30 08:49:36 +00001627
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628 // If the --gc-interval flag is set to a positive value, this
1629 // variable holds the value indicating the number of allocations
1630 // remain until the next failure and garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001631 int allocation_timeout_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632
1633 // Do we expect to be able to handle allocation failure at this
1634 // time?
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001635 bool disallow_allocation_failure_;
1636
1637 HeapDebugUtils* debug_utils_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638#endif // DEBUG
1639
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001640 // Indicates that the new space should be kept small due to high promotion
1641 // rates caused by the mutator allocating a lot of long-lived objects.
1642 bool new_space_high_promotion_mode_active_;
1643
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001644 // Limit that triggers a global GC on the next (normally caused) GC. This
1645 // is checked when we have already decided to do a GC to help determine
1646 // which collector to invoke.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001647 intptr_t old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001648
1649 // Limit that triggers a global GC as soon as is reasonable. This is
1650 // checked before expanding a paged space in the old generation and on
1651 // every allocation in large object space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001652 intptr_t old_gen_allocation_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001654 // Sometimes the heuristics dictate that those limits are increased. This
1655 // variable records that fact.
1656 int old_gen_limit_factor_;
1657
1658 // Used to adjust the limits that control the timing of the next GC.
1659 intptr_t size_of_old_gen_at_last_old_space_gc_;
1660
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001661 // Limit on the amount of externally allocated memory allowed
1662 // between global GCs. If reached a global GC is forced.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001663 intptr_t external_allocation_limit_;
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001664
kasper.lund7276f142008-07-30 08:49:36 +00001665 // The amount of external memory registered through the API kept alive
1666 // by global handles
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001667 int amount_of_external_allocated_memory_;
kasper.lund7276f142008-07-30 08:49:36 +00001668
1669 // Caches the amount of external memory registered at the last global gc.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001670 int amount_of_external_allocated_memory_at_last_global_gc_;
kasper.lund7276f142008-07-30 08:49:36 +00001671
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 // Indicates that an allocation has failed in the old generation since the
1673 // last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001674 int old_gen_exhausted_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001676 Object* roots_[kRootListLength];
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001677
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001678 Object* global_contexts_list_;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001679
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001680 StoreBufferRebuilder store_buffer_rebuilder_;
1681
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001682 struct StringTypeTable {
1683 InstanceType type;
1684 int size;
1685 RootListIndex index;
1686 };
1687
1688 struct ConstantSymbolTable {
1689 const char* contents;
1690 RootListIndex index;
1691 };
1692
1693 struct StructTable {
1694 InstanceType type;
1695 int size;
1696 RootListIndex index;
1697 };
1698
1699 static const StringTypeTable string_type_table[];
1700 static const ConstantSymbolTable constant_symbol_table[];
1701 static const StructTable struct_table[];
1702
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001703 // The special hidden symbol which is an empty string, but does not match
1704 // any string when looked up in properties.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001705 String* hidden_symbol_;
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001706
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 // GC callback function, called before and after mark-compact GC.
1708 // Allocations in the callback function are disallowed.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001709 struct GCPrologueCallbackPair {
1710 GCPrologueCallbackPair(GCPrologueCallback callback, GCType gc_type)
1711 : callback(callback), gc_type(gc_type) {
1712 }
1713 bool operator==(const GCPrologueCallbackPair& pair) const {
1714 return pair.callback == callback;
1715 }
1716 GCPrologueCallback callback;
1717 GCType gc_type;
1718 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001719 List<GCPrologueCallbackPair> gc_prologue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001720
1721 struct GCEpilogueCallbackPair {
1722 GCEpilogueCallbackPair(GCEpilogueCallback callback, GCType gc_type)
1723 : callback(callback), gc_type(gc_type) {
1724 }
1725 bool operator==(const GCEpilogueCallbackPair& pair) const {
1726 return pair.callback == callback;
1727 }
1728 GCEpilogueCallback callback;
1729 GCType gc_type;
1730 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001731 List<GCEpilogueCallbackPair> gc_epilogue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001732
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001733 GCCallback global_gc_prologue_callback_;
1734 GCCallback global_gc_epilogue_callback_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001736 // Support for computing object sizes during GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001737 HeapObjectCallback gc_safe_size_of_old_object_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001738 static int GcSafeSizeOfOldObject(HeapObject* object);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001739
1740 // Update the GC state. Called from the mark-compact collector.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001741 void MarkMapPointersAsEncoded(bool encoded) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001742 ASSERT(!encoded);
1743 gc_safe_size_of_old_object_ = &GcSafeSizeOfOldObject;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001744 }
1745
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746 // Checks whether a global GC is necessary
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001747 GarbageCollector SelectGarbageCollector(AllocationSpace space,
1748 const char** reason);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749
1750 // Performs garbage collection
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001751 // Returns whether there is a chance another major GC could
1752 // collect more garbage.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001753 bool PerformGarbageCollection(GarbageCollector collector,
1754 GCTracer* tracer);
1755
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001756
1757 inline void UpdateOldSpaceLimits();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001759
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001760 // Allocate an uninitialized object in map space. The behavior is identical
1761 // to Heap::AllocateRaw(size_in_bytes, MAP_SPACE), except that (a) it doesn't
1762 // have to test the allocation space argument and (b) can reduce code size
1763 // (since both AllocateRaw and AllocateRawMap are inlined).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001764 MUST_USE_RESULT inline MaybeObject* AllocateRawMap();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001765
1766 // Allocate an uninitialized object in the global property cell space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001767 MUST_USE_RESULT inline MaybeObject* AllocateRawCell();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001768
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 // Initializes a JSObject based on its map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001770 void InitializeJSObjectFromMap(JSObject* obj,
1771 FixedArray* properties,
1772 Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001774 bool CreateInitialMaps();
1775 bool CreateInitialObjects();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001776
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001777 // These five Create*EntryStub functions are here and forced to not be inlined
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +00001778 // because of a gcc-4.4 bug that assigns wrong vtable entries.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001779 NO_INLINE(void CreateJSEntryStub());
1780 NO_INLINE(void CreateJSConstructEntryStub());
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001781
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001782 void CreateFixedStubs();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001783
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001784 MaybeObject* CreateOddball(const char* to_string,
1785 Object* to_number,
1786 byte kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001788 // Allocate a JSArray with no elements
1789 MUST_USE_RESULT MaybeObject* AllocateJSArray(
1790 ElementsKind elements_kind,
1791 PretenureFlag pretenure = NOT_TENURED);
1792
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 // Allocate empty fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001794 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedArray();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001796 // Allocate empty fixed double array.
1797 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedDoubleArray();
1798
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799 // Performs a minor collection in new generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001800 void Scavenge();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001801
1802 static String* UpdateNewSpaceReferenceInExternalStringTableEntry(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001803 Heap* heap,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001804 Object** pointer);
1805
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001806 Address DoScavenge(ObjectVisitor* scavenge_visitor, Address new_space_front);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001807 static void ScavengeStoreBufferCallback(Heap* heap,
1808 MemoryChunk* page,
1809 StoreBufferEvent event);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810
1811 // Performs a major collection in the whole heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001812 void MarkCompact(GCTracer* tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813
1814 // Code to be run before and after mark-compact.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001815 void MarkCompactPrologue();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001816
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 // Record statistics before and after garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001818 void ReportStatisticsBeforeGC();
1819 void ReportStatisticsAfterGC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001821 // Slow part of scavenge object.
1822 static void ScavengeObjectSlow(HeapObject** p, HeapObject* object);
1823
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001824 // Initializes a function with a shared part and prototype.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001825 // Note: this code was factored out of AllocateFunction such that
1826 // other parts of the VM could use it. Specifically, a function that creates
1827 // instances of type JS_FUNCTION_TYPE benefit from the use of this function.
1828 // Please note this does not perform a garbage collection.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001829 inline void InitializeFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001830 JSFunction* function,
1831 SharedFunctionInfo* shared,
1832 Object* prototype);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001833
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001834 // Total RegExp code ever generated
1835 double total_regexp_code_generated_;
1836
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001837 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001838
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001839
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001840 // Allocates a small number to string cache.
1841 MUST_USE_RESULT MaybeObject* AllocateInitialNumberStringCache();
1842 // Creates and installs the full-sized number string cache.
1843 void AllocateFullSizeNumberStringCache();
1844 // Get the length of the number to string cache based on the max semispace
1845 // size.
1846 int FullSizeNumberStringCacheLength();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001847 // Flush the number to string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001848 void FlushNumberStringCache();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001849
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001850 void UpdateSurvivalRateTrend(int start_new_space_size);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001851
1852 enum SurvivalRateTrend { INCREASING, STABLE, DECREASING, FLUCTUATING };
1853
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001854 static const int kYoungSurvivalRateHighThreshold = 90;
1855 static const int kYoungSurvivalRateLowThreshold = 10;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001856 static const int kYoungSurvivalRateAllowedDeviation = 15;
1857
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001858 int young_survivors_after_last_gc_;
1859 int high_survival_rate_period_length_;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001860 int low_survival_rate_period_length_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001861 double survival_rate_;
1862 SurvivalRateTrend previous_survival_rate_trend_;
1863 SurvivalRateTrend survival_rate_trend_;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001864
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001865 void set_survival_rate_trend(SurvivalRateTrend survival_rate_trend) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001866 ASSERT(survival_rate_trend != FLUCTUATING);
1867 previous_survival_rate_trend_ = survival_rate_trend_;
1868 survival_rate_trend_ = survival_rate_trend;
1869 }
1870
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001871 SurvivalRateTrend survival_rate_trend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001872 if (survival_rate_trend_ == STABLE) {
1873 return STABLE;
1874 } else if (previous_survival_rate_trend_ == STABLE) {
1875 return survival_rate_trend_;
1876 } else if (survival_rate_trend_ != previous_survival_rate_trend_) {
1877 return FLUCTUATING;
1878 } else {
1879 return survival_rate_trend_;
1880 }
1881 }
1882
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001883 bool IsStableOrIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001884 switch (survival_rate_trend()) {
1885 case STABLE:
1886 case INCREASING:
1887 return true;
1888 default:
1889 return false;
1890 }
1891 }
1892
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001893 bool IsStableOrDecreasingSurvivalTrend() {
1894 switch (survival_rate_trend()) {
1895 case STABLE:
1896 case DECREASING:
1897 return true;
1898 default:
1899 return false;
1900 }
1901 }
1902
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001903 bool IsIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001904 return survival_rate_trend() == INCREASING;
1905 }
1906
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001907 bool IsHighSurvivalRate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001908 return high_survival_rate_period_length_ > 0;
1909 }
1910
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001911 bool IsLowSurvivalRate() {
1912 return low_survival_rate_period_length_ > 0;
1913 }
1914
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001915 void SelectScavengingVisitorsTable();
1916
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001917 void StartIdleRound() {
1918 mark_sweeps_since_idle_round_started_ = 0;
1919 ms_count_at_last_idle_notification_ = ms_count_;
1920 }
1921
1922 void FinishIdleRound() {
1923 mark_sweeps_since_idle_round_started_ = kMaxMarkSweepsInIdleRound;
1924 scavenges_since_last_idle_round_ = 0;
1925 }
1926
1927 bool EnoughGarbageSinceLastIdleRound() {
1928 return (scavenges_since_last_idle_round_ >= kIdleScavengeThreshold);
1929 }
1930
1931 bool WorthStartingGCWhenIdle() {
1932 if (contexts_disposed_ > 0) {
1933 return true;
1934 }
1935 return incremental_marking()->WorthActivating();
1936 }
1937
1938 // Returns true if no more GC work is left.
1939 bool IdleGlobalGC();
1940
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 static const int kInitialSymbolTableSize = 2048;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001942 static const int kInitialEvalCacheSize = 64;
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001943 static const int kInitialNumberStringCacheSize = 256;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001945 // Maximum GC pause.
1946 int max_gc_pause_;
1947
1948 // Maximum size of objects alive after GC.
1949 intptr_t max_alive_after_gc_;
1950
1951 // Minimal interval between two subsequent collections.
1952 int min_in_mutator_;
1953
1954 // Size of objects alive after last GC.
1955 intptr_t alive_after_last_gc_;
1956
1957 double last_gc_end_timestamp_;
1958
1959 MarkCompactCollector mark_compact_collector_;
1960
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001961 StoreBuffer store_buffer_;
1962
1963 Marking marking_;
1964
1965 IncrementalMarking incremental_marking_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001966
1967 int number_idle_notifications_;
1968 unsigned int last_idle_notification_gc_count_;
1969 bool last_idle_notification_gc_count_init_;
1970
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001971 bool idle_notification_will_schedule_next_gc_;
1972 int mark_sweeps_since_idle_round_started_;
1973 int ms_count_at_last_idle_notification_;
1974 unsigned int gc_count_at_last_idle_gc_;
1975 int scavenges_since_last_idle_round_;
1976
1977 static const int kMaxMarkSweepsInIdleRound = 7;
1978 static const int kIdleScavengeThreshold = 5;
1979
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001980 // Shared state read by the scavenge collector and set by ScavengeObject.
1981 PromotionQueue promotion_queue_;
1982
1983 // Flag is set when the heap has been configured. The heap can be repeatedly
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001984 // configured through the API until it is set up.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001985 bool configured_;
1986
1987 ExternalStringTable external_string_table_;
1988
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001989 VisitorDispatchTable<ScavengingCallback> scavenging_visitors_table_;
1990
1991 MemoryChunk* chunks_queued_for_free_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001992
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 friend class Factory;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001994 friend class GCTracer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 friend class DisallowAllocationFailure;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001996 friend class AlwaysAllocateScope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001997 friend class LinearAllocationScope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001998 friend class Page;
1999 friend class Isolate;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002000 friend class MarkCompactCollector;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002001 friend class StaticMarkingVisitor;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002002 friend class MapCompact;
2003
2004 DISALLOW_COPY_AND_ASSIGN(Heap);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002005};
2006
2007
ager@chromium.org67368652009-12-03 11:40:49 +00002008class HeapStats {
2009 public:
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002010 static const int kStartMarker = 0xDECADE00;
2011 static const int kEndMarker = 0xDECADE01;
2012
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002013 int* start_marker; // 0
2014 int* new_space_size; // 1
2015 int* new_space_capacity; // 2
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002016 intptr_t* old_pointer_space_size; // 3
2017 intptr_t* old_pointer_space_capacity; // 4
2018 intptr_t* old_data_space_size; // 5
2019 intptr_t* old_data_space_capacity; // 6
2020 intptr_t* code_space_size; // 7
2021 intptr_t* code_space_capacity; // 8
2022 intptr_t* map_space_size; // 9
2023 intptr_t* map_space_capacity; // 10
2024 intptr_t* cell_space_size; // 11
2025 intptr_t* cell_space_capacity; // 12
2026 intptr_t* lo_space_size; // 13
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002027 int* global_handle_count; // 14
2028 int* weak_global_handle_count; // 15
2029 int* pending_global_handle_count; // 16
2030 int* near_death_global_handle_count; // 17
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002031 int* free_global_handle_count; // 18
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002032 intptr_t* memory_allocator_size; // 19
2033 intptr_t* memory_allocator_capacity; // 20
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002034 int* objects_per_type; // 21
2035 int* size_per_type; // 22
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002036 int* os_error; // 23
2037 int* end_marker; // 24
ager@chromium.org60121232009-12-03 11:25:37 +00002038};
2039
2040
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002041class AlwaysAllocateScope {
2042 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002043 inline AlwaysAllocateScope();
2044 inline ~AlwaysAllocateScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045};
2046
2047
ager@chromium.org3811b432009-10-28 14:53:37 +00002048class LinearAllocationScope {
2049 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002050 inline LinearAllocationScope();
2051 inline ~LinearAllocationScope();
ager@chromium.org3811b432009-10-28 14:53:37 +00002052};
2053
2054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055#ifdef DEBUG
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002056// Visitor class to verify interior pointers in spaces that do not contain
2057// or care about intergenerational references. All heap object pointers have to
2058// point into the heap to a location that has a map pointer at its first word.
2059// Caveat: Heap::Contains is an approximation because it can return true for
2060// objects in a heap space but above the allocation pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061class VerifyPointersVisitor: public ObjectVisitor {
2062 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002063 inline void VisitPointers(Object** start, Object** end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065#endif
2066
2067
kasper.lund7276f142008-07-30 08:49:36 +00002068// Space iterator for iterating over all spaces of the heap.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002069// Returns each space in turn, and null when it is done.
2070class AllSpaces BASE_EMBEDDED {
2071 public:
2072 Space* next();
2073 AllSpaces() { counter_ = FIRST_SPACE; }
2074 private:
2075 int counter_;
2076};
2077
2078
2079// Space iterator for iterating over all old spaces of the heap: Old pointer
2080// space, old data space and code space.
2081// Returns each space in turn, and null when it is done.
2082class OldSpaces BASE_EMBEDDED {
2083 public:
2084 OldSpace* next();
2085 OldSpaces() { counter_ = OLD_POINTER_SPACE; }
2086 private:
2087 int counter_;
2088};
2089
2090
2091// Space iterator for iterating over all the paged spaces of the heap:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002092// Map space, old pointer space, old data space, code space and cell space.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002093// Returns each space in turn, and null when it is done.
2094class PagedSpaces BASE_EMBEDDED {
2095 public:
2096 PagedSpace* next();
2097 PagedSpaces() { counter_ = OLD_POINTER_SPACE; }
2098 private:
2099 int counter_;
2100};
2101
2102
2103// Space iterator for iterating over all spaces of the heap.
kasper.lund7276f142008-07-30 08:49:36 +00002104// For each space an object iterator is provided. The deallocation of the
2105// returned object iterators is handled by the space iterator.
kasper.lund7276f142008-07-30 08:49:36 +00002106class SpaceIterator : public Malloced {
2107 public:
2108 SpaceIterator();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002109 explicit SpaceIterator(HeapObjectCallback size_func);
kasper.lund7276f142008-07-30 08:49:36 +00002110 virtual ~SpaceIterator();
2111
2112 bool has_next();
2113 ObjectIterator* next();
2114
2115 private:
2116 ObjectIterator* CreateIterator();
2117
2118 int current_space_; // from enum AllocationSpace.
2119 ObjectIterator* iterator_; // object iterator for the current space.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002120 HeapObjectCallback size_func_;
kasper.lund7276f142008-07-30 08:49:36 +00002121};
2122
2123
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002124// A HeapIterator provides iteration over the whole heap. It
2125// aggregates the specific iterators for the different spaces as
2126// these can only iterate over one space only.
2127//
2128// HeapIterator can skip free list nodes (that is, de-allocated heap
2129// objects that still remain in the heap). As implementation of free
2130// nodes filtering uses GC marks, it can't be used during MS/MC GC
2131// phases. Also, it is forbidden to interrupt iteration in this mode,
2132// as this will leave heap objects marked (and thus, unusable).
whesse@chromium.org023421e2010-12-21 12:19:12 +00002133class HeapObjectsFilter;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134
2135class HeapIterator BASE_EMBEDDED {
2136 public:
whesse@chromium.org023421e2010-12-21 12:19:12 +00002137 enum HeapObjectsFiltering {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002138 kNoFiltering,
whesse@chromium.org023421e2010-12-21 12:19:12 +00002139 kFilterUnreachable
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002140 };
2141
2142 HeapIterator();
whesse@chromium.org023421e2010-12-21 12:19:12 +00002143 explicit HeapIterator(HeapObjectsFiltering filtering);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002144 ~HeapIterator();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 HeapObject* next();
2147 void reset();
2148
2149 private:
2150 // Perform the initialization.
2151 void Init();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 // Perform all necessary shutdown (destruction) work.
2153 void Shutdown();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002154 HeapObject* NextObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155
whesse@chromium.org023421e2010-12-21 12:19:12 +00002156 HeapObjectsFiltering filtering_;
2157 HeapObjectsFilter* filter_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 // Space iterator for iterating all the spaces.
2159 SpaceIterator* space_iterator_;
2160 // Object iterator for the space currently being iterated.
2161 ObjectIterator* object_iterator_;
2162};
2163
2164
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002165// Cache for mapping (map, property name) into field offset.
2166// Cleared at startup and prior to mark sweep collection.
2167class KeyedLookupCache {
2168 public:
2169 // Lookup field offset for (map, name). If absent, -1 is returned.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002170 int Lookup(Map* map, String* name);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002171
2172 // Update an element in the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002173 void Update(Map* map, String* name, int field_offset);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002174
2175 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002176 void Clear();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002177
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002178 static const int kLength = 256;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002179 static const int kCapacityMask = kLength - 1;
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00002180 static const int kMapHashShift = 5;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002181 static const int kHashMask = -4; // Zero the last two bits.
2182 static const int kEntriesPerBucket = 4;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002183 static const int kNotFound = -1;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002184
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002185 // kEntriesPerBucket should be a power of 2.
2186 STATIC_ASSERT((kEntriesPerBucket & (kEntriesPerBucket - 1)) == 0);
2187 STATIC_ASSERT(kEntriesPerBucket == -kHashMask);
2188
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002189 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002190 KeyedLookupCache() {
2191 for (int i = 0; i < kLength; ++i) {
2192 keys_[i].map = NULL;
2193 keys_[i].name = NULL;
2194 field_offsets_[i] = kNotFound;
2195 }
2196 }
2197
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002198 static inline int Hash(Map* map, String* name);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002199
2200 // Get the address of the keys and field_offsets arrays. Used in
2201 // generated code to perform cache lookups.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002202 Address keys_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002203 return reinterpret_cast<Address>(&keys_);
2204 }
2205
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002206 Address field_offsets_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002207 return reinterpret_cast<Address>(&field_offsets_);
2208 }
2209
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002210 struct Key {
2211 Map* map;
2212 String* name;
2213 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002214
2215 Key keys_[kLength];
2216 int field_offsets_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002217
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002218 friend class ExternalReference;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002219 friend class Isolate;
2220 DISALLOW_COPY_AND_ASSIGN(KeyedLookupCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002221};
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002222
2223
2224// Cache for mapping (array, property name) into descriptor index.
2225// The cache contains both positive and negative results.
2226// Descriptor index equals kNotFound means the property is absent.
2227// Cleared at startup and prior to any gc.
2228class DescriptorLookupCache {
2229 public:
2230 // Lookup descriptor index for (map, name).
2231 // If absent, kAbsent is returned.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002232 int Lookup(DescriptorArray* array, String* name) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002233 if (!StringShape(name).IsSymbol()) return kAbsent;
2234 int index = Hash(array, name);
2235 Key& key = keys_[index];
2236 if ((key.array == array) && (key.name == name)) return results_[index];
2237 return kAbsent;
2238 }
2239
2240 // Update an element in the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002241 void Update(DescriptorArray* array, String* name, int result) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002242 ASSERT(result != kAbsent);
2243 if (StringShape(name).IsSymbol()) {
2244 int index = Hash(array, name);
2245 Key& key = keys_[index];
2246 key.array = array;
2247 key.name = name;
2248 results_[index] = result;
2249 }
2250 }
2251
2252 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002253 void Clear();
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002254
2255 static const int kAbsent = -2;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002256
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002257 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002258 DescriptorLookupCache() {
2259 for (int i = 0; i < kLength; ++i) {
2260 keys_[i].array = NULL;
2261 keys_[i].name = NULL;
2262 results_[i] = kAbsent;
2263 }
2264 }
2265
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002266 static int Hash(DescriptorArray* array, String* name) {
2267 // Uses only lower 32 bits if pointers are larger.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002268 uint32_t array_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002269 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(array)) >> 2;
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002270 uint32_t name_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002271 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name)) >> 2;
2272 return (array_hash ^ name_hash) % kLength;
2273 }
2274
2275 static const int kLength = 64;
2276 struct Key {
2277 DescriptorArray* array;
2278 String* name;
2279 };
2280
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002281 Key keys_[kLength];
2282 int results_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002283
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002284 friend class Isolate;
2285 DISALLOW_COPY_AND_ASSIGN(DescriptorLookupCache);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286};
2287
2288
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002289#ifdef DEBUG
2290class DisallowAllocationFailure {
2291 public:
2292 inline DisallowAllocationFailure();
2293 inline ~DisallowAllocationFailure();
2294
2295 private:
2296 bool old_state_;
2297};
2298#endif
2299
2300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301// A helper class to document/test C++ scopes where we do not
2302// expect a GC. Usage:
2303//
2304// /* Allocation not allowed: we cannot handle a GC in this scope. */
2305// { AssertNoAllocation nogc;
2306// ...
2307// }
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002308class AssertNoAllocation {
2309 public:
2310 inline AssertNoAllocation();
2311 inline ~AssertNoAllocation();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312
2313#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314 private:
2315 bool old_state_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316#endif
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002317};
2318
2319
2320class DisableAssertNoAllocation {
2321 public:
2322 inline DisableAssertNoAllocation();
2323 inline ~DisableAssertNoAllocation();
2324
2325#ifdef DEBUG
2326 private:
2327 bool old_state_;
2328#endif
2329};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330
kasper.lund7276f142008-07-30 08:49:36 +00002331// GCTracer collects and prints ONE line after each garbage collector
2332// invocation IFF --trace_gc is used.
2333
2334class GCTracer BASE_EMBEDDED {
2335 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002336 class Scope BASE_EMBEDDED {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002337 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002338 enum ScopeId {
2339 EXTERNAL,
2340 MC_MARK,
2341 MC_SWEEP,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002342 MC_SWEEP_NEWSPACE,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002343 MC_EVACUATE_PAGES,
2344 MC_UPDATE_NEW_TO_NEW_POINTERS,
2345 MC_UPDATE_ROOT_TO_NEW_POINTERS,
2346 MC_UPDATE_OLD_TO_NEW_POINTERS,
2347 MC_UPDATE_POINTERS_TO_EVACUATED,
2348 MC_UPDATE_POINTERS_BETWEEN_EVACUATED,
2349 MC_UPDATE_MISC_POINTERS,
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00002350 MC_FLUSH_CODE,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002351 kNumberOfScopes
2352 };
2353
2354 Scope(GCTracer* tracer, ScopeId scope)
2355 : tracer_(tracer),
2356 scope_(scope) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002357 start_time_ = OS::TimeCurrentMillis();
2358 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002359
2360 ~Scope() {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002361 ASSERT(scope_ < kNumberOfScopes); // scope_ is unsigned.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002362 tracer_->scopes_[scope_] += OS::TimeCurrentMillis() - start_time_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002363 }
kasper.lund7276f142008-07-30 08:49:36 +00002364
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002365 private:
2366 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002367 ScopeId scope_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002368 double start_time_;
2369 };
2370
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002371 explicit GCTracer(Heap* heap,
2372 const char* gc_reason,
2373 const char* collector_reason);
kasper.lund7276f142008-07-30 08:49:36 +00002374 ~GCTracer();
2375
2376 // Sets the collector.
2377 void set_collector(GarbageCollector collector) { collector_ = collector; }
2378
2379 // Sets the GC count.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002380 void set_gc_count(unsigned int count) { gc_count_ = count; }
kasper.lund7276f142008-07-30 08:49:36 +00002381
2382 // Sets the full GC count.
2383 void set_full_gc_count(int count) { full_gc_count_ = count; }
2384
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002385 void increment_promoted_objects_size(int object_size) {
2386 promoted_objects_size_ += object_size;
2387 }
2388
kasper.lund7276f142008-07-30 08:49:36 +00002389 private:
2390 // Returns a string matching the collector.
2391 const char* CollectorString();
2392
2393 // Returns size of object in heap (in MB).
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002394 inline double SizeOfHeapObjects();
kasper.lund7276f142008-07-30 08:49:36 +00002395
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00002396 // Timestamp set in the constructor.
2397 double start_time_;
2398
2399 // Size of objects in heap set in constructor.
2400 intptr_t start_object_size_;
2401
2402 // Size of memory allocated from OS set in constructor.
2403 intptr_t start_memory_size_;
2404
2405 // Type of collector.
2406 GarbageCollector collector_;
kasper.lund7276f142008-07-30 08:49:36 +00002407
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002408 // A count (including this one, e.g. the first collection is 1) of the
kasper.lund7276f142008-07-30 08:49:36 +00002409 // number of garbage collections.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002410 unsigned int gc_count_;
kasper.lund7276f142008-07-30 08:49:36 +00002411
2412 // A count (including this one) of the number of full garbage collections.
2413 int full_gc_count_;
2414
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002415 // Amounts of time spent in different scopes during GC.
2416 double scopes_[Scope::kNumberOfScopes];
2417
2418 // Total amount of space either wasted or contained in one of free lists
2419 // before the current GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002420 intptr_t in_free_list_or_wasted_before_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002421
2422 // Difference between space used in the heap at the beginning of the current
2423 // collection and the end of the previous collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002424 intptr_t allocated_since_last_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002425
2426 // Amount of time spent in mutator that is time elapsed between end of the
2427 // previous collection and the beginning of the current one.
2428 double spent_in_mutator_;
2429
2430 // Size of objects promoted during the current collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002431 intptr_t promoted_objects_size_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002432
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002433 // Incremental marking steps counters.
2434 int steps_count_;
2435 double steps_took_;
2436 double longest_step_;
2437 int steps_count_since_last_gc_;
2438 double steps_took_since_last_gc_;
2439
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002440 Heap* heap_;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002441
2442 const char* gc_reason_;
2443 const char* collector_reason_;
kasper.lund7276f142008-07-30 08:49:36 +00002444};
2445
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002446
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002447class StringSplitCache {
2448 public:
2449 static Object* Lookup(FixedArray* cache, String* string, String* pattern);
2450 static void Enter(Heap* heap,
2451 FixedArray* cache,
2452 String* string,
2453 String* pattern,
2454 FixedArray* array);
2455 static void Clear(FixedArray* cache);
2456 static const int kStringSplitCacheSize = 0x100;
2457
2458 private:
2459 static const int kArrayEntriesPerCacheEntry = 4;
2460 static const int kStringOffset = 0;
2461 static const int kPatternOffset = 1;
2462 static const int kArrayOffset = 2;
2463
2464 static MaybeObject* WrapFixedArrayInJSArray(Object* fixed_array);
2465};
2466
2467
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002468class TranscendentalCache {
2469 public:
2470 enum Type {ACOS, ASIN, ATAN, COS, EXP, LOG, SIN, TAN, kNumberOfCaches};
whesse@chromium.org023421e2010-12-21 12:19:12 +00002471 static const int kTranscendentalTypeBits = 3;
2472 STATIC_ASSERT((1 << kTranscendentalTypeBits) >= kNumberOfCaches);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002473
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002474 // Returns a heap number with f(input), where f is a math function specified
2475 // by the 'type' argument.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002476 MUST_USE_RESULT inline MaybeObject* Get(Type type, double input);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002477
2478 // The cache contains raw Object pointers. This method disposes of
2479 // them before a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002480 void Clear();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002481
2482 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002483 class SubCache {
2484 static const int kCacheSize = 512;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002485
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002486 explicit SubCache(Type t);
2487
2488 MUST_USE_RESULT inline MaybeObject* Get(double input);
2489
2490 inline double Calculate(double input);
2491
2492 struct Element {
2493 uint32_t in[2];
2494 Object* output;
2495 };
2496
2497 union Converter {
2498 double dbl;
2499 uint32_t integers[2];
2500 };
2501
2502 inline static int Hash(const Converter& c) {
2503 uint32_t hash = (c.integers[0] ^ c.integers[1]);
2504 hash ^= static_cast<int32_t>(hash) >> 16;
2505 hash ^= static_cast<int32_t>(hash) >> 8;
2506 return (hash & (kCacheSize - 1));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002507 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002508
2509 Element elements_[kCacheSize];
2510 Type type_;
2511 Isolate* isolate_;
2512
2513 // Allow access to the caches_ array as an ExternalReference.
2514 friend class ExternalReference;
2515 // Inline implementation of the cache.
2516 friend class TranscendentalCacheStub;
2517 // For evaluating value.
2518 friend class TranscendentalCache;
2519
2520 DISALLOW_COPY_AND_ASSIGN(SubCache);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002521 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002522
2523 TranscendentalCache() {
2524 for (int i = 0; i < kNumberOfCaches; ++i) caches_[i] = NULL;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002525 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002526
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002527 // Used to create an external reference.
2528 inline Address cache_array_address();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002529
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002530 // Instantiation
2531 friend class Isolate;
2532 // Inline implementation of the caching.
2533 friend class TranscendentalCacheStub;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002534 // Allow access to the caches_ array as an ExternalReference.
2535 friend class ExternalReference;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002536
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002537 SubCache* caches_[kNumberOfCaches];
2538 DISALLOW_COPY_AND_ASSIGN(TranscendentalCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002539};
2540
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002541
2542// Abstract base class for checking whether a weak object should be retained.
2543class WeakObjectRetainer {
2544 public:
2545 virtual ~WeakObjectRetainer() {}
2546
2547 // Return whether this object should be retained. If NULL is returned the
2548 // object has no references. Otherwise the address of the retained object
2549 // should be returned as in some GC situations the object has been moved.
2550 virtual Object* RetainAs(Object* object) = 0;
2551};
2552
2553
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002554// Intrusive object marking uses least significant bit of
2555// heap object's map word to mark objects.
2556// Normally all map words have least significant bit set
2557// because they contain tagged map pointer.
2558// If the bit is not set object is marked.
2559// All objects should be unmarked before resuming
2560// JavaScript execution.
2561class IntrusiveMarking {
2562 public:
2563 static bool IsMarked(HeapObject* object) {
2564 return (object->map_word().ToRawValue() & kNotMarkedBit) == 0;
2565 }
2566
2567 static void ClearMark(HeapObject* object) {
2568 uintptr_t map_word = object->map_word().ToRawValue();
2569 object->set_map_word(MapWord::FromRawValue(map_word | kNotMarkedBit));
2570 ASSERT(!IsMarked(object));
2571 }
2572
2573 static void SetMark(HeapObject* object) {
2574 uintptr_t map_word = object->map_word().ToRawValue();
2575 object->set_map_word(MapWord::FromRawValue(map_word & ~kNotMarkedBit));
2576 ASSERT(IsMarked(object));
2577 }
2578
2579 static Map* MapOfMarkedObject(HeapObject* object) {
2580 uintptr_t map_word = object->map_word().ToRawValue();
2581 return MapWord::FromRawValue(map_word | kNotMarkedBit).ToMap();
2582 }
2583
2584 static int SizeOfMarkedObject(HeapObject* object) {
2585 return object->SizeFromMap(MapOfMarkedObject(object));
2586 }
2587
2588 private:
2589 static const uintptr_t kNotMarkedBit = 0x1;
2590 STATIC_ASSERT((kHeapObjectTag & kNotMarkedBit) != 0);
2591};
2592
2593
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002594#if defined(DEBUG) || defined(LIVE_OBJECT_LIST)
2595// Helper class for tracing paths to a search target Object from all roots.
2596// The TracePathFrom() method can be used to trace paths from a specific
2597// object to the search target object.
2598class PathTracer : public ObjectVisitor {
2599 public:
2600 enum WhatToFind {
2601 FIND_ALL, // Will find all matches.
2602 FIND_FIRST // Will stop the search after first match.
2603 };
2604
2605 // For the WhatToFind arg, if FIND_FIRST is specified, tracing will stop
2606 // after the first match. If FIND_ALL is specified, then tracing will be
2607 // done for all matches.
2608 PathTracer(Object* search_target,
2609 WhatToFind what_to_find,
2610 VisitMode visit_mode)
2611 : search_target_(search_target),
2612 found_target_(false),
2613 found_target_in_trace_(false),
2614 what_to_find_(what_to_find),
2615 visit_mode_(visit_mode),
2616 object_stack_(20),
2617 no_alloc() {}
2618
2619 virtual void VisitPointers(Object** start, Object** end);
2620
2621 void Reset();
2622 void TracePathFrom(Object** root);
2623
2624 bool found() const { return found_target_; }
2625
2626 static Object* const kAnyGlobalObject;
2627
2628 protected:
2629 class MarkVisitor;
2630 class UnmarkVisitor;
2631
2632 void MarkRecursively(Object** p, MarkVisitor* mark_visitor);
2633 void UnmarkRecursively(Object** p, UnmarkVisitor* unmark_visitor);
2634 virtual void ProcessResults();
2635
2636 // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject.
2637 static const int kMarkTag = 2;
2638
2639 Object* search_target_;
2640 bool found_target_;
2641 bool found_target_in_trace_;
2642 WhatToFind what_to_find_;
2643 VisitMode visit_mode_;
2644 List<Object*> object_stack_;
2645
2646 AssertNoAllocation no_alloc; // i.e. no gc allowed.
2647
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00002648 private:
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002649 DISALLOW_IMPLICIT_CONSTRUCTORS(PathTracer);
2650};
2651#endif // DEBUG || LIVE_OBJECT_LIST
2652
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653} } // namespace v8::internal
2654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655#endif // V8_HEAP_H_