blob: f2e2bc08b0729207e27cb0681ea5fafdde262e70 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "bootstrapper.h"
Ben Murdoch8b112d22011-06-08 16:22:53 +010033#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "compilation-cache.h"
35#include "debug.h"
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000036#include "deoptimizer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000037#include "global-handles.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000038#include "heap-profiler.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010039#include "incremental-marking.h"
Steve Block1e0659c2011-05-24 12:43:12 +010040#include "liveobjectlist-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000041#include "mark-compact.h"
42#include "natives.h"
Iain Merrick75681382010-08-19 15:07:18 +010043#include "objects-visiting.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010044#include "objects-visiting-inl.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010045#include "runtime-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000046#include "scopeinfo.h"
Steve Block3ce2e202009-11-05 08:53:23 +000047#include "snapshot.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010048#include "store-buffer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000049#include "v8threads.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010050#include "vm-state-inl.h"
Steve Block6ded16b2010-05-10 14:33:55 +010051#if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000052#include "regexp-macro-assembler.h"
Steve Blockd0582a62009-12-15 09:54:21 +000053#include "arm/regexp-macro-assembler-arm.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000054#endif
Steve Block44f0eee2011-05-26 01:26:41 +010055#if V8_TARGET_ARCH_MIPS && !V8_INTERPRETED_REGEXP
56#include "regexp-macro-assembler.h"
57#include "mips/regexp-macro-assembler-mips.h"
58#endif
Steve Block6ded16b2010-05-10 14:33:55 +010059
Steve Blocka7e24c12009-10-30 11:49:00 +000060namespace v8 {
61namespace internal {
62
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063static LazyMutex gc_initializer_mutex = LAZY_MUTEX_INITIALIZER;
Steve Blocka7e24c12009-10-30 11:49:00 +000064
Steve Blocka7e24c12009-10-30 11:49:00 +000065
Steve Block44f0eee2011-05-26 01:26:41 +010066Heap::Heap()
67 : isolate_(NULL),
Steve Blocka7e24c12009-10-30 11:49:00 +000068// semispace_size_ should be a power of 2 and old_generation_size_ should be
69// a multiple of Page::kPageSize.
Ben Murdocha28cdee2012-06-07 18:38:12 +010070
71// ANDROID
72//#if defined(ANDROID)
73//#define LUMP_OF_MEMORY (128 * KB)
74// code_range_size_(0),
75//#elif defined(V8_TARGET_ARCH_X64)
76// END ANDROID
77#if defined(V8_TARGET_ARCH_X64)
Ben Murdoch3ef787d2012-04-12 10:51:47 +010078#define LUMP_OF_MEMORY (2 * MB)
Steve Block44f0eee2011-05-26 01:26:41 +010079 code_range_size_(512*MB),
Steve Blocka7e24c12009-10-30 11:49:00 +000080#else
Ben Murdoch3ef787d2012-04-12 10:51:47 +010081#define LUMP_OF_MEMORY MB
Steve Block44f0eee2011-05-26 01:26:41 +010082 code_range_size_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +000083#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +010084 reserved_semispace_size_(8 * Max(LUMP_OF_MEMORY, Page::kPageSize)),
85 max_semispace_size_(8 * Max(LUMP_OF_MEMORY, Page::kPageSize)),
86 initial_semispace_size_(Page::kPageSize),
87 max_old_generation_size_(700ul * LUMP_OF_MEMORY),
88 max_executable_size_(256l * LUMP_OF_MEMORY),
89
Steve Blocka7e24c12009-10-30 11:49:00 +000090// Variables set based on semispace_size_ and old_generation_size_ in
Steve Block44f0eee2011-05-26 01:26:41 +010091// ConfigureHeap (survived_since_last_expansion_, external_allocation_limit_)
Steve Block3ce2e202009-11-05 08:53:23 +000092// Will be 4 * reserved_semispace_size_ to ensure that young
93// generation can be aligned to its size.
Steve Block44f0eee2011-05-26 01:26:41 +010094 survived_since_last_expansion_(0),
Ben Murdoch257744e2011-11-30 15:57:28 +000095 sweep_generation_(0),
Steve Block44f0eee2011-05-26 01:26:41 +010096 always_allocate_scope_depth_(0),
97 linear_allocation_scope_depth_(0),
98 contexts_disposed_(0),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010099 global_ic_age_(0),
100 scan_on_scavenge_pages_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100101 new_space_(this),
102 old_pointer_space_(NULL),
103 old_data_space_(NULL),
104 code_space_(NULL),
105 map_space_(NULL),
106 cell_space_(NULL),
107 lo_space_(NULL),
108 gc_state_(NOT_IN_GC),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000109 gc_post_processing_depth_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100110 ms_count_(0),
111 gc_count_(0),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100112 remembered_unmapped_pages_index_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100113 unflattened_strings_length_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +0000114#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100115 allocation_allowed_(true),
116 allocation_timeout_(0),
117 disallow_allocation_failure_(false),
118 debug_utils_(NULL),
Steve Blocka7e24c12009-10-30 11:49:00 +0000119#endif // DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100120 new_space_high_promotion_mode_active_(false),
Steve Block44f0eee2011-05-26 01:26:41 +0100121 old_gen_promotion_limit_(kMinimumPromotionLimit),
122 old_gen_allocation_limit_(kMinimumAllocationLimit),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100123 old_gen_limit_factor_(1),
124 size_of_old_gen_at_last_old_space_gc_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100125 external_allocation_limit_(0),
126 amount_of_external_allocated_memory_(0),
127 amount_of_external_allocated_memory_at_last_global_gc_(0),
128 old_gen_exhausted_(false),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100129 store_buffer_rebuilder_(store_buffer()),
Steve Block44f0eee2011-05-26 01:26:41 +0100130 hidden_symbol_(NULL),
131 global_gc_prologue_callback_(NULL),
132 global_gc_epilogue_callback_(NULL),
133 gc_safe_size_of_old_object_(NULL),
Steve Block053d10c2011-06-13 19:13:29 +0100134 total_regexp_code_generated_(0),
Steve Block44f0eee2011-05-26 01:26:41 +0100135 tracer_(NULL),
136 young_survivors_after_last_gc_(0),
137 high_survival_rate_period_length_(0),
138 survival_rate_(0),
139 previous_survival_rate_trend_(Heap::STABLE),
140 survival_rate_trend_(Heap::STABLE),
141 max_gc_pause_(0),
142 max_alive_after_gc_(0),
143 min_in_mutator_(kMaxInt),
144 alive_after_last_gc_(0),
145 last_gc_end_timestamp_(0.0),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100146 store_buffer_(this),
147 marking_(this),
148 incremental_marking_(this),
Steve Block44f0eee2011-05-26 01:26:41 +0100149 number_idle_notifications_(0),
150 last_idle_notification_gc_count_(0),
151 last_idle_notification_gc_count_init_(false),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100152 mark_sweeps_since_idle_round_started_(0),
153 ms_count_at_last_idle_notification_(0),
154 gc_count_at_last_idle_gc_(0),
155 scavenges_since_last_idle_round_(kIdleScavengeThreshold),
156 promotion_queue_(this),
Steve Block44f0eee2011-05-26 01:26:41 +0100157 configured_(false),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100158 chunks_queued_for_free_(NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100159 // Allow build-time customization of the max semispace size. Building
160 // V8 with snapshots and a non-default max semispace size is much
161 // easier if you can define it as part of the build environment.
162#if defined(V8_MAX_SEMISPACE_SIZE)
163 max_semispace_size_ = reserved_semispace_size_ = V8_MAX_SEMISPACE_SIZE;
164#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000166 intptr_t max_virtual = OS::MaxVirtualMemory();
167
168 if (max_virtual > 0) {
169 if (code_range_size_ > 0) {
170 // Reserve no more than 1/8 of the memory for the code range.
171 code_range_size_ = Min(code_range_size_, max_virtual >> 3);
172 }
173 }
174
Steve Block44f0eee2011-05-26 01:26:41 +0100175 memset(roots_, 0, sizeof(roots_[0]) * kRootListLength);
176 global_contexts_list_ = NULL;
177 mark_compact_collector_.heap_ = this;
178 external_string_table_.heap_ = this;
179}
180
Steve Blocka7e24c12009-10-30 11:49:00 +0000181
Ben Murdochf87a2032010-10-22 12:50:53 +0100182intptr_t Heap::Capacity() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100183 if (!HasBeenSetUp()) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185 return new_space_.Capacity() +
186 old_pointer_space_->Capacity() +
187 old_data_space_->Capacity() +
188 code_space_->Capacity() +
189 map_space_->Capacity() +
190 cell_space_->Capacity();
191}
192
193
Ben Murdochf87a2032010-10-22 12:50:53 +0100194intptr_t Heap::CommittedMemory() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100195 if (!HasBeenSetUp()) return 0;
Steve Block3ce2e202009-11-05 08:53:23 +0000196
197 return new_space_.CommittedMemory() +
198 old_pointer_space_->CommittedMemory() +
199 old_data_space_->CommittedMemory() +
200 code_space_->CommittedMemory() +
201 map_space_->CommittedMemory() +
202 cell_space_->CommittedMemory() +
203 lo_space_->Size();
204}
205
Russell Brenner90bac252010-11-18 13:33:46 -0800206intptr_t Heap::CommittedMemoryExecutable() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100207 if (!HasBeenSetUp()) return 0;
Russell Brenner90bac252010-11-18 13:33:46 -0800208
Steve Block44f0eee2011-05-26 01:26:41 +0100209 return isolate()->memory_allocator()->SizeExecutable();
Russell Brenner90bac252010-11-18 13:33:46 -0800210}
211
Steve Block3ce2e202009-11-05 08:53:23 +0000212
Ben Murdochf87a2032010-10-22 12:50:53 +0100213intptr_t Heap::Available() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100214 if (!HasBeenSetUp()) return 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000215
216 return new_space_.Available() +
217 old_pointer_space_->Available() +
218 old_data_space_->Available() +
219 code_space_->Available() +
220 map_space_->Available() +
221 cell_space_->Available();
222}
223
224
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100225bool Heap::HasBeenSetUp() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 return old_pointer_space_ != NULL &&
227 old_data_space_ != NULL &&
228 code_space_ != NULL &&
229 map_space_ != NULL &&
230 cell_space_ != NULL &&
231 lo_space_ != NULL;
232}
233
234
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100235int Heap::GcSafeSizeOfOldObject(HeapObject* object) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100236 if (IntrusiveMarking::IsMarked(object)) {
237 return IntrusiveMarking::SizeOfMarkedObject(object);
Ben Murdoch85b71792012-04-11 18:30:58 +0100238 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239 return object->SizeFromMap(object->map());
Ben Murdoch85b71792012-04-11 18:30:58 +0100240}
241
242
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100243GarbageCollector Heap::SelectGarbageCollector(AllocationSpace space,
244 const char** reason) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000245 // Is global GC requested?
246 if (space != NEW_SPACE || FLAG_gc_global) {
Steve Block44f0eee2011-05-26 01:26:41 +0100247 isolate_->counters()->gc_compactor_caused_by_request()->Increment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100248 *reason = "GC in old space requested";
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 return MARK_COMPACTOR;
250 }
251
252 // Is enough data promoted to justify a global GC?
253 if (OldGenerationPromotionLimitReached()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100254 isolate_->counters()->gc_compactor_caused_by_promoted_data()->Increment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100255 *reason = "promotion limit reached";
Steve Blocka7e24c12009-10-30 11:49:00 +0000256 return MARK_COMPACTOR;
257 }
258
259 // Have allocation in OLD and LO failed?
260 if (old_gen_exhausted_) {
Steve Block44f0eee2011-05-26 01:26:41 +0100261 isolate_->counters()->
262 gc_compactor_caused_by_oldspace_exhaustion()->Increment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100263 *reason = "old generations exhausted";
Steve Blocka7e24c12009-10-30 11:49:00 +0000264 return MARK_COMPACTOR;
265 }
266
267 // Is there enough space left in OLD to guarantee that a scavenge can
268 // succeed?
269 //
270 // Note that MemoryAllocator->MaxAvailable() undercounts the memory available
271 // for object promotion. It counts only the bytes that the memory
272 // allocator has not yet allocated from the OS and assigned to any space,
273 // and does not count available bytes already in the old space or code
274 // space. Undercounting is safe---we may get an unrequested full GC when
275 // a scavenge would have succeeded.
Steve Block44f0eee2011-05-26 01:26:41 +0100276 if (isolate_->memory_allocator()->MaxAvailable() <= new_space_.Size()) {
277 isolate_->counters()->
278 gc_compactor_caused_by_oldspace_exhaustion()->Increment();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100279 *reason = "scavenge might not succeed";
Steve Blocka7e24c12009-10-30 11:49:00 +0000280 return MARK_COMPACTOR;
281 }
282
283 // Default
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100284 *reason = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 return SCAVENGER;
286}
287
288
289// TODO(1238405): Combine the infrastructure for --heap-stats and
290// --log-gc to avoid the complicated preprocessor and flag testing.
Steve Blocka7e24c12009-10-30 11:49:00 +0000291void Heap::ReportStatisticsBeforeGC() {
292 // Heap::ReportHeapStatistics will also log NewSpace statistics when
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000293 // compiled --log-gc is set. The following logic is used to avoid
294 // double logging.
295#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 if (FLAG_heap_stats || FLAG_log_gc) new_space_.CollectStatistics();
297 if (FLAG_heap_stats) {
298 ReportHeapStatistics("Before GC");
299 } else if (FLAG_log_gc) {
300 new_space_.ReportStatistics();
301 }
302 if (FLAG_heap_stats || FLAG_log_gc) new_space_.ClearHistograms();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000303#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 if (FLAG_log_gc) {
305 new_space_.CollectStatistics();
306 new_space_.ReportStatistics();
307 new_space_.ClearHistograms();
308 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000309#endif // DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000310}
311
312
Steve Blocka7e24c12009-10-30 11:49:00 +0000313void Heap::PrintShortHeapStatistics() {
314 if (!FLAG_trace_gc_verbose) return;
Ben Murdochf87a2032010-10-22 12:50:53 +0100315 PrintF("Memory allocator, used: %8" V8_PTR_PREFIX "d"
316 ", available: %8" V8_PTR_PREFIX "d\n",
Steve Block44f0eee2011-05-26 01:26:41 +0100317 isolate_->memory_allocator()->Size(),
318 isolate_->memory_allocator()->Available());
Ben Murdochf87a2032010-10-22 12:50:53 +0100319 PrintF("New space, used: %8" V8_PTR_PREFIX "d"
320 ", available: %8" V8_PTR_PREFIX "d\n",
Steve Block3ce2e202009-11-05 08:53:23 +0000321 Heap::new_space_.Size(),
322 new_space_.Available());
Ben Murdochf87a2032010-10-22 12:50:53 +0100323 PrintF("Old pointers, used: %8" V8_PTR_PREFIX "d"
324 ", available: %8" V8_PTR_PREFIX "d"
325 ", waste: %8" V8_PTR_PREFIX "d\n",
Steve Block3ce2e202009-11-05 08:53:23 +0000326 old_pointer_space_->Size(),
327 old_pointer_space_->Available(),
328 old_pointer_space_->Waste());
Ben Murdochf87a2032010-10-22 12:50:53 +0100329 PrintF("Old data space, used: %8" V8_PTR_PREFIX "d"
330 ", available: %8" V8_PTR_PREFIX "d"
331 ", waste: %8" V8_PTR_PREFIX "d\n",
Steve Block3ce2e202009-11-05 08:53:23 +0000332 old_data_space_->Size(),
333 old_data_space_->Available(),
334 old_data_space_->Waste());
Ben Murdochf87a2032010-10-22 12:50:53 +0100335 PrintF("Code space, used: %8" V8_PTR_PREFIX "d"
336 ", available: %8" V8_PTR_PREFIX "d"
337 ", waste: %8" V8_PTR_PREFIX "d\n",
Steve Block3ce2e202009-11-05 08:53:23 +0000338 code_space_->Size(),
339 code_space_->Available(),
340 code_space_->Waste());
Ben Murdochf87a2032010-10-22 12:50:53 +0100341 PrintF("Map space, used: %8" V8_PTR_PREFIX "d"
342 ", available: %8" V8_PTR_PREFIX "d"
343 ", waste: %8" V8_PTR_PREFIX "d\n",
Steve Block3ce2e202009-11-05 08:53:23 +0000344 map_space_->Size(),
345 map_space_->Available(),
346 map_space_->Waste());
Ben Murdochf87a2032010-10-22 12:50:53 +0100347 PrintF("Cell space, used: %8" V8_PTR_PREFIX "d"
348 ", available: %8" V8_PTR_PREFIX "d"
349 ", waste: %8" V8_PTR_PREFIX "d\n",
Steve Block3ce2e202009-11-05 08:53:23 +0000350 cell_space_->Size(),
351 cell_space_->Available(),
352 cell_space_->Waste());
Ben Murdochf87a2032010-10-22 12:50:53 +0100353 PrintF("Large object space, used: %8" V8_PTR_PREFIX "d"
354 ", available: %8" V8_PTR_PREFIX "d\n",
Steve Block3ce2e202009-11-05 08:53:23 +0000355 lo_space_->Size(),
356 lo_space_->Available());
Steve Blocka7e24c12009-10-30 11:49:00 +0000357}
Steve Blocka7e24c12009-10-30 11:49:00 +0000358
359
360// TODO(1238405): Combine the infrastructure for --heap-stats and
361// --log-gc to avoid the complicated preprocessor and flag testing.
362void Heap::ReportStatisticsAfterGC() {
363 // Similar to the before GC, we use some complicated logic to ensure that
364 // NewSpace statistics are logged exactly once when --log-gc is turned on.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000365#if defined(DEBUG)
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 if (FLAG_heap_stats) {
367 new_space_.CollectStatistics();
368 ReportHeapStatistics("After GC");
369 } else if (FLAG_log_gc) {
370 new_space_.ReportStatistics();
371 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000372#else
Steve Blocka7e24c12009-10-30 11:49:00 +0000373 if (FLAG_log_gc) new_space_.ReportStatistics();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000374#endif // DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000375}
Steve Blocka7e24c12009-10-30 11:49:00 +0000376
377
378void Heap::GarbageCollectionPrologue() {
Steve Block44f0eee2011-05-26 01:26:41 +0100379 isolate_->transcendental_cache()->Clear();
Steve Block6ded16b2010-05-10 14:33:55 +0100380 ClearJSFunctionResultCaches();
Steve Blocka7e24c12009-10-30 11:49:00 +0000381 gc_count_++;
Steve Block6ded16b2010-05-10 14:33:55 +0100382 unflattened_strings_length_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000383#ifdef DEBUG
384 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
385 allow_allocation(false);
386
387 if (FLAG_verify_heap) {
388 Verify();
389 }
390
391 if (FLAG_gc_verbose) Print();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000392#endif // DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000393
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000394#if defined(DEBUG)
Steve Blocka7e24c12009-10-30 11:49:00 +0000395 ReportStatisticsBeforeGC();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000396#endif // DEBUG
Steve Block1e0659c2011-05-24 12:43:12 +0100397
398 LiveObjectList::GCPrologue();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399 store_buffer()->GCPrologue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000400}
401
Ben Murdochf87a2032010-10-22 12:50:53 +0100402intptr_t Heap::SizeOfObjects() {
403 intptr_t total = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 AllSpaces spaces;
Leon Clarked91b9f72010-01-27 17:25:45 +0000405 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800406 total += space->SizeOfObjects();
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 }
408 return total;
409}
410
411void Heap::GarbageCollectionEpilogue() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100412 store_buffer()->GCEpilogue();
Steve Block1e0659c2011-05-24 12:43:12 +0100413 LiveObjectList::GCEpilogue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000414#ifdef DEBUG
415 allow_allocation(true);
416 ZapFromSpace();
417
418 if (FLAG_verify_heap) {
419 Verify();
420 }
421
Steve Block44f0eee2011-05-26 01:26:41 +0100422 if (FLAG_print_global_handles) isolate_->global_handles()->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 if (FLAG_print_handles) PrintHandles();
424 if (FLAG_gc_verbose) Print();
425 if (FLAG_code_stats) ReportCodeStatistics("After GC");
426#endif
427
Steve Block44f0eee2011-05-26 01:26:41 +0100428 isolate_->counters()->alive_after_last_gc()->Set(
429 static_cast<int>(SizeOfObjects()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000430
Steve Block44f0eee2011-05-26 01:26:41 +0100431 isolate_->counters()->symbol_table_capacity()->Set(
432 symbol_table()->Capacity());
433 isolate_->counters()->number_of_symbols()->Set(
434 symbol_table()->NumberOfElements());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000435#if defined(DEBUG)
Steve Blocka7e24c12009-10-30 11:49:00 +0000436 ReportStatisticsAfterGC();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000437#endif // DEBUG
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000438#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100439 isolate_->debug()->AfterGarbageCollection();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000440#endif // ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +0000441}
442
443
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100444void Heap::CollectAllGarbage(int flags, const char* gc_reason) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000445 // Since we are ignoring the return value, the exact choice of space does
446 // not matter, so long as we do not specify NEW_SPACE, which would not
447 // cause a full GC.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100448 mark_compact_collector_.SetFlags(flags);
449 CollectGarbage(OLD_POINTER_SPACE, gc_reason);
450 mark_compact_collector_.SetFlags(kNoGCFlags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000451}
452
453
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100454void Heap::CollectAllAvailableGarbage(const char* gc_reason) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800455 // Since we are ignoring the return value, the exact choice of space does
456 // not matter, so long as we do not specify NEW_SPACE, which would not
457 // cause a full GC.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800458 // Major GC would invoke weak handle callbacks on weakly reachable
459 // handles, but won't collect weakly reachable objects until next
460 // major GC. Therefore if we collect aggressively and weak handle callback
461 // has been invoked, we rerun major GC to release objects which become
462 // garbage.
463 // Note: as weak callbacks can execute arbitrary code, we cannot
464 // hope that eventually there will be no weak callbacks invocations.
465 // Therefore stop recollecting after several attempts.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100466 mark_compact_collector()->SetFlags(kMakeHeapIterableMask |
467 kReduceMemoryFootprintMask);
468 isolate_->compilation_cache()->Clear();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800469 const int kMaxNumberOfAttempts = 7;
470 for (int attempt = 0; attempt < kMaxNumberOfAttempts; attempt++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100471 if (!CollectGarbage(OLD_POINTER_SPACE, MARK_COMPACTOR, gc_reason, NULL)) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800472 break;
473 }
474 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100475 mark_compact_collector()->SetFlags(kNoGCFlags);
476 new_space_.Shrink();
477 UncommitFromSpace();
478 Shrink();
479 incremental_marking()->UncommitMarkingDeque();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800480}
481
482
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100483bool Heap::CollectGarbage(AllocationSpace space,
484 GarbageCollector collector,
485 const char* gc_reason,
486 const char* collector_reason) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 // The VM is in the GC state until exiting this function.
Steve Block44f0eee2011-05-26 01:26:41 +0100488 VMState state(isolate_, GC);
Steve Blocka7e24c12009-10-30 11:49:00 +0000489
490#ifdef DEBUG
491 // Reset the allocation timeout to the GC interval, but make sure to
492 // allow at least a few allocations after a collection. The reason
493 // for this is that we have a lot of allocation sequences and we
494 // assume that a garbage collection will allow the subsequent
495 // allocation attempts to go through.
496 allocation_timeout_ = Max(6, FLAG_gc_interval);
497#endif
498
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100499 if (collector == SCAVENGER && !incremental_marking()->IsStopped()) {
500 if (FLAG_trace_incremental_marking) {
501 PrintF("[IncrementalMarking] Scavenge during marking.\n");
502 }
503 }
504
505 if (collector == MARK_COMPACTOR &&
506 !mark_compact_collector()->abort_incremental_marking_ &&
507 !incremental_marking()->IsStopped() &&
508 !incremental_marking()->should_hurry() &&
509 FLAG_incremental_marking_steps) {
Ben Murdoch8f9999f2012-04-23 10:39:17 +0100510 // Make progress in incremental marking.
511 const intptr_t kStepSizeWhenDelayedByScavenge = 1 * MB;
512 incremental_marking()->Step(kStepSizeWhenDelayedByScavenge,
513 IncrementalMarking::NO_GC_VIA_STACK_GUARD);
514 if (!incremental_marking()->IsComplete()) {
515 if (FLAG_trace_incremental_marking) {
516 PrintF("[IncrementalMarking] Delaying MarkSweep.\n");
517 }
518 collector = SCAVENGER;
519 collector_reason = "incremental marking delaying mark-sweep";
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100520 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100521 }
522
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800523 bool next_gc_likely_to_collect_more = false;
524
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100525 { GCTracer tracer(this, gc_reason, collector_reason);
Steve Blocka7e24c12009-10-30 11:49:00 +0000526 GarbageCollectionPrologue();
527 // The GC count was incremented in the prologue. Tell the tracer about
528 // it.
529 tracer.set_gc_count(gc_count_);
530
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 // Tell the tracer which collector we've selected.
532 tracer.set_collector(collector);
533
534 HistogramTimer* rate = (collector == SCAVENGER)
Steve Block44f0eee2011-05-26 01:26:41 +0100535 ? isolate_->counters()->gc_scavenger()
536 : isolate_->counters()->gc_compactor();
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 rate->Start();
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800538 next_gc_likely_to_collect_more =
539 PerformGarbageCollection(collector, &tracer);
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 rate->Stop();
541
542 GarbageCollectionEpilogue();
543 }
544
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100545 ASSERT(collector == SCAVENGER || incremental_marking()->IsStopped());
546 if (incremental_marking()->IsStopped()) {
547 if (incremental_marking()->WorthActivating() && NextGCIsLikelyToBeFull()) {
548 incremental_marking()->Start();
549 }
550 }
551
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800552 return next_gc_likely_to_collect_more;
Steve Blocka7e24c12009-10-30 11:49:00 +0000553}
554
555
556void Heap::PerformScavenge() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100557 GCTracer tracer(this, NULL, NULL);
558 if (incremental_marking()->IsStopped()) {
559 PerformGarbageCollection(SCAVENGER, &tracer);
560 } else {
561 PerformGarbageCollection(MARK_COMPACTOR, &tracer);
562 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000563}
564
565
566#ifdef DEBUG
567// Helper class for verifying the symbol table.
568class SymbolTableVerifier : public ObjectVisitor {
569 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 void VisitPointers(Object** start, Object** end) {
571 // Visit all HeapObject pointers in [start, end).
572 for (Object** p = start; p < end; p++) {
573 if ((*p)->IsHeapObject()) {
574 // Check that the symbol is actually a symbol.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100575 ASSERT((*p)->IsTheHole() || (*p)->IsUndefined() || (*p)->IsSymbol());
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 }
577 }
578 }
579};
580#endif // DEBUG
581
582
583static void VerifySymbolTable() {
584#ifdef DEBUG
585 SymbolTableVerifier verifier;
Steve Block44f0eee2011-05-26 01:26:41 +0100586 HEAP->symbol_table()->IterateElements(&verifier);
Steve Blocka7e24c12009-10-30 11:49:00 +0000587#endif // DEBUG
588}
589
590
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100591static bool AbortIncrementalMarkingAndCollectGarbage(
592 Heap* heap,
593 AllocationSpace space,
594 const char* gc_reason = NULL) {
595 heap->mark_compact_collector()->SetFlags(Heap::kAbortIncrementalMarkingMask);
596 bool result = heap->CollectGarbage(space, gc_reason);
597 heap->mark_compact_collector()->SetFlags(Heap::kNoGCFlags);
598 return result;
599}
600
601
Leon Clarkee46be812010-01-19 14:06:41 +0000602void Heap::ReserveSpace(
603 int new_space_size,
604 int pointer_space_size,
605 int data_space_size,
606 int code_space_size,
607 int map_space_size,
608 int cell_space_size,
609 int large_object_size) {
610 NewSpace* new_space = Heap::new_space();
611 PagedSpace* old_pointer_space = Heap::old_pointer_space();
612 PagedSpace* old_data_space = Heap::old_data_space();
613 PagedSpace* code_space = Heap::code_space();
614 PagedSpace* map_space = Heap::map_space();
615 PagedSpace* cell_space = Heap::cell_space();
616 LargeObjectSpace* lo_space = Heap::lo_space();
617 bool gc_performed = true;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100618 int counter = 0;
619 static const int kThreshold = 20;
620 while (gc_performed && counter++ < kThreshold) {
Leon Clarkee46be812010-01-19 14:06:41 +0000621 gc_performed = false;
622 if (!new_space->ReserveSpace(new_space_size)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100623 Heap::CollectGarbage(NEW_SPACE,
624 "failed to reserve space in the new space");
Leon Clarkee46be812010-01-19 14:06:41 +0000625 gc_performed = true;
626 }
627 if (!old_pointer_space->ReserveSpace(pointer_space_size)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100628 AbortIncrementalMarkingAndCollectGarbage(this, OLD_POINTER_SPACE,
629 "failed to reserve space in the old pointer space");
Leon Clarkee46be812010-01-19 14:06:41 +0000630 gc_performed = true;
631 }
632 if (!(old_data_space->ReserveSpace(data_space_size))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100633 AbortIncrementalMarkingAndCollectGarbage(this, OLD_DATA_SPACE,
634 "failed to reserve space in the old data space");
Leon Clarkee46be812010-01-19 14:06:41 +0000635 gc_performed = true;
636 }
637 if (!(code_space->ReserveSpace(code_space_size))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100638 AbortIncrementalMarkingAndCollectGarbage(this, CODE_SPACE,
639 "failed to reserve space in the code space");
Leon Clarkee46be812010-01-19 14:06:41 +0000640 gc_performed = true;
641 }
642 if (!(map_space->ReserveSpace(map_space_size))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100643 AbortIncrementalMarkingAndCollectGarbage(this, MAP_SPACE,
644 "failed to reserve space in the map space");
Leon Clarkee46be812010-01-19 14:06:41 +0000645 gc_performed = true;
646 }
647 if (!(cell_space->ReserveSpace(cell_space_size))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100648 AbortIncrementalMarkingAndCollectGarbage(this, CELL_SPACE,
649 "failed to reserve space in the cell space");
Leon Clarkee46be812010-01-19 14:06:41 +0000650 gc_performed = true;
651 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100652 // We add a slack-factor of 2 in order to have space for a series of
653 // large-object allocations that are only just larger than the page size.
Leon Clarkee46be812010-01-19 14:06:41 +0000654 large_object_size *= 2;
655 // The ReserveSpace method on the large object space checks how much
656 // we can expand the old generation. This includes expansion caused by
657 // allocation in the other spaces.
658 large_object_size += cell_space_size + map_space_size + code_space_size +
659 data_space_size + pointer_space_size;
660 if (!(lo_space->ReserveSpace(large_object_size))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100661 AbortIncrementalMarkingAndCollectGarbage(this, LO_SPACE,
662 "failed to reserve space in the large object space");
Leon Clarkee46be812010-01-19 14:06:41 +0000663 gc_performed = true;
664 }
665 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100666
667 if (gc_performed) {
668 // Failed to reserve the space after several attempts.
669 V8::FatalProcessOutOfMemory("Heap::ReserveSpace");
670 }
Leon Clarkee46be812010-01-19 14:06:41 +0000671}
672
673
Steve Blocka7e24c12009-10-30 11:49:00 +0000674void Heap::EnsureFromSpaceIsCommitted() {
675 if (new_space_.CommitFromSpaceIfNeeded()) return;
676
677 // Committing memory to from space failed.
678 // Try shrinking and try again.
679 Shrink();
680 if (new_space_.CommitFromSpaceIfNeeded()) return;
681
682 // Committing memory to from space failed again.
683 // Memory is exhausted and we will die.
684 V8::FatalProcessOutOfMemory("Committing semi space failed.");
685}
686
687
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800688void Heap::ClearJSFunctionResultCaches() {
Steve Block44f0eee2011-05-26 01:26:41 +0100689 if (isolate_->bootstrapper()->IsActive()) return;
Steve Block6ded16b2010-05-10 14:33:55 +0100690
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800691 Object* context = global_contexts_list_;
692 while (!context->IsUndefined()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100693 // Get the caches for this context. GC can happen when the context
694 // is not fully initialized, so the caches can be undefined.
695 Object* caches_or_undefined =
696 Context::cast(context)->get(Context::JSFUNCTION_RESULT_CACHES_INDEX);
697 if (!caches_or_undefined->IsUndefined()) {
698 FixedArray* caches = FixedArray::cast(caches_or_undefined);
699 // Clear the caches:
700 int length = caches->length();
701 for (int i = 0; i < length; i++) {
702 JSFunctionResultCache::cast(caches->get(i))->Clear();
703 }
Steve Block6ded16b2010-05-10 14:33:55 +0100704 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800705 // Get the next context:
706 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
Steve Block6ded16b2010-05-10 14:33:55 +0100707 }
Steve Block6ded16b2010-05-10 14:33:55 +0100708}
709
710
Steve Block44f0eee2011-05-26 01:26:41 +0100711
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100712void Heap::ClearNormalizedMapCaches() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100713 if (isolate_->bootstrapper()->IsActive() &&
714 !incremental_marking()->IsMarking()) {
715 return;
716 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100717
718 Object* context = global_contexts_list_;
719 while (!context->IsUndefined()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100720 // GC can happen when the context is not fully initialized,
721 // so the cache can be undefined.
722 Object* cache =
723 Context::cast(context)->get(Context::NORMALIZED_MAP_CACHE_INDEX);
724 if (!cache->IsUndefined()) {
725 NormalizedMapCache::cast(cache)->Clear();
726 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100727 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
728 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100729}
730
731
Steve Block8defd9f2010-07-08 12:39:36 +0100732void Heap::UpdateSurvivalRateTrend(int start_new_space_size) {
733 double survival_rate =
734 (static_cast<double>(young_survivors_after_last_gc_) * 100) /
735 start_new_space_size;
736
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100737 if (survival_rate > kYoungSurvivalRateHighThreshold) {
Steve Block8defd9f2010-07-08 12:39:36 +0100738 high_survival_rate_period_length_++;
739 } else {
740 high_survival_rate_period_length_ = 0;
741 }
742
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100743 if (survival_rate < kYoungSurvivalRateLowThreshold) {
744 low_survival_rate_period_length_++;
745 } else {
746 low_survival_rate_period_length_ = 0;
747 }
748
Steve Block8defd9f2010-07-08 12:39:36 +0100749 double survival_rate_diff = survival_rate_ - survival_rate;
750
751 if (survival_rate_diff > kYoungSurvivalRateAllowedDeviation) {
752 set_survival_rate_trend(DECREASING);
753 } else if (survival_rate_diff < -kYoungSurvivalRateAllowedDeviation) {
754 set_survival_rate_trend(INCREASING);
755 } else {
756 set_survival_rate_trend(STABLE);
757 }
758
759 survival_rate_ = survival_rate;
760}
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100761
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800762bool Heap::PerformGarbageCollection(GarbageCollector collector,
John Reck59135872010-11-02 12:39:01 -0700763 GCTracer* tracer) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800764 bool next_gc_likely_to_collect_more = false;
765
Ben Murdochf87a2032010-10-22 12:50:53 +0100766 if (collector != SCAVENGER) {
Steve Block44f0eee2011-05-26 01:26:41 +0100767 PROFILE(isolate_, CodeMovingGCEvent());
Ben Murdochf87a2032010-10-22 12:50:53 +0100768 }
769
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100770 if (FLAG_verify_heap) {
771 VerifySymbolTable();
772 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 if (collector == MARK_COMPACTOR && global_gc_prologue_callback_) {
774 ASSERT(!allocation_allowed_);
Leon Clarkef7060e22010-06-03 12:02:55 +0100775 GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000776 global_gc_prologue_callback_();
777 }
Steve Block6ded16b2010-05-10 14:33:55 +0100778
779 GCType gc_type =
780 collector == MARK_COMPACTOR ? kGCTypeMarkSweepCompact : kGCTypeScavenge;
781
782 for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) {
783 if (gc_type & gc_prologue_callbacks_[i].gc_type) {
784 gc_prologue_callbacks_[i].callback(gc_type, kNoGCCallbackFlags);
785 }
786 }
787
Steve Blocka7e24c12009-10-30 11:49:00 +0000788 EnsureFromSpaceIsCommitted();
Steve Block6ded16b2010-05-10 14:33:55 +0100789
Ben Murdochf87a2032010-10-22 12:50:53 +0100790 int start_new_space_size = Heap::new_space()->SizeAsInt();
Steve Block8defd9f2010-07-08 12:39:36 +0100791
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100792 if (IsHighSurvivalRate()) {
793 // We speed up the incremental marker if it is running so that it
794 // does not fall behind the rate of promotion, which would cause a
795 // constantly growing old space.
796 incremental_marking()->NotifyOfHighPromotionRate();
797 }
798
Steve Blocka7e24c12009-10-30 11:49:00 +0000799 if (collector == MARK_COMPACTOR) {
Steve Block6ded16b2010-05-10 14:33:55 +0100800 // Perform mark-sweep with optional compaction.
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 MarkCompact(tracer);
Ben Murdoch257744e2011-11-30 15:57:28 +0000802 sweep_generation_++;
Steve Block8defd9f2010-07-08 12:39:36 +0100803 bool high_survival_rate_during_scavenges = IsHighSurvivalRate() &&
804 IsStableOrIncreasingSurvivalTrend();
805
806 UpdateSurvivalRateTrend(start_new_space_size);
807
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100808 size_of_old_gen_at_last_old_space_gc_ = PromotedSpaceSize();
Steve Block8defd9f2010-07-08 12:39:36 +0100809
John Reck59135872010-11-02 12:39:01 -0700810 if (high_survival_rate_during_scavenges &&
811 IsStableOrIncreasingSurvivalTrend()) {
812 // Stable high survival rates of young objects both during partial and
813 // full collection indicate that mutator is either building or modifying
814 // a structure with a long lifetime.
815 // In this case we aggressively raise old generation memory limits to
816 // postpone subsequent mark-sweep collection and thus trade memory
817 // space for the mutation speed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100818 old_gen_limit_factor_ = 2;
819 } else {
820 old_gen_limit_factor_ = 1;
Steve Block8defd9f2010-07-08 12:39:36 +0100821 }
822
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100823 old_gen_promotion_limit_ =
824 OldGenPromotionLimit(size_of_old_gen_at_last_old_space_gc_);
825 old_gen_allocation_limit_ =
826 OldGenAllocationLimit(size_of_old_gen_at_last_old_space_gc_);
827
John Reck59135872010-11-02 12:39:01 -0700828 old_gen_exhausted_ = false;
Steve Block6ded16b2010-05-10 14:33:55 +0100829 } else {
Leon Clarkef7060e22010-06-03 12:02:55 +0100830 tracer_ = tracer;
Steve Block6ded16b2010-05-10 14:33:55 +0100831 Scavenge();
Leon Clarkef7060e22010-06-03 12:02:55 +0100832 tracer_ = NULL;
Steve Block8defd9f2010-07-08 12:39:36 +0100833
834 UpdateSurvivalRateTrend(start_new_space_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000836
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100837 if (!new_space_high_promotion_mode_active_ &&
838 new_space_.Capacity() == new_space_.MaximumCapacity() &&
839 IsStableOrIncreasingSurvivalTrend() &&
840 IsHighSurvivalRate()) {
841 // Stable high survival rates even though young generation is at
842 // maximum capacity indicates that most objects will be promoted.
843 // To decrease scavenger pauses and final mark-sweep pauses, we
844 // have to limit maximal capacity of the young generation.
845 new_space_high_promotion_mode_active_ = true;
846 if (FLAG_trace_gc) {
847 PrintF("Limited new space size due to high promotion rate: %d MB\n",
848 new_space_.InitialCapacity() / MB);
849 }
850 } else if (new_space_high_promotion_mode_active_ &&
851 IsStableOrDecreasingSurvivalTrend() &&
852 IsLowSurvivalRate()) {
853 // Decreasing low survival rates might indicate that the above high
854 // promotion mode is over and we should allow the young generation
855 // to grow again.
856 new_space_high_promotion_mode_active_ = false;
857 if (FLAG_trace_gc) {
858 PrintF("Unlimited new space size due to low promotion rate: %d MB\n",
859 new_space_.MaximumCapacity() / MB);
860 }
861 }
862
863 if (new_space_high_promotion_mode_active_ &&
864 new_space_.Capacity() > new_space_.InitialCapacity()) {
865 new_space_.Shrink();
866 }
867
Steve Block44f0eee2011-05-26 01:26:41 +0100868 isolate_->counters()->objs_since_last_young()->Set(0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000869
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000870 gc_post_processing_depth_++;
Ben Murdoch257744e2011-11-30 15:57:28 +0000871 { DisableAssertNoAllocation allow_allocation;
John Reck59135872010-11-02 12:39:01 -0700872 GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800873 next_gc_likely_to_collect_more =
Ben Murdoch257744e2011-11-30 15:57:28 +0000874 isolate_->global_handles()->PostGarbageCollectionProcessing(collector);
John Reck59135872010-11-02 12:39:01 -0700875 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000876 gc_post_processing_depth_--;
John Reck59135872010-11-02 12:39:01 -0700877
Steve Block3ce2e202009-11-05 08:53:23 +0000878 // Update relocatables.
879 Relocatable::PostGarbageCollectionProcessing();
Steve Blocka7e24c12009-10-30 11:49:00 +0000880
881 if (collector == MARK_COMPACTOR) {
882 // Register the amount of external allocated memory.
883 amount_of_external_allocated_memory_at_last_global_gc_ =
884 amount_of_external_allocated_memory_;
885 }
886
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100887 GCCallbackFlags callback_flags = kNoGCCallbackFlags;
Steve Block6ded16b2010-05-10 14:33:55 +0100888 for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) {
889 if (gc_type & gc_epilogue_callbacks_[i].gc_type) {
890 gc_epilogue_callbacks_[i].callback(gc_type, callback_flags);
891 }
892 }
893
Steve Blocka7e24c12009-10-30 11:49:00 +0000894 if (collector == MARK_COMPACTOR && global_gc_epilogue_callback_) {
895 ASSERT(!allocation_allowed_);
Leon Clarkef7060e22010-06-03 12:02:55 +0100896 GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000897 global_gc_epilogue_callback_();
898 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100899 if (FLAG_verify_heap) {
900 VerifySymbolTable();
901 }
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800902
903 return next_gc_likely_to_collect_more;
Steve Blocka7e24c12009-10-30 11:49:00 +0000904}
905
906
Steve Blocka7e24c12009-10-30 11:49:00 +0000907void Heap::MarkCompact(GCTracer* tracer) {
908 gc_state_ = MARK_COMPACT;
Steve Block44f0eee2011-05-26 01:26:41 +0100909 LOG(isolate_, ResourceEvent("markcompact", "begin"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000910
Steve Block44f0eee2011-05-26 01:26:41 +0100911 mark_compact_collector_.Prepare(tracer);
Steve Blocka7e24c12009-10-30 11:49:00 +0000912
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100913 ms_count_++;
914 tracer->set_full_gc_count(ms_count_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000915
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100916 MarkCompactPrologue();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100917
Steve Block44f0eee2011-05-26 01:26:41 +0100918 mark_compact_collector_.CollectGarbage();
Steve Blocka7e24c12009-10-30 11:49:00 +0000919
Steve Block44f0eee2011-05-26 01:26:41 +0100920 LOG(isolate_, ResourceEvent("markcompact", "end"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000921
922 gc_state_ = NOT_IN_GC;
923
Steve Block44f0eee2011-05-26 01:26:41 +0100924 isolate_->counters()->objs_since_last_full()->Set(0);
Steve Block6ded16b2010-05-10 14:33:55 +0100925
926 contexts_disposed_ = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100927
928 isolate_->set_context_exit_happened(false);
Steve Blocka7e24c12009-10-30 11:49:00 +0000929}
930
931
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100932void Heap::MarkCompactPrologue() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 // At any old GC clear the keyed lookup cache to enable collection of unused
934 // maps.
Steve Block44f0eee2011-05-26 01:26:41 +0100935 isolate_->keyed_lookup_cache()->Clear();
936 isolate_->context_slot_cache()->Clear();
937 isolate_->descriptor_lookup_cache()->Clear();
Ben Murdoch589d6972011-11-30 16:04:58 +0000938 StringSplitCache::Clear(string_split_cache());
Steve Blocka7e24c12009-10-30 11:49:00 +0000939
Steve Block44f0eee2011-05-26 01:26:41 +0100940 isolate_->compilation_cache()->MarkCompactPrologue();
Steve Blocka7e24c12009-10-30 11:49:00 +0000941
Kristian Monsen25f61362010-05-21 11:50:48 +0100942 CompletelyClearInstanceofCache();
943
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100944 FlushNumberStringCache();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000945 if (FLAG_cleanup_code_caches_at_gc) {
946 polymorphic_code_cache()->set_cache(undefined_value());
947 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000948
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100949 ClearNormalizedMapCaches();
Steve Blocka7e24c12009-10-30 11:49:00 +0000950}
951
952
953Object* Heap::FindCodeObject(Address a) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100954 return isolate()->inner_pointer_to_code_cache()->
955 GcSafeFindCodeForInnerPointer(a);
Steve Blocka7e24c12009-10-30 11:49:00 +0000956}
957
958
959// Helper class for copying HeapObjects
960class ScavengeVisitor: public ObjectVisitor {
961 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100962 explicit ScavengeVisitor(Heap* heap) : heap_(heap) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000963
964 void VisitPointer(Object** p) { ScavengePointer(p); }
965
966 void VisitPointers(Object** start, Object** end) {
967 // Copy all HeapObject pointers in [start, end)
968 for (Object** p = start; p < end; p++) ScavengePointer(p);
969 }
970
971 private:
972 void ScavengePointer(Object** p) {
973 Object* object = *p;
Steve Block44f0eee2011-05-26 01:26:41 +0100974 if (!heap_->InNewSpace(object)) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000975 Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p),
976 reinterpret_cast<HeapObject*>(object));
977 }
Steve Block44f0eee2011-05-26 01:26:41 +0100978
979 Heap* heap_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000980};
981
982
Steve Blocka7e24c12009-10-30 11:49:00 +0000983#ifdef DEBUG
984// Visitor class to verify pointers in code or data space do not point into
985// new space.
986class VerifyNonPointerSpacePointersVisitor: public ObjectVisitor {
987 public:
988 void VisitPointers(Object** start, Object**end) {
989 for (Object** current = start; current < end; current++) {
990 if ((*current)->IsHeapObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100991 ASSERT(!HEAP->InNewSpace(HeapObject::cast(*current)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000992 }
993 }
994 }
995};
996
997
998static void VerifyNonPointerSpacePointers() {
999 // Verify that there are no pointers to new space in spaces where we
1000 // do not expect them.
1001 VerifyNonPointerSpacePointersVisitor v;
Steve Block44f0eee2011-05-26 01:26:41 +01001002 HeapObjectIterator code_it(HEAP->code_space());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001003 for (HeapObject* object = code_it.Next();
1004 object != NULL; object = code_it.Next())
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 object->Iterate(&v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001006
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001007 // The old data space was normally swept conservatively so that the iterator
1008 // doesn't work, so we normally skip the next bit.
1009 if (!HEAP->old_data_space()->was_swept_conservatively()) {
1010 HeapObjectIterator data_it(HEAP->old_data_space());
1011 for (HeapObject* object = data_it.Next();
1012 object != NULL; object = data_it.Next())
1013 object->Iterate(&v);
1014 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001015}
1016#endif
1017
1018
Steve Block6ded16b2010-05-10 14:33:55 +01001019void Heap::CheckNewSpaceExpansionCriteria() {
1020 if (new_space_.Capacity() < new_space_.MaximumCapacity() &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001021 survived_since_last_expansion_ > new_space_.Capacity() &&
1022 !new_space_high_promotion_mode_active_) {
1023 // Grow the size of new space if there is room to grow, enough data
1024 // has survived scavenge since the last expansion and we are not in
1025 // high promotion mode.
Steve Block6ded16b2010-05-10 14:33:55 +01001026 new_space_.Grow();
1027 survived_since_last_expansion_ = 0;
1028 }
1029}
1030
1031
Ben Murdoch257744e2011-11-30 15:57:28 +00001032static bool IsUnscavengedHeapObject(Heap* heap, Object** p) {
1033 return heap->InNewSpace(*p) &&
1034 !HeapObject::cast(*p)->map_word().IsForwardingAddress();
1035}
1036
1037
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001038void Heap::ScavengeStoreBufferCallback(
1039 Heap* heap,
1040 MemoryChunk* page,
1041 StoreBufferEvent event) {
1042 heap->store_buffer_rebuilder_.Callback(page, event);
1043}
1044
1045
1046void StoreBufferRebuilder::Callback(MemoryChunk* page, StoreBufferEvent event) {
1047 if (event == kStoreBufferStartScanningPagesEvent) {
1048 start_of_current_page_ = NULL;
1049 current_page_ = NULL;
1050 } else if (event == kStoreBufferScanningPageEvent) {
1051 if (current_page_ != NULL) {
1052 // If this page already overflowed the store buffer during this iteration.
1053 if (current_page_->scan_on_scavenge()) {
1054 // Then we should wipe out the entries that have been added for it.
1055 store_buffer_->SetTop(start_of_current_page_);
1056 } else if (store_buffer_->Top() - start_of_current_page_ >=
1057 (store_buffer_->Limit() - store_buffer_->Top()) >> 2) {
1058 // Did we find too many pointers in the previous page? The heuristic is
1059 // that no page can take more then 1/5 the remaining slots in the store
1060 // buffer.
1061 current_page_->set_scan_on_scavenge(true);
1062 store_buffer_->SetTop(start_of_current_page_);
1063 } else {
1064 // In this case the page we scanned took a reasonable number of slots in
1065 // the store buffer. It has now been rehabilitated and is no longer
1066 // marked scan_on_scavenge.
1067 ASSERT(!current_page_->scan_on_scavenge());
1068 }
1069 }
1070 start_of_current_page_ = store_buffer_->Top();
1071 current_page_ = page;
1072 } else if (event == kStoreBufferFullEvent) {
1073 // The current page overflowed the store buffer again. Wipe out its entries
1074 // in the store buffer and mark it scan-on-scavenge again. This may happen
1075 // several times while scanning.
1076 if (current_page_ == NULL) {
1077 // Store Buffer overflowed while scanning promoted objects. These are not
1078 // in any particular page, though they are likely to be clustered by the
1079 // allocation routines.
1080 store_buffer_->EnsureSpace(StoreBuffer::kStoreBufferSize);
1081 } else {
1082 // Store Buffer overflowed while scanning a particular old space page for
1083 // pointers to new space.
1084 ASSERT(current_page_ == page);
1085 ASSERT(page != NULL);
1086 current_page_->set_scan_on_scavenge(true);
1087 ASSERT(start_of_current_page_ != store_buffer_->Top());
1088 store_buffer_->SetTop(start_of_current_page_);
1089 }
1090 } else {
1091 UNREACHABLE();
1092 }
1093}
1094
1095
1096void PromotionQueue::Initialize() {
1097 // Assumes that a NewSpacePage exactly fits a number of promotion queue
1098 // entries (where each is a pair of intptr_t). This allows us to simplify
1099 // the test fpr when to switch pages.
1100 ASSERT((Page::kPageSize - MemoryChunk::kBodyOffset) % (2 * kPointerSize)
1101 == 0);
1102 limit_ = reinterpret_cast<intptr_t*>(heap_->new_space()->ToSpaceStart());
1103 front_ = rear_ =
1104 reinterpret_cast<intptr_t*>(heap_->new_space()->ToSpaceEnd());
1105 emergency_stack_ = NULL;
1106 guard_ = false;
1107}
1108
1109
1110void PromotionQueue::RelocateQueueHead() {
1111 ASSERT(emergency_stack_ == NULL);
1112
1113 Page* p = Page::FromAllocationTop(reinterpret_cast<Address>(rear_));
1114 intptr_t* head_start = rear_;
1115 intptr_t* head_end =
1116 Min(front_, reinterpret_cast<intptr_t*>(p->area_end()));
1117
1118 int entries_count =
1119 static_cast<int>(head_end - head_start) / kEntrySizeInWords;
1120
1121 emergency_stack_ = new List<Entry>(2 * entries_count);
1122
1123 while (head_start != head_end) {
1124 int size = static_cast<int>(*(head_start++));
1125 HeapObject* obj = reinterpret_cast<HeapObject*>(*(head_start++));
1126 emergency_stack_->Add(Entry(obj, size));
1127 }
1128 rear_ = head_end;
1129}
1130
1131
Steve Blocka7e24c12009-10-30 11:49:00 +00001132void Heap::Scavenge() {
1133#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001134 if (FLAG_verify_heap) VerifyNonPointerSpacePointers();
Steve Blocka7e24c12009-10-30 11:49:00 +00001135#endif
1136
1137 gc_state_ = SCAVENGE;
1138
1139 // Implements Cheney's copying algorithm
Steve Block44f0eee2011-05-26 01:26:41 +01001140 LOG(isolate_, ResourceEvent("scavenge", "begin"));
Steve Blocka7e24c12009-10-30 11:49:00 +00001141
1142 // Clear descriptor cache.
Steve Block44f0eee2011-05-26 01:26:41 +01001143 isolate_->descriptor_lookup_cache()->Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +00001144
1145 // Used for updating survived_since_last_expansion_ at function end.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001146 intptr_t survived_watermark = PromotedSpaceSizeOfObjects();
Steve Blocka7e24c12009-10-30 11:49:00 +00001147
Steve Block6ded16b2010-05-10 14:33:55 +01001148 CheckNewSpaceExpansionCriteria();
Steve Blocka7e24c12009-10-30 11:49:00 +00001149
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001150 SelectScavengingVisitorsTable();
1151
1152 incremental_marking()->PrepareForScavenge();
1153
1154 AdvanceSweepers(static_cast<int>(new_space_.Size()));
1155
Steve Blocka7e24c12009-10-30 11:49:00 +00001156 // Flip the semispaces. After flipping, to space is empty, from space has
1157 // live objects.
1158 new_space_.Flip();
1159 new_space_.ResetAllocationInfo();
1160
1161 // We need to sweep newly copied objects which can be either in the
1162 // to space or promoted to the old generation. For to-space
1163 // objects, we treat the bottom of the to space as a queue. Newly
1164 // copied and unswept objects lie between a 'front' mark and the
1165 // allocation pointer.
1166 //
1167 // Promoted objects can go into various old-generation spaces, and
1168 // can be allocated internally in the spaces (from the free list).
1169 // We treat the top of the to space as a queue of addresses of
1170 // promoted objects. The addresses of newly promoted and unswept
1171 // objects lie between a 'front' mark and a 'rear' mark that is
1172 // updated as a side effect of promoting an object.
1173 //
1174 // There is guaranteed to be enough room at the top of the to space
1175 // for the addresses of promoted objects: every object promoted
1176 // frees up its size in bytes from the top of the new space, and
1177 // objects are at least one pointer in size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001178 Address new_space_front = new_space_.ToSpaceStart();
1179 promotion_queue_.Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +00001180
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001181#ifdef DEBUG
1182 store_buffer()->Clean();
1183#endif
1184
Steve Block44f0eee2011-05-26 01:26:41 +01001185 ScavengeVisitor scavenge_visitor(this);
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 // Copy roots.
Leon Clarkee46be812010-01-19 14:06:41 +00001187 IterateRoots(&scavenge_visitor, VISIT_ALL_IN_SCAVENGE);
Steve Blocka7e24c12009-10-30 11:49:00 +00001188
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001189 // Copy objects reachable from the old generation.
1190 {
1191 StoreBufferRebuildScope scope(this,
1192 store_buffer(),
1193 &ScavengeStoreBufferCallback);
1194 store_buffer()->IteratePointersToNewSpace(&ScavengeObject);
1195 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001196
1197 // Copy objects reachable from cells by scavenging cell values directly.
1198 HeapObjectIterator cell_iterator(cell_space_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001199 for (HeapObject* cell = cell_iterator.Next();
1200 cell != NULL; cell = cell_iterator.Next()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001201 if (cell->IsJSGlobalPropertyCell()) {
1202 Address value_address =
1203 reinterpret_cast<Address>(cell) +
1204 (JSGlobalPropertyCell::kValueOffset - kHeapObjectTag);
1205 scavenge_visitor.VisitPointer(reinterpret_cast<Object**>(value_address));
1206 }
1207 }
1208
Ben Murdochf87a2032010-10-22 12:50:53 +01001209 // Scavenge object reachable from the global contexts list directly.
1210 scavenge_visitor.VisitPointer(BitCast<Object**>(&global_contexts_list_));
1211
Leon Clarkee46be812010-01-19 14:06:41 +00001212 new_space_front = DoScavenge(&scavenge_visitor, new_space_front);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001213 isolate_->global_handles()->IdentifyNewSpaceWeakIndependentHandles(
Ben Murdoch257744e2011-11-30 15:57:28 +00001214 &IsUnscavengedHeapObject);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001215 isolate_->global_handles()->IterateNewSpaceWeakIndependentRoots(
1216 &scavenge_visitor);
Ben Murdoch257744e2011-11-30 15:57:28 +00001217 new_space_front = DoScavenge(&scavenge_visitor, new_space_front);
1218
Steve Block6ded16b2010-05-10 14:33:55 +01001219 UpdateNewSpaceReferencesInExternalStringTable(
1220 &UpdateNewSpaceReferenceInExternalStringTableEntry);
1221
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001222 promotion_queue_.Destroy();
1223
Steve Block1e0659c2011-05-24 12:43:12 +01001224 LiveObjectList::UpdateReferencesForScavengeGC();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001225 if (!FLAG_watch_ic_patching) {
1226 isolate()->runtime_profiler()->UpdateSamplesAfterScavenge();
1227 }
1228 incremental_marking()->UpdateMarkingDequeAfterScavenge();
Steve Block1e0659c2011-05-24 12:43:12 +01001229
Leon Clarkee46be812010-01-19 14:06:41 +00001230 ASSERT(new_space_front == new_space_.top());
1231
1232 // Set age mark.
1233 new_space_.set_age_mark(new_space_.top());
1234
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001235 new_space_.LowerInlineAllocationLimit(
1236 new_space_.inline_allocation_limit_step());
1237
Leon Clarkee46be812010-01-19 14:06:41 +00001238 // Update how much has survived scavenge.
Ben Murdochf87a2032010-10-22 12:50:53 +01001239 IncrementYoungSurvivorsCounter(static_cast<int>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001240 (PromotedSpaceSizeOfObjects() - survived_watermark) + new_space_.Size()));
Leon Clarkee46be812010-01-19 14:06:41 +00001241
Steve Block44f0eee2011-05-26 01:26:41 +01001242 LOG(isolate_, ResourceEvent("scavenge", "end"));
Leon Clarkee46be812010-01-19 14:06:41 +00001243
1244 gc_state_ = NOT_IN_GC;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001245
1246 scavenges_since_last_idle_round_++;
Leon Clarkee46be812010-01-19 14:06:41 +00001247}
1248
1249
Steve Block44f0eee2011-05-26 01:26:41 +01001250String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap,
1251 Object** p) {
Steve Block6ded16b2010-05-10 14:33:55 +01001252 MapWord first_word = HeapObject::cast(*p)->map_word();
1253
1254 if (!first_word.IsForwardingAddress()) {
1255 // Unreachable external string can be finalized.
Steve Block44f0eee2011-05-26 01:26:41 +01001256 heap->FinalizeExternalString(String::cast(*p));
Steve Block6ded16b2010-05-10 14:33:55 +01001257 return NULL;
1258 }
1259
1260 // String is still reachable.
1261 return String::cast(first_word.ToForwardingAddress());
1262}
1263
1264
1265void Heap::UpdateNewSpaceReferencesInExternalStringTable(
1266 ExternalStringTableUpdaterCallback updater_func) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001267 if (FLAG_verify_heap) {
1268 external_string_table_.Verify();
1269 }
Leon Clarkee46be812010-01-19 14:06:41 +00001270
Steve Block44f0eee2011-05-26 01:26:41 +01001271 if (external_string_table_.new_space_strings_.is_empty()) return;
Leon Clarkee46be812010-01-19 14:06:41 +00001272
Steve Block44f0eee2011-05-26 01:26:41 +01001273 Object** start = &external_string_table_.new_space_strings_[0];
1274 Object** end = start + external_string_table_.new_space_strings_.length();
Leon Clarkee46be812010-01-19 14:06:41 +00001275 Object** last = start;
1276
1277 for (Object** p = start; p < end; ++p) {
Steve Block44f0eee2011-05-26 01:26:41 +01001278 ASSERT(InFromSpace(*p));
1279 String* target = updater_func(this, p);
Leon Clarkee46be812010-01-19 14:06:41 +00001280
Steve Block6ded16b2010-05-10 14:33:55 +01001281 if (target == NULL) continue;
Leon Clarkee46be812010-01-19 14:06:41 +00001282
Leon Clarkee46be812010-01-19 14:06:41 +00001283 ASSERT(target->IsExternalString());
1284
Steve Block44f0eee2011-05-26 01:26:41 +01001285 if (InNewSpace(target)) {
Leon Clarkee46be812010-01-19 14:06:41 +00001286 // String is still in new space. Update the table entry.
1287 *last = target;
1288 ++last;
1289 } else {
1290 // String got promoted. Move it to the old string list.
Steve Block44f0eee2011-05-26 01:26:41 +01001291 external_string_table_.AddOldString(target);
Leon Clarkee46be812010-01-19 14:06:41 +00001292 }
1293 }
1294
1295 ASSERT(last <= end);
Steve Block44f0eee2011-05-26 01:26:41 +01001296 external_string_table_.ShrinkNewStrings(static_cast<int>(last - start));
Leon Clarkee46be812010-01-19 14:06:41 +00001297}
1298
1299
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001300void Heap::UpdateReferencesInExternalStringTable(
1301 ExternalStringTableUpdaterCallback updater_func) {
1302
1303 // Update old space string references.
1304 if (external_string_table_.old_space_strings_.length() > 0) {
1305 Object** start = &external_string_table_.old_space_strings_[0];
1306 Object** end = start + external_string_table_.old_space_strings_.length();
1307 for (Object** p = start; p < end; ++p) *p = updater_func(this, p);
1308 }
1309
1310 UpdateNewSpaceReferencesInExternalStringTable(updater_func);
1311}
1312
1313
Steve Block44f0eee2011-05-26 01:26:41 +01001314static Object* ProcessFunctionWeakReferences(Heap* heap,
1315 Object* function,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001316 WeakObjectRetainer* retainer) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001317 Object* undefined = heap->undefined_value();
1318 Object* head = undefined;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001319 JSFunction* tail = NULL;
1320 Object* candidate = function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001321 while (candidate != undefined) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001322 // Check whether to keep the candidate in the list.
1323 JSFunction* candidate_function = reinterpret_cast<JSFunction*>(candidate);
1324 Object* retain = retainer->RetainAs(candidate);
1325 if (retain != NULL) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001326 if (head == undefined) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001327 // First element in the list.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001328 head = retain;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001329 } else {
1330 // Subsequent elements in the list.
1331 ASSERT(tail != NULL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001332 tail->set_next_function_link(retain);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001333 }
1334 // Retained function is new tail.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001335 candidate_function = reinterpret_cast<JSFunction*>(retain);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001336 tail = candidate_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001337
1338 ASSERT(retain->IsUndefined() || retain->IsJSFunction());
1339
1340 if (retain == undefined) break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001341 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001342
Ben Murdochb0fe1622011-05-05 13:52:32 +01001343 // Move to next element in the list.
1344 candidate = candidate_function->next_function_link();
1345 }
1346
1347 // Terminate the list if there is one or more elements.
1348 if (tail != NULL) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001349 tail->set_next_function_link(undefined);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001350 }
1351
1352 return head;
1353}
1354
1355
Ben Murdochf87a2032010-10-22 12:50:53 +01001356void Heap::ProcessWeakReferences(WeakObjectRetainer* retainer) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001357 Object* undefined = undefined_value();
1358 Object* head = undefined;
Ben Murdochf87a2032010-10-22 12:50:53 +01001359 Context* tail = NULL;
1360 Object* candidate = global_contexts_list_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001361 while (candidate != undefined) {
Ben Murdochf87a2032010-10-22 12:50:53 +01001362 // Check whether to keep the candidate in the list.
1363 Context* candidate_context = reinterpret_cast<Context*>(candidate);
1364 Object* retain = retainer->RetainAs(candidate);
1365 if (retain != NULL) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001366 if (head == undefined) {
Ben Murdochf87a2032010-10-22 12:50:53 +01001367 // First element in the list.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001368 head = retain;
Ben Murdochf87a2032010-10-22 12:50:53 +01001369 } else {
1370 // Subsequent elements in the list.
1371 ASSERT(tail != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +01001372 tail->set_unchecked(this,
1373 Context::NEXT_CONTEXT_LINK,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001374 retain,
Ben Murdochf87a2032010-10-22 12:50:53 +01001375 UPDATE_WRITE_BARRIER);
1376 }
1377 // Retained context is new tail.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001378 candidate_context = reinterpret_cast<Context*>(retain);
Ben Murdochf87a2032010-10-22 12:50:53 +01001379 tail = candidate_context;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001380
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001381 if (retain == undefined) break;
1382
Ben Murdochb0fe1622011-05-05 13:52:32 +01001383 // Process the weak list of optimized functions for the context.
1384 Object* function_list_head =
1385 ProcessFunctionWeakReferences(
Steve Block44f0eee2011-05-26 01:26:41 +01001386 this,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001387 candidate_context->get(Context::OPTIMIZED_FUNCTIONS_LIST),
1388 retainer);
Steve Block44f0eee2011-05-26 01:26:41 +01001389 candidate_context->set_unchecked(this,
1390 Context::OPTIMIZED_FUNCTIONS_LIST,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001391 function_list_head,
1392 UPDATE_WRITE_BARRIER);
Ben Murdochf87a2032010-10-22 12:50:53 +01001393 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001394
Ben Murdochf87a2032010-10-22 12:50:53 +01001395 // Move to next element in the list.
1396 candidate = candidate_context->get(Context::NEXT_CONTEXT_LINK);
1397 }
1398
1399 // Terminate the list if there is one or more elements.
1400 if (tail != NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +01001401 tail->set_unchecked(this,
1402 Context::NEXT_CONTEXT_LINK,
Ben Murdochf87a2032010-10-22 12:50:53 +01001403 Heap::undefined_value(),
1404 UPDATE_WRITE_BARRIER);
1405 }
1406
1407 // Update the head of the list of contexts.
Steve Block44f0eee2011-05-26 01:26:41 +01001408 global_contexts_list_ = head;
Ben Murdochf87a2032010-10-22 12:50:53 +01001409}
1410
1411
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001412void Heap::VisitExternalResources(v8::ExternalResourceVisitor* visitor) {
1413 AssertNoAllocation no_allocation;
1414
1415 class VisitorAdapter : public ObjectVisitor {
1416 public:
1417 explicit VisitorAdapter(v8::ExternalResourceVisitor* visitor)
1418 : visitor_(visitor) {}
1419 virtual void VisitPointers(Object** start, Object** end) {
1420 for (Object** p = start; p < end; p++) {
1421 if ((*p)->IsExternalString()) {
1422 visitor_->VisitExternalString(Utils::ToLocal(
1423 Handle<String>(String::cast(*p))));
1424 }
1425 }
1426 }
1427 private:
1428 v8::ExternalResourceVisitor* visitor_;
1429 } visitor_adapter(visitor);
1430 external_string_table_.Iterate(&visitor_adapter);
1431}
1432
1433
Iain Merrick75681382010-08-19 15:07:18 +01001434class NewSpaceScavenger : public StaticNewSpaceVisitor<NewSpaceScavenger> {
1435 public:
Steve Block44f0eee2011-05-26 01:26:41 +01001436 static inline void VisitPointer(Heap* heap, Object** p) {
Iain Merrick75681382010-08-19 15:07:18 +01001437 Object* object = *p;
Steve Block44f0eee2011-05-26 01:26:41 +01001438 if (!heap->InNewSpace(object)) return;
Iain Merrick75681382010-08-19 15:07:18 +01001439 Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p),
1440 reinterpret_cast<HeapObject*>(object));
1441 }
1442};
1443
1444
Leon Clarkee46be812010-01-19 14:06:41 +00001445Address Heap::DoScavenge(ObjectVisitor* scavenge_visitor,
1446 Address new_space_front) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001447 do {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001448 SemiSpace::AssertValidRange(new_space_front, new_space_.top());
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 // The addresses new_space_front and new_space_.top() define a
1450 // queue of unprocessed copied objects. Process them until the
1451 // queue is empty.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001452 while (new_space_front != new_space_.top()) {
1453 if (!NewSpacePage::IsAtEnd(new_space_front)) {
1454 HeapObject* object = HeapObject::FromAddress(new_space_front);
1455 new_space_front +=
1456 NewSpaceScavenger::IterateBody(object->map(), object);
1457 } else {
1458 new_space_front =
1459 NewSpacePage::FromLimit(new_space_front)->next_page()->area_start();
1460 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001461 }
1462
1463 // Promote and process all the to-be-promoted objects.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001464 {
1465 StoreBufferRebuildScope scope(this,
1466 store_buffer(),
1467 &ScavengeStoreBufferCallback);
1468 while (!promotion_queue()->is_empty()) {
1469 HeapObject* target;
1470 int size;
1471 promotion_queue()->remove(&target, &size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001472
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001473 // Promoted object might be already partially visited
1474 // during old space pointer iteration. Thus we search specificly
1475 // for pointers to from semispace instead of looking for pointers
1476 // to new space.
1477 ASSERT(!target->IsMap());
1478 IterateAndMarkPointersToFromSpace(target->address(),
1479 target->address() + size,
1480 &ScavengeObject);
1481 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001482 }
1483
1484 // Take another spin if there are now unswept objects in new space
1485 // (there are currently no more unswept promoted objects).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001486 } while (new_space_front != new_space_.top());
Steve Blocka7e24c12009-10-30 11:49:00 +00001487
Leon Clarkee46be812010-01-19 14:06:41 +00001488 return new_space_front;
Steve Blocka7e24c12009-10-30 11:49:00 +00001489}
1490
1491
Ben Murdoch8b112d22011-06-08 16:22:53 +01001492enum LoggingAndProfiling {
1493 LOGGING_AND_PROFILING_ENABLED,
1494 LOGGING_AND_PROFILING_DISABLED
1495};
1496
1497
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001498enum MarksHandling { TRANSFER_MARKS, IGNORE_MARKS };
Ben Murdoch8b112d22011-06-08 16:22:53 +01001499
1500
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001501template<MarksHandling marks_handling,
1502 LoggingAndProfiling logging_and_profiling_mode>
Iain Merrick75681382010-08-19 15:07:18 +01001503class ScavengingVisitor : public StaticVisitorBase {
1504 public:
1505 static void Initialize() {
1506 table_.Register(kVisitSeqAsciiString, &EvacuateSeqAsciiString);
1507 table_.Register(kVisitSeqTwoByteString, &EvacuateSeqTwoByteString);
1508 table_.Register(kVisitShortcutCandidate, &EvacuateShortcutCandidate);
1509 table_.Register(kVisitByteArray, &EvacuateByteArray);
1510 table_.Register(kVisitFixedArray, &EvacuateFixedArray);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001511 table_.Register(kVisitFixedDoubleArray, &EvacuateFixedDoubleArray);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001512
Ben Murdochf87a2032010-10-22 12:50:53 +01001513 table_.Register(kVisitGlobalContext,
1514 &ObjectEvacuationStrategy<POINTER_OBJECT>::
Ben Murdoch8b112d22011-06-08 16:22:53 +01001515 template VisitSpecialized<Context::kSize>);
Iain Merrick75681382010-08-19 15:07:18 +01001516
1517 table_.Register(kVisitConsString,
1518 &ObjectEvacuationStrategy<POINTER_OBJECT>::
Ben Murdoch8b112d22011-06-08 16:22:53 +01001519 template VisitSpecialized<ConsString::kSize>);
Iain Merrick75681382010-08-19 15:07:18 +01001520
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001521 table_.Register(kVisitSlicedString,
1522 &ObjectEvacuationStrategy<POINTER_OBJECT>::
1523 template VisitSpecialized<SlicedString::kSize>);
1524
Iain Merrick75681382010-08-19 15:07:18 +01001525 table_.Register(kVisitSharedFunctionInfo,
1526 &ObjectEvacuationStrategy<POINTER_OBJECT>::
Ben Murdoch8b112d22011-06-08 16:22:53 +01001527 template VisitSpecialized<SharedFunctionInfo::kSize>);
Iain Merrick75681382010-08-19 15:07:18 +01001528
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001529 table_.Register(kVisitJSWeakMap,
1530 &ObjectEvacuationStrategy<POINTER_OBJECT>::
1531 Visit);
1532
Ben Murdoch257744e2011-11-30 15:57:28 +00001533 table_.Register(kVisitJSRegExp,
1534 &ObjectEvacuationStrategy<POINTER_OBJECT>::
1535 Visit);
1536
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001537 if (marks_handling == IGNORE_MARKS) {
1538 table_.Register(kVisitJSFunction,
1539 &ObjectEvacuationStrategy<POINTER_OBJECT>::
1540 template VisitSpecialized<JSFunction::kSize>);
1541 } else {
1542 table_.Register(kVisitJSFunction, &EvacuateJSFunction);
1543 }
Iain Merrick75681382010-08-19 15:07:18 +01001544
1545 table_.RegisterSpecializations<ObjectEvacuationStrategy<DATA_OBJECT>,
1546 kVisitDataObject,
1547 kVisitDataObjectGeneric>();
1548
1549 table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>,
1550 kVisitJSObject,
1551 kVisitJSObjectGeneric>();
1552
1553 table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>,
1554 kVisitStruct,
1555 kVisitStructGeneric>();
1556 }
1557
Ben Murdoch8b112d22011-06-08 16:22:53 +01001558 static VisitorDispatchTable<ScavengingCallback>* GetTable() {
1559 return &table_;
Iain Merrick75681382010-08-19 15:07:18 +01001560 }
1561
Iain Merrick75681382010-08-19 15:07:18 +01001562 private:
1563 enum ObjectContents { DATA_OBJECT, POINTER_OBJECT };
1564 enum SizeRestriction { SMALL, UNKNOWN_SIZE };
1565
Steve Block44f0eee2011-05-26 01:26:41 +01001566 static void RecordCopiedObject(Heap* heap, HeapObject* obj) {
Iain Merrick75681382010-08-19 15:07:18 +01001567 bool should_record = false;
Steve Blocka7e24c12009-10-30 11:49:00 +00001568#ifdef DEBUG
Iain Merrick75681382010-08-19 15:07:18 +01001569 should_record = FLAG_heap_stats;
Steve Blocka7e24c12009-10-30 11:49:00 +00001570#endif
Iain Merrick75681382010-08-19 15:07:18 +01001571 should_record = should_record || FLAG_log_gc;
Iain Merrick75681382010-08-19 15:07:18 +01001572 if (should_record) {
Steve Block44f0eee2011-05-26 01:26:41 +01001573 if (heap->new_space()->Contains(obj)) {
1574 heap->new_space()->RecordAllocation(obj);
Iain Merrick75681382010-08-19 15:07:18 +01001575 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001576 heap->new_space()->RecordPromotion(obj);
Iain Merrick75681382010-08-19 15:07:18 +01001577 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001578 }
1579 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001580
Iain Merrick75681382010-08-19 15:07:18 +01001581 // Helper function used by CopyObject to copy a source object to an
1582 // allocated target object and update the forwarding pointer in the source
1583 // object. Returns the target object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001584 INLINE(static void MigrateObject(Heap* heap,
1585 HeapObject* source,
1586 HeapObject* target,
1587 int size)) {
Iain Merrick75681382010-08-19 15:07:18 +01001588 // Copy the content of source to target.
Steve Block44f0eee2011-05-26 01:26:41 +01001589 heap->CopyBlock(target->address(), source->address(), size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001590
Iain Merrick75681382010-08-19 15:07:18 +01001591 // Set the forwarding address.
1592 source->set_map_word(MapWord::FromForwardingAddress(target));
Steve Blocka7e24c12009-10-30 11:49:00 +00001593
Ben Murdoch8b112d22011-06-08 16:22:53 +01001594 if (logging_and_profiling_mode == LOGGING_AND_PROFILING_ENABLED) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001595 // Update NewSpace stats if necessary.
1596 RecordCopiedObject(heap, target);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001597 HEAP_PROFILE(heap, ObjectMoveEvent(source->address(), target->address()));
Ben Murdoch8b112d22011-06-08 16:22:53 +01001598 Isolate* isolate = heap->isolate();
1599 if (isolate->logger()->is_logging() ||
Ben Murdoch257744e2011-11-30 15:57:28 +00001600 CpuProfiler::is_profiling(isolate)) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001601 if (target->IsSharedFunctionInfo()) {
1602 PROFILE(isolate, SharedFunctionInfoMoveEvent(
1603 source->address(), target->address()));
1604 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001605 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001606 }
1607
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001608 if (marks_handling == TRANSFER_MARKS) {
1609 if (Marking::TransferColor(source, target)) {
1610 MemoryChunk::IncrementLiveBytesFromGC(target->address(), size);
1611 }
1612 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001613 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001614
Iain Merrick75681382010-08-19 15:07:18 +01001615 template<ObjectContents object_contents, SizeRestriction size_restriction>
1616 static inline void EvacuateObject(Map* map,
1617 HeapObject** slot,
1618 HeapObject* object,
1619 int object_size) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001620 SLOW_ASSERT((size_restriction != SMALL) ||
1621 (object_size <= Page::kMaxNonCodeHeapObjectSize));
1622 SLOW_ASSERT(object->Size() == object_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001623
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001624 Heap* heap = map->GetHeap();
Steve Block44f0eee2011-05-26 01:26:41 +01001625 if (heap->ShouldBePromoted(object->address(), object_size)) {
John Reck59135872010-11-02 12:39:01 -07001626 MaybeObject* maybe_result;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001627
Iain Merrick75681382010-08-19 15:07:18 +01001628 if ((size_restriction != SMALL) &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001629 (object_size > Page::kMaxNonCodeHeapObjectSize)) {
1630 maybe_result = heap->lo_space()->AllocateRaw(object_size,
1631 NOT_EXECUTABLE);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001632 } else {
Iain Merrick75681382010-08-19 15:07:18 +01001633 if (object_contents == DATA_OBJECT) {
Steve Block44f0eee2011-05-26 01:26:41 +01001634 maybe_result = heap->old_data_space()->AllocateRaw(object_size);
Iain Merrick75681382010-08-19 15:07:18 +01001635 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01001636 maybe_result = heap->old_pointer_space()->AllocateRaw(object_size);
Iain Merrick75681382010-08-19 15:07:18 +01001637 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001638 }
1639
John Reck59135872010-11-02 12:39:01 -07001640 Object* result = NULL; // Initialization to please compiler.
1641 if (maybe_result->ToObject(&result)) {
Iain Merrick75681382010-08-19 15:07:18 +01001642 HeapObject* target = HeapObject::cast(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001643
1644 // Order is important: slot might be inside of the target if target
1645 // was allocated over a dead object and slot comes from the store
1646 // buffer.
1647 *slot = target;
1648 MigrateObject(heap, object, target, object_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001649
Iain Merrick75681382010-08-19 15:07:18 +01001650 if (object_contents == POINTER_OBJECT) {
Steve Block44f0eee2011-05-26 01:26:41 +01001651 heap->promotion_queue()->insert(target, object_size);
Iain Merrick75681382010-08-19 15:07:18 +01001652 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001653
Steve Block44f0eee2011-05-26 01:26:41 +01001654 heap->tracer()->increment_promoted_objects_size(object_size);
Iain Merrick75681382010-08-19 15:07:18 +01001655 return;
1656 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001657 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001658 MaybeObject* allocation = heap->new_space()->AllocateRaw(object_size);
1659 heap->promotion_queue()->SetNewLimit(heap->new_space()->top());
1660 Object* result = allocation->ToObjectUnchecked();
1661 HeapObject* target = HeapObject::cast(result);
1662
1663 // Order is important: slot might be inside of the target if target
1664 // was allocated over a dead object and slot comes from the store
1665 // buffer.
1666 *slot = target;
1667 MigrateObject(heap, object, target, object_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001668 return;
1669 }
1670
Iain Merrick75681382010-08-19 15:07:18 +01001671
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001672 static inline void EvacuateJSFunction(Map* map,
1673 HeapObject** slot,
1674 HeapObject* object) {
1675 ObjectEvacuationStrategy<POINTER_OBJECT>::
1676 template VisitSpecialized<JSFunction::kSize>(map, slot, object);
1677
1678 HeapObject* target = *slot;
1679 MarkBit mark_bit = Marking::MarkBitFrom(target);
1680 if (Marking::IsBlack(mark_bit)) {
1681 // This object is black and it might not be rescanned by marker.
1682 // We should explicitly record code entry slot for compaction because
1683 // promotion queue processing (IterateAndMarkPointersToFromSpace) will
1684 // miss it as it is not HeapObject-tagged.
1685 Address code_entry_slot =
1686 target->address() + JSFunction::kCodeEntryOffset;
1687 Code* code = Code::cast(Code::GetObjectFromEntryAddress(code_entry_slot));
1688 map->GetHeap()->mark_compact_collector()->
1689 RecordCodeEntrySlot(code_entry_slot, code);
1690 }
1691 }
1692
1693
Iain Merrick75681382010-08-19 15:07:18 +01001694 static inline void EvacuateFixedArray(Map* map,
1695 HeapObject** slot,
1696 HeapObject* object) {
1697 int object_size = FixedArray::BodyDescriptor::SizeOf(map, object);
1698 EvacuateObject<POINTER_OBJECT, UNKNOWN_SIZE>(map,
1699 slot,
1700 object,
1701 object_size);
1702 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001703
1704
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001705 static inline void EvacuateFixedDoubleArray(Map* map,
1706 HeapObject** slot,
1707 HeapObject* object) {
1708 int length = reinterpret_cast<FixedDoubleArray*>(object)->length();
1709 int object_size = FixedDoubleArray::SizeFor(length);
1710 EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map,
1711 slot,
1712 object,
1713 object_size);
1714 }
1715
1716
Iain Merrick75681382010-08-19 15:07:18 +01001717 static inline void EvacuateByteArray(Map* map,
1718 HeapObject** slot,
1719 HeapObject* object) {
1720 int object_size = reinterpret_cast<ByteArray*>(object)->ByteArraySize();
1721 EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size);
1722 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001723
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001724
Iain Merrick75681382010-08-19 15:07:18 +01001725 static inline void EvacuateSeqAsciiString(Map* map,
1726 HeapObject** slot,
1727 HeapObject* object) {
1728 int object_size = SeqAsciiString::cast(object)->
1729 SeqAsciiStringSize(map->instance_type());
1730 EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size);
1731 }
1732
1733
1734 static inline void EvacuateSeqTwoByteString(Map* map,
1735 HeapObject** slot,
1736 HeapObject* object) {
1737 int object_size = SeqTwoByteString::cast(object)->
1738 SeqTwoByteStringSize(map->instance_type());
1739 EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size);
1740 }
1741
1742
1743 static inline bool IsShortcutCandidate(int type) {
1744 return ((type & kShortcutTypeMask) == kShortcutTypeTag);
1745 }
1746
1747 static inline void EvacuateShortcutCandidate(Map* map,
1748 HeapObject** slot,
1749 HeapObject* object) {
1750 ASSERT(IsShortcutCandidate(map->instance_type()));
1751
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001752 Heap* heap = map->GetHeap();
1753
1754 if (marks_handling == IGNORE_MARKS &&
1755 ConsString::cast(object)->unchecked_second() ==
1756 heap->empty_string()) {
Iain Merrick75681382010-08-19 15:07:18 +01001757 HeapObject* first =
1758 HeapObject::cast(ConsString::cast(object)->unchecked_first());
1759
1760 *slot = first;
1761
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001762 if (!heap->InNewSpace(first)) {
Iain Merrick75681382010-08-19 15:07:18 +01001763 object->set_map_word(MapWord::FromForwardingAddress(first));
1764 return;
1765 }
1766
1767 MapWord first_word = first->map_word();
1768 if (first_word.IsForwardingAddress()) {
1769 HeapObject* target = first_word.ToForwardingAddress();
1770
1771 *slot = target;
1772 object->set_map_word(MapWord::FromForwardingAddress(target));
1773 return;
1774 }
1775
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001776 heap->DoScavengeObject(first->map(), slot, first);
Iain Merrick75681382010-08-19 15:07:18 +01001777 object->set_map_word(MapWord::FromForwardingAddress(*slot));
1778 return;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001779 }
Iain Merrick75681382010-08-19 15:07:18 +01001780
1781 int object_size = ConsString::kSize;
1782 EvacuateObject<POINTER_OBJECT, SMALL>(map, slot, object, object_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001783 }
1784
Iain Merrick75681382010-08-19 15:07:18 +01001785 template<ObjectContents object_contents>
1786 class ObjectEvacuationStrategy {
1787 public:
1788 template<int object_size>
1789 static inline void VisitSpecialized(Map* map,
1790 HeapObject** slot,
1791 HeapObject* object) {
1792 EvacuateObject<object_contents, SMALL>(map, slot, object, object_size);
1793 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001794
Iain Merrick75681382010-08-19 15:07:18 +01001795 static inline void Visit(Map* map,
1796 HeapObject** slot,
1797 HeapObject* object) {
1798 int object_size = map->instance_size();
1799 EvacuateObject<object_contents, SMALL>(map, slot, object, object_size);
1800 }
1801 };
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001802
Ben Murdoch8b112d22011-06-08 16:22:53 +01001803 static VisitorDispatchTable<ScavengingCallback> table_;
Iain Merrick75681382010-08-19 15:07:18 +01001804};
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001805
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001806
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001807template<MarksHandling marks_handling,
1808 LoggingAndProfiling logging_and_profiling_mode>
Ben Murdoch8b112d22011-06-08 16:22:53 +01001809VisitorDispatchTable<ScavengingCallback>
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001810 ScavengingVisitor<marks_handling, logging_and_profiling_mode>::table_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001811
1812
1813static void InitializeScavengingVisitorsTables() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001814 ScavengingVisitor<TRANSFER_MARKS,
1815 LOGGING_AND_PROFILING_DISABLED>::Initialize();
1816 ScavengingVisitor<IGNORE_MARKS, LOGGING_AND_PROFILING_DISABLED>::Initialize();
1817 ScavengingVisitor<TRANSFER_MARKS,
1818 LOGGING_AND_PROFILING_ENABLED>::Initialize();
1819 ScavengingVisitor<IGNORE_MARKS, LOGGING_AND_PROFILING_ENABLED>::Initialize();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001820}
1821
1822
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001823void Heap::SelectScavengingVisitorsTable() {
1824 bool logging_and_profiling =
1825 isolate()->logger()->is_logging() ||
Ben Murdoch257744e2011-11-30 15:57:28 +00001826 CpuProfiler::is_profiling(isolate()) ||
Ben Murdoch8b112d22011-06-08 16:22:53 +01001827 (isolate()->heap_profiler() != NULL &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001828 isolate()->heap_profiler()->is_profiling());
Ben Murdoch8b112d22011-06-08 16:22:53 +01001829
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001830 if (!incremental_marking()->IsMarking()) {
1831 if (!logging_and_profiling) {
1832 scavenging_visitors_table_.CopyFrom(
1833 ScavengingVisitor<IGNORE_MARKS,
1834 LOGGING_AND_PROFILING_DISABLED>::GetTable());
1835 } else {
1836 scavenging_visitors_table_.CopyFrom(
1837 ScavengingVisitor<IGNORE_MARKS,
1838 LOGGING_AND_PROFILING_ENABLED>::GetTable());
1839 }
1840 } else {
1841 if (!logging_and_profiling) {
1842 scavenging_visitors_table_.CopyFrom(
1843 ScavengingVisitor<TRANSFER_MARKS,
1844 LOGGING_AND_PROFILING_DISABLED>::GetTable());
1845 } else {
1846 scavenging_visitors_table_.CopyFrom(
1847 ScavengingVisitor<TRANSFER_MARKS,
1848 LOGGING_AND_PROFILING_ENABLED>::GetTable());
1849 }
1850
1851 if (incremental_marking()->IsCompacting()) {
1852 // When compacting forbid short-circuiting of cons-strings.
1853 // Scavenging code relies on the fact that new space object
1854 // can't be evacuated into evacuation candidate but
1855 // short-circuiting violates this assumption.
1856 scavenging_visitors_table_.Register(
1857 StaticVisitorBase::kVisitShortcutCandidate,
1858 scavenging_visitors_table_.GetVisitorById(
1859 StaticVisitorBase::kVisitConsString));
1860 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001861 }
1862}
Steve Blocka7e24c12009-10-30 11:49:00 +00001863
1864
1865void Heap::ScavengeObjectSlow(HeapObject** p, HeapObject* object) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001866 SLOW_ASSERT(HEAP->InFromSpace(object));
Steve Blocka7e24c12009-10-30 11:49:00 +00001867 MapWord first_word = object->map_word();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001868 SLOW_ASSERT(!first_word.IsForwardingAddress());
Ben Murdoch3bec4d22010-07-22 14:51:16 +01001869 Map* map = first_word.ToMap();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001870 map->GetHeap()->DoScavengeObject(map, p, object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001871}
1872
1873
John Reck59135872010-11-02 12:39:01 -07001874MaybeObject* Heap::AllocatePartialMap(InstanceType instance_type,
1875 int instance_size) {
1876 Object* result;
1877 { MaybeObject* maybe_result = AllocateRawMap();
1878 if (!maybe_result->ToObject(&result)) return maybe_result;
1879 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001880
1881 // Map::cast cannot be used due to uninitialized map field.
1882 reinterpret_cast<Map*>(result)->set_map(raw_unchecked_meta_map());
1883 reinterpret_cast<Map*>(result)->set_instance_type(instance_type);
1884 reinterpret_cast<Map*>(result)->set_instance_size(instance_size);
Steve Block44f0eee2011-05-26 01:26:41 +01001885 reinterpret_cast<Map*>(result)->set_visitor_id(
1886 StaticVisitorBase::GetVisitorId(instance_type, instance_size));
Steve Blocka7e24c12009-10-30 11:49:00 +00001887 reinterpret_cast<Map*>(result)->set_inobject_properties(0);
Leon Clarke4515c472010-02-03 11:58:03 +00001888 reinterpret_cast<Map*>(result)->set_pre_allocated_property_fields(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001889 reinterpret_cast<Map*>(result)->set_unused_property_fields(0);
Leon Clarke4515c472010-02-03 11:58:03 +00001890 reinterpret_cast<Map*>(result)->set_bit_field(0);
1891 reinterpret_cast<Map*>(result)->set_bit_field2(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001892 return result;
1893}
1894
1895
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001896MaybeObject* Heap::AllocateMap(InstanceType instance_type,
1897 int instance_size,
1898 ElementsKind elements_kind) {
John Reck59135872010-11-02 12:39:01 -07001899 Object* result;
1900 { MaybeObject* maybe_result = AllocateRawMap();
1901 if (!maybe_result->ToObject(&result)) return maybe_result;
1902 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001903
1904 Map* map = reinterpret_cast<Map*>(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001905 map->set_map_no_write_barrier(meta_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00001906 map->set_instance_type(instance_type);
Iain Merrick75681382010-08-19 15:07:18 +01001907 map->set_visitor_id(
1908 StaticVisitorBase::GetVisitorId(instance_type, instance_size));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001909 map->set_prototype(null_value(), SKIP_WRITE_BARRIER);
1910 map->set_constructor(null_value(), SKIP_WRITE_BARRIER);
Steve Blocka7e24c12009-10-30 11:49:00 +00001911 map->set_instance_size(instance_size);
1912 map->set_inobject_properties(0);
1913 map->set_pre_allocated_property_fields(0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001914 map->init_instance_descriptors();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001915 map->set_code_cache(empty_fixed_array(), SKIP_WRITE_BARRIER);
1916 map->set_prototype_transitions(empty_fixed_array(), SKIP_WRITE_BARRIER);
Steve Blocka7e24c12009-10-30 11:49:00 +00001917 map->set_unused_property_fields(0);
1918 map->set_bit_field(0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001919 map->set_bit_field2(1 << Map::kIsExtensible);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001920 map->set_elements_kind(elements_kind);
Leon Clarkee46be812010-01-19 14:06:41 +00001921
1922 // If the map object is aligned fill the padding area with Smi 0 objects.
1923 if (Map::kPadStart < Map::kSize) {
1924 memset(reinterpret_cast<byte*>(map) + Map::kPadStart - kHeapObjectTag,
1925 0,
1926 Map::kSize - Map::kPadStart);
1927 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001928 return map;
1929}
1930
1931
John Reck59135872010-11-02 12:39:01 -07001932MaybeObject* Heap::AllocateCodeCache() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001933 CodeCache* code_cache;
1934 { MaybeObject* maybe_code_cache = AllocateStruct(CODE_CACHE_TYPE);
1935 if (!maybe_code_cache->To(&code_cache)) return maybe_code_cache;
John Reck59135872010-11-02 12:39:01 -07001936 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001937 code_cache->set_default_cache(empty_fixed_array(), SKIP_WRITE_BARRIER);
1938 code_cache->set_normal_type_cache(undefined_value(), SKIP_WRITE_BARRIER);
Steve Block6ded16b2010-05-10 14:33:55 +01001939 return code_cache;
1940}
1941
1942
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001943MaybeObject* Heap::AllocatePolymorphicCodeCache() {
1944 return AllocateStruct(POLYMORPHIC_CODE_CACHE_TYPE);
1945}
1946
1947
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001948MaybeObject* Heap::AllocateAccessorPair() {
1949 AccessorPair* accessors;
1950 { MaybeObject* maybe_accessors = AllocateStruct(ACCESSOR_PAIR_TYPE);
1951 if (!maybe_accessors->To(&accessors)) return maybe_accessors;
1952 }
1953 accessors->set_getter(the_hole_value(), SKIP_WRITE_BARRIER);
1954 accessors->set_setter(the_hole_value(), SKIP_WRITE_BARRIER);
1955 return accessors;
1956}
1957
1958
1959MaybeObject* Heap::AllocateTypeFeedbackInfo() {
1960 TypeFeedbackInfo* info;
1961 { MaybeObject* maybe_info = AllocateStruct(TYPE_FEEDBACK_INFO_TYPE);
1962 if (!maybe_info->To(&info)) return maybe_info;
1963 }
1964 info->set_ic_total_count(0);
Ben Murdoch8f9999f2012-04-23 10:39:17 +01001965 info->set_ic_with_type_info_count(0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001966 info->set_type_feedback_cells(TypeFeedbackCells::cast(empty_fixed_array()),
1967 SKIP_WRITE_BARRIER);
1968 return info;
1969}
1970
1971
1972MaybeObject* Heap::AllocateAliasedArgumentsEntry(int aliased_context_slot) {
1973 AliasedArgumentsEntry* entry;
1974 { MaybeObject* maybe_entry = AllocateStruct(ALIASED_ARGUMENTS_ENTRY_TYPE);
1975 if (!maybe_entry->To(&entry)) return maybe_entry;
1976 }
1977 entry->set_aliased_context_slot(aliased_context_slot);
1978 return entry;
1979}
1980
1981
Steve Blocka7e24c12009-10-30 11:49:00 +00001982const Heap::StringTypeTable Heap::string_type_table[] = {
1983#define STRING_TYPE_ELEMENT(type, size, name, camel_name) \
1984 {type, size, k##camel_name##MapRootIndex},
1985 STRING_TYPE_LIST(STRING_TYPE_ELEMENT)
1986#undef STRING_TYPE_ELEMENT
1987};
1988
1989
1990const Heap::ConstantSymbolTable Heap::constant_symbol_table[] = {
1991#define CONSTANT_SYMBOL_ELEMENT(name, contents) \
1992 {contents, k##name##RootIndex},
1993 SYMBOL_LIST(CONSTANT_SYMBOL_ELEMENT)
1994#undef CONSTANT_SYMBOL_ELEMENT
1995};
1996
1997
1998const Heap::StructTable Heap::struct_table[] = {
1999#define STRUCT_TABLE_ELEMENT(NAME, Name, name) \
2000 { NAME##_TYPE, Name::kSize, k##Name##MapRootIndex },
2001 STRUCT_LIST(STRUCT_TABLE_ELEMENT)
2002#undef STRUCT_TABLE_ELEMENT
2003};
2004
2005
2006bool Heap::CreateInitialMaps() {
John Reck59135872010-11-02 12:39:01 -07002007 Object* obj;
2008 { MaybeObject* maybe_obj = AllocatePartialMap(MAP_TYPE, Map::kSize);
2009 if (!maybe_obj->ToObject(&obj)) return false;
2010 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002011 // Map::cast cannot be used due to uninitialized map field.
2012 Map* new_meta_map = reinterpret_cast<Map*>(obj);
2013 set_meta_map(new_meta_map);
2014 new_meta_map->set_map(new_meta_map);
2015
John Reck59135872010-11-02 12:39:01 -07002016 { MaybeObject* maybe_obj =
2017 AllocatePartialMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2018 if (!maybe_obj->ToObject(&obj)) return false;
2019 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002020 set_fixed_array_map(Map::cast(obj));
2021
John Reck59135872010-11-02 12:39:01 -07002022 { MaybeObject* maybe_obj = AllocatePartialMap(ODDBALL_TYPE, Oddball::kSize);
2023 if (!maybe_obj->ToObject(&obj)) return false;
2024 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002025 set_oddball_map(Map::cast(obj));
2026
Steve Block6ded16b2010-05-10 14:33:55 +01002027 // Allocate the empty array.
John Reck59135872010-11-02 12:39:01 -07002028 { MaybeObject* maybe_obj = AllocateEmptyFixedArray();
2029 if (!maybe_obj->ToObject(&obj)) return false;
2030 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002031 set_empty_fixed_array(FixedArray::cast(obj));
2032
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002033 { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_POINTER_SPACE);
John Reck59135872010-11-02 12:39:01 -07002034 if (!maybe_obj->ToObject(&obj)) return false;
2035 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002036 set_null_value(Oddball::cast(obj));
Steve Block44f0eee2011-05-26 01:26:41 +01002037 Oddball::cast(obj)->set_kind(Oddball::kNull);
Steve Blocka7e24c12009-10-30 11:49:00 +00002038
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002039 { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_POINTER_SPACE);
2040 if (!maybe_obj->ToObject(&obj)) return false;
2041 }
2042 set_undefined_value(Oddball::cast(obj));
2043 Oddball::cast(obj)->set_kind(Oddball::kUndefined);
2044 ASSERT(!InNewSpace(undefined_value()));
2045
Steve Blocka7e24c12009-10-30 11:49:00 +00002046 // Allocate the empty descriptor array.
John Reck59135872010-11-02 12:39:01 -07002047 { MaybeObject* maybe_obj = AllocateEmptyFixedArray();
2048 if (!maybe_obj->ToObject(&obj)) return false;
2049 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002050 set_empty_descriptor_array(DescriptorArray::cast(obj));
2051
2052 // Fix the instance_descriptors for the existing maps.
Ben Murdoch257744e2011-11-30 15:57:28 +00002053 meta_map()->init_instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00002054 meta_map()->set_code_cache(empty_fixed_array());
Steve Block053d10c2011-06-13 19:13:29 +01002055 meta_map()->set_prototype_transitions(empty_fixed_array());
Steve Blocka7e24c12009-10-30 11:49:00 +00002056
Ben Murdoch257744e2011-11-30 15:57:28 +00002057 fixed_array_map()->init_instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00002058 fixed_array_map()->set_code_cache(empty_fixed_array());
Steve Block053d10c2011-06-13 19:13:29 +01002059 fixed_array_map()->set_prototype_transitions(empty_fixed_array());
Steve Blocka7e24c12009-10-30 11:49:00 +00002060
Ben Murdoch257744e2011-11-30 15:57:28 +00002061 oddball_map()->init_instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00002062 oddball_map()->set_code_cache(empty_fixed_array());
Steve Block053d10c2011-06-13 19:13:29 +01002063 oddball_map()->set_prototype_transitions(empty_fixed_array());
Steve Blocka7e24c12009-10-30 11:49:00 +00002064
2065 // Fix prototype object for existing maps.
2066 meta_map()->set_prototype(null_value());
2067 meta_map()->set_constructor(null_value());
2068
2069 fixed_array_map()->set_prototype(null_value());
2070 fixed_array_map()->set_constructor(null_value());
2071
2072 oddball_map()->set_prototype(null_value());
2073 oddball_map()->set_constructor(null_value());
2074
John Reck59135872010-11-02 12:39:01 -07002075 { MaybeObject* maybe_obj =
2076 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2077 if (!maybe_obj->ToObject(&obj)) return false;
2078 }
Iain Merrick75681382010-08-19 15:07:18 +01002079 set_fixed_cow_array_map(Map::cast(obj));
2080 ASSERT(fixed_array_map() != fixed_cow_array_map());
2081
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002082 { MaybeObject* maybe_obj =
2083 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2084 if (!maybe_obj->ToObject(&obj)) return false;
2085 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002086 set_scope_info_map(Map::cast(obj));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002087
John Reck59135872010-11-02 12:39:01 -07002088 { MaybeObject* maybe_obj = AllocateMap(HEAP_NUMBER_TYPE, HeapNumber::kSize);
2089 if (!maybe_obj->ToObject(&obj)) return false;
2090 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 set_heap_number_map(Map::cast(obj));
2092
Ben Murdoch257744e2011-11-30 15:57:28 +00002093 { MaybeObject* maybe_obj = AllocateMap(FOREIGN_TYPE, Foreign::kSize);
John Reck59135872010-11-02 12:39:01 -07002094 if (!maybe_obj->ToObject(&obj)) return false;
2095 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002096 set_foreign_map(Map::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002097
2098 for (unsigned i = 0; i < ARRAY_SIZE(string_type_table); i++) {
2099 const StringTypeTable& entry = string_type_table[i];
John Reck59135872010-11-02 12:39:01 -07002100 { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size);
2101 if (!maybe_obj->ToObject(&obj)) return false;
2102 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002103 roots_[entry.index] = Map::cast(obj);
2104 }
2105
John Reck59135872010-11-02 12:39:01 -07002106 { MaybeObject* maybe_obj = AllocateMap(STRING_TYPE, kVariableSizeSentinel);
2107 if (!maybe_obj->ToObject(&obj)) return false;
2108 }
Steve Blockd0582a62009-12-15 09:54:21 +00002109 set_undetectable_string_map(Map::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002110 Map::cast(obj)->set_is_undetectable();
2111
John Reck59135872010-11-02 12:39:01 -07002112 { MaybeObject* maybe_obj =
2113 AllocateMap(ASCII_STRING_TYPE, kVariableSizeSentinel);
2114 if (!maybe_obj->ToObject(&obj)) return false;
2115 }
Steve Blockd0582a62009-12-15 09:54:21 +00002116 set_undetectable_ascii_string_map(Map::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002117 Map::cast(obj)->set_is_undetectable();
2118
John Reck59135872010-11-02 12:39:01 -07002119 { MaybeObject* maybe_obj =
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002120 AllocateMap(FIXED_DOUBLE_ARRAY_TYPE, kVariableSizeSentinel);
2121 if (!maybe_obj->ToObject(&obj)) return false;
2122 }
2123 set_fixed_double_array_map(Map::cast(obj));
2124
2125 { MaybeObject* maybe_obj =
John Reck59135872010-11-02 12:39:01 -07002126 AllocateMap(BYTE_ARRAY_TYPE, kVariableSizeSentinel);
2127 if (!maybe_obj->ToObject(&obj)) return false;
2128 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002129 set_byte_array_map(Map::cast(obj));
2130
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002131 { MaybeObject* maybe_obj =
2132 AllocateMap(FREE_SPACE_TYPE, kVariableSizeSentinel);
2133 if (!maybe_obj->ToObject(&obj)) return false;
2134 }
2135 set_free_space_map(Map::cast(obj));
2136
Ben Murdochb0fe1622011-05-05 13:52:32 +01002137 { MaybeObject* maybe_obj = AllocateByteArray(0, TENURED);
2138 if (!maybe_obj->ToObject(&obj)) return false;
2139 }
2140 set_empty_byte_array(ByteArray::cast(obj));
2141
John Reck59135872010-11-02 12:39:01 -07002142 { MaybeObject* maybe_obj =
Steve Block44f0eee2011-05-26 01:26:41 +01002143 AllocateMap(EXTERNAL_PIXEL_ARRAY_TYPE, ExternalArray::kAlignedSize);
John Reck59135872010-11-02 12:39:01 -07002144 if (!maybe_obj->ToObject(&obj)) return false;
2145 }
Steve Block44f0eee2011-05-26 01:26:41 +01002146 set_external_pixel_array_map(Map::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002147
John Reck59135872010-11-02 12:39:01 -07002148 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_BYTE_ARRAY_TYPE,
2149 ExternalArray::kAlignedSize);
2150 if (!maybe_obj->ToObject(&obj)) return false;
2151 }
Steve Block3ce2e202009-11-05 08:53:23 +00002152 set_external_byte_array_map(Map::cast(obj));
2153
John Reck59135872010-11-02 12:39:01 -07002154 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
2155 ExternalArray::kAlignedSize);
2156 if (!maybe_obj->ToObject(&obj)) return false;
2157 }
Steve Block3ce2e202009-11-05 08:53:23 +00002158 set_external_unsigned_byte_array_map(Map::cast(obj));
2159
John Reck59135872010-11-02 12:39:01 -07002160 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_SHORT_ARRAY_TYPE,
2161 ExternalArray::kAlignedSize);
2162 if (!maybe_obj->ToObject(&obj)) return false;
2163 }
Steve Block3ce2e202009-11-05 08:53:23 +00002164 set_external_short_array_map(Map::cast(obj));
2165
John Reck59135872010-11-02 12:39:01 -07002166 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
2167 ExternalArray::kAlignedSize);
2168 if (!maybe_obj->ToObject(&obj)) return false;
2169 }
Steve Block3ce2e202009-11-05 08:53:23 +00002170 set_external_unsigned_short_array_map(Map::cast(obj));
2171
John Reck59135872010-11-02 12:39:01 -07002172 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_INT_ARRAY_TYPE,
2173 ExternalArray::kAlignedSize);
2174 if (!maybe_obj->ToObject(&obj)) return false;
2175 }
Steve Block3ce2e202009-11-05 08:53:23 +00002176 set_external_int_array_map(Map::cast(obj));
2177
John Reck59135872010-11-02 12:39:01 -07002178 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
2179 ExternalArray::kAlignedSize);
2180 if (!maybe_obj->ToObject(&obj)) return false;
2181 }
Steve Block3ce2e202009-11-05 08:53:23 +00002182 set_external_unsigned_int_array_map(Map::cast(obj));
2183
John Reck59135872010-11-02 12:39:01 -07002184 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_FLOAT_ARRAY_TYPE,
2185 ExternalArray::kAlignedSize);
2186 if (!maybe_obj->ToObject(&obj)) return false;
2187 }
Steve Block3ce2e202009-11-05 08:53:23 +00002188 set_external_float_array_map(Map::cast(obj));
2189
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002190 { MaybeObject* maybe_obj =
2191 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2192 if (!maybe_obj->ToObject(&obj)) return false;
2193 }
2194 set_non_strict_arguments_elements_map(Map::cast(obj));
2195
Ben Murdoch257744e2011-11-30 15:57:28 +00002196 { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_DOUBLE_ARRAY_TYPE,
2197 ExternalArray::kAlignedSize);
2198 if (!maybe_obj->ToObject(&obj)) return false;
2199 }
2200 set_external_double_array_map(Map::cast(obj));
2201
John Reck59135872010-11-02 12:39:01 -07002202 { MaybeObject* maybe_obj = AllocateMap(CODE_TYPE, kVariableSizeSentinel);
2203 if (!maybe_obj->ToObject(&obj)) return false;
2204 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002205 set_code_map(Map::cast(obj));
2206
John Reck59135872010-11-02 12:39:01 -07002207 { MaybeObject* maybe_obj = AllocateMap(JS_GLOBAL_PROPERTY_CELL_TYPE,
2208 JSGlobalPropertyCell::kSize);
2209 if (!maybe_obj->ToObject(&obj)) return false;
2210 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002211 set_global_property_cell_map(Map::cast(obj));
2212
John Reck59135872010-11-02 12:39:01 -07002213 { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, kPointerSize);
2214 if (!maybe_obj->ToObject(&obj)) return false;
2215 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002216 set_one_pointer_filler_map(Map::cast(obj));
2217
John Reck59135872010-11-02 12:39:01 -07002218 { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, 2 * kPointerSize);
2219 if (!maybe_obj->ToObject(&obj)) return false;
2220 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002221 set_two_pointer_filler_map(Map::cast(obj));
2222
2223 for (unsigned i = 0; i < ARRAY_SIZE(struct_table); i++) {
2224 const StructTable& entry = struct_table[i];
John Reck59135872010-11-02 12:39:01 -07002225 { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size);
2226 if (!maybe_obj->ToObject(&obj)) return false;
2227 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002228 roots_[entry.index] = Map::cast(obj);
2229 }
2230
John Reck59135872010-11-02 12:39:01 -07002231 { MaybeObject* maybe_obj =
2232 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2233 if (!maybe_obj->ToObject(&obj)) return false;
2234 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002235 set_hash_table_map(Map::cast(obj));
2236
John Reck59135872010-11-02 12:39:01 -07002237 { MaybeObject* maybe_obj =
2238 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2239 if (!maybe_obj->ToObject(&obj)) return false;
2240 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002241 set_function_context_map(Map::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002242
John Reck59135872010-11-02 12:39:01 -07002243 { MaybeObject* maybe_obj =
2244 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2245 if (!maybe_obj->ToObject(&obj)) return false;
2246 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002247 set_catch_context_map(Map::cast(obj));
2248
John Reck59135872010-11-02 12:39:01 -07002249 { MaybeObject* maybe_obj =
2250 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2251 if (!maybe_obj->ToObject(&obj)) return false;
2252 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002253 set_with_context_map(Map::cast(obj));
2254
2255 { MaybeObject* maybe_obj =
2256 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2257 if (!maybe_obj->ToObject(&obj)) return false;
2258 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002259 set_block_context_map(Map::cast(obj));
2260
2261 { MaybeObject* maybe_obj =
2262 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2263 if (!maybe_obj->ToObject(&obj)) return false;
2264 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002265 set_module_context_map(Map::cast(obj));
2266
2267 { MaybeObject* maybe_obj =
2268 AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel);
2269 if (!maybe_obj->ToObject(&obj)) return false;
2270 }
Ben Murdochf87a2032010-10-22 12:50:53 +01002271 Map* global_context_map = Map::cast(obj);
2272 global_context_map->set_visitor_id(StaticVisitorBase::kVisitGlobalContext);
2273 set_global_context_map(global_context_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002274
John Reck59135872010-11-02 12:39:01 -07002275 { MaybeObject* maybe_obj = AllocateMap(SHARED_FUNCTION_INFO_TYPE,
2276 SharedFunctionInfo::kAlignedSize);
2277 if (!maybe_obj->ToObject(&obj)) return false;
2278 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002279 set_shared_function_info_map(Map::cast(obj));
2280
Steve Block1e0659c2011-05-24 12:43:12 +01002281 { MaybeObject* maybe_obj = AllocateMap(JS_MESSAGE_OBJECT_TYPE,
2282 JSMessageObject::kSize);
2283 if (!maybe_obj->ToObject(&obj)) return false;
2284 }
2285 set_message_object_map(Map::cast(obj));
2286
Steve Block44f0eee2011-05-26 01:26:41 +01002287 ASSERT(!InNewSpace(empty_fixed_array()));
Steve Blocka7e24c12009-10-30 11:49:00 +00002288 return true;
2289}
2290
2291
John Reck59135872010-11-02 12:39:01 -07002292MaybeObject* Heap::AllocateHeapNumber(double value, PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002293 // Statically ensure that it is safe to allocate heap numbers in paged
2294 // spaces.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002295 STATIC_ASSERT(HeapNumber::kSize <= Page::kNonCodeObjectAreaSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002296 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
2297
John Reck59135872010-11-02 12:39:01 -07002298 Object* result;
2299 { MaybeObject* maybe_result =
2300 AllocateRaw(HeapNumber::kSize, space, OLD_DATA_SPACE);
2301 if (!maybe_result->ToObject(&result)) return maybe_result;
2302 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002303
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002304 HeapObject::cast(result)->set_map_no_write_barrier(heap_number_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00002305 HeapNumber::cast(result)->set_value(value);
2306 return result;
2307}
2308
2309
John Reck59135872010-11-02 12:39:01 -07002310MaybeObject* Heap::AllocateHeapNumber(double value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002311 // Use general version, if we're forced to always allocate.
2312 if (always_allocate()) return AllocateHeapNumber(value, TENURED);
2313
2314 // This version of AllocateHeapNumber is optimized for
2315 // allocation in new space.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002316 STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxNonCodeHeapObjectSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002317 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
John Reck59135872010-11-02 12:39:01 -07002318 Object* result;
2319 { MaybeObject* maybe_result = new_space_.AllocateRaw(HeapNumber::kSize);
2320 if (!maybe_result->ToObject(&result)) return maybe_result;
2321 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002322 HeapObject::cast(result)->set_map_no_write_barrier(heap_number_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00002323 HeapNumber::cast(result)->set_value(value);
2324 return result;
2325}
2326
2327
John Reck59135872010-11-02 12:39:01 -07002328MaybeObject* Heap::AllocateJSGlobalPropertyCell(Object* value) {
2329 Object* result;
2330 { MaybeObject* maybe_result = AllocateRawCell();
2331 if (!maybe_result->ToObject(&result)) return maybe_result;
2332 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002333 HeapObject::cast(result)->set_map_no_write_barrier(
2334 global_property_cell_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00002335 JSGlobalPropertyCell::cast(result)->set_value(value);
2336 return result;
2337}
2338
2339
John Reck59135872010-11-02 12:39:01 -07002340MaybeObject* Heap::CreateOddball(const char* to_string,
Steve Block44f0eee2011-05-26 01:26:41 +01002341 Object* to_number,
2342 byte kind) {
John Reck59135872010-11-02 12:39:01 -07002343 Object* result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002344 { MaybeObject* maybe_result = Allocate(oddball_map(), OLD_POINTER_SPACE);
John Reck59135872010-11-02 12:39:01 -07002345 if (!maybe_result->ToObject(&result)) return maybe_result;
2346 }
Steve Block44f0eee2011-05-26 01:26:41 +01002347 return Oddball::cast(result)->Initialize(to_string, to_number, kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00002348}
2349
2350
2351bool Heap::CreateApiObjects() {
2352 Object* obj;
2353
John Reck59135872010-11-02 12:39:01 -07002354 { MaybeObject* maybe_obj = AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
2355 if (!maybe_obj->ToObject(&obj)) return false;
2356 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002357 // Don't use Smi-only elements optimizations for objects with the neander
2358 // map. There are too many cases where element values are set directly with a
2359 // bottleneck to trap the Smi-only -> fast elements transition, and there
2360 // appears to be no benefit for optimize this case.
2361 Map* new_neander_map = Map::cast(obj);
2362 new_neander_map->set_elements_kind(FAST_ELEMENTS);
2363 set_neander_map(new_neander_map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002364
Steve Block44f0eee2011-05-26 01:26:41 +01002365 { MaybeObject* maybe_obj = AllocateJSObjectFromMap(neander_map());
John Reck59135872010-11-02 12:39:01 -07002366 if (!maybe_obj->ToObject(&obj)) return false;
2367 }
2368 Object* elements;
2369 { MaybeObject* maybe_elements = AllocateFixedArray(2);
2370 if (!maybe_elements->ToObject(&elements)) return false;
2371 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002372 FixedArray::cast(elements)->set(0, Smi::FromInt(0));
2373 JSObject::cast(obj)->set_elements(FixedArray::cast(elements));
2374 set_message_listeners(JSObject::cast(obj));
2375
2376 return true;
2377}
2378
2379
Steve Blocka7e24c12009-10-30 11:49:00 +00002380void Heap::CreateJSEntryStub() {
2381 JSEntryStub stub;
2382 set_js_entry_code(*stub.GetCode());
2383}
2384
2385
2386void Heap::CreateJSConstructEntryStub() {
2387 JSConstructEntryStub stub;
2388 set_js_construct_entry_code(*stub.GetCode());
2389}
2390
2391
2392void Heap::CreateFixedStubs() {
2393 // Here we create roots for fixed stubs. They are needed at GC
2394 // for cooking and uncooking (check out frames.cc).
2395 // The eliminates the need for doing dictionary lookup in the
2396 // stub cache for these stubs.
2397 HandleScope scope;
2398 // gcc-4.4 has problem generating correct code of following snippet:
Steve Block44f0eee2011-05-26 01:26:41 +01002399 // { JSEntryStub stub;
2400 // js_entry_code_ = *stub.GetCode();
Steve Blocka7e24c12009-10-30 11:49:00 +00002401 // }
Steve Block44f0eee2011-05-26 01:26:41 +01002402 // { JSConstructEntryStub stub;
2403 // js_construct_entry_code_ = *stub.GetCode();
Steve Blocka7e24c12009-10-30 11:49:00 +00002404 // }
2405 // To workaround the problem, make separate functions without inlining.
Steve Blocka7e24c12009-10-30 11:49:00 +00002406 Heap::CreateJSEntryStub();
2407 Heap::CreateJSConstructEntryStub();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002408
2409 // Create stubs that should be there, so we don't unexpectedly have to
2410 // create them if we need them during the creation of another stub.
2411 // Stub creation mixes raw pointers and handles in an unsafe manner so
2412 // we cannot create stubs while we are creating stubs.
2413 CodeStub::GenerateStubsAheadOfTime();
Steve Blocka7e24c12009-10-30 11:49:00 +00002414}
2415
2416
2417bool Heap::CreateInitialObjects() {
2418 Object* obj;
2419
2420 // The -0 value must be set before NumberFromDouble works.
John Reck59135872010-11-02 12:39:01 -07002421 { MaybeObject* maybe_obj = AllocateHeapNumber(-0.0, TENURED);
2422 if (!maybe_obj->ToObject(&obj)) return false;
2423 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002424 set_minus_zero_value(HeapNumber::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002425 ASSERT(signbit(minus_zero_value()->Number()) != 0);
2426
John Reck59135872010-11-02 12:39:01 -07002427 { MaybeObject* maybe_obj = AllocateHeapNumber(OS::nan_value(), TENURED);
2428 if (!maybe_obj->ToObject(&obj)) return false;
2429 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002430 set_nan_value(HeapNumber::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002431
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002432 { MaybeObject* maybe_obj = AllocateHeapNumber(V8_INFINITY, TENURED);
John Reck59135872010-11-02 12:39:01 -07002433 if (!maybe_obj->ToObject(&obj)) return false;
2434 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002435 set_infinity_value(HeapNumber::cast(obj));
2436
2437 // The hole has not been created yet, but we want to put something
2438 // predictable in the gaps in the symbol table, so lets make that Smi zero.
2439 set_the_hole_value(reinterpret_cast<Oddball*>(Smi::FromInt(0)));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002440
Steve Blocka7e24c12009-10-30 11:49:00 +00002441 // Allocate initial symbol table.
John Reck59135872010-11-02 12:39:01 -07002442 { MaybeObject* maybe_obj = SymbolTable::Allocate(kInitialSymbolTableSize);
2443 if (!maybe_obj->ToObject(&obj)) return false;
2444 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002445 // Don't use set_symbol_table() due to asserts.
2446 roots_[kSymbolTableRootIndex] = obj;
2447
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002448 // Finish initializing oddballs after creating symboltable.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002449 { MaybeObject* maybe_obj =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002450 undefined_value()->Initialize("undefined",
2451 nan_value(),
2452 Oddball::kUndefined);
2453 if (!maybe_obj->ToObject(&obj)) return false;
2454 }
2455
2456 // Initialize the null_value.
2457 { MaybeObject* maybe_obj =
2458 null_value()->Initialize("null", Smi::FromInt(0), Oddball::kNull);
John Reck59135872010-11-02 12:39:01 -07002459 if (!maybe_obj->ToObject(&obj)) return false;
2460 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002461
Steve Block44f0eee2011-05-26 01:26:41 +01002462 { MaybeObject* maybe_obj = CreateOddball("true",
2463 Smi::FromInt(1),
2464 Oddball::kTrue);
John Reck59135872010-11-02 12:39:01 -07002465 if (!maybe_obj->ToObject(&obj)) return false;
2466 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002467 set_true_value(Oddball::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002468
Steve Block44f0eee2011-05-26 01:26:41 +01002469 { MaybeObject* maybe_obj = CreateOddball("false",
2470 Smi::FromInt(0),
2471 Oddball::kFalse);
John Reck59135872010-11-02 12:39:01 -07002472 if (!maybe_obj->ToObject(&obj)) return false;
2473 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002474 set_false_value(Oddball::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002475
Steve Block44f0eee2011-05-26 01:26:41 +01002476 { MaybeObject* maybe_obj = CreateOddball("hole",
2477 Smi::FromInt(-1),
2478 Oddball::kTheHole);
John Reck59135872010-11-02 12:39:01 -07002479 if (!maybe_obj->ToObject(&obj)) return false;
2480 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002481 set_the_hole_value(Oddball::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002482
Ben Murdoch086aeea2011-05-13 15:57:08 +01002483 { MaybeObject* maybe_obj = CreateOddball("arguments_marker",
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002484 Smi::FromInt(-4),
Steve Block44f0eee2011-05-26 01:26:41 +01002485 Oddball::kArgumentMarker);
Ben Murdoch086aeea2011-05-13 15:57:08 +01002486 if (!maybe_obj->ToObject(&obj)) return false;
2487 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002488 set_arguments_marker(Oddball::cast(obj));
Ben Murdoch086aeea2011-05-13 15:57:08 +01002489
Steve Block44f0eee2011-05-26 01:26:41 +01002490 { MaybeObject* maybe_obj = CreateOddball("no_interceptor_result_sentinel",
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002491 Smi::FromInt(-2),
Steve Block44f0eee2011-05-26 01:26:41 +01002492 Oddball::kOther);
John Reck59135872010-11-02 12:39:01 -07002493 if (!maybe_obj->ToObject(&obj)) return false;
2494 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002495 set_no_interceptor_result_sentinel(obj);
2496
Steve Block44f0eee2011-05-26 01:26:41 +01002497 { MaybeObject* maybe_obj = CreateOddball("termination_exception",
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002498 Smi::FromInt(-3),
Steve Block44f0eee2011-05-26 01:26:41 +01002499 Oddball::kOther);
John Reck59135872010-11-02 12:39:01 -07002500 if (!maybe_obj->ToObject(&obj)) return false;
2501 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002502 set_termination_exception(obj);
2503
2504 // Allocate the empty string.
John Reck59135872010-11-02 12:39:01 -07002505 { MaybeObject* maybe_obj = AllocateRawAsciiString(0, TENURED);
2506 if (!maybe_obj->ToObject(&obj)) return false;
2507 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002508 set_empty_string(String::cast(obj));
2509
2510 for (unsigned i = 0; i < ARRAY_SIZE(constant_symbol_table); i++) {
John Reck59135872010-11-02 12:39:01 -07002511 { MaybeObject* maybe_obj =
2512 LookupAsciiSymbol(constant_symbol_table[i].contents);
2513 if (!maybe_obj->ToObject(&obj)) return false;
2514 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002515 roots_[constant_symbol_table[i].index] = String::cast(obj);
2516 }
2517
2518 // Allocate the hidden symbol which is used to identify the hidden properties
2519 // in JSObjects. The hash code has a special value so that it will not match
2520 // the empty string when searching for the property. It cannot be part of the
2521 // loop above because it needs to be allocated manually with the special
2522 // hash code in place. The hash code for the hidden_symbol is zero to ensure
2523 // that it will always be at the first entry in property descriptors.
John Reck59135872010-11-02 12:39:01 -07002524 { MaybeObject* maybe_obj =
2525 AllocateSymbol(CStrVector(""), 0, String::kZeroHash);
2526 if (!maybe_obj->ToObject(&obj)) return false;
2527 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002528 hidden_symbol_ = String::cast(obj);
2529
Ben Murdoch257744e2011-11-30 15:57:28 +00002530 // Allocate the foreign for __proto__.
John Reck59135872010-11-02 12:39:01 -07002531 { MaybeObject* maybe_obj =
Ben Murdoch257744e2011-11-30 15:57:28 +00002532 AllocateForeign((Address) &Accessors::ObjectPrototype);
John Reck59135872010-11-02 12:39:01 -07002533 if (!maybe_obj->ToObject(&obj)) return false;
2534 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002535 set_prototype_accessors(Foreign::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002536
2537 // Allocate the code_stubs dictionary. The initial size is set to avoid
2538 // expanding the dictionary during bootstrapping.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002539 { MaybeObject* maybe_obj = UnseededNumberDictionary::Allocate(128);
John Reck59135872010-11-02 12:39:01 -07002540 if (!maybe_obj->ToObject(&obj)) return false;
2541 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00002542 set_code_stubs(UnseededNumberDictionary::cast(obj));
2543
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002544
Steve Blocka7e24c12009-10-30 11:49:00 +00002545 // Allocate the non_monomorphic_cache used in stub-cache.cc. The initial size
2546 // is set to avoid expanding the dictionary during bootstrapping.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002547 { MaybeObject* maybe_obj = UnseededNumberDictionary::Allocate(64);
John Reck59135872010-11-02 12:39:01 -07002548 if (!maybe_obj->ToObject(&obj)) return false;
2549 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00002550 set_non_monomorphic_cache(UnseededNumberDictionary::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002551
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002552 { MaybeObject* maybe_obj = AllocatePolymorphicCodeCache();
2553 if (!maybe_obj->ToObject(&obj)) return false;
2554 }
2555 set_polymorphic_code_cache(PolymorphicCodeCache::cast(obj));
2556
Kristian Monsen25f61362010-05-21 11:50:48 +01002557 set_instanceof_cache_function(Smi::FromInt(0));
2558 set_instanceof_cache_map(Smi::FromInt(0));
2559 set_instanceof_cache_answer(Smi::FromInt(0));
2560
Steve Blocka7e24c12009-10-30 11:49:00 +00002561 CreateFixedStubs();
2562
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002563 // Allocate the dictionary of intrinsic function names.
John Reck59135872010-11-02 12:39:01 -07002564 { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions);
2565 if (!maybe_obj->ToObject(&obj)) return false;
2566 }
Steve Block44f0eee2011-05-26 01:26:41 +01002567 { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(this,
2568 obj);
John Reck59135872010-11-02 12:39:01 -07002569 if (!maybe_obj->ToObject(&obj)) return false;
2570 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002571 set_intrinsic_function_names(StringDictionary::cast(obj));
2572
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002573 { MaybeObject* maybe_obj = AllocateInitialNumberStringCache();
2574 if (!maybe_obj->ToObject(&obj)) return false;
2575 }
2576 set_number_string_cache(FixedArray::cast(obj));
Steve Blocka7e24c12009-10-30 11:49:00 +00002577
Steve Block6ded16b2010-05-10 14:33:55 +01002578 // Allocate cache for single character ASCII strings.
John Reck59135872010-11-02 12:39:01 -07002579 { MaybeObject* maybe_obj =
2580 AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED);
2581 if (!maybe_obj->ToObject(&obj)) return false;
2582 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002583 set_single_character_string_cache(FixedArray::cast(obj));
2584
Ben Murdoch589d6972011-11-30 16:04:58 +00002585 // Allocate cache for string split.
2586 { MaybeObject* maybe_obj =
2587 AllocateFixedArray(StringSplitCache::kStringSplitCacheSize, TENURED);
2588 if (!maybe_obj->ToObject(&obj)) return false;
2589 }
2590 set_string_split_cache(FixedArray::cast(obj));
2591
Steve Blocka7e24c12009-10-30 11:49:00 +00002592 // Allocate cache for external strings pointing to native source code.
John Reck59135872010-11-02 12:39:01 -07002593 { MaybeObject* maybe_obj = AllocateFixedArray(Natives::GetBuiltinsCount());
2594 if (!maybe_obj->ToObject(&obj)) return false;
2595 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002596 set_natives_source_cache(FixedArray::cast(obj));
2597
Steve Block44f0eee2011-05-26 01:26:41 +01002598 // Handling of script id generation is in FACTORY->NewScript.
Steve Blocka7e24c12009-10-30 11:49:00 +00002599 set_last_script_id(undefined_value());
2600
2601 // Initialize keyed lookup cache.
Steve Block44f0eee2011-05-26 01:26:41 +01002602 isolate_->keyed_lookup_cache()->Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +00002603
2604 // Initialize context slot cache.
Steve Block44f0eee2011-05-26 01:26:41 +01002605 isolate_->context_slot_cache()->Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +00002606
2607 // Initialize descriptor cache.
Steve Block44f0eee2011-05-26 01:26:41 +01002608 isolate_->descriptor_lookup_cache()->Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +00002609
2610 // Initialize compilation cache.
Steve Block44f0eee2011-05-26 01:26:41 +01002611 isolate_->compilation_cache()->Clear();
Steve Blocka7e24c12009-10-30 11:49:00 +00002612
2613 return true;
2614}
2615
2616
Ben Murdoch589d6972011-11-30 16:04:58 +00002617Object* StringSplitCache::Lookup(
2618 FixedArray* cache, String* string, String* pattern) {
2619 if (!string->IsSymbol() || !pattern->IsSymbol()) return Smi::FromInt(0);
2620 uint32_t hash = string->Hash();
2621 uint32_t index = ((hash & (kStringSplitCacheSize - 1)) &
2622 ~(kArrayEntriesPerCacheEntry - 1));
2623 if (cache->get(index + kStringOffset) == string &&
2624 cache->get(index + kPatternOffset) == pattern) {
2625 return cache->get(index + kArrayOffset);
2626 }
2627 index = ((index + kArrayEntriesPerCacheEntry) & (kStringSplitCacheSize - 1));
2628 if (cache->get(index + kStringOffset) == string &&
2629 cache->get(index + kPatternOffset) == pattern) {
2630 return cache->get(index + kArrayOffset);
2631 }
2632 return Smi::FromInt(0);
2633}
2634
2635
2636void StringSplitCache::Enter(Heap* heap,
2637 FixedArray* cache,
2638 String* string,
2639 String* pattern,
2640 FixedArray* array) {
2641 if (!string->IsSymbol() || !pattern->IsSymbol()) return;
2642 uint32_t hash = string->Hash();
2643 uint32_t index = ((hash & (kStringSplitCacheSize - 1)) &
2644 ~(kArrayEntriesPerCacheEntry - 1));
2645 if (cache->get(index + kStringOffset) == Smi::FromInt(0)) {
2646 cache->set(index + kStringOffset, string);
2647 cache->set(index + kPatternOffset, pattern);
2648 cache->set(index + kArrayOffset, array);
2649 } else {
2650 uint32_t index2 =
2651 ((index + kArrayEntriesPerCacheEntry) & (kStringSplitCacheSize - 1));
2652 if (cache->get(index2 + kStringOffset) == Smi::FromInt(0)) {
2653 cache->set(index2 + kStringOffset, string);
2654 cache->set(index2 + kPatternOffset, pattern);
2655 cache->set(index2 + kArrayOffset, array);
2656 } else {
2657 cache->set(index2 + kStringOffset, Smi::FromInt(0));
2658 cache->set(index2 + kPatternOffset, Smi::FromInt(0));
2659 cache->set(index2 + kArrayOffset, Smi::FromInt(0));
2660 cache->set(index + kStringOffset, string);
2661 cache->set(index + kPatternOffset, pattern);
2662 cache->set(index + kArrayOffset, array);
2663 }
2664 }
2665 if (array->length() < 100) { // Limit how many new symbols we want to make.
2666 for (int i = 0; i < array->length(); i++) {
2667 String* str = String::cast(array->get(i));
2668 Object* symbol;
2669 MaybeObject* maybe_symbol = heap->LookupSymbol(str);
2670 if (maybe_symbol->ToObject(&symbol)) {
2671 array->set(i, symbol);
2672 }
2673 }
2674 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002675 array->set_map_no_write_barrier(heap->fixed_cow_array_map());
Ben Murdoch589d6972011-11-30 16:04:58 +00002676}
2677
2678
2679void StringSplitCache::Clear(FixedArray* cache) {
2680 for (int i = 0; i < kStringSplitCacheSize; i++) {
2681 cache->set(i, Smi::FromInt(0));
2682 }
2683}
2684
2685
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002686MaybeObject* Heap::AllocateInitialNumberStringCache() {
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002687 MaybeObject* maybe_obj =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002688 AllocateFixedArray(kInitialNumberStringCacheSize * 2, TENURED);
Ben Murdoch85b71792012-04-11 18:30:58 +01002689 return maybe_obj;
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002690}
2691
2692
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002693int Heap::FullSizeNumberStringCacheLength() {
2694 // Compute the size of the number string cache based on the max newspace size.
2695 // The number string cache has a minimum size based on twice the initial cache
2696 // size to ensure that it is bigger after being made 'full size'.
2697 int number_string_cache_size = max_semispace_size_ / 512;
2698 number_string_cache_size = Max(kInitialNumberStringCacheSize * 2,
2699 Min(0x4000, number_string_cache_size));
2700 // There is a string and a number per entry so the length is twice the number
2701 // of entries.
2702 return number_string_cache_size * 2;
2703}
2704
2705
2706void Heap::AllocateFullSizeNumberStringCache() {
2707 // The idea is to have a small number string cache in the snapshot to keep
2708 // boot-time memory usage down. If we expand the number string cache already
2709 // while creating the snapshot then that didn't work out.
2710 ASSERT(!Serializer::enabled());
2711 MaybeObject* maybe_obj =
2712 AllocateFixedArray(FullSizeNumberStringCacheLength(), TENURED);
2713 Object* new_cache;
2714 if (maybe_obj->ToObject(&new_cache)) {
2715 // We don't bother to repopulate the cache with entries from the old cache.
2716 // It will be repopulated soon enough with new strings.
2717 set_number_string_cache(FixedArray::cast(new_cache));
2718 }
2719 // If allocation fails then we just return without doing anything. It is only
2720 // a cache, so best effort is OK here.
2721}
2722
2723
Leon Clarkee46be812010-01-19 14:06:41 +00002724void Heap::FlushNumberStringCache() {
2725 // Flush the number to string cache.
2726 int len = number_string_cache()->length();
2727 for (int i = 0; i < len; i++) {
Steve Block44f0eee2011-05-26 01:26:41 +01002728 number_string_cache()->set_undefined(this, i);
Leon Clarkee46be812010-01-19 14:06:41 +00002729 }
2730}
2731
2732
Steve Blocka7e24c12009-10-30 11:49:00 +00002733static inline int double_get_hash(double d) {
2734 DoubleRepresentation rep(d);
Leon Clarkee46be812010-01-19 14:06:41 +00002735 return static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32);
Steve Blocka7e24c12009-10-30 11:49:00 +00002736}
2737
2738
2739static inline int smi_get_hash(Smi* smi) {
Leon Clarkee46be812010-01-19 14:06:41 +00002740 return smi->value();
Steve Blocka7e24c12009-10-30 11:49:00 +00002741}
2742
2743
Steve Blocka7e24c12009-10-30 11:49:00 +00002744Object* Heap::GetNumberStringCache(Object* number) {
2745 int hash;
Leon Clarkee46be812010-01-19 14:06:41 +00002746 int mask = (number_string_cache()->length() >> 1) - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00002747 if (number->IsSmi()) {
Leon Clarkee46be812010-01-19 14:06:41 +00002748 hash = smi_get_hash(Smi::cast(number)) & mask;
Steve Blocka7e24c12009-10-30 11:49:00 +00002749 } else {
Leon Clarkee46be812010-01-19 14:06:41 +00002750 hash = double_get_hash(number->Number()) & mask;
Steve Blocka7e24c12009-10-30 11:49:00 +00002751 }
2752 Object* key = number_string_cache()->get(hash * 2);
2753 if (key == number) {
2754 return String::cast(number_string_cache()->get(hash * 2 + 1));
2755 } else if (key->IsHeapNumber() &&
2756 number->IsHeapNumber() &&
2757 key->Number() == number->Number()) {
2758 return String::cast(number_string_cache()->get(hash * 2 + 1));
2759 }
2760 return undefined_value();
2761}
2762
2763
2764void Heap::SetNumberStringCache(Object* number, String* string) {
2765 int hash;
Leon Clarkee46be812010-01-19 14:06:41 +00002766 int mask = (number_string_cache()->length() >> 1) - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00002767 if (number->IsSmi()) {
Leon Clarkee46be812010-01-19 14:06:41 +00002768 hash = smi_get_hash(Smi::cast(number)) & mask;
Steve Blocka7e24c12009-10-30 11:49:00 +00002769 } else {
Leon Clarkee46be812010-01-19 14:06:41 +00002770 hash = double_get_hash(number->Number()) & mask;
Steve Blocka7e24c12009-10-30 11:49:00 +00002771 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002772 if (number_string_cache()->get(hash * 2) != undefined_value() &&
2773 number_string_cache()->length() != FullSizeNumberStringCacheLength()) {
2774 // The first time we have a hash collision, we move to the full sized
2775 // number string cache.
2776 AllocateFullSizeNumberStringCache();
2777 return;
2778 }
2779 number_string_cache()->set(hash * 2, number);
Steve Blocka7e24c12009-10-30 11:49:00 +00002780 number_string_cache()->set(hash * 2 + 1, string);
2781}
2782
2783
John Reck59135872010-11-02 12:39:01 -07002784MaybeObject* Heap::NumberToString(Object* number,
2785 bool check_number_string_cache) {
Steve Block44f0eee2011-05-26 01:26:41 +01002786 isolate_->counters()->number_to_string_runtime()->Increment();
Steve Block6ded16b2010-05-10 14:33:55 +01002787 if (check_number_string_cache) {
2788 Object* cached = GetNumberStringCache(number);
2789 if (cached != undefined_value()) {
2790 return cached;
2791 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002792 }
2793
2794 char arr[100];
2795 Vector<char> buffer(arr, ARRAY_SIZE(arr));
2796 const char* str;
2797 if (number->IsSmi()) {
2798 int num = Smi::cast(number)->value();
2799 str = IntToCString(num, buffer);
2800 } else {
2801 double num = HeapNumber::cast(number)->value();
2802 str = DoubleToCString(num, buffer);
2803 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002804
John Reck59135872010-11-02 12:39:01 -07002805 Object* js_string;
2806 MaybeObject* maybe_js_string = AllocateStringFromAscii(CStrVector(str));
2807 if (maybe_js_string->ToObject(&js_string)) {
2808 SetNumberStringCache(number, String::cast(js_string));
Steve Blocka7e24c12009-10-30 11:49:00 +00002809 }
John Reck59135872010-11-02 12:39:01 -07002810 return maybe_js_string;
Steve Blocka7e24c12009-10-30 11:49:00 +00002811}
2812
2813
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002814MaybeObject* Heap::Uint32ToString(uint32_t value,
2815 bool check_number_string_cache) {
2816 Object* number;
2817 MaybeObject* maybe = NumberFromUint32(value);
2818 if (!maybe->To<Object>(&number)) return maybe;
2819 return NumberToString(number, check_number_string_cache);
2820}
2821
2822
Steve Block3ce2e202009-11-05 08:53:23 +00002823Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) {
2824 return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]);
2825}
2826
2827
2828Heap::RootListIndex Heap::RootIndexForExternalArrayType(
2829 ExternalArrayType array_type) {
2830 switch (array_type) {
2831 case kExternalByteArray:
2832 return kExternalByteArrayMapRootIndex;
2833 case kExternalUnsignedByteArray:
2834 return kExternalUnsignedByteArrayMapRootIndex;
2835 case kExternalShortArray:
2836 return kExternalShortArrayMapRootIndex;
2837 case kExternalUnsignedShortArray:
2838 return kExternalUnsignedShortArrayMapRootIndex;
2839 case kExternalIntArray:
2840 return kExternalIntArrayMapRootIndex;
2841 case kExternalUnsignedIntArray:
2842 return kExternalUnsignedIntArrayMapRootIndex;
2843 case kExternalFloatArray:
2844 return kExternalFloatArrayMapRootIndex;
Ben Murdoch257744e2011-11-30 15:57:28 +00002845 case kExternalDoubleArray:
2846 return kExternalDoubleArrayMapRootIndex;
Steve Block44f0eee2011-05-26 01:26:41 +01002847 case kExternalPixelArray:
2848 return kExternalPixelArrayMapRootIndex;
Steve Block3ce2e202009-11-05 08:53:23 +00002849 default:
2850 UNREACHABLE();
2851 return kUndefinedValueRootIndex;
2852 }
2853}
2854
2855
John Reck59135872010-11-02 12:39:01 -07002856MaybeObject* Heap::NumberFromDouble(double value, PretenureFlag pretenure) {
Steve Block6ded16b2010-05-10 14:33:55 +01002857 // We need to distinguish the minus zero value and this cannot be
2858 // done after conversion to int. Doing this by comparing bit
2859 // patterns is faster than using fpclassify() et al.
2860 static const DoubleRepresentation minus_zero(-0.0);
2861
2862 DoubleRepresentation rep(value);
2863 if (rep.bits == minus_zero.bits) {
2864 return AllocateHeapNumber(-0.0, pretenure);
2865 }
2866
2867 int int_value = FastD2I(value);
2868 if (value == int_value && Smi::IsValid(int_value)) {
2869 return Smi::FromInt(int_value);
2870 }
2871
2872 // Materialize the value in the heap.
2873 return AllocateHeapNumber(value, pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +00002874}
2875
2876
Ben Murdoch257744e2011-11-30 15:57:28 +00002877MaybeObject* Heap::AllocateForeign(Address address, PretenureFlag pretenure) {
2878 // Statically ensure that it is safe to allocate foreigns in paged spaces.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002879 STATIC_ASSERT(Foreign::kSize <= Page::kMaxNonCodeHeapObjectSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002880 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002881 Foreign* result;
2882 MaybeObject* maybe_result = Allocate(foreign_map(), space);
2883 if (!maybe_result->To(&result)) return maybe_result;
2884 result->set_foreign_address(address);
Steve Blocka7e24c12009-10-30 11:49:00 +00002885 return result;
2886}
2887
2888
John Reck59135872010-11-02 12:39:01 -07002889MaybeObject* Heap::AllocateSharedFunctionInfo(Object* name) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002890 SharedFunctionInfo* share;
2891 MaybeObject* maybe = Allocate(shared_function_info_map(), OLD_POINTER_SPACE);
2892 if (!maybe->To<SharedFunctionInfo>(&share)) return maybe;
Steve Blocka7e24c12009-10-30 11:49:00 +00002893
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002894 // Set pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00002895 share->set_name(name);
Steve Block44f0eee2011-05-26 01:26:41 +01002896 Code* illegal = isolate_->builtins()->builtin(Builtins::kIllegal);
Steve Blocka7e24c12009-10-30 11:49:00 +00002897 share->set_code(illegal);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002898 share->set_scope_info(ScopeInfo::Empty());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002899 Code* construct_stub =
2900 isolate_->builtins()->builtin(Builtins::kJSConstructStubGeneric);
Steve Blocka7e24c12009-10-30 11:49:00 +00002901 share->set_construct_stub(construct_stub);
Steve Blocka7e24c12009-10-30 11:49:00 +00002902 share->set_instance_class_name(Object_symbol());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002903 share->set_function_data(undefined_value(), SKIP_WRITE_BARRIER);
2904 share->set_script(undefined_value(), SKIP_WRITE_BARRIER);
2905 share->set_debug_info(undefined_value(), SKIP_WRITE_BARRIER);
2906 share->set_inferred_name(empty_string(), SKIP_WRITE_BARRIER);
2907 share->set_initial_map(undefined_value(), SKIP_WRITE_BARRIER);
2908 share->set_this_property_assignments(undefined_value(), SKIP_WRITE_BARRIER);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002909 share->set_ast_node_count(0);
Ben Murdoch8f9999f2012-04-23 10:39:17 +01002910 share->set_deopt_counter(FLAG_deopt_every_n_times);
2911 share->set_ic_age(0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002912
2913 // Set integer fields (smi or int, depending on the architecture).
2914 share->set_length(0);
2915 share->set_formal_parameter_count(0);
2916 share->set_expected_nof_properties(0);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002917 share->set_num_literals(0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002918 share->set_start_position_and_type(0);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002919 share->set_end_position(0);
2920 share->set_function_token_position(0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002921 // All compiler hints default to false or 0.
2922 share->set_compiler_hints(0);
2923 share->set_this_property_assignments_count(0);
2924 share->set_opt_count(0);
2925
2926 return share;
Steve Blocka7e24c12009-10-30 11:49:00 +00002927}
2928
2929
Steve Block1e0659c2011-05-24 12:43:12 +01002930MaybeObject* Heap::AllocateJSMessageObject(String* type,
2931 JSArray* arguments,
2932 int start_position,
2933 int end_position,
2934 Object* script,
2935 Object* stack_trace,
2936 Object* stack_frames) {
2937 Object* result;
2938 { MaybeObject* maybe_result = Allocate(message_object_map(), NEW_SPACE);
2939 if (!maybe_result->ToObject(&result)) return maybe_result;
2940 }
2941 JSMessageObject* message = JSMessageObject::cast(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002942 message->set_properties(Heap::empty_fixed_array(), SKIP_WRITE_BARRIER);
2943 message->set_elements(Heap::empty_fixed_array(), SKIP_WRITE_BARRIER);
Steve Block1e0659c2011-05-24 12:43:12 +01002944 message->set_type(type);
2945 message->set_arguments(arguments);
2946 message->set_start_position(start_position);
2947 message->set_end_position(end_position);
2948 message->set_script(script);
2949 message->set_stack_trace(stack_trace);
2950 message->set_stack_frames(stack_frames);
2951 return result;
2952}
2953
2954
2955
Steve Blockd0582a62009-12-15 09:54:21 +00002956// Returns true for a character in a range. Both limits are inclusive.
2957static inline bool Between(uint32_t character, uint32_t from, uint32_t to) {
2958 // This makes uses of the the unsigned wraparound.
2959 return character - from <= to - from;
2960}
2961
2962
John Reck59135872010-11-02 12:39:01 -07002963MUST_USE_RESULT static inline MaybeObject* MakeOrFindTwoCharacterString(
Steve Block44f0eee2011-05-26 01:26:41 +01002964 Heap* heap,
John Reck59135872010-11-02 12:39:01 -07002965 uint32_t c1,
2966 uint32_t c2) {
Steve Blockd0582a62009-12-15 09:54:21 +00002967 String* symbol;
2968 // Numeric strings have a different hash algorithm not known by
2969 // LookupTwoCharsSymbolIfExists, so we skip this step for such strings.
2970 if ((!Between(c1, '0', '9') || !Between(c2, '0', '9')) &&
Steve Block44f0eee2011-05-26 01:26:41 +01002971 heap->symbol_table()->LookupTwoCharsSymbolIfExists(c1, c2, &symbol)) {
Steve Blockd0582a62009-12-15 09:54:21 +00002972 return symbol;
2973 // Now we know the length is 2, we might as well make use of that fact
2974 // when building the new string.
2975 } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this
2976 ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this.
John Reck59135872010-11-02 12:39:01 -07002977 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01002978 { MaybeObject* maybe_result = heap->AllocateRawAsciiString(2);
John Reck59135872010-11-02 12:39:01 -07002979 if (!maybe_result->ToObject(&result)) return maybe_result;
2980 }
Steve Blockd0582a62009-12-15 09:54:21 +00002981 char* dest = SeqAsciiString::cast(result)->GetChars();
2982 dest[0] = c1;
2983 dest[1] = c2;
2984 return result;
2985 } else {
John Reck59135872010-11-02 12:39:01 -07002986 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01002987 { MaybeObject* maybe_result = heap->AllocateRawTwoByteString(2);
John Reck59135872010-11-02 12:39:01 -07002988 if (!maybe_result->ToObject(&result)) return maybe_result;
2989 }
Steve Blockd0582a62009-12-15 09:54:21 +00002990 uc16* dest = SeqTwoByteString::cast(result)->GetChars();
2991 dest[0] = c1;
2992 dest[1] = c2;
2993 return result;
2994 }
2995}
2996
2997
John Reck59135872010-11-02 12:39:01 -07002998MaybeObject* Heap::AllocateConsString(String* first, String* second) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002999 int first_length = first->length();
Steve Blockd0582a62009-12-15 09:54:21 +00003000 if (first_length == 0) {
3001 return second;
3002 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003003
3004 int second_length = second->length();
Steve Blockd0582a62009-12-15 09:54:21 +00003005 if (second_length == 0) {
3006 return first;
3007 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003008
3009 int length = first_length + second_length;
Steve Blockd0582a62009-12-15 09:54:21 +00003010
3011 // Optimization for 2-byte strings often used as keys in a decompression
3012 // dictionary. Check whether we already have the string in the symbol
3013 // table to prevent creation of many unneccesary strings.
3014 if (length == 2) {
3015 unsigned c1 = first->Get(0);
3016 unsigned c2 = second->Get(0);
Steve Block44f0eee2011-05-26 01:26:41 +01003017 return MakeOrFindTwoCharacterString(this, c1, c2);
Steve Blockd0582a62009-12-15 09:54:21 +00003018 }
3019
Steve Block6ded16b2010-05-10 14:33:55 +01003020 bool first_is_ascii = first->IsAsciiRepresentation();
3021 bool second_is_ascii = second->IsAsciiRepresentation();
3022 bool is_ascii = first_is_ascii && second_is_ascii;
Steve Blocka7e24c12009-10-30 11:49:00 +00003023
3024 // Make sure that an out of memory exception is thrown if the length
Steve Block3ce2e202009-11-05 08:53:23 +00003025 // of the new cons string is too large.
3026 if (length > String::kMaxLength || length < 0) {
Steve Block44f0eee2011-05-26 01:26:41 +01003027 isolate()->context()->mark_out_of_memory();
Steve Blocka7e24c12009-10-30 11:49:00 +00003028 return Failure::OutOfMemoryException();
3029 }
3030
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003031 bool is_ascii_data_in_two_byte_string = false;
3032 if (!is_ascii) {
3033 // At least one of the strings uses two-byte representation so we
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003034 // can't use the fast case code for short ASCII strings below, but
3035 // we can try to save memory if all chars actually fit in ASCII.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003036 is_ascii_data_in_two_byte_string =
3037 first->HasOnlyAsciiChars() && second->HasOnlyAsciiChars();
3038 if (is_ascii_data_in_two_byte_string) {
Steve Block44f0eee2011-05-26 01:26:41 +01003039 isolate_->counters()->string_add_runtime_ext_to_ascii()->Increment();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003040 }
3041 }
3042
Steve Blocka7e24c12009-10-30 11:49:00 +00003043 // If the resulting string is small make a flat string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003044 if (length < ConsString::kMinLength) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003045 // Note that neither of the two inputs can be a slice because:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003046 STATIC_ASSERT(ConsString::kMinLength <= SlicedString::kMinLength);
Steve Blocka7e24c12009-10-30 11:49:00 +00003047 ASSERT(first->IsFlat());
3048 ASSERT(second->IsFlat());
3049 if (is_ascii) {
John Reck59135872010-11-02 12:39:01 -07003050 Object* result;
3051 { MaybeObject* maybe_result = AllocateRawAsciiString(length);
3052 if (!maybe_result->ToObject(&result)) return maybe_result;
3053 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003054 // Copy the characters into the new object.
3055 char* dest = SeqAsciiString::cast(result)->GetChars();
3056 // Copy first part.
Steve Blockd0582a62009-12-15 09:54:21 +00003057 const char* src;
3058 if (first->IsExternalString()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003059 src = ExternalAsciiString::cast(first)->GetChars();
Steve Blockd0582a62009-12-15 09:54:21 +00003060 } else {
3061 src = SeqAsciiString::cast(first)->GetChars();
3062 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003063 for (int i = 0; i < first_length; i++) *dest++ = src[i];
3064 // Copy second part.
Steve Blockd0582a62009-12-15 09:54:21 +00003065 if (second->IsExternalString()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003066 src = ExternalAsciiString::cast(second)->GetChars();
Steve Blockd0582a62009-12-15 09:54:21 +00003067 } else {
3068 src = SeqAsciiString::cast(second)->GetChars();
3069 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003070 for (int i = 0; i < second_length; i++) *dest++ = src[i];
3071 return result;
3072 } else {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003073 if (is_ascii_data_in_two_byte_string) {
John Reck59135872010-11-02 12:39:01 -07003074 Object* result;
3075 { MaybeObject* maybe_result = AllocateRawAsciiString(length);
3076 if (!maybe_result->ToObject(&result)) return maybe_result;
3077 }
Steve Block6ded16b2010-05-10 14:33:55 +01003078 // Copy the characters into the new object.
3079 char* dest = SeqAsciiString::cast(result)->GetChars();
3080 String::WriteToFlat(first, dest, 0, first_length);
3081 String::WriteToFlat(second, dest + first_length, 0, second_length);
Steve Block44f0eee2011-05-26 01:26:41 +01003082 isolate_->counters()->string_add_runtime_ext_to_ascii()->Increment();
Steve Block6ded16b2010-05-10 14:33:55 +01003083 return result;
3084 }
3085
John Reck59135872010-11-02 12:39:01 -07003086 Object* result;
3087 { MaybeObject* maybe_result = AllocateRawTwoByteString(length);
3088 if (!maybe_result->ToObject(&result)) return maybe_result;
3089 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003090 // Copy the characters into the new object.
3091 uc16* dest = SeqTwoByteString::cast(result)->GetChars();
3092 String::WriteToFlat(first, dest, 0, first_length);
3093 String::WriteToFlat(second, dest + first_length, 0, second_length);
3094 return result;
3095 }
3096 }
3097
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003098 Map* map = (is_ascii || is_ascii_data_in_two_byte_string) ?
3099 cons_ascii_string_map() : cons_string_map();
Steve Blocka7e24c12009-10-30 11:49:00 +00003100
John Reck59135872010-11-02 12:39:01 -07003101 Object* result;
3102 { MaybeObject* maybe_result = Allocate(map, NEW_SPACE);
3103 if (!maybe_result->ToObject(&result)) return maybe_result;
3104 }
Leon Clarke4515c472010-02-03 11:58:03 +00003105
3106 AssertNoAllocation no_gc;
Steve Blocka7e24c12009-10-30 11:49:00 +00003107 ConsString* cons_string = ConsString::cast(result);
Leon Clarke4515c472010-02-03 11:58:03 +00003108 WriteBarrierMode mode = cons_string->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00003109 cons_string->set_length(length);
Steve Blockd0582a62009-12-15 09:54:21 +00003110 cons_string->set_hash_field(String::kEmptyHashField);
3111 cons_string->set_first(first, mode);
3112 cons_string->set_second(second, mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00003113 return result;
3114}
3115
3116
John Reck59135872010-11-02 12:39:01 -07003117MaybeObject* Heap::AllocateSubString(String* buffer,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003118 int start,
3119 int end,
3120 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003121 int length = end - start;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003122 if (length <= 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003123 return empty_string();
3124 } else if (length == 1) {
Steve Block44f0eee2011-05-26 01:26:41 +01003125 return LookupSingleCharacterStringFromCode(buffer->Get(start));
Steve Blockd0582a62009-12-15 09:54:21 +00003126 } else if (length == 2) {
3127 // Optimization for 2-byte strings often used as keys in a decompression
3128 // dictionary. Check whether we already have the string in the symbol
3129 // table to prevent creation of many unneccesary strings.
3130 unsigned c1 = buffer->Get(start);
3131 unsigned c2 = buffer->Get(start + 1);
Steve Block44f0eee2011-05-26 01:26:41 +01003132 return MakeOrFindTwoCharacterString(this, c1, c2);
Steve Blocka7e24c12009-10-30 11:49:00 +00003133 }
3134
3135 // Make an attempt to flatten the buffer to reduce access time.
Leon Clarkef7060e22010-06-03 12:02:55 +01003136 buffer = buffer->TryFlattenGetString();
Steve Blocka7e24c12009-10-30 11:49:00 +00003137
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003138 if (!FLAG_string_slices ||
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003139 !buffer->IsFlat() ||
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003140 length < SlicedString::kMinLength ||
3141 pretenure == TENURED) {
3142 Object* result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003143 // WriteToFlat takes care of the case when an indirect string has a
3144 // different encoding from its underlying string. These encodings may
3145 // differ because of externalization.
3146 bool is_ascii = buffer->IsAsciiRepresentation();
3147 { MaybeObject* maybe_result = is_ascii
3148 ? AllocateRawAsciiString(length, pretenure)
3149 : AllocateRawTwoByteString(length, pretenure);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003150 if (!maybe_result->ToObject(&result)) return maybe_result;
3151 }
3152 String* string_result = String::cast(result);
3153 // Copy the characters into the new object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003154 if (is_ascii) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003155 ASSERT(string_result->IsAsciiRepresentation());
3156 char* dest = SeqAsciiString::cast(string_result)->GetChars();
3157 String::WriteToFlat(buffer, dest, start, end);
3158 } else {
3159 ASSERT(string_result->IsTwoByteRepresentation());
3160 uc16* dest = SeqTwoByteString::cast(string_result)->GetChars();
3161 String::WriteToFlat(buffer, dest, start, end);
3162 }
3163 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003164 }
Steve Blockd0582a62009-12-15 09:54:21 +00003165
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003166 ASSERT(buffer->IsFlat());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003167#if DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003168 if (FLAG_verify_heap) {
3169 buffer->StringVerify();
3170 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003171#endif
3172
3173 Object* result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003174 // When slicing an indirect string we use its encoding for a newly created
3175 // slice and don't check the encoding of the underlying string. This is safe
3176 // even if the encodings are different because of externalization. If an
3177 // indirect ASCII string is pointing to a two-byte string, the two-byte char
3178 // codes of the underlying string must still fit into ASCII (because
3179 // externalization must not change char codes).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003180 { Map* map = buffer->IsAsciiRepresentation()
3181 ? sliced_ascii_string_map()
3182 : sliced_string_map();
3183 MaybeObject* maybe_result = Allocate(map, NEW_SPACE);
3184 if (!maybe_result->ToObject(&result)) return maybe_result;
3185 }
3186
3187 AssertNoAllocation no_gc;
3188 SlicedString* sliced_string = SlicedString::cast(result);
3189 sliced_string->set_length(length);
3190 sliced_string->set_hash_field(String::kEmptyHashField);
3191 if (buffer->IsConsString()) {
3192 ConsString* cons = ConsString::cast(buffer);
3193 ASSERT(cons->second()->length() == 0);
3194 sliced_string->set_parent(cons->first());
3195 sliced_string->set_offset(start);
3196 } else if (buffer->IsSlicedString()) {
3197 // Prevent nesting sliced strings.
3198 SlicedString* parent_slice = SlicedString::cast(buffer);
3199 sliced_string->set_parent(parent_slice->parent());
3200 sliced_string->set_offset(start + parent_slice->offset());
3201 } else {
3202 sliced_string->set_parent(buffer);
3203 sliced_string->set_offset(start);
3204 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003205 ASSERT(sliced_string->parent()->IsSeqString() ||
3206 sliced_string->parent()->IsExternalString());
Steve Blocka7e24c12009-10-30 11:49:00 +00003207 return result;
3208}
3209
3210
John Reck59135872010-11-02 12:39:01 -07003211MaybeObject* Heap::AllocateExternalStringFromAscii(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003212 const ExternalAsciiString::Resource* resource) {
Steve Blockd0582a62009-12-15 09:54:21 +00003213 size_t length = resource->length();
3214 if (length > static_cast<size_t>(String::kMaxLength)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003215 isolate()->context()->mark_out_of_memory();
Steve Blockd0582a62009-12-15 09:54:21 +00003216 return Failure::OutOfMemoryException();
Steve Blocka7e24c12009-10-30 11:49:00 +00003217 }
3218
Steve Blockd0582a62009-12-15 09:54:21 +00003219 Map* map = external_ascii_string_map();
John Reck59135872010-11-02 12:39:01 -07003220 Object* result;
3221 { MaybeObject* maybe_result = Allocate(map, NEW_SPACE);
3222 if (!maybe_result->ToObject(&result)) return maybe_result;
3223 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003224
3225 ExternalAsciiString* external_string = ExternalAsciiString::cast(result);
Steve Blockd0582a62009-12-15 09:54:21 +00003226 external_string->set_length(static_cast<int>(length));
3227 external_string->set_hash_field(String::kEmptyHashField);
Steve Blocka7e24c12009-10-30 11:49:00 +00003228 external_string->set_resource(resource);
3229
3230 return result;
3231}
3232
3233
John Reck59135872010-11-02 12:39:01 -07003234MaybeObject* Heap::AllocateExternalStringFromTwoByte(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003235 const ExternalTwoByteString::Resource* resource) {
Steve Blockd0582a62009-12-15 09:54:21 +00003236 size_t length = resource->length();
3237 if (length > static_cast<size_t>(String::kMaxLength)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003238 isolate()->context()->mark_out_of_memory();
Steve Blockd0582a62009-12-15 09:54:21 +00003239 return Failure::OutOfMemoryException();
3240 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003241
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003242 // For small strings we check whether the resource contains only
Steve Block9fac8402011-05-12 15:51:54 +01003243 // ASCII characters. If yes, we use a different string map.
3244 static const size_t kAsciiCheckLengthLimit = 32;
3245 bool is_ascii = length <= kAsciiCheckLengthLimit &&
3246 String::IsAscii(resource->data(), static_cast<int>(length));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003247 Map* map = is_ascii ?
Steve Block44f0eee2011-05-26 01:26:41 +01003248 external_string_with_ascii_data_map() : external_string_map();
John Reck59135872010-11-02 12:39:01 -07003249 Object* result;
3250 { MaybeObject* maybe_result = Allocate(map, NEW_SPACE);
3251 if (!maybe_result->ToObject(&result)) return maybe_result;
3252 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003253
3254 ExternalTwoByteString* external_string = ExternalTwoByteString::cast(result);
Steve Blockd0582a62009-12-15 09:54:21 +00003255 external_string->set_length(static_cast<int>(length));
3256 external_string->set_hash_field(String::kEmptyHashField);
Steve Blocka7e24c12009-10-30 11:49:00 +00003257 external_string->set_resource(resource);
3258
3259 return result;
3260}
3261
3262
John Reck59135872010-11-02 12:39:01 -07003263MaybeObject* Heap::LookupSingleCharacterStringFromCode(uint16_t code) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003264 if (code <= String::kMaxAsciiCharCode) {
Steve Block44f0eee2011-05-26 01:26:41 +01003265 Object* value = single_character_string_cache()->get(code);
3266 if (value != undefined_value()) return value;
Steve Blocka7e24c12009-10-30 11:49:00 +00003267
3268 char buffer[1];
3269 buffer[0] = static_cast<char>(code);
John Reck59135872010-11-02 12:39:01 -07003270 Object* result;
3271 MaybeObject* maybe_result = LookupSymbol(Vector<const char>(buffer, 1));
Steve Blocka7e24c12009-10-30 11:49:00 +00003272
John Reck59135872010-11-02 12:39:01 -07003273 if (!maybe_result->ToObject(&result)) return maybe_result;
Steve Block44f0eee2011-05-26 01:26:41 +01003274 single_character_string_cache()->set(code, result);
Steve Blocka7e24c12009-10-30 11:49:00 +00003275 return result;
3276 }
3277
John Reck59135872010-11-02 12:39:01 -07003278 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01003279 { MaybeObject* maybe_result = AllocateRawTwoByteString(1);
John Reck59135872010-11-02 12:39:01 -07003280 if (!maybe_result->ToObject(&result)) return maybe_result;
3281 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003282 String* answer = String::cast(result);
3283 answer->Set(0, code);
3284 return answer;
3285}
3286
3287
John Reck59135872010-11-02 12:39:01 -07003288MaybeObject* Heap::AllocateByteArray(int length, PretenureFlag pretenure) {
Leon Clarkee46be812010-01-19 14:06:41 +00003289 if (length < 0 || length > ByteArray::kMaxLength) {
3290 return Failure::OutOfMemoryException();
3291 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003292 if (pretenure == NOT_TENURED) {
3293 return AllocateByteArray(length);
3294 }
3295 int size = ByteArray::SizeFor(length);
John Reck59135872010-11-02 12:39:01 -07003296 Object* result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003297 { MaybeObject* maybe_result = (size <= Page::kMaxNonCodeHeapObjectSize)
John Reck59135872010-11-02 12:39:01 -07003298 ? old_data_space_->AllocateRaw(size)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003299 : lo_space_->AllocateRaw(size, NOT_EXECUTABLE);
John Reck59135872010-11-02 12:39:01 -07003300 if (!maybe_result->ToObject(&result)) return maybe_result;
3301 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003302
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003303 reinterpret_cast<ByteArray*>(result)->set_map_no_write_barrier(
3304 byte_array_map());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003305 reinterpret_cast<ByteArray*>(result)->set_length(length);
Steve Blocka7e24c12009-10-30 11:49:00 +00003306 return result;
3307}
3308
3309
John Reck59135872010-11-02 12:39:01 -07003310MaybeObject* Heap::AllocateByteArray(int length) {
Leon Clarkee46be812010-01-19 14:06:41 +00003311 if (length < 0 || length > ByteArray::kMaxLength) {
3312 return Failure::OutOfMemoryException();
3313 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003314 int size = ByteArray::SizeFor(length);
3315 AllocationSpace space =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003316 (size > Page::kMaxNonCodeHeapObjectSize) ? LO_SPACE : NEW_SPACE;
John Reck59135872010-11-02 12:39:01 -07003317 Object* result;
3318 { MaybeObject* maybe_result = AllocateRaw(size, space, OLD_DATA_SPACE);
3319 if (!maybe_result->ToObject(&result)) return maybe_result;
3320 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003321
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003322 reinterpret_cast<ByteArray*>(result)->set_map_no_write_barrier(
3323 byte_array_map());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003324 reinterpret_cast<ByteArray*>(result)->set_length(length);
Steve Blocka7e24c12009-10-30 11:49:00 +00003325 return result;
3326}
3327
3328
3329void Heap::CreateFillerObjectAt(Address addr, int size) {
3330 if (size == 0) return;
3331 HeapObject* filler = HeapObject::FromAddress(addr);
3332 if (size == kPointerSize) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003333 filler->set_map_no_write_barrier(one_pointer_filler_map());
Steve Block6ded16b2010-05-10 14:33:55 +01003334 } else if (size == 2 * kPointerSize) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003335 filler->set_map_no_write_barrier(two_pointer_filler_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00003336 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003337 filler->set_map_no_write_barrier(free_space_map());
3338 FreeSpace::cast(filler)->set_size(size);
Steve Blocka7e24c12009-10-30 11:49:00 +00003339 }
3340}
3341
3342
John Reck59135872010-11-02 12:39:01 -07003343MaybeObject* Heap::AllocateExternalArray(int length,
3344 ExternalArrayType array_type,
3345 void* external_pointer,
3346 PretenureFlag pretenure) {
Steve Block3ce2e202009-11-05 08:53:23 +00003347 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
John Reck59135872010-11-02 12:39:01 -07003348 Object* result;
3349 { MaybeObject* maybe_result = AllocateRaw(ExternalArray::kAlignedSize,
3350 space,
3351 OLD_DATA_SPACE);
3352 if (!maybe_result->ToObject(&result)) return maybe_result;
3353 }
Steve Block3ce2e202009-11-05 08:53:23 +00003354
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003355 reinterpret_cast<ExternalArray*>(result)->set_map_no_write_barrier(
Steve Block3ce2e202009-11-05 08:53:23 +00003356 MapForExternalArrayType(array_type));
3357 reinterpret_cast<ExternalArray*>(result)->set_length(length);
3358 reinterpret_cast<ExternalArray*>(result)->set_external_pointer(
3359 external_pointer);
3360
3361 return result;
3362}
3363
3364
John Reck59135872010-11-02 12:39:01 -07003365MaybeObject* Heap::CreateCode(const CodeDesc& desc,
3366 Code::Flags flags,
Steve Block44f0eee2011-05-26 01:26:41 +01003367 Handle<Object> self_reference,
3368 bool immovable) {
Leon Clarkeac952652010-07-15 11:15:24 +01003369 // Allocate ByteArray before the Code object, so that we do not risk
3370 // leaving uninitialized Code object (and breaking the heap).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003371 ByteArray* reloc_info;
3372 MaybeObject* maybe_reloc_info = AllocateByteArray(desc.reloc_size, TENURED);
3373 if (!maybe_reloc_info->To(&reloc_info)) return maybe_reloc_info;
Leon Clarkeac952652010-07-15 11:15:24 +01003374
Steve Block44f0eee2011-05-26 01:26:41 +01003375 // Compute size.
Leon Clarkeac952652010-07-15 11:15:24 +01003376 int body_size = RoundUp(desc.instr_size, kObjectAlignment);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003377 int obj_size = Code::SizeFor(body_size);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003378 ASSERT(IsAligned(static_cast<intptr_t>(obj_size), kCodeAlignment));
John Reck59135872010-11-02 12:39:01 -07003379 MaybeObject* maybe_result;
Steve Block44f0eee2011-05-26 01:26:41 +01003380 // Large code objects and code objects which should stay at a fixed address
3381 // are allocated in large object space.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003382 if (obj_size > code_space()->AreaSize() || immovable) {
3383 maybe_result = lo_space_->AllocateRaw(obj_size, EXECUTABLE);
Steve Blocka7e24c12009-10-30 11:49:00 +00003384 } else {
John Reck59135872010-11-02 12:39:01 -07003385 maybe_result = code_space_->AllocateRaw(obj_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00003386 }
3387
John Reck59135872010-11-02 12:39:01 -07003388 Object* result;
3389 if (!maybe_result->ToObject(&result)) return maybe_result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003390
3391 // Initialize the object
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003392 HeapObject::cast(result)->set_map_no_write_barrier(code_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00003393 Code* code = Code::cast(result);
Steve Block44f0eee2011-05-26 01:26:41 +01003394 ASSERT(!isolate_->code_range()->exists() ||
3395 isolate_->code_range()->contains(code->address()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003396 code->set_instruction_size(desc.instr_size);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003397 code->set_relocation_info(reloc_info);
Steve Blocka7e24c12009-10-30 11:49:00 +00003398 code->set_flags(flags);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003399 if (code->is_call_stub() || code->is_keyed_call_stub()) {
3400 code->set_check_type(RECEIVER_MAP_CHECK);
3401 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003402 code->set_deoptimization_data(empty_fixed_array(), SKIP_WRITE_BARRIER);
3403 code->set_type_feedback_info(undefined_value(), SKIP_WRITE_BARRIER);
3404 code->set_handler_table(empty_fixed_array(), SKIP_WRITE_BARRIER);
3405 code->set_gc_metadata(Smi::FromInt(0));
3406 code->set_ic_age(global_ic_age_);
Steve Blocka7e24c12009-10-30 11:49:00 +00003407 // Allow self references to created code object by patching the handle to
3408 // point to the newly allocated Code object.
3409 if (!self_reference.is_null()) {
3410 *(self_reference.location()) = code;
3411 }
3412 // Migrate generated code.
3413 // The generated code can contain Object** values (typically from handles)
3414 // that are dereferenced during the copy to point directly to the actual heap
3415 // objects. These pointers can include references to the code object itself,
3416 // through the self_reference parameter.
3417 code->CopyFrom(desc);
Steve Blocka7e24c12009-10-30 11:49:00 +00003418
3419#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003420 if (FLAG_verify_heap) {
3421 code->Verify();
3422 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003423#endif
3424 return code;
3425}
3426
3427
John Reck59135872010-11-02 12:39:01 -07003428MaybeObject* Heap::CopyCode(Code* code) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003429 // Allocate an object the same size as the code object.
3430 int obj_size = code->Size();
John Reck59135872010-11-02 12:39:01 -07003431 MaybeObject* maybe_result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003432 if (obj_size > code_space()->AreaSize()) {
3433 maybe_result = lo_space_->AllocateRaw(obj_size, EXECUTABLE);
Steve Blocka7e24c12009-10-30 11:49:00 +00003434 } else {
John Reck59135872010-11-02 12:39:01 -07003435 maybe_result = code_space_->AllocateRaw(obj_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00003436 }
3437
John Reck59135872010-11-02 12:39:01 -07003438 Object* result;
3439 if (!maybe_result->ToObject(&result)) return maybe_result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003440
3441 // Copy code object.
3442 Address old_addr = code->address();
3443 Address new_addr = reinterpret_cast<HeapObject*>(result)->address();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003444 CopyBlock(new_addr, old_addr, obj_size);
Steve Blocka7e24c12009-10-30 11:49:00 +00003445 // Relocate the copy.
3446 Code* new_code = Code::cast(result);
Steve Block44f0eee2011-05-26 01:26:41 +01003447 ASSERT(!isolate_->code_range()->exists() ||
3448 isolate_->code_range()->contains(code->address()));
Steve Blocka7e24c12009-10-30 11:49:00 +00003449 new_code->Relocate(new_addr - old_addr);
3450 return new_code;
3451}
3452
3453
John Reck59135872010-11-02 12:39:01 -07003454MaybeObject* Heap::CopyCode(Code* code, Vector<byte> reloc_info) {
Leon Clarkeac952652010-07-15 11:15:24 +01003455 // Allocate ByteArray before the Code object, so that we do not risk
3456 // leaving uninitialized Code object (and breaking the heap).
John Reck59135872010-11-02 12:39:01 -07003457 Object* reloc_info_array;
3458 { MaybeObject* maybe_reloc_info_array =
3459 AllocateByteArray(reloc_info.length(), TENURED);
3460 if (!maybe_reloc_info_array->ToObject(&reloc_info_array)) {
3461 return maybe_reloc_info_array;
3462 }
3463 }
Leon Clarkeac952652010-07-15 11:15:24 +01003464
3465 int new_body_size = RoundUp(code->instruction_size(), kObjectAlignment);
Steve Block6ded16b2010-05-10 14:33:55 +01003466
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003467 int new_obj_size = Code::SizeFor(new_body_size);
Steve Block6ded16b2010-05-10 14:33:55 +01003468
3469 Address old_addr = code->address();
3470
3471 size_t relocation_offset =
Leon Clarkeac952652010-07-15 11:15:24 +01003472 static_cast<size_t>(code->instruction_end() - old_addr);
Steve Block6ded16b2010-05-10 14:33:55 +01003473
John Reck59135872010-11-02 12:39:01 -07003474 MaybeObject* maybe_result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003475 if (new_obj_size > code_space()->AreaSize()) {
3476 maybe_result = lo_space_->AllocateRaw(new_obj_size, EXECUTABLE);
Steve Block6ded16b2010-05-10 14:33:55 +01003477 } else {
John Reck59135872010-11-02 12:39:01 -07003478 maybe_result = code_space_->AllocateRaw(new_obj_size);
Steve Block6ded16b2010-05-10 14:33:55 +01003479 }
3480
John Reck59135872010-11-02 12:39:01 -07003481 Object* result;
3482 if (!maybe_result->ToObject(&result)) return maybe_result;
Steve Block6ded16b2010-05-10 14:33:55 +01003483
3484 // Copy code object.
3485 Address new_addr = reinterpret_cast<HeapObject*>(result)->address();
3486
3487 // Copy header and instructions.
3488 memcpy(new_addr, old_addr, relocation_offset);
3489
Steve Block6ded16b2010-05-10 14:33:55 +01003490 Code* new_code = Code::cast(result);
Leon Clarkeac952652010-07-15 11:15:24 +01003491 new_code->set_relocation_info(ByteArray::cast(reloc_info_array));
Steve Block6ded16b2010-05-10 14:33:55 +01003492
Leon Clarkeac952652010-07-15 11:15:24 +01003493 // Copy patched rinfo.
3494 memcpy(new_code->relocation_start(), reloc_info.start(), reloc_info.length());
Steve Block6ded16b2010-05-10 14:33:55 +01003495
3496 // Relocate the copy.
Steve Block44f0eee2011-05-26 01:26:41 +01003497 ASSERT(!isolate_->code_range()->exists() ||
3498 isolate_->code_range()->contains(code->address()));
Steve Block6ded16b2010-05-10 14:33:55 +01003499 new_code->Relocate(new_addr - old_addr);
3500
3501#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003502 if (FLAG_verify_heap) {
3503 code->Verify();
3504 }
Steve Block6ded16b2010-05-10 14:33:55 +01003505#endif
3506 return new_code;
3507}
3508
3509
John Reck59135872010-11-02 12:39:01 -07003510MaybeObject* Heap::Allocate(Map* map, AllocationSpace space) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003511 ASSERT(gc_state_ == NOT_IN_GC);
3512 ASSERT(map->instance_type() != MAP_TYPE);
Leon Clarkee46be812010-01-19 14:06:41 +00003513 // If allocation failures are disallowed, we may allocate in a different
3514 // space when new space is full and the object is not a large object.
3515 AllocationSpace retry_space =
3516 (space != NEW_SPACE) ? space : TargetSpaceId(map->instance_type());
John Reck59135872010-11-02 12:39:01 -07003517 Object* result;
3518 { MaybeObject* maybe_result =
3519 AllocateRaw(map->instance_size(), space, retry_space);
3520 if (!maybe_result->ToObject(&result)) return maybe_result;
3521 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003522 // No need for write barrier since object is white and map is in old space.
3523 HeapObject::cast(result)->set_map_no_write_barrier(map);
Steve Blocka7e24c12009-10-30 11:49:00 +00003524 return result;
3525}
3526
3527
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003528void Heap::InitializeFunction(JSFunction* function,
3529 SharedFunctionInfo* shared,
3530 Object* prototype) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003531 ASSERT(!prototype->IsMap());
3532 function->initialize_properties();
3533 function->initialize_elements();
3534 function->set_shared(shared);
Iain Merrick75681382010-08-19 15:07:18 +01003535 function->set_code(shared->code());
Steve Blocka7e24c12009-10-30 11:49:00 +00003536 function->set_prototype_or_initial_map(prototype);
3537 function->set_context(undefined_value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003538 function->set_literals_or_bindings(empty_fixed_array());
Ben Murdochb0fe1622011-05-05 13:52:32 +01003539 function->set_next_function_link(undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00003540}
3541
3542
John Reck59135872010-11-02 12:39:01 -07003543MaybeObject* Heap::AllocateFunctionPrototype(JSFunction* function) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003544 // Allocate the prototype. Make sure to use the object function
3545 // from the function's context, since the function can be from a
3546 // different context.
3547 JSFunction* object_function =
3548 function->context()->global_context()->object_function();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003549
3550 // Each function prototype gets a copy of the object function map.
3551 // This avoid unwanted sharing of maps between prototypes of different
3552 // constructors.
3553 Map* new_map;
3554 ASSERT(object_function->has_initial_map());
3555 { MaybeObject* maybe_map =
3556 object_function->initial_map()->CopyDropTransitions();
3557 if (!maybe_map->To<Map>(&new_map)) return maybe_map;
3558 }
John Reck59135872010-11-02 12:39:01 -07003559 Object* prototype;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003560 { MaybeObject* maybe_prototype = AllocateJSObjectFromMap(new_map);
John Reck59135872010-11-02 12:39:01 -07003561 if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype;
3562 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003563 // When creating the prototype for the function we must set its
3564 // constructor to the function.
John Reck59135872010-11-02 12:39:01 -07003565 Object* result;
3566 { MaybeObject* maybe_result =
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003567 JSObject::cast(prototype)->SetLocalPropertyIgnoreAttributes(
3568 constructor_symbol(), function, DONT_ENUM);
John Reck59135872010-11-02 12:39:01 -07003569 if (!maybe_result->ToObject(&result)) return maybe_result;
3570 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003571 return prototype;
3572}
3573
3574
John Reck59135872010-11-02 12:39:01 -07003575MaybeObject* Heap::AllocateFunction(Map* function_map,
3576 SharedFunctionInfo* shared,
3577 Object* prototype,
3578 PretenureFlag pretenure) {
Leon Clarkee46be812010-01-19 14:06:41 +00003579 AllocationSpace space =
3580 (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE;
John Reck59135872010-11-02 12:39:01 -07003581 Object* result;
3582 { MaybeObject* maybe_result = Allocate(function_map, space);
3583 if (!maybe_result->ToObject(&result)) return maybe_result;
3584 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003585 InitializeFunction(JSFunction::cast(result), shared, prototype);
3586 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +00003587}
3588
3589
John Reck59135872010-11-02 12:39:01 -07003590MaybeObject* Heap::AllocateArgumentsObject(Object* callee, int length) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003591 // To get fast allocation and map sharing for arguments objects we
3592 // allocate them based on an arguments boilerplate.
3593
Steve Block44f0eee2011-05-26 01:26:41 +01003594 JSObject* boilerplate;
3595 int arguments_object_size;
3596 bool strict_mode_callee = callee->IsJSFunction() &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003597 !JSFunction::cast(callee)->shared()->is_classic_mode();
Steve Block44f0eee2011-05-26 01:26:41 +01003598 if (strict_mode_callee) {
3599 boilerplate =
3600 isolate()->context()->global_context()->
3601 strict_mode_arguments_boilerplate();
3602 arguments_object_size = kArgumentsObjectSizeStrict;
3603 } else {
3604 boilerplate =
3605 isolate()->context()->global_context()->arguments_boilerplate();
3606 arguments_object_size = kArgumentsObjectSize;
3607 }
3608
Steve Blocka7e24c12009-10-30 11:49:00 +00003609 // This calls Copy directly rather than using Heap::AllocateRaw so we
3610 // duplicate the check here.
3611 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
3612
Leon Clarkee46be812010-01-19 14:06:41 +00003613 // Check that the size of the boilerplate matches our
3614 // expectations. The ArgumentsAccessStub::GenerateNewObject relies
3615 // on the size being a known constant.
Steve Block44f0eee2011-05-26 01:26:41 +01003616 ASSERT(arguments_object_size == boilerplate->map()->instance_size());
Leon Clarkee46be812010-01-19 14:06:41 +00003617
3618 // Do the allocation.
John Reck59135872010-11-02 12:39:01 -07003619 Object* result;
3620 { MaybeObject* maybe_result =
Steve Block44f0eee2011-05-26 01:26:41 +01003621 AllocateRaw(arguments_object_size, NEW_SPACE, OLD_POINTER_SPACE);
John Reck59135872010-11-02 12:39:01 -07003622 if (!maybe_result->ToObject(&result)) return maybe_result;
3623 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003624
3625 // Copy the content. The arguments boilerplate doesn't have any
3626 // fields that point to new space so it's safe to skip the write
3627 // barrier here.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003628 CopyBlock(HeapObject::cast(result)->address(),
3629 boilerplate->address(),
Steve Block44f0eee2011-05-26 01:26:41 +01003630 JSObject::kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003631
Steve Block44f0eee2011-05-26 01:26:41 +01003632 // Set the length property.
3633 JSObject::cast(result)->InObjectPropertyAtPut(kArgumentsLengthIndex,
Steve Blocka7e24c12009-10-30 11:49:00 +00003634 Smi::FromInt(length),
3635 SKIP_WRITE_BARRIER);
Steve Block44f0eee2011-05-26 01:26:41 +01003636 // Set the callee property for non-strict mode arguments object only.
3637 if (!strict_mode_callee) {
3638 JSObject::cast(result)->InObjectPropertyAtPut(kArgumentsCalleeIndex,
3639 callee);
3640 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003641
3642 // Check the state of the object
3643 ASSERT(JSObject::cast(result)->HasFastProperties());
3644 ASSERT(JSObject::cast(result)->HasFastElements());
3645
3646 return result;
3647}
3648
3649
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003650static bool HasDuplicates(DescriptorArray* descriptors) {
3651 int count = descriptors->number_of_descriptors();
3652 if (count > 1) {
3653 String* prev_key = descriptors->GetKey(0);
3654 for (int i = 1; i != count; i++) {
3655 String* current_key = descriptors->GetKey(i);
3656 if (prev_key == current_key) return true;
3657 prev_key = current_key;
3658 }
3659 }
3660 return false;
3661}
3662
3663
John Reck59135872010-11-02 12:39:01 -07003664MaybeObject* Heap::AllocateInitialMap(JSFunction* fun) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003665 ASSERT(!fun->has_initial_map());
3666
3667 // First create a new map with the size and number of in-object properties
3668 // suggested by the function.
3669 int instance_size = fun->shared()->CalculateInstanceSize();
3670 int in_object_properties = fun->shared()->CalculateInObjectProperties();
John Reck59135872010-11-02 12:39:01 -07003671 Object* map_obj;
Steve Block44f0eee2011-05-26 01:26:41 +01003672 { MaybeObject* maybe_map_obj = AllocateMap(JS_OBJECT_TYPE, instance_size);
John Reck59135872010-11-02 12:39:01 -07003673 if (!maybe_map_obj->ToObject(&map_obj)) return maybe_map_obj;
3674 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003675
3676 // Fetch or allocate prototype.
3677 Object* prototype;
3678 if (fun->has_instance_prototype()) {
3679 prototype = fun->instance_prototype();
3680 } else {
John Reck59135872010-11-02 12:39:01 -07003681 { MaybeObject* maybe_prototype = AllocateFunctionPrototype(fun);
3682 if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype;
3683 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003684 }
3685 Map* map = Map::cast(map_obj);
3686 map->set_inobject_properties(in_object_properties);
3687 map->set_unused_property_fields(in_object_properties);
3688 map->set_prototype(prototype);
Steve Block8defd9f2010-07-08 12:39:36 +01003689 ASSERT(map->has_fast_elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00003690
Andrei Popescu402d9372010-02-26 13:31:12 +00003691 // If the function has only simple this property assignments add
3692 // field descriptors for these to the initial map as the object
3693 // cannot be constructed without having these properties. Guard by
3694 // the inline_new flag so we only change the map if we generate a
3695 // specialized construct stub.
Steve Blocka7e24c12009-10-30 11:49:00 +00003696 ASSERT(in_object_properties <= Map::kMaxPreAllocatedPropertyFields);
Andrei Popescu402d9372010-02-26 13:31:12 +00003697 if (fun->shared()->CanGenerateInlineConstructor(prototype)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003698 int count = fun->shared()->this_property_assignments_count();
3699 if (count > in_object_properties) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003700 // Inline constructor can only handle inobject properties.
3701 fun->shared()->ForbidInlineConstructor();
3702 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003703 DescriptorArray* descriptors;
John Reck59135872010-11-02 12:39:01 -07003704 { MaybeObject* maybe_descriptors_obj = DescriptorArray::Allocate(count);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003705 if (!maybe_descriptors_obj->To<DescriptorArray>(&descriptors)) {
John Reck59135872010-11-02 12:39:01 -07003706 return maybe_descriptors_obj;
3707 }
3708 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003709 DescriptorArray::WhitenessWitness witness(descriptors);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003710 for (int i = 0; i < count; i++) {
3711 String* name = fun->shared()->GetThisPropertyAssignmentName(i);
3712 ASSERT(name->IsSymbol());
3713 FieldDescriptor field(name, i, NONE);
3714 field.SetEnumerationIndex(i);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003715 descriptors->Set(i, &field, witness);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003716 }
3717 descriptors->SetNextEnumerationIndex(count);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003718 descriptors->SortUnchecked(witness);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003719
3720 // The descriptors may contain duplicates because the compiler does not
3721 // guarantee the uniqueness of property names (it would have required
3722 // quadratic time). Once the descriptors are sorted we can check for
3723 // duplicates in linear time.
3724 if (HasDuplicates(descriptors)) {
3725 fun->shared()->ForbidInlineConstructor();
3726 } else {
3727 map->set_instance_descriptors(descriptors);
3728 map->set_pre_allocated_property_fields(count);
3729 map->set_unused_property_fields(in_object_properties - count);
3730 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003731 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003732 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003733
3734 fun->shared()->StartInobjectSlackTracking(map);
3735
Steve Blocka7e24c12009-10-30 11:49:00 +00003736 return map;
3737}
3738
3739
3740void Heap::InitializeJSObjectFromMap(JSObject* obj,
3741 FixedArray* properties,
3742 Map* map) {
3743 obj->set_properties(properties);
3744 obj->initialize_elements();
3745 // TODO(1240798): Initialize the object's body using valid initial values
3746 // according to the object's initial map. For example, if the map's
3747 // instance type is JS_ARRAY_TYPE, the length field should be initialized
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003748 // to a number (e.g. Smi::FromInt(0)) and the elements initialized to a
3749 // fixed array (e.g. Heap::empty_fixed_array()). Currently, the object
Steve Blocka7e24c12009-10-30 11:49:00 +00003750 // verification code has to cope with (temporarily) invalid objects. See
3751 // for example, JSArray::JSArrayVerify).
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003752 Object* filler;
3753 // We cannot always fill with one_pointer_filler_map because objects
3754 // created from API functions expect their internal fields to be initialized
3755 // with undefined_value.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003756 // Pre-allocated fields need to be initialized with undefined_value as well
3757 // so that object accesses before the constructor completes (e.g. in the
3758 // debugger) will not cause a crash.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003759 if (map->constructor()->IsJSFunction() &&
3760 JSFunction::cast(map->constructor())->shared()->
3761 IsInobjectSlackTrackingInProgress()) {
3762 // We might want to shrink the object later.
3763 ASSERT(obj->GetInternalFieldCount() == 0);
3764 filler = Heap::one_pointer_filler_map();
3765 } else {
3766 filler = Heap::undefined_value();
3767 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003768 obj->InitializeBody(map, Heap::undefined_value(), filler);
Steve Blocka7e24c12009-10-30 11:49:00 +00003769}
3770
3771
John Reck59135872010-11-02 12:39:01 -07003772MaybeObject* Heap::AllocateJSObjectFromMap(Map* map, PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003773 // JSFunctions should be allocated using AllocateFunction to be
3774 // properly initialized.
3775 ASSERT(map->instance_type() != JS_FUNCTION_TYPE);
3776
Steve Block8defd9f2010-07-08 12:39:36 +01003777 // Both types of global objects should be allocated using
3778 // AllocateGlobalObject to be properly initialized.
Steve Blocka7e24c12009-10-30 11:49:00 +00003779 ASSERT(map->instance_type() != JS_GLOBAL_OBJECT_TYPE);
3780 ASSERT(map->instance_type() != JS_BUILTINS_OBJECT_TYPE);
3781
3782 // Allocate the backing storage for the properties.
3783 int prop_size =
3784 map->pre_allocated_property_fields() +
3785 map->unused_property_fields() -
3786 map->inobject_properties();
3787 ASSERT(prop_size >= 0);
John Reck59135872010-11-02 12:39:01 -07003788 Object* properties;
3789 { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, pretenure);
3790 if (!maybe_properties->ToObject(&properties)) return maybe_properties;
3791 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003792
3793 // Allocate the JSObject.
3794 AllocationSpace space =
3795 (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003796 if (map->instance_size() > Page::kMaxNonCodeHeapObjectSize) space = LO_SPACE;
John Reck59135872010-11-02 12:39:01 -07003797 Object* obj;
3798 { MaybeObject* maybe_obj = Allocate(map, space);
3799 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3800 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003801
3802 // Initialize the JSObject.
3803 InitializeJSObjectFromMap(JSObject::cast(obj),
3804 FixedArray::cast(properties),
3805 map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003806 ASSERT(JSObject::cast(obj)->HasFastSmiOnlyElements() ||
3807 JSObject::cast(obj)->HasFastElements());
Steve Blocka7e24c12009-10-30 11:49:00 +00003808 return obj;
3809}
3810
3811
John Reck59135872010-11-02 12:39:01 -07003812MaybeObject* Heap::AllocateJSObject(JSFunction* constructor,
3813 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003814 // Allocate the initial map if absent.
3815 if (!constructor->has_initial_map()) {
John Reck59135872010-11-02 12:39:01 -07003816 Object* initial_map;
3817 { MaybeObject* maybe_initial_map = AllocateInitialMap(constructor);
3818 if (!maybe_initial_map->ToObject(&initial_map)) return maybe_initial_map;
3819 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003820 constructor->set_initial_map(Map::cast(initial_map));
3821 Map::cast(initial_map)->set_constructor(constructor);
3822 }
3823 // Allocate the object based on the constructors initial map.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003824 MaybeObject* result = AllocateJSObjectFromMap(
3825 constructor->initial_map(), pretenure);
John Reck59135872010-11-02 12:39:01 -07003826#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003827 // Make sure result is NOT a global object if valid.
John Reck59135872010-11-02 12:39:01 -07003828 Object* non_failure;
3829 ASSERT(!result->ToObject(&non_failure) || !non_failure->IsGlobalObject());
3830#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003831 return result;
3832}
3833
3834
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003835MaybeObject* Heap::AllocateJSArrayAndStorage(
3836 ElementsKind elements_kind,
3837 int length,
3838 int capacity,
3839 ArrayStorageAllocationMode mode,
3840 PretenureFlag pretenure) {
3841 ASSERT(capacity >= length);
3842 MaybeObject* maybe_array = AllocateJSArray(elements_kind, pretenure);
3843 JSArray* array;
3844 if (!maybe_array->To(&array)) return maybe_array;
3845
3846 if (capacity == 0) {
3847 array->set_length(Smi::FromInt(0));
3848 array->set_elements(empty_fixed_array());
3849 return array;
3850 }
3851
3852 FixedArrayBase* elms;
3853 MaybeObject* maybe_elms = NULL;
3854 if (elements_kind == FAST_DOUBLE_ELEMENTS) {
3855 if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
3856 maybe_elms = AllocateUninitializedFixedDoubleArray(capacity);
3857 } else {
3858 ASSERT(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
3859 maybe_elms = AllocateFixedDoubleArrayWithHoles(capacity);
3860 }
3861 } else {
3862 ASSERT(elements_kind == FAST_ELEMENTS ||
3863 elements_kind == FAST_SMI_ONLY_ELEMENTS);
3864 if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) {
3865 maybe_elms = AllocateUninitializedFixedArray(capacity);
3866 } else {
3867 ASSERT(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE);
3868 maybe_elms = AllocateFixedArrayWithHoles(capacity);
3869 }
3870 }
3871 if (!maybe_elms->To(&elms)) return maybe_elms;
3872
3873 array->set_elements(elms);
3874 array->set_length(Smi::FromInt(length));
3875 return array;
3876}
3877
3878
3879MaybeObject* Heap::AllocateJSArrayWithElements(
3880 FixedArrayBase* elements,
3881 ElementsKind elements_kind,
3882 PretenureFlag pretenure) {
3883 MaybeObject* maybe_array = AllocateJSArray(elements_kind, pretenure);
3884 JSArray* array;
3885 if (!maybe_array->To(&array)) return maybe_array;
3886
3887 array->set_elements(elements);
3888 array->set_length(Smi::FromInt(elements->length()));
3889 return array;
3890}
3891
3892
Ben Murdoch257744e2011-11-30 15:57:28 +00003893MaybeObject* Heap::AllocateJSProxy(Object* handler, Object* prototype) {
3894 // Allocate map.
3895 // TODO(rossberg): Once we optimize proxies, think about a scheme to share
3896 // maps. Will probably depend on the identity of the handler object, too.
3897 Map* map;
3898 MaybeObject* maybe_map_obj = AllocateMap(JS_PROXY_TYPE, JSProxy::kSize);
3899 if (!maybe_map_obj->To<Map>(&map)) return maybe_map_obj;
3900 map->set_prototype(prototype);
Ben Murdoch257744e2011-11-30 15:57:28 +00003901
3902 // Allocate the proxy object.
Ben Murdoch589d6972011-11-30 16:04:58 +00003903 JSProxy* result;
Ben Murdoch257744e2011-11-30 15:57:28 +00003904 MaybeObject* maybe_result = Allocate(map, NEW_SPACE);
Ben Murdoch589d6972011-11-30 16:04:58 +00003905 if (!maybe_result->To<JSProxy>(&result)) return maybe_result;
3906 result->InitializeBody(map->instance_size(), Smi::FromInt(0));
3907 result->set_handler(handler);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003908 result->set_hash(undefined_value(), SKIP_WRITE_BARRIER);
Ben Murdoch589d6972011-11-30 16:04:58 +00003909 return result;
3910}
3911
3912
3913MaybeObject* Heap::AllocateJSFunctionProxy(Object* handler,
3914 Object* call_trap,
3915 Object* construct_trap,
3916 Object* prototype) {
3917 // Allocate map.
3918 // TODO(rossberg): Once we optimize proxies, think about a scheme to share
3919 // maps. Will probably depend on the identity of the handler object, too.
3920 Map* map;
3921 MaybeObject* maybe_map_obj =
3922 AllocateMap(JS_FUNCTION_PROXY_TYPE, JSFunctionProxy::kSize);
3923 if (!maybe_map_obj->To<Map>(&map)) return maybe_map_obj;
3924 map->set_prototype(prototype);
3925
3926 // Allocate the proxy object.
3927 JSFunctionProxy* result;
3928 MaybeObject* maybe_result = Allocate(map, NEW_SPACE);
3929 if (!maybe_result->To<JSFunctionProxy>(&result)) return maybe_result;
3930 result->InitializeBody(map->instance_size(), Smi::FromInt(0));
3931 result->set_handler(handler);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003932 result->set_hash(undefined_value(), SKIP_WRITE_BARRIER);
Ben Murdoch589d6972011-11-30 16:04:58 +00003933 result->set_call_trap(call_trap);
3934 result->set_construct_trap(construct_trap);
Ben Murdoch257744e2011-11-30 15:57:28 +00003935 return result;
3936}
3937
3938
John Reck59135872010-11-02 12:39:01 -07003939MaybeObject* Heap::AllocateGlobalObject(JSFunction* constructor) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003940 ASSERT(constructor->has_initial_map());
3941 Map* map = constructor->initial_map();
3942
3943 // Make sure no field properties are described in the initial map.
3944 // This guarantees us that normalizing the properties does not
3945 // require us to change property values to JSGlobalPropertyCells.
3946 ASSERT(map->NextFreePropertyIndex() == 0);
3947
3948 // Make sure we don't have a ton of pre-allocated slots in the
3949 // global objects. They will be unused once we normalize the object.
3950 ASSERT(map->unused_property_fields() == 0);
3951 ASSERT(map->inobject_properties() == 0);
3952
3953 // Initial size of the backing store to avoid resize of the storage during
3954 // bootstrapping. The size differs between the JS global object ad the
3955 // builtins object.
3956 int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512;
3957
3958 // Allocate a dictionary object for backing storage.
John Reck59135872010-11-02 12:39:01 -07003959 Object* obj;
3960 { MaybeObject* maybe_obj =
3961 StringDictionary::Allocate(
3962 map->NumberOfDescribedProperties() * 2 + initial_size);
3963 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3964 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003965 StringDictionary* dictionary = StringDictionary::cast(obj);
3966
3967 // The global object might be created from an object template with accessors.
3968 // Fill these accessors into the dictionary.
3969 DescriptorArray* descs = map->instance_descriptors();
3970 for (int i = 0; i < descs->number_of_descriptors(); i++) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003971 PropertyDetails details(descs->GetDetails(i));
Steve Blocka7e24c12009-10-30 11:49:00 +00003972 ASSERT(details.type() == CALLBACKS); // Only accessors are expected.
3973 PropertyDetails d =
3974 PropertyDetails(details.attributes(), CALLBACKS, details.index());
3975 Object* value = descs->GetCallbacksObject(i);
Steve Block44f0eee2011-05-26 01:26:41 +01003976 { MaybeObject* maybe_value = AllocateJSGlobalPropertyCell(value);
John Reck59135872010-11-02 12:39:01 -07003977 if (!maybe_value->ToObject(&value)) return maybe_value;
3978 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003979
John Reck59135872010-11-02 12:39:01 -07003980 Object* result;
3981 { MaybeObject* maybe_result = dictionary->Add(descs->GetKey(i), value, d);
3982 if (!maybe_result->ToObject(&result)) return maybe_result;
3983 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003984 dictionary = StringDictionary::cast(result);
3985 }
3986
3987 // Allocate the global object and initialize it with the backing store.
John Reck59135872010-11-02 12:39:01 -07003988 { MaybeObject* maybe_obj = Allocate(map, OLD_POINTER_SPACE);
3989 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3990 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003991 JSObject* global = JSObject::cast(obj);
3992 InitializeJSObjectFromMap(global, dictionary, map);
3993
3994 // Create a new map for the global object.
John Reck59135872010-11-02 12:39:01 -07003995 { MaybeObject* maybe_obj = map->CopyDropDescriptors();
3996 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
3997 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003998 Map* new_map = Map::cast(obj);
3999
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004000 // Set up the global object as a normalized object.
Steve Blocka7e24c12009-10-30 11:49:00 +00004001 global->set_map(new_map);
Ben Murdoch257744e2011-11-30 15:57:28 +00004002 global->map()->clear_instance_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00004003 global->set_properties(dictionary);
4004
4005 // Make sure result is a global object with properties in dictionary.
4006 ASSERT(global->IsGlobalObject());
4007 ASSERT(!global->HasFastProperties());
4008 return global;
4009}
4010
4011
John Reck59135872010-11-02 12:39:01 -07004012MaybeObject* Heap::CopyJSObject(JSObject* source) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004013 // Never used to copy functions. If functions need to be copied we
4014 // have to be careful to clear the literals array.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004015 SLOW_ASSERT(!source->IsJSFunction());
Steve Blocka7e24c12009-10-30 11:49:00 +00004016
4017 // Make the clone.
4018 Map* map = source->map();
4019 int object_size = map->instance_size();
4020 Object* clone;
4021
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004022 WriteBarrierMode wb_mode = UPDATE_WRITE_BARRIER;
4023
Steve Blocka7e24c12009-10-30 11:49:00 +00004024 // If we're forced to always allocate, we use the general allocation
4025 // functions which may leave us with an object in old space.
4026 if (always_allocate()) {
John Reck59135872010-11-02 12:39:01 -07004027 { MaybeObject* maybe_clone =
4028 AllocateRaw(object_size, NEW_SPACE, OLD_POINTER_SPACE);
4029 if (!maybe_clone->ToObject(&clone)) return maybe_clone;
4030 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004031 Address clone_address = HeapObject::cast(clone)->address();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004032 CopyBlock(clone_address,
4033 source->address(),
Steve Blocka7e24c12009-10-30 11:49:00 +00004034 object_size);
4035 // Update write barrier for all fields that lie beyond the header.
Steve Block6ded16b2010-05-10 14:33:55 +01004036 RecordWrites(clone_address,
4037 JSObject::kHeaderSize,
4038 (object_size - JSObject::kHeaderSize) / kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004039 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004040 wb_mode = SKIP_WRITE_BARRIER;
John Reck59135872010-11-02 12:39:01 -07004041 { MaybeObject* maybe_clone = new_space_.AllocateRaw(object_size);
4042 if (!maybe_clone->ToObject(&clone)) return maybe_clone;
4043 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004044 SLOW_ASSERT(InNewSpace(clone));
Steve Blocka7e24c12009-10-30 11:49:00 +00004045 // Since we know the clone is allocated in new space, we can copy
4046 // the contents without worrying about updating the write barrier.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004047 CopyBlock(HeapObject::cast(clone)->address(),
4048 source->address(),
Steve Blocka7e24c12009-10-30 11:49:00 +00004049 object_size);
4050 }
4051
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004052 SLOW_ASSERT(
4053 JSObject::cast(clone)->GetElementsKind() == source->GetElementsKind());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004054 FixedArrayBase* elements = FixedArrayBase::cast(source->elements());
Steve Blocka7e24c12009-10-30 11:49:00 +00004055 FixedArray* properties = FixedArray::cast(source->properties());
4056 // Update elements if necessary.
Steve Block6ded16b2010-05-10 14:33:55 +01004057 if (elements->length() > 0) {
John Reck59135872010-11-02 12:39:01 -07004058 Object* elem;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004059 { MaybeObject* maybe_elem;
4060 if (elements->map() == fixed_cow_array_map()) {
4061 maybe_elem = FixedArray::cast(elements);
4062 } else if (source->HasFastDoubleElements()) {
4063 maybe_elem = CopyFixedDoubleArray(FixedDoubleArray::cast(elements));
4064 } else {
4065 maybe_elem = CopyFixedArray(FixedArray::cast(elements));
4066 }
John Reck59135872010-11-02 12:39:01 -07004067 if (!maybe_elem->ToObject(&elem)) return maybe_elem;
4068 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004069 JSObject::cast(clone)->set_elements(FixedArrayBase::cast(elem), wb_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00004070 }
4071 // Update properties if necessary.
4072 if (properties->length() > 0) {
John Reck59135872010-11-02 12:39:01 -07004073 Object* prop;
4074 { MaybeObject* maybe_prop = CopyFixedArray(properties);
4075 if (!maybe_prop->ToObject(&prop)) return maybe_prop;
4076 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004077 JSObject::cast(clone)->set_properties(FixedArray::cast(prop), wb_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00004078 }
4079 // Return the new clone.
4080 return clone;
4081}
4082
4083
Ben Murdoch589d6972011-11-30 16:04:58 +00004084MaybeObject* Heap::ReinitializeJSReceiver(
4085 JSReceiver* object, InstanceType type, int size) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004086 ASSERT(type >= FIRST_JS_OBJECT_TYPE);
Ben Murdoch589d6972011-11-30 16:04:58 +00004087
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004088 // Allocate fresh map.
4089 // TODO(rossberg): Once we optimize proxies, cache these maps.
4090 Map* map;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004091 MaybeObject* maybe = AllocateMap(type, size);
4092 if (!maybe->To<Map>(&map)) return maybe;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004093
Ben Murdoch589d6972011-11-30 16:04:58 +00004094 // Check that the receiver has at least the size of the fresh object.
4095 int size_difference = object->map()->instance_size() - map->instance_size();
4096 ASSERT(size_difference >= 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004097
4098 map->set_prototype(object->map()->prototype());
4099
4100 // Allocate the backing storage for the properties.
4101 int prop_size = map->unused_property_fields() - map->inobject_properties();
4102 Object* properties;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004103 maybe = AllocateFixedArray(prop_size, TENURED);
4104 if (!maybe->ToObject(&properties)) return maybe;
4105
4106 // Functions require some allocation, which might fail here.
4107 SharedFunctionInfo* shared = NULL;
4108 if (type == JS_FUNCTION_TYPE) {
4109 String* name;
4110 maybe = LookupAsciiSymbol("<freezing call trap>");
4111 if (!maybe->To<String>(&name)) return maybe;
4112 maybe = AllocateSharedFunctionInfo(name);
4113 if (!maybe->To<SharedFunctionInfo>(&shared)) return maybe;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004114 }
4115
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004116 // Because of possible retries of this function after failure,
4117 // we must NOT fail after this point, where we have changed the type!
4118
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004119 // Reset the map for the object.
4120 object->set_map(map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004121 JSObject* jsobj = JSObject::cast(object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004122
4123 // Reinitialize the object from the constructor map.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004124 InitializeJSObjectFromMap(jsobj, FixedArray::cast(properties), map);
Ben Murdoch589d6972011-11-30 16:04:58 +00004125
4126 // Functions require some minimal initialization.
4127 if (type == JS_FUNCTION_TYPE) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004128 map->set_function_with_prototype(true);
4129 InitializeFunction(JSFunction::cast(object), shared, the_hole_value());
4130 JSFunction::cast(object)->set_context(
4131 isolate()->context()->global_context());
Ben Murdoch589d6972011-11-30 16:04:58 +00004132 }
4133
4134 // Put in filler if the new object is smaller than the old.
4135 if (size_difference > 0) {
4136 CreateFillerObjectAt(
4137 object->address() + map->instance_size(), size_difference);
4138 }
4139
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004140 return object;
4141}
4142
4143
John Reck59135872010-11-02 12:39:01 -07004144MaybeObject* Heap::ReinitializeJSGlobalProxy(JSFunction* constructor,
4145 JSGlobalProxy* object) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004146 ASSERT(constructor->has_initial_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00004147 Map* map = constructor->initial_map();
4148
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004149 // Check that the already allocated object has the same size and type as
Steve Blocka7e24c12009-10-30 11:49:00 +00004150 // objects allocated using the constructor.
4151 ASSERT(map->instance_size() == object->map()->instance_size());
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004152 ASSERT(map->instance_type() == object->map()->instance_type());
Steve Blocka7e24c12009-10-30 11:49:00 +00004153
4154 // Allocate the backing storage for the properties.
4155 int prop_size = map->unused_property_fields() - map->inobject_properties();
John Reck59135872010-11-02 12:39:01 -07004156 Object* properties;
4157 { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, TENURED);
4158 if (!maybe_properties->ToObject(&properties)) return maybe_properties;
4159 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004160
4161 // Reset the map for the object.
4162 object->set_map(constructor->initial_map());
4163
4164 // Reinitialize the object from the constructor map.
4165 InitializeJSObjectFromMap(object, FixedArray::cast(properties), map);
4166 return object;
4167}
4168
4169
John Reck59135872010-11-02 12:39:01 -07004170MaybeObject* Heap::AllocateStringFromAscii(Vector<const char> string,
4171 PretenureFlag pretenure) {
Ben Murdoch589d6972011-11-30 16:04:58 +00004172 if (string.length() == 1) {
4173 return Heap::LookupSingleCharacterStringFromCode(string[0]);
4174 }
John Reck59135872010-11-02 12:39:01 -07004175 Object* result;
4176 { MaybeObject* maybe_result =
4177 AllocateRawAsciiString(string.length(), pretenure);
4178 if (!maybe_result->ToObject(&result)) return maybe_result;
4179 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004180
4181 // Copy the characters into the new object.
4182 SeqAsciiString* string_result = SeqAsciiString::cast(result);
4183 for (int i = 0; i < string.length(); i++) {
4184 string_result->SeqAsciiStringSet(i, string[i]);
4185 }
4186 return result;
4187}
4188
4189
Steve Block9fac8402011-05-12 15:51:54 +01004190MaybeObject* Heap::AllocateStringFromUtf8Slow(Vector<const char> string,
4191 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004192 // Count the number of characters in the UTF-8 string and check if
4193 // it is an ASCII string.
Ben Murdoch8b112d22011-06-08 16:22:53 +01004194 Access<UnicodeCache::Utf8Decoder>
4195 decoder(isolate_->unicode_cache()->utf8_decoder());
Steve Blocka7e24c12009-10-30 11:49:00 +00004196 decoder->Reset(string.start(), string.length());
4197 int chars = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00004198 while (decoder->has_more()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004199 uint32_t r = decoder->GetNext();
4200 if (r <= unibrow::Utf16::kMaxNonSurrogateCharCode) {
4201 chars++;
4202 } else {
4203 chars += 2;
4204 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004205 }
4206
John Reck59135872010-11-02 12:39:01 -07004207 Object* result;
4208 { MaybeObject* maybe_result = AllocateRawTwoByteString(chars, pretenure);
4209 if (!maybe_result->ToObject(&result)) return maybe_result;
4210 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004211
4212 // Convert and copy the characters into the new object.
4213 String* string_result = String::cast(result);
4214 decoder->Reset(string.start(), string.length());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004215 int i = 0;
4216 while (i < chars) {
4217 uint32_t r = decoder->GetNext();
4218 if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) {
4219 string_result->Set(i++, unibrow::Utf16::LeadSurrogate(r));
4220 string_result->Set(i++, unibrow::Utf16::TrailSurrogate(r));
4221 } else {
4222 string_result->Set(i++, r);
4223 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004224 }
4225 return result;
4226}
4227
4228
John Reck59135872010-11-02 12:39:01 -07004229MaybeObject* Heap::AllocateStringFromTwoByte(Vector<const uc16> string,
4230 PretenureFlag pretenure) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004231 // Check if the string is an ASCII string.
John Reck59135872010-11-02 12:39:01 -07004232 MaybeObject* maybe_result;
Steve Block9fac8402011-05-12 15:51:54 +01004233 if (String::IsAscii(string.start(), string.length())) {
John Reck59135872010-11-02 12:39:01 -07004234 maybe_result = AllocateRawAsciiString(string.length(), pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +00004235 } else { // It's not an ASCII string.
John Reck59135872010-11-02 12:39:01 -07004236 maybe_result = AllocateRawTwoByteString(string.length(), pretenure);
Steve Blocka7e24c12009-10-30 11:49:00 +00004237 }
John Reck59135872010-11-02 12:39:01 -07004238 Object* result;
4239 if (!maybe_result->ToObject(&result)) return maybe_result;
Steve Blocka7e24c12009-10-30 11:49:00 +00004240
4241 // Copy the characters into the new object, which may be either ASCII or
4242 // UTF-16.
4243 String* string_result = String::cast(result);
4244 for (int i = 0; i < string.length(); i++) {
4245 string_result->Set(i, string[i]);
4246 }
4247 return result;
4248}
4249
4250
4251Map* Heap::SymbolMapForString(String* string) {
4252 // If the string is in new space it cannot be used as a symbol.
4253 if (InNewSpace(string)) return NULL;
4254
4255 // Find the corresponding symbol map for strings.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004256 switch (string->map()->instance_type()) {
4257 case STRING_TYPE: return symbol_map();
4258 case ASCII_STRING_TYPE: return ascii_symbol_map();
4259 case CONS_STRING_TYPE: return cons_symbol_map();
4260 case CONS_ASCII_STRING_TYPE: return cons_ascii_symbol_map();
4261 case EXTERNAL_STRING_TYPE: return external_symbol_map();
4262 case EXTERNAL_ASCII_STRING_TYPE: return external_ascii_symbol_map();
4263 case EXTERNAL_STRING_WITH_ASCII_DATA_TYPE:
4264 return external_symbol_with_ascii_data_map();
4265 case SHORT_EXTERNAL_STRING_TYPE: return short_external_symbol_map();
4266 case SHORT_EXTERNAL_ASCII_STRING_TYPE:
4267 return short_external_ascii_symbol_map();
4268 case SHORT_EXTERNAL_STRING_WITH_ASCII_DATA_TYPE:
4269 return short_external_symbol_with_ascii_data_map();
4270 default: return NULL; // No match found.
Steve Block44f0eee2011-05-26 01:26:41 +01004271 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004272}
4273
4274
John Reck59135872010-11-02 12:39:01 -07004275MaybeObject* Heap::AllocateInternalSymbol(unibrow::CharacterStream* buffer,
4276 int chars,
4277 uint32_t hash_field) {
Leon Clarkee46be812010-01-19 14:06:41 +00004278 ASSERT(chars >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00004279 // Ensure the chars matches the number of characters in the buffer.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004280 ASSERT(static_cast<unsigned>(chars) == buffer->Utf16Length());
4281 // Determine whether the string is ASCII.
Steve Blocka7e24c12009-10-30 11:49:00 +00004282 bool is_ascii = true;
Leon Clarkee46be812010-01-19 14:06:41 +00004283 while (buffer->has_more()) {
4284 if (buffer->GetNext() > unibrow::Utf8::kMaxOneByteChar) {
4285 is_ascii = false;
4286 break;
4287 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004288 }
4289 buffer->Rewind();
4290
4291 // Compute map and object size.
4292 int size;
4293 Map* map;
4294
4295 if (is_ascii) {
Leon Clarkee46be812010-01-19 14:06:41 +00004296 if (chars > SeqAsciiString::kMaxLength) {
4297 return Failure::OutOfMemoryException();
4298 }
Steve Blockd0582a62009-12-15 09:54:21 +00004299 map = ascii_symbol_map();
Steve Blocka7e24c12009-10-30 11:49:00 +00004300 size = SeqAsciiString::SizeFor(chars);
4301 } else {
Leon Clarkee46be812010-01-19 14:06:41 +00004302 if (chars > SeqTwoByteString::kMaxLength) {
4303 return Failure::OutOfMemoryException();
4304 }
Steve Blockd0582a62009-12-15 09:54:21 +00004305 map = symbol_map();
Steve Blocka7e24c12009-10-30 11:49:00 +00004306 size = SeqTwoByteString::SizeFor(chars);
4307 }
4308
4309 // Allocate string.
John Reck59135872010-11-02 12:39:01 -07004310 Object* result;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004311 { MaybeObject* maybe_result = (size > Page::kMaxNonCodeHeapObjectSize)
4312 ? lo_space_->AllocateRaw(size, NOT_EXECUTABLE)
John Reck59135872010-11-02 12:39:01 -07004313 : old_data_space_->AllocateRaw(size);
4314 if (!maybe_result->ToObject(&result)) return maybe_result;
4315 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004316
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004317 reinterpret_cast<HeapObject*>(result)->set_map_no_write_barrier(map);
Steve Blockd0582a62009-12-15 09:54:21 +00004318 // Set length and hash fields of the allocated string.
Steve Blocka7e24c12009-10-30 11:49:00 +00004319 String* answer = String::cast(result);
Steve Blockd0582a62009-12-15 09:54:21 +00004320 answer->set_length(chars);
4321 answer->set_hash_field(hash_field);
Steve Blocka7e24c12009-10-30 11:49:00 +00004322
4323 ASSERT_EQ(size, answer->Size());
4324
4325 // Fill in the characters.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004326 int i = 0;
4327 while (i < chars) {
4328 uint32_t character = buffer->GetNext();
4329 if (character > unibrow::Utf16::kMaxNonSurrogateCharCode) {
4330 answer->Set(i++, unibrow::Utf16::LeadSurrogate(character));
4331 answer->Set(i++, unibrow::Utf16::TrailSurrogate(character));
4332 } else {
4333 answer->Set(i++, character);
4334 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004335 }
4336 return answer;
4337}
4338
4339
John Reck59135872010-11-02 12:39:01 -07004340MaybeObject* Heap::AllocateRawAsciiString(int length, PretenureFlag pretenure) {
Leon Clarkee46be812010-01-19 14:06:41 +00004341 if (length < 0 || length > SeqAsciiString::kMaxLength) {
4342 return Failure::OutOfMemoryException();
4343 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004344
4345 int size = SeqAsciiString::SizeFor(length);
Leon Clarkee46be812010-01-19 14:06:41 +00004346 ASSERT(size <= SeqAsciiString::kMaxSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004347
Leon Clarkee46be812010-01-19 14:06:41 +00004348 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
4349 AllocationSpace retry_space = OLD_DATA_SPACE;
4350
Steve Blocka7e24c12009-10-30 11:49:00 +00004351 if (space == NEW_SPACE) {
Leon Clarkee46be812010-01-19 14:06:41 +00004352 if (size > kMaxObjectSizeInNewSpace) {
4353 // Allocate in large object space, retry space will be ignored.
4354 space = LO_SPACE;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004355 } else if (size > Page::kMaxNonCodeHeapObjectSize) {
Leon Clarkee46be812010-01-19 14:06:41 +00004356 // Allocate in new space, retry in large object space.
4357 retry_space = LO_SPACE;
4358 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004359 } else if (space == OLD_DATA_SPACE &&
4360 size > Page::kMaxNonCodeHeapObjectSize) {
Leon Clarkee46be812010-01-19 14:06:41 +00004361 space = LO_SPACE;
Steve Blocka7e24c12009-10-30 11:49:00 +00004362 }
John Reck59135872010-11-02 12:39:01 -07004363 Object* result;
4364 { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space);
4365 if (!maybe_result->ToObject(&result)) return maybe_result;
4366 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004367
Steve Blocka7e24c12009-10-30 11:49:00 +00004368 // Partially initialize the object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004369 HeapObject::cast(result)->set_map_no_write_barrier(ascii_string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00004370 String::cast(result)->set_length(length);
Steve Blockd0582a62009-12-15 09:54:21 +00004371 String::cast(result)->set_hash_field(String::kEmptyHashField);
Steve Blocka7e24c12009-10-30 11:49:00 +00004372 ASSERT_EQ(size, HeapObject::cast(result)->Size());
4373 return result;
4374}
4375
4376
John Reck59135872010-11-02 12:39:01 -07004377MaybeObject* Heap::AllocateRawTwoByteString(int length,
4378 PretenureFlag pretenure) {
Leon Clarkee46be812010-01-19 14:06:41 +00004379 if (length < 0 || length > SeqTwoByteString::kMaxLength) {
4380 return Failure::OutOfMemoryException();
Steve Blocka7e24c12009-10-30 11:49:00 +00004381 }
Leon Clarkee46be812010-01-19 14:06:41 +00004382 int size = SeqTwoByteString::SizeFor(length);
4383 ASSERT(size <= SeqTwoByteString::kMaxSize);
4384 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
4385 AllocationSpace retry_space = OLD_DATA_SPACE;
4386
4387 if (space == NEW_SPACE) {
4388 if (size > kMaxObjectSizeInNewSpace) {
4389 // Allocate in large object space, retry space will be ignored.
4390 space = LO_SPACE;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004391 } else if (size > Page::kMaxNonCodeHeapObjectSize) {
Leon Clarkee46be812010-01-19 14:06:41 +00004392 // Allocate in new space, retry in large object space.
4393 retry_space = LO_SPACE;
4394 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004395 } else if (space == OLD_DATA_SPACE &&
4396 size > Page::kMaxNonCodeHeapObjectSize) {
Leon Clarkee46be812010-01-19 14:06:41 +00004397 space = LO_SPACE;
4398 }
John Reck59135872010-11-02 12:39:01 -07004399 Object* result;
4400 { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space);
4401 if (!maybe_result->ToObject(&result)) return maybe_result;
4402 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004403
Steve Blocka7e24c12009-10-30 11:49:00 +00004404 // Partially initialize the object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004405 HeapObject::cast(result)->set_map_no_write_barrier(string_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00004406 String::cast(result)->set_length(length);
Steve Blockd0582a62009-12-15 09:54:21 +00004407 String::cast(result)->set_hash_field(String::kEmptyHashField);
Steve Blocka7e24c12009-10-30 11:49:00 +00004408 ASSERT_EQ(size, HeapObject::cast(result)->Size());
4409 return result;
4410}
4411
4412
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004413MaybeObject* Heap::AllocateJSArray(
4414 ElementsKind elements_kind,
4415 PretenureFlag pretenure) {
4416 Context* global_context = isolate()->context()->global_context();
4417 JSFunction* array_function = global_context->array_function();
4418 Map* map = array_function->initial_map();
4419 if (elements_kind == FAST_DOUBLE_ELEMENTS) {
4420 map = Map::cast(global_context->double_js_array_map());
4421 } else if (elements_kind == FAST_ELEMENTS || !FLAG_smi_only_arrays) {
4422 map = Map::cast(global_context->object_js_array_map());
4423 } else {
4424 ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS);
4425 ASSERT(map == global_context->smi_js_array_map());
4426 }
4427
4428 return AllocateJSObjectFromMap(map, pretenure);
4429}
4430
4431
John Reck59135872010-11-02 12:39:01 -07004432MaybeObject* Heap::AllocateEmptyFixedArray() {
Steve Blocka7e24c12009-10-30 11:49:00 +00004433 int size = FixedArray::SizeFor(0);
John Reck59135872010-11-02 12:39:01 -07004434 Object* result;
4435 { MaybeObject* maybe_result =
4436 AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE);
4437 if (!maybe_result->ToObject(&result)) return maybe_result;
4438 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004439 // Initialize the object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004440 reinterpret_cast<FixedArray*>(result)->set_map_no_write_barrier(
4441 fixed_array_map());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004442 reinterpret_cast<FixedArray*>(result)->set_length(0);
Steve Blocka7e24c12009-10-30 11:49:00 +00004443 return result;
4444}
4445
4446
John Reck59135872010-11-02 12:39:01 -07004447MaybeObject* Heap::AllocateRawFixedArray(int length) {
Leon Clarkee46be812010-01-19 14:06:41 +00004448 if (length < 0 || length > FixedArray::kMaxLength) {
4449 return Failure::OutOfMemoryException();
4450 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004451 ASSERT(length > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00004452 // Use the general function if we're forced to always allocate.
4453 if (always_allocate()) return AllocateFixedArray(length, TENURED);
4454 // Allocate the raw data for a fixed array.
4455 int size = FixedArray::SizeFor(length);
4456 return size <= kMaxObjectSizeInNewSpace
4457 ? new_space_.AllocateRaw(size)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004458 : lo_space_->AllocateRaw(size, NOT_EXECUTABLE);
Steve Blocka7e24c12009-10-30 11:49:00 +00004459}
4460
4461
John Reck59135872010-11-02 12:39:01 -07004462MaybeObject* Heap::CopyFixedArrayWithMap(FixedArray* src, Map* map) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004463 int len = src->length();
John Reck59135872010-11-02 12:39:01 -07004464 Object* obj;
4465 { MaybeObject* maybe_obj = AllocateRawFixedArray(len);
4466 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
4467 }
Steve Block44f0eee2011-05-26 01:26:41 +01004468 if (InNewSpace(obj)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004469 HeapObject* dst = HeapObject::cast(obj);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004470 dst->set_map_no_write_barrier(map);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004471 CopyBlock(dst->address() + kPointerSize,
4472 src->address() + kPointerSize,
4473 FixedArray::SizeFor(len) - kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004474 return obj;
4475 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004476 HeapObject::cast(obj)->set_map_no_write_barrier(map);
Steve Blocka7e24c12009-10-30 11:49:00 +00004477 FixedArray* result = FixedArray::cast(obj);
4478 result->set_length(len);
Leon Clarke4515c472010-02-03 11:58:03 +00004479
Steve Blocka7e24c12009-10-30 11:49:00 +00004480 // Copy the content
Leon Clarke4515c472010-02-03 11:58:03 +00004481 AssertNoAllocation no_gc;
4482 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
Steve Blocka7e24c12009-10-30 11:49:00 +00004483 for (int i = 0; i < len; i++) result->set(i, src->get(i), mode);
4484 return result;
4485}
4486
4487
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004488MaybeObject* Heap::CopyFixedDoubleArrayWithMap(FixedDoubleArray* src,
4489 Map* map) {
4490 int len = src->length();
4491 Object* obj;
4492 { MaybeObject* maybe_obj = AllocateRawFixedDoubleArray(len, NOT_TENURED);
4493 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
4494 }
4495 HeapObject* dst = HeapObject::cast(obj);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004496 dst->set_map_no_write_barrier(map);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004497 CopyBlock(
4498 dst->address() + FixedDoubleArray::kLengthOffset,
4499 src->address() + FixedDoubleArray::kLengthOffset,
4500 FixedDoubleArray::SizeFor(len) - FixedDoubleArray::kLengthOffset);
4501 return obj;
4502}
4503
4504
John Reck59135872010-11-02 12:39:01 -07004505MaybeObject* Heap::AllocateFixedArray(int length) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004506 ASSERT(length >= 0);
4507 if (length == 0) return empty_fixed_array();
John Reck59135872010-11-02 12:39:01 -07004508 Object* result;
4509 { MaybeObject* maybe_result = AllocateRawFixedArray(length);
4510 if (!maybe_result->ToObject(&result)) return maybe_result;
Steve Blocka7e24c12009-10-30 11:49:00 +00004511 }
John Reck59135872010-11-02 12:39:01 -07004512 // Initialize header.
4513 FixedArray* array = reinterpret_cast<FixedArray*>(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004514 array->set_map_no_write_barrier(fixed_array_map());
John Reck59135872010-11-02 12:39:01 -07004515 array->set_length(length);
4516 // Initialize body.
Steve Block44f0eee2011-05-26 01:26:41 +01004517 ASSERT(!InNewSpace(undefined_value()));
John Reck59135872010-11-02 12:39:01 -07004518 MemsetPointer(array->data_start(), undefined_value(), length);
Steve Blocka7e24c12009-10-30 11:49:00 +00004519 return result;
4520}
4521
4522
John Reck59135872010-11-02 12:39:01 -07004523MaybeObject* Heap::AllocateRawFixedArray(int length, PretenureFlag pretenure) {
Leon Clarkee46be812010-01-19 14:06:41 +00004524 if (length < 0 || length > FixedArray::kMaxLength) {
4525 return Failure::OutOfMemoryException();
4526 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004527
Leon Clarkee46be812010-01-19 14:06:41 +00004528 AllocationSpace space =
4529 (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE;
Steve Blocka7e24c12009-10-30 11:49:00 +00004530 int size = FixedArray::SizeFor(length);
Leon Clarkee46be812010-01-19 14:06:41 +00004531 if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) {
4532 // Too big for new space.
4533 space = LO_SPACE;
4534 } else if (space == OLD_POINTER_SPACE &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004535 size > Page::kMaxNonCodeHeapObjectSize) {
Leon Clarkee46be812010-01-19 14:06:41 +00004536 // Too big for old pointer space.
4537 space = LO_SPACE;
4538 }
4539
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004540 AllocationSpace retry_space =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004541 (size <= Page::kMaxNonCodeHeapObjectSize) ? OLD_POINTER_SPACE : LO_SPACE;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004542
4543 return AllocateRaw(size, space, retry_space);
Steve Blocka7e24c12009-10-30 11:49:00 +00004544}
4545
4546
John Reck59135872010-11-02 12:39:01 -07004547MUST_USE_RESULT static MaybeObject* AllocateFixedArrayWithFiller(
Steve Block44f0eee2011-05-26 01:26:41 +01004548 Heap* heap,
John Reck59135872010-11-02 12:39:01 -07004549 int length,
4550 PretenureFlag pretenure,
4551 Object* filler) {
Steve Block6ded16b2010-05-10 14:33:55 +01004552 ASSERT(length >= 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004553 ASSERT(heap->empty_fixed_array()->IsFixedArray());
4554 if (length == 0) return heap->empty_fixed_array();
Steve Block6ded16b2010-05-10 14:33:55 +01004555
Steve Block44f0eee2011-05-26 01:26:41 +01004556 ASSERT(!heap->InNewSpace(filler));
John Reck59135872010-11-02 12:39:01 -07004557 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01004558 { MaybeObject* maybe_result = heap->AllocateRawFixedArray(length, pretenure);
John Reck59135872010-11-02 12:39:01 -07004559 if (!maybe_result->ToObject(&result)) return maybe_result;
4560 }
Steve Block6ded16b2010-05-10 14:33:55 +01004561
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004562 HeapObject::cast(result)->set_map_no_write_barrier(heap->fixed_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +01004563 FixedArray* array = FixedArray::cast(result);
4564 array->set_length(length);
4565 MemsetPointer(array->data_start(), filler, length);
4566 return array;
4567}
4568
4569
John Reck59135872010-11-02 12:39:01 -07004570MaybeObject* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +01004571 return AllocateFixedArrayWithFiller(this,
4572 length,
4573 pretenure,
4574 undefined_value());
Steve Block6ded16b2010-05-10 14:33:55 +01004575}
4576
4577
John Reck59135872010-11-02 12:39:01 -07004578MaybeObject* Heap::AllocateFixedArrayWithHoles(int length,
4579 PretenureFlag pretenure) {
Steve Block44f0eee2011-05-26 01:26:41 +01004580 return AllocateFixedArrayWithFiller(this,
4581 length,
4582 pretenure,
4583 the_hole_value());
Steve Block6ded16b2010-05-10 14:33:55 +01004584}
4585
4586
John Reck59135872010-11-02 12:39:01 -07004587MaybeObject* Heap::AllocateUninitializedFixedArray(int length) {
Steve Block6ded16b2010-05-10 14:33:55 +01004588 if (length == 0) return empty_fixed_array();
4589
John Reck59135872010-11-02 12:39:01 -07004590 Object* obj;
4591 { MaybeObject* maybe_obj = AllocateRawFixedArray(length);
4592 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
4593 }
Steve Block6ded16b2010-05-10 14:33:55 +01004594
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004595 reinterpret_cast<FixedArray*>(obj)->set_map_no_write_barrier(
4596 fixed_array_map());
Steve Block6ded16b2010-05-10 14:33:55 +01004597 FixedArray::cast(obj)->set_length(length);
4598 return obj;
4599}
4600
4601
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004602MaybeObject* Heap::AllocateEmptyFixedDoubleArray() {
4603 int size = FixedDoubleArray::SizeFor(0);
4604 Object* result;
4605 { MaybeObject* maybe_result =
4606 AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE);
4607 if (!maybe_result->ToObject(&result)) return maybe_result;
4608 }
4609 // Initialize the object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004610 reinterpret_cast<FixedDoubleArray*>(result)->set_map_no_write_barrier(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004611 fixed_double_array_map());
4612 reinterpret_cast<FixedDoubleArray*>(result)->set_length(0);
4613 return result;
4614}
4615
4616
4617MaybeObject* Heap::AllocateUninitializedFixedDoubleArray(
4618 int length,
4619 PretenureFlag pretenure) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004620 if (length == 0) return empty_fixed_array();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004621
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004622 Object* elements_object;
4623 MaybeObject* maybe_obj = AllocateRawFixedDoubleArray(length, pretenure);
4624 if (!maybe_obj->ToObject(&elements_object)) return maybe_obj;
4625 FixedDoubleArray* elements =
4626 reinterpret_cast<FixedDoubleArray*>(elements_object);
4627
4628 elements->set_map_no_write_barrier(fixed_double_array_map());
4629 elements->set_length(length);
4630 return elements;
4631}
4632
4633
4634MaybeObject* Heap::AllocateFixedDoubleArrayWithHoles(
4635 int length,
4636 PretenureFlag pretenure) {
4637 if (length == 0) return empty_fixed_array();
4638
4639 Object* elements_object;
4640 MaybeObject* maybe_obj = AllocateRawFixedDoubleArray(length, pretenure);
4641 if (!maybe_obj->ToObject(&elements_object)) return maybe_obj;
4642 FixedDoubleArray* elements =
4643 reinterpret_cast<FixedDoubleArray*>(elements_object);
4644
4645 for (int i = 0; i < length; ++i) {
4646 elements->set_the_hole(i);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004647 }
4648
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004649 elements->set_map_no_write_barrier(fixed_double_array_map());
4650 elements->set_length(length);
4651 return elements;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004652}
4653
4654
4655MaybeObject* Heap::AllocateRawFixedDoubleArray(int length,
4656 PretenureFlag pretenure) {
4657 if (length < 0 || length > FixedDoubleArray::kMaxLength) {
4658 return Failure::OutOfMemoryException();
4659 }
4660
4661 AllocationSpace space =
4662 (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
4663 int size = FixedDoubleArray::SizeFor(length);
4664 if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) {
4665 // Too big for new space.
4666 space = LO_SPACE;
4667 } else if (space == OLD_DATA_SPACE &&
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004668 size > Page::kMaxNonCodeHeapObjectSize) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004669 // Too big for old data space.
4670 space = LO_SPACE;
4671 }
4672
4673 AllocationSpace retry_space =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004674 (size <= Page::kMaxNonCodeHeapObjectSize) ? OLD_DATA_SPACE : LO_SPACE;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004675
4676 return AllocateRaw(size, space, retry_space);
4677}
4678
4679
John Reck59135872010-11-02 12:39:01 -07004680MaybeObject* Heap::AllocateHashTable(int length, PretenureFlag pretenure) {
4681 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01004682 { MaybeObject* maybe_result = AllocateFixedArray(length, pretenure);
John Reck59135872010-11-02 12:39:01 -07004683 if (!maybe_result->ToObject(&result)) return maybe_result;
4684 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004685 reinterpret_cast<HeapObject*>(result)->set_map_no_write_barrier(
4686 hash_table_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00004687 ASSERT(result->IsHashTable());
4688 return result;
4689}
4690
4691
John Reck59135872010-11-02 12:39:01 -07004692MaybeObject* Heap::AllocateGlobalContext() {
4693 Object* result;
4694 { MaybeObject* maybe_result =
Steve Block44f0eee2011-05-26 01:26:41 +01004695 AllocateFixedArray(Context::GLOBAL_CONTEXT_SLOTS);
John Reck59135872010-11-02 12:39:01 -07004696 if (!maybe_result->ToObject(&result)) return maybe_result;
4697 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004698 Context* context = reinterpret_cast<Context*>(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004699 context->set_map_no_write_barrier(global_context_map());
4700 context->set_smi_js_array_map(undefined_value());
4701 context->set_double_js_array_map(undefined_value());
4702 context->set_object_js_array_map(undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +00004703 ASSERT(context->IsGlobalContext());
4704 ASSERT(result->IsContext());
4705 return result;
4706}
4707
4708
John Reck59135872010-11-02 12:39:01 -07004709MaybeObject* Heap::AllocateFunctionContext(int length, JSFunction* function) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004710 ASSERT(length >= Context::MIN_CONTEXT_SLOTS);
John Reck59135872010-11-02 12:39:01 -07004711 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01004712 { MaybeObject* maybe_result = AllocateFixedArray(length);
John Reck59135872010-11-02 12:39:01 -07004713 if (!maybe_result->ToObject(&result)) return maybe_result;
4714 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004715 Context* context = reinterpret_cast<Context*>(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004716 context->set_map_no_write_barrier(function_context_map());
Steve Blocka7e24c12009-10-30 11:49:00 +00004717 context->set_closure(function);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004718 context->set_previous(function->context());
Steve Blocka7e24c12009-10-30 11:49:00 +00004719 context->set_extension(NULL);
4720 context->set_global(function->context()->global());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004721 return context;
Steve Blocka7e24c12009-10-30 11:49:00 +00004722}
4723
4724
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004725MaybeObject* Heap::AllocateCatchContext(JSFunction* function,
4726 Context* previous,
4727 String* name,
4728 Object* thrown_object) {
4729 STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == Context::THROWN_OBJECT_INDEX);
4730 Object* result;
4731 { MaybeObject* maybe_result =
4732 AllocateFixedArray(Context::MIN_CONTEXT_SLOTS + 1);
4733 if (!maybe_result->ToObject(&result)) return maybe_result;
4734 }
4735 Context* context = reinterpret_cast<Context*>(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004736 context->set_map_no_write_barrier(catch_context_map());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004737 context->set_closure(function);
4738 context->set_previous(previous);
4739 context->set_extension(name);
4740 context->set_global(previous->global());
4741 context->set(Context::THROWN_OBJECT_INDEX, thrown_object);
4742 return context;
4743}
4744
4745
4746MaybeObject* Heap::AllocateWithContext(JSFunction* function,
4747 Context* previous,
4748 JSObject* extension) {
John Reck59135872010-11-02 12:39:01 -07004749 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01004750 { MaybeObject* maybe_result = AllocateFixedArray(Context::MIN_CONTEXT_SLOTS);
John Reck59135872010-11-02 12:39:01 -07004751 if (!maybe_result->ToObject(&result)) return maybe_result;
4752 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004753 Context* context = reinterpret_cast<Context*>(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004754 context->set_map_no_write_barrier(with_context_map());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004755 context->set_closure(function);
Steve Blocka7e24c12009-10-30 11:49:00 +00004756 context->set_previous(previous);
4757 context->set_extension(extension);
4758 context->set_global(previous->global());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004759 return context;
Steve Blocka7e24c12009-10-30 11:49:00 +00004760}
4761
4762
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004763MaybeObject* Heap::AllocateBlockContext(JSFunction* function,
4764 Context* previous,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004765 ScopeInfo* scope_info) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004766 Object* result;
4767 { MaybeObject* maybe_result =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004768 AllocateFixedArrayWithHoles(scope_info->ContextLength());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004769 if (!maybe_result->ToObject(&result)) return maybe_result;
4770 }
4771 Context* context = reinterpret_cast<Context*>(result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004772 context->set_map_no_write_barrier(block_context_map());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004773 context->set_closure(function);
4774 context->set_previous(previous);
4775 context->set_extension(scope_info);
4776 context->set_global(previous->global());
4777 return context;
4778}
4779
4780
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004781MaybeObject* Heap::AllocateScopeInfo(int length) {
4782 FixedArray* scope_info;
4783 MaybeObject* maybe_scope_info = AllocateFixedArray(length, TENURED);
4784 if (!maybe_scope_info->To(&scope_info)) return maybe_scope_info;
4785 scope_info->set_map_no_write_barrier(scope_info_map());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004786 return scope_info;
4787}
4788
4789
John Reck59135872010-11-02 12:39:01 -07004790MaybeObject* Heap::AllocateStruct(InstanceType type) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004791 Map* map;
4792 switch (type) {
Steve Block44f0eee2011-05-26 01:26:41 +01004793#define MAKE_CASE(NAME, Name, name) \
4794 case NAME##_TYPE: map = name##_map(); break;
Steve Blocka7e24c12009-10-30 11:49:00 +00004795STRUCT_LIST(MAKE_CASE)
4796#undef MAKE_CASE
4797 default:
4798 UNREACHABLE();
4799 return Failure::InternalError();
4800 }
4801 int size = map->instance_size();
4802 AllocationSpace space =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004803 (size > Page::kMaxNonCodeHeapObjectSize) ? LO_SPACE : OLD_POINTER_SPACE;
John Reck59135872010-11-02 12:39:01 -07004804 Object* result;
Steve Block44f0eee2011-05-26 01:26:41 +01004805 { MaybeObject* maybe_result = Allocate(map, space);
John Reck59135872010-11-02 12:39:01 -07004806 if (!maybe_result->ToObject(&result)) return maybe_result;
4807 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004808 Struct::cast(result)->InitializeBody(size);
4809 return result;
4810}
4811
4812
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004813bool Heap::IsHeapIterable() {
4814 return (!old_pointer_space()->was_swept_conservatively() &&
4815 !old_data_space()->was_swept_conservatively());
4816}
4817
4818
4819void Heap::EnsureHeapIsIterable() {
4820 ASSERT(IsAllocationAllowed());
4821 if (!IsHeapIterable()) {
4822 CollectAllGarbage(kMakeHeapIterableMask, "Heap::EnsureHeapIsIterable");
4823 }
4824 ASSERT(IsHeapIterable());
4825}
4826
4827
4828void Heap::AdvanceIdleIncrementalMarking(intptr_t step_size) {
Ben Murdoch8f9999f2012-04-23 10:39:17 +01004829 incremental_marking()->Step(step_size,
4830 IncrementalMarking::NO_GC_VIA_STACK_GUARD);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004831
4832 if (incremental_marking()->IsComplete()) {
4833 bool uncommit = false;
4834 if (gc_count_at_last_idle_gc_ == gc_count_) {
4835 // No GC since the last full GC, the mutator is probably not active.
4836 isolate_->compilation_cache()->Clear();
4837 uncommit = true;
4838 }
4839 CollectAllGarbage(kNoGCFlags, "idle notification: finalize incremental");
4840 gc_count_at_last_idle_gc_ = gc_count_;
4841 if (uncommit) {
4842 new_space_.Shrink();
4843 UncommitFromSpace();
4844 }
4845 }
4846}
4847
4848
4849bool Heap::IdleNotification(int hint) {
4850 const int kMaxHint = 1000;
4851 intptr_t size_factor = Min(Max(hint, 30), kMaxHint) / 10;
4852 // The size factor is in range [3..100].
4853 intptr_t step_size = size_factor * IncrementalMarking::kAllocatedThreshold;
4854
4855 if (contexts_disposed_ > 0) {
4856 if (hint >= kMaxHint) {
4857 // The embedder is requesting a lot of GC work after context disposal,
4858 // we age inline caches so that they don't keep objects from
4859 // the old context alive.
4860 AgeInlineCaches();
4861 }
4862 int mark_sweep_time = Min(TimeMarkSweepWouldTakeInMs(), 1000);
4863 if (hint >= mark_sweep_time && !FLAG_expose_gc &&
4864 incremental_marking()->IsStopped()) {
4865 HistogramTimerScope scope(isolate_->counters()->gc_context());
4866 CollectAllGarbage(kReduceMemoryFootprintMask,
4867 "idle notification: contexts disposed");
4868 } else {
4869 AdvanceIdleIncrementalMarking(step_size);
4870 contexts_disposed_ = 0;
4871 }
4872 // Make sure that we have no pending context disposals.
4873 // Take into account that we might have decided to delay full collection
4874 // because incremental marking is in progress.
4875 ASSERT((contexts_disposed_ == 0) || !incremental_marking()->IsStopped());
4876 return false;
4877 }
4878
4879 if (hint >= kMaxHint || !FLAG_incremental_marking ||
4880 FLAG_expose_gc || Serializer::enabled()) {
4881 return IdleGlobalGC();
4882 }
4883
4884 // By doing small chunks of GC work in each IdleNotification,
4885 // perform a round of incremental GCs and after that wait until
4886 // the mutator creates enough garbage to justify a new round.
4887 // An incremental GC progresses as follows:
4888 // 1. many incremental marking steps,
4889 // 2. one old space mark-sweep-compact,
4890 // 3. many lazy sweep steps.
4891 // Use mark-sweep-compact events to count incremental GCs in a round.
4892
4893
4894 if (incremental_marking()->IsStopped()) {
4895 if (!IsSweepingComplete() &&
4896 !AdvanceSweepers(static_cast<int>(step_size))) {
4897 return false;
4898 }
4899 }
4900
4901 if (mark_sweeps_since_idle_round_started_ >= kMaxMarkSweepsInIdleRound) {
4902 if (EnoughGarbageSinceLastIdleRound()) {
4903 StartIdleRound();
4904 } else {
4905 return true;
4906 }
4907 }
4908
4909 int new_mark_sweeps = ms_count_ - ms_count_at_last_idle_notification_;
4910 mark_sweeps_since_idle_round_started_ += new_mark_sweeps;
4911 ms_count_at_last_idle_notification_ = ms_count_;
4912
4913 if (mark_sweeps_since_idle_round_started_ >= kMaxMarkSweepsInIdleRound) {
4914 FinishIdleRound();
4915 return true;
4916 }
4917
4918 if (incremental_marking()->IsStopped()) {
4919 if (!WorthStartingGCWhenIdle()) {
4920 FinishIdleRound();
4921 return true;
4922 }
4923 incremental_marking()->Start();
4924 }
4925
4926 AdvanceIdleIncrementalMarking(step_size);
4927 return false;
4928}
4929
4930
4931bool Heap::IdleGlobalGC() {
Steve Blocka7e24c12009-10-30 11:49:00 +00004932 static const int kIdlesBeforeScavenge = 4;
4933 static const int kIdlesBeforeMarkSweep = 7;
4934 static const int kIdlesBeforeMarkCompact = 8;
Ben Murdochdb5a90a2011-01-06 18:27:03 +00004935 static const int kMaxIdleCount = kIdlesBeforeMarkCompact + 1;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004936 static const unsigned int kGCsBetweenCleanup = 4;
Steve Block44f0eee2011-05-26 01:26:41 +01004937
4938 if (!last_idle_notification_gc_count_init_) {
4939 last_idle_notification_gc_count_ = gc_count_;
4940 last_idle_notification_gc_count_init_ = true;
4941 }
Steve Blocka7e24c12009-10-30 11:49:00 +00004942
Steve Block6ded16b2010-05-10 14:33:55 +01004943 bool uncommit = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00004944 bool finished = false;
4945
Ben Murdochdb5a90a2011-01-06 18:27:03 +00004946 // Reset the number of idle notifications received when a number of
4947 // GCs have taken place. This allows another round of cleanup based
4948 // on idle notifications if enough work has been carried out to
4949 // provoke a number of garbage collections.
Steve Block44f0eee2011-05-26 01:26:41 +01004950 if (gc_count_ - last_idle_notification_gc_count_ < kGCsBetweenCleanup) {
4951 number_idle_notifications_ =
4952 Min(number_idle_notifications_ + 1, kMaxIdleCount);
Steve Blocka7e24c12009-10-30 11:49:00 +00004953 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004954 number_idle_notifications_ = 0;
4955 last_idle_notification_gc_count_ = gc_count_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004956 }
4957
Steve Block44f0eee2011-05-26 01:26:41 +01004958 if (number_idle_notifications_ == kIdlesBeforeScavenge) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004959 CollectGarbage(NEW_SPACE, "idle notification");
Steve Blocka7e24c12009-10-30 11:49:00 +00004960 new_space_.Shrink();
Steve Block44f0eee2011-05-26 01:26:41 +01004961 last_idle_notification_gc_count_ = gc_count_;
4962 } else if (number_idle_notifications_ == kIdlesBeforeMarkSweep) {
Steve Blockd0582a62009-12-15 09:54:21 +00004963 // Before doing the mark-sweep collections we clear the
4964 // compilation cache to avoid hanging on to source code and
4965 // generated code for cached functions.
Steve Block44f0eee2011-05-26 01:26:41 +01004966 isolate_->compilation_cache()->Clear();
Steve Blockd0582a62009-12-15 09:54:21 +00004967
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004968 CollectAllGarbage(kReduceMemoryFootprintMask, "idle notification");
Steve Blocka7e24c12009-10-30 11:49:00 +00004969 new_space_.Shrink();
Steve Block44f0eee2011-05-26 01:26:41 +01004970 last_idle_notification_gc_count_ = gc_count_;
Steve Blocka7e24c12009-10-30 11:49:00 +00004971
Steve Block44f0eee2011-05-26 01:26:41 +01004972 } else if (number_idle_notifications_ == kIdlesBeforeMarkCompact) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004973 CollectAllGarbage(kReduceMemoryFootprintMask, "idle notification");
Steve Blocka7e24c12009-10-30 11:49:00 +00004974 new_space_.Shrink();
Steve Block44f0eee2011-05-26 01:26:41 +01004975 last_idle_notification_gc_count_ = gc_count_;
4976 number_idle_notifications_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00004977 finished = true;
Steve Block44f0eee2011-05-26 01:26:41 +01004978 } else if (number_idle_notifications_ > kIdlesBeforeMarkCompact) {
Ben Murdochdb5a90a2011-01-06 18:27:03 +00004979 // If we have received more than kIdlesBeforeMarkCompact idle
4980 // notifications we do not perform any cleanup because we don't
4981 // expect to gain much by doing so.
4982 finished = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00004983 }
4984
Steve Block44f0eee2011-05-26 01:26:41 +01004985 if (uncommit) UncommitFromSpace();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004986
Steve Blocka7e24c12009-10-30 11:49:00 +00004987 return finished;
4988}
4989
4990
4991#ifdef DEBUG
4992
4993void Heap::Print() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004994 if (!HasBeenSetUp()) return;
Steve Block44f0eee2011-05-26 01:26:41 +01004995 isolate()->PrintStack();
Steve Blocka7e24c12009-10-30 11:49:00 +00004996 AllSpaces spaces;
Leon Clarked91b9f72010-01-27 17:25:45 +00004997 for (Space* space = spaces.next(); space != NULL; space = spaces.next())
4998 space->Print();
Steve Blocka7e24c12009-10-30 11:49:00 +00004999}
5000
5001
5002void Heap::ReportCodeStatistics(const char* title) {
5003 PrintF(">>>>>> Code Stats (%s) >>>>>>\n", title);
5004 PagedSpace::ResetCodeStatistics();
5005 // We do not look for code in new space, map space, or old space. If code
5006 // somehow ends up in those spaces, we would miss it here.
5007 code_space_->CollectCodeStatistics();
5008 lo_space_->CollectCodeStatistics();
5009 PagedSpace::ReportCodeStatistics();
5010}
5011
5012
5013// This function expects that NewSpace's allocated objects histogram is
5014// populated (via a call to CollectStatistics or else as a side effect of a
5015// just-completed scavenge collection).
5016void Heap::ReportHeapStatistics(const char* title) {
5017 USE(title);
5018 PrintF(">>>>>> =============== %s (%d) =============== >>>>>>\n",
5019 title, gc_count_);
Ben Murdochf87a2032010-10-22 12:50:53 +01005020 PrintF("old_gen_promotion_limit_ %" V8_PTR_PREFIX "d\n",
5021 old_gen_promotion_limit_);
5022 PrintF("old_gen_allocation_limit_ %" V8_PTR_PREFIX "d\n",
5023 old_gen_allocation_limit_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005024 PrintF("old_gen_limit_factor_ %d\n", old_gen_limit_factor_);
Steve Blocka7e24c12009-10-30 11:49:00 +00005025
5026 PrintF("\n");
5027 PrintF("Number of handles : %d\n", HandleScope::NumberOfHandles());
Steve Block44f0eee2011-05-26 01:26:41 +01005028 isolate_->global_handles()->PrintStats();
Steve Blocka7e24c12009-10-30 11:49:00 +00005029 PrintF("\n");
5030
5031 PrintF("Heap statistics : ");
Steve Block44f0eee2011-05-26 01:26:41 +01005032 isolate_->memory_allocator()->ReportStatistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00005033 PrintF("To space : ");
5034 new_space_.ReportStatistics();
5035 PrintF("Old pointer space : ");
5036 old_pointer_space_->ReportStatistics();
5037 PrintF("Old data space : ");
5038 old_data_space_->ReportStatistics();
5039 PrintF("Code space : ");
5040 code_space_->ReportStatistics();
5041 PrintF("Map space : ");
5042 map_space_->ReportStatistics();
5043 PrintF("Cell space : ");
5044 cell_space_->ReportStatistics();
5045 PrintF("Large object space : ");
5046 lo_space_->ReportStatistics();
5047 PrintF(">>>>>> ========================================= >>>>>>\n");
5048}
5049
5050#endif // DEBUG
5051
5052bool Heap::Contains(HeapObject* value) {
5053 return Contains(value->address());
5054}
5055
5056
5057bool Heap::Contains(Address addr) {
5058 if (OS::IsOutsideAllocatedSpace(addr)) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005059 return HasBeenSetUp() &&
Steve Blocka7e24c12009-10-30 11:49:00 +00005060 (new_space_.ToSpaceContains(addr) ||
5061 old_pointer_space_->Contains(addr) ||
5062 old_data_space_->Contains(addr) ||
5063 code_space_->Contains(addr) ||
5064 map_space_->Contains(addr) ||
5065 cell_space_->Contains(addr) ||
5066 lo_space_->SlowContains(addr));
5067}
5068
5069
5070bool Heap::InSpace(HeapObject* value, AllocationSpace space) {
5071 return InSpace(value->address(), space);
5072}
5073
5074
5075bool Heap::InSpace(Address addr, AllocationSpace space) {
5076 if (OS::IsOutsideAllocatedSpace(addr)) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005077 if (!HasBeenSetUp()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005078
5079 switch (space) {
5080 case NEW_SPACE:
5081 return new_space_.ToSpaceContains(addr);
5082 case OLD_POINTER_SPACE:
5083 return old_pointer_space_->Contains(addr);
5084 case OLD_DATA_SPACE:
5085 return old_data_space_->Contains(addr);
5086 case CODE_SPACE:
5087 return code_space_->Contains(addr);
5088 case MAP_SPACE:
5089 return map_space_->Contains(addr);
5090 case CELL_SPACE:
5091 return cell_space_->Contains(addr);
5092 case LO_SPACE:
5093 return lo_space_->SlowContains(addr);
5094 }
5095
5096 return false;
5097}
5098
5099
5100#ifdef DEBUG
Ben Murdoch85b71792012-04-11 18:30:58 +01005101void Heap::Verify() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005102 ASSERT(HasBeenSetUp());
5103
5104 store_buffer()->Verify();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005105
Steve Blocka7e24c12009-10-30 11:49:00 +00005106 VerifyPointersVisitor visitor;
Steve Blockd0582a62009-12-15 09:54:21 +00005107 IterateRoots(&visitor, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00005108
5109 new_space_.Verify();
5110
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005111 old_pointer_space_->Verify(&visitor);
5112 map_space_->Verify(&visitor);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005113
5114 VerifyPointersVisitor no_dirty_regions_visitor;
5115 old_data_space_->Verify(&no_dirty_regions_visitor);
5116 code_space_->Verify(&no_dirty_regions_visitor);
5117 cell_space_->Verify(&no_dirty_regions_visitor);
Steve Blocka7e24c12009-10-30 11:49:00 +00005118
5119 lo_space_->Verify();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005120
5121 VerifyNoAccessorPairSharing();
5122}
5123
5124
5125void Heap::VerifyNoAccessorPairSharing() {
5126 // Verification is done in 2 phases: First we mark all AccessorPairs, checking
5127 // that we mark only unmarked pairs, then we clear all marks, restoring the
5128 // initial state. We use the Smi tag of the AccessorPair's getter as the
5129 // marking bit, because we can never see a Smi as the getter.
5130 for (int phase = 0; phase < 2; phase++) {
5131 HeapObjectIterator iter(map_space());
5132 for (HeapObject* obj = iter.Next(); obj != NULL; obj = iter.Next()) {
5133 if (obj->IsMap()) {
5134 DescriptorArray* descs = Map::cast(obj)->instance_descriptors();
5135 for (int i = 0; i < descs->number_of_descriptors(); i++) {
5136 if (descs->GetType(i) == CALLBACKS &&
5137 descs->GetValue(i)->IsAccessorPair()) {
5138 AccessorPair* accessors = AccessorPair::cast(descs->GetValue(i));
5139 uintptr_t before = reinterpret_cast<intptr_t>(accessors->getter());
5140 uintptr_t after = (phase == 0) ?
5141 ((before & ~kSmiTagMask) | kSmiTag) :
5142 ((before & ~kHeapObjectTag) | kHeapObjectTag);
5143 CHECK(before != after);
5144 accessors->set_getter(reinterpret_cast<Object*>(after));
5145 }
5146 }
5147 }
5148 }
5149 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01005150}
Steve Blocka7e24c12009-10-30 11:49:00 +00005151#endif // DEBUG
5152
5153
John Reck59135872010-11-02 12:39:01 -07005154MaybeObject* Heap::LookupSymbol(Vector<const char> string) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005155 Object* symbol = NULL;
John Reck59135872010-11-02 12:39:01 -07005156 Object* new_table;
5157 { MaybeObject* maybe_new_table =
5158 symbol_table()->LookupSymbol(string, &symbol);
5159 if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table;
5160 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005161 // Can't use set_symbol_table because SymbolTable::cast knows that
5162 // SymbolTable is a singleton and checks for identity.
5163 roots_[kSymbolTableRootIndex] = new_table;
5164 ASSERT(symbol != NULL);
5165 return symbol;
5166}
5167
5168
Steve Block9fac8402011-05-12 15:51:54 +01005169MaybeObject* Heap::LookupAsciiSymbol(Vector<const char> string) {
5170 Object* symbol = NULL;
5171 Object* new_table;
5172 { MaybeObject* maybe_new_table =
5173 symbol_table()->LookupAsciiSymbol(string, &symbol);
5174 if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table;
5175 }
5176 // Can't use set_symbol_table because SymbolTable::cast knows that
5177 // SymbolTable is a singleton and checks for identity.
5178 roots_[kSymbolTableRootIndex] = new_table;
5179 ASSERT(symbol != NULL);
5180 return symbol;
5181}
5182
5183
Ben Murdoch257744e2011-11-30 15:57:28 +00005184MaybeObject* Heap::LookupAsciiSymbol(Handle<SeqAsciiString> string,
5185 int from,
5186 int length) {
5187 Object* symbol = NULL;
5188 Object* new_table;
5189 { MaybeObject* maybe_new_table =
5190 symbol_table()->LookupSubStringAsciiSymbol(string,
5191 from,
5192 length,
5193 &symbol);
5194 if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table;
5195 }
5196 // Can't use set_symbol_table because SymbolTable::cast knows that
5197 // SymbolTable is a singleton and checks for identity.
5198 roots_[kSymbolTableRootIndex] = new_table;
5199 ASSERT(symbol != NULL);
5200 return symbol;
5201}
5202
5203
Steve Block9fac8402011-05-12 15:51:54 +01005204MaybeObject* Heap::LookupTwoByteSymbol(Vector<const uc16> string) {
5205 Object* symbol = NULL;
5206 Object* new_table;
5207 { MaybeObject* maybe_new_table =
5208 symbol_table()->LookupTwoByteSymbol(string, &symbol);
5209 if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table;
5210 }
5211 // Can't use set_symbol_table because SymbolTable::cast knows that
5212 // SymbolTable is a singleton and checks for identity.
5213 roots_[kSymbolTableRootIndex] = new_table;
5214 ASSERT(symbol != NULL);
5215 return symbol;
5216}
5217
5218
John Reck59135872010-11-02 12:39:01 -07005219MaybeObject* Heap::LookupSymbol(String* string) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005220 if (string->IsSymbol()) return string;
5221 Object* symbol = NULL;
John Reck59135872010-11-02 12:39:01 -07005222 Object* new_table;
5223 { MaybeObject* maybe_new_table =
5224 symbol_table()->LookupString(string, &symbol);
5225 if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table;
5226 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005227 // Can't use set_symbol_table because SymbolTable::cast knows that
5228 // SymbolTable is a singleton and checks for identity.
5229 roots_[kSymbolTableRootIndex] = new_table;
5230 ASSERT(symbol != NULL);
5231 return symbol;
5232}
5233
5234
5235bool Heap::LookupSymbolIfExists(String* string, String** symbol) {
5236 if (string->IsSymbol()) {
5237 *symbol = string;
5238 return true;
5239 }
5240 return symbol_table()->LookupSymbolIfExists(string, symbol);
5241}
5242
5243
5244#ifdef DEBUG
5245void Heap::ZapFromSpace() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005246 NewSpacePageIterator it(new_space_.FromSpaceStart(),
5247 new_space_.FromSpaceEnd());
5248 while (it.has_next()) {
5249 NewSpacePage* page = it.next();
5250 for (Address cursor = page->area_start(), limit = page->area_end();
5251 cursor < limit;
5252 cursor += kPointerSize) {
5253 Memory::Address_at(cursor) = kFromSpaceZapValue;
5254 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005255 }
5256}
5257#endif // DEBUG
5258
5259
Ben Murdoch3bec4d22010-07-22 14:51:16 +01005260void Heap::IterateAndMarkPointersToFromSpace(Address start,
5261 Address end,
5262 ObjectSlotCallback callback) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005263 Address slot_address = start;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005264
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005265 // We are not collecting slots on new space objects during mutation
5266 // thus we have to scan for pointers to evacuation candidates when we
5267 // promote objects. But we should not record any slots in non-black
5268 // objects. Grey object's slots would be rescanned.
5269 // White object might not survive until the end of collection
5270 // it would be a violation of the invariant to record it's slots.
5271 bool record_slots = false;
5272 if (incremental_marking()->IsCompacting()) {
5273 MarkBit mark_bit = Marking::MarkBitFrom(HeapObject::FromAddress(start));
5274 record_slots = Marking::IsBlack(mark_bit);
5275 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005276
5277 while (slot_address < end) {
5278 Object** slot = reinterpret_cast<Object**>(slot_address);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005279 Object* object = *slot;
5280 // If the store buffer becomes overfull we mark pages as being exempt from
5281 // the store buffer. These pages are scanned to find pointers that point
5282 // to the new space. In that case we may hit newly promoted objects and
5283 // fix the pointers before the promotion queue gets to them. Thus the 'if'.
5284 if (object->IsHeapObject()) {
5285 if (Heap::InFromSpace(object)) {
5286 callback(reinterpret_cast<HeapObject**>(slot),
5287 HeapObject::cast(object));
5288 Object* new_object = *slot;
5289 if (InNewSpace(new_object)) {
5290 SLOW_ASSERT(Heap::InToSpace(new_object));
5291 SLOW_ASSERT(new_object->IsHeapObject());
5292 store_buffer_.EnterDirectlyIntoStoreBuffer(
5293 reinterpret_cast<Address>(slot));
5294 }
5295 SLOW_ASSERT(!MarkCompactCollector::IsOnEvacuationCandidate(new_object));
5296 } else if (record_slots &&
5297 MarkCompactCollector::IsOnEvacuationCandidate(object)) {
5298 mark_compact_collector()->RecordSlot(slot, slot, object);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005299 }
5300 }
5301 slot_address += kPointerSize;
5302 }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005303}
5304
5305
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005306#ifdef DEBUG
5307typedef bool (*CheckStoreBufferFilter)(Object** addr);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005308
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005309
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005310bool IsAMapPointerAddress(Object** addr) {
5311 uintptr_t a = reinterpret_cast<uintptr_t>(addr);
5312 int mod = a % Map::kSize;
5313 return mod >= Map::kPointerFieldsBeginOffset &&
5314 mod < Map::kPointerFieldsEndOffset;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005315}
Ben Murdoch85b71792012-04-11 18:30:58 +01005316
5317
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005318bool EverythingsAPointer(Object** addr) {
5319 return true;
5320}
Ben Murdoch85b71792012-04-11 18:30:58 +01005321
Ben Murdoch85b71792012-04-11 18:30:58 +01005322
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005323static void CheckStoreBuffer(Heap* heap,
5324 Object** current,
5325 Object** limit,
5326 Object**** store_buffer_position,
5327 Object*** store_buffer_top,
5328 CheckStoreBufferFilter filter,
5329 Address special_garbage_start,
5330 Address special_garbage_end) {
5331 Map* free_space_map = heap->free_space_map();
5332 for ( ; current < limit; current++) {
5333 Object* o = *current;
5334 Address current_address = reinterpret_cast<Address>(current);
5335 // Skip free space.
5336 if (o == free_space_map) {
5337 Address current_address = reinterpret_cast<Address>(current);
5338 FreeSpace* free_space =
5339 FreeSpace::cast(HeapObject::FromAddress(current_address));
5340 int skip = free_space->Size();
5341 ASSERT(current_address + skip <= reinterpret_cast<Address>(limit));
5342 ASSERT(skip > 0);
5343 current_address += skip - kPointerSize;
5344 current = reinterpret_cast<Object**>(current_address);
5345 continue;
Ben Murdoch85b71792012-04-11 18:30:58 +01005346 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005347 // Skip the current linear allocation space between top and limit which is
5348 // unmarked with the free space map, but can contain junk.
5349 if (current_address == special_garbage_start &&
5350 special_garbage_end != special_garbage_start) {
5351 current_address = special_garbage_end - kPointerSize;
5352 current = reinterpret_cast<Object**>(current_address);
5353 continue;
5354 }
5355 if (!(*filter)(current)) continue;
5356 ASSERT(current_address < special_garbage_start ||
5357 current_address >= special_garbage_end);
5358 ASSERT(reinterpret_cast<uintptr_t>(o) != kFreeListZapValue);
5359 // We have to check that the pointer does not point into new space
5360 // without trying to cast it to a heap object since the hash field of
5361 // a string can contain values like 1 and 3 which are tagged null
5362 // pointers.
5363 if (!heap->InNewSpace(o)) continue;
5364 while (**store_buffer_position < current &&
5365 *store_buffer_position < store_buffer_top) {
5366 (*store_buffer_position)++;
5367 }
5368 if (**store_buffer_position != current ||
5369 *store_buffer_position == store_buffer_top) {
5370 Object** obj_start = current;
5371 while (!(*obj_start)->IsMap()) obj_start--;
5372 UNREACHABLE();
5373 }
Ben Murdoch85b71792012-04-11 18:30:58 +01005374 }
5375}
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005376
5377
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005378// Check that the store buffer contains all intergenerational pointers by
5379// scanning a page and ensuring that all pointers to young space are in the
5380// store buffer.
5381void Heap::OldPointerSpaceCheckStoreBuffer() {
5382 OldSpace* space = old_pointer_space();
5383 PageIterator pages(space);
5384
5385 store_buffer()->SortUniq();
5386
5387 while (pages.has_next()) {
5388 Page* page = pages.next();
5389 Object** current = reinterpret_cast<Object**>(page->area_start());
5390
5391 Address end = page->area_end();
5392
5393 Object*** store_buffer_position = store_buffer()->Start();
5394 Object*** store_buffer_top = store_buffer()->Top();
5395
5396 Object** limit = reinterpret_cast<Object**>(end);
5397 CheckStoreBuffer(this,
5398 current,
5399 limit,
5400 &store_buffer_position,
5401 store_buffer_top,
5402 &EverythingsAPointer,
5403 space->top(),
5404 space->limit());
5405 }
5406}
5407
5408
5409void Heap::MapSpaceCheckStoreBuffer() {
5410 MapSpace* space = map_space();
5411 PageIterator pages(space);
5412
5413 store_buffer()->SortUniq();
5414
5415 while (pages.has_next()) {
5416 Page* page = pages.next();
5417 Object** current = reinterpret_cast<Object**>(page->area_start());
5418
5419 Address end = page->area_end();
5420
5421 Object*** store_buffer_position = store_buffer()->Start();
5422 Object*** store_buffer_top = store_buffer()->Top();
5423
5424 Object** limit = reinterpret_cast<Object**>(end);
5425 CheckStoreBuffer(this,
5426 current,
5427 limit,
5428 &store_buffer_position,
5429 store_buffer_top,
5430 &IsAMapPointerAddress,
5431 space->top(),
5432 space->limit());
5433 }
5434}
5435
5436
5437void Heap::LargeObjectSpaceCheckStoreBuffer() {
5438 LargeObjectIterator it(lo_space());
5439 for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) {
5440 // We only have code, sequential strings, or fixed arrays in large
5441 // object space, and only fixed arrays can possibly contain pointers to
5442 // the young generation.
5443 if (object->IsFixedArray()) {
5444 Object*** store_buffer_position = store_buffer()->Start();
5445 Object*** store_buffer_top = store_buffer()->Top();
5446 Object** current = reinterpret_cast<Object**>(object->address());
5447 Object** limit =
5448 reinterpret_cast<Object**>(object->address() + object->Size());
5449 CheckStoreBuffer(this,
5450 current,
5451 limit,
5452 &store_buffer_position,
5453 store_buffer_top,
5454 &EverythingsAPointer,
5455 NULL,
5456 NULL);
5457 }
5458 }
5459}
5460#endif
5461
5462
Steve Blockd0582a62009-12-15 09:54:21 +00005463void Heap::IterateRoots(ObjectVisitor* v, VisitMode mode) {
5464 IterateStrongRoots(v, mode);
Leon Clarked91b9f72010-01-27 17:25:45 +00005465 IterateWeakRoots(v, mode);
5466}
5467
5468
5469void Heap::IterateWeakRoots(ObjectVisitor* v, VisitMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005470 v->VisitPointer(reinterpret_cast<Object**>(&roots_[kSymbolTableRootIndex]));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005471 v->Synchronize(VisitorSynchronization::kSymbolTable);
Ben Murdoch257744e2011-11-30 15:57:28 +00005472 if (mode != VISIT_ALL_IN_SCAVENGE &&
5473 mode != VISIT_ALL_IN_SWEEP_NEWSPACE) {
Leon Clarkee46be812010-01-19 14:06:41 +00005474 // Scavenge collections have special processing for this.
Steve Block44f0eee2011-05-26 01:26:41 +01005475 external_string_table_.Iterate(v);
Leon Clarkee46be812010-01-19 14:06:41 +00005476 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005477 v->Synchronize(VisitorSynchronization::kExternalStringsTable);
Steve Blocka7e24c12009-10-30 11:49:00 +00005478}
5479
5480
Steve Blockd0582a62009-12-15 09:54:21 +00005481void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005482 v->VisitPointers(&roots_[0], &roots_[kStrongRootListLength]);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005483 v->Synchronize(VisitorSynchronization::kStrongRootList);
Steve Blocka7e24c12009-10-30 11:49:00 +00005484
Iain Merrick75681382010-08-19 15:07:18 +01005485 v->VisitPointer(BitCast<Object**>(&hidden_symbol_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005486 v->Synchronize(VisitorSynchronization::kSymbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00005487
Steve Block44f0eee2011-05-26 01:26:41 +01005488 isolate_->bootstrapper()->Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005489 v->Synchronize(VisitorSynchronization::kBootstrapper);
Steve Block44f0eee2011-05-26 01:26:41 +01005490 isolate_->Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005491 v->Synchronize(VisitorSynchronization::kTop);
Steve Blocka7e24c12009-10-30 11:49:00 +00005492 Relocatable::Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005493 v->Synchronize(VisitorSynchronization::kRelocatable);
Steve Blocka7e24c12009-10-30 11:49:00 +00005494
5495#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01005496 isolate_->debug()->Iterate(v);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005497 if (isolate_->deoptimizer_data() != NULL) {
5498 isolate_->deoptimizer_data()->Iterate(v);
5499 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005500#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005501 v->Synchronize(VisitorSynchronization::kDebug);
Steve Block44f0eee2011-05-26 01:26:41 +01005502 isolate_->compilation_cache()->Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005503 v->Synchronize(VisitorSynchronization::kCompilationCache);
Steve Blocka7e24c12009-10-30 11:49:00 +00005504
5505 // Iterate over local handles in handle scopes.
Steve Block44f0eee2011-05-26 01:26:41 +01005506 isolate_->handle_scope_implementer()->Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005507 v->Synchronize(VisitorSynchronization::kHandleScope);
Steve Blocka7e24c12009-10-30 11:49:00 +00005508
Leon Clarkee46be812010-01-19 14:06:41 +00005509 // Iterate over the builtin code objects and code stubs in the
5510 // heap. Note that it is not necessary to iterate over code objects
5511 // on scavenge collections.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005512 if (mode != VISIT_ALL_IN_SCAVENGE) {
Steve Block44f0eee2011-05-26 01:26:41 +01005513 isolate_->builtins()->IterateBuiltins(v);
Leon Clarkee46be812010-01-19 14:06:41 +00005514 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005515 v->Synchronize(VisitorSynchronization::kBuiltins);
Steve Blocka7e24c12009-10-30 11:49:00 +00005516
5517 // Iterate over global handles.
Ben Murdoch257744e2011-11-30 15:57:28 +00005518 switch (mode) {
5519 case VISIT_ONLY_STRONG:
5520 isolate_->global_handles()->IterateStrongRoots(v);
5521 break;
5522 case VISIT_ALL_IN_SCAVENGE:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005523 isolate_->global_handles()->IterateNewSpaceStrongAndDependentRoots(v);
Ben Murdoch257744e2011-11-30 15:57:28 +00005524 break;
5525 case VISIT_ALL_IN_SWEEP_NEWSPACE:
5526 case VISIT_ALL:
5527 isolate_->global_handles()->IterateAllRoots(v);
5528 break;
Steve Blockd0582a62009-12-15 09:54:21 +00005529 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005530 v->Synchronize(VisitorSynchronization::kGlobalHandles);
Steve Blocka7e24c12009-10-30 11:49:00 +00005531
5532 // Iterate over pointers being held by inactive threads.
Steve Block44f0eee2011-05-26 01:26:41 +01005533 isolate_->thread_manager()->Iterate(v);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005534 v->Synchronize(VisitorSynchronization::kThreadManager);
Leon Clarked91b9f72010-01-27 17:25:45 +00005535
5536 // Iterate over the pointers the Serialization/Deserialization code is
5537 // holding.
5538 // During garbage collection this keeps the partial snapshot cache alive.
5539 // During deserialization of the startup snapshot this creates the partial
5540 // snapshot cache and deserializes the objects it refers to. During
5541 // serialization this does nothing, since the partial snapshot cache is
5542 // empty. However the next thing we do is create the partial snapshot,
5543 // filling up the partial snapshot cache with objects it needs as we go.
5544 SerializerDeserializer::Iterate(v);
5545 // We don't do a v->Synchronize call here, because in debug mode that will
5546 // output a flag to the snapshot. However at this point the serializer and
5547 // deserializer are deliberately a little unsynchronized (see above) so the
5548 // checking of the sync flag in the snapshot would fail.
Steve Blocka7e24c12009-10-30 11:49:00 +00005549}
Steve Blocka7e24c12009-10-30 11:49:00 +00005550
5551
Steve Blocka7e24c12009-10-30 11:49:00 +00005552// TODO(1236194): Since the heap size is configurable on the command line
5553// and through the API, we should gracefully handle the case that the heap
5554// size is not big enough to fit all the initial objects.
Russell Brenner90bac252010-11-18 13:33:46 -08005555bool Heap::ConfigureHeap(int max_semispace_size,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005556 intptr_t max_old_gen_size,
5557 intptr_t max_executable_size) {
5558 if (HasBeenSetUp()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005559
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005560 if (max_semispace_size > 0) {
5561 if (max_semispace_size < Page::kPageSize) {
5562 max_semispace_size = Page::kPageSize;
5563 if (FLAG_trace_gc) {
5564 PrintF("Max semispace size cannot be less than %dkbytes\n",
5565 Page::kPageSize >> 10);
5566 }
5567 }
5568 max_semispace_size_ = max_semispace_size;
5569 }
Steve Block3ce2e202009-11-05 08:53:23 +00005570
5571 if (Snapshot::IsEnabled()) {
5572 // If we are using a snapshot we always reserve the default amount
5573 // of memory for each semispace because code in the snapshot has
5574 // write-barrier code that relies on the size and alignment of new
5575 // space. We therefore cannot use a larger max semispace size
5576 // than the default reserved semispace size.
5577 if (max_semispace_size_ > reserved_semispace_size_) {
5578 max_semispace_size_ = reserved_semispace_size_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005579 if (FLAG_trace_gc) {
5580 PrintF("Max semispace size cannot be more than %dkbytes\n",
5581 reserved_semispace_size_ >> 10);
5582 }
Steve Block3ce2e202009-11-05 08:53:23 +00005583 }
5584 } else {
5585 // If we are not using snapshots we reserve space for the actual
5586 // max semispace size.
5587 reserved_semispace_size_ = max_semispace_size_;
5588 }
5589
5590 if (max_old_gen_size > 0) max_old_generation_size_ = max_old_gen_size;
Russell Brenner90bac252010-11-18 13:33:46 -08005591 if (max_executable_size > 0) {
5592 max_executable_size_ = RoundUp(max_executable_size, Page::kPageSize);
5593 }
5594
5595 // The max executable size must be less than or equal to the max old
5596 // generation size.
5597 if (max_executable_size_ > max_old_generation_size_) {
5598 max_executable_size_ = max_old_generation_size_;
5599 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005600
5601 // The new space size must be a power of two to support single-bit testing
5602 // for containment.
Steve Block3ce2e202009-11-05 08:53:23 +00005603 max_semispace_size_ = RoundUpToPowerOf2(max_semispace_size_);
5604 reserved_semispace_size_ = RoundUpToPowerOf2(reserved_semispace_size_);
5605 initial_semispace_size_ = Min(initial_semispace_size_, max_semispace_size_);
5606 external_allocation_limit_ = 10 * max_semispace_size_;
Steve Blocka7e24c12009-10-30 11:49:00 +00005607
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005608 // The old generation is paged and needs at least one page for each space.
5609 int paged_space_count = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1;
5610 max_old_generation_size_ = Max(static_cast<intptr_t>(paged_space_count *
5611 Page::kPageSize),
5612 RoundUp(max_old_generation_size_,
5613 Page::kPageSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00005614
Steve Block44f0eee2011-05-26 01:26:41 +01005615 configured_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00005616 return true;
5617}
5618
5619
5620bool Heap::ConfigureHeapDefault() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005621 return ConfigureHeap(static_cast<intptr_t>(FLAG_max_new_space_size / 2) * KB,
5622 static_cast<intptr_t>(FLAG_max_old_space_size) * MB,
5623 static_cast<intptr_t>(FLAG_max_executable_size) * MB);
Steve Blocka7e24c12009-10-30 11:49:00 +00005624}
5625
5626
Ben Murdochbb769b22010-08-11 14:56:33 +01005627void Heap::RecordStats(HeapStats* stats, bool take_snapshot) {
Iain Merrick75681382010-08-19 15:07:18 +01005628 *stats->start_marker = HeapStats::kStartMarker;
5629 *stats->end_marker = HeapStats::kEndMarker;
Ben Murdochf87a2032010-10-22 12:50:53 +01005630 *stats->new_space_size = new_space_.SizeAsInt();
5631 *stats->new_space_capacity = static_cast<int>(new_space_.Capacity());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005632 *stats->old_pointer_space_size = old_pointer_space_->SizeOfObjects();
Steve Blockd0582a62009-12-15 09:54:21 +00005633 *stats->old_pointer_space_capacity = old_pointer_space_->Capacity();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005634 *stats->old_data_space_size = old_data_space_->SizeOfObjects();
Steve Blockd0582a62009-12-15 09:54:21 +00005635 *stats->old_data_space_capacity = old_data_space_->Capacity();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005636 *stats->code_space_size = code_space_->SizeOfObjects();
Steve Blockd0582a62009-12-15 09:54:21 +00005637 *stats->code_space_capacity = code_space_->Capacity();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005638 *stats->map_space_size = map_space_->SizeOfObjects();
Steve Blockd0582a62009-12-15 09:54:21 +00005639 *stats->map_space_capacity = map_space_->Capacity();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005640 *stats->cell_space_size = cell_space_->SizeOfObjects();
Steve Blockd0582a62009-12-15 09:54:21 +00005641 *stats->cell_space_capacity = cell_space_->Capacity();
5642 *stats->lo_space_size = lo_space_->Size();
Steve Block44f0eee2011-05-26 01:26:41 +01005643 isolate_->global_handles()->RecordStats(stats);
5644 *stats->memory_allocator_size = isolate()->memory_allocator()->Size();
Ben Murdochbb769b22010-08-11 14:56:33 +01005645 *stats->memory_allocator_capacity =
Steve Block44f0eee2011-05-26 01:26:41 +01005646 isolate()->memory_allocator()->Size() +
5647 isolate()->memory_allocator()->Available();
Iain Merrick75681382010-08-19 15:07:18 +01005648 *stats->os_error = OS::GetLastError();
Steve Block44f0eee2011-05-26 01:26:41 +01005649 isolate()->memory_allocator()->Available();
Ben Murdochbb769b22010-08-11 14:56:33 +01005650 if (take_snapshot) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005651 HeapIterator iterator;
Ben Murdochbb769b22010-08-11 14:56:33 +01005652 for (HeapObject* obj = iterator.next();
5653 obj != NULL;
5654 obj = iterator.next()) {
Ben Murdochbb769b22010-08-11 14:56:33 +01005655 InstanceType type = obj->map()->instance_type();
5656 ASSERT(0 <= type && type <= LAST_TYPE);
5657 stats->objects_per_type[type]++;
5658 stats->size_per_type[type] += obj->Size();
5659 }
5660 }
Steve Blockd0582a62009-12-15 09:54:21 +00005661}
5662
5663
Ben Murdochf87a2032010-10-22 12:50:53 +01005664intptr_t Heap::PromotedSpaceSize() {
Steve Blocka7e24c12009-10-30 11:49:00 +00005665 return old_pointer_space_->Size()
5666 + old_data_space_->Size()
5667 + code_space_->Size()
5668 + map_space_->Size()
5669 + cell_space_->Size()
5670 + lo_space_->Size();
5671}
5672
5673
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005674intptr_t Heap::PromotedSpaceSizeOfObjects() {
5675 return old_pointer_space_->SizeOfObjects()
5676 + old_data_space_->SizeOfObjects()
5677 + code_space_->SizeOfObjects()
5678 + map_space_->SizeOfObjects()
5679 + cell_space_->SizeOfObjects()
5680 + lo_space_->SizeOfObjects();
5681}
5682
5683
Steve Blocka7e24c12009-10-30 11:49:00 +00005684int Heap::PromotedExternalMemorySize() {
5685 if (amount_of_external_allocated_memory_
5686 <= amount_of_external_allocated_memory_at_last_global_gc_) return 0;
5687 return amount_of_external_allocated_memory_
5688 - amount_of_external_allocated_memory_at_last_global_gc_;
5689}
5690
Steve Block44f0eee2011-05-26 01:26:41 +01005691#ifdef DEBUG
5692
5693// Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject.
5694static const int kMarkTag = 2;
5695
5696
5697class HeapDebugUtils {
5698 public:
5699 explicit HeapDebugUtils(Heap* heap)
5700 : search_for_any_global_(false),
5701 search_target_(NULL),
5702 found_target_(false),
5703 object_stack_(20),
5704 heap_(heap) {
5705 }
5706
5707 class MarkObjectVisitor : public ObjectVisitor {
5708 public:
5709 explicit MarkObjectVisitor(HeapDebugUtils* utils) : utils_(utils) { }
5710
5711 void VisitPointers(Object** start, Object** end) {
5712 // Copy all HeapObject pointers in [start, end)
5713 for (Object** p = start; p < end; p++) {
5714 if ((*p)->IsHeapObject())
5715 utils_->MarkObjectRecursively(p);
5716 }
5717 }
5718
5719 HeapDebugUtils* utils_;
5720 };
5721
5722 void MarkObjectRecursively(Object** p) {
5723 if (!(*p)->IsHeapObject()) return;
5724
5725 HeapObject* obj = HeapObject::cast(*p);
5726
5727 Object* map = obj->map();
5728
5729 if (!map->IsHeapObject()) return; // visited before
5730
5731 if (found_target_) return; // stop if target found
5732 object_stack_.Add(obj);
5733 if ((search_for_any_global_ && obj->IsJSGlobalObject()) ||
5734 (!search_for_any_global_ && (obj == search_target_))) {
5735 found_target_ = true;
5736 return;
5737 }
5738
5739 // not visited yet
5740 Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map));
5741
5742 Address map_addr = map_p->address();
5743
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005744 obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_addr + kMarkTag));
Steve Block44f0eee2011-05-26 01:26:41 +01005745
5746 MarkObjectRecursively(&map);
5747
5748 MarkObjectVisitor mark_visitor(this);
5749
5750 obj->IterateBody(map_p->instance_type(), obj->SizeFromMap(map_p),
5751 &mark_visitor);
5752
5753 if (!found_target_) // don't pop if found the target
5754 object_stack_.RemoveLast();
5755 }
5756
5757
5758 class UnmarkObjectVisitor : public ObjectVisitor {
5759 public:
5760 explicit UnmarkObjectVisitor(HeapDebugUtils* utils) : utils_(utils) { }
5761
5762 void VisitPointers(Object** start, Object** end) {
5763 // Copy all HeapObject pointers in [start, end)
5764 for (Object** p = start; p < end; p++) {
5765 if ((*p)->IsHeapObject())
5766 utils_->UnmarkObjectRecursively(p);
5767 }
5768 }
5769
5770 HeapDebugUtils* utils_;
5771 };
5772
5773
5774 void UnmarkObjectRecursively(Object** p) {
5775 if (!(*p)->IsHeapObject()) return;
5776
5777 HeapObject* obj = HeapObject::cast(*p);
5778
5779 Object* map = obj->map();
5780
5781 if (map->IsHeapObject()) return; // unmarked already
5782
5783 Address map_addr = reinterpret_cast<Address>(map);
5784
5785 map_addr -= kMarkTag;
5786
5787 ASSERT_TAG_ALIGNED(map_addr);
5788
5789 HeapObject* map_p = HeapObject::FromAddress(map_addr);
5790
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005791 obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_p));
Steve Block44f0eee2011-05-26 01:26:41 +01005792
5793 UnmarkObjectRecursively(reinterpret_cast<Object**>(&map_p));
5794
5795 UnmarkObjectVisitor unmark_visitor(this);
5796
5797 obj->IterateBody(Map::cast(map_p)->instance_type(),
5798 obj->SizeFromMap(Map::cast(map_p)),
5799 &unmark_visitor);
5800 }
5801
5802
5803 void MarkRootObjectRecursively(Object** root) {
5804 if (search_for_any_global_) {
5805 ASSERT(search_target_ == NULL);
5806 } else {
5807 ASSERT(search_target_->IsHeapObject());
5808 }
5809 found_target_ = false;
5810 object_stack_.Clear();
5811
5812 MarkObjectRecursively(root);
5813 UnmarkObjectRecursively(root);
5814
5815 if (found_target_) {
5816 PrintF("=====================================\n");
5817 PrintF("==== Path to object ====\n");
5818 PrintF("=====================================\n\n");
5819
5820 ASSERT(!object_stack_.is_empty());
5821 for (int i = 0; i < object_stack_.length(); i++) {
5822 if (i > 0) PrintF("\n |\n |\n V\n\n");
5823 Object* obj = object_stack_[i];
5824 obj->Print();
5825 }
5826 PrintF("=====================================\n");
5827 }
5828 }
5829
5830 // Helper class for visiting HeapObjects recursively.
5831 class MarkRootVisitor: public ObjectVisitor {
5832 public:
5833 explicit MarkRootVisitor(HeapDebugUtils* utils) : utils_(utils) { }
5834
5835 void VisitPointers(Object** start, Object** end) {
5836 // Visit all HeapObject pointers in [start, end)
5837 for (Object** p = start; p < end; p++) {
5838 if ((*p)->IsHeapObject())
5839 utils_->MarkRootObjectRecursively(p);
5840 }
5841 }
5842
5843 HeapDebugUtils* utils_;
5844 };
5845
5846 bool search_for_any_global_;
5847 Object* search_target_;
5848 bool found_target_;
5849 List<Object*> object_stack_;
5850 Heap* heap_;
5851
5852 friend class Heap;
5853};
5854
5855#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005856
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005857bool Heap::SetUp(bool create_heap_objects) {
Steve Block44f0eee2011-05-26 01:26:41 +01005858#ifdef DEBUG
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005859 allocation_timeout_ = FLAG_gc_interval;
Steve Block44f0eee2011-05-26 01:26:41 +01005860 debug_utils_ = new HeapDebugUtils(this);
5861#endif
5862
Steve Blocka7e24c12009-10-30 11:49:00 +00005863 // Initialize heap spaces and initial maps and objects. Whenever something
5864 // goes wrong, just return false. The caller should check the results and
5865 // call Heap::TearDown() to release allocated memory.
5866 //
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005867 // If the heap is not yet configured (e.g. through the API), configure it.
Steve Blocka7e24c12009-10-30 11:49:00 +00005868 // Configuration is based on the flags new-space-size (really the semispace
5869 // size) and old-space-size if set or the initial values of semispace_size_
5870 // and old_generation_size_ otherwise.
Steve Block44f0eee2011-05-26 01:26:41 +01005871 if (!configured_) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005872 if (!ConfigureHeapDefault()) return false;
5873 }
5874
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005875 gc_initializer_mutex.Pointer()->Lock();
Steve Block44f0eee2011-05-26 01:26:41 +01005876 static bool initialized_gc = false;
5877 if (!initialized_gc) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005878 initialized_gc = true;
5879 InitializeScavengingVisitorsTables();
5880 NewSpaceScavenger::Initialize();
5881 MarkCompactCollector::Initialize();
Steve Block44f0eee2011-05-26 01:26:41 +01005882 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005883 gc_initializer_mutex.Pointer()->Unlock();
Iain Merrick75681382010-08-19 15:07:18 +01005884
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005885 MarkMapPointersAsEncoded(false);
5886
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005887 // Set up memory allocator.
5888 if (!isolate_->memory_allocator()->SetUp(MaxReserved(), MaxExecutableSize()))
Steve Block44f0eee2011-05-26 01:26:41 +01005889 return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005890
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005891 // Set up new space.
5892 if (!new_space_.SetUp(reserved_semispace_size_, max_semispace_size_)) {
Steve Block3ce2e202009-11-05 08:53:23 +00005893 return false;
5894 }
Steve Blocka7e24c12009-10-30 11:49:00 +00005895
5896 // Initialize old pointer space.
5897 old_pointer_space_ =
Steve Block44f0eee2011-05-26 01:26:41 +01005898 new OldSpace(this,
5899 max_old_generation_size_,
5900 OLD_POINTER_SPACE,
5901 NOT_EXECUTABLE);
Steve Blocka7e24c12009-10-30 11:49:00 +00005902 if (old_pointer_space_ == NULL) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005903 if (!old_pointer_space_->SetUp()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005904
5905 // Initialize old data space.
5906 old_data_space_ =
Steve Block44f0eee2011-05-26 01:26:41 +01005907 new OldSpace(this,
5908 max_old_generation_size_,
5909 OLD_DATA_SPACE,
5910 NOT_EXECUTABLE);
Steve Blocka7e24c12009-10-30 11:49:00 +00005911 if (old_data_space_ == NULL) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005912 if (!old_data_space_->SetUp()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005913
5914 // Initialize the code space, set its maximum capacity to the old
5915 // generation size. It needs executable memory.
5916 // On 64-bit platform(s), we put all code objects in a 2 GB range of
5917 // virtual address space, so that they can call each other with near calls.
5918 if (code_range_size_ > 0) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005919 if (!isolate_->code_range()->SetUp(code_range_size_)) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005920 return false;
5921 }
5922 }
5923
5924 code_space_ =
Steve Block44f0eee2011-05-26 01:26:41 +01005925 new OldSpace(this, max_old_generation_size_, CODE_SPACE, EXECUTABLE);
Steve Blocka7e24c12009-10-30 11:49:00 +00005926 if (code_space_ == NULL) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005927 if (!code_space_->SetUp()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005928
5929 // Initialize map space.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005930 map_space_ = new MapSpace(this, max_old_generation_size_, MAP_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +00005931 if (map_space_ == NULL) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005932 if (!map_space_->SetUp()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005933
5934 // Initialize global property cell space.
Steve Block44f0eee2011-05-26 01:26:41 +01005935 cell_space_ = new CellSpace(this, max_old_generation_size_, CELL_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +00005936 if (cell_space_ == NULL) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005937 if (!cell_space_->SetUp()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +00005938
5939 // The large object code space may contain code or data. We set the memory
5940 // to be non-executable here for safety, but this means we need to enable it
5941 // explicitly when allocating large code objects.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005942 lo_space_ = new LargeObjectSpace(this, max_old_generation_size_, LO_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +00005943 if (lo_space_ == NULL) return false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005944 if (!lo_space_->SetUp()) return false;
Ben Murdochc7cc0282012-03-05 14:35:55 +00005945
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005946 // Set up the seed that is used to randomize the string hash function.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005947 ASSERT(hash_seed() == 0);
5948 if (FLAG_randomize_hashes) {
5949 if (FLAG_hash_seed == 0) {
5950 set_hash_seed(
5951 Smi::FromInt(V8::RandomPrivate(isolate()) & 0x3fffffff));
5952 } else {
5953 set_hash_seed(Smi::FromInt(FLAG_hash_seed));
5954 }
5955 }
5956
Steve Blocka7e24c12009-10-30 11:49:00 +00005957 if (create_heap_objects) {
5958 // Create initial maps.
5959 if (!CreateInitialMaps()) return false;
5960 if (!CreateApiObjects()) return false;
5961
5962 // Create initial objects
5963 if (!CreateInitialObjects()) return false;
Ben Murdochf87a2032010-10-22 12:50:53 +01005964
5965 global_contexts_list_ = undefined_value();
Steve Blocka7e24c12009-10-30 11:49:00 +00005966 }
5967
Steve Block44f0eee2011-05-26 01:26:41 +01005968 LOG(isolate_, IntPtrTEvent("heap-capacity", Capacity()));
5969 LOG(isolate_, IntPtrTEvent("heap-available", Available()));
Steve Blocka7e24c12009-10-30 11:49:00 +00005970
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005971 store_buffer()->SetUp();
5972
Steve Blocka7e24c12009-10-30 11:49:00 +00005973 return true;
5974}
5975
5976
Steve Blockd0582a62009-12-15 09:54:21 +00005977void Heap::SetStackLimits() {
Steve Block44f0eee2011-05-26 01:26:41 +01005978 ASSERT(isolate_ != NULL);
5979 ASSERT(isolate_ == isolate());
Steve Blocka7e24c12009-10-30 11:49:00 +00005980 // On 64 bit machines, pointers are generally out of range of Smis. We write
5981 // something that looks like an out of range Smi to the GC.
5982
Steve Blockd0582a62009-12-15 09:54:21 +00005983 // Set up the special root array entries containing the stack limits.
5984 // These are actually addresses, but the tag makes the GC ignore it.
Steve Blocka7e24c12009-10-30 11:49:00 +00005985 roots_[kStackLimitRootIndex] =
Steve Blockd0582a62009-12-15 09:54:21 +00005986 reinterpret_cast<Object*>(
Steve Block44f0eee2011-05-26 01:26:41 +01005987 (isolate_->stack_guard()->jslimit() & ~kSmiTagMask) | kSmiTag);
Steve Blockd0582a62009-12-15 09:54:21 +00005988 roots_[kRealStackLimitRootIndex] =
5989 reinterpret_cast<Object*>(
Steve Block44f0eee2011-05-26 01:26:41 +01005990 (isolate_->stack_guard()->real_jslimit() & ~kSmiTagMask) | kSmiTag);
Steve Blocka7e24c12009-10-30 11:49:00 +00005991}
5992
5993
5994void Heap::TearDown() {
Leon Clarkef7060e22010-06-03 12:02:55 +01005995 if (FLAG_print_cumulative_gc_stat) {
5996 PrintF("\n\n");
5997 PrintF("gc_count=%d ", gc_count_);
5998 PrintF("mark_sweep_count=%d ", ms_count_);
Steve Block44f0eee2011-05-26 01:26:41 +01005999 PrintF("max_gc_pause=%d ", get_max_gc_pause());
6000 PrintF("min_in_mutator=%d ", get_min_in_mutator());
Ben Murdochf87a2032010-10-22 12:50:53 +01006001 PrintF("max_alive_after_gc=%" V8_PTR_PREFIX "d ",
Steve Block44f0eee2011-05-26 01:26:41 +01006002 get_max_alive_after_gc());
Leon Clarkef7060e22010-06-03 12:02:55 +01006003 PrintF("\n\n");
6004 }
6005
Steve Block44f0eee2011-05-26 01:26:41 +01006006 isolate_->global_handles()->TearDown();
Steve Blocka7e24c12009-10-30 11:49:00 +00006007
Steve Block44f0eee2011-05-26 01:26:41 +01006008 external_string_table_.TearDown();
Leon Clarkee46be812010-01-19 14:06:41 +00006009
Steve Blocka7e24c12009-10-30 11:49:00 +00006010 new_space_.TearDown();
6011
6012 if (old_pointer_space_ != NULL) {
6013 old_pointer_space_->TearDown();
6014 delete old_pointer_space_;
6015 old_pointer_space_ = NULL;
6016 }
6017
6018 if (old_data_space_ != NULL) {
6019 old_data_space_->TearDown();
6020 delete old_data_space_;
6021 old_data_space_ = NULL;
6022 }
6023
6024 if (code_space_ != NULL) {
6025 code_space_->TearDown();
6026 delete code_space_;
6027 code_space_ = NULL;
6028 }
6029
6030 if (map_space_ != NULL) {
6031 map_space_->TearDown();
6032 delete map_space_;
6033 map_space_ = NULL;
6034 }
6035
6036 if (cell_space_ != NULL) {
6037 cell_space_->TearDown();
6038 delete cell_space_;
6039 cell_space_ = NULL;
6040 }
6041
6042 if (lo_space_ != NULL) {
6043 lo_space_->TearDown();
6044 delete lo_space_;
6045 lo_space_ = NULL;
6046 }
6047
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006048 store_buffer()->TearDown();
6049 incremental_marking()->TearDown();
6050
Steve Block44f0eee2011-05-26 01:26:41 +01006051 isolate_->memory_allocator()->TearDown();
6052
6053#ifdef DEBUG
6054 delete debug_utils_;
6055 debug_utils_ = NULL;
6056#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006057}
6058
6059
6060void Heap::Shrink() {
6061 // Try to shrink all paged spaces.
6062 PagedSpaces spaces;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006063 for (PagedSpace* space = spaces.next();
6064 space != NULL;
6065 space = spaces.next()) {
6066 space->ReleaseAllUnusedPages();
6067 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006068}
6069
6070
Steve Block6ded16b2010-05-10 14:33:55 +01006071void Heap::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) {
6072 ASSERT(callback != NULL);
6073 GCPrologueCallbackPair pair(callback, gc_type);
6074 ASSERT(!gc_prologue_callbacks_.Contains(pair));
6075 return gc_prologue_callbacks_.Add(pair);
6076}
6077
6078
6079void Heap::RemoveGCPrologueCallback(GCPrologueCallback callback) {
6080 ASSERT(callback != NULL);
6081 for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) {
6082 if (gc_prologue_callbacks_[i].callback == callback) {
6083 gc_prologue_callbacks_.Remove(i);
6084 return;
6085 }
6086 }
6087 UNREACHABLE();
6088}
6089
6090
6091void Heap::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) {
6092 ASSERT(callback != NULL);
6093 GCEpilogueCallbackPair pair(callback, gc_type);
6094 ASSERT(!gc_epilogue_callbacks_.Contains(pair));
6095 return gc_epilogue_callbacks_.Add(pair);
6096}
6097
6098
6099void Heap::RemoveGCEpilogueCallback(GCEpilogueCallback callback) {
6100 ASSERT(callback != NULL);
6101 for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) {
6102 if (gc_epilogue_callbacks_[i].callback == callback) {
6103 gc_epilogue_callbacks_.Remove(i);
6104 return;
6105 }
6106 }
6107 UNREACHABLE();
6108}
6109
6110
Steve Blocka7e24c12009-10-30 11:49:00 +00006111#ifdef DEBUG
6112
6113class PrintHandleVisitor: public ObjectVisitor {
6114 public:
6115 void VisitPointers(Object** start, Object** end) {
6116 for (Object** p = start; p < end; p++)
Ben Murdochf87a2032010-10-22 12:50:53 +01006117 PrintF(" handle %p to %p\n",
6118 reinterpret_cast<void*>(p),
6119 reinterpret_cast<void*>(*p));
Steve Blocka7e24c12009-10-30 11:49:00 +00006120 }
6121};
6122
6123void Heap::PrintHandles() {
6124 PrintF("Handles:\n");
6125 PrintHandleVisitor v;
Steve Block44f0eee2011-05-26 01:26:41 +01006126 isolate_->handle_scope_implementer()->Iterate(&v);
Steve Blocka7e24c12009-10-30 11:49:00 +00006127}
6128
6129#endif
6130
6131
6132Space* AllSpaces::next() {
6133 switch (counter_++) {
6134 case NEW_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006135 return HEAP->new_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006136 case OLD_POINTER_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006137 return HEAP->old_pointer_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006138 case OLD_DATA_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006139 return HEAP->old_data_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006140 case CODE_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006141 return HEAP->code_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006142 case MAP_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006143 return HEAP->map_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006144 case CELL_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006145 return HEAP->cell_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006146 case LO_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006147 return HEAP->lo_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006148 default:
6149 return NULL;
6150 }
6151}
6152
6153
6154PagedSpace* PagedSpaces::next() {
6155 switch (counter_++) {
6156 case OLD_POINTER_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006157 return HEAP->old_pointer_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006158 case OLD_DATA_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006159 return HEAP->old_data_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006160 case CODE_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006161 return HEAP->code_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006162 case MAP_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006163 return HEAP->map_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006164 case CELL_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006165 return HEAP->cell_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006166 default:
6167 return NULL;
6168 }
6169}
6170
6171
6172
6173OldSpace* OldSpaces::next() {
6174 switch (counter_++) {
6175 case OLD_POINTER_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006176 return HEAP->old_pointer_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006177 case OLD_DATA_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006178 return HEAP->old_data_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006179 case CODE_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006180 return HEAP->code_space();
Steve Blocka7e24c12009-10-30 11:49:00 +00006181 default:
6182 return NULL;
6183 }
6184}
6185
6186
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006187SpaceIterator::SpaceIterator()
6188 : current_space_(FIRST_SPACE),
6189 iterator_(NULL),
6190 size_func_(NULL) {
6191}
6192
6193
6194SpaceIterator::SpaceIterator(HeapObjectCallback size_func)
6195 : current_space_(FIRST_SPACE),
6196 iterator_(NULL),
6197 size_func_(size_func) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006198}
6199
6200
6201SpaceIterator::~SpaceIterator() {
6202 // Delete active iterator if any.
6203 delete iterator_;
6204}
6205
6206
6207bool SpaceIterator::has_next() {
6208 // Iterate until no more spaces.
6209 return current_space_ != LAST_SPACE;
6210}
6211
6212
6213ObjectIterator* SpaceIterator::next() {
6214 if (iterator_ != NULL) {
6215 delete iterator_;
6216 iterator_ = NULL;
6217 // Move to the next space
6218 current_space_++;
6219 if (current_space_ > LAST_SPACE) {
6220 return NULL;
6221 }
6222 }
6223
6224 // Return iterator for the new current space.
6225 return CreateIterator();
6226}
6227
6228
6229// Create an iterator for the space to iterate.
6230ObjectIterator* SpaceIterator::CreateIterator() {
6231 ASSERT(iterator_ == NULL);
6232
6233 switch (current_space_) {
6234 case NEW_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006235 iterator_ = new SemiSpaceIterator(HEAP->new_space(), size_func_);
Steve Blocka7e24c12009-10-30 11:49:00 +00006236 break;
6237 case OLD_POINTER_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006238 iterator_ = new HeapObjectIterator(HEAP->old_pointer_space(), size_func_);
Steve Blocka7e24c12009-10-30 11:49:00 +00006239 break;
6240 case OLD_DATA_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006241 iterator_ = new HeapObjectIterator(HEAP->old_data_space(), size_func_);
Steve Blocka7e24c12009-10-30 11:49:00 +00006242 break;
6243 case CODE_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006244 iterator_ = new HeapObjectIterator(HEAP->code_space(), size_func_);
Steve Blocka7e24c12009-10-30 11:49:00 +00006245 break;
6246 case MAP_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006247 iterator_ = new HeapObjectIterator(HEAP->map_space(), size_func_);
Steve Blocka7e24c12009-10-30 11:49:00 +00006248 break;
6249 case CELL_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006250 iterator_ = new HeapObjectIterator(HEAP->cell_space(), size_func_);
Steve Blocka7e24c12009-10-30 11:49:00 +00006251 break;
6252 case LO_SPACE:
Steve Block44f0eee2011-05-26 01:26:41 +01006253 iterator_ = new LargeObjectIterator(HEAP->lo_space(), size_func_);
Steve Blocka7e24c12009-10-30 11:49:00 +00006254 break;
6255 }
6256
6257 // Return the newly allocated iterator;
6258 ASSERT(iterator_ != NULL);
6259 return iterator_;
6260}
6261
6262
Ben Murdochb0fe1622011-05-05 13:52:32 +01006263class HeapObjectsFilter {
6264 public:
6265 virtual ~HeapObjectsFilter() {}
6266 virtual bool SkipObject(HeapObject* object) = 0;
6267};
6268
6269
Ben Murdoch85b71792012-04-11 18:30:58 +01006270class UnreachableObjectsFilter : public HeapObjectsFilter {
6271 public:
6272 UnreachableObjectsFilter() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006273 MarkReachableObjects();
6274 }
6275
6276 ~UnreachableObjectsFilter() {
6277 Isolate::Current()->heap()->mark_compact_collector()->ClearMarkbits();
Ben Murdoch85b71792012-04-11 18:30:58 +01006278 }
6279
6280 bool SkipObject(HeapObject* object) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006281 MarkBit mark_bit = Marking::MarkBitFrom(object);
6282 return !mark_bit.Get();
Ben Murdoch85b71792012-04-11 18:30:58 +01006283 }
6284
6285 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006286 class MarkingVisitor : public ObjectVisitor {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006287 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006288 MarkingVisitor() : marking_stack_(10) {}
Ben Murdochb0fe1622011-05-05 13:52:32 +01006289
6290 void VisitPointers(Object** start, Object** end) {
6291 for (Object** p = start; p < end; p++) {
6292 if (!(*p)->IsHeapObject()) continue;
6293 HeapObject* obj = HeapObject::cast(*p);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006294 MarkBit mark_bit = Marking::MarkBitFrom(obj);
6295 if (!mark_bit.Get()) {
6296 mark_bit.Set();
6297 marking_stack_.Add(obj);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006298 }
6299 }
6300 }
6301
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006302 void TransitiveClosure() {
6303 while (!marking_stack_.is_empty()) {
6304 HeapObject* obj = marking_stack_.RemoveLast();
6305 obj->Iterate(this);
6306 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01006307 }
6308
6309 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006310 List<HeapObject*> marking_stack_;
Ben Murdochb0fe1622011-05-05 13:52:32 +01006311 };
6312
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006313 void MarkReachableObjects() {
6314 Heap* heap = Isolate::Current()->heap();
6315 MarkingVisitor visitor;
6316 heap->IterateRoots(&visitor, VISIT_ALL);
6317 visitor.TransitiveClosure();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006318 }
6319
6320 AssertNoAllocation no_alloc;
6321};
6322
6323
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006324HeapIterator::HeapIterator()
6325 : filtering_(HeapIterator::kNoFiltering),
6326 filter_(NULL) {
6327 Init();
6328}
6329
6330
Ben Murdochb0fe1622011-05-05 13:52:32 +01006331HeapIterator::HeapIterator(HeapIterator::HeapObjectsFiltering filtering)
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006332 : filtering_(filtering),
6333 filter_(NULL) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006334 Init();
6335}
6336
6337
6338HeapIterator::~HeapIterator() {
6339 Shutdown();
6340}
6341
6342
6343void HeapIterator::Init() {
6344 // Start the iteration.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006345 space_iterator_ = new SpaceIterator;
Ben Murdochb0fe1622011-05-05 13:52:32 +01006346 switch (filtering_) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006347 case kFilterUnreachable:
6348 filter_ = new UnreachableObjectsFilter;
6349 break;
6350 default:
6351 break;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006352 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006353 object_iterator_ = space_iterator_->next();
6354}
6355
6356
6357void HeapIterator::Shutdown() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006358#ifdef DEBUG
Ben Murdochb0fe1622011-05-05 13:52:32 +01006359 // Assert that in filtering mode we have iterated through all
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006360 // objects. Otherwise, heap will be left in an inconsistent state.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006361 if (filtering_ != kNoFiltering) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006362 ASSERT(object_iterator_ == NULL);
6363 }
6364#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006365 // Make sure the last iterator is deallocated.
6366 delete space_iterator_;
6367 space_iterator_ = NULL;
6368 object_iterator_ = NULL;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006369 delete filter_;
6370 filter_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00006371}
6372
6373
Leon Clarked91b9f72010-01-27 17:25:45 +00006374HeapObject* HeapIterator::next() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006375 if (filter_ == NULL) return NextObject();
6376
6377 HeapObject* obj = NextObject();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006378 while (obj != NULL && filter_->SkipObject(obj)) obj = NextObject();
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006379 return obj;
6380}
6381
6382
6383HeapObject* HeapIterator::NextObject() {
Steve Blocka7e24c12009-10-30 11:49:00 +00006384 // No iterator means we are done.
Leon Clarked91b9f72010-01-27 17:25:45 +00006385 if (object_iterator_ == NULL) return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00006386
Leon Clarked91b9f72010-01-27 17:25:45 +00006387 if (HeapObject* obj = object_iterator_->next_object()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006388 // If the current iterator has more objects we are fine.
Leon Clarked91b9f72010-01-27 17:25:45 +00006389 return obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00006390 } else {
6391 // Go though the spaces looking for one that has objects.
6392 while (space_iterator_->has_next()) {
6393 object_iterator_ = space_iterator_->next();
Leon Clarked91b9f72010-01-27 17:25:45 +00006394 if (HeapObject* obj = object_iterator_->next_object()) {
6395 return obj;
Steve Blocka7e24c12009-10-30 11:49:00 +00006396 }
6397 }
6398 }
6399 // Done with the last space.
6400 object_iterator_ = NULL;
Leon Clarked91b9f72010-01-27 17:25:45 +00006401 return NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +00006402}
6403
6404
6405void HeapIterator::reset() {
6406 // Restart the iterator.
6407 Shutdown();
6408 Init();
6409}
6410
6411
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006412#if defined(DEBUG) || defined(LIVE_OBJECT_LIST)
Steve Blocka7e24c12009-10-30 11:49:00 +00006413
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006414Object* const PathTracer::kAnyGlobalObject = reinterpret_cast<Object*>(NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +00006415
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006416class PathTracer::MarkVisitor: public ObjectVisitor {
Steve Blocka7e24c12009-10-30 11:49:00 +00006417 public:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006418 explicit MarkVisitor(PathTracer* tracer) : tracer_(tracer) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00006419 void VisitPointers(Object** start, Object** end) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006420 // Scan all HeapObject pointers in [start, end)
6421 for (Object** p = start; !tracer_->found() && (p < end); p++) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006422 if ((*p)->IsHeapObject())
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006423 tracer_->MarkRecursively(p, this);
Steve Blocka7e24c12009-10-30 11:49:00 +00006424 }
6425 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006426
6427 private:
6428 PathTracer* tracer_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006429};
6430
Steve Blocka7e24c12009-10-30 11:49:00 +00006431
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006432class PathTracer::UnmarkVisitor: public ObjectVisitor {
6433 public:
6434 explicit UnmarkVisitor(PathTracer* tracer) : tracer_(tracer) {}
6435 void VisitPointers(Object** start, Object** end) {
6436 // Scan all HeapObject pointers in [start, end)
6437 for (Object** p = start; p < end; p++) {
6438 if ((*p)->IsHeapObject())
6439 tracer_->UnmarkRecursively(p, this);
6440 }
6441 }
6442
6443 private:
6444 PathTracer* tracer_;
6445};
6446
6447
6448void PathTracer::VisitPointers(Object** start, Object** end) {
6449 bool done = ((what_to_find_ == FIND_FIRST) && found_target_);
6450 // Visit all HeapObject pointers in [start, end)
6451 for (Object** p = start; !done && (p < end); p++) {
6452 if ((*p)->IsHeapObject()) {
6453 TracePathFrom(p);
6454 done = ((what_to_find_ == FIND_FIRST) && found_target_);
6455 }
6456 }
6457}
6458
6459
6460void PathTracer::Reset() {
6461 found_target_ = false;
6462 object_stack_.Clear();
6463}
6464
6465
6466void PathTracer::TracePathFrom(Object** root) {
6467 ASSERT((search_target_ == kAnyGlobalObject) ||
6468 search_target_->IsHeapObject());
6469 found_target_in_trace_ = false;
6470 object_stack_.Clear();
6471
6472 MarkVisitor mark_visitor(this);
6473 MarkRecursively(root, &mark_visitor);
6474
6475 UnmarkVisitor unmark_visitor(this);
6476 UnmarkRecursively(root, &unmark_visitor);
6477
6478 ProcessResults();
6479}
6480
6481
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006482static bool SafeIsGlobalContext(HeapObject* obj) {
6483 return obj->map() == obj->GetHeap()->raw_unchecked_global_context_map();
6484}
6485
6486
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006487void PathTracer::MarkRecursively(Object** p, MarkVisitor* mark_visitor) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006488 if (!(*p)->IsHeapObject()) return;
6489
6490 HeapObject* obj = HeapObject::cast(*p);
6491
6492 Object* map = obj->map();
6493
6494 if (!map->IsHeapObject()) return; // visited before
6495
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006496 if (found_target_in_trace_) return; // stop if target found
6497 object_stack_.Add(obj);
6498 if (((search_target_ == kAnyGlobalObject) && obj->IsJSGlobalObject()) ||
6499 (obj == search_target_)) {
6500 found_target_in_trace_ = true;
6501 found_target_ = true;
Steve Blocka7e24c12009-10-30 11:49:00 +00006502 return;
6503 }
6504
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006505 bool is_global_context = SafeIsGlobalContext(obj);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006506
Steve Blocka7e24c12009-10-30 11:49:00 +00006507 // not visited yet
6508 Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map));
6509
6510 Address map_addr = map_p->address();
6511
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006512 obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_addr + kMarkTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00006513
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006514 // Scan the object body.
6515 if (is_global_context && (visit_mode_ == VISIT_ONLY_STRONG)) {
6516 // This is specialized to scan Context's properly.
6517 Object** start = reinterpret_cast<Object**>(obj->address() +
6518 Context::kHeaderSize);
6519 Object** end = reinterpret_cast<Object**>(obj->address() +
6520 Context::kHeaderSize + Context::FIRST_WEAK_SLOT * kPointerSize);
6521 mark_visitor->VisitPointers(start, end);
6522 } else {
6523 obj->IterateBody(map_p->instance_type(),
6524 obj->SizeFromMap(map_p),
6525 mark_visitor);
6526 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006527
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006528 // Scan the map after the body because the body is a lot more interesting
6529 // when doing leak detection.
6530 MarkRecursively(&map, mark_visitor);
Steve Blocka7e24c12009-10-30 11:49:00 +00006531
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006532 if (!found_target_in_trace_) // don't pop if found the target
6533 object_stack_.RemoveLast();
Steve Blocka7e24c12009-10-30 11:49:00 +00006534}
6535
6536
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006537void PathTracer::UnmarkRecursively(Object** p, UnmarkVisitor* unmark_visitor) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006538 if (!(*p)->IsHeapObject()) return;
6539
6540 HeapObject* obj = HeapObject::cast(*p);
6541
6542 Object* map = obj->map();
6543
6544 if (map->IsHeapObject()) return; // unmarked already
6545
6546 Address map_addr = reinterpret_cast<Address>(map);
6547
6548 map_addr -= kMarkTag;
6549
6550 ASSERT_TAG_ALIGNED(map_addr);
6551
6552 HeapObject* map_p = HeapObject::FromAddress(map_addr);
6553
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006554 obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_p));
Steve Blocka7e24c12009-10-30 11:49:00 +00006555
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006556 UnmarkRecursively(reinterpret_cast<Object**>(&map_p), unmark_visitor);
Steve Blocka7e24c12009-10-30 11:49:00 +00006557
6558 obj->IterateBody(Map::cast(map_p)->instance_type(),
6559 obj->SizeFromMap(Map::cast(map_p)),
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006560 unmark_visitor);
Steve Blocka7e24c12009-10-30 11:49:00 +00006561}
6562
6563
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006564void PathTracer::ProcessResults() {
6565 if (found_target_) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006566 PrintF("=====================================\n");
6567 PrintF("==== Path to object ====\n");
6568 PrintF("=====================================\n\n");
6569
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006570 ASSERT(!object_stack_.is_empty());
6571 for (int i = 0; i < object_stack_.length(); i++) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006572 if (i > 0) PrintF("\n |\n |\n V\n\n");
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006573 Object* obj = object_stack_[i];
6574#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00006575 obj->Print();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006576#else
6577 obj->ShortPrint();
6578#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006579 }
6580 PrintF("=====================================\n");
6581 }
6582}
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006583#endif // DEBUG || LIVE_OBJECT_LIST
Steve Blocka7e24c12009-10-30 11:49:00 +00006584
6585
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006586#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006587// Triggers a depth-first traversal of reachable objects from roots
6588// and finds a path to a specific heap object and prints it.
Leon Clarkee46be812010-01-19 14:06:41 +00006589void Heap::TracePathToObject(Object* target) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006590 PathTracer tracer(target, PathTracer::FIND_ALL, VISIT_ALL);
6591 IterateRoots(&tracer, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00006592}
6593
6594
6595// Triggers a depth-first traversal of reachable objects from roots
6596// and finds a path to any global object and prints it. Useful for
6597// determining the source for leaks of global objects.
6598void Heap::TracePathToGlobal() {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006599 PathTracer tracer(PathTracer::kAnyGlobalObject,
6600 PathTracer::FIND_ALL,
6601 VISIT_ALL);
6602 IterateRoots(&tracer, VISIT_ONLY_STRONG);
Steve Blocka7e24c12009-10-30 11:49:00 +00006603}
6604#endif
6605
6606
Ben Murdochf87a2032010-10-22 12:50:53 +01006607static intptr_t CountTotalHolesSize() {
6608 intptr_t holes_size = 0;
Leon Clarkef7060e22010-06-03 12:02:55 +01006609 OldSpaces spaces;
6610 for (OldSpace* space = spaces.next();
6611 space != NULL;
6612 space = spaces.next()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006613 holes_size += space->Waste() + space->Available();
Leon Clarkef7060e22010-06-03 12:02:55 +01006614 }
6615 return holes_size;
6616}
6617
6618
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006619GCTracer::GCTracer(Heap* heap,
6620 const char* gc_reason,
6621 const char* collector_reason)
Steve Blocka7e24c12009-10-30 11:49:00 +00006622 : start_time_(0.0),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006623 start_object_size_(0),
6624 start_memory_size_(0),
Steve Blocka7e24c12009-10-30 11:49:00 +00006625 gc_count_(0),
6626 full_gc_count_(0),
Leon Clarkef7060e22010-06-03 12:02:55 +01006627 allocated_since_last_gc_(0),
6628 spent_in_mutator_(0),
Steve Block44f0eee2011-05-26 01:26:41 +01006629 promoted_objects_size_(0),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006630 heap_(heap),
6631 gc_reason_(gc_reason),
6632 collector_reason_(collector_reason) {
Leon Clarkef7060e22010-06-03 12:02:55 +01006633 if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return;
Steve Blocka7e24c12009-10-30 11:49:00 +00006634 start_time_ = OS::TimeCurrentMillis();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006635 start_object_size_ = heap_->SizeOfObjects();
6636 start_memory_size_ = heap_->isolate()->memory_allocator()->Size();
Leon Clarkef7060e22010-06-03 12:02:55 +01006637
6638 for (int i = 0; i < Scope::kNumberOfScopes; i++) {
6639 scopes_[i] = 0;
6640 }
6641
6642 in_free_list_or_wasted_before_gc_ = CountTotalHolesSize();
6643
Steve Block44f0eee2011-05-26 01:26:41 +01006644 allocated_since_last_gc_ =
6645 heap_->SizeOfObjects() - heap_->alive_after_last_gc_;
Leon Clarkef7060e22010-06-03 12:02:55 +01006646
Steve Block44f0eee2011-05-26 01:26:41 +01006647 if (heap_->last_gc_end_timestamp_ > 0) {
6648 spent_in_mutator_ = Max(start_time_ - heap_->last_gc_end_timestamp_, 0.0);
Leon Clarkef7060e22010-06-03 12:02:55 +01006649 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006650
6651 steps_count_ = heap_->incremental_marking()->steps_count();
6652 steps_took_ = heap_->incremental_marking()->steps_took();
6653 longest_step_ = heap_->incremental_marking()->longest_step();
6654 steps_count_since_last_gc_ =
6655 heap_->incremental_marking()->steps_count_since_last_gc();
6656 steps_took_since_last_gc_ =
6657 heap_->incremental_marking()->steps_took_since_last_gc();
Steve Blocka7e24c12009-10-30 11:49:00 +00006658}
6659
6660
6661GCTracer::~GCTracer() {
Steve Blocka7e24c12009-10-30 11:49:00 +00006662 // Printf ONE line iff flag is set.
Leon Clarkef7060e22010-06-03 12:02:55 +01006663 if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return;
6664
Steve Block44f0eee2011-05-26 01:26:41 +01006665 bool first_gc = (heap_->last_gc_end_timestamp_ == 0);
Leon Clarkef7060e22010-06-03 12:02:55 +01006666
Steve Block44f0eee2011-05-26 01:26:41 +01006667 heap_->alive_after_last_gc_ = heap_->SizeOfObjects();
6668 heap_->last_gc_end_timestamp_ = OS::TimeCurrentMillis();
Leon Clarkef7060e22010-06-03 12:02:55 +01006669
Steve Block44f0eee2011-05-26 01:26:41 +01006670 int time = static_cast<int>(heap_->last_gc_end_timestamp_ - start_time_);
Leon Clarkef7060e22010-06-03 12:02:55 +01006671
6672 // Update cumulative GC statistics if required.
6673 if (FLAG_print_cumulative_gc_stat) {
Steve Block44f0eee2011-05-26 01:26:41 +01006674 heap_->max_gc_pause_ = Max(heap_->max_gc_pause_, time);
6675 heap_->max_alive_after_gc_ = Max(heap_->max_alive_after_gc_,
6676 heap_->alive_after_last_gc_);
Leon Clarkef7060e22010-06-03 12:02:55 +01006677 if (!first_gc) {
Steve Block44f0eee2011-05-26 01:26:41 +01006678 heap_->min_in_mutator_ = Min(heap_->min_in_mutator_,
6679 static_cast<int>(spent_in_mutator_));
Leon Clarkef7060e22010-06-03 12:02:55 +01006680 }
6681 }
6682
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006683 PrintF("%8.0f ms: ", heap_->isolate()->time_millis_since_init());
6684
Leon Clarkef7060e22010-06-03 12:02:55 +01006685 if (!FLAG_trace_gc_nvp) {
6686 int external_time = static_cast<int>(scopes_[Scope::EXTERNAL]);
6687
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006688 double end_memory_size_mb =
6689 static_cast<double>(heap_->isolate()->memory_allocator()->Size()) / MB;
6690
6691 PrintF("%s %.1f (%.1f) -> %.1f (%.1f) MB, ",
Leon Clarkef7060e22010-06-03 12:02:55 +01006692 CollectorString(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006693 static_cast<double>(start_object_size_) / MB,
6694 static_cast<double>(start_memory_size_) / MB,
6695 SizeOfHeapObjects(),
6696 end_memory_size_mb);
Leon Clarkef7060e22010-06-03 12:02:55 +01006697
6698 if (external_time > 0) PrintF("%d / ", external_time);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006699 PrintF("%d ms", time);
6700 if (steps_count_ > 0) {
6701 if (collector_ == SCAVENGER) {
6702 PrintF(" (+ %d ms in %d steps since last GC)",
6703 static_cast<int>(steps_took_since_last_gc_),
6704 steps_count_since_last_gc_);
6705 } else {
6706 PrintF(" (+ %d ms in %d steps since start of marking, "
6707 "biggest step %f ms)",
6708 static_cast<int>(steps_took_),
6709 steps_count_,
6710 longest_step_);
6711 }
6712 }
6713
6714 if (gc_reason_ != NULL) {
6715 PrintF(" [%s]", gc_reason_);
6716 }
6717
6718 if (collector_reason_ != NULL) {
6719 PrintF(" [%s]", collector_reason_);
6720 }
6721
6722 PrintF(".\n");
Leon Clarkef7060e22010-06-03 12:02:55 +01006723 } else {
6724 PrintF("pause=%d ", time);
6725 PrintF("mutator=%d ",
6726 static_cast<int>(spent_in_mutator_));
6727
6728 PrintF("gc=");
6729 switch (collector_) {
6730 case SCAVENGER:
6731 PrintF("s");
6732 break;
6733 case MARK_COMPACTOR:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006734 PrintF("ms");
Leon Clarkef7060e22010-06-03 12:02:55 +01006735 break;
6736 default:
6737 UNREACHABLE();
6738 }
6739 PrintF(" ");
6740
6741 PrintF("external=%d ", static_cast<int>(scopes_[Scope::EXTERNAL]));
6742 PrintF("mark=%d ", static_cast<int>(scopes_[Scope::MC_MARK]));
6743 PrintF("sweep=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP]));
Iain Merrick75681382010-08-19 15:07:18 +01006744 PrintF("sweepns=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP_NEWSPACE]));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006745 PrintF("evacuate=%d ", static_cast<int>(scopes_[Scope::MC_EVACUATE_PAGES]));
6746 PrintF("new_new=%d ",
6747 static_cast<int>(scopes_[Scope::MC_UPDATE_NEW_TO_NEW_POINTERS]));
6748 PrintF("root_new=%d ",
6749 static_cast<int>(scopes_[Scope::MC_UPDATE_ROOT_TO_NEW_POINTERS]));
6750 PrintF("old_new=%d ",
6751 static_cast<int>(scopes_[Scope::MC_UPDATE_OLD_TO_NEW_POINTERS]));
6752 PrintF("compaction_ptrs=%d ",
6753 static_cast<int>(scopes_[Scope::MC_UPDATE_POINTERS_TO_EVACUATED]));
6754 PrintF("intracompaction_ptrs=%d ", static_cast<int>(scopes_[
6755 Scope::MC_UPDATE_POINTERS_BETWEEN_EVACUATED]));
6756 PrintF("misc_compaction=%d ",
6757 static_cast<int>(scopes_[Scope::MC_UPDATE_MISC_POINTERS]));
Leon Clarkef7060e22010-06-03 12:02:55 +01006758
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006759 PrintF("total_size_before=%" V8_PTR_PREFIX "d ", start_object_size_);
Steve Block44f0eee2011-05-26 01:26:41 +01006760 PrintF("total_size_after=%" V8_PTR_PREFIX "d ", heap_->SizeOfObjects());
Ben Murdochf87a2032010-10-22 12:50:53 +01006761 PrintF("holes_size_before=%" V8_PTR_PREFIX "d ",
6762 in_free_list_or_wasted_before_gc_);
6763 PrintF("holes_size_after=%" V8_PTR_PREFIX "d ", CountTotalHolesSize());
Leon Clarkef7060e22010-06-03 12:02:55 +01006764
Ben Murdochf87a2032010-10-22 12:50:53 +01006765 PrintF("allocated=%" V8_PTR_PREFIX "d ", allocated_since_last_gc_);
6766 PrintF("promoted=%" V8_PTR_PREFIX "d ", promoted_objects_size_);
Leon Clarkef7060e22010-06-03 12:02:55 +01006767
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006768 if (collector_ == SCAVENGER) {
6769 PrintF("stepscount=%d ", steps_count_since_last_gc_);
6770 PrintF("stepstook=%d ", static_cast<int>(steps_took_since_last_gc_));
6771 } else {
6772 PrintF("stepscount=%d ", steps_count_);
6773 PrintF("stepstook=%d ", static_cast<int>(steps_took_));
6774 }
6775
Leon Clarkef7060e22010-06-03 12:02:55 +01006776 PrintF("\n");
6777 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006778
Steve Block44f0eee2011-05-26 01:26:41 +01006779 heap_->PrintShortHeapStatistics();
Steve Blocka7e24c12009-10-30 11:49:00 +00006780}
6781
6782
6783const char* GCTracer::CollectorString() {
6784 switch (collector_) {
6785 case SCAVENGER:
6786 return "Scavenge";
6787 case MARK_COMPACTOR:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006788 return "Mark-sweep";
Steve Blocka7e24c12009-10-30 11:49:00 +00006789 }
6790 return "Unknown GC";
6791}
6792
6793
6794int KeyedLookupCache::Hash(Map* map, String* name) {
6795 // Uses only lower 32 bits if pointers are larger.
6796 uintptr_t addr_hash =
Leon Clarkee46be812010-01-19 14:06:41 +00006797 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)) >> kMapHashShift;
Andrei Popescu402d9372010-02-26 13:31:12 +00006798 return static_cast<uint32_t>((addr_hash ^ name->Hash()) & kCapacityMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00006799}
6800
6801
6802int KeyedLookupCache::Lookup(Map* map, String* name) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006803 int index = (Hash(map, name) & kHashMask);
6804 for (int i = 0; i < kEntriesPerBucket; i++) {
6805 Key& key = keys_[index + i];
6806 if ((key.map == map) && key.name->Equals(name)) {
6807 return field_offsets_[index + i];
6808 }
Steve Blocka7e24c12009-10-30 11:49:00 +00006809 }
Steve Block44f0eee2011-05-26 01:26:41 +01006810 return kNotFound;
Steve Blocka7e24c12009-10-30 11:49:00 +00006811}
6812
6813
6814void KeyedLookupCache::Update(Map* map, String* name, int field_offset) {
6815 String* symbol;
Steve Block44f0eee2011-05-26 01:26:41 +01006816 if (HEAP->LookupSymbolIfExists(name, &symbol)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006817 int index = (Hash(map, symbol) & kHashMask);
6818 // After a GC there will be free slots, so we use them in order (this may
6819 // help to get the most frequently used one in position 0).
6820 for (int i = 0; i< kEntriesPerBucket; i++) {
6821 Key& key = keys_[index];
6822 Object* free_entry_indicator = NULL;
6823 if (key.map == free_entry_indicator) {
6824 key.map = map;
6825 key.name = symbol;
6826 field_offsets_[index + i] = field_offset;
6827 return;
6828 }
6829 }
6830 // No free entry found in this bucket, so we move them all down one and
6831 // put the new entry at position zero.
6832 for (int i = kEntriesPerBucket - 1; i > 0; i--) {
6833 Key& key = keys_[index + i];
6834 Key& key2 = keys_[index + i - 1];
6835 key = key2;
6836 field_offsets_[index + i] = field_offsets_[index + i - 1];
6837 }
6838
6839 // Write the new first entry.
Steve Blocka7e24c12009-10-30 11:49:00 +00006840 Key& key = keys_[index];
6841 key.map = map;
6842 key.name = symbol;
6843 field_offsets_[index] = field_offset;
6844 }
6845}
6846
6847
6848void KeyedLookupCache::Clear() {
6849 for (int index = 0; index < kLength; index++) keys_[index].map = NULL;
6850}
6851
6852
Steve Blocka7e24c12009-10-30 11:49:00 +00006853void DescriptorLookupCache::Clear() {
6854 for (int index = 0; index < kLength; index++) keys_[index].array = NULL;
6855}
6856
6857
Steve Blocka7e24c12009-10-30 11:49:00 +00006858#ifdef DEBUG
Ben Murdochf87a2032010-10-22 12:50:53 +01006859void Heap::GarbageCollectionGreedyCheck() {
Steve Blocka7e24c12009-10-30 11:49:00 +00006860 ASSERT(FLAG_gc_greedy);
Steve Block44f0eee2011-05-26 01:26:41 +01006861 if (isolate_->bootstrapper()->IsActive()) return;
Ben Murdochf87a2032010-10-22 12:50:53 +01006862 if (disallow_allocation_failure()) return;
6863 CollectGarbage(NEW_SPACE);
Steve Blocka7e24c12009-10-30 11:49:00 +00006864}
6865#endif
6866
6867
Steve Block44f0eee2011-05-26 01:26:41 +01006868TranscendentalCache::SubCache::SubCache(Type t)
6869 : type_(t),
6870 isolate_(Isolate::Current()) {
Steve Blocka7e24c12009-10-30 11:49:00 +00006871 uint32_t in0 = 0xffffffffu; // Bit-pattern for a NaN that isn't
6872 uint32_t in1 = 0xffffffffu; // generated by the FPU.
6873 for (int i = 0; i < kCacheSize; i++) {
6874 elements_[i].in[0] = in0;
6875 elements_[i].in[1] = in1;
6876 elements_[i].output = NULL;
6877 }
6878}
6879
6880
Steve Blocka7e24c12009-10-30 11:49:00 +00006881void TranscendentalCache::Clear() {
6882 for (int i = 0; i < kNumberOfCaches; i++) {
6883 if (caches_[i] != NULL) {
6884 delete caches_[i];
6885 caches_[i] = NULL;
6886 }
6887 }
6888}
6889
6890
Leon Clarkee46be812010-01-19 14:06:41 +00006891void ExternalStringTable::CleanUp() {
6892 int last = 0;
6893 for (int i = 0; i < new_space_strings_.length(); ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006894 if (new_space_strings_[i] == heap_->raw_unchecked_the_hole_value()) {
6895 continue;
6896 }
Steve Block44f0eee2011-05-26 01:26:41 +01006897 if (heap_->InNewSpace(new_space_strings_[i])) {
Leon Clarkee46be812010-01-19 14:06:41 +00006898 new_space_strings_[last++] = new_space_strings_[i];
6899 } else {
6900 old_space_strings_.Add(new_space_strings_[i]);
6901 }
6902 }
6903 new_space_strings_.Rewind(last);
6904 last = 0;
6905 for (int i = 0; i < old_space_strings_.length(); ++i) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006906 if (old_space_strings_[i] == heap_->raw_unchecked_the_hole_value()) {
6907 continue;
6908 }
Steve Block44f0eee2011-05-26 01:26:41 +01006909 ASSERT(!heap_->InNewSpace(old_space_strings_[i]));
Leon Clarkee46be812010-01-19 14:06:41 +00006910 old_space_strings_[last++] = old_space_strings_[i];
6911 }
6912 old_space_strings_.Rewind(last);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006913 if (FLAG_verify_heap) {
6914 Verify();
6915 }
Leon Clarkee46be812010-01-19 14:06:41 +00006916}
6917
6918
6919void ExternalStringTable::TearDown() {
6920 new_space_strings_.Free();
6921 old_space_strings_.Free();
6922}
6923
6924
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006925void Heap::QueueMemoryChunkForFree(MemoryChunk* chunk) {
6926 chunk->set_next_chunk(chunks_queued_for_free_);
6927 chunks_queued_for_free_ = chunk;
6928}
6929
6930
6931void Heap::FreeQueuedChunks() {
6932 if (chunks_queued_for_free_ == NULL) return;
6933 MemoryChunk* next;
6934 MemoryChunk* chunk;
6935 for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) {
6936 next = chunk->next_chunk();
6937 chunk->SetFlag(MemoryChunk::ABOUT_TO_BE_FREED);
6938
6939 if (chunk->owner()->identity() == LO_SPACE) {
6940 // StoreBuffer::Filter relies on MemoryChunk::FromAnyPointerAddress.
6941 // If FromAnyPointerAddress encounters a slot that belongs to a large
6942 // chunk queued for deletion it will fail to find the chunk because
6943 // it try to perform a search in the list of pages owned by of the large
6944 // object space and queued chunks were detached from that list.
6945 // To work around this we split large chunk into normal kPageSize aligned
6946 // pieces and initialize size, owner and flags field of every piece.
6947 // If FromAnyPointerAddress encounters a slot that belongs to one of
6948 // these smaller pieces it will treat it as a slot on a normal Page.
6949 Address chunk_end = chunk->address() + chunk->size();
6950 MemoryChunk* inner = MemoryChunk::FromAddress(
6951 chunk->address() + Page::kPageSize);
6952 MemoryChunk* inner_last = MemoryChunk::FromAddress(chunk_end - 1);
6953 while (inner <= inner_last) {
6954 // Size of a large chunk is always a multiple of
6955 // OS::AllocateAlignment() so there is always
6956 // enough space for a fake MemoryChunk header.
6957 Address area_end = Min(inner->address() + Page::kPageSize, chunk_end);
6958 // Guard against overflow.
6959 if (area_end < inner->address()) area_end = chunk_end;
6960 inner->SetArea(inner->address(), area_end);
6961 inner->set_size(Page::kPageSize);
6962 inner->set_owner(lo_space());
6963 inner->SetFlag(MemoryChunk::ABOUT_TO_BE_FREED);
6964 inner = MemoryChunk::FromAddress(
6965 inner->address() + Page::kPageSize);
6966 }
6967 }
6968 }
6969 isolate_->heap()->store_buffer()->Compact();
6970 isolate_->heap()->store_buffer()->Filter(MemoryChunk::ABOUT_TO_BE_FREED);
6971 for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) {
6972 next = chunk->next_chunk();
6973 isolate_->memory_allocator()->Free(chunk);
6974 }
6975 chunks_queued_for_free_ = NULL;
6976}
6977
6978
6979void Heap::RememberUnmappedPage(Address page, bool compacted) {
6980 uintptr_t p = reinterpret_cast<uintptr_t>(page);
6981 // Tag the page pointer to make it findable in the dump file.
6982 if (compacted) {
6983 p ^= 0xc1ead & (Page::kPageSize - 1); // Cleared.
6984 } else {
6985 p ^= 0x1d1ed & (Page::kPageSize - 1); // I died.
6986 }
6987 remembered_unmapped_pages_[remembered_unmapped_pages_index_] =
6988 reinterpret_cast<Address>(p);
6989 remembered_unmapped_pages_index_++;
6990 remembered_unmapped_pages_index_ %= kRememberedUnmappedPages;
6991}
6992
Steve Blocka7e24c12009-10-30 11:49:00 +00006993} } // namespace v8::internal