blob: fbcc70df53155153df896c7383c3bbf21de83899 [file] [log] [blame]
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00001// Copyright 2010 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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000033#include "spaces.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000034#include "splay-tree-inl.h"
35#include "v8-counters.h"
ager@chromium.org18ad94b2009-09-02 08:22:29 +000036
kasperl@chromium.org71affb52009-05-26 05:44:31 +000037namespace v8 {
38namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041// Defines all the roots in Heap.
ager@chromium.org18ad94b2009-09-02 08:22:29 +000042#define UNCONDITIONAL_STRONG_ROOT_LIST(V) \
ager@chromium.org3811b432009-10-28 14:53:37 +000043 /* Put the byte array map early. We need it to be in place by the time */ \
44 /* the deserializer hits the next page, since it wants to put a byte */ \
45 /* array in the unused space at the end of the page. */ \
46 V(Map, byte_array_map, ByteArrayMap) \
47 V(Map, one_pointer_filler_map, OnePointerFillerMap) \
48 V(Map, two_pointer_filler_map, TwoPointerFillerMap) \
49 /* Cluster the most popular ones in a few cache lines here at the top. */ \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000050 V(Smi, stack_limit, StackLimit) \
51 V(Object, undefined_value, UndefinedValue) \
52 V(Object, the_hole_value, TheHoleValue) \
53 V(Object, null_value, NullValue) \
54 V(Object, true_value, TrueValue) \
55 V(Object, false_value, FalseValue) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000056 V(Map, heap_number_map, HeapNumberMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000057 V(Map, global_context_map, GlobalContextMap) \
58 V(Map, fixed_array_map, FixedArrayMap) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +000059 V(Map, fixed_cow_array_map, FixedCOWArrayMap) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +000060 V(Object, no_interceptor_result_sentinel, NoInterceptorResultSentinel) \
61 V(Map, meta_map, MetaMap) \
62 V(Object, termination_exception, TerminationException) \
63 V(Map, hash_table_map, HashTableMap) \
64 V(FixedArray, empty_fixed_array, EmptyFixedArray) \
kasperl@chromium.orga5551262010-12-07 12:49:48 +000065 V(ByteArray, empty_byte_array, EmptyByteArray) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000066 V(Map, string_map, StringMap) \
67 V(Map, ascii_string_map, AsciiStringMap) \
68 V(Map, symbol_map, SymbolMap) \
69 V(Map, ascii_symbol_map, AsciiSymbolMap) \
70 V(Map, cons_symbol_map, ConsSymbolMap) \
71 V(Map, cons_ascii_symbol_map, ConsAsciiSymbolMap) \
72 V(Map, external_symbol_map, ExternalSymbolMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000073 V(Map, external_symbol_with_ascii_data_map, ExternalSymbolWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000074 V(Map, external_ascii_symbol_map, ExternalAsciiSymbolMap) \
75 V(Map, cons_string_map, ConsStringMap) \
76 V(Map, cons_ascii_string_map, ConsAsciiStringMap) \
77 V(Map, external_string_map, ExternalStringMap) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +000078 V(Map, external_string_with_ascii_data_map, ExternalStringWithAsciiDataMap) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +000079 V(Map, external_ascii_string_map, ExternalAsciiStringMap) \
80 V(Map, undetectable_string_map, UndetectableStringMap) \
81 V(Map, undetectable_ascii_string_map, UndetectableAsciiStringMap) \
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +000082 V(Map, pixel_array_map, PixelArrayMap) \
ager@chromium.org3811b432009-10-28 14:53:37 +000083 V(Map, external_byte_array_map, ExternalByteArrayMap) \
84 V(Map, external_unsigned_byte_array_map, ExternalUnsignedByteArrayMap) \
85 V(Map, external_short_array_map, ExternalShortArrayMap) \
86 V(Map, external_unsigned_short_array_map, ExternalUnsignedShortArrayMap) \
87 V(Map, external_int_array_map, ExternalIntArrayMap) \
88 V(Map, external_unsigned_int_array_map, ExternalUnsignedIntArrayMap) \
89 V(Map, external_float_array_map, ExternalFloatArrayMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000090 V(Map, context_map, ContextMap) \
91 V(Map, catch_context_map, CatchContextMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000092 V(Map, code_map, CodeMap) \
93 V(Map, oddball_map, OddballMap) \
94 V(Map, global_property_cell_map, GlobalPropertyCellMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000095 V(Map, shared_function_info_map, SharedFunctionInfoMap) \
96 V(Map, proxy_map, ProxyMap) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000097 V(Object, nan_value, NanValue) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000098 V(Object, minus_zero_value, MinusZeroValue) \
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000099 V(Object, instanceof_cache_function, InstanceofCacheFunction) \
100 V(Object, instanceof_cache_map, InstanceofCacheMap) \
101 V(Object, instanceof_cache_answer, InstanceofCacheAnswer) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000102 V(String, empty_string, EmptyString) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000103 V(DescriptorArray, empty_descriptor_array, EmptyDescriptorArray) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000104 V(Map, neander_map, NeanderMap) \
105 V(JSObject, message_listeners, MessageListeners) \
106 V(Proxy, prototype_accessors, PrototypeAccessors) \
107 V(NumberDictionary, code_stubs, CodeStubs) \
108 V(NumberDictionary, non_monomorphic_cache, NonMonomorphicCache) \
109 V(Code, js_entry_code, JsEntryCode) \
110 V(Code, js_construct_entry_code, JsConstructEntryCode) \
111 V(Code, c_entry_code, CEntryCode) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000112 V(FixedArray, number_string_cache, NumberStringCache) \
113 V(FixedArray, single_character_string_cache, SingleCharacterStringCache) \
114 V(FixedArray, natives_source_cache, NativesSourceCache) \
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000115 V(Object, last_script_id, LastScriptId) \
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000116 V(Script, empty_script, EmptyScript) \
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000117 V(Smi, real_stack_limit, RealStackLimit) \
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +0000118 V(StringDictionary, intrinsic_function_names, IntrinsicFunctionNames) \
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000119
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000120#if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000121#define STRONG_ROOT_LIST(V) \
122 UNCONDITIONAL_STRONG_ROOT_LIST(V) \
123 V(Code, re_c_entry_code, RegExpCEntryCode)
124#else
125#define STRONG_ROOT_LIST(V) UNCONDITIONAL_STRONG_ROOT_LIST(V)
126#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127
128#define ROOT_LIST(V) \
129 STRONG_ROOT_LIST(V) \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000130 V(SymbolTable, symbol_table, SymbolTable)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131
132#define SYMBOL_LIST(V) \
133 V(Array_symbol, "Array") \
134 V(Object_symbol, "Object") \
135 V(Proto_symbol, "__proto__") \
136 V(StringImpl_symbol, "StringImpl") \
137 V(arguments_symbol, "arguments") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000138 V(Arguments_symbol, "Arguments") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 V(arguments_shadow_symbol, ".arguments") \
140 V(call_symbol, "call") \
141 V(apply_symbol, "apply") \
142 V(caller_symbol, "caller") \
143 V(boolean_symbol, "boolean") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000144 V(Boolean_symbol, "Boolean") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 V(callee_symbol, "callee") \
146 V(constructor_symbol, "constructor") \
147 V(code_symbol, ".code") \
148 V(result_symbol, ".result") \
149 V(catch_var_symbol, ".catch-var") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 V(empty_symbol, "") \
151 V(eval_symbol, "eval") \
152 V(function_symbol, "function") \
153 V(length_symbol, "length") \
154 V(name_symbol, "name") \
155 V(number_symbol, "number") \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000156 V(Number_symbol, "Number") \
157 V(RegExp_symbol, "RegExp") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000158 V(source_symbol, "source") \
159 V(global_symbol, "global") \
160 V(ignore_case_symbol, "ignoreCase") \
161 V(multiline_symbol, "multiline") \
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000162 V(input_symbol, "input") \
163 V(index_symbol, "index") \
lrn@chromium.org25156de2010-04-06 13:10:27 +0000164 V(last_index_symbol, "lastIndex") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 V(object_symbol, "object") \
166 V(prototype_symbol, "prototype") \
167 V(string_symbol, "string") \
ager@chromium.org7c537e22008-10-16 08:43:32 +0000168 V(String_symbol, "String") \
169 V(Date_symbol, "Date") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 V(this_symbol, "this") \
171 V(to_string_symbol, "toString") \
172 V(char_at_symbol, "CharAt") \
173 V(undefined_symbol, "undefined") \
174 V(value_of_symbol, "valueOf") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 V(InitializeVarGlobal_symbol, "InitializeVarGlobal") \
176 V(InitializeConstGlobal_symbol, "InitializeConstGlobal") \
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000177 V(KeyedLoadSpecialized_symbol, "KeyedLoadSpecialized") \
178 V(KeyedStoreSpecialized_symbol, "KeyedStoreSpecialized") \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 V(stack_overflow_symbol, "kStackOverflowBoilerplate") \
180 V(illegal_access_symbol, "illegal access") \
181 V(out_of_memory_symbol, "out-of-memory") \
182 V(illegal_execution_state_symbol, "illegal execution state") \
183 V(get_symbol, "get") \
184 V(set_symbol, "set") \
185 V(function_class_symbol, "Function") \
186 V(illegal_argument_symbol, "illegal argument") \
187 V(MakeReferenceError_symbol, "MakeReferenceError") \
188 V(MakeSyntaxError_symbol, "MakeSyntaxError") \
189 V(MakeTypeError_symbol, "MakeTypeError") \
190 V(invalid_lhs_in_assignment_symbol, "invalid_lhs_in_assignment") \
191 V(invalid_lhs_in_for_in_symbol, "invalid_lhs_in_for_in") \
192 V(invalid_lhs_in_postfix_op_symbol, "invalid_lhs_in_postfix_op") \
193 V(invalid_lhs_in_prefix_op_symbol, "invalid_lhs_in_prefix_op") \
194 V(illegal_return_symbol, "illegal_return") \
195 V(illegal_break_symbol, "illegal_break") \
196 V(illegal_continue_symbol, "illegal_continue") \
197 V(unknown_label_symbol, "unknown_label") \
198 V(redeclaration_symbol, "redeclaration") \
199 V(failure_symbol, "<failure>") \
200 V(space_symbol, " ") \
201 V(exec_symbol, "exec") \
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000202 V(zero_symbol, "0") \
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000203 V(global_eval_symbol, "GlobalEval") \
ager@chromium.org3811b432009-10-28 14:53:37 +0000204 V(identity_hash_symbol, "v8::IdentityHash") \
205 V(closure_symbol, "(closure)")
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206
207
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000208// Forward declarations.
kasper.lund7276f142008-07-30 08:49:36 +0000209class GCTracer;
ager@chromium.org60121232009-12-03 11:25:37 +0000210class HeapStats;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000211class WeakObjectRetainer;
kasper.lund7276f142008-07-30 08:49:36 +0000212
213
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000214typedef String* (*ExternalStringTableUpdaterCallback)(Object** pointer);
215
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000216typedef bool (*DirtyRegionCallback)(Address start,
217 Address end,
218 ObjectSlotCallback copy_object_func);
219
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000220
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221// The all static Heap captures the interface to the global object heap.
222// All JavaScript contexts by this process share the same object heap.
223
224class Heap : public AllStatic {
225 public:
226 // Configure heap size before setup. Return false if the heap has been
227 // setup already.
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000228 static bool ConfigureHeap(int max_semispace_size,
229 int max_old_gen_size,
230 int max_executable_size);
kasper.lund7276f142008-07-30 08:49:36 +0000231 static bool ConfigureHeapDefault();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232
233 // Initializes the global object heap. If create_heap_objects is true,
234 // also creates the basic non-mutable objects.
235 // Returns whether it succeeded.
236 static bool Setup(bool create_heap_objects);
237
238 // Destroys all memory allocated by the heap.
239 static void TearDown();
240
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000241 // Set the stack limit in the roots_ array. Some architectures generate
242 // code that looks here, because it is faster than loading from the static
243 // jslimit_/real_jslimit_ variable in the StackGuard.
244 static void SetStackLimits();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000245
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 // Returns whether Setup has been called.
247 static bool HasBeenSetup();
248
ager@chromium.org3811b432009-10-28 14:53:37 +0000249 // Returns the maximum amount of memory reserved for the heap. For
250 // the young generation, we reserve 4 times the amount needed for a
251 // semi space. The young generation consists of two semi spaces and
252 // we reserve twice the amount needed for those in order to ensure
253 // that new space can be aligned to its size.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000254 static intptr_t MaxReserved() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000255 return 4 * reserved_semispace_size_ + max_old_generation_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000257 static int MaxSemiSpaceSize() { return max_semispace_size_; }
258 static int ReservedSemiSpaceSize() { return reserved_semispace_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 static int InitialSemiSpaceSize() { return initial_semispace_size_; }
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000260 static intptr_t MaxOldGenerationSize() { return max_old_generation_size_; }
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000261 static intptr_t MaxExecutableSize() { return max_executable_size_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262
263 // Returns the capacity of the heap in bytes w/o growing. Heap grows when
264 // more spaces are needed until it reaches the limit.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000265 static intptr_t Capacity();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266
ager@chromium.org3811b432009-10-28 14:53:37 +0000267 // Returns the amount of memory currently committed for the heap.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000268 static intptr_t CommittedMemory();
ager@chromium.org3811b432009-10-28 14:53:37 +0000269
ager@chromium.org01fe7df2010-11-10 11:59:11 +0000270 // Returns the amount of executable memory currently committed for the heap.
271 static intptr_t CommittedMemoryExecutable();
272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000273 // Returns the available bytes in space w/o growing.
274 // Heap doesn't guarantee that it can allocate an object that requires
275 // all available bytes. Check MaxHeapObjectSize() instead.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000276 static intptr_t Available();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000278 // Returns the maximum object size in paged space.
279 static inline int MaxObjectSizeInPagedSpace();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280
281 // Returns of size of all objects residing in the heap.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000282 static intptr_t SizeOfObjects();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283
284 // Return the starting address and a mask for the new space. And-masking an
285 // address with the mask will result in the start address of the new space
286 // for all addresses in either semispace.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000287 static Address NewSpaceStart() { return new_space_.start(); }
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000288 static uintptr_t NewSpaceMask() { return new_space_.mask(); }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000289 static Address NewSpaceTop() { return new_space_.top(); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000291 static NewSpace* new_space() { return &new_space_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000292 static OldSpace* old_pointer_space() { return old_pointer_space_; }
293 static OldSpace* old_data_space() { return old_data_space_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 static OldSpace* code_space() { return code_space_; }
295 static MapSpace* map_space() { return map_space_; }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000296 static CellSpace* cell_space() { return cell_space_; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 static LargeObjectSpace* lo_space() { return lo_space_; }
298
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000299 static bool always_allocate() { return always_allocate_scope_depth_ != 0; }
300 static Address always_allocate_scope_depth_address() {
301 return reinterpret_cast<Address>(&always_allocate_scope_depth_);
302 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000303 static bool linear_allocation() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000304 return linear_allocation_scope_depth_ != 0;
ager@chromium.org3811b432009-10-28 14:53:37 +0000305 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 static Address* NewSpaceAllocationTopAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000308 return new_space_.allocation_top_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309 }
310 static Address* NewSpaceAllocationLimitAddress() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000311 return new_space_.allocation_limit_address();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 }
313
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000314 // Uncommit unused semi space.
315 static bool UncommitFromSpace() { return new_space_.UncommitFromSpace(); }
316
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +0000317#ifdef ENABLE_HEAP_PROTECTION
318 // Protect/unprotect the heap by marking all spaces read-only/writable.
319 static void Protect();
320 static void Unprotect();
321#endif
322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000323 // Allocates and initializes a new JavaScript object based on a
324 // constructor.
325 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
326 // failed.
327 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000328 MUST_USE_RESULT static MaybeObject* AllocateJSObject(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000329 JSFunction* constructor, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000331 // Allocates and initializes a new global object based on a constructor.
332 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
333 // failed.
334 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000335 MUST_USE_RESULT static MaybeObject* AllocateGlobalObject(
336 JSFunction* constructor);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000337
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000338 // Returns a deep copy of the JavaScript object.
339 // Properties and elements are copied too.
340 // Returns failure if allocation failed.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000341 MUST_USE_RESULT static MaybeObject* CopyJSObject(JSObject* source);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000342
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343 // Allocates the function prototype.
344 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
345 // failed.
346 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000347 MUST_USE_RESULT static MaybeObject* AllocateFunctionPrototype(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000348 JSFunction* function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000350 // Reinitialize an JSGlobalProxy based on a constructor. The object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 // must have the same size as objects allocated using the
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000352 // constructor. The object is reinitialized and behaves as an
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 // object that has been freshly allocated using the constructor.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000354 MUST_USE_RESULT static MaybeObject* ReinitializeJSGlobalProxy(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000355 JSFunction* constructor,
356 JSGlobalProxy* global);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000357
358 // Allocates and initializes a new JavaScript object based on a map.
359 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
360 // failed.
361 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000362 MUST_USE_RESULT static MaybeObject* AllocateJSObjectFromMap(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000363 Map* map, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000364
365 // Allocates a heap object based on the map.
366 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
367 // failed.
368 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000369 MUST_USE_RESULT static MaybeObject* Allocate(Map* map, AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370
371 // Allocates a JS Map in the heap.
372 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
373 // failed.
374 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000375 MUST_USE_RESULT static MaybeObject* AllocateMap(InstanceType instance_type,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000376 int instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377
378 // Allocates a partial map for bootstrapping.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000379 MUST_USE_RESULT static MaybeObject* AllocatePartialMap(
380 InstanceType instance_type,
381 int instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382
383 // Allocate a map for the specified function
lrn@chromium.org303ada72010-10-27 09:33:13 +0000384 MUST_USE_RESULT static MaybeObject* AllocateInitialMap(JSFunction* fun);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000385
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000386 // Allocates an empty code cache.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000387 MUST_USE_RESULT static MaybeObject* AllocateCodeCache();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000388
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000389 // Clear the Instanceof cache (used when a prototype changes).
390 static void ClearInstanceofCache() {
391 set_instanceof_cache_function(the_hole_value());
392 }
393
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 // Allocates and fully initializes a String. There are two String
395 // encodings: ASCII and two byte. One should choose between the three string
396 // allocation functions based on the encoding of the string buffer used to
397 // initialized the string.
398 // - ...FromAscii initializes the string from a buffer that is ASCII
399 // encoded (it does not check that the buffer is ASCII encoded) and the
400 // result will be ASCII encoded.
401 // - ...FromUTF8 initializes the string from a buffer that is UTF-8
402 // encoded. If the characters are all single-byte characters, the
403 // result will be ASCII encoded, otherwise it will converted to two
404 // byte.
405 // - ...FromTwoByte initializes the string from a buffer that is two-byte
406 // encoded. If the characters are all single-byte characters, the
407 // result will be converted to ASCII, otherwise it will be left as
408 // two-byte.
409 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
410 // failed.
411 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000412 MUST_USE_RESULT static MaybeObject* AllocateStringFromAscii(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413 Vector<const char> str,
414 PretenureFlag pretenure = NOT_TENURED);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000415 MUST_USE_RESULT static MaybeObject* AllocateStringFromUtf8(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 Vector<const char> str,
417 PretenureFlag pretenure = NOT_TENURED);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000418 MUST_USE_RESULT static MaybeObject* AllocateStringFromTwoByte(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000419 Vector<const uc16> str,
420 PretenureFlag pretenure = NOT_TENURED);
421
422 // Allocates a symbol in old space based on the character stream.
423 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
424 // failed.
425 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000426 MUST_USE_RESULT static inline MaybeObject* AllocateSymbol(
427 Vector<const char> str,
428 int chars,
429 uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000430
lrn@chromium.org303ada72010-10-27 09:33:13 +0000431 MUST_USE_RESULT static MaybeObject* AllocateInternalSymbol(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000432 unibrow::CharacterStream* buffer, int chars, uint32_t hash_field);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000433
lrn@chromium.org303ada72010-10-27 09:33:13 +0000434 MUST_USE_RESULT static MaybeObject* AllocateExternalSymbol(
435 Vector<const char> str,
436 int chars);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000437
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000438
439 // Allocates and partially initializes a String. There are two String
440 // encodings: ASCII and two byte. These functions allocate a string of the
441 // given length and set its map and length fields. The characters of the
442 // string are uninitialized.
443 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
444 // failed.
445 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000446 MUST_USE_RESULT static MaybeObject* AllocateRawAsciiString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000447 int length,
448 PretenureFlag pretenure = NOT_TENURED);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000449 MUST_USE_RESULT static MaybeObject* AllocateRawTwoByteString(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450 int length,
451 PretenureFlag pretenure = NOT_TENURED);
452
453 // Computes a single character string where the character has code.
454 // A cache is used for ascii codes.
455 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
456 // failed. Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000457 MUST_USE_RESULT static MaybeObject* LookupSingleCharacterStringFromCode(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000458 uint16_t code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
460 // Allocate a byte array of the specified length
461 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
462 // failed.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000463 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000464 MUST_USE_RESULT static MaybeObject* AllocateByteArray(int length,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000465 PretenureFlag pretenure);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000466
467 // Allocate a non-tenured byte array of the specified length
468 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
469 // failed.
470 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000471 MUST_USE_RESULT static MaybeObject* AllocateByteArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000473 // Allocate a pixel array of the specified length
474 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
475 // failed.
476 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000477 MUST_USE_RESULT static MaybeObject* AllocatePixelArray(int length,
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000478 uint8_t* external_pointer,
479 PretenureFlag pretenure);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000480
ager@chromium.org3811b432009-10-28 14:53:37 +0000481 // Allocates an external array of the specified length and type.
482 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
483 // failed.
484 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000485 MUST_USE_RESULT static MaybeObject* AllocateExternalArray(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000486 int length,
487 ExternalArrayType array_type,
488 void* external_pointer,
489 PretenureFlag pretenure);
ager@chromium.org3811b432009-10-28 14:53:37 +0000490
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000491 // Allocate a tenured JS global property cell.
492 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
493 // failed.
494 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000495 MUST_USE_RESULT static MaybeObject* AllocateJSGlobalPropertyCell(
496 Object* value);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 // Allocates a fixed array initialized with undefined values
499 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
500 // failed.
501 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000502 MUST_USE_RESULT static MaybeObject* AllocateFixedArray(
503 int length,
504 PretenureFlag pretenure);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000505 // Allocates a fixed array initialized with undefined values
lrn@chromium.org303ada72010-10-27 09:33:13 +0000506 MUST_USE_RESULT static MaybeObject* AllocateFixedArray(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000508 // Allocates an uninitialized fixed array. It must be filled by the caller.
509 //
510 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
511 // failed.
512 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000513 MUST_USE_RESULT static MaybeObject* AllocateUninitializedFixedArray(
514 int length);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000515
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000516 // Make a copy of src and return it. Returns
517 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000518 MUST_USE_RESULT static inline MaybeObject* CopyFixedArray(FixedArray* src);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000519
520 // Make a copy of src, set the map, and return the copy. Returns
521 // Failure::RetryAfterGC(requested_bytes, space) if the allocation failed.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000522 MUST_USE_RESULT static MaybeObject* CopyFixedArrayWithMap(FixedArray* src,
523 Map* map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524
525 // Allocates a fixed array initialized with the hole values.
526 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
527 // failed.
528 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000529 MUST_USE_RESULT static MaybeObject* AllocateFixedArrayWithHoles(
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000530 int length,
531 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
533 // AllocateHashTable is identical to AllocateFixedArray except
534 // that the resulting object has hash_table_map as map.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000535 MUST_USE_RESULT static MaybeObject* AllocateHashTable(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000536 int length, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537
538 // Allocate a global (but otherwise uninitialized) context.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000539 MUST_USE_RESULT static MaybeObject* AllocateGlobalContext();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540
541 // Allocate a function context.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000542 MUST_USE_RESULT static MaybeObject* AllocateFunctionContext(
543 int length,
544 JSFunction* closure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545
546 // Allocate a 'with' context.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000547 MUST_USE_RESULT static MaybeObject* AllocateWithContext(
548 Context* previous,
549 JSObject* extension,
550 bool is_catch_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551
552 // Allocates a new utility object in the old generation.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000553 MUST_USE_RESULT static MaybeObject* AllocateStruct(InstanceType type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 // Allocates a function initialized with a shared part.
556 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
557 // failed.
558 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000559 MUST_USE_RESULT static MaybeObject* AllocateFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000560 Map* function_map,
561 SharedFunctionInfo* shared,
562 Object* prototype,
563 PretenureFlag pretenure = TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564
565 // Indicies for direct access into argument objects.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000566 static const int kArgumentsObjectSize =
567 JSObject::kHeaderSize + 2 * kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 static const int arguments_callee_index = 0;
569 static const int arguments_length_index = 1;
570
571 // Allocates an arguments object - optionally with an elements array.
572 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
573 // failed.
574 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000575 MUST_USE_RESULT static MaybeObject* AllocateArgumentsObject(Object* callee,
576 int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 // Same as NewNumberFromDouble, but may return a preallocated/immutable
579 // number object (e.g., minus_zero_value_, nan_value_)
lrn@chromium.org303ada72010-10-27 09:33:13 +0000580 MUST_USE_RESULT static MaybeObject* NumberFromDouble(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000581 double value, PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582
583 // Allocated a HeapNumber from value.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000584 MUST_USE_RESULT static MaybeObject* AllocateHeapNumber(
585 double value,
586 PretenureFlag pretenure);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000587 // pretenure = NOT_TENURED.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000588 MUST_USE_RESULT static MaybeObject* AllocateHeapNumber(double value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589
590 // Converts an int into either a Smi or a HeapNumber object.
591 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
592 // failed.
593 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000594 MUST_USE_RESULT static inline MaybeObject* NumberFromInt32(int32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595
596 // Converts an int into either a Smi or a HeapNumber object.
597 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
598 // failed.
599 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000600 MUST_USE_RESULT static inline MaybeObject* NumberFromUint32(uint32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
602 // Allocates a new proxy object.
603 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
604 // failed.
605 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000606 MUST_USE_RESULT static MaybeObject* AllocateProxy(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000607 Address proxy,
608 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609
610 // Allocates a new SharedFunctionInfo object.
611 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
612 // failed.
613 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000614 MUST_USE_RESULT static MaybeObject* AllocateSharedFunctionInfo(Object* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000615
616 // Allocates a new cons string object.
617 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
618 // failed.
619 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000620 MUST_USE_RESULT static MaybeObject* AllocateConsString(String* first,
621 String* second);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623 // Allocates a new sub string object which is a substring of an underlying
624 // string buffer stretching from the index start (inclusive) to the index
625 // end (exclusive).
626 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
627 // failed.
628 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000629 MUST_USE_RESULT static MaybeObject* AllocateSubString(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000630 String* buffer,
631 int start,
632 int end,
633 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634
635 // Allocate a new external string object, which is backed by a string
636 // resource that resides outside the V8 heap.
637 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
638 // failed.
639 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000640 MUST_USE_RESULT static MaybeObject* AllocateExternalStringFromAscii(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 ExternalAsciiString::Resource* resource);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000642 MUST_USE_RESULT static MaybeObject* AllocateExternalStringFromTwoByte(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 ExternalTwoByteString::Resource* resource);
644
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000645 // Finalizes an external string by deleting the associated external
646 // data and clearing the resource pointer.
647 static inline void FinalizeExternalString(String* string);
648
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000649 // Allocates an uninitialized object. The memory is non-executable if the
650 // hardware and OS allow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
652 // failed.
653 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000654 MUST_USE_RESULT static inline MaybeObject* AllocateRaw(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000655 int size_in_bytes,
656 AllocationSpace space,
657 AllocationSpace retry_space);
kasper.lund7276f142008-07-30 08:49:36 +0000658
ager@chromium.org6f10e412009-02-13 10:11:16 +0000659 // Initialize a filler object to keep the ability to iterate over the heap
660 // when shortening objects.
661 static void CreateFillerObjectAt(Address addr, int size);
662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 // Makes a new native code object
664 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000665 // failed. On success, the pointer to the Code object is stored in the
666 // self_reference. This allows generated code to reference its own Code
667 // object by containing this pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000669 MUST_USE_RESULT static MaybeObject* CreateCode(const CodeDesc& desc,
670 Code::Flags flags,
671 Handle<Object> self_reference);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672
lrn@chromium.org303ada72010-10-27 09:33:13 +0000673 MUST_USE_RESULT static MaybeObject* CopyCode(Code* code);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000674
675 // Copy the code and scope info part of the code object, but insert
676 // the provided data as the relocation information.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000677 MUST_USE_RESULT static MaybeObject* CopyCode(Code* code,
678 Vector<byte> reloc_info);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000679
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680 // Finds the symbol for string in the symbol table.
681 // If not found, a new symbol is added to the table and returned.
682 // Returns Failure::RetryAfterGC(requested_bytes, space) if allocation
683 // failed.
684 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000685 MUST_USE_RESULT static MaybeObject* LookupSymbol(Vector<const char> str);
686 MUST_USE_RESULT static MaybeObject* LookupAsciiSymbol(const char* str) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 return LookupSymbol(CStrVector(str));
688 }
lrn@chromium.org303ada72010-10-27 09:33:13 +0000689 MUST_USE_RESULT static MaybeObject* LookupSymbol(String* str);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000690 static bool LookupSymbolIfExists(String* str, String** symbol);
ager@chromium.org6141cbe2009-11-20 12:14:52 +0000691 static bool LookupTwoCharsSymbolIfExists(String* str, String** symbol);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692
693 // Compute the matching symbol map for a string if possible.
694 // NULL is returned if string is in new space or not flattened.
695 static Map* SymbolMapForString(String* str);
696
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000697 // Tries to flatten a string before compare operation.
698 //
699 // Returns a failure in case it was decided that flattening was
700 // necessary and failed. Note, if flattening is not necessary the
701 // string might stay non-flat even when not a failure is returned.
702 //
703 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000704 MUST_USE_RESULT static inline MaybeObject* PrepareForCompare(String* str);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000705
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 // Converts the given boolean condition to JavaScript boolean value.
707 static Object* ToBoolean(bool condition) {
708 return condition ? true_value() : false_value();
709 }
710
711 // Code that should be run before and after each GC. Includes some
712 // reporting/verification activities when compiled with DEBUG set.
713 static void GarbageCollectionPrologue();
714 static void GarbageCollectionEpilogue();
715
716 // Performs garbage collection operation.
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000717 // Returns whether there is a chance that another major GC could
718 // collect more garbage.
719 static bool CollectGarbage(AllocationSpace space, GarbageCollector collector);
720
721 // Performs garbage collection operation.
722 // Returns whether there is a chance that another major GC could
723 // collect more garbage.
724 inline static bool CollectGarbage(AllocationSpace space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000726 // Performs a full garbage collection. Force compaction if the
727 // parameter is true.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000728 static void CollectAllGarbage(bool force_compaction);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000729
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +0000730 // Last hope GC, should try to squeeze as much as possible.
731 static void CollectAllAvailableGarbage();
732
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000733 // Notify the heap that a context has been disposed.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000734 static int NotifyContextDisposed() { return ++contexts_disposed_; }
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000735
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000736 // Utility to invoke the scavenger. This is needed in test code to
737 // ensure correct callback for weak global handles.
kasper.lund7276f142008-07-30 08:49:36 +0000738 static void PerformScavenge();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000740#ifdef DEBUG
741 // Utility used with flag gc-greedy.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000742 static void GarbageCollectionGreedyCheck();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000743#endif
744
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000745 static void AddGCPrologueCallback(
746 GCEpilogueCallback callback, GCType gc_type_filter);
747 static void RemoveGCPrologueCallback(GCEpilogueCallback callback);
748
749 static void AddGCEpilogueCallback(
750 GCEpilogueCallback callback, GCType gc_type_filter);
751 static void RemoveGCEpilogueCallback(GCEpilogueCallback callback);
752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 static void SetGlobalGCPrologueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000754 ASSERT((callback == NULL) ^ (global_gc_prologue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 global_gc_prologue_callback_ = callback;
756 }
757 static void SetGlobalGCEpilogueCallback(GCCallback callback) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000758 ASSERT((callback == NULL) ^ (global_gc_epilogue_callback_ == NULL));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 global_gc_epilogue_callback_ = callback;
760 }
761
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000762 // Heap root getters. We have versions with and without type::cast() here.
763 // You can't use type::cast during GC because the assert fails.
764#define ROOT_ACCESSOR(type, name, camel_name) \
765 static inline type* name() { \
766 return type::cast(roots_[k##camel_name##RootIndex]); \
767 } \
768 static inline type* raw_unchecked_##name() { \
769 return reinterpret_cast<type*>(roots_[k##camel_name##RootIndex]); \
770 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771 ROOT_LIST(ROOT_ACCESSOR)
772#undef ROOT_ACCESSOR
773
774// Utility type maps
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000775#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
776 static inline Map* name##_map() { \
777 return Map::cast(roots_[k##Name##MapRootIndex]); \
778 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 STRUCT_LIST(STRUCT_MAP_ACCESSOR)
780#undef STRUCT_MAP_ACCESSOR
781
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000782#define SYMBOL_ACCESSOR(name, str) static inline String* name() { \
783 return String::cast(roots_[k##name##RootIndex]); \
784 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 SYMBOL_LIST(SYMBOL_ACCESSOR)
786#undef SYMBOL_ACCESSOR
787
ager@chromium.org3b45ab52009-03-19 22:21:34 +0000788 // The hidden_symbol is special because it is the empty string, but does
789 // not match the empty string.
790 static String* hidden_symbol() { return hidden_symbol_; }
791
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000792 static void set_global_contexts_list(Object* object) {
793 global_contexts_list_ = object;
794 }
795 static Object* global_contexts_list() { return global_contexts_list_; }
796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 // Iterates over all roots in the heap.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000798 static void IterateRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000799 // Iterates over all strong roots in the heap.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000800 static void IterateStrongRoots(ObjectVisitor* v, VisitMode mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000801 // Iterates over all the other roots in the heap.
802 static void IterateWeakRoots(ObjectVisitor* v, VisitMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000804 enum ExpectedPageWatermarkState {
805 WATERMARK_SHOULD_BE_VALID,
806 WATERMARK_CAN_BE_INVALID
807 };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000808
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000809 // For each dirty region on a page in use from an old space call
810 // visit_dirty_region callback.
811 // If either visit_dirty_region or callback can cause an allocation
812 // in old space and changes in allocation watermark then
813 // can_preallocate_during_iteration should be set to true.
814 // All pages will be marked as having invalid watermark upon
815 // iteration completion.
816 static void IterateDirtyRegions(
817 PagedSpace* space,
818 DirtyRegionCallback visit_dirty_region,
819 ObjectSlotCallback callback,
820 ExpectedPageWatermarkState expected_page_watermark_state);
821
822 // Interpret marks as a bitvector of dirty marks for regions of size
823 // Page::kRegionSize aligned by Page::kRegionAlignmentMask and covering
824 // memory interval from start to top. For each dirty region call a
825 // visit_dirty_region callback. Return updated bitvector of dirty marks.
826 static uint32_t IterateDirtyRegions(uint32_t marks,
827 Address start,
828 Address end,
829 DirtyRegionCallback visit_dirty_region,
830 ObjectSlotCallback callback);
831
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000832 // Iterate pointers to from semispace of new space found in memory interval
833 // from start to end.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000834 // Update dirty marks for page containing start address.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000835 static void IterateAndMarkPointersToFromSpace(Address start,
836 Address end,
837 ObjectSlotCallback callback);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000838
839 // Iterate pointers to new space found in memory interval from start to end.
840 // Return true if pointers to new space was found.
841 static bool IteratePointersInDirtyRegion(Address start,
842 Address end,
843 ObjectSlotCallback callback);
844
845
846 // Iterate pointers to new space found in memory interval from start to end.
847 // This interval is considered to belong to the map space.
848 // Return true if pointers to new space was found.
849 static bool IteratePointersInDirtyMapsRegion(Address start,
850 Address end,
851 ObjectSlotCallback callback);
852
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000853
854 // Returns whether the object resides in new space.
855 static inline bool InNewSpace(Object* object);
856 static inline bool InFromSpace(Object* object);
857 static inline bool InToSpace(Object* object);
858
859 // Checks whether an address/object in the heap (including auxiliary
860 // area and unused area).
861 static bool Contains(Address addr);
862 static bool Contains(HeapObject* value);
863
864 // Checks whether an address/object in a space.
ager@chromium.org3811b432009-10-28 14:53:37 +0000865 // Currently used by tests, serialization and heap verification only.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 static bool InSpace(Address addr, AllocationSpace space);
867 static bool InSpace(HeapObject* value, AllocationSpace space);
868
kasper.lund7276f142008-07-30 08:49:36 +0000869 // Finds out which space an object should get promoted to based on its type.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000870 static inline OldSpace* TargetSpace(HeapObject* object);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000871 static inline AllocationSpace TargetSpaceId(InstanceType type);
kasper.lund7276f142008-07-30 08:49:36 +0000872
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 // Sets the stub_cache_ (only used when expanding the dictionary).
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000874 static void public_set_code_stubs(NumberDictionary* value) {
875 roots_[kCodeStubsRootIndex] = value;
876 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000877
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000878 // Support for computing object sizes for old objects during GCs. Returns
879 // a function that is guaranteed to be safe for computing object sizes in
880 // the current GC phase.
881 static HeapObjectCallback GcSafeSizeOfOldObjectFunction() {
882 return gc_safe_size_of_old_object_;
883 }
884
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885 // Sets the non_monomorphic_cache_ (only used when expanding the dictionary).
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000886 static void public_set_non_monomorphic_cache(NumberDictionary* value) {
887 roots_[kNonMonomorphicCacheRootIndex] = value;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 }
889
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000890 static void public_set_empty_script(Script* script) {
891 roots_[kEmptyScriptRootIndex] = script;
892 }
893
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000894 // Update the next script id.
895 static inline void SetLastScriptId(Object* last_script_id);
896
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000897 // Generated code can embed this address to get access to the roots.
898 static Object** roots_address() { return roots_; }
899
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000900 // Get address of global contexts list for serialization support.
901 static Object** global_contexts_list_address() {
902 return &global_contexts_list_;
903 }
904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905#ifdef DEBUG
906 static void Print();
907 static void PrintHandles();
908
909 // Verify the heap is in its normal state before or after a GC.
910 static void Verify();
911
912 // Report heap statistics.
913 static void ReportHeapStatistics(const char* title);
914 static void ReportCodeStatistics(const char* title);
915
916 // Fill in bogus values in from space
917 static void ZapFromSpace();
918#endif
919
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000920#if defined(ENABLE_LOGGING_AND_PROFILING)
921 // Print short heap statistics.
922 static void PrintShortHeapStatistics();
923#endif
924
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000925 // Makes a new symbol object
926 // Returns Failure::RetryAfterGC(requested_bytes, space) if the allocation
927 // failed.
928 // Please note this function does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000929 MUST_USE_RESULT static MaybeObject* CreateSymbol(const char* str,
930 int length,
931 int hash);
932 MUST_USE_RESULT static MaybeObject* CreateSymbol(String* str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000933
934 // Write barrier support for address[offset] = o.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000935 static inline void RecordWrite(Address address, int offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000936
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000937 // Write barrier support for address[start : start + len[ = o.
938 static inline void RecordWrites(Address address, int start, int len);
939
kasper.lund7276f142008-07-30 08:49:36 +0000940 // Given an address occupied by a live code object, return that object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941 static Object* FindCodeObject(Address a);
942
943 // Invoke Shrink on shrinkable spaces.
944 static void Shrink();
945
946 enum HeapState { NOT_IN_GC, SCAVENGE, MARK_COMPACT };
947 static inline HeapState gc_state() { return gc_state_; }
948
949#ifdef DEBUG
950 static bool IsAllocationAllowed() { return allocation_allowed_; }
951 static inline bool allow_allocation(bool enable);
952
953 static bool disallow_allocation_failure() {
954 return disallow_allocation_failure_;
955 }
956
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000957 static void TracePathToObject(Object* target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000958 static void TracePathToGlobal();
959#endif
960
ager@chromium.org32912102009-01-16 10:38:43 +0000961 // Callback function passed to Heap::Iterate etc. Copies an object if
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000962 // necessary, the object might be promoted to an old space. The caller must
963 // ensure the precondition that the object is (a) a heap object and (b) in
964 // the heap's from space.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000965 static void ScavengePointer(HeapObject** p);
966 static inline void ScavengeObject(HeapObject** p, HeapObject* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000967
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000968 // Commits from space if it is uncommitted.
969 static void EnsureFromSpaceIsCommitted();
970
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000971 // Support for partial snapshots. After calling this we can allocate a
972 // certain number of bytes using only linear allocation (with a
973 // LinearAllocationScope and an AlwaysAllocateScope) without using freelists
974 // or causing a GC. It returns true of space was reserved or false if a GC is
975 // needed. For paged spaces the space requested must include the space wasted
976 // at the end of each page when allocating linearly.
977 static void ReserveSpace(
978 int new_space_size,
979 int pointer_space_size,
980 int data_space_size,
981 int code_space_size,
982 int map_space_size,
983 int cell_space_size,
984 int large_object_size);
985
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 //
987 // Support for the API.
988 //
989
990 static bool CreateApiObjects();
991
992 // Attempt to find the number in a small cache. If we finds it, return
993 // the string representation of the number. Otherwise return undefined.
994 static Object* GetNumberStringCache(Object* number);
995
996 // Update the cache with a new number-string pair.
997 static void SetNumberStringCache(Object* number, String* str);
998
kasper.lund7276f142008-07-30 08:49:36 +0000999 // Adjusts the amount of registered external memory.
1000 // Returns the adjusted value.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001001 static inline int AdjustAmountOfExternalAllocatedMemory(int change_in_bytes);
kasper.lund7276f142008-07-30 08:49:36 +00001002
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001003 // Allocate uninitialized fixed array.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001004 MUST_USE_RESULT static MaybeObject* AllocateRawFixedArray(int length);
1005 MUST_USE_RESULT static MaybeObject* AllocateRawFixedArray(
1006 int length,
1007 PretenureFlag pretenure);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001008
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001009 // True if we have reached the allocation limit in the old generation that
1010 // should force the next GC (caused normally) to be a full one.
1011 static bool OldGenerationPromotionLimitReached() {
1012 return (PromotedSpaceSize() + PromotedExternalMemorySize())
1013 > old_gen_promotion_limit_;
1014 }
1015
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001016 static intptr_t OldGenerationSpaceAvailable() {
1017 return old_gen_allocation_limit_ -
1018 (PromotedSpaceSize() + PromotedExternalMemorySize());
1019 }
1020
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001021 // True if we have reached the allocation limit in the old generation that
1022 // should artificially cause a GC right now.
1023 static bool OldGenerationAllocationLimitReached() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001024 return OldGenerationSpaceAvailable() < 0;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001025 }
1026
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001027 // Can be called when the embedding application is idle.
ager@chromium.org96c75b52009-08-26 09:13:16 +00001028 static bool IdleNotification();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001029
1030 // Declare all the root indices.
1031 enum RootListIndex {
1032#define ROOT_INDEX_DECLARATION(type, name, camel_name) k##camel_name##RootIndex,
1033 STRONG_ROOT_LIST(ROOT_INDEX_DECLARATION)
1034#undef ROOT_INDEX_DECLARATION
1035
1036// Utility type maps
1037#define DECLARE_STRUCT_MAP(NAME, Name, name) k##Name##MapRootIndex,
1038 STRUCT_LIST(DECLARE_STRUCT_MAP)
1039#undef DECLARE_STRUCT_MAP
1040
1041#define SYMBOL_INDEX_DECLARATION(name, str) k##name##RootIndex,
1042 SYMBOL_LIST(SYMBOL_INDEX_DECLARATION)
1043#undef SYMBOL_DECLARATION
1044
1045 kSymbolTableRootIndex,
1046 kStrongRootListLength = kSymbolTableRootIndex,
1047 kRootListLength
1048 };
1049
lrn@chromium.org303ada72010-10-27 09:33:13 +00001050 MUST_USE_RESULT static MaybeObject* NumberToString(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001051 Object* number,
1052 bool check_number_string_cache = true);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001053
ager@chromium.org3811b432009-10-28 14:53:37 +00001054 static Map* MapForExternalArrayType(ExternalArrayType array_type);
1055 static RootListIndex RootIndexForExternalArrayType(
1056 ExternalArrayType array_type);
1057
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001058 static void RecordStats(HeapStats* stats, bool take_snapshot = false);
ager@chromium.org60121232009-12-03 11:25:37 +00001059
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001060 // Copy block of memory from src to dst. Size of block should be aligned
1061 // by pointer size.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001062 static inline void CopyBlock(Address dst, Address src, int byte_size);
1063
1064 static inline void CopyBlockToOldSpaceAndUpdateRegionMarks(Address dst,
1065 Address src,
1066 int byte_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001067
1068 // Optimized version of memmove for blocks with pointer size aligned sizes and
1069 // pointer size aligned addresses.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001070 static inline void MoveBlock(Address dst, Address src, int byte_size);
1071
1072 static inline void MoveBlockToOldSpaceAndUpdateRegionMarks(Address dst,
1073 Address src,
1074 int byte_size);
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001075
1076 // Check new space expansion criteria and expand semispaces if it was hit.
1077 static void CheckNewSpaceExpansionCriteria();
1078
1079 static inline void IncrementYoungSurvivorsCounter(int survived) {
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001080 young_survivors_after_last_gc_ = survived;
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001081 survived_since_last_expansion_ += survived;
1082 }
1083
1084 static void UpdateNewSpaceReferencesInExternalStringTable(
1085 ExternalStringTableUpdaterCallback updater_func);
1086
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001087 static void ProcessWeakReferences(WeakObjectRetainer* retainer);
1088
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001089 // Helper function that governs the promotion policy from new space to
1090 // old. If the object's old address lies below the new space's age
1091 // mark or if we've already filled the bottom 1/16th of the to space,
1092 // we try to promote this object.
1093 static inline bool ShouldBePromoted(Address old_address, int object_size);
1094
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001095 static int MaxObjectSizeInNewSpace() { return kMaxObjectSizeInNewSpace; }
1096
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001097 static void ClearJSFunctionResultCaches();
1098
ricow@chromium.org65fae842010-08-25 15:26:24 +00001099 static void ClearNormalizedMapCaches();
1100
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001101 static GCTracer* tracer() { return tracer_; }
1102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 private:
ager@chromium.org3811b432009-10-28 14:53:37 +00001104 static int reserved_semispace_size_;
1105 static int max_semispace_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 static int initial_semispace_size_;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001107 static intptr_t max_old_generation_size_;
ager@chromium.org01fe7df2010-11-10 11:59:11 +00001108 static intptr_t max_executable_size_;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001109 static intptr_t code_range_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001111 // For keeping track of how much data has survived
1112 // scavenge since last new space expansion.
1113 static int survived_since_last_expansion_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001115 static int always_allocate_scope_depth_;
ager@chromium.org3811b432009-10-28 14:53:37 +00001116 static int linear_allocation_scope_depth_;
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +00001117
1118 // For keeping track of context disposals.
1119 static int contexts_disposed_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001120
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001121#if defined(V8_TARGET_ARCH_X64)
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001122 static const int kMaxObjectSizeInNewSpace = 1024*KB;
lrn@chromium.org8541d772010-12-15 12:05:09 +00001123#else
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001124 static const int kMaxObjectSizeInNewSpace = 512*KB;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001125#endif
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001126
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001127 static NewSpace new_space_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001128 static OldSpace* old_pointer_space_;
1129 static OldSpace* old_data_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130 static OldSpace* code_space_;
1131 static MapSpace* map_space_;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001132 static CellSpace* cell_space_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 static LargeObjectSpace* lo_space_;
1134 static HeapState gc_state_;
1135
1136 // Returns the size of object residing in non new spaces.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001137 static intptr_t PromotedSpaceSize();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138
kasper.lund7276f142008-07-30 08:49:36 +00001139 // Returns the amount of external memory registered since last global gc.
1140 static int PromotedExternalMemorySize();
1141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 static int mc_count_; // how many mark-compact collections happened
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001143 static int ms_count_; // how many mark-sweep collections happened
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144 static int gc_count_; // how many gc happened
1145
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001146 // Total length of the strings we failed to flatten since the last GC.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001147 static int unflattened_strings_length_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001148
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001149#define ROOT_ACCESSOR(type, name, camel_name) \
1150 static inline void set_##name(type* value) { \
1151 roots_[k##camel_name##RootIndex] = value; \
1152 }
1153 ROOT_LIST(ROOT_ACCESSOR)
1154#undef ROOT_ACCESSOR
1155
kasper.lund7276f142008-07-30 08:49:36 +00001156#ifdef DEBUG
1157 static bool allocation_allowed_;
1158
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159 // If the --gc-interval flag is set to a positive value, this
1160 // variable holds the value indicating the number of allocations
1161 // remain until the next failure and garbage collection.
1162 static int allocation_timeout_;
1163
1164 // Do we expect to be able to handle allocation failure at this
1165 // time?
1166 static bool disallow_allocation_failure_;
1167#endif // DEBUG
1168
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001169 // Limit that triggers a global GC on the next (normally caused) GC. This
1170 // is checked when we have already decided to do a GC to help determine
1171 // which collector to invoke.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001172 static intptr_t old_gen_promotion_limit_;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001173
1174 // Limit that triggers a global GC as soon as is reasonable. This is
1175 // checked before expanding a paged space in the old generation and on
1176 // every allocation in large object space.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001177 static intptr_t old_gen_allocation_limit_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001179 // Limit on the amount of externally allocated memory allowed
1180 // between global GCs. If reached a global GC is forced.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001181 static intptr_t external_allocation_limit_;
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001182
kasper.lund7276f142008-07-30 08:49:36 +00001183 // The amount of external memory registered through the API kept alive
1184 // by global handles
1185 static int amount_of_external_allocated_memory_;
1186
1187 // Caches the amount of external memory registered at the last global gc.
1188 static int amount_of_external_allocated_memory_at_last_global_gc_;
1189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 // Indicates that an allocation has failed in the old generation since the
1191 // last GC.
1192 static int old_gen_exhausted_;
1193
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001194 static Object* roots_[kRootListLength];
1195
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00001196 static Object* global_contexts_list_;
1197
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001198 struct StringTypeTable {
1199 InstanceType type;
1200 int size;
1201 RootListIndex index;
1202 };
1203
1204 struct ConstantSymbolTable {
1205 const char* contents;
1206 RootListIndex index;
1207 };
1208
1209 struct StructTable {
1210 InstanceType type;
1211 int size;
1212 RootListIndex index;
1213 };
1214
1215 static const StringTypeTable string_type_table[];
1216 static const ConstantSymbolTable constant_symbol_table[];
1217 static const StructTable struct_table[];
1218
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001219 // The special hidden symbol which is an empty string, but does not match
1220 // any string when looked up in properties.
1221 static String* hidden_symbol_;
1222
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 // GC callback function, called before and after mark-compact GC.
1224 // Allocations in the callback function are disallowed.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001225 struct GCPrologueCallbackPair {
1226 GCPrologueCallbackPair(GCPrologueCallback callback, GCType gc_type)
1227 : callback(callback), gc_type(gc_type) {
1228 }
1229 bool operator==(const GCPrologueCallbackPair& pair) const {
1230 return pair.callback == callback;
1231 }
1232 GCPrologueCallback callback;
1233 GCType gc_type;
1234 };
1235 static List<GCPrologueCallbackPair> gc_prologue_callbacks_;
1236
1237 struct GCEpilogueCallbackPair {
1238 GCEpilogueCallbackPair(GCEpilogueCallback callback, GCType gc_type)
1239 : callback(callback), gc_type(gc_type) {
1240 }
1241 bool operator==(const GCEpilogueCallbackPair& pair) const {
1242 return pair.callback == callback;
1243 }
1244 GCEpilogueCallback callback;
1245 GCType gc_type;
1246 };
1247 static List<GCEpilogueCallbackPair> gc_epilogue_callbacks_;
1248
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 static GCCallback global_gc_prologue_callback_;
1250 static GCCallback global_gc_epilogue_callback_;
1251
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001252 // Support for computing object sizes during GC.
1253 static HeapObjectCallback gc_safe_size_of_old_object_;
1254 static int GcSafeSizeOfOldObject(HeapObject* object);
1255 static int GcSafeSizeOfOldObjectWithEncodedMap(HeapObject* object);
1256
1257 // Update the GC state. Called from the mark-compact collector.
1258 static void MarkMapPointersAsEncoded(bool encoded) {
1259 gc_safe_size_of_old_object_ = encoded
1260 ? &GcSafeSizeOfOldObjectWithEncodedMap
1261 : &GcSafeSizeOfOldObject;
1262 }
1263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 // Checks whether a global GC is necessary
1265 static GarbageCollector SelectGarbageCollector(AllocationSpace space);
1266
1267 // Performs garbage collection
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +00001268 // Returns whether there is a chance another major GC could
1269 // collect more garbage.
1270 static bool PerformGarbageCollection(GarbageCollector collector,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001271 GCTracer* tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001272
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001273 // Allocate an uninitialized object in map space. The behavior is identical
1274 // to Heap::AllocateRaw(size_in_bytes, MAP_SPACE), except that (a) it doesn't
1275 // have to test the allocation space argument and (b) can reduce code size
1276 // (since both AllocateRaw and AllocateRawMap are inlined).
lrn@chromium.org303ada72010-10-27 09:33:13 +00001277 MUST_USE_RESULT static inline MaybeObject* AllocateRawMap();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001278
1279 // Allocate an uninitialized object in the global property cell space.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001280 MUST_USE_RESULT static inline MaybeObject* AllocateRawCell();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282 // Initializes a JSObject based on its map.
1283 static void InitializeJSObjectFromMap(JSObject* obj,
1284 FixedArray* properties,
1285 Map* map);
1286
1287 static bool CreateInitialMaps();
1288 static bool CreateInitialObjects();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001289
sgjesse@chromium.org76ae6992010-08-05 15:54:25 +00001290 // These four Create*EntryStub functions are here and forced to not be inlined
1291 // because of a gcc-4.4 bug that assigns wrong vtable entries.
1292 NO_INLINE(static void CreateCEntryStub());
1293 NO_INLINE(static void CreateJSEntryStub());
1294 NO_INLINE(static void CreateJSConstructEntryStub());
1295 NO_INLINE(static void CreateRegExpCEntryStub());
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001296
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297 static void CreateFixedStubs();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001298
lrn@chromium.org303ada72010-10-27 09:33:13 +00001299 MUST_USE_RESULT static MaybeObject* CreateOddball(const char* to_string,
1300 Object* to_number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301
1302 // Allocate empty fixed array.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001303 MUST_USE_RESULT static MaybeObject* AllocateEmptyFixedArray();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304
1305 // Performs a minor collection in new generation.
1306 static void Scavenge();
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00001307
1308 static String* UpdateNewSpaceReferenceInExternalStringTableEntry(
1309 Object** pointer);
1310
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001311 static Address DoScavenge(ObjectVisitor* scavenge_visitor,
1312 Address new_space_front);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313
1314 // Performs a major collection in the whole heap.
kasper.lund7276f142008-07-30 08:49:36 +00001315 static void MarkCompact(GCTracer* tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316
1317 // Code to be run before and after mark-compact.
kasperl@chromium.org061ef742009-02-27 12:16:20 +00001318 static void MarkCompactPrologue(bool is_compacting);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001320 // Completely clear the Instanceof cache (to stop it keeping objects alive
1321 // around a GC).
1322 static void CompletelyClearInstanceofCache() {
1323 set_instanceof_cache_map(the_hole_value());
1324 set_instanceof_cache_function(the_hole_value());
1325 }
1326
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 // Record statistics before and after garbage collection.
1329 static void ReportStatisticsBeforeGC();
1330 static void ReportStatisticsAfterGC();
1331#endif
1332
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001333 // Slow part of scavenge object.
1334 static void ScavengeObjectSlow(HeapObject** p, HeapObject* object);
1335
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001336 // Initializes a function with a shared part and prototype.
1337 // Returns the function.
1338 // Note: this code was factored out of AllocateFunction such that
1339 // other parts of the VM could use it. Specifically, a function that creates
1340 // instances of type JS_FUNCTION_TYPE benefit from the use of this function.
1341 // Please note this does not perform a garbage collection.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001342 MUST_USE_RESULT static inline MaybeObject* InitializeFunction(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001343 JSFunction* function,
1344 SharedFunctionInfo* shared,
1345 Object* prototype);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001346
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001347 static GCTracer* tracer_;
1348
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001349
1350 // Initializes the number to string cache based on the max semispace size.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001351 MUST_USE_RESULT static MaybeObject* InitializeNumberStringCache();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001352 // Flush the number to string cache.
1353 static void FlushNumberStringCache();
1354
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00001355 static void UpdateSurvivalRateTrend(int start_new_space_size);
1356
1357 enum SurvivalRateTrend { INCREASING, STABLE, DECREASING, FLUCTUATING };
1358
1359 static const int kYoungSurvivalRateThreshold = 90;
1360 static const int kYoungSurvivalRateAllowedDeviation = 15;
1361
1362 static int young_survivors_after_last_gc_;
1363 static int high_survival_rate_period_length_;
1364 static double survival_rate_;
1365 static SurvivalRateTrend previous_survival_rate_trend_;
1366 static SurvivalRateTrend survival_rate_trend_;
1367
1368 static void set_survival_rate_trend(SurvivalRateTrend survival_rate_trend) {
1369 ASSERT(survival_rate_trend != FLUCTUATING);
1370 previous_survival_rate_trend_ = survival_rate_trend_;
1371 survival_rate_trend_ = survival_rate_trend;
1372 }
1373
1374 static SurvivalRateTrend survival_rate_trend() {
1375 if (survival_rate_trend_ == STABLE) {
1376 return STABLE;
1377 } else if (previous_survival_rate_trend_ == STABLE) {
1378 return survival_rate_trend_;
1379 } else if (survival_rate_trend_ != previous_survival_rate_trend_) {
1380 return FLUCTUATING;
1381 } else {
1382 return survival_rate_trend_;
1383 }
1384 }
1385
1386 static bool IsStableOrIncreasingSurvivalTrend() {
1387 switch (survival_rate_trend()) {
1388 case STABLE:
1389 case INCREASING:
1390 return true;
1391 default:
1392 return false;
1393 }
1394 }
1395
1396 static bool IsIncreasingSurvivalTrend() {
1397 return survival_rate_trend() == INCREASING;
1398 }
1399
1400 static bool IsHighSurvivalRate() {
1401 return high_survival_rate_period_length_ > 0;
1402 }
1403
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404 static const int kInitialSymbolTableSize = 2048;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001405 static const int kInitialEvalCacheSize = 64;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406
1407 friend class Factory;
1408 friend class DisallowAllocationFailure;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001409 friend class AlwaysAllocateScope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001410 friend class LinearAllocationScope;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00001411 friend class MarkCompactCollector;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001412};
1413
1414
ager@chromium.org67368652009-12-03 11:40:49 +00001415class HeapStats {
1416 public:
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001417 static const int kStartMarker = 0xDECADE00;
1418 static const int kEndMarker = 0xDECADE01;
1419
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001420 int* start_marker; // 0
1421 int* new_space_size; // 1
1422 int* new_space_capacity; // 2
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001423 intptr_t* old_pointer_space_size; // 3
1424 intptr_t* old_pointer_space_capacity; // 4
1425 intptr_t* old_data_space_size; // 5
1426 intptr_t* old_data_space_capacity; // 6
1427 intptr_t* code_space_size; // 7
1428 intptr_t* code_space_capacity; // 8
1429 intptr_t* map_space_size; // 9
1430 intptr_t* map_space_capacity; // 10
1431 intptr_t* cell_space_size; // 11
1432 intptr_t* cell_space_capacity; // 12
1433 intptr_t* lo_space_size; // 13
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001434 int* global_handle_count; // 14
1435 int* weak_global_handle_count; // 15
1436 int* pending_global_handle_count; // 16
1437 int* near_death_global_handle_count; // 17
1438 int* destroyed_global_handle_count; // 18
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001439 intptr_t* memory_allocator_size; // 19
1440 intptr_t* memory_allocator_capacity; // 20
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001441 int* objects_per_type; // 21
1442 int* size_per_type; // 22
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001443 int* os_error; // 23
1444 int* end_marker; // 24
ager@chromium.org60121232009-12-03 11:25:37 +00001445};
1446
1447
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001448class AlwaysAllocateScope {
1449 public:
1450 AlwaysAllocateScope() {
1451 // We shouldn't hit any nested scopes, because that requires
1452 // non-handle code to call handle code. The code still works but
1453 // performance will degrade, so we want to catch this situation
1454 // in debug mode.
1455 ASSERT(Heap::always_allocate_scope_depth_ == 0);
1456 Heap::always_allocate_scope_depth_++;
1457 }
1458
1459 ~AlwaysAllocateScope() {
1460 Heap::always_allocate_scope_depth_--;
1461 ASSERT(Heap::always_allocate_scope_depth_ == 0);
1462 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001463};
1464
1465
ager@chromium.org3811b432009-10-28 14:53:37 +00001466class LinearAllocationScope {
1467 public:
1468 LinearAllocationScope() {
1469 Heap::linear_allocation_scope_depth_++;
1470 }
1471
1472 ~LinearAllocationScope() {
1473 Heap::linear_allocation_scope_depth_--;
1474 ASSERT(Heap::linear_allocation_scope_depth_ >= 0);
1475 }
1476};
1477
1478
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479#ifdef DEBUG
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001480// Visitor class to verify interior pointers in spaces that do not contain
1481// or care about intergenerational references. All heap object pointers have to
1482// point into the heap to a location that has a map pointer at its first word.
1483// Caveat: Heap::Contains is an approximation because it can return true for
1484// objects in a heap space but above the allocation pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485class VerifyPointersVisitor: public ObjectVisitor {
1486 public:
1487 void VisitPointers(Object** start, Object** end) {
1488 for (Object** current = start; current < end; current++) {
1489 if ((*current)->IsHeapObject()) {
1490 HeapObject* object = HeapObject::cast(*current);
1491 ASSERT(Heap::Contains(object));
1492 ASSERT(object->map()->IsMap());
1493 }
1494 }
1495 }
1496};
1497
1498
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001499// Visitor class to verify interior pointers in spaces that use region marks
1500// to keep track of intergenerational references.
1501// As VerifyPointersVisitor but also checks that dirty marks are set
1502// for regions covering intergenerational references.
1503class VerifyPointersAndDirtyRegionsVisitor: public ObjectVisitor {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 public:
1505 void VisitPointers(Object** start, Object** end) {
1506 for (Object** current = start; current < end; current++) {
1507 if ((*current)->IsHeapObject()) {
1508 HeapObject* object = HeapObject::cast(*current);
1509 ASSERT(Heap::Contains(object));
1510 ASSERT(object->map()->IsMap());
1511 if (Heap::InNewSpace(object)) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001512 ASSERT(Heap::InToSpace(object));
1513 Address addr = reinterpret_cast<Address>(current);
1514 ASSERT(Page::FromAddress(addr)->IsRegionDirty(addr));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515 }
1516 }
1517 }
1518 }
1519};
1520#endif
1521
1522
kasper.lund7276f142008-07-30 08:49:36 +00001523// Space iterator for iterating over all spaces of the heap.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001524// Returns each space in turn, and null when it is done.
1525class AllSpaces BASE_EMBEDDED {
1526 public:
1527 Space* next();
1528 AllSpaces() { counter_ = FIRST_SPACE; }
1529 private:
1530 int counter_;
1531};
1532
1533
1534// Space iterator for iterating over all old spaces of the heap: Old pointer
1535// space, old data space and code space.
1536// Returns each space in turn, and null when it is done.
1537class OldSpaces BASE_EMBEDDED {
1538 public:
1539 OldSpace* next();
1540 OldSpaces() { counter_ = OLD_POINTER_SPACE; }
1541 private:
1542 int counter_;
1543};
1544
1545
1546// Space iterator for iterating over all the paged spaces of the heap:
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001547// Map space, old pointer space, old data space, code space and cell space.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001548// Returns each space in turn, and null when it is done.
1549class PagedSpaces BASE_EMBEDDED {
1550 public:
1551 PagedSpace* next();
1552 PagedSpaces() { counter_ = OLD_POINTER_SPACE; }
1553 private:
1554 int counter_;
1555};
1556
1557
1558// Space iterator for iterating over all spaces of the heap.
kasper.lund7276f142008-07-30 08:49:36 +00001559// For each space an object iterator is provided. The deallocation of the
1560// returned object iterators is handled by the space iterator.
kasper.lund7276f142008-07-30 08:49:36 +00001561class SpaceIterator : public Malloced {
1562 public:
1563 SpaceIterator();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001564 explicit SpaceIterator(HeapObjectCallback size_func);
kasper.lund7276f142008-07-30 08:49:36 +00001565 virtual ~SpaceIterator();
1566
1567 bool has_next();
1568 ObjectIterator* next();
1569
1570 private:
1571 ObjectIterator* CreateIterator();
1572
1573 int current_space_; // from enum AllocationSpace.
1574 ObjectIterator* iterator_; // object iterator for the current space.
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001575 HeapObjectCallback size_func_;
kasper.lund7276f142008-07-30 08:49:36 +00001576};
1577
1578
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001579// A HeapIterator provides iteration over the whole heap. It
1580// aggregates the specific iterators for the different spaces as
1581// these can only iterate over one space only.
1582//
1583// HeapIterator can skip free list nodes (that is, de-allocated heap
1584// objects that still remain in the heap). As implementation of free
1585// nodes filtering uses GC marks, it can't be used during MS/MC GC
1586// phases. Also, it is forbidden to interrupt iteration in this mode,
1587// as this will leave heap objects marked (and thus, unusable).
1588class FreeListNodesFilter;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589
1590class HeapIterator BASE_EMBEDDED {
1591 public:
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001592 enum FreeListNodesFiltering {
1593 kNoFiltering,
1594 kPreciseFiltering
1595 };
1596
1597 HeapIterator();
1598 explicit HeapIterator(FreeListNodesFiltering filtering);
1599 ~HeapIterator();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 HeapObject* next();
1602 void reset();
1603
1604 private:
1605 // Perform the initialization.
1606 void Init();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 // Perform all necessary shutdown (destruction) work.
1608 void Shutdown();
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001609 HeapObject* NextObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00001611 FreeListNodesFiltering filtering_;
1612 FreeListNodesFilter* filter_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613 // Space iterator for iterating all the spaces.
1614 SpaceIterator* space_iterator_;
1615 // Object iterator for the space currently being iterated.
1616 ObjectIterator* object_iterator_;
1617};
1618
1619
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001620// Cache for mapping (map, property name) into field offset.
1621// Cleared at startup and prior to mark sweep collection.
1622class KeyedLookupCache {
1623 public:
1624 // Lookup field offset for (map, name). If absent, -1 is returned.
1625 static int Lookup(Map* map, String* name);
1626
1627 // Update an element in the cache.
1628 static void Update(Map* map, String* name, int field_offset);
1629
1630 // Clear the cache.
1631 static void Clear();
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001632
1633 static const int kLength = 64;
1634 static const int kCapacityMask = kLength - 1;
1635 static const int kMapHashShift = 2;
1636
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001637 private:
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001638 static inline int Hash(Map* map, String* name);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001639
1640 // Get the address of the keys and field_offsets arrays. Used in
1641 // generated code to perform cache lookups.
1642 static Address keys_address() {
1643 return reinterpret_cast<Address>(&keys_);
1644 }
1645
1646 static Address field_offsets_address() {
1647 return reinterpret_cast<Address>(&field_offsets_);
1648 }
1649
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001650 struct Key {
1651 Map* map;
1652 String* name;
1653 };
1654 static Key keys_[kLength];
1655 static int field_offsets_[kLength];
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001656
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001657 friend class ExternalReference;
1658};
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001659
1660
1661// Cache for mapping (array, property name) into descriptor index.
1662// The cache contains both positive and negative results.
1663// Descriptor index equals kNotFound means the property is absent.
1664// Cleared at startup and prior to any gc.
1665class DescriptorLookupCache {
1666 public:
1667 // Lookup descriptor index for (map, name).
1668 // If absent, kAbsent is returned.
1669 static int Lookup(DescriptorArray* array, String* name) {
1670 if (!StringShape(name).IsSymbol()) return kAbsent;
1671 int index = Hash(array, name);
1672 Key& key = keys_[index];
1673 if ((key.array == array) && (key.name == name)) return results_[index];
1674 return kAbsent;
1675 }
1676
1677 // Update an element in the cache.
1678 static void Update(DescriptorArray* array, String* name, int result) {
1679 ASSERT(result != kAbsent);
1680 if (StringShape(name).IsSymbol()) {
1681 int index = Hash(array, name);
1682 Key& key = keys_[index];
1683 key.array = array;
1684 key.name = name;
1685 results_[index] = result;
1686 }
1687 }
1688
1689 // Clear the cache.
1690 static void Clear();
1691
1692 static const int kAbsent = -2;
1693 private:
1694 static int Hash(DescriptorArray* array, String* name) {
1695 // Uses only lower 32 bits if pointers are larger.
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00001696 uint32_t array_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001697 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(array)) >> 2;
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00001698 uint32_t name_hash =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001699 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name)) >> 2;
1700 return (array_hash ^ name_hash) % kLength;
1701 }
1702
1703 static const int kLength = 64;
1704 struct Key {
1705 DescriptorArray* array;
1706 String* name;
1707 };
1708
1709 static Key keys_[kLength];
1710 static int results_[kLength];
1711};
1712
1713
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714// ----------------------------------------------------------------------------
1715// Marking stack for tracing live objects.
1716
1717class MarkingStack {
1718 public:
1719 void Initialize(Address low, Address high) {
1720 top_ = low_ = reinterpret_cast<HeapObject**>(low);
1721 high_ = reinterpret_cast<HeapObject**>(high);
1722 overflowed_ = false;
1723 }
1724
1725 bool is_full() { return top_ >= high_; }
1726
1727 bool is_empty() { return top_ <= low_; }
1728
1729 bool overflowed() { return overflowed_; }
1730
1731 void clear_overflowed() { overflowed_ = false; }
1732
mads.s.ager31e71382008-08-13 09:32:07 +00001733 // Push the (marked) object on the marking stack if there is room,
1734 // otherwise mark the object as overflowed and wait for a rescan of the
1735 // heap.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001736 void Push(HeapObject* object) {
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001737 CHECK(object->IsHeapObject());
mads.s.ager31e71382008-08-13 09:32:07 +00001738 if (is_full()) {
1739 object->SetOverflow();
1740 overflowed_ = true;
1741 } else {
1742 *(top_++) = object;
1743 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 }
1745
1746 HeapObject* Pop() {
1747 ASSERT(!is_empty());
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001748 HeapObject* object = *(--top_);
1749 CHECK(object->IsHeapObject());
1750 return object;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 }
1752
1753 private:
1754 HeapObject** low_;
1755 HeapObject** top_;
1756 HeapObject** high_;
1757 bool overflowed_;
1758};
1759
1760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761// A helper class to document/test C++ scopes where we do not
1762// expect a GC. Usage:
1763//
1764// /* Allocation not allowed: we cannot handle a GC in this scope. */
1765// { AssertNoAllocation nogc;
1766// ...
1767// }
1768
1769#ifdef DEBUG
1770
1771class DisallowAllocationFailure {
1772 public:
1773 DisallowAllocationFailure() {
1774 old_state_ = Heap::disallow_allocation_failure_;
1775 Heap::disallow_allocation_failure_ = true;
1776 }
1777 ~DisallowAllocationFailure() {
1778 Heap::disallow_allocation_failure_ = old_state_;
1779 }
1780 private:
1781 bool old_state_;
1782};
1783
1784class AssertNoAllocation {
1785 public:
1786 AssertNoAllocation() {
1787 old_state_ = Heap::allow_allocation(false);
1788 }
1789
1790 ~AssertNoAllocation() {
1791 Heap::allow_allocation(old_state_);
1792 }
1793
1794 private:
1795 bool old_state_;
1796};
1797
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00001798class DisableAssertNoAllocation {
1799 public:
1800 DisableAssertNoAllocation() {
1801 old_state_ = Heap::allow_allocation(true);
1802 }
1803
1804 ~DisableAssertNoAllocation() {
1805 Heap::allow_allocation(old_state_);
1806 }
1807
1808 private:
1809 bool old_state_;
1810};
1811
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812#else // ndef DEBUG
1813
1814class AssertNoAllocation {
1815 public:
1816 AssertNoAllocation() { }
1817 ~AssertNoAllocation() { }
1818};
1819
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00001820class DisableAssertNoAllocation {
1821 public:
1822 DisableAssertNoAllocation() { }
1823 ~DisableAssertNoAllocation() { }
1824};
1825
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826#endif
1827
kasper.lund7276f142008-07-30 08:49:36 +00001828// GCTracer collects and prints ONE line after each garbage collector
1829// invocation IFF --trace_gc is used.
1830
1831class GCTracer BASE_EMBEDDED {
1832 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001833 class Scope BASE_EMBEDDED {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001834 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001835 enum ScopeId {
1836 EXTERNAL,
1837 MC_MARK,
1838 MC_SWEEP,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001839 MC_SWEEP_NEWSPACE,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001840 MC_COMPACT,
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00001841 MC_FLUSH_CODE,
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001842 kNumberOfScopes
1843 };
1844
1845 Scope(GCTracer* tracer, ScopeId scope)
1846 : tracer_(tracer),
1847 scope_(scope) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001848 start_time_ = OS::TimeCurrentMillis();
1849 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001850
1851 ~Scope() {
1852 ASSERT((0 <= scope_) && (scope_ < kNumberOfScopes));
1853 tracer_->scopes_[scope_] += OS::TimeCurrentMillis() - start_time_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001854 }
kasper.lund7276f142008-07-30 08:49:36 +00001855
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001856 private:
1857 GCTracer* tracer_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001858 ScopeId scope_;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001859 double start_time_;
1860 };
1861
1862 GCTracer();
kasper.lund7276f142008-07-30 08:49:36 +00001863 ~GCTracer();
1864
1865 // Sets the collector.
1866 void set_collector(GarbageCollector collector) { collector_ = collector; }
1867
1868 // Sets the GC count.
1869 void set_gc_count(int count) { gc_count_ = count; }
1870
1871 // Sets the full GC count.
1872 void set_full_gc_count(int count) { full_gc_count_ = count; }
1873
1874 // Sets the flag that this is a compacting full GC.
1875 void set_is_compacting() { is_compacting_ = true; }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001876 bool is_compacting() const { return is_compacting_; }
kasper.lund7276f142008-07-30 08:49:36 +00001877
1878 // Increment and decrement the count of marked objects.
1879 void increment_marked_count() { ++marked_count_; }
1880 void decrement_marked_count() { --marked_count_; }
1881
1882 int marked_count() { return marked_count_; }
1883
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001884 void increment_promoted_objects_size(int object_size) {
1885 promoted_objects_size_ += object_size;
1886 }
1887
1888 // Returns maximum GC pause.
1889 static int get_max_gc_pause() { return max_gc_pause_; }
1890
1891 // Returns maximum size of objects alive after GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001892 static intptr_t get_max_alive_after_gc() { return max_alive_after_gc_; }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001893
1894 // Returns minimal interval between two subsequent collections.
1895 static int get_min_in_mutator() { return min_in_mutator_; }
1896
kasper.lund7276f142008-07-30 08:49:36 +00001897 private:
1898 // Returns a string matching the collector.
1899 const char* CollectorString();
1900
1901 // Returns size of object in heap (in MB).
1902 double SizeOfHeapObjects() {
1903 return (static_cast<double>(Heap::SizeOfObjects())) / MB;
1904 }
1905
1906 double start_time_; // Timestamp set in the constructor.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001907 intptr_t start_size_; // Size of objects in heap set in constructor.
kasper.lund7276f142008-07-30 08:49:36 +00001908 GarbageCollector collector_; // Type of collector.
1909
1910 // A count (including this one, eg, the first collection is 1) of the
1911 // number of garbage collections.
1912 int gc_count_;
1913
1914 // A count (including this one) of the number of full garbage collections.
1915 int full_gc_count_;
1916
1917 // True if the current GC is a compacting full collection, false
1918 // otherwise.
1919 bool is_compacting_;
1920
1921 // True if the *previous* full GC cwas a compacting collection (will be
1922 // false if there has not been a previous full GC).
1923 bool previous_has_compacted_;
1924
1925 // On a full GC, a count of the number of marked objects. Incremented
1926 // when an object is marked and decremented when an object's mark bit is
1927 // cleared. Will be zero on a scavenge collection.
1928 int marked_count_;
1929
1930 // The count from the end of the previous full GC. Will be zero if there
1931 // was no previous full GC.
1932 int previous_marked_count_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001933
1934 // Amounts of time spent in different scopes during GC.
1935 double scopes_[Scope::kNumberOfScopes];
1936
1937 // Total amount of space either wasted or contained in one of free lists
1938 // before the current GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001939 intptr_t in_free_list_or_wasted_before_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001940
1941 // Difference between space used in the heap at the beginning of the current
1942 // collection and the end of the previous collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001943 intptr_t allocated_since_last_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001944
1945 // Amount of time spent in mutator that is time elapsed between end of the
1946 // previous collection and the beginning of the current one.
1947 double spent_in_mutator_;
1948
1949 // Size of objects promoted during the current collection.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001950 intptr_t promoted_objects_size_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001951
1952 // Maximum GC pause.
1953 static int max_gc_pause_;
1954
1955 // Maximum size of objects alive after GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001956 static intptr_t max_alive_after_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001957
1958 // Minimal interval between two subsequent collections.
1959 static int min_in_mutator_;
1960
1961 // Size of objects alive after last GC.
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +00001962 static intptr_t alive_after_last_gc_;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00001963
1964 static double last_gc_end_timestamp_;
kasper.lund7276f142008-07-30 08:49:36 +00001965};
1966
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001967
1968class TranscendentalCache {
1969 public:
1970 enum Type {ACOS, ASIN, ATAN, COS, EXP, LOG, SIN, TAN, kNumberOfCaches};
1971
1972 explicit TranscendentalCache(Type t);
1973
1974 // Returns a heap number with f(input), where f is a math function specified
1975 // by the 'type' argument.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001976 MUST_USE_RESULT static inline MaybeObject* Get(Type type, double input) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001977 TranscendentalCache* cache = caches_[type];
1978 if (cache == NULL) {
1979 caches_[type] = cache = new TranscendentalCache(type);
1980 }
1981 return cache->Get(input);
1982 }
1983
1984 // The cache contains raw Object pointers. This method disposes of
1985 // them before a garbage collection.
1986 static void Clear();
1987
1988 private:
lrn@chromium.org303ada72010-10-27 09:33:13 +00001989 MUST_USE_RESULT inline MaybeObject* Get(double input) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001990 Converter c;
1991 c.dbl = input;
1992 int hash = Hash(c);
1993 Element e = elements_[hash];
1994 if (e.in[0] == c.integers[0] &&
1995 e.in[1] == c.integers[1]) {
1996 ASSERT(e.output != NULL);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001997 Counters::transcendental_cache_hit.Increment();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001998 return e.output;
1999 }
2000 double answer = Calculate(input);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002001 Counters::transcendental_cache_miss.Increment();
lrn@chromium.org303ada72010-10-27 09:33:13 +00002002 Object* heap_number;
2003 { MaybeObject* maybe_heap_number = Heap::AllocateHeapNumber(answer);
2004 if (!maybe_heap_number->ToObject(&heap_number)) return maybe_heap_number;
2005 }
2006 elements_[hash].in[0] = c.integers[0];
2007 elements_[hash].in[1] = c.integers[1];
2008 elements_[hash].output = heap_number;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002009 return heap_number;
2010 }
2011
2012 inline double Calculate(double input) {
2013 switch (type_) {
2014 case ACOS:
2015 return acos(input);
2016 case ASIN:
2017 return asin(input);
2018 case ATAN:
2019 return atan(input);
2020 case COS:
2021 return cos(input);
2022 case EXP:
2023 return exp(input);
2024 case LOG:
2025 return log(input);
2026 case SIN:
2027 return sin(input);
2028 case TAN:
2029 return tan(input);
2030 default:
2031 return 0.0; // Never happens.
2032 }
2033 }
2034 static const int kCacheSize = 512;
2035 struct Element {
2036 uint32_t in[2];
2037 Object* output;
2038 };
2039 union Converter {
2040 double dbl;
2041 uint32_t integers[2];
2042 };
2043 inline static int Hash(const Converter& c) {
2044 uint32_t hash = (c.integers[0] ^ c.integers[1]);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002045 hash ^= static_cast<int32_t>(hash) >> 16;
2046 hash ^= static_cast<int32_t>(hash) >> 8;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002047 return (hash & (kCacheSize - 1));
2048 }
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002049
2050 static Address cache_array_address() {
2051 // Used to create an external reference.
2052 return reinterpret_cast<Address>(caches_);
2053 }
2054
2055 // Allow access to the caches_ array as an ExternalReference.
2056 friend class ExternalReference;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002057 // Inline implementation of the cache.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002058 friend class TranscendentalCacheStub;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002059 friend class TranscendentalCacheSSE2Stub;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002060
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002061 static TranscendentalCache* caches_[kNumberOfCaches];
2062 Element elements_[kCacheSize];
2063 Type type_;
2064};
2065
2066
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002067// External strings table is a place where all external strings are
2068// registered. We need to keep track of such strings to properly
2069// finalize them.
2070class ExternalStringTable : public AllStatic {
2071 public:
2072 // Registers an external string.
2073 inline static void AddString(String* string);
2074
2075 inline static void Iterate(ObjectVisitor* v);
2076
2077 // Restores internal invariant and gets rid of collected strings.
2078 // Must be called after each Iterate() that modified the strings.
2079 static void CleanUp();
2080
2081 // Destroys all allocated memory.
2082 static void TearDown();
2083
2084 private:
2085 friend class Heap;
2086
2087 inline static void Verify();
2088
2089 inline static void AddOldString(String* string);
2090
2091 // Notifies the table that only a prefix of the new list is valid.
2092 inline static void ShrinkNewStrings(int position);
2093
2094 // To speed up scavenge collections new space string are kept
2095 // separate from old space strings.
2096 static List<Object*> new_space_strings_;
2097 static List<Object*> old_space_strings_;
2098};
2099
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00002100
2101// Abstract base class for checking whether a weak object should be retained.
2102class WeakObjectRetainer {
2103 public:
2104 virtual ~WeakObjectRetainer() {}
2105
2106 // Return whether this object should be retained. If NULL is returned the
2107 // object has no references. Otherwise the address of the retained object
2108 // should be returned as in some GC situations the object has been moved.
2109 virtual Object* RetainAs(Object* object) = 0;
2110};
2111
2112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113} } // namespace v8::internal
2114
2115#endif // V8_HEAP_H_