blob: 67818d261eb21722cd41b0bff7d68a3939349200 [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) \
77 V(FixedDoubleArray, empty_fixed_double_array, EmptyFixedDoubleArray) \
78 V(String, empty_string, EmptyString) \
79 V(DescriptorArray, empty_descriptor_array, EmptyDescriptorArray) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000080 V(Smi, stack_limit, StackLimit) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000081 V(Oddball, frame_alignment_marker, FrameAlignmentMarker) \
82 V(Oddball, arguments_marker, ArgumentsMarker) \
83 /* The first 32 roots above this line should be boring from a GC point of */ \
84 /* view. This means they are never in new space and never on a page that */ \
85 /* is being compacted. */ \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000086 V(FixedArray, number_string_cache, NumberStringCache) \
87 V(Object, instanceof_cache_function, InstanceofCacheFunction) \
88 V(Object, instanceof_cache_map, InstanceofCacheMap) \
89 V(Object, instanceof_cache_answer, InstanceofCacheAnswer) \
90 V(FixedArray, single_character_string_cache, SingleCharacterStringCache) \
jkummerow@chromium.org486075a2011-09-07 12:44:28 +000091 V(FixedArray, string_split_cache, StringSplitCache) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000092 V(Object, termination_exception, TerminationException) \
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000093 V(Smi, hash_seed, HashSeed) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000094 V(Map, string_map, StringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000095 V(Map, symbol_map, SymbolMap) \
karlklose@chromium.org8f806e82011-03-07 14:06:08 +000096 V(Map, cons_string_map, ConsStringMap) \
97 V(Map, cons_ascii_string_map, ConsAsciiStringMap) \
ricow@chromium.org4668a2c2011-08-29 10:41:00 +000098 V(Map, sliced_string_map, SlicedStringMap) \
99 V(Map, sliced_ascii_string_map, SlicedAsciiStringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000100 V(Map, cons_symbol_map, ConsSymbolMap) \
101 V(Map, cons_ascii_symbol_map, ConsAsciiSymbolMap) \
102 V(Map, external_symbol_map, ExternalSymbolMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000103 V(Map, external_symbol_with_ascii_data_map, ExternalSymbolWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000104 V(Map, external_ascii_symbol_map, ExternalAsciiSymbolMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000105 V(Map, external_string_map, ExternalStringMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000106 V(Map, external_string_with_ascii_data_map, ExternalStringWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000107 V(Map, external_ascii_string_map, ExternalAsciiStringMap) \
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000108 V(Map, short_external_symbol_map, ShortExternalSymbolMap) \
109 V(Map, \
110 short_external_symbol_with_ascii_data_map, \
111 ShortExternalSymbolWithAsciiDataMap) \
112 V(Map, short_external_ascii_symbol_map, ShortExternalAsciiSymbolMap) \
113 V(Map, short_external_string_map, ShortExternalStringMap) \
114 V(Map, \
115 short_external_string_with_ascii_data_map, \
116 ShortExternalStringWithAsciiDataMap) \
117 V(Map, short_external_ascii_string_map, ShortExternalAsciiStringMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000118 V(Map, undetectable_string_map, UndetectableStringMap) \
119 V(Map, undetectable_ascii_string_map, UndetectableAsciiStringMap) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000120 V(Map, external_pixel_array_map, ExternalPixelArrayMap) \
ager@chromium.org3811b432009-10-28 14:53:37 +0000121 V(Map, external_byte_array_map, ExternalByteArrayMap) \
122 V(Map, external_unsigned_byte_array_map, ExternalUnsignedByteArrayMap) \
123 V(Map, external_short_array_map, ExternalShortArrayMap) \
124 V(Map, external_unsigned_short_array_map, ExternalUnsignedShortArrayMap) \
125 V(Map, external_int_array_map, ExternalIntArrayMap) \
126 V(Map, external_unsigned_int_array_map, ExternalUnsignedIntArrayMap) \
127 V(Map, external_float_array_map, ExternalFloatArrayMap) \
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000128 V(Map, external_double_array_map, ExternalDoubleArrayMap) \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000129 V(Map, non_strict_arguments_elements_map, NonStrictArgumentsElementsMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000130 V(Map, function_context_map, FunctionContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000131 V(Map, catch_context_map, CatchContextMap) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000132 V(Map, with_context_map, WithContextMap) \
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000133 V(Map, block_context_map, BlockContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000134 V(Map, oddball_map, OddballMap) \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000135 V(Map, message_object_map, JSMessageObjectMap) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000136 V(Map, foreign_map, ForeignMap) \
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000137 V(HeapNumber, nan_value, NanValue) \
138 V(HeapNumber, infinity_value, InfinityValue) \
139 V(HeapNumber, minus_zero_value, MinusZeroValue) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000140 V(Map, neander_map, NeanderMap) \
141 V(JSObject, message_listeners, MessageListeners) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000142 V(Foreign, prototype_accessors, PrototypeAccessors) \
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000143 V(UnseededNumberDictionary, code_stubs, CodeStubs) \
144 V(UnseededNumberDictionary, non_monomorphic_cache, NonMonomorphicCache) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000145 V(PolymorphicCodeCache, polymorphic_code_cache, PolymorphicCodeCache) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000146 V(Code, js_entry_code, JsEntryCode) \
147 V(Code, js_construct_entry_code, JsConstructEntryCode) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000148 V(FixedArray, natives_source_cache, NativesSourceCache) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000149 V(Object, last_script_id, LastScriptId) \
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000150 V(Script, empty_script, EmptyScript) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000151 V(Smi, real_stack_limit, RealStackLimit) \
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000152 V(StringDictionary, intrinsic_function_names, IntrinsicFunctionNames) \
yangguo@chromium.org659ceec2012-01-26 07:37:54 +0000153 V(Smi, arguments_adaptor_deopt_pc_offset, ArgumentsAdaptorDeoptPCOffset)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000154
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155#define ROOT_LIST(V) \
156 STRONG_ROOT_LIST(V) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000157 V(SymbolTable, symbol_table, SymbolTable)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158
159#define SYMBOL_LIST(V) \
160 V(Array_symbol, "Array") \
161 V(Object_symbol, "Object") \
162 V(Proto_symbol, "__proto__") \
163 V(StringImpl_symbol, "StringImpl") \
164 V(arguments_symbol, "arguments") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000165 V(Arguments_symbol, "Arguments") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 V(call_symbol, "call") \
167 V(apply_symbol, "apply") \
168 V(caller_symbol, "caller") \
169 V(boolean_symbol, "boolean") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000170 V(Boolean_symbol, "Boolean") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 V(callee_symbol, "callee") \
172 V(constructor_symbol, "constructor") \
173 V(code_symbol, ".code") \
174 V(result_symbol, ".result") \
175 V(catch_var_symbol, ".catch-var") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 V(empty_symbol, "") \
177 V(eval_symbol, "eval") \
178 V(function_symbol, "function") \
179 V(length_symbol, "length") \
180 V(name_symbol, "name") \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000181 V(native_symbol, "native") \
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000182 V(null_symbol, "null") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 V(number_symbol, "number") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000184 V(Number_symbol, "Number") \
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000185 V(nan_symbol, "NaN") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000186 V(RegExp_symbol, "RegExp") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000187 V(source_symbol, "source") \
188 V(global_symbol, "global") \
189 V(ignore_case_symbol, "ignoreCase") \
190 V(multiline_symbol, "multiline") \
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000191 V(input_symbol, "input") \
192 V(index_symbol, "index") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000193 V(last_index_symbol, "lastIndex") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 V(object_symbol, "object") \
195 V(prototype_symbol, "prototype") \
196 V(string_symbol, "string") \
ager@chromium.org7c537e22008-10-16 08:43:32 +0000197 V(String_symbol, "String") \
198 V(Date_symbol, "Date") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 V(this_symbol, "this") \
200 V(to_string_symbol, "toString") \
201 V(char_at_symbol, "CharAt") \
202 V(undefined_symbol, "undefined") \
203 V(value_of_symbol, "valueOf") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 V(InitializeVarGlobal_symbol, "InitializeVarGlobal") \
205 V(InitializeConstGlobal_symbol, "InitializeConstGlobal") \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000206 V(KeyedLoadElementMonomorphic_symbol, \
207 "KeyedLoadElementMonomorphic") \
208 V(KeyedLoadElementPolymorphic_symbol, \
209 "KeyedLoadElementPolymorphic") \
210 V(KeyedStoreElementMonomorphic_symbol, \
211 "KeyedStoreElementMonomorphic") \
212 V(KeyedStoreElementPolymorphic_symbol, \
213 "KeyedStoreElementPolymorphic") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214 V(stack_overflow_symbol, "kStackOverflowBoilerplate") \
215 V(illegal_access_symbol, "illegal access") \
216 V(out_of_memory_symbol, "out-of-memory") \
217 V(illegal_execution_state_symbol, "illegal execution state") \
218 V(get_symbol, "get") \
219 V(set_symbol, "set") \
220 V(function_class_symbol, "Function") \
221 V(illegal_argument_symbol, "illegal argument") \
222 V(MakeReferenceError_symbol, "MakeReferenceError") \
223 V(MakeSyntaxError_symbol, "MakeSyntaxError") \
224 V(MakeTypeError_symbol, "MakeTypeError") \
225 V(invalid_lhs_in_assignment_symbol, "invalid_lhs_in_assignment") \
226 V(invalid_lhs_in_for_in_symbol, "invalid_lhs_in_for_in") \
227 V(invalid_lhs_in_postfix_op_symbol, "invalid_lhs_in_postfix_op") \
228 V(invalid_lhs_in_prefix_op_symbol, "invalid_lhs_in_prefix_op") \
229 V(illegal_return_symbol, "illegal_return") \
230 V(illegal_break_symbol, "illegal_break") \
231 V(illegal_continue_symbol, "illegal_continue") \
232 V(unknown_label_symbol, "unknown_label") \
233 V(redeclaration_symbol, "redeclaration") \
234 V(failure_symbol, "<failure>") \
235 V(space_symbol, " ") \
236 V(exec_symbol, "exec") \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000237 V(zero_symbol, "0") \
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000238 V(global_eval_symbol, "GlobalEval") \
ager@chromium.org3811b432009-10-28 14:53:37 +0000239 V(identity_hash_symbol, "v8::IdentityHash") \
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000240 V(closure_symbol, "(closure)") \
ager@chromium.org04921a82011-06-27 13:21:41 +0000241 V(use_strict, "use strict") \
242 V(dot_symbol, ".") \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000243 V(anonymous_function_symbol, "(anonymous function)") \
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000244 V(compare_ic_symbol, ".compare_ic") \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000245 V(infinity_symbol, "Infinity") \
246 V(minus_infinity_symbol, "-Infinity")
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000248// Forward declarations.
kasper.lund7276f142008-07-30 08:49:36 +0000249class GCTracer;
ager@chromium.org60121232009-12-03 11:25:37 +0000250class HeapStats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000251class Isolate;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000252class WeakObjectRetainer;
kasper.lund7276f142008-07-30 08:49:36 +0000253
254
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000255typedef String* (*ExternalStringTableUpdaterCallback)(Heap* heap,
256 Object** pointer);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000257
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000258class StoreBufferRebuilder {
259 public:
260 explicit StoreBufferRebuilder(StoreBuffer* store_buffer)
261 : store_buffer_(store_buffer) {
262 }
263
264 void Callback(MemoryChunk* page, StoreBufferEvent event);
265
266 private:
267 StoreBuffer* store_buffer_;
268
269 // We record in this variable how full the store buffer was when we started
270 // iterating over the current page, finding pointers to new space. If the
271 // store buffer overflows again we can exempt the page from the store buffer
272 // by rewinding to this point instead of having to search the store buffer.
273 Object*** start_of_current_page_;
274 // The current page we are scanning in the store buffer iterator.
275 MemoryChunk* current_page_;
276};
277
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000278
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000279
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280// The all static Heap captures the interface to the global object heap.
281// All JavaScript contexts by this process share the same object heap.
282
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000283#ifdef DEBUG
284class HeapDebugUtils;
285#endif
286
287
288// A queue of objects promoted during scavenge. Each object is accompanied
289// by it's size to avoid dereferencing a map pointer for scanning.
290class PromotionQueue {
291 public:
danno@chromium.orgc612e022011-11-10 11:38:15 +0000292 explicit PromotionQueue(Heap* heap)
293 : front_(NULL),
294 rear_(NULL),
295 limit_(NULL),
296 emergency_stack_(0),
297 heap_(heap) { }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000298
danno@chromium.orgc612e022011-11-10 11:38:15 +0000299 void Initialize();
300
301 void Destroy() {
302 ASSERT(is_empty());
303 delete emergency_stack_;
304 emergency_stack_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 }
306
danno@chromium.orgc612e022011-11-10 11:38:15 +0000307 inline void ActivateGuardIfOnTheSamePage();
308
309 Page* GetHeadPage() {
310 return Page::FromAllocationTop(reinterpret_cast<Address>(rear_));
311 }
312
313 void SetNewLimit(Address limit) {
314 if (!guard_) {
315 return;
316 }
317
318 ASSERT(GetHeadPage() == Page::FromAllocationTop(limit));
319 limit_ = reinterpret_cast<intptr_t*>(limit);
320
321 if (limit_ <= rear_) {
322 return;
323 }
324
325 RelocateQueueHead();
326 }
327
328 bool is_empty() {
329 return (front_ == rear_) &&
330 (emergency_stack_ == NULL || emergency_stack_->length() == 0);
331 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000332
333 inline void insert(HeapObject* target, int size);
334
335 void remove(HeapObject** target, int* size) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000336 ASSERT(!is_empty());
danno@chromium.orgc612e022011-11-10 11:38:15 +0000337 if (front_ == rear_) {
338 Entry e = emergency_stack_->RemoveLast();
339 *target = e.obj_;
340 *size = e.size_;
341 return;
342 }
343
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000344 if (NewSpacePage::IsAtStart(reinterpret_cast<Address>(front_))) {
345 NewSpacePage* front_page =
346 NewSpacePage::FromAddress(reinterpret_cast<Address>(front_));
347 ASSERT(!front_page->prev_page()->is_anchor());
348 front_ =
349 reinterpret_cast<intptr_t*>(front_page->prev_page()->body_limit());
350 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000351 *target = reinterpret_cast<HeapObject*>(*(--front_));
352 *size = static_cast<int>(*(--front_));
353 // Assert no underflow.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000354 SemiSpace::AssertValidRange(reinterpret_cast<Address>(rear_),
355 reinterpret_cast<Address>(front_));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000356 }
357
358 private:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000359 // The front of the queue is higher in the memory page chain than the rear.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000360 intptr_t* front_;
361 intptr_t* rear_;
danno@chromium.orgc612e022011-11-10 11:38:15 +0000362 intptr_t* limit_;
363
364 bool guard_;
365
366 static const int kEntrySizeInWords = 2;
367
368 struct Entry {
369 Entry(HeapObject* obj, int size) : obj_(obj), size_(size) { }
370
371 HeapObject* obj_;
372 int size_;
373 };
374 List<Entry>* emergency_stack_;
375
376 Heap* heap_;
377
378 void RelocateQueueHead();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000379
380 DISALLOW_COPY_AND_ASSIGN(PromotionQueue);
381};
382
383
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000384typedef void (*ScavengingCallback)(Map* map,
385 HeapObject** slot,
386 HeapObject* object);
387
388
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389// External strings table is a place where all external strings are
390// registered. We need to keep track of such strings to properly
391// finalize them.
392class ExternalStringTable {
393 public:
394 // Registers an external string.
395 inline void AddString(String* string);
396
397 inline void Iterate(ObjectVisitor* v);
398
399 // Restores internal invariant and gets rid of collected strings.
400 // Must be called after each Iterate() that modified the strings.
401 void CleanUp();
402
403 // Destroys all allocated memory.
404 void TearDown();
405
406 private:
407 ExternalStringTable() { }
408
409 friend class Heap;
410
411 inline void Verify();
412
413 inline void AddOldString(String* string);
414
415 // Notifies the table that only a prefix of the new list is valid.
416 inline void ShrinkNewStrings(int position);
417
418 // To speed up scavenge collections new space string are kept
419 // separate from old space strings.
420 List<Object*> new_space_strings_;
421 List<Object*> old_space_strings_;
422
423 Heap* heap_;
424
425 DISALLOW_COPY_AND_ASSIGN(ExternalStringTable);
426};
427
428
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000429enum ArrayStorageAllocationMode {
430 DONT_INITIALIZE_ARRAY_ELEMENTS,
431 INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE
432};
433
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434class Heap {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 public:
436 // Configure heap size before setup. Return false if the heap has been
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000437 // set up already.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000438 bool ConfigureHeap(int max_semispace_size,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000439 intptr_t max_old_gen_size,
440 intptr_t max_executable_size);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000441 bool ConfigureHeapDefault();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442
443 // Initializes the global object heap. If create_heap_objects is true,
444 // also creates the basic non-mutable objects.
445 // Returns whether it succeeded.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000446 bool SetUp(bool create_heap_objects);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447
448 // Destroys all memory allocated by the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000449 void TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000451 // Set the stack limit in the roots_ array. Some architectures generate
452 // code that looks here, because it is faster than loading from the static
453 // jslimit_/real_jslimit_ variable in the StackGuard.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000454 void SetStackLimits();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000455
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000456 // Returns whether SetUp has been called.
457 bool HasBeenSetUp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458
ager@chromium.org3811b432009-10-28 14:53:37 +0000459 // Returns the maximum amount of memory reserved for the heap. For
460 // the young generation, we reserve 4 times the amount needed for a
461 // semi space. The young generation consists of two semi spaces and
462 // we reserve twice the amount needed for those in order to ensure
463 // that new space can be aligned to its size.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000464 intptr_t MaxReserved() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000465 return 4 * reserved_semispace_size_ + max_old_generation_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000467 int MaxSemiSpaceSize() { return max_semispace_size_; }
468 int ReservedSemiSpaceSize() { return reserved_semispace_size_; }
469 int InitialSemiSpaceSize() { return initial_semispace_size_; }
470 intptr_t MaxOldGenerationSize() { return max_old_generation_size_; }
471 intptr_t MaxExecutableSize() { return max_executable_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
473 // Returns the capacity of the heap in bytes w/o growing. Heap grows when
474 // more spaces are needed until it reaches the limit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000475 intptr_t Capacity();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476
ager@chromium.org3811b432009-10-28 14:53:37 +0000477 // Returns the amount of memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000478 intptr_t CommittedMemory();
ager@chromium.org3811b432009-10-28 14:53:37 +0000479
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000480 // Returns the amount of executable memory currently committed for the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000481 intptr_t CommittedMemoryExecutable();
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000482
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 // Returns the available bytes in space w/o growing.
484 // Heap doesn't guarantee that it can allocate an object that requires
485 // all available bytes. Check MaxHeapObjectSize() instead.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000486 intptr_t Available();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000488 // Returns the maximum object size in paged space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000489 inline int MaxObjectSizeInPagedSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490
491 // Returns of size of all objects residing in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000492 intptr_t SizeOfObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000493
494 // Return the starting address and a mask for the new space. And-masking an
495 // address with the mask will result in the start address of the new space
496 // for all addresses in either semispace.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000497 Address NewSpaceStart() { return new_space_.start(); }
498 uintptr_t NewSpaceMask() { return new_space_.mask(); }
499 Address NewSpaceTop() { return new_space_.top(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000501 NewSpace* new_space() { return &new_space_; }
502 OldSpace* old_pointer_space() { return old_pointer_space_; }
503 OldSpace* old_data_space() { return old_data_space_; }
504 OldSpace* code_space() { return code_space_; }
505 MapSpace* map_space() { return map_space_; }
506 CellSpace* cell_space() { return cell_space_; }
507 LargeObjectSpace* lo_space() { return lo_space_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000509 bool always_allocate() { return always_allocate_scope_depth_ != 0; }
510 Address always_allocate_scope_depth_address() {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000511 return reinterpret_cast<Address>(&always_allocate_scope_depth_);
512 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000513 bool linear_allocation() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000514 return linear_allocation_scope_depth_ != 0;
ager@chromium.org3811b432009-10-28 14:53:37 +0000515 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000516
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000517 Address* NewSpaceAllocationTopAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000518 return new_space_.allocation_top_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000520 Address* NewSpaceAllocationLimitAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000521 return new_space_.allocation_limit_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 }
523
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000524 // Uncommit unused semi space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000525 bool UncommitFromSpace() { return new_space_.UncommitFromSpace(); }
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527 // Allocates and initializes a new JavaScript object based on a
528 // constructor.
529 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
530 // failed.
531 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 MUST_USE_RESULT MaybeObject* AllocateJSObject(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000533 JSFunction* constructor, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000535 // Allocate a JSArray with no elements
536 MUST_USE_RESULT MaybeObject* AllocateEmptyJSArray(
537 ElementsKind elements_kind,
538 PretenureFlag pretenure = NOT_TENURED) {
539 return AllocateJSArrayAndStorage(elements_kind, 0, 0,
540 DONT_INITIALIZE_ARRAY_ELEMENTS,
541 pretenure);
542 }
543
544 // Allocate a JSArray with a specified length but elements that are left
545 // uninitialized.
546 MUST_USE_RESULT MaybeObject* AllocateJSArrayAndStorage(
547 ElementsKind elements_kind,
548 int length,
549 int capacity,
550 ArrayStorageAllocationMode mode = DONT_INITIALIZE_ARRAY_ELEMENTS,
551 PretenureFlag pretenure = NOT_TENURED);
552
553 // Allocate a JSArray with no elements
554 MUST_USE_RESULT MaybeObject* AllocateJSArrayWithElements(
555 FixedArrayBase* array_base,
556 ElementsKind elements_kind,
557 PretenureFlag pretenure = NOT_TENURED);
558
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000559 // Allocates and initializes a new global object based on a constructor.
560 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
561 // failed.
562 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000563 MUST_USE_RESULT MaybeObject* AllocateGlobalObject(JSFunction* constructor);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000564
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000565 // Returns a deep copy of the JavaScript object.
566 // Properties and elements are copied too.
567 // Returns failure if allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568 MUST_USE_RESULT MaybeObject* CopyJSObject(JSObject* source);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000569
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 // Allocates the function prototype.
571 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
572 // failed.
573 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000574 MUST_USE_RESULT MaybeObject* AllocateFunctionPrototype(JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575
lrn@chromium.org34e60782011-09-15 07:25:40 +0000576 // Allocates a Harmony proxy or function proxy.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000577 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
578 // failed.
579 // Please note this does not perform a garbage collection.
580 MUST_USE_RESULT MaybeObject* AllocateJSProxy(Object* handler,
581 Object* prototype);
582
lrn@chromium.org34e60782011-09-15 07:25:40 +0000583 MUST_USE_RESULT MaybeObject* AllocateJSFunctionProxy(Object* handler,
584 Object* call_trap,
585 Object* construct_trap,
586 Object* prototype);
587
588 // Reinitialize a JSReceiver into an (empty) JS object of respective type and
589 // size, but keeping the original prototype. The receiver must have at least
590 // the size of the new object. The object is reinitialized and behaves as an
591 // object that has been freshly allocated.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000592 // Returns failure if an error occured, otherwise object.
lrn@chromium.org34e60782011-09-15 07:25:40 +0000593 MUST_USE_RESULT MaybeObject* ReinitializeJSReceiver(JSReceiver* object,
594 InstanceType type,
595 int size);
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000596
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000597 // Reinitialize an JSGlobalProxy based on a constructor. The object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 // must have the same size as objects allocated using the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000599 // constructor. The object is reinitialized and behaves as an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 // object that has been freshly allocated using the constructor.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000601 MUST_USE_RESULT MaybeObject* ReinitializeJSGlobalProxy(
602 JSFunction* constructor, JSGlobalProxy* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603
604 // Allocates and initializes a new JavaScript object based on a map.
605 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
606 // failed.
607 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000608 MUST_USE_RESULT MaybeObject* AllocateJSObjectFromMap(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000609 Map* map, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
611 // Allocates a heap object based on the map.
612 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
613 // failed.
614 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000615 MUST_USE_RESULT MaybeObject* Allocate(Map* map, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616
617 // Allocates a JS Map in the heap.
618 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
619 // failed.
620 // Please note this function does not perform a garbage collection.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000621 MUST_USE_RESULT MaybeObject* AllocateMap(
622 InstanceType instance_type,
623 int instance_size,
624 ElementsKind elements_kind = FAST_ELEMENTS);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625
626 // Allocates a partial map for bootstrapping.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000627 MUST_USE_RESULT MaybeObject* AllocatePartialMap(InstanceType instance_type,
628 int instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629
630 // Allocate a map for the specified function
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000631 MUST_USE_RESULT MaybeObject* AllocateInitialMap(JSFunction* fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000633 // Allocates an empty code cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000634 MUST_USE_RESULT MaybeObject* AllocateCodeCache();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000635
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000636 // Allocates a serialized scope info.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000637 MUST_USE_RESULT MaybeObject* AllocateScopeInfo(int length);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000638
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000639 // Allocates an empty PolymorphicCodeCache.
640 MUST_USE_RESULT MaybeObject* AllocatePolymorphicCodeCache();
641
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000642 // Allocates a pre-tenured empty AccessorPair.
643 MUST_USE_RESULT MaybeObject* AllocateAccessorPair();
644
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000645 // Clear the Instanceof cache (used when a prototype changes).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000646 inline void ClearInstanceofCache();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648 // Allocates and fully initializes a String. There are two String
649 // encodings: ASCII and two byte. One should choose between the three string
650 // allocation functions based on the encoding of the string buffer used to
651 // initialized the string.
652 // - ...FromAscii initializes the string from a buffer that is ASCII
653 // encoded (it does not check that the buffer is ASCII encoded) and the
654 // result will be ASCII encoded.
655 // - ...FromUTF8 initializes the string from a buffer that is UTF-8
656 // encoded. If the characters are all single-byte characters, the
657 // result will be ASCII encoded, otherwise it will converted to two
658 // byte.
659 // - ...FromTwoByte initializes the string from a buffer that is two-byte
660 // encoded. If the characters are all single-byte characters, the
661 // result will be converted to ASCII, otherwise it will be left as
662 // two-byte.
663 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
664 // failed.
665 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000666 MUST_USE_RESULT MaybeObject* AllocateStringFromAscii(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 Vector<const char> str,
668 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000669 MUST_USE_RESULT inline MaybeObject* AllocateStringFromUtf8(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000670 Vector<const char> str,
671 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000672 MUST_USE_RESULT MaybeObject* AllocateStringFromUtf8Slow(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 Vector<const char> str,
674 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000675 MUST_USE_RESULT MaybeObject* AllocateStringFromTwoByte(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 Vector<const uc16> str,
677 PretenureFlag pretenure = NOT_TENURED);
678
679 // Allocates a symbol in old space based on the character stream.
680 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
681 // failed.
682 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000683 MUST_USE_RESULT inline MaybeObject* AllocateSymbol(Vector<const char> str,
684 int chars,
685 uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000686
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000687 MUST_USE_RESULT inline MaybeObject* AllocateAsciiSymbol(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000688 Vector<const char> str,
689 uint32_t hash_field);
690
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000691 MUST_USE_RESULT inline MaybeObject* AllocateTwoByteSymbol(
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +0000692 Vector<const uc16> str,
693 uint32_t hash_field);
694
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000695 MUST_USE_RESULT MaybeObject* AllocateInternalSymbol(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000696 unibrow::CharacterStream* buffer, int chars, uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000697
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000698 MUST_USE_RESULT MaybeObject* AllocateExternalSymbol(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000699 Vector<const char> str,
700 int chars);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 // Allocates and partially initializes a String. There are two String
703 // encodings: ASCII and two byte. These functions allocate a string of the
704 // given length and set its map and length fields. The characters of the
705 // string are uninitialized.
706 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
707 // failed.
708 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000709 MUST_USE_RESULT MaybeObject* AllocateRawAsciiString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710 int length,
711 PretenureFlag pretenure = NOT_TENURED);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000712 MUST_USE_RESULT MaybeObject* AllocateRawTwoByteString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000713 int length,
714 PretenureFlag pretenure = NOT_TENURED);
715
716 // Computes a single character string where the character has code.
ulan@chromium.org2efb9002012-01-19 15:36:35 +0000717 // A cache is used for ASCII codes.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
719 // failed. Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000720 MUST_USE_RESULT MaybeObject* LookupSingleCharacterStringFromCode(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000721 uint16_t code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722
723 // Allocate a byte array of the specified length
724 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
725 // failed.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000726 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000727 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length,
728 PretenureFlag pretenure);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000729
730 // Allocate a non-tenured byte array of the specified length
731 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
732 // failed.
733 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000734 MUST_USE_RESULT MaybeObject* AllocateByteArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735
ager@chromium.org3811b432009-10-28 14:53:37 +0000736 // Allocates an external array of the specified length and type.
737 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
738 // failed.
739 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000740 MUST_USE_RESULT MaybeObject* AllocateExternalArray(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000741 int length,
742 ExternalArrayType array_type,
743 void* external_pointer,
744 PretenureFlag pretenure);
ager@chromium.org3811b432009-10-28 14:53:37 +0000745
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000746 // Allocate a tenured JS global property cell.
747 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
748 // failed.
749 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000750 MUST_USE_RESULT MaybeObject* AllocateJSGlobalPropertyCell(Object* value);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000751
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 // Allocates a fixed array initialized with undefined values
753 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
754 // failed.
755 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000756 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length,
757 PretenureFlag pretenure);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000758 // Allocates a fixed array initialized with undefined values
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000759 MUST_USE_RESULT MaybeObject* AllocateFixedArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000761 // Allocates an uninitialized fixed array. It must be filled by the caller.
762 //
763 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
764 // failed.
765 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000766 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedArray(int length);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000767
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000768 // Make a copy of src and return it. Returns
769 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000770 MUST_USE_RESULT inline MaybeObject* CopyFixedArray(FixedArray* src);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000771
772 // Make a copy of src, set the map, and return the copy. Returns
773 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000774 MUST_USE_RESULT MaybeObject* CopyFixedArrayWithMap(FixedArray* src, Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000775
ricow@chromium.org2c99e282011-07-28 09:15:17 +0000776 // Make a copy of src and return it. Returns
777 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
778 MUST_USE_RESULT inline MaybeObject* CopyFixedDoubleArray(
779 FixedDoubleArray* src);
780
781 // Make a copy of src, set the map, and return the copy. Returns
782 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
783 MUST_USE_RESULT MaybeObject* CopyFixedDoubleArrayWithMap(
784 FixedDoubleArray* src, Map* map);
785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786 // Allocates a fixed array initialized with the hole values.
787 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
788 // failed.
789 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000790 MUST_USE_RESULT MaybeObject* AllocateFixedArrayWithHoles(
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000791 int length,
792 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000794 MUST_USE_RESULT MaybeObject* AllocateRawFixedDoubleArray(
795 int length,
796 PretenureFlag pretenure);
797
798 // Allocates a fixed double array with uninitialized values. Returns
799 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
800 // Please note this does not perform a garbage collection.
801 MUST_USE_RESULT MaybeObject* AllocateUninitializedFixedDoubleArray(
802 int length,
803 PretenureFlag pretenure = NOT_TENURED);
804
danno@chromium.orgfa458e42012-02-01 10:48:36 +0000805 // Allocates a fixed double array with hole values. Returns
806 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
807 // Please note this does not perform a garbage collection.
808 MUST_USE_RESULT MaybeObject* AllocateFixedDoubleArrayWithHoles(
809 int length,
810 PretenureFlag pretenure = NOT_TENURED);
811
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812 // AllocateHashTable is identical to AllocateFixedArray except
813 // that the resulting object has hash_table_map as map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000814 MUST_USE_RESULT MaybeObject* AllocateHashTable(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000815 int length, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816
817 // Allocate a global (but otherwise uninitialized) context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000818 MUST_USE_RESULT MaybeObject* AllocateGlobalContext();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000819
820 // Allocate a function context.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000821 MUST_USE_RESULT MaybeObject* AllocateFunctionContext(int length,
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000822 JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000824 // Allocate a catch context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000825 MUST_USE_RESULT MaybeObject* AllocateCatchContext(JSFunction* function,
826 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000827 String* name,
828 Object* thrown_object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829 // Allocate a 'with' context.
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000830 MUST_USE_RESULT MaybeObject* AllocateWithContext(JSFunction* function,
831 Context* previous,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000832 JSObject* extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000833
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000834 // Allocate a block context.
835 MUST_USE_RESULT MaybeObject* AllocateBlockContext(JSFunction* function,
836 Context* previous,
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000837 ScopeInfo* info);
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000839 // Allocates a new utility object in the old generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000840 MUST_USE_RESULT MaybeObject* AllocateStruct(InstanceType type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000841
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000842 // Allocates a function initialized with a shared part.
843 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
844 // failed.
845 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000846 MUST_USE_RESULT MaybeObject* AllocateFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000847 Map* function_map,
848 SharedFunctionInfo* shared,
849 Object* prototype,
850 PretenureFlag pretenure = TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000852 // Arguments object size.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000853 static const int kArgumentsObjectSize =
854 JSObject::kHeaderSize + 2 * kPointerSize;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000855 // Strict mode arguments has no callee so it is smaller.
856 static const int kArgumentsObjectSizeStrict =
857 JSObject::kHeaderSize + 1 * kPointerSize;
858 // Indicies for direct access into argument objects.
859 static const int kArgumentsLengthIndex = 0;
860 // callee is only valid in non-strict mode.
861 static const int kArgumentsCalleeIndex = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862
863 // Allocates an arguments object - optionally with an elements array.
864 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
865 // failed.
866 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000867 MUST_USE_RESULT MaybeObject* AllocateArgumentsObject(
868 Object* callee, int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870 // Same as NewNumberFromDouble, but may return a preallocated/immutable
871 // number object (e.g., minus_zero_value_, nan_value_)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000872 MUST_USE_RESULT MaybeObject* NumberFromDouble(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000873 double value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
875 // Allocated a HeapNumber from value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000876 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(
lrn@chromium.org303ada72010-10-27 09:33:13 +0000877 double value,
878 PretenureFlag pretenure);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000879 // pretenure = NOT_TENURED
880 MUST_USE_RESULT MaybeObject* AllocateHeapNumber(double value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
882 // Converts an int into either a Smi or a HeapNumber object.
883 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
884 // failed.
885 // Please note this does not perform a garbage collection.
erikcorry0ad885c2011-11-21 13:51:57 +0000886 MUST_USE_RESULT inline MaybeObject* NumberFromInt32(
887 int32_t value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888
889 // Converts an int into either a Smi or a HeapNumber object.
890 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
891 // failed.
892 // Please note this does not perform a garbage collection.
erikcorry0ad885c2011-11-21 13:51:57 +0000893 MUST_USE_RESULT inline MaybeObject* NumberFromUint32(
894 uint32_t value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000896 // Allocates a new foreign object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
898 // failed.
899 // Please note this does not perform a garbage collection.
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000900 MUST_USE_RESULT MaybeObject* AllocateForeign(
901 Address address, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
903 // Allocates a new SharedFunctionInfo object.
904 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
905 // failed.
906 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000907 MUST_USE_RESULT MaybeObject* AllocateSharedFunctionInfo(Object* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000909 // Allocates a new JSMessageObject object.
910 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
911 // failed.
912 // Please note that this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000913 MUST_USE_RESULT MaybeObject* AllocateJSMessageObject(
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000914 String* type,
915 JSArray* arguments,
916 int start_position,
917 int end_position,
918 Object* script,
919 Object* stack_trace,
920 Object* stack_frames);
921
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 // Allocates a new cons string object.
923 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
924 // failed.
925 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000926 MUST_USE_RESULT MaybeObject* AllocateConsString(String* first,
927 String* second);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929 // Allocates a new sub string object which is a substring of an underlying
930 // string buffer stretching from the index start (inclusive) to the index
931 // end (exclusive).
932 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
933 // failed.
934 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000935 MUST_USE_RESULT MaybeObject* AllocateSubString(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000936 String* buffer,
937 int start,
938 int end,
939 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940
941 // Allocate a new external string object, which is backed by a string
942 // resource that resides outside the V8 heap.
943 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
944 // failed.
945 // Please note this does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000946 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromAscii(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000947 const ExternalAsciiString::Resource* resource);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000948 MUST_USE_RESULT MaybeObject* AllocateExternalStringFromTwoByte(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000949 const ExternalTwoByteString::Resource* resource);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000950
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000951 // Finalizes an external string by deleting the associated external
952 // data and clearing the resource pointer.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000953 inline void FinalizeExternalString(String* string);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000954
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000955 // Allocates an uninitialized object. The memory is non-executable if the
956 // hardware and OS allow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000957 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
958 // failed.
959 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000960 MUST_USE_RESULT inline MaybeObject* AllocateRaw(int size_in_bytes,
961 AllocationSpace space,
962 AllocationSpace retry_space);
kasper.lund7276f142008-07-30 08:49:36 +0000963
ager@chromium.org6f10e412009-02-13 10:11:16 +0000964 // Initialize a filler object to keep the ability to iterate over the heap
965 // when shortening objects.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000966 void CreateFillerObjectAt(Address addr, int size);
ager@chromium.org6f10e412009-02-13 10:11:16 +0000967
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 // Makes a new native code object
969 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000970 // failed. On success, the pointer to the Code object is stored in the
971 // self_reference. This allows generated code to reference its own Code
972 // object by containing this pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000974 MUST_USE_RESULT MaybeObject* CreateCode(const CodeDesc& desc,
975 Code::Flags flags,
976 Handle<Object> self_reference,
977 bool immovable = false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000979 MUST_USE_RESULT MaybeObject* CopyCode(Code* code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000980
981 // Copy the code and scope info part of the code object, but insert
982 // the provided data as the relocation information.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000983 MUST_USE_RESULT MaybeObject* CopyCode(Code* code, Vector<byte> reloc_info);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000984
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 // Finds the symbol for string in the symbol table.
986 // If not found, a new symbol is added to the table and returned.
987 // Returns Failure::RetryAfterGC(requested_bytes, space) if allocation
988 // failed.
989 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000990 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str);
991 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +0000992 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000993 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(const char* str) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994 return LookupSymbol(CStrVector(str));
995 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000996 MUST_USE_RESULT MaybeObject* LookupSymbol(String* str);
danno@chromium.org40cb8782011-05-25 07:58:50 +0000997 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Handle<SeqAsciiString> string,
998 int from,
999 int length);
1000
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001001 bool LookupSymbolIfExists(String* str, String** symbol);
1002 bool LookupTwoCharsSymbolIfExists(String* str, String** symbol);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003
1004 // Compute the matching symbol map for a string if possible.
1005 // NULL is returned if string is in new space or not flattened.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001006 Map* SymbolMapForString(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001008 // Tries to flatten a string before compare operation.
1009 //
1010 // Returns a failure in case it was decided that flattening was
1011 // necessary and failed. Note, if flattening is not necessary the
1012 // string might stay non-flat even when not a failure is returned.
1013 //
1014 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001015 MUST_USE_RESULT inline MaybeObject* PrepareForCompare(String* str);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001016
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 // Converts the given boolean condition to JavaScript boolean value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001018 inline Object* ToBoolean(bool condition);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019
1020 // Code that should be run before and after each GC. Includes some
1021 // reporting/verification activities when compiled with DEBUG set.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001022 void GarbageCollectionPrologue();
1023 void GarbageCollectionEpilogue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024
1025 // Performs garbage collection operation.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001026 // Returns whether there is a chance that another major GC could
1027 // collect more garbage.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001028 bool CollectGarbage(AllocationSpace space,
1029 GarbageCollector collector,
1030 const char* gc_reason,
1031 const char* collector_reason);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001032
1033 // Performs garbage collection operation.
1034 // Returns whether there is a chance that another major GC could
1035 // collect more garbage.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001036 inline bool CollectGarbage(AllocationSpace space,
1037 const char* gc_reason = NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001039 static const int kNoGCFlags = 0;
1040 static const int kMakeHeapIterableMask = 1;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001041 static const int kReduceMemoryFootprintMask = 2;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001042
1043 // Performs a full garbage collection. If (flags & kMakeHeapIterableMask) is
1044 // non-zero, then the slower precise sweeper is used, which leaves the heap
1045 // in a state where we can iterate over the heap visiting all objects.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001046 void CollectAllGarbage(int flags, const char* gc_reason = NULL);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001047
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001048 // Last hope GC, should try to squeeze as much as possible.
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001049 void CollectAllAvailableGarbage(const char* gc_reason = NULL);
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001050
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001051 // Check whether the heap is currently iterable.
1052 bool IsHeapIterable();
1053
1054 // Ensure that we have swept all spaces in such a way that we can iterate
1055 // over all objects. May cause a GC.
1056 void EnsureHeapIsIterable();
1057
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001058 // Notify the heap that a context has been disposed.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001059 int NotifyContextDisposed() { return ++contexts_disposed_; }
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 // Utility to invoke the scavenger. This is needed in test code to
1062 // ensure correct callback for weak global handles.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001063 void PerformScavenge();
1064
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001065 inline void increment_scan_on_scavenge_pages() {
1066 scan_on_scavenge_pages_++;
1067 if (FLAG_gc_verbose) {
1068 PrintF("Scan-on-scavenge pages: %d\n", scan_on_scavenge_pages_);
1069 }
1070 }
1071
1072 inline void decrement_scan_on_scavenge_pages() {
1073 scan_on_scavenge_pages_--;
1074 if (FLAG_gc_verbose) {
1075 PrintF("Scan-on-scavenge pages: %d\n", scan_on_scavenge_pages_);
1076 }
1077 }
1078
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001079 PromotionQueue* promotion_queue() { return &promotion_queue_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001081#ifdef DEBUG
1082 // Utility used with flag gc-greedy.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001083 void GarbageCollectionGreedyCheck();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001084#endif
1085
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001086 void AddGCPrologueCallback(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001087 GCEpilogueCallback callback, GCType gc_type_filter);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001088 void RemoveGCPrologueCallback(GCEpilogueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001089
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001090 void AddGCEpilogueCallback(
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001091 GCEpilogueCallback callback, GCType gc_type_filter);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001092 void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001093
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001094 void SetGlobalGCPrologueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001095 ASSERT((callback == NULL) ^ (global_gc_prologue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096 global_gc_prologue_callback_ = callback;
1097 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001098 void SetGlobalGCEpilogueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001099 ASSERT((callback == NULL) ^ (global_gc_epilogue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 global_gc_epilogue_callback_ = callback;
1101 }
1102
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001103 // Heap root getters. We have versions with and without type::cast() here.
1104 // You can't use type::cast during GC because the assert fails.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001105 // TODO(1490): Try removing the unchecked accessors, now that GC marking does
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001106 // not corrupt the map.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001107#define ROOT_ACCESSOR(type, name, camel_name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001108 type* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001109 return type::cast(roots_[k##camel_name##RootIndex]); \
1110 } \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001111 type* raw_unchecked_##name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001112 return reinterpret_cast<type*>(roots_[k##camel_name##RootIndex]); \
1113 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 ROOT_LIST(ROOT_ACCESSOR)
1115#undef ROOT_ACCESSOR
1116
1117// Utility type maps
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001118#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001119 Map* name##_map() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001120 return Map::cast(roots_[k##Name##MapRootIndex]); \
1121 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001122 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
1123#undef STRUCT_MAP_ACCESSOR
1124
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001125#define SYMBOL_ACCESSOR(name, str) String* name() { \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001126 return String::cast(roots_[k##name##RootIndex]); \
1127 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001128 SYMBOL_LIST(SYMBOL_ACCESSOR)
1129#undef SYMBOL_ACCESSOR
1130
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001131 // The hidden_symbol is special because it is the empty string, but does
1132 // not match the empty string.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001133 String* hidden_symbol() { return hidden_symbol_; }
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001134
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001135 void set_global_contexts_list(Object* object) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001136 global_contexts_list_ = object;
1137 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001138 Object* global_contexts_list() { return global_contexts_list_; }
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001139
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001140 // Number of mark-sweeps.
1141 int ms_count() { return ms_count_; }
1142
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143 // Iterates over all roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001144 void IterateRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145 // Iterates over all strong roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001146 void IterateStrongRoots(ObjectVisitor* v, VisitMode mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001147 // Iterates over all the other roots in the heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001148 void IterateWeakRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001150 // Iterate pointers to from semispace of new space found in memory interval
1151 // from start to end.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001152 void IterateAndMarkPointersToFromSpace(Address start,
1153 Address end,
1154 ObjectSlotCallback callback);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 // Returns whether the object resides in new space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001157 inline bool InNewSpace(Object* object);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001158 inline bool InNewSpace(Address addr);
1159 inline bool InNewSpacePage(Address addr);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001160 inline bool InFromSpace(Object* object);
1161 inline bool InToSpace(Object* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162
1163 // Checks whether an address/object in the heap (including auxiliary
1164 // area and unused area).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001165 bool Contains(Address addr);
1166 bool Contains(HeapObject* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167
1168 // Checks whether an address/object in a space.
ager@chromium.org3811b432009-10-28 14:53:37 +00001169 // Currently used by tests, serialization and heap verification only.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001170 bool InSpace(Address addr, AllocationSpace space);
1171 bool InSpace(HeapObject* value, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172
kasper.lund7276f142008-07-30 08:49:36 +00001173 // Finds out which space an object should get promoted to based on its type.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001174 inline OldSpace* TargetSpace(HeapObject* object);
1175 inline AllocationSpace TargetSpaceId(InstanceType type);
kasper.lund7276f142008-07-30 08:49:36 +00001176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 // Sets the stub_cache_ (only used when expanding the dictionary).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001178 void public_set_code_stubs(UnseededNumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001179 roots_[kCodeStubsRootIndex] = value;
1180 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001182 // Support for computing object sizes for old objects during GCs. Returns
1183 // a function that is guaranteed to be safe for computing object sizes in
1184 // the current GC phase.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001185 HeapObjectCallback GcSafeSizeOfOldObjectFunction() {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001186 return gc_safe_size_of_old_object_;
1187 }
1188
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189 // Sets the non_monomorphic_cache_ (only used when expanding the dictionary).
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001190 void public_set_non_monomorphic_cache(UnseededNumberDictionary* value) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001191 roots_[kNonMonomorphicCacheRootIndex] = value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 }
1193
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001194 void public_set_empty_script(Script* script) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001195 roots_[kEmptyScriptRootIndex] = script;
1196 }
1197
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001198 void public_set_store_buffer_top(Address* top) {
1199 roots_[kStoreBufferTopRootIndex] = reinterpret_cast<Smi*>(top);
1200 }
1201
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001202 // Update the next script id.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001203 inline void SetLastScriptId(Object* last_script_id);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001204
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001205 // Generated code can embed this address to get access to the roots.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001206 Object** roots_array_start() { return roots_; }
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001207
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001208 Address* store_buffer_top_address() {
1209 return reinterpret_cast<Address*>(&roots_[kStoreBufferTopRootIndex]);
1210 }
1211
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001212 // Get address of global contexts list for serialization support.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001213 Object** global_contexts_list_address() {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001214 return &global_contexts_list_;
1215 }
1216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001218 void Print();
1219 void PrintHandles();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220
1221 // Verify the heap is in its normal state before or after a GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001222 void Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001224 void OldPointerSpaceCheckStoreBuffer();
1225 void MapSpaceCheckStoreBuffer();
1226 void LargeObjectSpaceCheckStoreBuffer();
1227
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228 // Report heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001229 void ReportHeapStatistics(const char* title);
1230 void ReportCodeStatistics(const char* title);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231
1232 // Fill in bogus values in from space
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001233 void ZapFromSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234#endif
1235
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001236 // Print short heap statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001237 void PrintShortHeapStatistics();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 // Makes a new symbol object
1240 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
1241 // failed.
1242 // Please note this function does not perform a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001243 MUST_USE_RESULT MaybeObject* CreateSymbol(
1244 const char* str, int length, int hash);
1245 MUST_USE_RESULT MaybeObject* CreateSymbol(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246
1247 // Write barrier support for address[offset] = o.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001248 inline void RecordWrite(Address address, int offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001250 // Write barrier support for address[start : start + len[ = o.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001251 inline void RecordWrites(Address address, int start, int len);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001252
kasper.lund7276f142008-07-30 08:49:36 +00001253 // Given an address occupied by a live code object, return that object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001254 Object* FindCodeObject(Address a);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255
1256 // Invoke Shrink on shrinkable spaces.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001257 void Shrink();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258
1259 enum HeapState { NOT_IN_GC, SCAVENGE, MARK_COMPACT };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001260 inline HeapState gc_state() { return gc_state_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001262 inline bool IsInGCPostProcessing() { return gc_post_processing_depth_ > 0; }
1263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001265 bool IsAllocationAllowed() { return allocation_allowed_; }
1266 inline bool allow_allocation(bool enable);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001268 bool disallow_allocation_failure() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 return disallow_allocation_failure_;
1270 }
1271
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001272 void TracePathToObject(Object* target);
1273 void TracePathToGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274#endif
1275
ager@chromium.org32912102009-01-16 10:38:43 +00001276 // Callback function passed to Heap::Iterate etc. Copies an object if
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 // necessary, the object might be promoted to an old space. The caller must
1278 // ensure the precondition that the object is (a) a heap object and (b) in
1279 // the heap's from space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001280 static inline void ScavengePointer(HeapObject** p);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001281 static inline void ScavengeObject(HeapObject** p, HeapObject* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001283 // Commits from space if it is uncommitted.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001284 void EnsureFromSpaceIsCommitted();
ager@chromium.orgadd848f2009-08-13 12:44:13 +00001285
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001286 // Support for partial snapshots. After calling this we can allocate a
1287 // certain number of bytes using only linear allocation (with a
1288 // LinearAllocationScope and an AlwaysAllocateScope) without using freelists
1289 // or causing a GC. It returns true of space was reserved or false if a GC is
1290 // needed. For paged spaces the space requested must include the space wasted
1291 // at the end of each page when allocating linearly.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001292 void ReserveSpace(
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001293 int new_space_size,
1294 int pointer_space_size,
1295 int data_space_size,
1296 int code_space_size,
1297 int map_space_size,
1298 int cell_space_size,
1299 int large_object_size);
1300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 //
1302 // Support for the API.
1303 //
1304
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001305 bool CreateApiObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306
1307 // Attempt to find the number in a small cache. If we finds it, return
1308 // the string representation of the number. Otherwise return undefined.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001309 Object* GetNumberStringCache(Object* number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310
1311 // Update the cache with a new number-string pair.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001312 void SetNumberStringCache(Object* number, String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313
kasper.lund7276f142008-07-30 08:49:36 +00001314 // Adjusts the amount of registered external memory.
1315 // Returns the adjusted value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001316 inline int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00001317
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001318 // Allocate uninitialized fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001319 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length);
1320 MUST_USE_RESULT MaybeObject* AllocateRawFixedArray(int length,
1321 PretenureFlag pretenure);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001322
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001323 inline intptr_t PromotedTotalSize() {
1324 return PromotedSpaceSize() + PromotedExternalMemorySize();
1325 }
1326
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001327 // True if we have reached the allocation limit in the old generation that
1328 // should force the next GC (caused normally) to be a full one.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001329 inline bool OldGenerationPromotionLimitReached() {
1330 return PromotedTotalSize() > old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001331 }
1332
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001333 inline intptr_t OldGenerationSpaceAvailable() {
1334 return old_gen_allocation_limit_ - PromotedTotalSize();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001335 }
1336
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001337 static const intptr_t kMinimumPromotionLimit = 5 * Page::kPageSize;
1338 static const intptr_t kMinimumAllocationLimit =
1339 8 * (Page::kPageSize > MB ? Page::kPageSize : MB);
1340
1341 // When we sweep lazily we initially guess that there is no garbage on the
1342 // heap and set the limits for the next GC accordingly. As we sweep we find
1343 // out that some of the pages contained garbage and we have to adjust
1344 // downwards the size of the heap. This means the limits that control the
1345 // timing of the next GC also need to be adjusted downwards.
1346 void LowerOldGenLimits(intptr_t adjustment) {
1347 size_of_old_gen_at_last_old_space_gc_ -= adjustment;
1348 old_gen_promotion_limit_ =
1349 OldGenPromotionLimit(size_of_old_gen_at_last_old_space_gc_);
1350 old_gen_allocation_limit_ =
1351 OldGenAllocationLimit(size_of_old_gen_at_last_old_space_gc_);
1352 }
1353
1354 intptr_t OldGenPromotionLimit(intptr_t old_gen_size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001355 const int divisor = FLAG_stress_compaction ? 10 : 3;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001356 intptr_t limit =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001357 Max(old_gen_size + old_gen_size / divisor, kMinimumPromotionLimit);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001358 limit += new_space_.Capacity();
1359 limit *= old_gen_limit_factor_;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001360 intptr_t halfway_to_the_max = (old_gen_size + max_old_generation_size_) / 2;
1361 return Min(limit, halfway_to_the_max);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001362 }
1363
1364 intptr_t OldGenAllocationLimit(intptr_t old_gen_size) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001365 const int divisor = FLAG_stress_compaction ? 8 : 2;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001366 intptr_t limit =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001367 Max(old_gen_size + old_gen_size / divisor, kMinimumAllocationLimit);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001368 limit += new_space_.Capacity();
1369 limit *= old_gen_limit_factor_;
danno@chromium.orgc612e022011-11-10 11:38:15 +00001370 intptr_t halfway_to_the_max = (old_gen_size + max_old_generation_size_) / 2;
1371 return Min(limit, halfway_to_the_max);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001372 }
1373
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001374 // Implements the corresponding V8 API function.
1375 bool IdleNotification(int hint);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001376
1377 // Declare all the root indices.
1378 enum RootListIndex {
1379#define ROOT_INDEX_DECLARATION(type, name, camel_name) k##camel_name##RootIndex,
1380 STRONG_ROOT_LIST(ROOT_INDEX_DECLARATION)
1381#undef ROOT_INDEX_DECLARATION
1382
1383// Utility type maps
1384#define DECLARE_STRUCT_MAP(NAME, Name, name) k##Name##MapRootIndex,
1385 STRUCT_LIST(DECLARE_STRUCT_MAP)
1386#undef DECLARE_STRUCT_MAP
1387
1388#define SYMBOL_INDEX_DECLARATION(name, str) k##name##RootIndex,
1389 SYMBOL_LIST(SYMBOL_INDEX_DECLARATION)
1390#undef SYMBOL_DECLARATION
1391
1392 kSymbolTableRootIndex,
1393 kStrongRootListLength = kSymbolTableRootIndex,
1394 kRootListLength
1395 };
1396
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001397 MUST_USE_RESULT MaybeObject* NumberToString(
1398 Object* number, bool check_number_string_cache = true);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001399 MUST_USE_RESULT MaybeObject* Uint32ToString(
1400 uint32_t value, bool check_number_string_cache = true);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001401
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001402 Map* MapForExternalArrayType(ExternalArrayType array_type);
1403 RootListIndex RootIndexForExternalArrayType(
ager@chromium.org3811b432009-10-28 14:53:37 +00001404 ExternalArrayType array_type);
1405
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001406 void RecordStats(HeapStats* stats, bool take_snapshot = false);
ager@chromium.org60121232009-12-03 11:25:37 +00001407
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001408 // Copy block of memory from src to dst. Size of block should be aligned
1409 // by pointer size.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001410 static inline void CopyBlock(Address dst, Address src, int byte_size);
1411
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001412 // Optimized version of memmove for blocks with pointer size aligned sizes and
1413 // pointer size aligned addresses.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001414 static inline void MoveBlock(Address dst, Address src, int byte_size);
1415
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001416 // Check new space expansion criteria and expand semispaces if it was hit.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001417 void CheckNewSpaceExpansionCriteria();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001418
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001419 inline void IncrementYoungSurvivorsCounter(int survived) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001420 ASSERT(survived >= 0);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001421 young_survivors_after_last_gc_ = survived;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001422 survived_since_last_expansion_ += survived;
1423 }
1424
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001425 inline bool NextGCIsLikelyToBeFull() {
1426 if (FLAG_gc_global) return true;
1427
1428 intptr_t total_promoted = PromotedTotalSize();
1429
1430 intptr_t adjusted_promotion_limit =
1431 old_gen_promotion_limit_ - new_space_.Capacity();
1432
1433 if (total_promoted >= adjusted_promotion_limit) return true;
1434
1435 intptr_t adjusted_allocation_limit =
1436 old_gen_allocation_limit_ - new_space_.Capacity() / 5;
1437
1438 if (PromotedSpaceSize() >= adjusted_allocation_limit) return true;
1439
1440 return false;
1441 }
1442
1443
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001444 void UpdateNewSpaceReferencesInExternalStringTable(
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001445 ExternalStringTableUpdaterCallback updater_func);
1446
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001447 void UpdateReferencesInExternalStringTable(
1448 ExternalStringTableUpdaterCallback updater_func);
1449
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001450 void ProcessWeakReferences(WeakObjectRetainer* retainer);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001451
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001452 void VisitExternalResources(v8::ExternalResourceVisitor* visitor);
1453
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001454 // Helper function that governs the promotion policy from new space to
1455 // old. If the object's old address lies below the new space's age
1456 // mark or if we've already filled the bottom 1/16th of the to space,
1457 // we try to promote this object.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001458 inline bool ShouldBePromoted(Address old_address, int object_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001459
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001460 int MaxObjectSizeInNewSpace() { return kMaxObjectSizeInNewSpace; }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001461
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001462 void ClearJSFunctionResultCaches();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001463
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001464 void ClearNormalizedMapCaches();
ricow@chromium.org65fae842010-08-25 15:26:24 +00001465
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001466 GCTracer* tracer() { return tracer_; }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001467
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001468 // Returns the size of objects residing in non new spaces.
1469 intptr_t PromotedSpaceSize();
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001470 intptr_t PromotedSpaceSizeOfObjects();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001471
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001472 double total_regexp_code_generated() { return total_regexp_code_generated_; }
1473 void IncreaseTotalRegexpCodeGenerated(int size) {
1474 total_regexp_code_generated_ += size;
1475 }
1476
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001477 // Returns maximum GC pause.
1478 int get_max_gc_pause() { return max_gc_pause_; }
1479
1480 // Returns maximum size of objects alive after GC.
1481 intptr_t get_max_alive_after_gc() { return max_alive_after_gc_; }
1482
1483 // Returns minimal interval between two subsequent collections.
1484 int get_min_in_mutator() { return min_in_mutator_; }
1485
1486 MarkCompactCollector* mark_compact_collector() {
1487 return &mark_compact_collector_;
1488 }
1489
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001490 StoreBuffer* store_buffer() {
1491 return &store_buffer_;
1492 }
1493
1494 Marking* marking() {
1495 return &marking_;
1496 }
1497
1498 IncrementalMarking* incremental_marking() {
1499 return &incremental_marking_;
1500 }
1501
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001502 bool IsSweepingComplete() {
1503 return old_data_space()->IsSweepingComplete() &&
1504 old_pointer_space()->IsSweepingComplete();
1505 }
1506
1507 bool AdvanceSweepers(int step_size) {
1508 bool sweeping_complete = old_data_space()->AdvanceSweeper(step_size);
1509 sweeping_complete &= old_pointer_space()->AdvanceSweeper(step_size);
1510 return sweeping_complete;
1511 }
1512
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001513 ExternalStringTable* external_string_table() {
1514 return &external_string_table_;
1515 }
1516
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001517 // Returns the current sweep generation.
1518 int sweep_generation() {
1519 return sweep_generation_;
1520 }
1521
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001522 inline Isolate* isolate();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001523
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001524 inline void CallGlobalGCPrologueCallback() {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001525 if (global_gc_prologue_callback_ != NULL) global_gc_prologue_callback_();
1526 }
1527
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001528 inline void CallGlobalGCEpilogueCallback() {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001529 if (global_gc_epilogue_callback_ != NULL) global_gc_epilogue_callback_();
1530 }
1531
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001532 inline bool OldGenerationAllocationLimitReached();
1533
1534 inline void DoScavengeObject(Map* map, HeapObject** slot, HeapObject* obj) {
1535 scavenging_visitors_table_.GetVisitor(map)(map, slot, obj);
1536 }
1537
1538 void QueueMemoryChunkForFree(MemoryChunk* chunk);
1539 void FreeQueuedChunks();
1540
1541 // Completely clear the Instanceof cache (to stop it keeping objects alive
1542 // around a GC).
1543 inline void CompletelyClearInstanceofCache();
1544
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001545 // The roots that have an index less than this are always in old space.
1546 static const int kOldSpaceRoots = 0x20;
1547
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001548 bool idle_notification_will_schedule_next_gc() {
1549 return idle_notification_will_schedule_next_gc_;
1550 }
1551
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001552 uint32_t HashSeed() {
1553 uint32_t seed = static_cast<uint32_t>(hash_seed()->value());
1554 ASSERT(FLAG_randomize_hashes || seed == 0);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00001555 return seed;
1556 }
1557
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001558 void SetArgumentsAdaptorDeoptPCOffset(int pc_offset) {
1559 ASSERT(arguments_adaptor_deopt_pc_offset() == Smi::FromInt(0));
1560 set_arguments_adaptor_deopt_pc_offset(Smi::FromInt(pc_offset));
1561 }
1562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001564 Heap();
1565
1566 // This can be calculated directly from a pointer to the heap; however, it is
1567 // more expedient to get at the isolate directly from within Heap methods.
1568 Isolate* isolate_;
1569
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001570 intptr_t code_range_size_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001571 int reserved_semispace_size_;
1572 int max_semispace_size_;
1573 int initial_semispace_size_;
1574 intptr_t max_old_generation_size_;
1575 intptr_t max_executable_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001577 // For keeping track of how much data has survived
1578 // scavenge since last new space expansion.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001579 int survived_since_last_expansion_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001581 // For keeping track on when to flush RegExp code.
1582 int sweep_generation_;
1583
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001584 int always_allocate_scope_depth_;
1585 int linear_allocation_scope_depth_;
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00001586
1587 // For keeping track of context disposals.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001588 int contexts_disposed_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001589
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001590 int scan_on_scavenge_pages_;
1591
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001592#if defined(V8_TARGET_ARCH_X64)
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001593 static const int kMaxObjectSizeInNewSpace = 1024*KB;
lrn@chromium.org8541d772010-12-15 12:05:09 +00001594#else
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001595 static const int kMaxObjectSizeInNewSpace = 512*KB;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001596#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001597
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001598 NewSpace new_space_;
1599 OldSpace* old_pointer_space_;
1600 OldSpace* old_data_space_;
1601 OldSpace* code_space_;
1602 MapSpace* map_space_;
1603 CellSpace* cell_space_;
1604 LargeObjectSpace* lo_space_;
1605 HeapState gc_state_;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001606 int gc_post_processing_depth_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607
kasper.lund7276f142008-07-30 08:49:36 +00001608 // Returns the amount of external memory registered since last global gc.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001609 int PromotedExternalMemorySize();
kasper.lund7276f142008-07-30 08:49:36 +00001610
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001611 int ms_count_; // how many mark-sweep collections happened
1612 unsigned int gc_count_; // how many gc happened
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001614 // Total length of the strings we failed to flatten since the last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001615 int unflattened_strings_length_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001616
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001617#define ROOT_ACCESSOR(type, name, camel_name) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001618 inline void set_##name(type* value) { \
1619 /* The deserializer makes use of the fact that these common roots are */ \
1620 /* never in new space and never on a page that is being compacted. */ \
1621 ASSERT(k##camel_name##RootIndex >= kOldSpaceRoots || !InNewSpace(value)); \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001622 roots_[k##camel_name##RootIndex] = value; \
1623 }
1624 ROOT_LIST(ROOT_ACCESSOR)
1625#undef ROOT_ACCESSOR
1626
kasper.lund7276f142008-07-30 08:49:36 +00001627#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001628 bool allocation_allowed_;
kasper.lund7276f142008-07-30 08:49:36 +00001629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001630 // If the --gc-interval flag is set to a positive value, this
1631 // variable holds the value indicating the number of allocations
1632 // remain until the next failure and garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001633 int allocation_timeout_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634
1635 // Do we expect to be able to handle allocation failure at this
1636 // time?
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001637 bool disallow_allocation_failure_;
1638
1639 HeapDebugUtils* debug_utils_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640#endif // DEBUG
1641
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001642 // Indicates that the new space should be kept small due to high promotion
1643 // rates caused by the mutator allocating a lot of long-lived objects.
1644 bool new_space_high_promotion_mode_active_;
1645
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001646 // Limit that triggers a global GC on the next (normally caused) GC. This
1647 // is checked when we have already decided to do a GC to help determine
1648 // which collector to invoke.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001649 intptr_t old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001650
1651 // Limit that triggers a global GC as soon as is reasonable. This is
1652 // checked before expanding a paged space in the old generation and on
1653 // every allocation in large object space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001654 intptr_t old_gen_allocation_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001656 // Sometimes the heuristics dictate that those limits are increased. This
1657 // variable records that fact.
1658 int old_gen_limit_factor_;
1659
1660 // Used to adjust the limits that control the timing of the next GC.
1661 intptr_t size_of_old_gen_at_last_old_space_gc_;
1662
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001663 // Limit on the amount of externally allocated memory allowed
1664 // between global GCs. If reached a global GC is forced.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001665 intptr_t external_allocation_limit_;
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001666
kasper.lund7276f142008-07-30 08:49:36 +00001667 // The amount of external memory registered through the API kept alive
1668 // by global handles
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001669 int amount_of_external_allocated_memory_;
kasper.lund7276f142008-07-30 08:49:36 +00001670
1671 // Caches the amount of external memory registered at the last global gc.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001672 int amount_of_external_allocated_memory_at_last_global_gc_;
kasper.lund7276f142008-07-30 08:49:36 +00001673
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674 // Indicates that an allocation has failed in the old generation since the
1675 // last GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001676 int old_gen_exhausted_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001678 Object* roots_[kRootListLength];
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001679
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001680 Object* global_contexts_list_;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001681
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001682 StoreBufferRebuilder store_buffer_rebuilder_;
1683
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001684 struct StringTypeTable {
1685 InstanceType type;
1686 int size;
1687 RootListIndex index;
1688 };
1689
1690 struct ConstantSymbolTable {
1691 const char* contents;
1692 RootListIndex index;
1693 };
1694
1695 struct StructTable {
1696 InstanceType type;
1697 int size;
1698 RootListIndex index;
1699 };
1700
1701 static const StringTypeTable string_type_table[];
1702 static const ConstantSymbolTable constant_symbol_table[];
1703 static const StructTable struct_table[];
1704
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001705 // The special hidden symbol which is an empty string, but does not match
1706 // any string when looked up in properties.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001707 String* hidden_symbol_;
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001708
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 // GC callback function, called before and after mark-compact GC.
1710 // Allocations in the callback function are disallowed.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001711 struct GCPrologueCallbackPair {
1712 GCPrologueCallbackPair(GCPrologueCallback callback, GCType gc_type)
1713 : callback(callback), gc_type(gc_type) {
1714 }
1715 bool operator==(const GCPrologueCallbackPair& pair) const {
1716 return pair.callback == callback;
1717 }
1718 GCPrologueCallback callback;
1719 GCType gc_type;
1720 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001721 List<GCPrologueCallbackPair> gc_prologue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001722
1723 struct GCEpilogueCallbackPair {
1724 GCEpilogueCallbackPair(GCEpilogueCallback callback, GCType gc_type)
1725 : callback(callback), gc_type(gc_type) {
1726 }
1727 bool operator==(const GCEpilogueCallbackPair& pair) const {
1728 return pair.callback == callback;
1729 }
1730 GCEpilogueCallback callback;
1731 GCType gc_type;
1732 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001733 List<GCEpilogueCallbackPair> gc_epilogue_callbacks_;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001734
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001735 GCCallback global_gc_prologue_callback_;
1736 GCCallback global_gc_epilogue_callback_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001738 // Support for computing object sizes during GC.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001739 HeapObjectCallback gc_safe_size_of_old_object_;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001740 static int GcSafeSizeOfOldObject(HeapObject* object);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001741
1742 // Update the GC state. Called from the mark-compact collector.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001743 void MarkMapPointersAsEncoded(bool encoded) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001744 ASSERT(!encoded);
1745 gc_safe_size_of_old_object_ = &GcSafeSizeOfOldObject;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001746 }
1747
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748 // Checks whether a global GC is necessary
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001749 GarbageCollector SelectGarbageCollector(AllocationSpace space,
1750 const char** reason);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751
1752 // Performs garbage collection
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001753 // Returns whether there is a chance another major GC could
1754 // collect more garbage.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001755 bool PerformGarbageCollection(GarbageCollector collector,
1756 GCTracer* tracer);
1757
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001758
1759 inline void UpdateOldSpaceLimits();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001761
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001762 // Allocate an uninitialized object in map space. The behavior is identical
1763 // to Heap::AllocateRaw(size_in_bytes, MAP_SPACE), except that (a) it doesn't
1764 // have to test the allocation space argument and (b) can reduce code size
1765 // (since both AllocateRaw and AllocateRawMap are inlined).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001766 MUST_USE_RESULT inline MaybeObject* AllocateRawMap();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001767
1768 // Allocate an uninitialized object in the global property cell space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001769 MUST_USE_RESULT inline MaybeObject* AllocateRawCell();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 // Initializes a JSObject based on its map.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001772 void InitializeJSObjectFromMap(JSObject* obj,
1773 FixedArray* properties,
1774 Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001776 bool CreateInitialMaps();
1777 bool CreateInitialObjects();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001778
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001779 // These five Create*EntryStub functions are here and forced to not be inlined
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +00001780 // because of a gcc-4.4 bug that assigns wrong vtable entries.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001781 NO_INLINE(void CreateJSEntryStub());
1782 NO_INLINE(void CreateJSConstructEntryStub());
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001783
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001784 void CreateFixedStubs();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001785
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001786 MaybeObject* CreateOddball(const char* to_string,
1787 Object* to_number,
1788 byte kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001790 // Allocate a JSArray with no elements
1791 MUST_USE_RESULT MaybeObject* AllocateJSArray(
1792 ElementsKind elements_kind,
1793 PretenureFlag pretenure = NOT_TENURED);
1794
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 // Allocate empty fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001796 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedArray();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001798 // Allocate empty fixed double array.
1799 MUST_USE_RESULT MaybeObject* AllocateEmptyFixedDoubleArray();
1800
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 // Performs a minor collection in new generation.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001802 void Scavenge();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001803
1804 static String* UpdateNewSpaceReferenceInExternalStringTableEntry(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001805 Heap* heap,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001806 Object** pointer);
1807
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001808 Address DoScavenge(ObjectVisitor* scavenge_visitor, Address new_space_front);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001809 static void ScavengeStoreBufferCallback(Heap* heap,
1810 MemoryChunk* page,
1811 StoreBufferEvent event);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812
1813 // Performs a major collection in the whole heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001814 void MarkCompact(GCTracer* tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815
1816 // Code to be run before and after mark-compact.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001817 void MarkCompactPrologue();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001818
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 // Record statistics before and after garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001820 void ReportStatisticsBeforeGC();
1821 void ReportStatisticsAfterGC();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001823 // Slow part of scavenge object.
1824 static void ScavengeObjectSlow(HeapObject** p, HeapObject* object);
1825
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001826 // Initializes a function with a shared part and prototype.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001827 // Note: this code was factored out of AllocateFunction such that
1828 // other parts of the VM could use it. Specifically, a function that creates
1829 // instances of type JS_FUNCTION_TYPE benefit from the use of this function.
1830 // Please note this does not perform a garbage collection.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001831 inline void InitializeFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001832 JSFunction* function,
1833 SharedFunctionInfo* shared,
1834 Object* prototype);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001835
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001836 // Total RegExp code ever generated
1837 double total_regexp_code_generated_;
1838
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001839 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001840
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001841
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001842 // Allocates a small number to string cache.
1843 MUST_USE_RESULT MaybeObject* AllocateInitialNumberStringCache();
1844 // Creates and installs the full-sized number string cache.
1845 void AllocateFullSizeNumberStringCache();
1846 // Get the length of the number to string cache based on the max semispace
1847 // size.
1848 int FullSizeNumberStringCacheLength();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001849 // Flush the number to string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001850 void FlushNumberStringCache();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001851
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001852 void UpdateSurvivalRateTrend(int start_new_space_size);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001853
1854 enum SurvivalRateTrend { INCREASING, STABLE, DECREASING, FLUCTUATING };
1855
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001856 static const int kYoungSurvivalRateHighThreshold = 90;
1857 static const int kYoungSurvivalRateLowThreshold = 10;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001858 static const int kYoungSurvivalRateAllowedDeviation = 15;
1859
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001860 int young_survivors_after_last_gc_;
1861 int high_survival_rate_period_length_;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001862 int low_survival_rate_period_length_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001863 double survival_rate_;
1864 SurvivalRateTrend previous_survival_rate_trend_;
1865 SurvivalRateTrend survival_rate_trend_;
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001866
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001867 void set_survival_rate_trend(SurvivalRateTrend survival_rate_trend) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001868 ASSERT(survival_rate_trend != FLUCTUATING);
1869 previous_survival_rate_trend_ = survival_rate_trend_;
1870 survival_rate_trend_ = survival_rate_trend;
1871 }
1872
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001873 SurvivalRateTrend survival_rate_trend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001874 if (survival_rate_trend_ == STABLE) {
1875 return STABLE;
1876 } else if (previous_survival_rate_trend_ == STABLE) {
1877 return survival_rate_trend_;
1878 } else if (survival_rate_trend_ != previous_survival_rate_trend_) {
1879 return FLUCTUATING;
1880 } else {
1881 return survival_rate_trend_;
1882 }
1883 }
1884
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001885 bool IsStableOrIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001886 switch (survival_rate_trend()) {
1887 case STABLE:
1888 case INCREASING:
1889 return true;
1890 default:
1891 return false;
1892 }
1893 }
1894
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001895 bool IsStableOrDecreasingSurvivalTrend() {
1896 switch (survival_rate_trend()) {
1897 case STABLE:
1898 case DECREASING:
1899 return true;
1900 default:
1901 return false;
1902 }
1903 }
1904
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001905 bool IsIncreasingSurvivalTrend() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001906 return survival_rate_trend() == INCREASING;
1907 }
1908
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001909 bool IsHighSurvivalRate() {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001910 return high_survival_rate_period_length_ > 0;
1911 }
1912
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00001913 bool IsLowSurvivalRate() {
1914 return low_survival_rate_period_length_ > 0;
1915 }
1916
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001917 void SelectScavengingVisitorsTable();
1918
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001919 void StartIdleRound() {
1920 mark_sweeps_since_idle_round_started_ = 0;
1921 ms_count_at_last_idle_notification_ = ms_count_;
1922 }
1923
1924 void FinishIdleRound() {
1925 mark_sweeps_since_idle_round_started_ = kMaxMarkSweepsInIdleRound;
1926 scavenges_since_last_idle_round_ = 0;
1927 }
1928
1929 bool EnoughGarbageSinceLastIdleRound() {
1930 return (scavenges_since_last_idle_round_ >= kIdleScavengeThreshold);
1931 }
1932
1933 bool WorthStartingGCWhenIdle() {
1934 if (contexts_disposed_ > 0) {
1935 return true;
1936 }
1937 return incremental_marking()->WorthActivating();
1938 }
1939
1940 // Returns true if no more GC work is left.
1941 bool IdleGlobalGC();
1942
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 static const int kInitialSymbolTableSize = 2048;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001944 static const int kInitialEvalCacheSize = 64;
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001945 static const int kInitialNumberStringCacheSize = 256;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001947 // Maximum GC pause.
1948 int max_gc_pause_;
1949
1950 // Maximum size of objects alive after GC.
1951 intptr_t max_alive_after_gc_;
1952
1953 // Minimal interval between two subsequent collections.
1954 int min_in_mutator_;
1955
1956 // Size of objects alive after last GC.
1957 intptr_t alive_after_last_gc_;
1958
1959 double last_gc_end_timestamp_;
1960
1961 MarkCompactCollector mark_compact_collector_;
1962
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001963 StoreBuffer store_buffer_;
1964
1965 Marking marking_;
1966
1967 IncrementalMarking incremental_marking_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001968
1969 int number_idle_notifications_;
1970 unsigned int last_idle_notification_gc_count_;
1971 bool last_idle_notification_gc_count_init_;
1972
svenpanne@chromium.orgecb9dd62011-12-01 08:22:35 +00001973 bool idle_notification_will_schedule_next_gc_;
1974 int mark_sweeps_since_idle_round_started_;
1975 int ms_count_at_last_idle_notification_;
1976 unsigned int gc_count_at_last_idle_gc_;
1977 int scavenges_since_last_idle_round_;
1978
1979 static const int kMaxMarkSweepsInIdleRound = 7;
1980 static const int kIdleScavengeThreshold = 5;
1981
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001982 // Shared state read by the scavenge collector and set by ScavengeObject.
1983 PromotionQueue promotion_queue_;
1984
1985 // Flag is set when the heap has been configured. The heap can be repeatedly
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001986 // configured through the API until it is set up.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001987 bool configured_;
1988
1989 ExternalStringTable external_string_table_;
1990
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001991 VisitorDispatchTable<ScavengingCallback> scavenging_visitors_table_;
1992
1993 MemoryChunk* chunks_queued_for_free_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 friend class Factory;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001996 friend class GCTracer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997 friend class DisallowAllocationFailure;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001998 friend class AlwaysAllocateScope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001999 friend class LinearAllocationScope;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002000 friend class Page;
2001 friend class Isolate;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002002 friend class MarkCompactCollector;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002003 friend class StaticMarkingVisitor;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002004 friend class MapCompact;
2005
2006 DISALLOW_COPY_AND_ASSIGN(Heap);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002007};
2008
2009
ager@chromium.org67368652009-12-03 11:40:49 +00002010class HeapStats {
2011 public:
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002012 static const int kStartMarker = 0xDECADE00;
2013 static const int kEndMarker = 0xDECADE01;
2014
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002015 int* start_marker; // 0
2016 int* new_space_size; // 1
2017 int* new_space_capacity; // 2
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002018 intptr_t* old_pointer_space_size; // 3
2019 intptr_t* old_pointer_space_capacity; // 4
2020 intptr_t* old_data_space_size; // 5
2021 intptr_t* old_data_space_capacity; // 6
2022 intptr_t* code_space_size; // 7
2023 intptr_t* code_space_capacity; // 8
2024 intptr_t* map_space_size; // 9
2025 intptr_t* map_space_capacity; // 10
2026 intptr_t* cell_space_size; // 11
2027 intptr_t* cell_space_capacity; // 12
2028 intptr_t* lo_space_size; // 13
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002029 int* global_handle_count; // 14
2030 int* weak_global_handle_count; // 15
2031 int* pending_global_handle_count; // 16
2032 int* near_death_global_handle_count; // 17
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002033 int* free_global_handle_count; // 18
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002034 intptr_t* memory_allocator_size; // 19
2035 intptr_t* memory_allocator_capacity; // 20
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00002036 int* objects_per_type; // 21
2037 int* size_per_type; // 22
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002038 int* os_error; // 23
2039 int* end_marker; // 24
ager@chromium.org60121232009-12-03 11:25:37 +00002040};
2041
2042
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002043class AlwaysAllocateScope {
2044 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002045 inline AlwaysAllocateScope();
2046 inline ~AlwaysAllocateScope();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047};
2048
2049
ager@chromium.org3811b432009-10-28 14:53:37 +00002050class LinearAllocationScope {
2051 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002052 inline LinearAllocationScope();
2053 inline ~LinearAllocationScope();
ager@chromium.org3811b432009-10-28 14:53:37 +00002054};
2055
2056
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057#ifdef DEBUG
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002058// Visitor class to verify interior pointers in spaces that do not contain
2059// or care about intergenerational references. All heap object pointers have to
2060// point into the heap to a location that has a map pointer at its first word.
2061// Caveat: Heap::Contains is an approximation because it can return true for
2062// objects in a heap space but above the allocation pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063class VerifyPointersVisitor: public ObjectVisitor {
2064 public:
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002065 inline void VisitPointers(Object** start, Object** end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067#endif
2068
2069
kasper.lund7276f142008-07-30 08:49:36 +00002070// Space iterator for iterating over all spaces of the heap.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002071// Returns each space in turn, and null when it is done.
2072class AllSpaces BASE_EMBEDDED {
2073 public:
2074 Space* next();
2075 AllSpaces() { counter_ = FIRST_SPACE; }
2076 private:
2077 int counter_;
2078};
2079
2080
2081// Space iterator for iterating over all old spaces of the heap: Old pointer
2082// space, old data space and code space.
2083// Returns each space in turn, and null when it is done.
2084class OldSpaces BASE_EMBEDDED {
2085 public:
2086 OldSpace* next();
2087 OldSpaces() { counter_ = OLD_POINTER_SPACE; }
2088 private:
2089 int counter_;
2090};
2091
2092
2093// Space iterator for iterating over all the paged spaces of the heap:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002094// Map space, old pointer space, old data space, code space and cell space.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002095// Returns each space in turn, and null when it is done.
2096class PagedSpaces BASE_EMBEDDED {
2097 public:
2098 PagedSpace* next();
2099 PagedSpaces() { counter_ = OLD_POINTER_SPACE; }
2100 private:
2101 int counter_;
2102};
2103
2104
2105// Space iterator for iterating over all spaces of the heap.
kasper.lund7276f142008-07-30 08:49:36 +00002106// For each space an object iterator is provided. The deallocation of the
2107// returned object iterators is handled by the space iterator.
kasper.lund7276f142008-07-30 08:49:36 +00002108class SpaceIterator : public Malloced {
2109 public:
2110 SpaceIterator();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002111 explicit SpaceIterator(HeapObjectCallback size_func);
kasper.lund7276f142008-07-30 08:49:36 +00002112 virtual ~SpaceIterator();
2113
2114 bool has_next();
2115 ObjectIterator* next();
2116
2117 private:
2118 ObjectIterator* CreateIterator();
2119
2120 int current_space_; // from enum AllocationSpace.
2121 ObjectIterator* iterator_; // object iterator for the current space.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002122 HeapObjectCallback size_func_;
kasper.lund7276f142008-07-30 08:49:36 +00002123};
2124
2125
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002126// A HeapIterator provides iteration over the whole heap. It
2127// aggregates the specific iterators for the different spaces as
2128// these can only iterate over one space only.
2129//
2130// HeapIterator can skip free list nodes (that is, de-allocated heap
2131// objects that still remain in the heap). As implementation of free
2132// nodes filtering uses GC marks, it can't be used during MS/MC GC
2133// phases. Also, it is forbidden to interrupt iteration in this mode,
2134// as this will leave heap objects marked (and thus, unusable).
whesse@chromium.org023421e2010-12-21 12:19:12 +00002135class HeapObjectsFilter;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136
2137class HeapIterator BASE_EMBEDDED {
2138 public:
whesse@chromium.org023421e2010-12-21 12:19:12 +00002139 enum HeapObjectsFiltering {
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002140 kNoFiltering,
whesse@chromium.org023421e2010-12-21 12:19:12 +00002141 kFilterUnreachable
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002142 };
2143
2144 HeapIterator();
whesse@chromium.org023421e2010-12-21 12:19:12 +00002145 explicit HeapIterator(HeapObjectsFiltering filtering);
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002146 ~HeapIterator();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 HeapObject* next();
2149 void reset();
2150
2151 private:
2152 // Perform the initialization.
2153 void Init();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 // Perform all necessary shutdown (destruction) work.
2155 void Shutdown();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00002156 HeapObject* NextObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157
whesse@chromium.org023421e2010-12-21 12:19:12 +00002158 HeapObjectsFiltering filtering_;
2159 HeapObjectsFilter* filter_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 // Space iterator for iterating all the spaces.
2161 SpaceIterator* space_iterator_;
2162 // Object iterator for the space currently being iterated.
2163 ObjectIterator* object_iterator_;
2164};
2165
2166
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002167// Cache for mapping (map, property name) into field offset.
2168// Cleared at startup and prior to mark sweep collection.
2169class KeyedLookupCache {
2170 public:
2171 // Lookup field offset for (map, name). If absent, -1 is returned.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002172 int Lookup(Map* map, String* name);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002173
2174 // Update an element in the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002175 void Update(Map* map, String* name, int field_offset);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002176
2177 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002178 void Clear();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002179
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002180 static const int kLength = 256;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002181 static const int kCapacityMask = kLength - 1;
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00002182 static const int kMapHashShift = 5;
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002183 static const int kHashMask = -4; // Zero the last two bits.
2184 static const int kEntriesPerBucket = 4;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002185 static const int kNotFound = -1;
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002186
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00002187 // kEntriesPerBucket should be a power of 2.
2188 STATIC_ASSERT((kEntriesPerBucket & (kEntriesPerBucket - 1)) == 0);
2189 STATIC_ASSERT(kEntriesPerBucket == -kHashMask);
2190
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002191 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002192 KeyedLookupCache() {
2193 for (int i = 0; i < kLength; ++i) {
2194 keys_[i].map = NULL;
2195 keys_[i].name = NULL;
2196 field_offsets_[i] = kNotFound;
2197 }
2198 }
2199
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002200 static inline int Hash(Map* map, String* name);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002201
2202 // Get the address of the keys and field_offsets arrays. Used in
2203 // generated code to perform cache lookups.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002204 Address keys_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002205 return reinterpret_cast<Address>(&keys_);
2206 }
2207
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002208 Address field_offsets_address() {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002209 return reinterpret_cast<Address>(&field_offsets_);
2210 }
2211
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002212 struct Key {
2213 Map* map;
2214 String* name;
2215 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002216
2217 Key keys_[kLength];
2218 int field_offsets_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002219
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002220 friend class ExternalReference;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002221 friend class Isolate;
2222 DISALLOW_COPY_AND_ASSIGN(KeyedLookupCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002223};
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002224
2225
2226// Cache for mapping (array, property name) into descriptor index.
2227// The cache contains both positive and negative results.
2228// Descriptor index equals kNotFound means the property is absent.
2229// Cleared at startup and prior to any gc.
2230class DescriptorLookupCache {
2231 public:
2232 // Lookup descriptor index for (map, name).
2233 // If absent, kAbsent is returned.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002234 int Lookup(DescriptorArray* array, String* name) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002235 if (!StringShape(name).IsSymbol()) return kAbsent;
2236 int index = Hash(array, name);
2237 Key& key = keys_[index];
2238 if ((key.array == array) && (key.name == name)) return results_[index];
2239 return kAbsent;
2240 }
2241
2242 // Update an element in the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002243 void Update(DescriptorArray* array, String* name, int result) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002244 ASSERT(result != kAbsent);
2245 if (StringShape(name).IsSymbol()) {
2246 int index = Hash(array, name);
2247 Key& key = keys_[index];
2248 key.array = array;
2249 key.name = name;
2250 results_[index] = result;
2251 }
2252 }
2253
2254 // Clear the cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002255 void Clear();
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002256
2257 static const int kAbsent = -2;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00002258
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002259 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002260 DescriptorLookupCache() {
2261 for (int i = 0; i < kLength; ++i) {
2262 keys_[i].array = NULL;
2263 keys_[i].name = NULL;
2264 results_[i] = kAbsent;
2265 }
2266 }
2267
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002268 static int Hash(DescriptorArray* array, String* name) {
2269 // Uses only lower 32 bits if pointers are larger.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002270 uint32_t array_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002271 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(array)) >> 2;
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00002272 uint32_t name_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002273 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name)) >> 2;
2274 return (array_hash ^ name_hash) % kLength;
2275 }
2276
2277 static const int kLength = 64;
2278 struct Key {
2279 DescriptorArray* array;
2280 String* name;
2281 };
2282
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002283 Key keys_[kLength];
2284 int results_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002285
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002286 friend class Isolate;
2287 DISALLOW_COPY_AND_ASSIGN(DescriptorLookupCache);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288};
2289
2290
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002291#ifdef DEBUG
2292class DisallowAllocationFailure {
2293 public:
2294 inline DisallowAllocationFailure();
2295 inline ~DisallowAllocationFailure();
2296
2297 private:
2298 bool old_state_;
2299};
2300#endif
2301
2302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303// A helper class to document/test C++ scopes where we do not
2304// expect a GC. Usage:
2305//
2306// /* Allocation not allowed: we cannot handle a GC in this scope. */
2307// { AssertNoAllocation nogc;
2308// ...
2309// }
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002310class AssertNoAllocation {
2311 public:
2312 inline AssertNoAllocation();
2313 inline ~AssertNoAllocation();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314
2315#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 private:
2317 bool old_state_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318#endif
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002319};
2320
2321
2322class DisableAssertNoAllocation {
2323 public:
2324 inline DisableAssertNoAllocation();
2325 inline ~DisableAssertNoAllocation();
2326
2327#ifdef DEBUG
2328 private:
2329 bool old_state_;
2330#endif
2331};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002332
kasper.lund7276f142008-07-30 08:49:36 +00002333// GCTracer collects and prints ONE line after each garbage collector
2334// invocation IFF --trace_gc is used.
2335
2336class GCTracer BASE_EMBEDDED {
2337 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002338 class Scope BASE_EMBEDDED {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002339 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002340 enum ScopeId {
2341 EXTERNAL,
2342 MC_MARK,
2343 MC_SWEEP,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002344 MC_SWEEP_NEWSPACE,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002345 MC_EVACUATE_PAGES,
2346 MC_UPDATE_NEW_TO_NEW_POINTERS,
2347 MC_UPDATE_ROOT_TO_NEW_POINTERS,
2348 MC_UPDATE_OLD_TO_NEW_POINTERS,
2349 MC_UPDATE_POINTERS_TO_EVACUATED,
2350 MC_UPDATE_POINTERS_BETWEEN_EVACUATED,
2351 MC_UPDATE_MISC_POINTERS,
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00002352 MC_FLUSH_CODE,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002353 kNumberOfScopes
2354 };
2355
2356 Scope(GCTracer* tracer, ScopeId scope)
2357 : tracer_(tracer),
2358 scope_(scope) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002359 start_time_ = OS::TimeCurrentMillis();
2360 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002361
2362 ~Scope() {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002363 ASSERT(scope_ < kNumberOfScopes); // scope_ is unsigned.
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002364 tracer_->scopes_[scope_] += OS::TimeCurrentMillis() - start_time_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002365 }
kasper.lund7276f142008-07-30 08:49:36 +00002366
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002367 private:
2368 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002369 ScopeId scope_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002370 double start_time_;
2371 };
2372
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002373 explicit GCTracer(Heap* heap,
2374 const char* gc_reason,
2375 const char* collector_reason);
kasper.lund7276f142008-07-30 08:49:36 +00002376 ~GCTracer();
2377
2378 // Sets the collector.
2379 void set_collector(GarbageCollector collector) { collector_ = collector; }
2380
2381 // Sets the GC count.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002382 void set_gc_count(unsigned int count) { gc_count_ = count; }
kasper.lund7276f142008-07-30 08:49:36 +00002383
2384 // Sets the full GC count.
2385 void set_full_gc_count(int count) { full_gc_count_ = count; }
2386
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002387 void increment_promoted_objects_size(int object_size) {
2388 promoted_objects_size_ += object_size;
2389 }
2390
kasper.lund7276f142008-07-30 08:49:36 +00002391 private:
2392 // Returns a string matching the collector.
2393 const char* CollectorString();
2394
2395 // Returns size of object in heap (in MB).
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002396 inline double SizeOfHeapObjects();
kasper.lund7276f142008-07-30 08:49:36 +00002397
jkummerow@chromium.org1145ef82012-02-02 16:21:15 +00002398 // Timestamp set in the constructor.
2399 double start_time_;
2400
2401 // Size of objects in heap set in constructor.
2402 intptr_t start_object_size_;
2403
2404 // Size of memory allocated from OS set in constructor.
2405 intptr_t start_memory_size_;
2406
2407 // Type of collector.
2408 GarbageCollector collector_;
kasper.lund7276f142008-07-30 08:49:36 +00002409
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002410 // A count (including this one, e.g. the first collection is 1) of the
kasper.lund7276f142008-07-30 08:49:36 +00002411 // number of garbage collections.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002412 unsigned int gc_count_;
kasper.lund7276f142008-07-30 08:49:36 +00002413
2414 // A count (including this one) of the number of full garbage collections.
2415 int full_gc_count_;
2416
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002417 // Amounts of time spent in different scopes during GC.
2418 double scopes_[Scope::kNumberOfScopes];
2419
2420 // Total amount of space either wasted or contained in one of free lists
2421 // before the current GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002422 intptr_t in_free_list_or_wasted_before_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002423
2424 // Difference between space used in the heap at the beginning of the current
2425 // collection and the end of the previous collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002426 intptr_t allocated_since_last_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002427
2428 // Amount of time spent in mutator that is time elapsed between end of the
2429 // previous collection and the beginning of the current one.
2430 double spent_in_mutator_;
2431
2432 // Size of objects promoted during the current collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00002433 intptr_t promoted_objects_size_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002434
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002435 // Incremental marking steps counters.
2436 int steps_count_;
2437 double steps_took_;
2438 double longest_step_;
2439 int steps_count_since_last_gc_;
2440 double steps_took_since_last_gc_;
2441
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002442 Heap* heap_;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002443
2444 const char* gc_reason_;
2445 const char* collector_reason_;
kasper.lund7276f142008-07-30 08:49:36 +00002446};
2447
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002448
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00002449class StringSplitCache {
2450 public:
2451 static Object* Lookup(FixedArray* cache, String* string, String* pattern);
2452 static void Enter(Heap* heap,
2453 FixedArray* cache,
2454 String* string,
2455 String* pattern,
2456 FixedArray* array);
2457 static void Clear(FixedArray* cache);
2458 static const int kStringSplitCacheSize = 0x100;
2459
2460 private:
2461 static const int kArrayEntriesPerCacheEntry = 4;
2462 static const int kStringOffset = 0;
2463 static const int kPatternOffset = 1;
2464 static const int kArrayOffset = 2;
2465
2466 static MaybeObject* WrapFixedArrayInJSArray(Object* fixed_array);
2467};
2468
2469
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002470class TranscendentalCache {
2471 public:
2472 enum Type {ACOS, ASIN, ATAN, COS, EXP, LOG, SIN, TAN, kNumberOfCaches};
whesse@chromium.org023421e2010-12-21 12:19:12 +00002473 static const int kTranscendentalTypeBits = 3;
2474 STATIC_ASSERT((1 << kTranscendentalTypeBits) >= kNumberOfCaches);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002475
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002476 // Returns a heap number with f(input), where f is a math function specified
2477 // by the 'type' argument.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002478 MUST_USE_RESULT inline MaybeObject* Get(Type type, double input);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002479
2480 // The cache contains raw Object pointers. This method disposes of
2481 // them before a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002482 void Clear();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002483
2484 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002485 class SubCache {
2486 static const int kCacheSize = 512;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002487
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002488 explicit SubCache(Type t);
2489
2490 MUST_USE_RESULT inline MaybeObject* Get(double input);
2491
2492 inline double Calculate(double input);
2493
2494 struct Element {
2495 uint32_t in[2];
2496 Object* output;
2497 };
2498
2499 union Converter {
2500 double dbl;
2501 uint32_t integers[2];
2502 };
2503
2504 inline static int Hash(const Converter& c) {
2505 uint32_t hash = (c.integers[0] ^ c.integers[1]);
2506 hash ^= static_cast<int32_t>(hash) >> 16;
2507 hash ^= static_cast<int32_t>(hash) >> 8;
2508 return (hash & (kCacheSize - 1));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002509 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002510
2511 Element elements_[kCacheSize];
2512 Type type_;
2513 Isolate* isolate_;
2514
2515 // Allow access to the caches_ array as an ExternalReference.
2516 friend class ExternalReference;
2517 // Inline implementation of the cache.
2518 friend class TranscendentalCacheStub;
2519 // For evaluating value.
2520 friend class TranscendentalCache;
2521
2522 DISALLOW_COPY_AND_ASSIGN(SubCache);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002523 };
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002524
2525 TranscendentalCache() {
2526 for (int i = 0; i < kNumberOfCaches; ++i) caches_[i] = NULL;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002527 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002528
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002529 // Used to create an external reference.
2530 inline Address cache_array_address();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002531
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002532 // Instantiation
2533 friend class Isolate;
2534 // Inline implementation of the caching.
2535 friend class TranscendentalCacheStub;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002536 // Allow access to the caches_ array as an ExternalReference.
2537 friend class ExternalReference;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002538
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002539 SubCache* caches_[kNumberOfCaches];
2540 DISALLOW_COPY_AND_ASSIGN(TranscendentalCache);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002541};
2542
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002543
2544// Abstract base class for checking whether a weak object should be retained.
2545class WeakObjectRetainer {
2546 public:
2547 virtual ~WeakObjectRetainer() {}
2548
2549 // Return whether this object should be retained. If NULL is returned the
2550 // object has no references. Otherwise the address of the retained object
2551 // should be returned as in some GC situations the object has been moved.
2552 virtual Object* RetainAs(Object* object) = 0;
2553};
2554
2555
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002556// Intrusive object marking uses least significant bit of
2557// heap object's map word to mark objects.
2558// Normally all map words have least significant bit set
2559// because they contain tagged map pointer.
2560// If the bit is not set object is marked.
2561// All objects should be unmarked before resuming
2562// JavaScript execution.
2563class IntrusiveMarking {
2564 public:
2565 static bool IsMarked(HeapObject* object) {
2566 return (object->map_word().ToRawValue() & kNotMarkedBit) == 0;
2567 }
2568
2569 static void ClearMark(HeapObject* object) {
2570 uintptr_t map_word = object->map_word().ToRawValue();
2571 object->set_map_word(MapWord::FromRawValue(map_word | kNotMarkedBit));
2572 ASSERT(!IsMarked(object));
2573 }
2574
2575 static void SetMark(HeapObject* object) {
2576 uintptr_t map_word = object->map_word().ToRawValue();
2577 object->set_map_word(MapWord::FromRawValue(map_word & ~kNotMarkedBit));
2578 ASSERT(IsMarked(object));
2579 }
2580
2581 static Map* MapOfMarkedObject(HeapObject* object) {
2582 uintptr_t map_word = object->map_word().ToRawValue();
2583 return MapWord::FromRawValue(map_word | kNotMarkedBit).ToMap();
2584 }
2585
2586 static int SizeOfMarkedObject(HeapObject* object) {
2587 return object->SizeFromMap(MapOfMarkedObject(object));
2588 }
2589
2590 private:
2591 static const uintptr_t kNotMarkedBit = 0x1;
2592 STATIC_ASSERT((kHeapObjectTag & kNotMarkedBit) != 0);
2593};
2594
2595
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002596#if defined(DEBUG) || defined(LIVE_OBJECT_LIST)
2597// Helper class for tracing paths to a search target Object from all roots.
2598// The TracePathFrom() method can be used to trace paths from a specific
2599// object to the search target object.
2600class PathTracer : public ObjectVisitor {
2601 public:
2602 enum WhatToFind {
2603 FIND_ALL, // Will find all matches.
2604 FIND_FIRST // Will stop the search after first match.
2605 };
2606
2607 // For the WhatToFind arg, if FIND_FIRST is specified, tracing will stop
2608 // after the first match. If FIND_ALL is specified, then tracing will be
2609 // done for all matches.
2610 PathTracer(Object* search_target,
2611 WhatToFind what_to_find,
2612 VisitMode visit_mode)
2613 : search_target_(search_target),
2614 found_target_(false),
2615 found_target_in_trace_(false),
2616 what_to_find_(what_to_find),
2617 visit_mode_(visit_mode),
2618 object_stack_(20),
2619 no_alloc() {}
2620
2621 virtual void VisitPointers(Object** start, Object** end);
2622
2623 void Reset();
2624 void TracePathFrom(Object** root);
2625
2626 bool found() const { return found_target_; }
2627
2628 static Object* const kAnyGlobalObject;
2629
2630 protected:
2631 class MarkVisitor;
2632 class UnmarkVisitor;
2633
2634 void MarkRecursively(Object** p, MarkVisitor* mark_visitor);
2635 void UnmarkRecursively(Object** p, UnmarkVisitor* unmark_visitor);
2636 virtual void ProcessResults();
2637
2638 // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject.
2639 static const int kMarkTag = 2;
2640
2641 Object* search_target_;
2642 bool found_target_;
2643 bool found_target_in_trace_;
2644 WhatToFind what_to_find_;
2645 VisitMode visit_mode_;
2646 List<Object*> object_stack_;
2647
2648 AssertNoAllocation no_alloc; // i.e. no gc allowed.
2649
jkummerow@chromium.org05ed9dd2012-01-23 14:42:48 +00002650 private:
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002651 DISALLOW_IMPLICIT_CONSTRUCTORS(PathTracer);
2652};
2653#endif // DEBUG || LIVE_OBJECT_LIST
2654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655} } // namespace v8::internal
2656
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657#endif // V8_HEAP_H_