blob: b477b45e07b3340f729d1275445407fc7edbd4d7 [file] [log] [blame]
ager@chromium.org71daaf62009-04-01 07:22:49 +00001// Copyright 2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "accessors.h"
31#include "api.h"
32#include "bootstrapper.h"
33#include "codegen-inl.h"
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000034#include "compilation-cache.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "debug.h"
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000036#include "heap-profiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000037#include "global-handles.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038#include "mark-compact.h"
39#include "natives.h"
40#include "scanner.h"
41#include "scopeinfo.h"
ager@chromium.org3811b432009-10-28 14:53:37 +000042#include "snapshot.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043#include "v8threads.h"
ager@chromium.org18ad94b2009-09-02 08:22:29 +000044#if V8_TARGET_ARCH_ARM && V8_NATIVE_REGEXP
45#include "regexp-macro-assembler.h"
ager@chromium.org3811b432009-10-28 14:53:37 +000046#include "arm/regexp-macro-assembler-arm.h"
ager@chromium.org18ad94b2009-09-02 08:22:29 +000047#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000049
kasperl@chromium.org71affb52009-05-26 05:44:31 +000050namespace v8 {
51namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
ager@chromium.org3b45ab52009-03-19 22:21:34 +000054String* Heap::hidden_symbol_;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000055Object* Heap::roots_[Heap::kRootListLength];
56
ager@chromium.org3b45ab52009-03-19 22:21:34 +000057
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000058NewSpace Heap::new_space_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +000059OldSpace* Heap::old_pointer_space_ = NULL;
60OldSpace* Heap::old_data_space_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061OldSpace* Heap::code_space_ = NULL;
62MapSpace* Heap::map_space_ = NULL;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +000063CellSpace* Heap::cell_space_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064LargeObjectSpace* Heap::lo_space_ = NULL;
65
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000066static const int kMinimumPromotionLimit = 2*MB;
67static const int kMinimumAllocationLimit = 8*MB;
68
69int Heap::old_gen_promotion_limit_ = kMinimumPromotionLimit;
70int Heap::old_gen_allocation_limit_ = kMinimumAllocationLimit;
71
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000072int Heap::old_gen_exhausted_ = false;
73
kasper.lund7276f142008-07-30 08:49:36 +000074int Heap::amount_of_external_allocated_memory_ = 0;
75int Heap::amount_of_external_allocated_memory_at_last_global_gc_ = 0;
76
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077// semispace_size_ should be a power of 2 and old_generation_size_ should be
78// a multiple of Page::kPageSize.
kasperl@chromium.orge959c182009-07-27 08:59:04 +000079#if defined(ANDROID)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000080int Heap::max_semispace_size_ = 2*MB;
81int Heap::max_old_generation_size_ = 192*MB;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000082int Heap::initial_semispace_size_ = 128*KB;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000083size_t Heap::code_range_size_ = 0;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +000084#elif defined(V8_TARGET_ARCH_X64)
ager@chromium.org3811b432009-10-28 14:53:37 +000085int Heap::max_semispace_size_ = 16*MB;
86int Heap::max_old_generation_size_ = 1*GB;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000087int Heap::initial_semispace_size_ = 1*MB;
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +000088size_t Heap::code_range_size_ = 512*MB;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000089#else
ager@chromium.org3811b432009-10-28 14:53:37 +000090int Heap::max_semispace_size_ = 8*MB;
91int Heap::max_old_generation_size_ = 512*MB;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000092int Heap::initial_semispace_size_ = 512*KB;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000093size_t Heap::code_range_size_ = 0;
ager@chromium.orgeadaf222009-06-16 09:43:10 +000094#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095
ager@chromium.org3811b432009-10-28 14:53:37 +000096// The snapshot semispace size will be the default semispace size if
97// snapshotting is used and will be the requested semispace size as
98// set up by ConfigureHeap otherwise.
99int Heap::reserved_semispace_size_ = Heap::max_semispace_size_;
100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101GCCallback Heap::global_gc_prologue_callback_ = NULL;
102GCCallback Heap::global_gc_epilogue_callback_ = NULL;
103
104// Variables set based on semispace_size_ and old_generation_size_ in
105// ConfigureHeap.
ager@chromium.org3811b432009-10-28 14:53:37 +0000106
107// Will be 4 * reserved_semispace_size_ to ensure that young
108// generation can be aligned to its size.
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000109int Heap::survived_since_last_expansion_ = 0;
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000110int Heap::external_allocation_limit_ = 0;
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112Heap::HeapState Heap::gc_state_ = NOT_IN_GC;
113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114int Heap::mc_count_ = 0;
115int Heap::gc_count_ = 0;
116
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000117int Heap::unflattended_strings_length_ = 0;
118
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000119int Heap::always_allocate_scope_depth_ = 0;
ager@chromium.org3811b432009-10-28 14:53:37 +0000120int Heap::linear_allocation_scope_depth_ = 0;
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +0000121int Heap::contexts_disposed_ = 0;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000122
kasper.lund7276f142008-07-30 08:49:36 +0000123#ifdef DEBUG
124bool Heap::allocation_allowed_ = true;
125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126int Heap::allocation_timeout_ = 0;
127bool Heap::disallow_allocation_failure_ = false;
128#endif // DEBUG
129
130
131int Heap::Capacity() {
132 if (!HasBeenSetup()) return 0;
133
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000134 return new_space_.Capacity() +
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000135 old_pointer_space_->Capacity() +
136 old_data_space_->Capacity() +
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 code_space_->Capacity() +
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000138 map_space_->Capacity() +
139 cell_space_->Capacity();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140}
141
142
ager@chromium.org3811b432009-10-28 14:53:37 +0000143int Heap::CommittedMemory() {
144 if (!HasBeenSetup()) return 0;
145
146 return new_space_.CommittedMemory() +
147 old_pointer_space_->CommittedMemory() +
148 old_data_space_->CommittedMemory() +
149 code_space_->CommittedMemory() +
150 map_space_->CommittedMemory() +
151 cell_space_->CommittedMemory() +
152 lo_space_->Size();
153}
154
155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156int Heap::Available() {
157 if (!HasBeenSetup()) return 0;
158
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000159 return new_space_.Available() +
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000160 old_pointer_space_->Available() +
161 old_data_space_->Available() +
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 code_space_->Available() +
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000163 map_space_->Available() +
164 cell_space_->Available();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165}
166
167
168bool Heap::HasBeenSetup() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000169 return old_pointer_space_ != NULL &&
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000170 old_data_space_ != NULL &&
171 code_space_ != NULL &&
172 map_space_ != NULL &&
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000173 cell_space_ != NULL &&
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000174 lo_space_ != NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175}
176
177
178GarbageCollector Heap::SelectGarbageCollector(AllocationSpace space) {
179 // Is global GC requested?
180 if (space != NEW_SPACE || FLAG_gc_global) {
181 Counters::gc_compactor_caused_by_request.Increment();
182 return MARK_COMPACTOR;
183 }
184
185 // Is enough data promoted to justify a global GC?
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000186 if (OldGenerationPromotionLimitReached()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 Counters::gc_compactor_caused_by_promoted_data.Increment();
188 return MARK_COMPACTOR;
189 }
190
191 // Have allocation in OLD and LO failed?
192 if (old_gen_exhausted_) {
193 Counters::gc_compactor_caused_by_oldspace_exhaustion.Increment();
194 return MARK_COMPACTOR;
195 }
196
197 // Is there enough space left in OLD to guarantee that a scavenge can
198 // succeed?
199 //
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000200 // Note that MemoryAllocator->MaxAvailable() undercounts the memory available
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 // for object promotion. It counts only the bytes that the memory
202 // allocator has not yet allocated from the OS and assigned to any space,
203 // and does not count available bytes already in the old space or code
204 // space. Undercounting is safe---we may get an unrequested full GC when
205 // a scavenge would have succeeded.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000206 if (MemoryAllocator::MaxAvailable() <= new_space_.Size()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 Counters::gc_compactor_caused_by_oldspace_exhaustion.Increment();
208 return MARK_COMPACTOR;
209 }
210
211 // Default
212 return SCAVENGER;
213}
214
215
216// TODO(1238405): Combine the infrastructure for --heap-stats and
217// --log-gc to avoid the complicated preprocessor and flag testing.
218#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
219void Heap::ReportStatisticsBeforeGC() {
220 // Heap::ReportHeapStatistics will also log NewSpace statistics when
221 // compiled with ENABLE_LOGGING_AND_PROFILING and --log-gc is set. The
222 // following logic is used to avoid double logging.
223#if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING)
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000224 if (FLAG_heap_stats || FLAG_log_gc) new_space_.CollectStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 if (FLAG_heap_stats) {
226 ReportHeapStatistics("Before GC");
227 } else if (FLAG_log_gc) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000228 new_space_.ReportStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000229 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000230 if (FLAG_heap_stats || FLAG_log_gc) new_space_.ClearHistograms();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231#elif defined(DEBUG)
232 if (FLAG_heap_stats) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000233 new_space_.CollectStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234 ReportHeapStatistics("Before GC");
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000235 new_space_.ClearHistograms();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236 }
237#elif defined(ENABLE_LOGGING_AND_PROFILING)
238 if (FLAG_log_gc) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000239 new_space_.CollectStatistics();
240 new_space_.ReportStatistics();
241 new_space_.ClearHistograms();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 }
243#endif
244}
245
246
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000247#if defined(ENABLE_LOGGING_AND_PROFILING)
248void Heap::PrintShortHeapStatistics() {
249 if (!FLAG_trace_gc_verbose) return;
250 PrintF("Memory allocator, used: %8d, available: %8d\n",
ager@chromium.org3811b432009-10-28 14:53:37 +0000251 MemoryAllocator::Size(),
252 MemoryAllocator::Available());
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000253 PrintF("New space, used: %8d, available: %8d\n",
ager@chromium.org3811b432009-10-28 14:53:37 +0000254 Heap::new_space_.Size(),
255 new_space_.Available());
256 PrintF("Old pointers, used: %8d, available: %8d, waste: %8d\n",
257 old_pointer_space_->Size(),
258 old_pointer_space_->Available(),
259 old_pointer_space_->Waste());
260 PrintF("Old data space, used: %8d, available: %8d, waste: %8d\n",
261 old_data_space_->Size(),
262 old_data_space_->Available(),
263 old_data_space_->Waste());
264 PrintF("Code space, used: %8d, available: %8d, waste: %8d\n",
265 code_space_->Size(),
266 code_space_->Available(),
267 code_space_->Waste());
268 PrintF("Map space, used: %8d, available: %8d, waste: %8d\n",
269 map_space_->Size(),
270 map_space_->Available(),
271 map_space_->Waste());
272 PrintF("Cell space, used: %8d, available: %8d, waste: %8d\n",
273 cell_space_->Size(),
274 cell_space_->Available(),
275 cell_space_->Waste());
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000276 PrintF("Large object space, used: %8d, avaialble: %8d\n",
ager@chromium.org3811b432009-10-28 14:53:37 +0000277 lo_space_->Size(),
278 lo_space_->Available());
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000279}
280#endif
281
282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283// TODO(1238405): Combine the infrastructure for --heap-stats and
284// --log-gc to avoid the complicated preprocessor and flag testing.
285void Heap::ReportStatisticsAfterGC() {
286 // Similar to the before GC, we use some complicated logic to ensure that
287 // NewSpace statistics are logged exactly once when --log-gc is turned on.
288#if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING)
289 if (FLAG_heap_stats) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000290 new_space_.CollectStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 ReportHeapStatistics("After GC");
292 } else if (FLAG_log_gc) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000293 new_space_.ReportStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 }
295#elif defined(DEBUG)
296 if (FLAG_heap_stats) ReportHeapStatistics("After GC");
297#elif defined(ENABLE_LOGGING_AND_PROFILING)
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000298 if (FLAG_log_gc) new_space_.ReportStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299#endif
300}
301#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
302
303
304void Heap::GarbageCollectionPrologue() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000305 TranscendentalCache::Clear();
kasper.lund7276f142008-07-30 08:49:36 +0000306 gc_count_++;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000307 unflattended_strings_length_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308#ifdef DEBUG
309 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
310 allow_allocation(false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311
312 if (FLAG_verify_heap) {
313 Verify();
314 }
315
316 if (FLAG_gc_verbose) Print();
317
318 if (FLAG_print_rset) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000319 // Not all spaces have remembered set bits that we care about.
320 old_pointer_space_->PrintRSet();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 map_space_->PrintRSet();
322 lo_space_->PrintRSet();
323 }
324#endif
325
326#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
327 ReportStatisticsBeforeGC();
328#endif
329}
330
331int Heap::SizeOfObjects() {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000332 int total = 0;
333 AllSpaces spaces;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000334 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000335 total += space->Size();
336 }
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000337 return total;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338}
339
340void Heap::GarbageCollectionEpilogue() {
341#ifdef DEBUG
342 allow_allocation(true);
343 ZapFromSpace();
344
345 if (FLAG_verify_heap) {
346 Verify();
347 }
348
349 if (FLAG_print_global_handles) GlobalHandles::Print();
350 if (FLAG_print_handles) PrintHandles();
351 if (FLAG_gc_verbose) Print();
352 if (FLAG_code_stats) ReportCodeStatistics("After GC");
353#endif
354
355 Counters::alive_after_last_gc.Set(SizeOfObjects());
356
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000357 Counters::symbol_table_capacity.Set(symbol_table()->Capacity());
358 Counters::number_of_symbols.Set(symbol_table()->NumberOfElements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
360 ReportStatisticsAfterGC();
361#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000362#ifdef ENABLE_DEBUGGER_SUPPORT
363 Debug::AfterGarbageCollection();
364#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365}
366
367
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000368void Heap::CollectAllGarbage(bool force_compaction) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000369 // Since we are ignoring the return value, the exact choice of space does
370 // not matter, so long as we do not specify NEW_SPACE, which would not
371 // cause a full GC.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000372 MarkCompactCollector::SetForceCompaction(force_compaction);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000373 CollectGarbage(0, OLD_POINTER_SPACE);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +0000374 MarkCompactCollector::SetForceCompaction(false);
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000375}
376
377
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378bool Heap::CollectGarbage(int requested_size, AllocationSpace space) {
379 // The VM is in the GC state until exiting this function.
380 VMState state(GC);
381
382#ifdef DEBUG
383 // Reset the allocation timeout to the GC interval, but make sure to
384 // allow at least a few allocations after a collection. The reason
385 // for this is that we have a lot of allocation sequences and we
386 // assume that a garbage collection will allow the subsequent
387 // allocation attempts to go through.
388 allocation_timeout_ = Max(6, FLAG_gc_interval);
389#endif
390
391 { GCTracer tracer;
392 GarbageCollectionPrologue();
kasper.lund7276f142008-07-30 08:49:36 +0000393 // The GC count was incremented in the prologue. Tell the tracer about
394 // it.
395 tracer.set_gc_count(gc_count_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396
397 GarbageCollector collector = SelectGarbageCollector(space);
kasper.lund7276f142008-07-30 08:49:36 +0000398 // Tell the tracer which collector we've selected.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000399 tracer.set_collector(collector);
400
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000401 HistogramTimer* rate = (collector == SCAVENGER)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000402 ? &Counters::gc_scavenger
403 : &Counters::gc_compactor;
404 rate->Start();
kasper.lund7276f142008-07-30 08:49:36 +0000405 PerformGarbageCollection(space, collector, &tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406 rate->Stop();
407
408 GarbageCollectionEpilogue();
409 }
410
411
412#ifdef ENABLE_LOGGING_AND_PROFILING
413 if (FLAG_log_gc) HeapProfiler::WriteSample();
414#endif
415
416 switch (space) {
417 case NEW_SPACE:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000418 return new_space_.Available() >= requested_size;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000419 case OLD_POINTER_SPACE:
420 return old_pointer_space_->Available() >= requested_size;
421 case OLD_DATA_SPACE:
422 return old_data_space_->Available() >= requested_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 case CODE_SPACE:
424 return code_space_->Available() >= requested_size;
425 case MAP_SPACE:
426 return map_space_->Available() >= requested_size;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000427 case CELL_SPACE:
428 return cell_space_->Available() >= requested_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429 case LO_SPACE:
430 return lo_space_->Available() >= requested_size;
431 }
432 return false;
433}
434
435
kasper.lund7276f142008-07-30 08:49:36 +0000436void Heap::PerformScavenge() {
437 GCTracer tracer;
438 PerformGarbageCollection(NEW_SPACE, SCAVENGER, &tracer);
439}
440
441
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000442#ifdef DEBUG
kasperl@chromium.org416c5b02009-04-14 14:03:52 +0000443// Helper class for verifying the symbol table.
444class SymbolTableVerifier : public ObjectVisitor {
445 public:
446 SymbolTableVerifier() { }
447 void VisitPointers(Object** start, Object** end) {
448 // Visit all HeapObject pointers in [start, end).
449 for (Object** p = start; p < end; p++) {
450 if ((*p)->IsHeapObject()) {
451 // Check that the symbol is actually a symbol.
452 ASSERT((*p)->IsNull() || (*p)->IsUndefined() || (*p)->IsSymbol());
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000453 }
454 }
kasperl@chromium.org416c5b02009-04-14 14:03:52 +0000455 }
456};
457#endif // DEBUG
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000458
kasperl@chromium.org416c5b02009-04-14 14:03:52 +0000459
460static void VerifySymbolTable() {
461#ifdef DEBUG
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000462 SymbolTableVerifier verifier;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000463 Heap::symbol_table()->IterateElements(&verifier);
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000464#endif // DEBUG
465}
466
467
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000468void Heap::ReserveSpace(
469 int new_space_size,
470 int pointer_space_size,
471 int data_space_size,
472 int code_space_size,
473 int map_space_size,
474 int cell_space_size,
475 int large_object_size) {
476 NewSpace* new_space = Heap::new_space();
477 PagedSpace* old_pointer_space = Heap::old_pointer_space();
478 PagedSpace* old_data_space = Heap::old_data_space();
479 PagedSpace* code_space = Heap::code_space();
480 PagedSpace* map_space = Heap::map_space();
481 PagedSpace* cell_space = Heap::cell_space();
482 LargeObjectSpace* lo_space = Heap::lo_space();
483 bool gc_performed = true;
484 while (gc_performed) {
485 gc_performed = false;
486 if (!new_space->ReserveSpace(new_space_size)) {
487 Heap::CollectGarbage(new_space_size, NEW_SPACE);
488 gc_performed = true;
489 }
490 if (!old_pointer_space->ReserveSpace(pointer_space_size)) {
491 Heap::CollectGarbage(pointer_space_size, OLD_POINTER_SPACE);
492 gc_performed = true;
493 }
494 if (!(old_data_space->ReserveSpace(data_space_size))) {
495 Heap::CollectGarbage(data_space_size, OLD_DATA_SPACE);
496 gc_performed = true;
497 }
498 if (!(code_space->ReserveSpace(code_space_size))) {
499 Heap::CollectGarbage(code_space_size, CODE_SPACE);
500 gc_performed = true;
501 }
502 if (!(map_space->ReserveSpace(map_space_size))) {
503 Heap::CollectGarbage(map_space_size, MAP_SPACE);
504 gc_performed = true;
505 }
506 if (!(cell_space->ReserveSpace(cell_space_size))) {
507 Heap::CollectGarbage(cell_space_size, CELL_SPACE);
508 gc_performed = true;
509 }
510 // We add a slack-factor of 2 in order to have space for the remembered
511 // set and a series of large-object allocations that are only just larger
512 // than the page size.
513 large_object_size *= 2;
514 // The ReserveSpace method on the large object space checks how much
515 // we can expand the old generation. This includes expansion caused by
516 // allocation in the other spaces.
517 large_object_size += cell_space_size + map_space_size + code_space_size +
518 data_space_size + pointer_space_size;
519 if (!(lo_space->ReserveSpace(large_object_size))) {
520 Heap::CollectGarbage(large_object_size, LO_SPACE);
521 gc_performed = true;
522 }
523 }
524}
525
526
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000527void Heap::EnsureFromSpaceIsCommitted() {
528 if (new_space_.CommitFromSpaceIfNeeded()) return;
529
530 // Committing memory to from space failed.
531 // Try shrinking and try again.
532 Shrink();
533 if (new_space_.CommitFromSpaceIfNeeded()) return;
534
535 // Committing memory to from space failed again.
536 // Memory is exhausted and we will die.
537 V8::FatalProcessOutOfMemory("Committing semi space failed.");
538}
539
540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541void Heap::PerformGarbageCollection(AllocationSpace space,
kasper.lund7276f142008-07-30 08:49:36 +0000542 GarbageCollector collector,
543 GCTracer* tracer) {
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000544 VerifySymbolTable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 if (collector == MARK_COMPACTOR && global_gc_prologue_callback_) {
546 ASSERT(!allocation_allowed_);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000547 GCTracer::ExternalScope scope(tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 global_gc_prologue_callback_();
549 }
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000550 EnsureFromSpaceIsCommitted();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000551
552 // Perform mark-sweep with optional compaction.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 if (collector == MARK_COMPACTOR) {
kasper.lund7276f142008-07-30 08:49:36 +0000554 MarkCompact(tracer);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000555 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000557 // Always perform a scavenge to make room in new space.
558 Scavenge();
559
560 // Update the old space promotion limits after the scavenge due to
561 // promotions during scavenge.
562 if (collector == MARK_COMPACTOR) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000563 int old_gen_size = PromotedSpaceSize();
564 old_gen_promotion_limit_ =
565 old_gen_size + Max(kMinimumPromotionLimit, old_gen_size / 3);
566 old_gen_allocation_limit_ =
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000567 old_gen_size + Max(kMinimumAllocationLimit, old_gen_size / 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 old_gen_exhausted_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 }
ager@chromium.org439e85a2009-08-26 13:15:29 +0000570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571 Counters::objs_since_last_young.Set(0);
572
ager@chromium.org3811b432009-10-28 14:53:37 +0000573 if (collector == MARK_COMPACTOR) {
574 DisableAssertNoAllocation allow_allocation;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000575 GCTracer::ExternalScope scope(tracer);
ager@chromium.org3811b432009-10-28 14:53:37 +0000576 GlobalHandles::PostGarbageCollectionProcessing();
577 }
578
579 // Update relocatables.
580 Relocatable::PostGarbageCollectionProcessing();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581
kasper.lund7276f142008-07-30 08:49:36 +0000582 if (collector == MARK_COMPACTOR) {
583 // Register the amount of external allocated memory.
584 amount_of_external_allocated_memory_at_last_global_gc_ =
585 amount_of_external_allocated_memory_;
586 }
587
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 if (collector == MARK_COMPACTOR && global_gc_epilogue_callback_) {
589 ASSERT(!allocation_allowed_);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000590 GCTracer::ExternalScope scope(tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 global_gc_epilogue_callback_();
592 }
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000593 VerifySymbolTable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594}
595
596
kasper.lund7276f142008-07-30 08:49:36 +0000597void Heap::MarkCompact(GCTracer* tracer) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 gc_state_ = MARK_COMPACT;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 mc_count_++;
kasper.lund7276f142008-07-30 08:49:36 +0000600 tracer->set_full_gc_count(mc_count_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 LOG(ResourceEvent("markcompact", "begin"));
602
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000603 MarkCompactCollector::Prepare(tracer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000605 bool is_compacting = MarkCompactCollector::IsCompacting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000607 MarkCompactPrologue(is_compacting);
608
609 MarkCompactCollector::CollectGarbage();
610
611 MarkCompactEpilogue(is_compacting);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612
613 LOG(ResourceEvent("markcompact", "end"));
614
615 gc_state_ = NOT_IN_GC;
616
617 Shrink();
618
619 Counters::objs_since_last_full.Set(0);
kasperl@chromium.org8b2bb262010-03-01 09:46:28 +0000620
621 contexts_disposed_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622}
623
624
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000625void Heap::MarkCompactPrologue(bool is_compacting) {
626 // At any old GC clear the keyed lookup cache to enable collection of unused
627 // maps.
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000628 KeyedLookupCache::Clear();
629 ContextSlotCache::Clear();
630 DescriptorLookupCache::Clear();
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000631
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000632 CompilationCache::MarkCompactPrologue();
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000633
634 Top::MarkCompactPrologue(is_compacting);
635 ThreadManager::MarkCompactPrologue(is_compacting);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000636
637 if (is_compacting) FlushNumberStringCache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638}
639
640
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000641void Heap::MarkCompactEpilogue(bool is_compacting) {
642 Top::MarkCompactEpilogue(is_compacting);
643 ThreadManager::MarkCompactEpilogue(is_compacting);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000644}
645
646
647Object* Heap::FindCodeObject(Address a) {
648 Object* obj = code_space_->FindObject(a);
649 if (obj->IsFailure()) {
650 obj = lo_space_->FindObject(a);
651 }
kasper.lund7276f142008-07-30 08:49:36 +0000652 ASSERT(!obj->IsFailure());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 return obj;
654}
655
656
657// Helper class for copying HeapObjects
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000658class ScavengeVisitor: public ObjectVisitor {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 public:
660
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000661 void VisitPointer(Object** p) { ScavengePointer(p); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662
663 void VisitPointers(Object** start, Object** end) {
664 // Copy all HeapObject pointers in [start, end)
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000665 for (Object** p = start; p < end; p++) ScavengePointer(p);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 }
667
668 private:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000669 void ScavengePointer(Object** p) {
670 Object* object = *p;
671 if (!Heap::InNewSpace(object)) return;
672 Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p),
673 reinterpret_cast<HeapObject*>(object));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 }
675};
676
677
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000678// A queue of pointers and maps of to-be-promoted objects during a
679// scavenge collection.
680class PromotionQueue {
681 public:
682 void Initialize(Address start_address) {
683 front_ = rear_ = reinterpret_cast<HeapObject**>(start_address);
684 }
685
686 bool is_empty() { return front_ <= rear_; }
687
688 void insert(HeapObject* object, Map* map) {
689 *(--rear_) = object;
690 *(--rear_) = map;
691 // Assert no overflow into live objects.
692 ASSERT(reinterpret_cast<Address>(rear_) >= Heap::new_space()->top());
693 }
694
695 void remove(HeapObject** object, Map** map) {
696 *object = *(--front_);
697 *map = Map::cast(*(--front_));
698 // Assert no underflow.
699 ASSERT(front_ >= rear_);
700 }
701
702 private:
703 // The front of the queue is higher in memory than the rear.
704 HeapObject** front_;
705 HeapObject** rear_;
706};
707
708
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000709// Shared state read by the scavenge collector and set by ScavengeObject.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000710static PromotionQueue promotion_queue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711
712
713#ifdef DEBUG
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000714// Visitor class to verify pointers in code or data space do not point into
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715// new space.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000716class VerifyNonPointerSpacePointersVisitor: public ObjectVisitor {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717 public:
718 void VisitPointers(Object** start, Object**end) {
719 for (Object** current = start; current < end; current++) {
720 if ((*current)->IsHeapObject()) {
721 ASSERT(!Heap::InNewSpace(HeapObject::cast(*current)));
722 }
723 }
724 }
725};
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000726
727
728static void VerifyNonPointerSpacePointers() {
729 // Verify that there are no pointers to new space in spaces where we
730 // do not expect them.
731 VerifyNonPointerSpacePointersVisitor v;
732 HeapObjectIterator code_it(Heap::code_space());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000733 for (HeapObject* object = code_it.next();
734 object != NULL; object = code_it.next())
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000735 object->Iterate(&v);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000736
737 HeapObjectIterator data_it(Heap::old_data_space());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000738 for (HeapObject* object = data_it.next();
739 object != NULL; object = data_it.next())
740 object->Iterate(&v);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000741}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742#endif
743
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000744
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745void Heap::Scavenge() {
746#ifdef DEBUG
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000747 if (FLAG_enable_slow_asserts) VerifyNonPointerSpacePointers();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748#endif
749
750 gc_state_ = SCAVENGE;
751
752 // Implements Cheney's copying algorithm
753 LOG(ResourceEvent("scavenge", "begin"));
754
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000755 // Clear descriptor cache.
756 DescriptorLookupCache::Clear();
757
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000758 // Used for updating survived_since_last_expansion_ at function end.
759 int survived_watermark = PromotedSpaceSize();
760
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000761 if (new_space_.Capacity() < new_space_.MaximumCapacity() &&
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000762 survived_since_last_expansion_ > new_space_.Capacity()) {
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000763 // Grow the size of new space if there is room to grow and enough
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000764 // data has survived scavenge since the last expansion.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000765 new_space_.Grow();
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000766 survived_since_last_expansion_ = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767 }
768
769 // Flip the semispaces. After flipping, to space is empty, from space has
770 // live objects.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000771 new_space_.Flip();
772 new_space_.ResetAllocationInfo();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000774 // We need to sweep newly copied objects which can be either in the
775 // to space or promoted to the old generation. For to-space
776 // objects, we treat the bottom of the to space as a queue. Newly
777 // copied and unswept objects lie between a 'front' mark and the
778 // allocation pointer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000779 //
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000780 // Promoted objects can go into various old-generation spaces, and
781 // can be allocated internally in the spaces (from the free list).
782 // We treat the top of the to space as a queue of addresses of
783 // promoted objects. The addresses of newly promoted and unswept
784 // objects lie between a 'front' mark and a 'rear' mark that is
785 // updated as a side effect of promoting an object.
786 //
787 // There is guaranteed to be enough room at the top of the to space
788 // for the addresses of promoted objects: every object promoted
789 // frees up its size in bytes from the top of the new space, and
790 // objects are at least one pointer in size.
791 Address new_space_front = new_space_.ToSpaceLow();
792 promotion_queue.Initialize(new_space_.ToSpaceHigh());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000794 ScavengeVisitor scavenge_visitor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 // Copy roots.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000796 IterateRoots(&scavenge_visitor, VISIT_ALL_IN_SCAVENGE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000797
798 // Copy objects reachable from the old generation. By definition,
799 // there are no intergenerational pointers in code or data spaces.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000800 IterateRSet(old_pointer_space_, &ScavengePointer);
801 IterateRSet(map_space_, &ScavengePointer);
802 lo_space_->IterateRSet(&ScavengePointer);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000803
804 // Copy objects reachable from cells by scavenging cell values directly.
805 HeapObjectIterator cell_iterator(cell_space_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000806 for (HeapObject* cell = cell_iterator.next();
807 cell != NULL; cell = cell_iterator.next()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000808 if (cell->IsJSGlobalPropertyCell()) {
809 Address value_address =
810 reinterpret_cast<Address>(cell) +
811 (JSGlobalPropertyCell::kValueOffset - kHeapObjectTag);
812 scavenge_visitor.VisitPointer(reinterpret_cast<Object**>(value_address));
813 }
814 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000816 new_space_front = DoScavenge(&scavenge_visitor, new_space_front);
817
818 ScavengeExternalStringTable();
819 ASSERT(new_space_front == new_space_.top());
820
821 // Set age mark.
822 new_space_.set_age_mark(new_space_.top());
823
824 // Update how much has survived scavenge.
825 survived_since_last_expansion_ +=
826 (PromotedSpaceSize() - survived_watermark) + new_space_.Size();
827
828 LOG(ResourceEvent("scavenge", "end"));
829
830 gc_state_ = NOT_IN_GC;
831}
832
833
834void Heap::ScavengeExternalStringTable() {
835 ExternalStringTable::Verify();
836
837 if (ExternalStringTable::new_space_strings_.is_empty()) return;
838
839 Object** start = &ExternalStringTable::new_space_strings_[0];
840 Object** end = start + ExternalStringTable::new_space_strings_.length();
841 Object** last = start;
842
843 for (Object** p = start; p < end; ++p) {
844 ASSERT(Heap::InFromSpace(*p));
845 MapWord first_word = HeapObject::cast(*p)->map_word();
846
847 if (!first_word.IsForwardingAddress()) {
848 // Unreachable external string can be finalized.
849 FinalizeExternalString(String::cast(*p));
850 continue;
851 }
852
853 // String is still reachable.
854 String* target = String::cast(first_word.ToForwardingAddress());
855 ASSERT(target->IsExternalString());
856
857 if (Heap::InNewSpace(target)) {
858 // String is still in new space. Update the table entry.
859 *last = target;
860 ++last;
861 } else {
862 // String got promoted. Move it to the old string list.
863 ExternalStringTable::AddOldString(target);
864 }
865 }
866
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000867 ASSERT(last <= end);
868 ExternalStringTable::ShrinkNewStrings(static_cast<int>(last - start));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000869}
870
871
872Address Heap::DoScavenge(ObjectVisitor* scavenge_visitor,
873 Address new_space_front) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000874 do {
875 ASSERT(new_space_front <= new_space_.top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000877 // The addresses new_space_front and new_space_.top() define a
878 // queue of unprocessed copied objects. Process them until the
879 // queue is empty.
880 while (new_space_front < new_space_.top()) {
881 HeapObject* object = HeapObject::FromAddress(new_space_front);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000882 object->Iterate(scavenge_visitor);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000883 new_space_front += object->Size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 }
885
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000886 // Promote and process all the to-be-promoted objects.
887 while (!promotion_queue.is_empty()) {
888 HeapObject* source;
889 Map* map;
890 promotion_queue.remove(&source, &map);
891 // Copy the from-space object to its new location (given by the
892 // forwarding address) and fix its map.
893 HeapObject* target = source->map_word().ToForwardingAddress();
894 CopyBlock(reinterpret_cast<Object**>(target->address()),
895 reinterpret_cast<Object**>(source->address()),
896 source->SizeFromMap(map));
897 target->set_map(map);
898
899#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
900 // Update NewSpace stats if necessary.
901 RecordCopiedObject(target);
902#endif
903 // Visit the newly copied object for pointers to new space.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000904 target->Iterate(scavenge_visitor);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000905 UpdateRSet(target);
906 }
907
908 // Take another spin if there are now unswept objects in new space
909 // (there are currently no more unswept promoted objects).
910 } while (new_space_front < new_space_.top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000912 return new_space_front;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913}
914
915
916void Heap::ClearRSetRange(Address start, int size_in_bytes) {
917 uint32_t start_bit;
918 Address start_word_address =
919 Page::ComputeRSetBitPosition(start, 0, &start_bit);
920 uint32_t end_bit;
921 Address end_word_address =
922 Page::ComputeRSetBitPosition(start + size_in_bytes - kIntSize,
923 0,
924 &end_bit);
925
926 // We want to clear the bits in the starting word starting with the
927 // first bit, and in the ending word up to and including the last
928 // bit. Build a pair of bitmasks to do that.
929 uint32_t start_bitmask = start_bit - 1;
930 uint32_t end_bitmask = ~((end_bit << 1) - 1);
931
932 // If the start address and end address are the same, we mask that
933 // word once, otherwise mask the starting and ending word
934 // separately and all the ones in between.
935 if (start_word_address == end_word_address) {
936 Memory::uint32_at(start_word_address) &= (start_bitmask | end_bitmask);
937 } else {
938 Memory::uint32_at(start_word_address) &= start_bitmask;
939 Memory::uint32_at(end_word_address) &= end_bitmask;
940 start_word_address += kIntSize;
941 memset(start_word_address, 0, end_word_address - start_word_address);
942 }
943}
944
945
946class UpdateRSetVisitor: public ObjectVisitor {
947 public:
948
949 void VisitPointer(Object** p) {
950 UpdateRSet(p);
951 }
952
953 void VisitPointers(Object** start, Object** end) {
954 // Update a store into slots [start, end), used (a) to update remembered
955 // set when promoting a young object to old space or (b) to rebuild
956 // remembered sets after a mark-compact collection.
957 for (Object** p = start; p < end; p++) UpdateRSet(p);
958 }
959 private:
960
961 void UpdateRSet(Object** p) {
962 // The remembered set should not be set. It should be clear for objects
963 // newly copied to old space, and it is cleared before rebuilding in the
964 // mark-compact collector.
965 ASSERT(!Page::IsRSetSet(reinterpret_cast<Address>(p), 0));
966 if (Heap::InNewSpace(*p)) {
967 Page::SetRSet(reinterpret_cast<Address>(p), 0);
968 }
969 }
970};
971
972
973int Heap::UpdateRSet(HeapObject* obj) {
974 ASSERT(!InNewSpace(obj));
975 // Special handling of fixed arrays to iterate the body based on the start
976 // address and offset. Just iterating the pointers as in UpdateRSetVisitor
977 // will not work because Page::SetRSet needs to have the start of the
sgjesse@chromium.orgb9d7da12009-08-05 08:38:10 +0000978 // object for large object pages.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000979 if (obj->IsFixedArray()) {
980 FixedArray* array = FixedArray::cast(obj);
981 int length = array->length();
982 for (int i = 0; i < length; i++) {
983 int offset = FixedArray::kHeaderSize + i * kPointerSize;
984 ASSERT(!Page::IsRSetSet(obj->address(), offset));
985 if (Heap::InNewSpace(array->get(i))) {
986 Page::SetRSet(obj->address(), offset);
987 }
988 }
989 } else if (!obj->IsCode()) {
990 // Skip code object, we know it does not contain inter-generational
991 // pointers.
992 UpdateRSetVisitor v;
993 obj->Iterate(&v);
994 }
995 return obj->Size();
996}
997
998
999void Heap::RebuildRSets() {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001000 // By definition, we do not care about remembered set bits in code,
1001 // data, or cell spaces.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001002 map_space_->ClearRSet();
1003 RebuildRSets(map_space_);
1004
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001005 old_pointer_space_->ClearRSet();
1006 RebuildRSets(old_pointer_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007
1008 Heap::lo_space_->ClearRSet();
1009 RebuildRSets(lo_space_);
1010}
1011
1012
1013void Heap::RebuildRSets(PagedSpace* space) {
1014 HeapObjectIterator it(space);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001015 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next())
1016 Heap::UpdateRSet(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017}
1018
1019
1020void Heap::RebuildRSets(LargeObjectSpace* space) {
1021 LargeObjectIterator it(space);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001022 for (HeapObject* obj = it.next(); obj != NULL; obj = it.next())
1023 Heap::UpdateRSet(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024}
1025
1026
1027#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1028void Heap::RecordCopiedObject(HeapObject* obj) {
1029 bool should_record = false;
1030#ifdef DEBUG
1031 should_record = FLAG_heap_stats;
1032#endif
1033#ifdef ENABLE_LOGGING_AND_PROFILING
1034 should_record = should_record || FLAG_log_gc;
1035#endif
1036 if (should_record) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001037 if (new_space_.Contains(obj)) {
1038 new_space_.RecordAllocation(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 } else {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001040 new_space_.RecordPromotion(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001041 }
1042 }
1043}
1044#endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
1045
1046
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001047
1048HeapObject* Heap::MigrateObject(HeapObject* source,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 HeapObject* target,
1050 int size) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001051 // Copy the content of source to target.
1052 CopyBlock(reinterpret_cast<Object**>(target->address()),
1053 reinterpret_cast<Object**>(source->address()),
1054 size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055
kasper.lund7276f142008-07-30 08:49:36 +00001056 // Set the forwarding address.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001057 source->set_map_word(MapWord::FromForwardingAddress(target));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059#if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001060 // Update NewSpace stats if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 RecordCopiedObject(target);
1062#endif
1063
1064 return target;
1065}
1066
1067
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001068static inline bool IsShortcutCandidate(HeapObject* object, Map* map) {
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00001069 STATIC_ASSERT(kNotStringTag != 0 && kSymbolTag != 0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001070 ASSERT(object->map() == map);
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00001071 InstanceType type = map->instance_type();
1072 if ((type & kShortcutTypeMask) != kShortcutTypeTag) return false;
1073 ASSERT(object->IsString() && !object->IsSymbol());
1074 return ConsString::cast(object)->unchecked_second() == Heap::empty_string();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001075}
1076
1077
1078void Heap::ScavengeObjectSlow(HeapObject** p, HeapObject* object) {
1079 ASSERT(InFromSpace(object));
1080 MapWord first_word = object->map_word();
1081 ASSERT(!first_word.IsForwardingAddress());
1082
1083 // Optimization: Bypass flattened ConsString objects.
1084 if (IsShortcutCandidate(object, first_word.ToMap())) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001085 object = HeapObject::cast(ConsString::cast(object)->unchecked_first());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 *p = object;
1087 // After patching *p we have to repeat the checks that object is in the
1088 // active semispace of the young generation and not already copied.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001089 if (!InNewSpace(object)) return;
kasper.lund7276f142008-07-30 08:49:36 +00001090 first_word = object->map_word();
1091 if (first_word.IsForwardingAddress()) {
1092 *p = first_word.ToForwardingAddress();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093 return;
1094 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095 }
1096
kasper.lund7276f142008-07-30 08:49:36 +00001097 int object_size = object->SizeFromMap(first_word.ToMap());
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001098 // We rely on live objects in new space to be at least two pointers,
1099 // so we can store the from-space address and map pointer of promoted
1100 // objects in the to space.
1101 ASSERT(object_size >= 2 * kPointerSize);
1102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 // If the object should be promoted, we try to copy it to old space.
1104 if (ShouldBePromoted(object->address(), object_size)) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001105 Object* result;
1106 if (object_size > MaxObjectSizeInPagedSpace()) {
1107 result = lo_space_->AllocateRawFixedArray(object_size);
1108 if (!result->IsFailure()) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001109 // Save the from-space object pointer and its map pointer at the
1110 // top of the to space to be swept and copied later. Write the
1111 // forwarding address over the map word of the from-space
1112 // object.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001113 HeapObject* target = HeapObject::cast(result);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00001114 promotion_queue.insert(object, first_word.ToMap());
1115 object->set_map_word(MapWord::FromForwardingAddress(target));
1116
1117 // Give the space allocated for the result a proper map by
1118 // treating it as a free list node (not linked into the free
1119 // list).
1120 FreeListNode* node = FreeListNode::FromAddress(target->address());
1121 node->set_size(object_size);
1122
1123 *p = target;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001124 return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001125 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001126 } else {
1127 OldSpace* target_space = Heap::TargetSpace(object);
1128 ASSERT(target_space == Heap::old_pointer_space_ ||
1129 target_space == Heap::old_data_space_);
1130 result = target_space->AllocateRaw(object_size);
1131 if (!result->IsFailure()) {
1132 HeapObject* target = HeapObject::cast(result);
1133 if (target_space == Heap::old_pointer_space_) {
1134 // Save the from-space object pointer and its map pointer at the
1135 // top of the to space to be swept and copied later. Write the
1136 // forwarding address over the map word of the from-space
1137 // object.
1138 promotion_queue.insert(object, first_word.ToMap());
1139 object->set_map_word(MapWord::FromForwardingAddress(target));
1140
1141 // Give the space allocated for the result a proper map by
1142 // treating it as a free list node (not linked into the free
1143 // list).
1144 FreeListNode* node = FreeListNode::FromAddress(target->address());
1145 node->set_size(object_size);
1146
1147 *p = target;
1148 } else {
1149 // Objects promoted to the data space can be copied immediately
1150 // and not revisited---we will never sweep that space for
1151 // pointers and the copied objects do not contain pointers to
1152 // new space objects.
1153 *p = MigrateObject(object, target, object_size);
1154#ifdef DEBUG
1155 VerifyNonPointerSpacePointersVisitor v;
1156 (*p)->Iterate(&v);
1157#endif
1158 }
1159 return;
1160 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 }
1162 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 // The object should remain in new space or the old space allocation failed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001164 Object* result = new_space_.AllocateRaw(object_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 // Failed allocation at this point is utterly unexpected.
1166 ASSERT(!result->IsFailure());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001167 *p = MigrateObject(object, HeapObject::cast(result), object_size);
1168}
1169
1170
1171void Heap::ScavengePointer(HeapObject** p) {
1172 ScavengeObject(p, *p);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173}
1174
1175
1176Object* Heap::AllocatePartialMap(InstanceType instance_type,
1177 int instance_size) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001178 Object* result = AllocateRawMap();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 if (result->IsFailure()) return result;
1180
1181 // Map::cast cannot be used due to uninitialized map field.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001182 reinterpret_cast<Map*>(result)->set_map(raw_unchecked_meta_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183 reinterpret_cast<Map*>(result)->set_instance_type(instance_type);
1184 reinterpret_cast<Map*>(result)->set_instance_size(instance_size);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001185 reinterpret_cast<Map*>(result)->set_inobject_properties(0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001186 reinterpret_cast<Map*>(result)->set_pre_allocated_property_fields(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 reinterpret_cast<Map*>(result)->set_unused_property_fields(0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001188 reinterpret_cast<Map*>(result)->set_bit_field(0);
1189 reinterpret_cast<Map*>(result)->set_bit_field2(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 return result;
1191}
1192
1193
1194Object* Heap::AllocateMap(InstanceType instance_type, int instance_size) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001195 Object* result = AllocateRawMap();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196 if (result->IsFailure()) return result;
1197
1198 Map* map = reinterpret_cast<Map*>(result);
1199 map->set_map(meta_map());
1200 map->set_instance_type(instance_type);
1201 map->set_prototype(null_value());
1202 map->set_constructor(null_value());
1203 map->set_instance_size(instance_size);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001204 map->set_inobject_properties(0);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001205 map->set_pre_allocated_property_fields(0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001206 map->set_instance_descriptors(empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 map->set_code_cache(empty_fixed_array());
1208 map->set_unused_property_fields(0);
1209 map->set_bit_field(0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001210 map->set_bit_field2(1 << Map::kIsExtensible);
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001211
1212 // If the map object is aligned fill the padding area with Smi 0 objects.
1213 if (Map::kPadStart < Map::kSize) {
1214 memset(reinterpret_cast<byte*>(map) + Map::kPadStart - kHeapObjectTag,
1215 0,
1216 Map::kSize - Map::kPadStart);
1217 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 return map;
1219}
1220
1221
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001222Object* Heap::AllocateCodeCache() {
1223 Object* result = AllocateStruct(CODE_CACHE_TYPE);
1224 if (result->IsFailure()) return result;
1225 CodeCache* code_cache = CodeCache::cast(result);
1226 code_cache->set_default_cache(empty_fixed_array());
1227 code_cache->set_normal_type_cache(undefined_value());
1228 return code_cache;
1229}
1230
1231
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001232const Heap::StringTypeTable Heap::string_type_table[] = {
1233#define STRING_TYPE_ELEMENT(type, size, name, camel_name) \
1234 {type, size, k##camel_name##MapRootIndex},
1235 STRING_TYPE_LIST(STRING_TYPE_ELEMENT)
1236#undef STRING_TYPE_ELEMENT
1237};
1238
1239
1240const Heap::ConstantSymbolTable Heap::constant_symbol_table[] = {
1241#define CONSTANT_SYMBOL_ELEMENT(name, contents) \
1242 {contents, k##name##RootIndex},
1243 SYMBOL_LIST(CONSTANT_SYMBOL_ELEMENT)
1244#undef CONSTANT_SYMBOL_ELEMENT
1245};
1246
1247
1248const Heap::StructTable Heap::struct_table[] = {
1249#define STRUCT_TABLE_ELEMENT(NAME, Name, name) \
1250 { NAME##_TYPE, Name::kSize, k##Name##MapRootIndex },
1251 STRUCT_LIST(STRUCT_TABLE_ELEMENT)
1252#undef STRUCT_TABLE_ELEMENT
1253};
1254
1255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256bool Heap::CreateInitialMaps() {
1257 Object* obj = AllocatePartialMap(MAP_TYPE, Map::kSize);
1258 if (obj->IsFailure()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001259 // Map::cast cannot be used due to uninitialized map field.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001260 Map* new_meta_map = reinterpret_cast<Map*>(obj);
1261 set_meta_map(new_meta_map);
1262 new_meta_map->set_map(new_meta_map);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001264 obj = AllocatePartialMap(FIXED_ARRAY_TYPE, FixedArray::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001266 set_fixed_array_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267
1268 obj = AllocatePartialMap(ODDBALL_TYPE, Oddball::kSize);
1269 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001270 set_oddball_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271
1272 // Allocate the empty array
1273 obj = AllocateEmptyFixedArray();
1274 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001275 set_empty_fixed_array(FixedArray::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001277 obj = Allocate(oddball_map(), OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001279 set_null_value(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001281 // Allocate the empty descriptor array.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001282 obj = AllocateEmptyFixedArray();
1283 if (obj->IsFailure()) return false;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001284 set_empty_descriptor_array(DescriptorArray::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001286 // Fix the instance_descriptors for the existing maps.
1287 meta_map()->set_instance_descriptors(empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 meta_map()->set_code_cache(empty_fixed_array());
1289
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001290 fixed_array_map()->set_instance_descriptors(empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 fixed_array_map()->set_code_cache(empty_fixed_array());
1292
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001293 oddball_map()->set_instance_descriptors(empty_descriptor_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 oddball_map()->set_code_cache(empty_fixed_array());
1295
1296 // Fix prototype object for existing maps.
1297 meta_map()->set_prototype(null_value());
1298 meta_map()->set_constructor(null_value());
1299
1300 fixed_array_map()->set_prototype(null_value());
1301 fixed_array_map()->set_constructor(null_value());
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 oddball_map()->set_prototype(null_value());
1304 oddball_map()->set_constructor(null_value());
1305
1306 obj = AllocateMap(HEAP_NUMBER_TYPE, HeapNumber::kSize);
1307 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001308 set_heap_number_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309
1310 obj = AllocateMap(PROXY_TYPE, Proxy::kSize);
1311 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001312 set_proxy_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001314 for (unsigned i = 0; i < ARRAY_SIZE(string_type_table); i++) {
1315 const StringTypeTable& entry = string_type_table[i];
1316 obj = AllocateMap(entry.type, entry.size);
1317 if (obj->IsFailure()) return false;
1318 roots_[entry.index] = Map::cast(obj);
1319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001321 obj = AllocateMap(STRING_TYPE, SeqTwoByteString::kAlignedSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 if (obj->IsFailure()) return false;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001323 set_undetectable_string_map(Map::cast(obj));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001324 Map::cast(obj)->set_is_undetectable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001326 obj = AllocateMap(ASCII_STRING_TYPE, SeqAsciiString::kAlignedSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 if (obj->IsFailure()) return false;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001328 set_undetectable_ascii_string_map(Map::cast(obj));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001329 Map::cast(obj)->set_is_undetectable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001331 obj = AllocateMap(BYTE_ARRAY_TYPE, ByteArray::kAlignedSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001333 set_byte_array_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001335 obj = AllocateMap(PIXEL_ARRAY_TYPE, PixelArray::kAlignedSize);
1336 if (obj->IsFailure()) return false;
1337 set_pixel_array_map(Map::cast(obj));
1338
ager@chromium.org3811b432009-10-28 14:53:37 +00001339 obj = AllocateMap(EXTERNAL_BYTE_ARRAY_TYPE,
1340 ExternalArray::kAlignedSize);
1341 if (obj->IsFailure()) return false;
1342 set_external_byte_array_map(Map::cast(obj));
1343
1344 obj = AllocateMap(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
1345 ExternalArray::kAlignedSize);
1346 if (obj->IsFailure()) return false;
1347 set_external_unsigned_byte_array_map(Map::cast(obj));
1348
1349 obj = AllocateMap(EXTERNAL_SHORT_ARRAY_TYPE,
1350 ExternalArray::kAlignedSize);
1351 if (obj->IsFailure()) return false;
1352 set_external_short_array_map(Map::cast(obj));
1353
1354 obj = AllocateMap(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
1355 ExternalArray::kAlignedSize);
1356 if (obj->IsFailure()) return false;
1357 set_external_unsigned_short_array_map(Map::cast(obj));
1358
1359 obj = AllocateMap(EXTERNAL_INT_ARRAY_TYPE,
1360 ExternalArray::kAlignedSize);
1361 if (obj->IsFailure()) return false;
1362 set_external_int_array_map(Map::cast(obj));
1363
1364 obj = AllocateMap(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
1365 ExternalArray::kAlignedSize);
1366 if (obj->IsFailure()) return false;
1367 set_external_unsigned_int_array_map(Map::cast(obj));
1368
1369 obj = AllocateMap(EXTERNAL_FLOAT_ARRAY_TYPE,
1370 ExternalArray::kAlignedSize);
1371 if (obj->IsFailure()) return false;
1372 set_external_float_array_map(Map::cast(obj));
1373
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001374 obj = AllocateMap(CODE_TYPE, Code::kHeaderSize);
1375 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001376 set_code_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001378 obj = AllocateMap(JS_GLOBAL_PROPERTY_CELL_TYPE,
1379 JSGlobalPropertyCell::kSize);
1380 if (obj->IsFailure()) return false;
1381 set_global_property_cell_map(Map::cast(obj));
1382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 obj = AllocateMap(FILLER_TYPE, kPointerSize);
1384 if (obj->IsFailure()) return false;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001385 set_one_pointer_filler_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386
1387 obj = AllocateMap(FILLER_TYPE, 2 * kPointerSize);
1388 if (obj->IsFailure()) return false;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001389 set_two_pointer_filler_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001391 for (unsigned i = 0; i < ARRAY_SIZE(struct_table); i++) {
1392 const StructTable& entry = struct_table[i];
1393 obj = AllocateMap(entry.type, entry.size);
1394 if (obj->IsFailure()) return false;
1395 roots_[entry.index] = Map::cast(obj);
1396 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397
ager@chromium.org236ad962008-09-25 09:45:57 +00001398 obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001400 set_hash_table_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401
ager@chromium.org236ad962008-09-25 09:45:57 +00001402 obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001404 set_context_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405
ager@chromium.org236ad962008-09-25 09:45:57 +00001406 obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001408 set_catch_context_map(Map::cast(obj));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001409
1410 obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize);
1411 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001412 set_global_context_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413
1414 obj = AllocateMap(JS_FUNCTION_TYPE, JSFunction::kSize);
1415 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001416 set_boilerplate_function_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417
1418 obj = AllocateMap(SHARED_FUNCTION_INFO_TYPE, SharedFunctionInfo::kSize);
1419 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001420 set_shared_function_info_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001422 ASSERT(!Heap::InNewSpace(Heap::empty_fixed_array()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 return true;
1424}
1425
1426
1427Object* Heap::AllocateHeapNumber(double value, PretenureFlag pretenure) {
1428 // Statically ensure that it is safe to allocate heap numbers in paged
1429 // spaces.
1430 STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001431 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001432
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001433 Object* result = AllocateRaw(HeapNumber::kSize, space, OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434 if (result->IsFailure()) return result;
1435
1436 HeapObject::cast(result)->set_map(heap_number_map());
1437 HeapNumber::cast(result)->set_value(value);
1438 return result;
1439}
1440
1441
1442Object* Heap::AllocateHeapNumber(double value) {
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001443 // Use general version, if we're forced to always allocate.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001444 if (always_allocate()) return AllocateHeapNumber(value, TENURED);
1445
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 // This version of AllocateHeapNumber is optimized for
1447 // allocation in new space.
1448 STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize);
1449 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001450 Object* result = new_space_.AllocateRaw(HeapNumber::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 if (result->IsFailure()) return result;
1452 HeapObject::cast(result)->set_map(heap_number_map());
1453 HeapNumber::cast(result)->set_value(value);
1454 return result;
1455}
1456
1457
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001458Object* Heap::AllocateJSGlobalPropertyCell(Object* value) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001459 Object* result = AllocateRawCell();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001460 if (result->IsFailure()) return result;
1461 HeapObject::cast(result)->set_map(global_property_cell_map());
1462 JSGlobalPropertyCell::cast(result)->set_value(value);
1463 return result;
1464}
1465
1466
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467Object* Heap::CreateOddball(Map* map,
1468 const char* to_string,
1469 Object* to_number) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001470 Object* result = Allocate(map, OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 if (result->IsFailure()) return result;
1472 return Oddball::cast(result)->Initialize(to_string, to_number);
1473}
1474
1475
1476bool Heap::CreateApiObjects() {
1477 Object* obj;
1478
1479 obj = AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
1480 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001481 set_neander_map(Map::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001483 obj = Heap::AllocateJSObjectFromMap(neander_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 if (obj->IsFailure()) return false;
1485 Object* elements = AllocateFixedArray(2);
1486 if (elements->IsFailure()) return false;
1487 FixedArray::cast(elements)->set(0, Smi::FromInt(0));
1488 JSObject::cast(obj)->set_elements(FixedArray::cast(elements));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001489 set_message_listeners(JSObject::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 return true;
1492}
1493
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001494
1495void Heap::CreateCEntryStub() {
ager@chromium.orga1645e22009-09-09 19:27:10 +00001496 CEntryStub stub(1);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001497 set_c_entry_code(*stub.GetCode());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001498}
1499
1500
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001501#if V8_TARGET_ARCH_ARM && V8_NATIVE_REGEXP
1502void Heap::CreateRegExpCEntryStub() {
1503 RegExpCEntryStub stub;
1504 set_re_c_entry_code(*stub.GetCode());
1505}
1506#endif
1507
1508
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001509void Heap::CreateJSEntryStub() {
1510 JSEntryStub stub;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001511 set_js_entry_code(*stub.GetCode());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001512}
1513
1514
1515void Heap::CreateJSConstructEntryStub() {
1516 JSConstructEntryStub stub;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001517 set_js_construct_entry_code(*stub.GetCode());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001518}
1519
1520
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001521void Heap::CreateFixedStubs() {
1522 // Here we create roots for fixed stubs. They are needed at GC
1523 // for cooking and uncooking (check out frames.cc).
1524 // The eliminates the need for doing dictionary lookup in the
1525 // stub cache for these stubs.
1526 HandleScope scope;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001527 // gcc-4.4 has problem generating correct code of following snippet:
1528 // { CEntryStub stub;
1529 // c_entry_code_ = *stub.GetCode();
1530 // }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001531 // { DebuggerStatementStub stub;
1532 // debugger_statement_code_ = *stub.GetCode();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001533 // }
1534 // To workaround the problem, make separate functions without inlining.
1535 Heap::CreateCEntryStub();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001536 Heap::CreateJSEntryStub();
1537 Heap::CreateJSConstructEntryStub();
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001538#if V8_TARGET_ARCH_ARM && V8_NATIVE_REGEXP
1539 Heap::CreateRegExpCEntryStub();
1540#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541}
1542
1543
1544bool Heap::CreateInitialObjects() {
1545 Object* obj;
1546
1547 // The -0 value must be set before NumberFromDouble works.
1548 obj = AllocateHeapNumber(-0.0, TENURED);
1549 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001550 set_minus_zero_value(obj);
1551 ASSERT(signbit(minus_zero_value()->Number()) != 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552
1553 obj = AllocateHeapNumber(OS::nan_value(), TENURED);
1554 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001555 set_nan_value(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001557 obj = Allocate(oddball_map(), OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001559 set_undefined_value(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 ASSERT(!InNewSpace(undefined_value()));
1561
1562 // Allocate initial symbol table.
1563 obj = SymbolTable::Allocate(kInitialSymbolTableSize);
1564 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001565 // Don't use set_symbol_table() due to asserts.
1566 roots_[kSymbolTableRootIndex] = obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567
1568 // Assign the print strings for oddballs after creating symboltable.
1569 Object* symbol = LookupAsciiSymbol("undefined");
1570 if (symbol->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001571 Oddball::cast(undefined_value())->set_to_string(String::cast(symbol));
1572 Oddball::cast(undefined_value())->set_to_number(nan_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573
1574 // Assign the print strings for oddballs after creating symboltable.
1575 symbol = LookupAsciiSymbol("null");
1576 if (symbol->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001577 Oddball::cast(null_value())->set_to_string(String::cast(symbol));
1578 Oddball::cast(null_value())->set_to_number(Smi::FromInt(0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579
1580 // Allocate the null_value
1581 obj = Oddball::cast(null_value())->Initialize("null", Smi::FromInt(0));
1582 if (obj->IsFailure()) return false;
1583
1584 obj = CreateOddball(oddball_map(), "true", Smi::FromInt(1));
1585 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001586 set_true_value(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587
1588 obj = CreateOddball(oddball_map(), "false", Smi::FromInt(0));
1589 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001590 set_false_value(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591
1592 obj = CreateOddball(oddball_map(), "hole", Smi::FromInt(-1));
1593 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001594 set_the_hole_value(obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001596 obj = CreateOddball(
1597 oddball_map(), "no_interceptor_result_sentinel", Smi::FromInt(-2));
1598 if (obj->IsFailure()) return false;
1599 set_no_interceptor_result_sentinel(obj);
1600
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00001601 obj = CreateOddball(oddball_map(), "termination_exception", Smi::FromInt(-3));
1602 if (obj->IsFailure()) return false;
1603 set_termination_exception(obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001604
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 // Allocate the empty string.
1606 obj = AllocateRawAsciiString(0, TENURED);
1607 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001608 set_empty_string(String::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001610 for (unsigned i = 0; i < ARRAY_SIZE(constant_symbol_table); i++) {
1611 obj = LookupAsciiSymbol(constant_symbol_table[i].contents);
1612 if (obj->IsFailure()) return false;
1613 roots_[constant_symbol_table[i].index] = String::cast(obj);
1614 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001616 // Allocate the hidden symbol which is used to identify the hidden properties
1617 // in JSObjects. The hash code has a special value so that it will not match
1618 // the empty string when searching for the property. It cannot be part of the
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001619 // loop above because it needs to be allocated manually with the special
ager@chromium.org3b45ab52009-03-19 22:21:34 +00001620 // hash code in place. The hash code for the hidden_symbol is zero to ensure
1621 // that it will always be at the first entry in property descriptors.
1622 obj = AllocateSymbol(CStrVector(""), 0, String::kHashComputedMask);
1623 if (obj->IsFailure()) return false;
1624 hidden_symbol_ = String::cast(obj);
1625
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626 // Allocate the proxy for __proto__.
1627 obj = AllocateProxy((Address) &Accessors::ObjectPrototype);
1628 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001629 set_prototype_accessors(Proxy::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001630
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001631 // Allocate the code_stubs dictionary. The initial size is set to avoid
1632 // expanding the dictionary during bootstrapping.
1633 obj = NumberDictionary::Allocate(128);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001635 set_code_stubs(NumberDictionary::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001637 // Allocate the non_monomorphic_cache used in stub-cache.cc. The initial size
1638 // is set to avoid expanding the dictionary during bootstrapping.
1639 obj = NumberDictionary::Allocate(64);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001641 set_non_monomorphic_cache(NumberDictionary::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642
1643 CreateFixedStubs();
1644
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001645 if (InitializeNumberStringCache()->IsFailure()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646
1647 // Allocate cache for single character strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001648 obj = AllocateFixedArray(String::kMaxAsciiCharCode+1, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001650 set_single_character_string_cache(FixedArray::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651
1652 // Allocate cache for external strings pointing to native source code.
1653 obj = AllocateFixedArray(Natives::GetBuiltinsCount());
1654 if (obj->IsFailure()) return false;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001655 set_natives_source_cache(FixedArray::cast(obj));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001656
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001657 // Handling of script id generation is in Factory::NewScript.
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001658 set_last_script_id(undefined_value());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001659
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001660 // Initialize keyed lookup cache.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001661 KeyedLookupCache::Clear();
1662
1663 // Initialize context slot cache.
1664 ContextSlotCache::Clear();
1665
1666 // Initialize descriptor cache.
1667 DescriptorLookupCache::Clear();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001668
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001669 // Initialize compilation cache.
1670 CompilationCache::Clear();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001671
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 return true;
1673}
1674
1675
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001676Object* Heap::InitializeNumberStringCache() {
1677 // Compute the size of the number string cache based on the max heap size.
1678 // max_semispace_size_ == 512 KB => number_string_cache_size = 32.
1679 // max_semispace_size_ == 8 MB => number_string_cache_size = 16KB.
1680 int number_string_cache_size = max_semispace_size_ / 512;
1681 number_string_cache_size = Max(32, Min(16*KB, number_string_cache_size));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001682 Object* obj = AllocateFixedArray(number_string_cache_size * 2, TENURED);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001683 if (!obj->IsFailure()) set_number_string_cache(FixedArray::cast(obj));
1684 return obj;
1685}
1686
1687
1688void Heap::FlushNumberStringCache() {
1689 // Flush the number to string cache.
1690 int len = number_string_cache()->length();
1691 for (int i = 0; i < len; i++) {
1692 number_string_cache()->set_undefined(i);
1693 }
1694}
1695
1696
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697static inline int double_get_hash(double d) {
1698 DoubleRepresentation rep(d);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001699 return static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700}
1701
1702
1703static inline int smi_get_hash(Smi* smi) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001704 return smi->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705}
1706
1707
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708Object* Heap::GetNumberStringCache(Object* number) {
1709 int hash;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001710 int mask = (number_string_cache()->length() >> 1) - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 if (number->IsSmi()) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001712 hash = smi_get_hash(Smi::cast(number)) & mask;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 } else {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001714 hash = double_get_hash(number->Number()) & mask;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001716 Object* key = number_string_cache()->get(hash * 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 if (key == number) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001718 return String::cast(number_string_cache()->get(hash * 2 + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 } else if (key->IsHeapNumber() &&
1720 number->IsHeapNumber() &&
1721 key->Number() == number->Number()) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001722 return String::cast(number_string_cache()->get(hash * 2 + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 }
1724 return undefined_value();
1725}
1726
1727
1728void Heap::SetNumberStringCache(Object* number, String* string) {
1729 int hash;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001730 int mask = (number_string_cache()->length() >> 1) - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731 if (number->IsSmi()) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001732 hash = smi_get_hash(Smi::cast(number)) & mask;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001733 number_string_cache()->set(hash * 2, Smi::cast(number));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734 } else {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001735 hash = double_get_hash(number->Number()) & mask;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001736 number_string_cache()->set(hash * 2, number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001738 number_string_cache()->set(hash * 2 + 1, string);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739}
1740
1741
1742Object* Heap::SmiOrNumberFromDouble(double value,
1743 bool new_object,
1744 PretenureFlag pretenure) {
1745 // We need to distinguish the minus zero value and this cannot be
1746 // done after conversion to int. Doing this by comparing bit
1747 // patterns is faster than using fpclassify() et al.
1748 static const DoubleRepresentation plus_zero(0.0);
1749 static const DoubleRepresentation minus_zero(-0.0);
1750 static const DoubleRepresentation nan(OS::nan_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001751 ASSERT(minus_zero_value() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752 ASSERT(sizeof(plus_zero.value) == sizeof(plus_zero.bits));
1753
1754 DoubleRepresentation rep(value);
1755 if (rep.bits == plus_zero.bits) return Smi::FromInt(0); // not uncommon
1756 if (rep.bits == minus_zero.bits) {
1757 return new_object ? AllocateHeapNumber(-0.0, pretenure)
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001758 : minus_zero_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 }
1760 if (rep.bits == nan.bits) {
1761 return new_object
1762 ? AllocateHeapNumber(OS::nan_value(), pretenure)
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001763 : nan_value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 }
1765
1766 // Try to represent the value as a tagged small integer.
1767 int int_value = FastD2I(value);
1768 if (value == FastI2D(int_value) && Smi::IsValid(int_value)) {
1769 return Smi::FromInt(int_value);
1770 }
1771
1772 // Materialize the value in the heap.
1773 return AllocateHeapNumber(value, pretenure);
1774}
1775
1776
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001777Object* Heap::NumberToString(Object* number) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001778 Counters::number_to_string_runtime.Increment();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001779 Object* cached = GetNumberStringCache(number);
1780 if (cached != undefined_value()) {
1781 return cached;
1782 }
1783
1784 char arr[100];
1785 Vector<char> buffer(arr, ARRAY_SIZE(arr));
1786 const char* str;
1787 if (number->IsSmi()) {
1788 int num = Smi::cast(number)->value();
1789 str = IntToCString(num, buffer);
1790 } else {
1791 double num = HeapNumber::cast(number)->value();
1792 str = DoubleToCString(num, buffer);
1793 }
1794 Object* result = AllocateStringFromAscii(CStrVector(str));
1795
1796 if (!result->IsFailure()) {
1797 SetNumberStringCache(number, String::cast(result));
1798 }
1799 return result;
1800}
1801
1802
ager@chromium.org3811b432009-10-28 14:53:37 +00001803Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) {
1804 return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]);
1805}
1806
1807
1808Heap::RootListIndex Heap::RootIndexForExternalArrayType(
1809 ExternalArrayType array_type) {
1810 switch (array_type) {
1811 case kExternalByteArray:
1812 return kExternalByteArrayMapRootIndex;
1813 case kExternalUnsignedByteArray:
1814 return kExternalUnsignedByteArrayMapRootIndex;
1815 case kExternalShortArray:
1816 return kExternalShortArrayMapRootIndex;
1817 case kExternalUnsignedShortArray:
1818 return kExternalUnsignedShortArrayMapRootIndex;
1819 case kExternalIntArray:
1820 return kExternalIntArrayMapRootIndex;
1821 case kExternalUnsignedIntArray:
1822 return kExternalUnsignedIntArrayMapRootIndex;
1823 case kExternalFloatArray:
1824 return kExternalFloatArrayMapRootIndex;
1825 default:
1826 UNREACHABLE();
1827 return kUndefinedValueRootIndex;
1828 }
1829}
1830
1831
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832Object* Heap::NewNumberFromDouble(double value, PretenureFlag pretenure) {
1833 return SmiOrNumberFromDouble(value,
1834 true /* number object must be new */,
1835 pretenure);
1836}
1837
1838
1839Object* Heap::NumberFromDouble(double value, PretenureFlag pretenure) {
1840 return SmiOrNumberFromDouble(value,
1841 false /* use preallocated NaN, -0.0 */,
1842 pretenure);
1843}
1844
1845
1846Object* Heap::AllocateProxy(Address proxy, PretenureFlag pretenure) {
1847 // Statically ensure that it is safe to allocate proxies in paged spaces.
1848 STATIC_ASSERT(Proxy::kSize <= Page::kMaxHeapObjectSize);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001849 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 Object* result = Allocate(proxy_map(), space);
1851 if (result->IsFailure()) return result;
1852
1853 Proxy::cast(result)->set_proxy(proxy);
1854 return result;
1855}
1856
1857
1858Object* Heap::AllocateSharedFunctionInfo(Object* name) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001859 Object* result = Allocate(shared_function_info_map(), OLD_POINTER_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 if (result->IsFailure()) return result;
1861
1862 SharedFunctionInfo* share = SharedFunctionInfo::cast(result);
1863 share->set_name(name);
1864 Code* illegal = Builtins::builtin(Builtins::Illegal);
1865 share->set_code(illegal);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001866 Code* construct_stub = Builtins::builtin(Builtins::JSConstructStubGeneric);
1867 share->set_construct_stub(construct_stub);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 share->set_expected_nof_properties(0);
1869 share->set_length(0);
1870 share->set_formal_parameter_count(0);
1871 share->set_instance_class_name(Object_symbol());
1872 share->set_function_data(undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 share->set_script(undefined_value());
1874 share->set_start_position_and_type(0);
1875 share->set_debug_info(undefined_value());
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00001876 share->set_inferred_name(empty_string());
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001877 share->set_compiler_hints(0);
1878 share->set_this_property_assignments_count(0);
1879 share->set_this_property_assignments(undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 return result;
1881}
1882
1883
ager@chromium.org6141cbe2009-11-20 12:14:52 +00001884// Returns true for a character in a range. Both limits are inclusive.
1885static inline bool Between(uint32_t character, uint32_t from, uint32_t to) {
1886 // This makes uses of the the unsigned wraparound.
1887 return character - from <= to - from;
1888}
1889
1890
1891static inline Object* MakeOrFindTwoCharacterString(uint32_t c1, uint32_t c2) {
1892 String* symbol;
1893 // Numeric strings have a different hash algorithm not known by
1894 // LookupTwoCharsSymbolIfExists, so we skip this step for such strings.
1895 if ((!Between(c1, '0', '9') || !Between(c2, '0', '9')) &&
1896 Heap::symbol_table()->LookupTwoCharsSymbolIfExists(c1, c2, &symbol)) {
1897 return symbol;
1898 // Now we know the length is 2, we might as well make use of that fact
1899 // when building the new string.
1900 } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this
1901 ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this.
1902 Object* result = Heap::AllocateRawAsciiString(2);
1903 if (result->IsFailure()) return result;
1904 char* dest = SeqAsciiString::cast(result)->GetChars();
1905 dest[0] = c1;
1906 dest[1] = c2;
1907 return result;
1908 } else {
1909 Object* result = Heap::AllocateRawTwoByteString(2);
1910 if (result->IsFailure()) return result;
1911 uc16* dest = SeqTwoByteString::cast(result)->GetChars();
1912 dest[0] = c1;
1913 dest[1] = c2;
1914 return result;
1915 }
1916}
1917
1918
ager@chromium.org3e875802009-06-29 08:26:34 +00001919Object* Heap::AllocateConsString(String* first, String* second) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001920 int first_length = first->length();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001921 if (first_length == 0) {
1922 return second;
1923 }
ager@chromium.org3e875802009-06-29 08:26:34 +00001924
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001925 int second_length = second->length();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001926 if (second_length == 0) {
1927 return first;
1928 }
ager@chromium.org3e875802009-06-29 08:26:34 +00001929
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001930 int length = first_length + second_length;
ager@chromium.org6141cbe2009-11-20 12:14:52 +00001931
1932 // Optimization for 2-byte strings often used as keys in a decompression
1933 // dictionary. Check whether we already have the string in the symbol
1934 // table to prevent creation of many unneccesary strings.
1935 if (length == 2) {
1936 unsigned c1 = first->Get(0);
1937 unsigned c2 = second->Get(0);
1938 return MakeOrFindTwoCharacterString(c1, c2);
1939 }
1940
ager@chromium.org5ec48922009-05-05 07:25:34 +00001941 bool is_ascii = first->IsAsciiRepresentation()
1942 && second->IsAsciiRepresentation();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943
ager@chromium.org3e875802009-06-29 08:26:34 +00001944 // Make sure that an out of memory exception is thrown if the length
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001945 // of the new cons string is too large.
1946 if (length > String::kMaxLength || length < 0) {
ager@chromium.org3e875802009-06-29 08:26:34 +00001947 Top::context()->mark_out_of_memory();
1948 return Failure::OutOfMemoryException();
1949 }
1950
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 // If the resulting string is small make a flat string.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001952 if (length < String::kMinNonFlatLength) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001953 ASSERT(first->IsFlat());
1954 ASSERT(second->IsFlat());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001955 if (is_ascii) {
1956 Object* result = AllocateRawAsciiString(length);
1957 if (result->IsFailure()) return result;
1958 // Copy the characters into the new object.
1959 char* dest = SeqAsciiString::cast(result)->GetChars();
ager@chromium.org3e875802009-06-29 08:26:34 +00001960 // Copy first part.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001961 const char* src;
1962 if (first->IsExternalString()) {
1963 src = ExternalAsciiString::cast(first)->resource()->data();
1964 } else {
1965 src = SeqAsciiString::cast(first)->GetChars();
1966 }
ager@chromium.org3e875802009-06-29 08:26:34 +00001967 for (int i = 0; i < first_length; i++) *dest++ = src[i];
1968 // Copy second part.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001969 if (second->IsExternalString()) {
1970 src = ExternalAsciiString::cast(second)->resource()->data();
1971 } else {
1972 src = SeqAsciiString::cast(second)->GetChars();
1973 }
ager@chromium.org3e875802009-06-29 08:26:34 +00001974 for (int i = 0; i < second_length; i++) *dest++ = src[i];
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001975 return result;
1976 } else {
1977 Object* result = AllocateRawTwoByteString(length);
1978 if (result->IsFailure()) return result;
1979 // Copy the characters into the new object.
1980 uc16* dest = SeqTwoByteString::cast(result)->GetChars();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001981 String::WriteToFlat(first, dest, 0, first_length);
1982 String::WriteToFlat(second, dest + first_length, 0, second_length);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001983 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985 }
1986
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001987 Map* map = is_ascii ? cons_ascii_string_map() : cons_string_map();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001989 Object* result = Allocate(map, NEW_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 if (result->IsFailure()) return result;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001991
1992 AssertNoAllocation no_gc;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 ConsString* cons_string = ConsString::cast(result);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001994 WriteBarrierMode mode = cons_string->GetWriteBarrierMode(no_gc);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001995 cons_string->set_length(length);
1996 cons_string->set_hash_field(String::kEmptyHashField);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001997 cons_string->set_first(first, mode);
1998 cons_string->set_second(second, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 return result;
2000}
2001
2002
ager@chromium.org870a0b62008-11-04 11:43:05 +00002003Object* Heap::AllocateSubString(String* buffer,
ager@chromium.org870a0b62008-11-04 11:43:05 +00002004 int start,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002005 int end,
2006 PretenureFlag pretenure) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 int length = end - start;
2008
ager@chromium.org7c537e22008-10-16 08:43:32 +00002009 if (length == 1) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00002010 return Heap::LookupSingleCharacterStringFromCode(
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002011 buffer->Get(start));
ager@chromium.org6141cbe2009-11-20 12:14:52 +00002012 } else if (length == 2) {
2013 // Optimization for 2-byte strings often used as keys in a decompression
2014 // dictionary. Check whether we already have the string in the symbol
2015 // table to prevent creation of many unneccesary strings.
2016 unsigned c1 = buffer->Get(start);
2017 unsigned c2 = buffer->Get(start + 1);
2018 return MakeOrFindTwoCharacterString(c1, c2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002019 }
2020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 // Make an attempt to flatten the buffer to reduce access time.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002022 buffer->TryFlatten();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023
ager@chromium.org5ec48922009-05-05 07:25:34 +00002024 Object* result = buffer->IsAsciiRepresentation()
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002025 ? AllocateRawAsciiString(length, pretenure )
2026 : AllocateRawTwoByteString(length, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 if (result->IsFailure()) return result;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002028 String* string_result = String::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 // Copy the characters into the new object.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002030 if (buffer->IsAsciiRepresentation()) {
2031 ASSERT(string_result->IsAsciiRepresentation());
2032 char* dest = SeqAsciiString::cast(string_result)->GetChars();
2033 String::WriteToFlat(buffer, dest, start, end);
2034 } else {
2035 ASSERT(string_result->IsTwoByteRepresentation());
2036 uc16* dest = SeqTwoByteString::cast(string_result)->GetChars();
2037 String::WriteToFlat(buffer, dest, start, end);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002039
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 return result;
2041}
2042
2043
2044Object* Heap::AllocateExternalStringFromAscii(
2045 ExternalAsciiString::Resource* resource) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002046 size_t length = resource->length();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002047 if (length > static_cast<size_t>(String::kMaxLength)) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002048 Top::context()->mark_out_of_memory();
2049 return Failure::OutOfMemoryException();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 }
2051
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002052 Map* map = external_ascii_string_map();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002053 Object* result = Allocate(map, NEW_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 if (result->IsFailure()) return result;
2055
2056 ExternalAsciiString* external_string = ExternalAsciiString::cast(result);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002057 external_string->set_length(static_cast<int>(length));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002058 external_string->set_hash_field(String::kEmptyHashField);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059 external_string->set_resource(resource);
2060
2061 return result;
2062}
2063
2064
2065Object* Heap::AllocateExternalStringFromTwoByte(
2066 ExternalTwoByteString::Resource* resource) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002067 size_t length = resource->length();
2068 if (length > static_cast<size_t>(String::kMaxLength)) {
2069 Top::context()->mark_out_of_memory();
2070 return Failure::OutOfMemoryException();
2071 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002072
2073 Map* map = Heap::external_string_map();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002074 Object* result = Allocate(map, NEW_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 if (result->IsFailure()) return result;
2076
2077 ExternalTwoByteString* external_string = ExternalTwoByteString::cast(result);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002078 external_string->set_length(static_cast<int>(length));
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002079 external_string->set_hash_field(String::kEmptyHashField);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 external_string->set_resource(resource);
2081
2082 return result;
2083}
2084
2085
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002086Object* Heap::LookupSingleCharacterStringFromCode(uint16_t code) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 if (code <= String::kMaxAsciiCharCode) {
2088 Object* value = Heap::single_character_string_cache()->get(code);
2089 if (value != Heap::undefined_value()) return value;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002090
2091 char buffer[1];
2092 buffer[0] = static_cast<char>(code);
2093 Object* result = LookupSymbol(Vector<const char>(buffer, 1));
2094
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 if (result->IsFailure()) return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 Heap::single_character_string_cache()->set(code, result);
2097 return result;
2098 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002099
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002100 Object* result = Heap::AllocateRawTwoByteString(1);
2101 if (result->IsFailure()) return result;
ager@chromium.org870a0b62008-11-04 11:43:05 +00002102 String* answer = String::cast(result);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002103 answer->Set(0, code);
ager@chromium.org870a0b62008-11-04 11:43:05 +00002104 return answer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105}
2106
2107
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002108Object* Heap::AllocateByteArray(int length, PretenureFlag pretenure) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002109 if (length < 0 || length > ByteArray::kMaxLength) {
2110 return Failure::OutOfMemoryException();
2111 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002112 if (pretenure == NOT_TENURED) {
2113 return AllocateByteArray(length);
2114 }
2115 int size = ByteArray::SizeFor(length);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002116 Object* result = (size <= MaxObjectSizeInPagedSpace())
2117 ? old_data_space_->AllocateRaw(size)
2118 : lo_space_->AllocateRaw(size);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002119 if (result->IsFailure()) return result;
2120
2121 reinterpret_cast<Array*>(result)->set_map(byte_array_map());
2122 reinterpret_cast<Array*>(result)->set_length(length);
2123 return result;
2124}
2125
2126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127Object* Heap::AllocateByteArray(int length) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002128 if (length < 0 || length > ByteArray::kMaxLength) {
2129 return Failure::OutOfMemoryException();
2130 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 int size = ByteArray::SizeFor(length);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002132 AllocationSpace space =
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002133 (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : NEW_SPACE;
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002134 Object* result = AllocateRaw(size, space, OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 if (result->IsFailure()) return result;
2136
2137 reinterpret_cast<Array*>(result)->set_map(byte_array_map());
2138 reinterpret_cast<Array*>(result)->set_length(length);
2139 return result;
2140}
2141
2142
ager@chromium.org6f10e412009-02-13 10:11:16 +00002143void Heap::CreateFillerObjectAt(Address addr, int size) {
2144 if (size == 0) return;
2145 HeapObject* filler = HeapObject::FromAddress(addr);
2146 if (size == kPointerSize) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002147 filler->set_map(Heap::one_pointer_filler_map());
ager@chromium.org6f10e412009-02-13 10:11:16 +00002148 } else {
2149 filler->set_map(Heap::byte_array_map());
2150 ByteArray::cast(filler)->set_length(ByteArray::LengthFor(size));
2151 }
2152}
2153
2154
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002155Object* Heap::AllocatePixelArray(int length,
2156 uint8_t* external_pointer,
2157 PretenureFlag pretenure) {
2158 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002159 Object* result = AllocateRaw(PixelArray::kAlignedSize, space, OLD_DATA_SPACE);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002160 if (result->IsFailure()) return result;
2161
2162 reinterpret_cast<PixelArray*>(result)->set_map(pixel_array_map());
2163 reinterpret_cast<PixelArray*>(result)->set_length(length);
2164 reinterpret_cast<PixelArray*>(result)->set_external_pointer(external_pointer);
2165
2166 return result;
2167}
2168
2169
ager@chromium.org3811b432009-10-28 14:53:37 +00002170Object* Heap::AllocateExternalArray(int length,
2171 ExternalArrayType array_type,
2172 void* external_pointer,
2173 PretenureFlag pretenure) {
2174 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
ager@chromium.org3811b432009-10-28 14:53:37 +00002175 Object* result = AllocateRaw(ExternalArray::kAlignedSize,
2176 space,
2177 OLD_DATA_SPACE);
ager@chromium.org3811b432009-10-28 14:53:37 +00002178 if (result->IsFailure()) return result;
2179
2180 reinterpret_cast<ExternalArray*>(result)->set_map(
2181 MapForExternalArrayType(array_type));
2182 reinterpret_cast<ExternalArray*>(result)->set_length(length);
2183 reinterpret_cast<ExternalArray*>(result)->set_external_pointer(
2184 external_pointer);
2185
2186 return result;
2187}
2188
2189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190Object* Heap::CreateCode(const CodeDesc& desc,
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002191 ZoneScopeInfo* sinfo,
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002192 Code::Flags flags,
kasperl@chromium.org061ef742009-02-27 12:16:20 +00002193 Handle<Object> self_reference) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 // Compute size
2195 int body_size = RoundUp(desc.instr_size + desc.reloc_size, kObjectAlignment);
2196 int sinfo_size = 0;
2197 if (sinfo != NULL) sinfo_size = sinfo->Serialize(NULL);
2198 int obj_size = Code::SizeFor(body_size, sinfo_size);
kasperl@chromium.org061ef742009-02-27 12:16:20 +00002199 ASSERT(IsAligned(obj_size, Code::kCodeAlignment));
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002200 Object* result;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002201 if (obj_size > MaxObjectSizeInPagedSpace()) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002202 result = lo_space_->AllocateRawCode(obj_size);
2203 } else {
2204 result = code_space_->AllocateRaw(obj_size);
2205 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 if (result->IsFailure()) return result;
2208
2209 // Initialize the object
2210 HeapObject::cast(result)->set_map(code_map());
2211 Code* code = Code::cast(result);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002212 ASSERT(!CodeRange::exists() || CodeRange::contains(code->address()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 code->set_instruction_size(desc.instr_size);
2214 code->set_relocation_size(desc.reloc_size);
2215 code->set_sinfo_size(sinfo_size);
2216 code->set_flags(flags);
kasperl@chromium.org061ef742009-02-27 12:16:20 +00002217 // Allow self references to created code object by patching the handle to
2218 // point to the newly allocated Code object.
2219 if (!self_reference.is_null()) {
2220 *(self_reference.location()) = code;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002221 }
2222 // Migrate generated code.
2223 // The generated code can contain Object** values (typically from handles)
2224 // that are dereferenced during the copy to point directly to the actual heap
2225 // objects. These pointers can include references to the code object itself,
2226 // through the self_reference parameter.
2227 code->CopyFrom(desc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 if (sinfo != NULL) sinfo->Serialize(code); // write scope info
2229
2230#ifdef DEBUG
2231 code->Verify();
2232#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 return code;
2234}
2235
2236
2237Object* Heap::CopyCode(Code* code) {
2238 // Allocate an object the same size as the code object.
2239 int obj_size = code->Size();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002240 Object* result;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002241 if (obj_size > MaxObjectSizeInPagedSpace()) {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002242 result = lo_space_->AllocateRawCode(obj_size);
2243 } else {
2244 result = code_space_->AllocateRaw(obj_size);
2245 }
2246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 if (result->IsFailure()) return result;
2248
2249 // Copy code object.
2250 Address old_addr = code->address();
2251 Address new_addr = reinterpret_cast<HeapObject*>(result)->address();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002252 CopyBlock(reinterpret_cast<Object**>(new_addr),
2253 reinterpret_cast<Object**>(old_addr),
2254 obj_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 // Relocate the copy.
2256 Code* new_code = Code::cast(result);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002257 ASSERT(!CodeRange::exists() || CodeRange::contains(code->address()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 new_code->Relocate(new_addr - old_addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 return new_code;
2260}
2261
2262
fschneider@chromium.org086aac62010-03-17 13:18:24 +00002263Object* Heap::CopyCode(Code* code, Vector<byte> reloc_info) {
2264 int new_body_size = RoundUp(code->instruction_size() + reloc_info.length(),
2265 kObjectAlignment);
2266
2267 int sinfo_size = code->sinfo_size();
2268
2269 int new_obj_size = Code::SizeFor(new_body_size, sinfo_size);
2270
2271 Address old_addr = code->address();
2272
2273 int relocation_offset = code->relocation_start() - old_addr;
2274
2275 Object* result;
2276 if (new_obj_size > MaxObjectSizeInPagedSpace()) {
2277 result = lo_space_->AllocateRawCode(new_obj_size);
2278 } else {
2279 result = code_space_->AllocateRaw(new_obj_size);
2280 }
2281
2282 if (result->IsFailure()) return result;
2283
2284 // Copy code object.
2285 Address new_addr = reinterpret_cast<HeapObject*>(result)->address();
2286
2287 // Copy header and instructions.
2288 memcpy(new_addr, old_addr, relocation_offset);
2289
2290 // Copy patched rinfo.
2291 memcpy(new_addr + relocation_offset,
2292 reloc_info.start(),
2293 reloc_info.length());
2294
2295 Code* new_code = Code::cast(result);
2296 new_code->set_relocation_size(reloc_info.length());
2297
2298 // Copy sinfo.
2299 memcpy(new_code->sinfo_start(), code->sinfo_start(), code->sinfo_size());
2300
2301 // Relocate the copy.
2302 ASSERT(!CodeRange::exists() || CodeRange::contains(code->address()));
2303 new_code->Relocate(new_addr - old_addr);
2304
2305#ifdef DEBUG
2306 code->Verify();
2307#endif
2308 return new_code;
2309}
2310
2311
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312Object* Heap::Allocate(Map* map, AllocationSpace space) {
2313 ASSERT(gc_state_ == NOT_IN_GC);
2314 ASSERT(map->instance_type() != MAP_TYPE);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002315 // If allocation failures are disallowed, we may allocate in a different
2316 // space when new space is full and the object is not a large object.
2317 AllocationSpace retry_space =
2318 (space != NEW_SPACE) ? space : TargetSpaceId(map->instance_type());
2319 Object* result =
2320 AllocateRaw(map->instance_size(), space, retry_space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 if (result->IsFailure()) return result;
2322 HeapObject::cast(result)->set_map(map);
ager@chromium.org3811b432009-10-28 14:53:37 +00002323#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002324 ProducerHeapProfile::RecordJSObjectAllocation(result);
ager@chromium.org3811b432009-10-28 14:53:37 +00002325#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 return result;
2327}
2328
2329
2330Object* Heap::InitializeFunction(JSFunction* function,
2331 SharedFunctionInfo* shared,
2332 Object* prototype) {
2333 ASSERT(!prototype->IsMap());
2334 function->initialize_properties();
2335 function->initialize_elements();
2336 function->set_shared(shared);
2337 function->set_prototype_or_initial_map(prototype);
2338 function->set_context(undefined_value());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002339 function->set_literals(empty_fixed_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 return function;
2341}
2342
2343
2344Object* Heap::AllocateFunctionPrototype(JSFunction* function) {
ager@chromium.orgddb913d2009-01-27 10:01:48 +00002345 // Allocate the prototype. Make sure to use the object function
2346 // from the function's context, since the function can be from a
2347 // different context.
2348 JSFunction* object_function =
2349 function->context()->global_context()->object_function();
2350 Object* prototype = AllocateJSObject(object_function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 if (prototype->IsFailure()) return prototype;
2352 // When creating the prototype for the function we must set its
2353 // constructor to the function.
2354 Object* result =
2355 JSObject::cast(prototype)->SetProperty(constructor_symbol(),
2356 function,
2357 DONT_ENUM);
2358 if (result->IsFailure()) return result;
2359 return prototype;
2360}
2361
2362
2363Object* Heap::AllocateFunction(Map* function_map,
2364 SharedFunctionInfo* shared,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00002365 Object* prototype,
2366 PretenureFlag pretenure) {
2367 AllocationSpace space =
2368 (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE;
2369 Object* result = Allocate(function_map, space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002370 if (result->IsFailure()) return result;
2371 return InitializeFunction(JSFunction::cast(result), shared, prototype);
2372}
2373
2374
2375Object* Heap::AllocateArgumentsObject(Object* callee, int length) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002376 // To get fast allocation and map sharing for arguments objects we
2377 // allocate them based on an arguments boilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378
2379 // This calls Copy directly rather than using Heap::AllocateRaw so we
2380 // duplicate the check here.
2381 ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC);
2382
2383 JSObject* boilerplate =
2384 Top::context()->global_context()->arguments_boilerplate();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002385
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00002386 // Check that the size of the boilerplate matches our
2387 // expectations. The ArgumentsAccessStub::GenerateNewObject relies
2388 // on the size being a known constant.
2389 ASSERT(kArgumentsObjectSize == boilerplate->map()->instance_size());
2390
2391 // Do the allocation.
2392 Object* result =
2393 AllocateRaw(kArgumentsObjectSize, NEW_SPACE, OLD_POINTER_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394 if (result->IsFailure()) return result;
2395
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002396 // Copy the content. The arguments boilerplate doesn't have any
2397 // fields that point to new space so it's safe to skip the write
2398 // barrier here.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002399 CopyBlock(reinterpret_cast<Object**>(HeapObject::cast(result)->address()),
2400 reinterpret_cast<Object**>(boilerplate->address()),
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00002401 kArgumentsObjectSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002403 // Set the two properties.
2404 JSObject::cast(result)->InObjectPropertyAtPut(arguments_callee_index,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002405 callee);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002406 JSObject::cast(result)->InObjectPropertyAtPut(arguments_length_index,
2407 Smi::FromInt(length),
2408 SKIP_WRITE_BARRIER);
2409
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 // Check the state of the object
2411 ASSERT(JSObject::cast(result)->HasFastProperties());
2412 ASSERT(JSObject::cast(result)->HasFastElements());
2413
2414 return result;
2415}
2416
2417
2418Object* Heap::AllocateInitialMap(JSFunction* fun) {
2419 ASSERT(!fun->has_initial_map());
2420
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002421 // First create a new map with the size and number of in-object properties
2422 // suggested by the function.
2423 int instance_size = fun->shared()->CalculateInstanceSize();
2424 int in_object_properties = fun->shared()->CalculateInObjectProperties();
ager@chromium.org7c537e22008-10-16 08:43:32 +00002425 Object* map_obj = Heap::AllocateMap(JS_OBJECT_TYPE, instance_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 if (map_obj->IsFailure()) return map_obj;
2427
2428 // Fetch or allocate prototype.
2429 Object* prototype;
2430 if (fun->has_instance_prototype()) {
2431 prototype = fun->instance_prototype();
2432 } else {
2433 prototype = AllocateFunctionPrototype(fun);
2434 if (prototype->IsFailure()) return prototype;
2435 }
2436 Map* map = Map::cast(map_obj);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002437 map->set_inobject_properties(in_object_properties);
2438 map->set_unused_property_fields(in_object_properties);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439 map->set_prototype(prototype);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002440
ager@chromium.org5c838252010-02-19 08:53:10 +00002441 // If the function has only simple this property assignments add
2442 // field descriptors for these to the initial map as the object
2443 // cannot be constructed without having these properties. Guard by
2444 // the inline_new flag so we only change the map if we generate a
2445 // specialized construct stub.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002446 ASSERT(in_object_properties <= Map::kMaxPreAllocatedPropertyFields);
ager@chromium.org5c838252010-02-19 08:53:10 +00002447 if (fun->shared()->CanGenerateInlineConstructor(prototype)) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002448 int count = fun->shared()->this_property_assignments_count();
2449 if (count > in_object_properties) {
2450 count = in_object_properties;
2451 }
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00002452 Object* descriptors_obj = DescriptorArray::Allocate(count);
2453 if (descriptors_obj->IsFailure()) return descriptors_obj;
2454 DescriptorArray* descriptors = DescriptorArray::cast(descriptors_obj);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002455 for (int i = 0; i < count; i++) {
2456 String* name = fun->shared()->GetThisPropertyAssignmentName(i);
2457 ASSERT(name->IsSymbol());
2458 FieldDescriptor field(name, i, NONE);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002459 field.SetEnumerationIndex(i);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002460 descriptors->Set(i, &field);
2461 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002462 descriptors->SetNextEnumerationIndex(count);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002463 descriptors->Sort();
2464 map->set_instance_descriptors(descriptors);
2465 map->set_pre_allocated_property_fields(count);
2466 map->set_unused_property_fields(in_object_properties - count);
2467 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 return map;
2469}
2470
2471
2472void Heap::InitializeJSObjectFromMap(JSObject* obj,
2473 FixedArray* properties,
2474 Map* map) {
2475 obj->set_properties(properties);
2476 obj->initialize_elements();
2477 // TODO(1240798): Initialize the object's body using valid initial values
2478 // according to the object's initial map. For example, if the map's
2479 // instance type is JS_ARRAY_TYPE, the length field should be initialized
2480 // to a number (eg, Smi::FromInt(0)) and the elements initialized to a
2481 // fixed array (eg, Heap::empty_fixed_array()). Currently, the object
2482 // verification code has to cope with (temporarily) invalid objects. See
2483 // for example, JSArray::JSArrayVerify).
2484 obj->InitializeBody(map->instance_size());
2485}
2486
2487
2488Object* Heap::AllocateJSObjectFromMap(Map* map, PretenureFlag pretenure) {
2489 // JSFunctions should be allocated using AllocateFunction to be
2490 // properly initialized.
2491 ASSERT(map->instance_type() != JS_FUNCTION_TYPE);
2492
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002493 // Both types of globla objects should be allocated using
2494 // AllocateGloblaObject to be properly initialized.
2495 ASSERT(map->instance_type() != JS_GLOBAL_OBJECT_TYPE);
2496 ASSERT(map->instance_type() != JS_BUILTINS_OBJECT_TYPE);
2497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002498 // Allocate the backing storage for the properties.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002499 int prop_size =
2500 map->pre_allocated_property_fields() +
2501 map->unused_property_fields() -
2502 map->inobject_properties();
2503 ASSERT(prop_size >= 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002504 Object* properties = AllocateFixedArray(prop_size, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 if (properties->IsFailure()) return properties;
2506
2507 // Allocate the JSObject.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002508 AllocationSpace space =
2509 (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002510 if (map->instance_size() > MaxObjectSizeInPagedSpace()) space = LO_SPACE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511 Object* obj = Allocate(map, space);
2512 if (obj->IsFailure()) return obj;
2513
2514 // Initialize the JSObject.
2515 InitializeJSObjectFromMap(JSObject::cast(obj),
2516 FixedArray::cast(properties),
2517 map);
2518 return obj;
2519}
2520
2521
2522Object* Heap::AllocateJSObject(JSFunction* constructor,
2523 PretenureFlag pretenure) {
2524 // Allocate the initial map if absent.
2525 if (!constructor->has_initial_map()) {
2526 Object* initial_map = AllocateInitialMap(constructor);
2527 if (initial_map->IsFailure()) return initial_map;
2528 constructor->set_initial_map(Map::cast(initial_map));
2529 Map::cast(initial_map)->set_constructor(constructor);
2530 }
2531 // Allocate the object based on the constructors initial map.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002532 Object* result =
2533 AllocateJSObjectFromMap(constructor->initial_map(), pretenure);
2534 // Make sure result is NOT a global object if valid.
2535 ASSERT(result->IsFailure() || !result->IsGlobalObject());
2536 return result;
2537}
2538
2539
2540Object* Heap::AllocateGlobalObject(JSFunction* constructor) {
2541 ASSERT(constructor->has_initial_map());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002542 Map* map = constructor->initial_map();
2543
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002544 // Make sure no field properties are described in the initial map.
2545 // This guarantees us that normalizing the properties does not
2546 // require us to change property values to JSGlobalPropertyCells.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002547 ASSERT(map->NextFreePropertyIndex() == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002548
2549 // Make sure we don't have a ton of pre-allocated slots in the
2550 // global objects. They will be unused once we normalize the object.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002551 ASSERT(map->unused_property_fields() == 0);
2552 ASSERT(map->inobject_properties() == 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002553
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002554 // Initial size of the backing store to avoid resize of the storage during
2555 // bootstrapping. The size differs between the JS global object ad the
2556 // builtins object.
2557 int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002558
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002559 // Allocate a dictionary object for backing storage.
2560 Object* obj =
2561 StringDictionary::Allocate(
2562 map->NumberOfDescribedProperties() * 2 + initial_size);
2563 if (obj->IsFailure()) return obj;
2564 StringDictionary* dictionary = StringDictionary::cast(obj);
2565
2566 // The global object might be created from an object template with accessors.
2567 // Fill these accessors into the dictionary.
2568 DescriptorArray* descs = map->instance_descriptors();
2569 for (int i = 0; i < descs->number_of_descriptors(); i++) {
2570 PropertyDetails details = descs->GetDetails(i);
2571 ASSERT(details.type() == CALLBACKS); // Only accessors are expected.
2572 PropertyDetails d =
2573 PropertyDetails(details.attributes(), CALLBACKS, details.index());
2574 Object* value = descs->GetCallbacksObject(i);
2575 value = Heap::AllocateJSGlobalPropertyCell(value);
2576 if (value->IsFailure()) return value;
2577
2578 Object* result = dictionary->Add(descs->GetKey(i), value, d);
2579 if (result->IsFailure()) return result;
2580 dictionary = StringDictionary::cast(result);
2581 }
2582
2583 // Allocate the global object and initialize it with the backing store.
2584 obj = Allocate(map, OLD_POINTER_SPACE);
2585 if (obj->IsFailure()) return obj;
2586 JSObject* global = JSObject::cast(obj);
2587 InitializeJSObjectFromMap(global, dictionary, map);
2588
2589 // Create a new map for the global object.
2590 obj = map->CopyDropDescriptors();
2591 if (obj->IsFailure()) return obj;
2592 Map* new_map = Map::cast(obj);
2593
2594 // Setup the global object as a normalized object.
2595 global->set_map(new_map);
2596 global->map()->set_instance_descriptors(Heap::empty_descriptor_array());
2597 global->set_properties(dictionary);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002598
2599 // Make sure result is a global object with properties in dictionary.
2600 ASSERT(global->IsGlobalObject());
2601 ASSERT(!global->HasFastProperties());
2602 return global;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603}
2604
2605
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002606Object* Heap::CopyJSObject(JSObject* source) {
2607 // Never used to copy functions. If functions need to be copied we
2608 // have to be careful to clear the literals array.
2609 ASSERT(!source->IsJSFunction());
2610
2611 // Make the clone.
2612 Map* map = source->map();
2613 int object_size = map->instance_size();
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002614 Object* clone;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002615
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002616 // If we're forced to always allocate, we use the general allocation
2617 // functions which may leave us with an object in old space.
2618 if (always_allocate()) {
2619 clone = AllocateRaw(object_size, NEW_SPACE, OLD_POINTER_SPACE);
2620 if (clone->IsFailure()) return clone;
2621 Address clone_address = HeapObject::cast(clone)->address();
2622 CopyBlock(reinterpret_cast<Object**>(clone_address),
2623 reinterpret_cast<Object**>(source->address()),
2624 object_size);
2625 // Update write barrier for all fields that lie beyond the header.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002626 RecordWrites(clone_address,
2627 JSObject::kHeaderSize,
2628 object_size - JSObject::kHeaderSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002629 } else {
2630 clone = new_space_.AllocateRaw(object_size);
2631 if (clone->IsFailure()) return clone;
2632 ASSERT(Heap::InNewSpace(clone));
2633 // Since we know the clone is allocated in new space, we can copy
ager@chromium.org32912102009-01-16 10:38:43 +00002634 // the contents without worrying about updating the write barrier.
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002635 CopyBlock(reinterpret_cast<Object**>(HeapObject::cast(clone)->address()),
2636 reinterpret_cast<Object**>(source->address()),
2637 object_size);
2638 }
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002639
2640 FixedArray* elements = FixedArray::cast(source->elements());
2641 FixedArray* properties = FixedArray::cast(source->properties());
2642 // Update elements if necessary.
2643 if (elements->length()> 0) {
2644 Object* elem = CopyFixedArray(elements);
2645 if (elem->IsFailure()) return elem;
2646 JSObject::cast(clone)->set_elements(FixedArray::cast(elem));
2647 }
2648 // Update properties if necessary.
2649 if (properties->length() > 0) {
2650 Object* prop = CopyFixedArray(properties);
2651 if (prop->IsFailure()) return prop;
2652 JSObject::cast(clone)->set_properties(FixedArray::cast(prop));
2653 }
2654 // Return the new clone.
ager@chromium.org3811b432009-10-28 14:53:37 +00002655#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002656 ProducerHeapProfile::RecordJSObjectAllocation(clone);
ager@chromium.org3811b432009-10-28 14:53:37 +00002657#endif
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002658 return clone;
2659}
2660
2661
2662Object* Heap::ReinitializeJSGlobalProxy(JSFunction* constructor,
2663 JSGlobalProxy* object) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 // Allocate initial map if absent.
2665 if (!constructor->has_initial_map()) {
2666 Object* initial_map = AllocateInitialMap(constructor);
2667 if (initial_map->IsFailure()) return initial_map;
2668 constructor->set_initial_map(Map::cast(initial_map));
2669 Map::cast(initial_map)->set_constructor(constructor);
2670 }
2671
2672 Map* map = constructor->initial_map();
2673
2674 // Check that the already allocated object has the same size as
2675 // objects allocated using the constructor.
2676 ASSERT(map->instance_size() == object->map()->instance_size());
2677
2678 // Allocate the backing storage for the properties.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002679 int prop_size = map->unused_property_fields() - map->inobject_properties();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002680 Object* properties = AllocateFixedArray(prop_size, TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002681 if (properties->IsFailure()) return properties;
2682
2683 // Reset the map for the object.
2684 object->set_map(constructor->initial_map());
2685
2686 // Reinitialize the object from the constructor map.
2687 InitializeJSObjectFromMap(object, FixedArray::cast(properties), map);
2688 return object;
2689}
2690
2691
2692Object* Heap::AllocateStringFromAscii(Vector<const char> string,
2693 PretenureFlag pretenure) {
2694 Object* result = AllocateRawAsciiString(string.length(), pretenure);
2695 if (result->IsFailure()) return result;
2696
2697 // Copy the characters into the new object.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002698 SeqAsciiString* string_result = SeqAsciiString::cast(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 for (int i = 0; i < string.length(); i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002700 string_result->SeqAsciiStringSet(i, string[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 }
2702 return result;
2703}
2704
2705
2706Object* Heap::AllocateStringFromUtf8(Vector<const char> string,
2707 PretenureFlag pretenure) {
2708 // Count the number of characters in the UTF-8 string and check if
2709 // it is an ASCII string.
2710 Access<Scanner::Utf8Decoder> decoder(Scanner::utf8_decoder());
2711 decoder->Reset(string.start(), string.length());
2712 int chars = 0;
2713 bool is_ascii = true;
2714 while (decoder->has_more()) {
2715 uc32 r = decoder->GetNext();
2716 if (r > String::kMaxAsciiCharCode) is_ascii = false;
2717 chars++;
2718 }
2719
2720 // If the string is ascii, we do not need to convert the characters
2721 // since UTF8 is backwards compatible with ascii.
2722 if (is_ascii) return AllocateStringFromAscii(string, pretenure);
2723
2724 Object* result = AllocateRawTwoByteString(chars, pretenure);
2725 if (result->IsFailure()) return result;
2726
2727 // Convert and copy the characters into the new object.
2728 String* string_result = String::cast(result);
2729 decoder->Reset(string.start(), string.length());
2730 for (int i = 0; i < chars; i++) {
2731 uc32 r = decoder->GetNext();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002732 string_result->Set(i, r);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733 }
2734 return result;
2735}
2736
2737
2738Object* Heap::AllocateStringFromTwoByte(Vector<const uc16> string,
2739 PretenureFlag pretenure) {
2740 // Check if the string is an ASCII string.
2741 int i = 0;
2742 while (i < string.length() && string[i] <= String::kMaxAsciiCharCode) i++;
2743
2744 Object* result;
2745 if (i == string.length()) { // It's an ASCII string.
2746 result = AllocateRawAsciiString(string.length(), pretenure);
2747 } else { // It's not an ASCII string.
2748 result = AllocateRawTwoByteString(string.length(), pretenure);
2749 }
2750 if (result->IsFailure()) return result;
2751
2752 // Copy the characters into the new object, which may be either ASCII or
2753 // UTF-16.
2754 String* string_result = String::cast(result);
2755 for (int i = 0; i < string.length(); i++) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002756 string_result->Set(i, string[i]);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757 }
2758 return result;
2759}
2760
2761
2762Map* Heap::SymbolMapForString(String* string) {
2763 // If the string is in new space it cannot be used as a symbol.
2764 if (InNewSpace(string)) return NULL;
2765
2766 // Find the corresponding symbol map for strings.
2767 Map* map = string->map();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002768 if (map == ascii_string_map()) return ascii_symbol_map();
2769 if (map == string_map()) return symbol_map();
2770 if (map == cons_string_map()) return cons_symbol_map();
2771 if (map == cons_ascii_string_map()) return cons_ascii_symbol_map();
2772 if (map == external_string_map()) return external_symbol_map();
2773 if (map == external_ascii_string_map()) return external_ascii_symbol_map();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774
2775 // No match found.
2776 return NULL;
2777}
2778
2779
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002780Object* Heap::AllocateInternalSymbol(unibrow::CharacterStream* buffer,
2781 int chars,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002782 uint32_t hash_field) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002783 ASSERT(chars >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784 // Ensure the chars matches the number of characters in the buffer.
2785 ASSERT(static_cast<unsigned>(chars) == buffer->Length());
2786 // Determine whether the string is ascii.
2787 bool is_ascii = true;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002788 while (buffer->has_more()) {
2789 if (buffer->GetNext() > unibrow::Utf8::kMaxOneByteChar) {
2790 is_ascii = false;
2791 break;
2792 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002793 }
2794 buffer->Rewind();
2795
2796 // Compute map and object size.
2797 int size;
2798 Map* map;
2799
2800 if (is_ascii) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002801 if (chars > SeqAsciiString::kMaxLength) {
2802 return Failure::OutOfMemoryException();
2803 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002804 map = ascii_symbol_map();
ager@chromium.org7c537e22008-10-16 08:43:32 +00002805 size = SeqAsciiString::SizeFor(chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 } else {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002807 if (chars > SeqTwoByteString::kMaxLength) {
2808 return Failure::OutOfMemoryException();
2809 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002810 map = symbol_map();
ager@chromium.org7c537e22008-10-16 08:43:32 +00002811 size = SeqTwoByteString::SizeFor(chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812 }
2813
2814 // Allocate string.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002815 Object* result = (size > MaxObjectSizeInPagedSpace())
2816 ? lo_space_->AllocateRaw(size)
2817 : old_data_space_->AllocateRaw(size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818 if (result->IsFailure()) return result;
2819
2820 reinterpret_cast<HeapObject*>(result)->set_map(map);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002821 // Set length and hash fields of the allocated string.
ager@chromium.org870a0b62008-11-04 11:43:05 +00002822 String* answer = String::cast(result);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002823 answer->set_length(chars);
2824 answer->set_hash_field(hash_field);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
ager@chromium.org870a0b62008-11-04 11:43:05 +00002826 ASSERT_EQ(size, answer->Size());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002827
2828 // Fill in the characters.
2829 for (int i = 0; i < chars; i++) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002830 answer->Set(i, buffer->GetNext());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 }
ager@chromium.org870a0b62008-11-04 11:43:05 +00002832 return answer;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002833}
2834
2835
2836Object* Heap::AllocateRawAsciiString(int length, PretenureFlag pretenure) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002837 if (length < 0 || length > SeqAsciiString::kMaxLength) {
2838 return Failure::OutOfMemoryException();
2839 }
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002840
ager@chromium.org7c537e22008-10-16 08:43:32 +00002841 int size = SeqAsciiString::SizeFor(length);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002842 ASSERT(size <= SeqAsciiString::kMaxSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002844 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
2845 AllocationSpace retry_space = OLD_DATA_SPACE;
2846
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002847 if (space == NEW_SPACE) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002848 if (size > kMaxObjectSizeInNewSpace) {
2849 // Allocate in large object space, retry space will be ignored.
2850 space = LO_SPACE;
2851 } else if (size > MaxObjectSizeInPagedSpace()) {
2852 // Allocate in new space, retry in large object space.
2853 retry_space = LO_SPACE;
2854 }
2855 } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) {
2856 space = LO_SPACE;
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002857 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002858 Object* result = AllocateRaw(size, space, retry_space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002859 if (result->IsFailure()) return result;
2860
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861 // Partially initialize the object.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002862 HeapObject::cast(result)->set_map(ascii_string_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002863 String::cast(result)->set_length(length);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002864 String::cast(result)->set_hash_field(String::kEmptyHashField);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002865 ASSERT_EQ(size, HeapObject::cast(result)->Size());
2866 return result;
2867}
2868
2869
2870Object* Heap::AllocateRawTwoByteString(int length, PretenureFlag pretenure) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002871 if (length < 0 || length > SeqTwoByteString::kMaxLength) {
2872 return Failure::OutOfMemoryException();
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002873 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002874 int size = SeqTwoByteString::SizeFor(length);
2875 ASSERT(size <= SeqTwoByteString::kMaxSize);
2876 AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE;
2877 AllocationSpace retry_space = OLD_DATA_SPACE;
2878
2879 if (space == NEW_SPACE) {
2880 if (size > kMaxObjectSizeInNewSpace) {
2881 // Allocate in large object space, retry space will be ignored.
2882 space = LO_SPACE;
2883 } else if (size > MaxObjectSizeInPagedSpace()) {
2884 // Allocate in new space, retry in large object space.
2885 retry_space = LO_SPACE;
2886 }
2887 } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) {
2888 space = LO_SPACE;
2889 }
2890 Object* result = AllocateRaw(size, space, retry_space);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891 if (result->IsFailure()) return result;
2892
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893 // Partially initialize the object.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002894 HeapObject::cast(result)->set_map(string_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895 String::cast(result)->set_length(length);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002896 String::cast(result)->set_hash_field(String::kEmptyHashField);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002897 ASSERT_EQ(size, HeapObject::cast(result)->Size());
2898 return result;
2899}
2900
2901
2902Object* Heap::AllocateEmptyFixedArray() {
2903 int size = FixedArray::SizeFor(0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002904 Object* result = AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905 if (result->IsFailure()) return result;
2906 // Initialize the object.
2907 reinterpret_cast<Array*>(result)->set_map(fixed_array_map());
2908 reinterpret_cast<Array*>(result)->set_length(0);
2909 return result;
2910}
2911
2912
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002913Object* Heap::AllocateRawFixedArray(int length) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002914 if (length < 0 || length > FixedArray::kMaxLength) {
2915 return Failure::OutOfMemoryException();
2916 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002917 // Use the general function if we're forced to always allocate.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002918 if (always_allocate()) return AllocateFixedArray(length, TENURED);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002919 // Allocate the raw data for a fixed array.
2920 int size = FixedArray::SizeFor(length);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002921 return size <= kMaxObjectSizeInNewSpace
2922 ? new_space_.AllocateRaw(size)
2923 : lo_space_->AllocateRawFixedArray(size);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002924}
2925
2926
2927Object* Heap::CopyFixedArray(FixedArray* src) {
2928 int len = src->length();
2929 Object* obj = AllocateRawFixedArray(len);
2930 if (obj->IsFailure()) return obj;
2931 if (Heap::InNewSpace(obj)) {
2932 HeapObject* dst = HeapObject::cast(obj);
2933 CopyBlock(reinterpret_cast<Object**>(dst->address()),
2934 reinterpret_cast<Object**>(src->address()),
2935 FixedArray::SizeFor(len));
2936 return obj;
2937 }
2938 HeapObject::cast(obj)->set_map(src->map());
2939 FixedArray* result = FixedArray::cast(obj);
2940 result->set_length(len);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002941
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002942 // Copy the content
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002943 AssertNoAllocation no_gc;
2944 WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002945 for (int i = 0; i < len; i++) result->set(i, src->get(i), mode);
2946 return result;
2947}
2948
2949
2950Object* Heap::AllocateFixedArray(int length) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00002951 ASSERT(length >= 0);
ager@chromium.org32912102009-01-16 10:38:43 +00002952 if (length == 0) return empty_fixed_array();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002953 Object* result = AllocateRawFixedArray(length);
2954 if (!result->IsFailure()) {
2955 // Initialize header.
2956 reinterpret_cast<Array*>(result)->set_map(fixed_array_map());
2957 FixedArray* array = FixedArray::cast(result);
2958 array->set_length(length);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002959 // Initialize body.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002960 ASSERT(!Heap::InNewSpace(undefined_value()));
2961 MemsetPointer(array->data_start(), undefined_value(), length);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002962 }
2963 return result;
2964}
2965
2966
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967Object* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002968 ASSERT(length >= 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002969 ASSERT(empty_fixed_array()->IsFixedArray());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002970 if (length < 0 || length > FixedArray::kMaxLength) {
2971 return Failure::OutOfMemoryException();
2972 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 if (length == 0) return empty_fixed_array();
2974
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002975 AllocationSpace space =
2976 (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002977 int size = FixedArray::SizeFor(length);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002978 if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) {
2979 // Too big for new space.
2980 space = LO_SPACE;
2981 } else if (space == OLD_POINTER_SPACE &&
2982 size > MaxObjectSizeInPagedSpace()) {
2983 // Too big for old pointer space.
2984 space = LO_SPACE;
2985 }
2986
2987 // Specialize allocation for the space.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002988 Object* result = Failure::OutOfMemoryException();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002989 if (space == NEW_SPACE) {
2990 // We cannot use Heap::AllocateRaw() because it will not properly
2991 // allocate extra remembered set bits if always_allocate() is true and
2992 // new space allocation fails.
2993 result = new_space_.AllocateRaw(size);
2994 if (result->IsFailure() && always_allocate()) {
2995 if (size <= MaxObjectSizeInPagedSpace()) {
2996 result = old_pointer_space_->AllocateRaw(size);
2997 } else {
2998 result = lo_space_->AllocateRawFixedArray(size);
2999 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00003000 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003001 } else if (space == OLD_POINTER_SPACE) {
3002 result = old_pointer_space_->AllocateRaw(size);
3003 } else {
3004 ASSERT(space == LO_SPACE);
3005 result = lo_space_->AllocateRawFixedArray(size);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00003006 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003007 if (result->IsFailure()) return result;
3008
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009 // Initialize the object.
3010 reinterpret_cast<Array*>(result)->set_map(fixed_array_map());
3011 FixedArray* array = FixedArray::cast(result);
3012 array->set_length(length);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003013 ASSERT(!Heap::InNewSpace(undefined_value()));
3014 MemsetPointer(array->data_start(), undefined_value(), length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015 return array;
3016}
3017
3018
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003019Object* Heap::AllocateUninitializedFixedArray(int length) {
3020 if (length == 0) return empty_fixed_array();
3021
3022 Object* obj = AllocateRawFixedArray(length);
3023 if (obj->IsFailure()) return obj;
3024
3025 reinterpret_cast<FixedArray*>(obj)->set_map(fixed_array_map());
3026 FixedArray::cast(obj)->set_length(length);
3027 return obj;
3028}
3029
3030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003031Object* Heap::AllocateFixedArrayWithHoles(int length) {
3032 if (length == 0) return empty_fixed_array();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003033 Object* result = AllocateRawFixedArray(length);
3034 if (!result->IsFailure()) {
3035 // Initialize header.
3036 reinterpret_cast<Array*>(result)->set_map(fixed_array_map());
3037 FixedArray* array = FixedArray::cast(result);
3038 array->set_length(length);
3039 // Initialize body.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003040 ASSERT(!Heap::InNewSpace(the_hole_value()));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003041 MemsetPointer(array->data_start(), the_hole_value(), length);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003042 }
3043 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003044}
3045
3046
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003047Object* Heap::AllocateHashTable(int length, PretenureFlag pretenure) {
3048 Object* result = Heap::AllocateFixedArray(length, pretenure);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003049 if (result->IsFailure()) return result;
3050 reinterpret_cast<Array*>(result)->set_map(hash_table_map());
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003051 ASSERT(result->IsHashTable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052 return result;
3053}
3054
3055
3056Object* Heap::AllocateGlobalContext() {
3057 Object* result = Heap::AllocateFixedArray(Context::GLOBAL_CONTEXT_SLOTS);
3058 if (result->IsFailure()) return result;
3059 Context* context = reinterpret_cast<Context*>(result);
3060 context->set_map(global_context_map());
3061 ASSERT(context->IsGlobalContext());
3062 ASSERT(result->IsContext());
3063 return result;
3064}
3065
3066
3067Object* Heap::AllocateFunctionContext(int length, JSFunction* function) {
3068 ASSERT(length >= Context::MIN_CONTEXT_SLOTS);
3069 Object* result = Heap::AllocateFixedArray(length);
3070 if (result->IsFailure()) return result;
3071 Context* context = reinterpret_cast<Context*>(result);
3072 context->set_map(context_map());
3073 context->set_closure(function);
3074 context->set_fcontext(context);
3075 context->set_previous(NULL);
3076 context->set_extension(NULL);
3077 context->set_global(function->context()->global());
3078 ASSERT(!context->IsGlobalContext());
3079 ASSERT(context->is_function_context());
3080 ASSERT(result->IsContext());
3081 return result;
3082}
3083
3084
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003085Object* Heap::AllocateWithContext(Context* previous,
3086 JSObject* extension,
3087 bool is_catch_context) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 Object* result = Heap::AllocateFixedArray(Context::MIN_CONTEXT_SLOTS);
3089 if (result->IsFailure()) return result;
3090 Context* context = reinterpret_cast<Context*>(result);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003091 context->set_map(is_catch_context ? catch_context_map() : context_map());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003092 context->set_closure(previous->closure());
3093 context->set_fcontext(previous->fcontext());
3094 context->set_previous(previous);
3095 context->set_extension(extension);
3096 context->set_global(previous->global());
3097 ASSERT(!context->IsGlobalContext());
3098 ASSERT(!context->is_function_context());
3099 ASSERT(result->IsContext());
3100 return result;
3101}
3102
3103
3104Object* Heap::AllocateStruct(InstanceType type) {
3105 Map* map;
3106 switch (type) {
3107#define MAKE_CASE(NAME, Name, name) case NAME##_TYPE: map = name##_map(); break;
3108STRUCT_LIST(MAKE_CASE)
3109#undef MAKE_CASE
3110 default:
3111 UNREACHABLE();
3112 return Failure::InternalError();
3113 }
3114 int size = map->instance_size();
3115 AllocationSpace space =
ager@chromium.org5aa501c2009-06-23 07:57:28 +00003116 (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : OLD_POINTER_SPACE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117 Object* result = Heap::Allocate(map, space);
3118 if (result->IsFailure()) return result;
3119 Struct::cast(result)->InitializeBody(size);
3120 return result;
3121}
3122
3123
ager@chromium.org96c75b52009-08-26 09:13:16 +00003124bool Heap::IdleNotification() {
ager@chromium.orga1645e22009-09-09 19:27:10 +00003125 static const int kIdlesBeforeScavenge = 4;
3126 static const int kIdlesBeforeMarkSweep = 7;
3127 static const int kIdlesBeforeMarkCompact = 8;
ager@chromium.org96c75b52009-08-26 09:13:16 +00003128 static int number_idle_notifications = 0;
3129 static int last_gc_count = gc_count_;
3130
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003131 bool uncommit = true;
ager@chromium.org96c75b52009-08-26 09:13:16 +00003132 bool finished = false;
3133
3134 if (last_gc_count == gc_count_) {
3135 number_idle_notifications++;
3136 } else {
3137 number_idle_notifications = 0;
3138 last_gc_count = gc_count_;
3139 }
3140
ager@chromium.orga1645e22009-09-09 19:27:10 +00003141 if (number_idle_notifications == kIdlesBeforeScavenge) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003142 if (contexts_disposed_ > 0) {
3143 HistogramTimerScope scope(&Counters::gc_context);
3144 CollectAllGarbage(false);
3145 } else {
3146 CollectGarbage(0, NEW_SPACE);
3147 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00003148 new_space_.Shrink();
ager@chromium.org96c75b52009-08-26 09:13:16 +00003149 last_gc_count = gc_count_;
ager@chromium.orga1645e22009-09-09 19:27:10 +00003150
3151 } else if (number_idle_notifications == kIdlesBeforeMarkSweep) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003152 // Before doing the mark-sweep collections we clear the
3153 // compilation cache to avoid hanging on to source code and
3154 // generated code for cached functions.
3155 CompilationCache::Clear();
3156
ager@chromium.orga1645e22009-09-09 19:27:10 +00003157 CollectAllGarbage(false);
3158 new_space_.Shrink();
3159 last_gc_count = gc_count_;
3160
3161 } else if (number_idle_notifications == kIdlesBeforeMarkCompact) {
3162 CollectAllGarbage(true);
3163 new_space_.Shrink();
3164 last_gc_count = gc_count_;
3165 number_idle_notifications = 0;
3166 finished = true;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003167
3168 } else if (contexts_disposed_ > 0) {
3169 if (FLAG_expose_gc) {
3170 contexts_disposed_ = 0;
3171 } else {
3172 HistogramTimerScope scope(&Counters::gc_context);
3173 CollectAllGarbage(false);
3174 last_gc_count = gc_count_;
3175 }
3176 // If this is the first idle notification, we reset the
3177 // notification count to avoid letting idle notifications for
3178 // context disposal garbage collections start a potentially too
3179 // aggressive idle GC cycle.
3180 if (number_idle_notifications <= 1) {
3181 number_idle_notifications = 0;
3182 uncommit = false;
3183 }
ager@chromium.org96c75b52009-08-26 09:13:16 +00003184 }
3185
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003186 // Make sure that we have no pending context disposals and
3187 // conditionally uncommit from space.
3188 ASSERT(contexts_disposed_ == 0);
3189 if (uncommit) Heap::UncommitFromSpace();
ager@chromium.org96c75b52009-08-26 09:13:16 +00003190 return finished;
3191}
3192
3193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003194#ifdef DEBUG
3195
3196void Heap::Print() {
3197 if (!HasBeenSetup()) return;
3198 Top::PrintStack();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003199 AllSpaces spaces;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003200 for (Space* space = spaces.next(); space != NULL; space = spaces.next())
3201 space->Print();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003202}
3203
3204
3205void Heap::ReportCodeStatistics(const char* title) {
3206 PrintF(">>>>>> Code Stats (%s) >>>>>>\n", title);
3207 PagedSpace::ResetCodeStatistics();
3208 // We do not look for code in new space, map space, or old space. If code
3209 // somehow ends up in those spaces, we would miss it here.
3210 code_space_->CollectCodeStatistics();
3211 lo_space_->CollectCodeStatistics();
3212 PagedSpace::ReportCodeStatistics();
3213}
3214
3215
3216// This function expects that NewSpace's allocated objects histogram is
3217// populated (via a call to CollectStatistics or else as a side effect of a
3218// just-completed scavenge collection).
3219void Heap::ReportHeapStatistics(const char* title) {
3220 USE(title);
3221 PrintF(">>>>>> =============== %s (%d) =============== >>>>>>\n",
3222 title, gc_count_);
3223 PrintF("mark-compact GC : %d\n", mc_count_);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00003224 PrintF("old_gen_promotion_limit_ %d\n", old_gen_promotion_limit_);
3225 PrintF("old_gen_allocation_limit_ %d\n", old_gen_allocation_limit_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226
3227 PrintF("\n");
3228 PrintF("Number of handles : %d\n", HandleScope::NumberOfHandles());
3229 GlobalHandles::PrintStats();
3230 PrintF("\n");
3231
3232 PrintF("Heap statistics : ");
3233 MemoryAllocator::ReportStatistics();
3234 PrintF("To space : ");
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003235 new_space_.ReportStatistics();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003236 PrintF("Old pointer space : ");
3237 old_pointer_space_->ReportStatistics();
3238 PrintF("Old data space : ");
3239 old_data_space_->ReportStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003240 PrintF("Code space : ");
3241 code_space_->ReportStatistics();
3242 PrintF("Map space : ");
3243 map_space_->ReportStatistics();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003244 PrintF("Cell space : ");
3245 cell_space_->ReportStatistics();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003246 PrintF("Large object space : ");
3247 lo_space_->ReportStatistics();
3248 PrintF(">>>>>> ========================================= >>>>>>\n");
3249}
3250
3251#endif // DEBUG
3252
3253bool Heap::Contains(HeapObject* value) {
3254 return Contains(value->address());
3255}
3256
3257
3258bool Heap::Contains(Address addr) {
3259 if (OS::IsOutsideAllocatedSpace(addr)) return false;
3260 return HasBeenSetup() &&
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003261 (new_space_.ToSpaceContains(addr) ||
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003262 old_pointer_space_->Contains(addr) ||
3263 old_data_space_->Contains(addr) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003264 code_space_->Contains(addr) ||
3265 map_space_->Contains(addr) ||
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003266 cell_space_->Contains(addr) ||
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003267 lo_space_->SlowContains(addr));
3268}
3269
3270
3271bool Heap::InSpace(HeapObject* value, AllocationSpace space) {
3272 return InSpace(value->address(), space);
3273}
3274
3275
3276bool Heap::InSpace(Address addr, AllocationSpace space) {
3277 if (OS::IsOutsideAllocatedSpace(addr)) return false;
3278 if (!HasBeenSetup()) return false;
3279
3280 switch (space) {
3281 case NEW_SPACE:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003282 return new_space_.ToSpaceContains(addr);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003283 case OLD_POINTER_SPACE:
3284 return old_pointer_space_->Contains(addr);
3285 case OLD_DATA_SPACE:
3286 return old_data_space_->Contains(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287 case CODE_SPACE:
3288 return code_space_->Contains(addr);
3289 case MAP_SPACE:
3290 return map_space_->Contains(addr);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003291 case CELL_SPACE:
3292 return cell_space_->Contains(addr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293 case LO_SPACE:
3294 return lo_space_->SlowContains(addr);
3295 }
3296
3297 return false;
3298}
3299
3300
3301#ifdef DEBUG
3302void Heap::Verify() {
3303 ASSERT(HasBeenSetup());
3304
3305 VerifyPointersVisitor visitor;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003306 IterateRoots(&visitor, VISIT_ONLY_STRONG);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003308 new_space_.Verify();
3309
3310 VerifyPointersAndRSetVisitor rset_visitor;
3311 old_pointer_space_->Verify(&rset_visitor);
3312 map_space_->Verify(&rset_visitor);
3313
3314 VerifyPointersVisitor no_rset_visitor;
3315 old_data_space_->Verify(&no_rset_visitor);
3316 code_space_->Verify(&no_rset_visitor);
3317 cell_space_->Verify(&no_rset_visitor);
3318
3319 lo_space_->Verify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003320}
3321#endif // DEBUG
3322
3323
3324Object* Heap::LookupSymbol(Vector<const char> string) {
3325 Object* symbol = NULL;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003326 Object* new_table = symbol_table()->LookupSymbol(string, &symbol);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003327 if (new_table->IsFailure()) return new_table;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003328 // Can't use set_symbol_table because SymbolTable::cast knows that
3329 // SymbolTable is a singleton and checks for identity.
3330 roots_[kSymbolTableRootIndex] = new_table;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003331 ASSERT(symbol != NULL);
3332 return symbol;
3333}
3334
3335
3336Object* Heap::LookupSymbol(String* string) {
3337 if (string->IsSymbol()) return string;
3338 Object* symbol = NULL;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003339 Object* new_table = symbol_table()->LookupString(string, &symbol);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003340 if (new_table->IsFailure()) return new_table;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003341 // Can't use set_symbol_table because SymbolTable::cast knows that
3342 // SymbolTable is a singleton and checks for identity.
3343 roots_[kSymbolTableRootIndex] = new_table;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003344 ASSERT(symbol != NULL);
3345 return symbol;
3346}
3347
3348
ager@chromium.org7c537e22008-10-16 08:43:32 +00003349bool Heap::LookupSymbolIfExists(String* string, String** symbol) {
3350 if (string->IsSymbol()) {
3351 *symbol = string;
3352 return true;
3353 }
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003354 return symbol_table()->LookupSymbolIfExists(string, symbol);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003355}
3356
3357
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003358#ifdef DEBUG
3359void Heap::ZapFromSpace() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003360 ASSERT(reinterpret_cast<Object*>(kFromSpaceZapValue)->IsHeapObject());
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003361 for (Address a = new_space_.FromSpaceLow();
3362 a < new_space_.FromSpaceHigh();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003363 a += kPointerSize) {
3364 Memory::Address_at(a) = kFromSpaceZapValue;
3365 }
3366}
3367#endif // DEBUG
3368
3369
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003370int Heap::IterateRSetRange(Address object_start,
3371 Address object_end,
3372 Address rset_start,
3373 ObjectSlotCallback copy_object_func) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003374 Address object_address = object_start;
3375 Address rset_address = rset_start;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003376 int set_bits_count = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003377
3378 // Loop over all the pointers in [object_start, object_end).
3379 while (object_address < object_end) {
3380 uint32_t rset_word = Memory::uint32_at(rset_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003381 if (rset_word != 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382 uint32_t result_rset = rset_word;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003383 for (uint32_t bitmask = 1; bitmask != 0; bitmask = bitmask << 1) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003384 // Do not dereference pointers at or past object_end.
3385 if ((rset_word & bitmask) != 0 && object_address < object_end) {
3386 Object** object_p = reinterpret_cast<Object**>(object_address);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003387 if (Heap::InNewSpace(*object_p)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003388 copy_object_func(reinterpret_cast<HeapObject**>(object_p));
3389 }
3390 // If this pointer does not need to be remembered anymore, clear
3391 // the remembered set bit.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003392 if (!Heap::InNewSpace(*object_p)) result_rset &= ~bitmask;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003393 set_bits_count++;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 }
3395 object_address += kPointerSize;
3396 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003397 // Update the remembered set if it has changed.
3398 if (result_rset != rset_word) {
3399 Memory::uint32_at(rset_address) = result_rset;
3400 }
3401 } else {
3402 // No bits in the word were set. This is the common case.
3403 object_address += kPointerSize * kBitsPerInt;
3404 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 rset_address += kIntSize;
3406 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003407 return set_bits_count;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003408}
3409
3410
3411void Heap::IterateRSet(PagedSpace* space, ObjectSlotCallback copy_object_func) {
3412 ASSERT(Page::is_rset_in_use());
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003413 ASSERT(space == old_pointer_space_ || space == map_space_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003414
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003415 static void* paged_rset_histogram = StatsTable::CreateHistogram(
3416 "V8.RSetPaged",
3417 0,
3418 Page::kObjectAreaSize / kPointerSize,
3419 30);
3420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003421 PageIterator it(space, PageIterator::PAGES_IN_USE);
3422 while (it.has_next()) {
3423 Page* page = it.next();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003424 int count = IterateRSetRange(page->ObjectAreaStart(), page->AllocationTop(),
3425 page->RSetStart(), copy_object_func);
3426 if (paged_rset_histogram != NULL) {
3427 StatsTable::AddHistogramSample(paged_rset_histogram, count);
3428 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003429 }
3430}
3431
3432
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003433void Heap::IterateRoots(ObjectVisitor* v, VisitMode mode) {
3434 IterateStrongRoots(v, mode);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003435 IterateWeakRoots(v, mode);
3436}
3437
3438
3439void Heap::IterateWeakRoots(ObjectVisitor* v, VisitMode mode) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003440 v->VisitPointer(reinterpret_cast<Object**>(&roots_[kSymbolTableRootIndex]));
ager@chromium.org3811b432009-10-28 14:53:37 +00003441 v->Synchronize("symbol_table");
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003442 if (mode != VISIT_ALL_IN_SCAVENGE) {
3443 // Scavenge collections have special processing for this.
3444 ExternalStringTable::Iterate(v);
3445 }
3446 v->Synchronize("external_string_table");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003447}
3448
3449
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003450void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) {
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003451 v->VisitPointers(&roots_[0], &roots_[kStrongRootListLength]);
ager@chromium.org3811b432009-10-28 14:53:37 +00003452 v->Synchronize("strong_root_list");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003453
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003454 v->VisitPointer(BitCast<Object**, String**>(&hidden_symbol_));
ager@chromium.org3811b432009-10-28 14:53:37 +00003455 v->Synchronize("symbol");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003456
3457 Bootstrapper::Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +00003458 v->Synchronize("bootstrapper");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459 Top::Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +00003460 v->Synchronize("top");
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003461 Relocatable::Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +00003462 v->Synchronize("relocatable");
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003463
3464#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003465 Debug::Iterate(v);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003466#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003467 v->Synchronize("debug");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003468 CompilationCache::Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +00003469 v->Synchronize("compilationcache");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003470
3471 // Iterate over local handles in handle scopes.
3472 HandleScopeImplementer::Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +00003473 v->Synchronize("handlescope");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003474
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003475 // Iterate over the builtin code objects and code stubs in the
3476 // heap. Note that it is not necessary to iterate over code objects
3477 // on scavenge collections.
3478 if (mode != VISIT_ALL_IN_SCAVENGE) {
3479 Builtins::IterateBuiltins(v);
3480 }
ager@chromium.org3811b432009-10-28 14:53:37 +00003481 v->Synchronize("builtins");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003482
3483 // Iterate over global handles.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003484 if (mode == VISIT_ONLY_STRONG) {
3485 GlobalHandles::IterateStrongRoots(v);
3486 } else {
3487 GlobalHandles::IterateAllRoots(v);
3488 }
ager@chromium.org3811b432009-10-28 14:53:37 +00003489 v->Synchronize("globalhandles");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003490
3491 // Iterate over pointers being held by inactive threads.
3492 ThreadManager::Iterate(v);
ager@chromium.org3811b432009-10-28 14:53:37 +00003493 v->Synchronize("threadmanager");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003494
3495 // Iterate over the pointers the Serialization/Deserialization code is
3496 // holding.
3497 // During garbage collection this keeps the partial snapshot cache alive.
3498 // During deserialization of the startup snapshot this creates the partial
3499 // snapshot cache and deserializes the objects it refers to. During
3500 // serialization this does nothing, since the partial snapshot cache is
3501 // empty. However the next thing we do is create the partial snapshot,
3502 // filling up the partial snapshot cache with objects it needs as we go.
3503 SerializerDeserializer::Iterate(v);
3504 // We don't do a v->Synchronize call here, because in debug mode that will
3505 // output a flag to the snapshot. However at this point the serializer and
3506 // deserializer are deliberately a little unsynchronized (see above) so the
3507 // checking of the sync flag in the snapshot would fail.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509
3510
3511// Flag is set when the heap has been configured. The heap can be repeatedly
3512// configured through the API until it is setup.
3513static bool heap_configured = false;
3514
3515// TODO(1236194): Since the heap size is configurable on the command line
3516// and through the API, we should gracefully handle the case that the heap
3517// size is not big enough to fit all the initial objects.
ager@chromium.org3811b432009-10-28 14:53:37 +00003518bool Heap::ConfigureHeap(int max_semispace_size, int max_old_gen_size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003519 if (HasBeenSetup()) return false;
3520
ager@chromium.org3811b432009-10-28 14:53:37 +00003521 if (max_semispace_size > 0) max_semispace_size_ = max_semispace_size;
3522
3523 if (Snapshot::IsEnabled()) {
3524 // If we are using a snapshot we always reserve the default amount
3525 // of memory for each semispace because code in the snapshot has
3526 // write-barrier code that relies on the size and alignment of new
3527 // space. We therefore cannot use a larger max semispace size
3528 // than the default reserved semispace size.
3529 if (max_semispace_size_ > reserved_semispace_size_) {
3530 max_semispace_size_ = reserved_semispace_size_;
3531 }
3532 } else {
3533 // If we are not using snapshots we reserve space for the actual
3534 // max semispace size.
3535 reserved_semispace_size_ = max_semispace_size_;
3536 }
3537
3538 if (max_old_gen_size > 0) max_old_generation_size_ = max_old_gen_size;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539
3540 // The new space size must be a power of two to support single-bit testing
3541 // for containment.
ager@chromium.org3811b432009-10-28 14:53:37 +00003542 max_semispace_size_ = RoundUpToPowerOf2(max_semispace_size_);
3543 reserved_semispace_size_ = RoundUpToPowerOf2(reserved_semispace_size_);
3544 initial_semispace_size_ = Min(initial_semispace_size_, max_semispace_size_);
3545 external_allocation_limit_ = 10 * max_semispace_size_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546
3547 // The old generation is paged.
ager@chromium.org3811b432009-10-28 14:53:37 +00003548 max_old_generation_size_ = RoundUp(max_old_generation_size_, Page::kPageSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003549
3550 heap_configured = true;
3551 return true;
3552}
3553
3554
kasper.lund7276f142008-07-30 08:49:36 +00003555bool Heap::ConfigureHeapDefault() {
ager@chromium.org3811b432009-10-28 14:53:37 +00003556 return ConfigureHeap(FLAG_max_new_space_size / 2, FLAG_max_old_space_size);
kasper.lund7276f142008-07-30 08:49:36 +00003557}
3558
3559
ager@chromium.org60121232009-12-03 11:25:37 +00003560void Heap::RecordStats(HeapStats* stats) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003561 *stats->start_marker = 0xDECADE00;
3562 *stats->end_marker = 0xDECADE01;
3563 *stats->new_space_size = new_space_.Size();
3564 *stats->new_space_capacity = new_space_.Capacity();
3565 *stats->old_pointer_space_size = old_pointer_space_->Size();
3566 *stats->old_pointer_space_capacity = old_pointer_space_->Capacity();
3567 *stats->old_data_space_size = old_data_space_->Size();
3568 *stats->old_data_space_capacity = old_data_space_->Capacity();
3569 *stats->code_space_size = code_space_->Size();
3570 *stats->code_space_capacity = code_space_->Capacity();
3571 *stats->map_space_size = map_space_->Size();
3572 *stats->map_space_capacity = map_space_->Capacity();
3573 *stats->cell_space_size = cell_space_->Size();
3574 *stats->cell_space_capacity = cell_space_->Capacity();
3575 *stats->lo_space_size = lo_space_->Size();
ager@chromium.org60121232009-12-03 11:25:37 +00003576 GlobalHandles::RecordStats(stats);
3577}
3578
3579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003580int Heap::PromotedSpaceSize() {
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003581 return old_pointer_space_->Size()
3582 + old_data_space_->Size()
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583 + code_space_->Size()
3584 + map_space_->Size()
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003585 + cell_space_->Size()
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586 + lo_space_->Size();
3587}
3588
3589
kasper.lund7276f142008-07-30 08:49:36 +00003590int Heap::PromotedExternalMemorySize() {
3591 if (amount_of_external_allocated_memory_
3592 <= amount_of_external_allocated_memory_at_last_global_gc_) return 0;
3593 return amount_of_external_allocated_memory_
3594 - amount_of_external_allocated_memory_at_last_global_gc_;
3595}
3596
3597
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003598bool Heap::Setup(bool create_heap_objects) {
3599 // Initialize heap spaces and initial maps and objects. Whenever something
3600 // goes wrong, just return false. The caller should check the results and
3601 // call Heap::TearDown() to release allocated memory.
3602 //
3603 // If the heap is not yet configured (eg, through the API), configure it.
3604 // Configuration is based on the flags new-space-size (really the semispace
3605 // size) and old-space-size if set or the initial values of semispace_size_
3606 // and old_generation_size_ otherwise.
3607 if (!heap_configured) {
kasper.lund7276f142008-07-30 08:49:36 +00003608 if (!ConfigureHeapDefault()) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003609 }
3610
ager@chromium.orga1645e22009-09-09 19:27:10 +00003611 // Setup memory allocator and reserve a chunk of memory for new
ager@chromium.org3811b432009-10-28 14:53:37 +00003612 // space. The chunk is double the size of the requested reserved
3613 // new space size to ensure that we can find a pair of semispaces that
3614 // are contiguous and aligned to their size.
3615 if (!MemoryAllocator::Setup(MaxReserved())) return false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00003616 void* chunk =
ager@chromium.org3811b432009-10-28 14:53:37 +00003617 MemoryAllocator::ReserveInitialChunk(4 * reserved_semispace_size_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003618 if (chunk == NULL) return false;
3619
ager@chromium.orga1645e22009-09-09 19:27:10 +00003620 // Align the pair of semispaces to their size, which must be a power
3621 // of 2.
ager@chromium.orga1645e22009-09-09 19:27:10 +00003622 Address new_space_start =
ager@chromium.org3811b432009-10-28 14:53:37 +00003623 RoundUp(reinterpret_cast<byte*>(chunk), 2 * reserved_semispace_size_);
3624 if (!new_space_.Setup(new_space_start, 2 * reserved_semispace_size_)) {
3625 return false;
3626 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003627
ager@chromium.orga1645e22009-09-09 19:27:10 +00003628 // Initialize old pointer space.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003629 old_pointer_space_ =
ager@chromium.org3811b432009-10-28 14:53:37 +00003630 new OldSpace(max_old_generation_size_, OLD_POINTER_SPACE, NOT_EXECUTABLE);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003631 if (old_pointer_space_ == NULL) return false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00003632 if (!old_pointer_space_->Setup(NULL, 0)) return false;
3633
3634 // Initialize old data space.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003635 old_data_space_ =
ager@chromium.org3811b432009-10-28 14:53:37 +00003636 new OldSpace(max_old_generation_size_, OLD_DATA_SPACE, NOT_EXECUTABLE);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003637 if (old_data_space_ == NULL) return false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00003638 if (!old_data_space_->Setup(NULL, 0)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639
3640 // Initialize the code space, set its maximum capacity to the old
kasper.lund7276f142008-07-30 08:49:36 +00003641 // generation size. It needs executable memory.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003642 // On 64-bit platform(s), we put all code objects in a 2 GB range of
3643 // virtual address space, so that they can call each other with near calls.
3644 if (code_range_size_ > 0) {
3645 if (!CodeRange::Setup(code_range_size_)) {
3646 return false;
3647 }
3648 }
3649
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003650 code_space_ =
ager@chromium.org3811b432009-10-28 14:53:37 +00003651 new OldSpace(max_old_generation_size_, CODE_SPACE, EXECUTABLE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003652 if (code_space_ == NULL) return false;
ager@chromium.orga1645e22009-09-09 19:27:10 +00003653 if (!code_space_->Setup(NULL, 0)) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654
3655 // Initialize map space.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003656 map_space_ = new MapSpace(FLAG_use_big_map_space
3657 ? max_old_generation_size_
kasperl@chromium.orgeac059f2010-01-25 11:02:06 +00003658 : MapSpace::kMaxMapPageIndex * Page::kPageSize,
3659 FLAG_max_map_space_pages,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003660 MAP_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003661 if (map_space_ == NULL) return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003662 if (!map_space_->Setup(NULL, 0)) return false;
3663
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003664 // Initialize global property cell space.
ager@chromium.org3811b432009-10-28 14:53:37 +00003665 cell_space_ = new CellSpace(max_old_generation_size_, CELL_SPACE);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003666 if (cell_space_ == NULL) return false;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003667 if (!cell_space_->Setup(NULL, 0)) return false;
3668
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003669 // The large object code space may contain code or data. We set the memory
3670 // to be non-executable here for safety, but this means we need to enable it
3671 // explicitly when allocating large code objects.
3672 lo_space_ = new LargeObjectSpace(LO_SPACE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003673 if (lo_space_ == NULL) return false;
3674 if (!lo_space_->Setup()) return false;
3675
3676 if (create_heap_objects) {
3677 // Create initial maps.
3678 if (!CreateInitialMaps()) return false;
3679 if (!CreateApiObjects()) return false;
3680
3681 // Create initial objects
3682 if (!CreateInitialObjects()) return false;
3683 }
3684
3685 LOG(IntEvent("heap-capacity", Capacity()));
3686 LOG(IntEvent("heap-available", Available()));
3687
ager@chromium.org3811b432009-10-28 14:53:37 +00003688#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003689 // This should be called only after initial objects have been created.
3690 ProducerHeapProfile::Setup();
ager@chromium.org3811b432009-10-28 14:53:37 +00003691#endif
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00003692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003693 return true;
3694}
3695
3696
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003697void Heap::SetStackLimits() {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003698 // On 64 bit machines, pointers are generally out of range of Smis. We write
3699 // something that looks like an out of range Smi to the GC.
3700
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003701 // Set up the special root array entries containing the stack limits.
3702 // These are actually addresses, but the tag makes the GC ignore it.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003703 roots_[kStackLimitRootIndex] =
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003704 reinterpret_cast<Object*>(
3705 (StackGuard::jslimit() & ~kSmiTagMask) | kSmiTag);
3706 roots_[kRealStackLimitRootIndex] =
3707 reinterpret_cast<Object*>(
3708 (StackGuard::real_jslimit() & ~kSmiTagMask) | kSmiTag);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003709}
3710
3711
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712void Heap::TearDown() {
3713 GlobalHandles::TearDown();
3714
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003715 ExternalStringTable::TearDown();
3716
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003717 new_space_.TearDown();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003718
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003719 if (old_pointer_space_ != NULL) {
3720 old_pointer_space_->TearDown();
3721 delete old_pointer_space_;
3722 old_pointer_space_ = NULL;
3723 }
3724
3725 if (old_data_space_ != NULL) {
3726 old_data_space_->TearDown();
3727 delete old_data_space_;
3728 old_data_space_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003729 }
3730
3731 if (code_space_ != NULL) {
3732 code_space_->TearDown();
3733 delete code_space_;
3734 code_space_ = NULL;
3735 }
3736
3737 if (map_space_ != NULL) {
3738 map_space_->TearDown();
3739 delete map_space_;
3740 map_space_ = NULL;
3741 }
3742
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003743 if (cell_space_ != NULL) {
3744 cell_space_->TearDown();
3745 delete cell_space_;
3746 cell_space_ = NULL;
3747 }
3748
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003749 if (lo_space_ != NULL) {
3750 lo_space_->TearDown();
3751 delete lo_space_;
3752 lo_space_ = NULL;
3753 }
3754
3755 MemoryAllocator::TearDown();
3756}
3757
3758
3759void Heap::Shrink() {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003760 // Try to shrink all paged spaces.
3761 PagedSpaces spaces;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003762 for (PagedSpace* space = spaces.next(); space != NULL; space = spaces.next())
3763 space->Shrink();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764}
3765
3766
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00003767#ifdef ENABLE_HEAP_PROTECTION
3768
3769void Heap::Protect() {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003770 if (HasBeenSetup()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003771 AllSpaces spaces;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003772 for (Space* space = spaces.next(); space != NULL; space = spaces.next())
3773 space->Protect();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003774 }
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00003775}
3776
3777
3778void Heap::Unprotect() {
ager@chromium.org71daaf62009-04-01 07:22:49 +00003779 if (HasBeenSetup()) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003780 AllSpaces spaces;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003781 for (Space* space = spaces.next(); space != NULL; space = spaces.next())
3782 space->Unprotect();
ager@chromium.org71daaf62009-04-01 07:22:49 +00003783 }
kasperl@chromium.orgf5aa8372009-03-24 14:47:14 +00003784}
3785
3786#endif
3787
3788
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003789#ifdef DEBUG
3790
3791class PrintHandleVisitor: public ObjectVisitor {
3792 public:
3793 void VisitPointers(Object** start, Object** end) {
3794 for (Object** p = start; p < end; p++)
3795 PrintF(" handle %p to %p\n", p, *p);
3796 }
3797};
3798
3799void Heap::PrintHandles() {
3800 PrintF("Handles:\n");
3801 PrintHandleVisitor v;
3802 HandleScopeImplementer::Iterate(&v);
3803}
3804
3805#endif
3806
3807
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003808Space* AllSpaces::next() {
3809 switch (counter_++) {
3810 case NEW_SPACE:
3811 return Heap::new_space();
3812 case OLD_POINTER_SPACE:
3813 return Heap::old_pointer_space();
3814 case OLD_DATA_SPACE:
3815 return Heap::old_data_space();
3816 case CODE_SPACE:
3817 return Heap::code_space();
3818 case MAP_SPACE:
3819 return Heap::map_space();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003820 case CELL_SPACE:
3821 return Heap::cell_space();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003822 case LO_SPACE:
3823 return Heap::lo_space();
3824 default:
3825 return NULL;
3826 }
3827}
3828
3829
3830PagedSpace* PagedSpaces::next() {
3831 switch (counter_++) {
3832 case OLD_POINTER_SPACE:
3833 return Heap::old_pointer_space();
3834 case OLD_DATA_SPACE:
3835 return Heap::old_data_space();
3836 case CODE_SPACE:
3837 return Heap::code_space();
3838 case MAP_SPACE:
3839 return Heap::map_space();
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003840 case CELL_SPACE:
3841 return Heap::cell_space();
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003842 default:
3843 return NULL;
3844 }
3845}
3846
3847
3848
3849OldSpace* OldSpaces::next() {
3850 switch (counter_++) {
3851 case OLD_POINTER_SPACE:
3852 return Heap::old_pointer_space();
3853 case OLD_DATA_SPACE:
3854 return Heap::old_data_space();
3855 case CODE_SPACE:
3856 return Heap::code_space();
3857 default:
3858 return NULL;
3859 }
3860}
3861
3862
kasper.lund7276f142008-07-30 08:49:36 +00003863SpaceIterator::SpaceIterator() : current_space_(FIRST_SPACE), iterator_(NULL) {
3864}
3865
3866
3867SpaceIterator::~SpaceIterator() {
3868 // Delete active iterator if any.
3869 delete iterator_;
3870}
3871
3872
3873bool SpaceIterator::has_next() {
3874 // Iterate until no more spaces.
3875 return current_space_ != LAST_SPACE;
3876}
3877
3878
3879ObjectIterator* SpaceIterator::next() {
3880 if (iterator_ != NULL) {
3881 delete iterator_;
3882 iterator_ = NULL;
3883 // Move to the next space
3884 current_space_++;
3885 if (current_space_ > LAST_SPACE) {
3886 return NULL;
3887 }
3888 }
3889
3890 // Return iterator for the new current space.
3891 return CreateIterator();
3892}
3893
3894
3895// Create an iterator for the space to iterate.
3896ObjectIterator* SpaceIterator::CreateIterator() {
3897 ASSERT(iterator_ == NULL);
3898
3899 switch (current_space_) {
3900 case NEW_SPACE:
3901 iterator_ = new SemiSpaceIterator(Heap::new_space());
3902 break;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003903 case OLD_POINTER_SPACE:
3904 iterator_ = new HeapObjectIterator(Heap::old_pointer_space());
3905 break;
3906 case OLD_DATA_SPACE:
3907 iterator_ = new HeapObjectIterator(Heap::old_data_space());
kasper.lund7276f142008-07-30 08:49:36 +00003908 break;
3909 case CODE_SPACE:
3910 iterator_ = new HeapObjectIterator(Heap::code_space());
3911 break;
3912 case MAP_SPACE:
3913 iterator_ = new HeapObjectIterator(Heap::map_space());
3914 break;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00003915 case CELL_SPACE:
3916 iterator_ = new HeapObjectIterator(Heap::cell_space());
3917 break;
kasper.lund7276f142008-07-30 08:49:36 +00003918 case LO_SPACE:
3919 iterator_ = new LargeObjectIterator(Heap::lo_space());
3920 break;
3921 }
3922
3923 // Return the newly allocated iterator;
3924 ASSERT(iterator_ != NULL);
3925 return iterator_;
3926}
3927
3928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929HeapIterator::HeapIterator() {
3930 Init();
3931}
3932
3933
3934HeapIterator::~HeapIterator() {
3935 Shutdown();
3936}
3937
3938
3939void HeapIterator::Init() {
3940 // Start the iteration.
3941 space_iterator_ = new SpaceIterator();
3942 object_iterator_ = space_iterator_->next();
3943}
3944
3945
3946void HeapIterator::Shutdown() {
3947 // Make sure the last iterator is deallocated.
3948 delete space_iterator_;
3949 space_iterator_ = NULL;
3950 object_iterator_ = NULL;
3951}
3952
3953
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003954HeapObject* HeapIterator::next() {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003955 // No iterator means we are done.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003956 if (object_iterator_ == NULL) return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003958 if (HeapObject* obj = object_iterator_->next_object()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003959 // If the current iterator has more objects we are fine.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003960 return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003961 } else {
3962 // Go though the spaces looking for one that has objects.
3963 while (space_iterator_->has_next()) {
3964 object_iterator_ = space_iterator_->next();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003965 if (HeapObject* obj = object_iterator_->next_object()) {
3966 return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003967 }
3968 }
3969 }
3970 // Done with the last space.
3971 object_iterator_ = NULL;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003972 return NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003973}
3974
3975
3976void HeapIterator::reset() {
3977 // Restart the iterator.
3978 Shutdown();
3979 Init();
3980}
3981
3982
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003983#ifdef DEBUG
3984
3985static bool search_for_any_global;
3986static Object* search_target;
3987static bool found_target;
3988static List<Object*> object_stack(20);
3989
3990
3991// Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject.
3992static const int kMarkTag = 2;
3993
3994static void MarkObjectRecursively(Object** p);
3995class MarkObjectVisitor : public ObjectVisitor {
3996 public:
3997 void VisitPointers(Object** start, Object** end) {
3998 // Copy all HeapObject pointers in [start, end)
3999 for (Object** p = start; p < end; p++) {
4000 if ((*p)->IsHeapObject())
4001 MarkObjectRecursively(p);
4002 }
4003 }
4004};
4005
4006static MarkObjectVisitor mark_visitor;
4007
4008static void MarkObjectRecursively(Object** p) {
4009 if (!(*p)->IsHeapObject()) return;
4010
4011 HeapObject* obj = HeapObject::cast(*p);
4012
4013 Object* map = obj->map();
4014
4015 if (!map->IsHeapObject()) return; // visited before
4016
4017 if (found_target) return; // stop if target found
4018 object_stack.Add(obj);
4019 if ((search_for_any_global && obj->IsJSGlobalObject()) ||
4020 (!search_for_any_global && (obj == search_target))) {
4021 found_target = true;
4022 return;
4023 }
4024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004025 // not visited yet
4026 Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map));
4027
4028 Address map_addr = map_p->address();
4029
4030 obj->set_map(reinterpret_cast<Map*>(map_addr + kMarkTag));
4031
4032 MarkObjectRecursively(&map);
4033
4034 obj->IterateBody(map_p->instance_type(), obj->SizeFromMap(map_p),
4035 &mark_visitor);
4036
4037 if (!found_target) // don't pop if found the target
4038 object_stack.RemoveLast();
4039}
4040
4041
4042static void UnmarkObjectRecursively(Object** p);
4043class UnmarkObjectVisitor : public ObjectVisitor {
4044 public:
4045 void VisitPointers(Object** start, Object** end) {
4046 // Copy all HeapObject pointers in [start, end)
4047 for (Object** p = start; p < end; p++) {
4048 if ((*p)->IsHeapObject())
4049 UnmarkObjectRecursively(p);
4050 }
4051 }
4052};
4053
4054static UnmarkObjectVisitor unmark_visitor;
4055
4056static void UnmarkObjectRecursively(Object** p) {
4057 if (!(*p)->IsHeapObject()) return;
4058
4059 HeapObject* obj = HeapObject::cast(*p);
4060
4061 Object* map = obj->map();
4062
4063 if (map->IsHeapObject()) return; // unmarked already
4064
4065 Address map_addr = reinterpret_cast<Address>(map);
4066
4067 map_addr -= kMarkTag;
4068
4069 ASSERT_TAG_ALIGNED(map_addr);
4070
4071 HeapObject* map_p = HeapObject::FromAddress(map_addr);
4072
4073 obj->set_map(reinterpret_cast<Map*>(map_p));
4074
4075 UnmarkObjectRecursively(reinterpret_cast<Object**>(&map_p));
4076
4077 obj->IterateBody(Map::cast(map_p)->instance_type(),
4078 obj->SizeFromMap(Map::cast(map_p)),
4079 &unmark_visitor);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004080}
4081
4082
4083static void MarkRootObjectRecursively(Object** root) {
4084 if (search_for_any_global) {
4085 ASSERT(search_target == NULL);
4086 } else {
4087 ASSERT(search_target->IsHeapObject());
4088 }
4089 found_target = false;
4090 object_stack.Clear();
4091
4092 MarkObjectRecursively(root);
4093 UnmarkObjectRecursively(root);
4094
4095 if (found_target) {
4096 PrintF("=====================================\n");
4097 PrintF("==== Path to object ====\n");
4098 PrintF("=====================================\n\n");
4099
4100 ASSERT(!object_stack.is_empty());
4101 for (int i = 0; i < object_stack.length(); i++) {
4102 if (i > 0) PrintF("\n |\n |\n V\n\n");
4103 Object* obj = object_stack[i];
4104 obj->Print();
4105 }
4106 PrintF("=====================================\n");
4107 }
4108}
4109
4110
4111// Helper class for visiting HeapObjects recursively.
4112class MarkRootVisitor: public ObjectVisitor {
4113 public:
4114 void VisitPointers(Object** start, Object** end) {
4115 // Visit all HeapObject pointers in [start, end)
4116 for (Object** p = start; p < end; p++) {
4117 if ((*p)->IsHeapObject())
4118 MarkRootObjectRecursively(p);
4119 }
4120 }
4121};
4122
4123
4124// Triggers a depth-first traversal of reachable objects from roots
4125// and finds a path to a specific heap object and prints it.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004126void Heap::TracePathToObject(Object* target) {
4127 search_target = target;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004128 search_for_any_global = false;
4129
4130 MarkRootVisitor root_visitor;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004131 IterateRoots(&root_visitor, VISIT_ONLY_STRONG);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004132}
4133
4134
4135// Triggers a depth-first traversal of reachable objects from roots
4136// and finds a path to any global object and prints it. Useful for
4137// determining the source for leaks of global objects.
4138void Heap::TracePathToGlobal() {
4139 search_target = NULL;
4140 search_for_any_global = true;
4141
4142 MarkRootVisitor root_visitor;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004143 IterateRoots(&root_visitor, VISIT_ONLY_STRONG);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004144}
4145#endif
4146
4147
kasper.lund7276f142008-07-30 08:49:36 +00004148GCTracer::GCTracer()
4149 : start_time_(0.0),
4150 start_size_(0.0),
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004151 external_time_(0.0),
kasper.lund7276f142008-07-30 08:49:36 +00004152 gc_count_(0),
4153 full_gc_count_(0),
4154 is_compacting_(false),
4155 marked_count_(0) {
4156 // These two fields reflect the state of the previous full collection.
4157 // Set them before they are changed by the collector.
4158 previous_has_compacted_ = MarkCompactCollector::HasCompacted();
4159 previous_marked_count_ = MarkCompactCollector::previous_marked_count();
4160 if (!FLAG_trace_gc) return;
4161 start_time_ = OS::TimeCurrentMillis();
4162 start_size_ = SizeOfHeapObjects();
4163}
4164
4165
4166GCTracer::~GCTracer() {
4167 if (!FLAG_trace_gc) return;
4168 // Printf ONE line iff flag is set.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004169 int time = static_cast<int>(OS::TimeCurrentMillis() - start_time_);
4170 int external_time = static_cast<int>(external_time_);
4171 PrintF("%s %.1f -> %.1f MB, ",
4172 CollectorString(), start_size_, SizeOfHeapObjects());
4173 if (external_time > 0) PrintF("%d / ", external_time);
4174 PrintF("%d ms.\n", time);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004175
4176#if defined(ENABLE_LOGGING_AND_PROFILING)
4177 Heap::PrintShortHeapStatistics();
4178#endif
kasper.lund7276f142008-07-30 08:49:36 +00004179}
4180
4181
4182const char* GCTracer::CollectorString() {
4183 switch (collector_) {
4184 case SCAVENGER:
4185 return "Scavenge";
4186 case MARK_COMPACTOR:
4187 return MarkCompactCollector::HasCompacted() ? "Mark-compact"
4188 : "Mark-sweep";
4189 }
4190 return "Unknown GC";
4191}
4192
4193
ager@chromium.org5aa501c2009-06-23 07:57:28 +00004194int KeyedLookupCache::Hash(Map* map, String* name) {
4195 // Uses only lower 32 bits if pointers are larger.
4196 uintptr_t addr_hash =
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004197 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)) >> kMapHashShift;
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00004198 return static_cast<uint32_t>((addr_hash ^ name->Hash()) & kCapacityMask);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00004199}
4200
4201
4202int KeyedLookupCache::Lookup(Map* map, String* name) {
4203 int index = Hash(map, name);
4204 Key& key = keys_[index];
4205 if ((key.map == map) && key.name->Equals(name)) {
4206 return field_offsets_[index];
4207 }
4208 return -1;
4209}
4210
4211
4212void KeyedLookupCache::Update(Map* map, String* name, int field_offset) {
4213 String* symbol;
4214 if (Heap::LookupSymbolIfExists(name, &symbol)) {
4215 int index = Hash(map, symbol);
4216 Key& key = keys_[index];
4217 key.map = map;
4218 key.name = symbol;
4219 field_offsets_[index] = field_offset;
4220 }
4221}
4222
4223
4224void KeyedLookupCache::Clear() {
4225 for (int index = 0; index < kLength; index++) keys_[index].map = NULL;
4226}
4227
4228
4229KeyedLookupCache::Key KeyedLookupCache::keys_[KeyedLookupCache::kLength];
4230
4231
4232int KeyedLookupCache::field_offsets_[KeyedLookupCache::kLength];
4233
4234
4235void DescriptorLookupCache::Clear() {
4236 for (int index = 0; index < kLength; index++) keys_[index].array = NULL;
4237}
4238
4239
4240DescriptorLookupCache::Key
4241DescriptorLookupCache::keys_[DescriptorLookupCache::kLength];
4242
4243int DescriptorLookupCache::results_[DescriptorLookupCache::kLength];
4244
4245
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004246#ifdef DEBUG
4247bool Heap::GarbageCollectionGreedyCheck() {
4248 ASSERT(FLAG_gc_greedy);
4249 if (Bootstrapper::IsActive()) return true;
4250 if (disallow_allocation_failure()) return true;
4251 return CollectGarbage(0, NEW_SPACE);
4252}
4253#endif
4254
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004255
4256TranscendentalCache::TranscendentalCache(TranscendentalCache::Type t)
4257 : type_(t) {
4258 uint32_t in0 = 0xffffffffu; // Bit-pattern for a NaN that isn't
4259 uint32_t in1 = 0xffffffffu; // generated by the FPU.
4260 for (int i = 0; i < kCacheSize; i++) {
4261 elements_[i].in[0] = in0;
4262 elements_[i].in[1] = in1;
4263 elements_[i].output = NULL;
4264 }
4265}
4266
4267
4268TranscendentalCache* TranscendentalCache::caches_[kNumberOfCaches];
4269
4270
4271void TranscendentalCache::Clear() {
4272 for (int i = 0; i < kNumberOfCaches; i++) {
4273 if (caches_[i] != NULL) {
4274 delete caches_[i];
4275 caches_[i] = NULL;
4276 }
4277 }
4278}
4279
4280
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004281void ExternalStringTable::CleanUp() {
4282 int last = 0;
4283 for (int i = 0; i < new_space_strings_.length(); ++i) {
4284 if (new_space_strings_[i] == Heap::raw_unchecked_null_value()) continue;
4285 if (Heap::InNewSpace(new_space_strings_[i])) {
4286 new_space_strings_[last++] = new_space_strings_[i];
4287 } else {
4288 old_space_strings_.Add(new_space_strings_[i]);
4289 }
4290 }
4291 new_space_strings_.Rewind(last);
4292 last = 0;
4293 for (int i = 0; i < old_space_strings_.length(); ++i) {
4294 if (old_space_strings_[i] == Heap::raw_unchecked_null_value()) continue;
4295 ASSERT(!Heap::InNewSpace(old_space_strings_[i]));
4296 old_space_strings_[last++] = old_space_strings_[i];
4297 }
4298 old_space_strings_.Rewind(last);
4299 Verify();
4300}
4301
4302
4303void ExternalStringTable::TearDown() {
4304 new_space_strings_.Free();
4305 old_space_strings_.Free();
4306}
4307
4308
4309List<Object*> ExternalStringTable::new_space_strings_;
4310List<Object*> ExternalStringTable::old_space_strings_;
4311
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004312} } // namespace v8::internal