Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "accessors.h" |
| 31 | #include "api.h" |
| 32 | #include "bootstrapper.h" |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 33 | #include "codegen.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 34 | #include "compilation-cache.h" |
| 35 | #include "debug.h" |
| 36 | #include "heap-profiler.h" |
| 37 | #include "global-handles.h" |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 38 | #include "liveobjectlist-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | #include "mark-compact.h" |
| 40 | #include "natives.h" |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 41 | #include "objects-visiting.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 42 | #include "runtime-profiler.h" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 43 | #include "scanner-base.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | #include "scopeinfo.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 45 | #include "snapshot.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | #include "v8threads.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 47 | #include "vm-state-inl.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 48 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | #include "regexp-macro-assembler.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 50 | #include "arm/regexp-macro-assembler-arm.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 52 | #if V8_TARGET_ARCH_MIPS && !V8_INTERPRETED_REGEXP |
| 53 | #include "regexp-macro-assembler.h" |
| 54 | #include "mips/regexp-macro-assembler-mips.h" |
| 55 | #endif |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 56 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 57 | namespace v8 { |
| 58 | namespace internal { |
| 59 | |
| 60 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 61 | static const intptr_t kMinimumPromotionLimit = 2 * MB; |
| 62 | static const intptr_t kMinimumAllocationLimit = 8 * MB; |
| 63 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 65 | static Mutex* gc_initializer_mutex = OS::CreateMutex(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 68 | Heap::Heap() |
| 69 | : isolate_(NULL), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | // semispace_size_ should be a power of 2 and old_generation_size_ should be |
| 71 | // a multiple of Page::kPageSize. |
| 72 | #if defined(ANDROID) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 73 | reserved_semispace_size_(2*MB), |
| 74 | max_semispace_size_(2*MB), |
| 75 | initial_semispace_size_(128*KB), |
| 76 | max_old_generation_size_(192*MB), |
| 77 | max_executable_size_(max_old_generation_size_), |
| 78 | code_range_size_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 79 | #elif defined(V8_TARGET_ARCH_X64) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 80 | reserved_semispace_size_(16*MB), |
| 81 | max_semispace_size_(16*MB), |
| 82 | initial_semispace_size_(1*MB), |
| 83 | max_old_generation_size_(1*GB), |
| 84 | max_executable_size_(256*MB), |
| 85 | code_range_size_(512*MB), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | #else |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 87 | reserved_semispace_size_(8*MB), |
| 88 | max_semispace_size_(8*MB), |
| 89 | initial_semispace_size_(512*KB), |
| 90 | max_old_generation_size_(512*MB), |
| 91 | max_executable_size_(128*MB), |
| 92 | code_range_size_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 94 | // Variables set based on semispace_size_ and old_generation_size_ in |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 95 | // ConfigureHeap (survived_since_last_expansion_, external_allocation_limit_) |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 96 | // Will be 4 * reserved_semispace_size_ to ensure that young |
| 97 | // generation can be aligned to its size. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 98 | survived_since_last_expansion_(0), |
| 99 | always_allocate_scope_depth_(0), |
| 100 | linear_allocation_scope_depth_(0), |
| 101 | contexts_disposed_(0), |
| 102 | new_space_(this), |
| 103 | old_pointer_space_(NULL), |
| 104 | old_data_space_(NULL), |
| 105 | code_space_(NULL), |
| 106 | map_space_(NULL), |
| 107 | cell_space_(NULL), |
| 108 | lo_space_(NULL), |
| 109 | gc_state_(NOT_IN_GC), |
| 110 | mc_count_(0), |
| 111 | ms_count_(0), |
| 112 | gc_count_(0), |
| 113 | unflattened_strings_length_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | #ifdef DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 115 | allocation_allowed_(true), |
| 116 | allocation_timeout_(0), |
| 117 | disallow_allocation_failure_(false), |
| 118 | debug_utils_(NULL), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | #endif // DEBUG |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 120 | old_gen_promotion_limit_(kMinimumPromotionLimit), |
| 121 | old_gen_allocation_limit_(kMinimumAllocationLimit), |
| 122 | external_allocation_limit_(0), |
| 123 | amount_of_external_allocated_memory_(0), |
| 124 | amount_of_external_allocated_memory_at_last_global_gc_(0), |
| 125 | old_gen_exhausted_(false), |
| 126 | hidden_symbol_(NULL), |
| 127 | global_gc_prologue_callback_(NULL), |
| 128 | global_gc_epilogue_callback_(NULL), |
| 129 | gc_safe_size_of_old_object_(NULL), |
| 130 | tracer_(NULL), |
| 131 | young_survivors_after_last_gc_(0), |
| 132 | high_survival_rate_period_length_(0), |
| 133 | survival_rate_(0), |
| 134 | previous_survival_rate_trend_(Heap::STABLE), |
| 135 | survival_rate_trend_(Heap::STABLE), |
| 136 | max_gc_pause_(0), |
| 137 | max_alive_after_gc_(0), |
| 138 | min_in_mutator_(kMaxInt), |
| 139 | alive_after_last_gc_(0), |
| 140 | last_gc_end_timestamp_(0.0), |
| 141 | page_watermark_invalidated_mark_(1 << Page::WATERMARK_INVALIDATED), |
| 142 | number_idle_notifications_(0), |
| 143 | last_idle_notification_gc_count_(0), |
| 144 | last_idle_notification_gc_count_init_(false), |
| 145 | configured_(false), |
| 146 | is_safe_to_read_maps_(true) { |
| 147 | // Allow build-time customization of the max semispace size. Building |
| 148 | // V8 with snapshots and a non-default max semispace size is much |
| 149 | // easier if you can define it as part of the build environment. |
| 150 | #if defined(V8_MAX_SEMISPACE_SIZE) |
| 151 | max_semispace_size_ = reserved_semispace_size_ = V8_MAX_SEMISPACE_SIZE; |
| 152 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 154 | memset(roots_, 0, sizeof(roots_[0]) * kRootListLength); |
| 155 | global_contexts_list_ = NULL; |
| 156 | mark_compact_collector_.heap_ = this; |
| 157 | external_string_table_.heap_ = this; |
| 158 | } |
| 159 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 160 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 161 | intptr_t Heap::Capacity() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | if (!HasBeenSetup()) return 0; |
| 163 | |
| 164 | return new_space_.Capacity() + |
| 165 | old_pointer_space_->Capacity() + |
| 166 | old_data_space_->Capacity() + |
| 167 | code_space_->Capacity() + |
| 168 | map_space_->Capacity() + |
| 169 | cell_space_->Capacity(); |
| 170 | } |
| 171 | |
| 172 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 173 | intptr_t Heap::CommittedMemory() { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 174 | if (!HasBeenSetup()) return 0; |
| 175 | |
| 176 | return new_space_.CommittedMemory() + |
| 177 | old_pointer_space_->CommittedMemory() + |
| 178 | old_data_space_->CommittedMemory() + |
| 179 | code_space_->CommittedMemory() + |
| 180 | map_space_->CommittedMemory() + |
| 181 | cell_space_->CommittedMemory() + |
| 182 | lo_space_->Size(); |
| 183 | } |
| 184 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 185 | intptr_t Heap::CommittedMemoryExecutable() { |
| 186 | if (!HasBeenSetup()) return 0; |
| 187 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 188 | return isolate()->memory_allocator()->SizeExecutable(); |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 191 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 192 | intptr_t Heap::Available() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | if (!HasBeenSetup()) return 0; |
| 194 | |
| 195 | return new_space_.Available() + |
| 196 | old_pointer_space_->Available() + |
| 197 | old_data_space_->Available() + |
| 198 | code_space_->Available() + |
| 199 | map_space_->Available() + |
| 200 | cell_space_->Available(); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | bool Heap::HasBeenSetup() { |
| 205 | return old_pointer_space_ != NULL && |
| 206 | old_data_space_ != NULL && |
| 207 | code_space_ != NULL && |
| 208 | map_space_ != NULL && |
| 209 | cell_space_ != NULL && |
| 210 | lo_space_ != NULL; |
| 211 | } |
| 212 | |
| 213 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 214 | int Heap::GcSafeSizeOfOldObject(HeapObject* object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 215 | ASSERT(!HEAP->InNewSpace(object)); // Code only works for old objects. |
| 216 | ASSERT(!HEAP->mark_compact_collector()->are_map_pointers_encoded()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 217 | MapWord map_word = object->map_word(); |
| 218 | map_word.ClearMark(); |
| 219 | map_word.ClearOverflow(); |
| 220 | return object->SizeFromMap(map_word.ToMap()); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | int Heap::GcSafeSizeOfOldObjectWithEncodedMap(HeapObject* object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 225 | ASSERT(!HEAP->InNewSpace(object)); // Code only works for old objects. |
| 226 | ASSERT(HEAP->mark_compact_collector()->are_map_pointers_encoded()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 227 | uint32_t marker = Memory::uint32_at(object->address()); |
| 228 | if (marker == MarkCompactCollector::kSingleFreeEncoding) { |
| 229 | return kIntSize; |
| 230 | } else if (marker == MarkCompactCollector::kMultiFreeEncoding) { |
| 231 | return Memory::int_at(object->address() + kIntSize); |
| 232 | } else { |
| 233 | MapWord map_word = object->map_word(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 234 | Address map_address = map_word.DecodeMapAddress(HEAP->map_space()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 235 | Map* map = reinterpret_cast<Map*>(HeapObject::FromAddress(map_address)); |
| 236 | return object->SizeFromMap(map); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | GarbageCollector Heap::SelectGarbageCollector(AllocationSpace space) { |
| 242 | // Is global GC requested? |
| 243 | if (space != NEW_SPACE || FLAG_gc_global) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 244 | isolate_->counters()->gc_compactor_caused_by_request()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | return MARK_COMPACTOR; |
| 246 | } |
| 247 | |
| 248 | // Is enough data promoted to justify a global GC? |
| 249 | if (OldGenerationPromotionLimitReached()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 250 | isolate_->counters()->gc_compactor_caused_by_promoted_data()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | return MARK_COMPACTOR; |
| 252 | } |
| 253 | |
| 254 | // Have allocation in OLD and LO failed? |
| 255 | if (old_gen_exhausted_) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 256 | isolate_->counters()-> |
| 257 | gc_compactor_caused_by_oldspace_exhaustion()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 258 | return MARK_COMPACTOR; |
| 259 | } |
| 260 | |
| 261 | // Is there enough space left in OLD to guarantee that a scavenge can |
| 262 | // succeed? |
| 263 | // |
| 264 | // Note that MemoryAllocator->MaxAvailable() undercounts the memory available |
| 265 | // for object promotion. It counts only the bytes that the memory |
| 266 | // allocator has not yet allocated from the OS and assigned to any space, |
| 267 | // and does not count available bytes already in the old space or code |
| 268 | // space. Undercounting is safe---we may get an unrequested full GC when |
| 269 | // a scavenge would have succeeded. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 270 | if (isolate_->memory_allocator()->MaxAvailable() <= new_space_.Size()) { |
| 271 | isolate_->counters()-> |
| 272 | gc_compactor_caused_by_oldspace_exhaustion()->Increment(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | return MARK_COMPACTOR; |
| 274 | } |
| 275 | |
| 276 | // Default |
| 277 | return SCAVENGER; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 282 | // --log-gc to avoid the complicated preprocessor and flag testing. |
| 283 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 284 | void Heap::ReportStatisticsBeforeGC() { |
| 285 | // Heap::ReportHeapStatistics will also log NewSpace statistics when |
| 286 | // compiled with ENABLE_LOGGING_AND_PROFILING and --log-gc is set. The |
| 287 | // following logic is used to avoid double logging. |
| 288 | #if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING) |
| 289 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.CollectStatistics(); |
| 290 | if (FLAG_heap_stats) { |
| 291 | ReportHeapStatistics("Before GC"); |
| 292 | } else if (FLAG_log_gc) { |
| 293 | new_space_.ReportStatistics(); |
| 294 | } |
| 295 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.ClearHistograms(); |
| 296 | #elif defined(DEBUG) |
| 297 | if (FLAG_heap_stats) { |
| 298 | new_space_.CollectStatistics(); |
| 299 | ReportHeapStatistics("Before GC"); |
| 300 | new_space_.ClearHistograms(); |
| 301 | } |
| 302 | #elif defined(ENABLE_LOGGING_AND_PROFILING) |
| 303 | if (FLAG_log_gc) { |
| 304 | new_space_.CollectStatistics(); |
| 305 | new_space_.ReportStatistics(); |
| 306 | new_space_.ClearHistograms(); |
| 307 | } |
| 308 | #endif |
| 309 | } |
| 310 | |
| 311 | |
| 312 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
| 313 | void Heap::PrintShortHeapStatistics() { |
| 314 | if (!FLAG_trace_gc_verbose) return; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 315 | PrintF("Memory allocator, used: %8" V8_PTR_PREFIX "d" |
| 316 | ", available: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 317 | isolate_->memory_allocator()->Size(), |
| 318 | isolate_->memory_allocator()->Available()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 319 | PrintF("New space, used: %8" V8_PTR_PREFIX "d" |
| 320 | ", available: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 321 | Heap::new_space_.Size(), |
| 322 | new_space_.Available()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 323 | PrintF("Old pointers, used: %8" V8_PTR_PREFIX "d" |
| 324 | ", available: %8" V8_PTR_PREFIX "d" |
| 325 | ", waste: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 326 | old_pointer_space_->Size(), |
| 327 | old_pointer_space_->Available(), |
| 328 | old_pointer_space_->Waste()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 329 | PrintF("Old data space, used: %8" V8_PTR_PREFIX "d" |
| 330 | ", available: %8" V8_PTR_PREFIX "d" |
| 331 | ", waste: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 332 | old_data_space_->Size(), |
| 333 | old_data_space_->Available(), |
| 334 | old_data_space_->Waste()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 335 | PrintF("Code space, used: %8" V8_PTR_PREFIX "d" |
| 336 | ", available: %8" V8_PTR_PREFIX "d" |
| 337 | ", waste: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 338 | code_space_->Size(), |
| 339 | code_space_->Available(), |
| 340 | code_space_->Waste()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 341 | PrintF("Map space, used: %8" V8_PTR_PREFIX "d" |
| 342 | ", available: %8" V8_PTR_PREFIX "d" |
| 343 | ", waste: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 344 | map_space_->Size(), |
| 345 | map_space_->Available(), |
| 346 | map_space_->Waste()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 347 | PrintF("Cell space, used: %8" V8_PTR_PREFIX "d" |
| 348 | ", available: %8" V8_PTR_PREFIX "d" |
| 349 | ", waste: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 350 | cell_space_->Size(), |
| 351 | cell_space_->Available(), |
| 352 | cell_space_->Waste()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 353 | PrintF("Large object space, used: %8" V8_PTR_PREFIX "d" |
| 354 | ", available: %8" V8_PTR_PREFIX "d\n", |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 355 | lo_space_->Size(), |
| 356 | lo_space_->Available()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | } |
| 358 | #endif |
| 359 | |
| 360 | |
| 361 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 362 | // --log-gc to avoid the complicated preprocessor and flag testing. |
| 363 | void Heap::ReportStatisticsAfterGC() { |
| 364 | // Similar to the before GC, we use some complicated logic to ensure that |
| 365 | // NewSpace statistics are logged exactly once when --log-gc is turned on. |
| 366 | #if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING) |
| 367 | if (FLAG_heap_stats) { |
| 368 | new_space_.CollectStatistics(); |
| 369 | ReportHeapStatistics("After GC"); |
| 370 | } else if (FLAG_log_gc) { |
| 371 | new_space_.ReportStatistics(); |
| 372 | } |
| 373 | #elif defined(DEBUG) |
| 374 | if (FLAG_heap_stats) ReportHeapStatistics("After GC"); |
| 375 | #elif defined(ENABLE_LOGGING_AND_PROFILING) |
| 376 | if (FLAG_log_gc) new_space_.ReportStatistics(); |
| 377 | #endif |
| 378 | } |
| 379 | #endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 380 | |
| 381 | |
| 382 | void Heap::GarbageCollectionPrologue() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 383 | isolate_->transcendental_cache()->Clear(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 384 | ClearJSFunctionResultCaches(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | gc_count_++; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 386 | unflattened_strings_length_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 387 | #ifdef DEBUG |
| 388 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 389 | allow_allocation(false); |
| 390 | |
| 391 | if (FLAG_verify_heap) { |
| 392 | Verify(); |
| 393 | } |
| 394 | |
| 395 | if (FLAG_gc_verbose) Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | #endif |
| 397 | |
| 398 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 399 | ReportStatisticsBeforeGC(); |
| 400 | #endif |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 401 | |
| 402 | LiveObjectList::GCPrologue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 405 | intptr_t Heap::SizeOfObjects() { |
| 406 | intptr_t total = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | AllSpaces spaces; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 408 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 409 | total += space->SizeOfObjects(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 410 | } |
| 411 | return total; |
| 412 | } |
| 413 | |
| 414 | void Heap::GarbageCollectionEpilogue() { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 415 | LiveObjectList::GCEpilogue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 416 | #ifdef DEBUG |
| 417 | allow_allocation(true); |
| 418 | ZapFromSpace(); |
| 419 | |
| 420 | if (FLAG_verify_heap) { |
| 421 | Verify(); |
| 422 | } |
| 423 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 424 | if (FLAG_print_global_handles) isolate_->global_handles()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 425 | if (FLAG_print_handles) PrintHandles(); |
| 426 | if (FLAG_gc_verbose) Print(); |
| 427 | if (FLAG_code_stats) ReportCodeStatistics("After GC"); |
| 428 | #endif |
| 429 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 430 | isolate_->counters()->alive_after_last_gc()->Set( |
| 431 | static_cast<int>(SizeOfObjects())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 432 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 433 | isolate_->counters()->symbol_table_capacity()->Set( |
| 434 | symbol_table()->Capacity()); |
| 435 | isolate_->counters()->number_of_symbols()->Set( |
| 436 | symbol_table()->NumberOfElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 437 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 438 | ReportStatisticsAfterGC(); |
| 439 | #endif |
| 440 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 441 | isolate_->debug()->AfterGarbageCollection(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 442 | #endif |
| 443 | } |
| 444 | |
| 445 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 446 | void Heap::CollectAllGarbage(bool force_compaction) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | // Since we are ignoring the return value, the exact choice of space does |
| 448 | // not matter, so long as we do not specify NEW_SPACE, which would not |
| 449 | // cause a full GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 450 | mark_compact_collector_.SetForceCompaction(force_compaction); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 451 | CollectGarbage(OLD_POINTER_SPACE); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 452 | mark_compact_collector_.SetForceCompaction(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 456 | void Heap::CollectAllAvailableGarbage() { |
| 457 | // Since we are ignoring the return value, the exact choice of space does |
| 458 | // not matter, so long as we do not specify NEW_SPACE, which would not |
| 459 | // cause a full GC. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 460 | mark_compact_collector()->SetForceCompaction(true); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 461 | |
| 462 | // Major GC would invoke weak handle callbacks on weakly reachable |
| 463 | // handles, but won't collect weakly reachable objects until next |
| 464 | // major GC. Therefore if we collect aggressively and weak handle callback |
| 465 | // has been invoked, we rerun major GC to release objects which become |
| 466 | // garbage. |
| 467 | // Note: as weak callbacks can execute arbitrary code, we cannot |
| 468 | // hope that eventually there will be no weak callbacks invocations. |
| 469 | // Therefore stop recollecting after several attempts. |
| 470 | const int kMaxNumberOfAttempts = 7; |
| 471 | for (int attempt = 0; attempt < kMaxNumberOfAttempts; attempt++) { |
| 472 | if (!CollectGarbage(OLD_POINTER_SPACE, MARK_COMPACTOR)) { |
| 473 | break; |
| 474 | } |
| 475 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 476 | mark_compact_collector()->SetForceCompaction(false); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | |
| 480 | bool Heap::CollectGarbage(AllocationSpace space, GarbageCollector collector) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 481 | // The VM is in the GC state until exiting this function. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 482 | VMState state(isolate_, GC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 483 | |
| 484 | #ifdef DEBUG |
| 485 | // Reset the allocation timeout to the GC interval, but make sure to |
| 486 | // allow at least a few allocations after a collection. The reason |
| 487 | // for this is that we have a lot of allocation sequences and we |
| 488 | // assume that a garbage collection will allow the subsequent |
| 489 | // allocation attempts to go through. |
| 490 | allocation_timeout_ = Max(6, FLAG_gc_interval); |
| 491 | #endif |
| 492 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 493 | bool next_gc_likely_to_collect_more = false; |
| 494 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 495 | { GCTracer tracer(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 496 | GarbageCollectionPrologue(); |
| 497 | // The GC count was incremented in the prologue. Tell the tracer about |
| 498 | // it. |
| 499 | tracer.set_gc_count(gc_count_); |
| 500 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 501 | // Tell the tracer which collector we've selected. |
| 502 | tracer.set_collector(collector); |
| 503 | |
| 504 | HistogramTimer* rate = (collector == SCAVENGER) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 505 | ? isolate_->counters()->gc_scavenger() |
| 506 | : isolate_->counters()->gc_compactor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 507 | rate->Start(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 508 | next_gc_likely_to_collect_more = |
| 509 | PerformGarbageCollection(collector, &tracer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 510 | rate->Stop(); |
| 511 | |
| 512 | GarbageCollectionEpilogue(); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 517 | if (FLAG_log_gc) HeapProfiler::WriteSample(); |
| 518 | #endif |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 519 | |
| 520 | return next_gc_likely_to_collect_more; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
| 524 | void Heap::PerformScavenge() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 525 | GCTracer tracer(this); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 526 | PerformGarbageCollection(SCAVENGER, &tracer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
| 530 | #ifdef DEBUG |
| 531 | // Helper class for verifying the symbol table. |
| 532 | class SymbolTableVerifier : public ObjectVisitor { |
| 533 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 534 | void VisitPointers(Object** start, Object** end) { |
| 535 | // Visit all HeapObject pointers in [start, end). |
| 536 | for (Object** p = start; p < end; p++) { |
| 537 | if ((*p)->IsHeapObject()) { |
| 538 | // Check that the symbol is actually a symbol. |
| 539 | ASSERT((*p)->IsNull() || (*p)->IsUndefined() || (*p)->IsSymbol()); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | }; |
| 544 | #endif // DEBUG |
| 545 | |
| 546 | |
| 547 | static void VerifySymbolTable() { |
| 548 | #ifdef DEBUG |
| 549 | SymbolTableVerifier verifier; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 550 | HEAP->symbol_table()->IterateElements(&verifier); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 551 | #endif // DEBUG |
| 552 | } |
| 553 | |
| 554 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 555 | void Heap::ReserveSpace( |
| 556 | int new_space_size, |
| 557 | int pointer_space_size, |
| 558 | int data_space_size, |
| 559 | int code_space_size, |
| 560 | int map_space_size, |
| 561 | int cell_space_size, |
| 562 | int large_object_size) { |
| 563 | NewSpace* new_space = Heap::new_space(); |
| 564 | PagedSpace* old_pointer_space = Heap::old_pointer_space(); |
| 565 | PagedSpace* old_data_space = Heap::old_data_space(); |
| 566 | PagedSpace* code_space = Heap::code_space(); |
| 567 | PagedSpace* map_space = Heap::map_space(); |
| 568 | PagedSpace* cell_space = Heap::cell_space(); |
| 569 | LargeObjectSpace* lo_space = Heap::lo_space(); |
| 570 | bool gc_performed = true; |
| 571 | while (gc_performed) { |
| 572 | gc_performed = false; |
| 573 | if (!new_space->ReserveSpace(new_space_size)) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 574 | Heap::CollectGarbage(NEW_SPACE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 575 | gc_performed = true; |
| 576 | } |
| 577 | if (!old_pointer_space->ReserveSpace(pointer_space_size)) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 578 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 579 | gc_performed = true; |
| 580 | } |
| 581 | if (!(old_data_space->ReserveSpace(data_space_size))) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 582 | Heap::CollectGarbage(OLD_DATA_SPACE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 583 | gc_performed = true; |
| 584 | } |
| 585 | if (!(code_space->ReserveSpace(code_space_size))) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 586 | Heap::CollectGarbage(CODE_SPACE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 587 | gc_performed = true; |
| 588 | } |
| 589 | if (!(map_space->ReserveSpace(map_space_size))) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 590 | Heap::CollectGarbage(MAP_SPACE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 591 | gc_performed = true; |
| 592 | } |
| 593 | if (!(cell_space->ReserveSpace(cell_space_size))) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 594 | Heap::CollectGarbage(CELL_SPACE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 595 | gc_performed = true; |
| 596 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 597 | // We add a slack-factor of 2 in order to have space for a series of |
| 598 | // large-object allocations that are only just larger than the page size. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 599 | large_object_size *= 2; |
| 600 | // The ReserveSpace method on the large object space checks how much |
| 601 | // we can expand the old generation. This includes expansion caused by |
| 602 | // allocation in the other spaces. |
| 603 | large_object_size += cell_space_size + map_space_size + code_space_size + |
| 604 | data_space_size + pointer_space_size; |
| 605 | if (!(lo_space->ReserveSpace(large_object_size))) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 606 | Heap::CollectGarbage(LO_SPACE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 607 | gc_performed = true; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 613 | void Heap::EnsureFromSpaceIsCommitted() { |
| 614 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 615 | |
| 616 | // Committing memory to from space failed. |
| 617 | // Try shrinking and try again. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 618 | PagedSpaces spaces; |
| 619 | for (PagedSpace* space = spaces.next(); |
| 620 | space != NULL; |
| 621 | space = spaces.next()) { |
| 622 | space->RelinkPageListInChunkOrder(true); |
| 623 | } |
| 624 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 625 | Shrink(); |
| 626 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 627 | |
| 628 | // Committing memory to from space failed again. |
| 629 | // Memory is exhausted and we will die. |
| 630 | V8::FatalProcessOutOfMemory("Committing semi space failed."); |
| 631 | } |
| 632 | |
| 633 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 634 | void Heap::ClearJSFunctionResultCaches() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 635 | if (isolate_->bootstrapper()->IsActive()) return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 636 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 637 | Object* context = global_contexts_list_; |
| 638 | while (!context->IsUndefined()) { |
| 639 | // Get the caches for this context: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 640 | FixedArray* caches = |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 641 | Context::cast(context)->jsfunction_result_caches(); |
| 642 | // Clear the caches: |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 643 | int length = caches->length(); |
| 644 | for (int i = 0; i < length; i++) { |
| 645 | JSFunctionResultCache::cast(caches->get(i))->Clear(); |
| 646 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 647 | // Get the next context: |
| 648 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 649 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 653 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 654 | void Heap::ClearNormalizedMapCaches() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 655 | if (isolate_->bootstrapper()->IsActive()) return; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 656 | |
| 657 | Object* context = global_contexts_list_; |
| 658 | while (!context->IsUndefined()) { |
| 659 | Context::cast(context)->normalized_map_cache()->Clear(); |
| 660 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
| 661 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 665 | #ifdef DEBUG |
| 666 | |
| 667 | enum PageWatermarkValidity { |
| 668 | ALL_VALID, |
| 669 | ALL_INVALID |
| 670 | }; |
| 671 | |
| 672 | static void VerifyPageWatermarkValidity(PagedSpace* space, |
| 673 | PageWatermarkValidity validity) { |
| 674 | PageIterator it(space, PageIterator::PAGES_IN_USE); |
| 675 | bool expected_value = (validity == ALL_VALID); |
| 676 | while (it.has_next()) { |
| 677 | Page* page = it.next(); |
| 678 | ASSERT(page->IsWatermarkValid() == expected_value); |
| 679 | } |
| 680 | } |
| 681 | #endif |
| 682 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 683 | void Heap::UpdateSurvivalRateTrend(int start_new_space_size) { |
| 684 | double survival_rate = |
| 685 | (static_cast<double>(young_survivors_after_last_gc_) * 100) / |
| 686 | start_new_space_size; |
| 687 | |
| 688 | if (survival_rate > kYoungSurvivalRateThreshold) { |
| 689 | high_survival_rate_period_length_++; |
| 690 | } else { |
| 691 | high_survival_rate_period_length_ = 0; |
| 692 | } |
| 693 | |
| 694 | double survival_rate_diff = survival_rate_ - survival_rate; |
| 695 | |
| 696 | if (survival_rate_diff > kYoungSurvivalRateAllowedDeviation) { |
| 697 | set_survival_rate_trend(DECREASING); |
| 698 | } else if (survival_rate_diff < -kYoungSurvivalRateAllowedDeviation) { |
| 699 | set_survival_rate_trend(INCREASING); |
| 700 | } else { |
| 701 | set_survival_rate_trend(STABLE); |
| 702 | } |
| 703 | |
| 704 | survival_rate_ = survival_rate; |
| 705 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 706 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 707 | bool Heap::PerformGarbageCollection(GarbageCollector collector, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 708 | GCTracer* tracer) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 709 | bool next_gc_likely_to_collect_more = false; |
| 710 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 711 | if (collector != SCAVENGER) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 712 | PROFILE(isolate_, CodeMovingGCEvent()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 713 | } |
| 714 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 715 | VerifySymbolTable(); |
| 716 | if (collector == MARK_COMPACTOR && global_gc_prologue_callback_) { |
| 717 | ASSERT(!allocation_allowed_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 718 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 719 | global_gc_prologue_callback_(); |
| 720 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 721 | |
| 722 | GCType gc_type = |
| 723 | collector == MARK_COMPACTOR ? kGCTypeMarkSweepCompact : kGCTypeScavenge; |
| 724 | |
| 725 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 726 | if (gc_type & gc_prologue_callbacks_[i].gc_type) { |
| 727 | gc_prologue_callbacks_[i].callback(gc_type, kNoGCCallbackFlags); |
| 728 | } |
| 729 | } |
| 730 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 731 | EnsureFromSpaceIsCommitted(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 732 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 733 | int start_new_space_size = Heap::new_space()->SizeAsInt(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 734 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 735 | if (collector == MARK_COMPACTOR) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 736 | // Perform mark-sweep with optional compaction. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 737 | MarkCompact(tracer); |
| 738 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 739 | bool high_survival_rate_during_scavenges = IsHighSurvivalRate() && |
| 740 | IsStableOrIncreasingSurvivalTrend(); |
| 741 | |
| 742 | UpdateSurvivalRateTrend(start_new_space_size); |
| 743 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 744 | intptr_t old_gen_size = PromotedSpaceSize(); |
| 745 | old_gen_promotion_limit_ = |
| 746 | old_gen_size + Max(kMinimumPromotionLimit, old_gen_size / 3); |
| 747 | old_gen_allocation_limit_ = |
| 748 | old_gen_size + Max(kMinimumAllocationLimit, old_gen_size / 2); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 749 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 750 | if (high_survival_rate_during_scavenges && |
| 751 | IsStableOrIncreasingSurvivalTrend()) { |
| 752 | // Stable high survival rates of young objects both during partial and |
| 753 | // full collection indicate that mutator is either building or modifying |
| 754 | // a structure with a long lifetime. |
| 755 | // In this case we aggressively raise old generation memory limits to |
| 756 | // postpone subsequent mark-sweep collection and thus trade memory |
| 757 | // space for the mutation speed. |
| 758 | old_gen_promotion_limit_ *= 2; |
| 759 | old_gen_allocation_limit_ *= 2; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 760 | } |
| 761 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 762 | old_gen_exhausted_ = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 763 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 764 | tracer_ = tracer; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 765 | Scavenge(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 766 | tracer_ = NULL; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 767 | |
| 768 | UpdateSurvivalRateTrend(start_new_space_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 769 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 770 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 771 | isolate_->counters()->objs_since_last_young()->Set(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 772 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 773 | if (collector == MARK_COMPACTOR) { |
| 774 | DisableAssertNoAllocation allow_allocation; |
| 775 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 776 | next_gc_likely_to_collect_more = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 777 | isolate_->global_handles()->PostGarbageCollectionProcessing(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 780 | // Update relocatables. |
| 781 | Relocatable::PostGarbageCollectionProcessing(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 782 | |
| 783 | if (collector == MARK_COMPACTOR) { |
| 784 | // Register the amount of external allocated memory. |
| 785 | amount_of_external_allocated_memory_at_last_global_gc_ = |
| 786 | amount_of_external_allocated_memory_; |
| 787 | } |
| 788 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 789 | GCCallbackFlags callback_flags = tracer->is_compacting() |
| 790 | ? kGCCallbackFlagCompacted |
| 791 | : kNoGCCallbackFlags; |
| 792 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 793 | if (gc_type & gc_epilogue_callbacks_[i].gc_type) { |
| 794 | gc_epilogue_callbacks_[i].callback(gc_type, callback_flags); |
| 795 | } |
| 796 | } |
| 797 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 798 | if (collector == MARK_COMPACTOR && global_gc_epilogue_callback_) { |
| 799 | ASSERT(!allocation_allowed_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 800 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 801 | global_gc_epilogue_callback_(); |
| 802 | } |
| 803 | VerifySymbolTable(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 804 | |
| 805 | return next_gc_likely_to_collect_more; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 809 | void Heap::MarkCompact(GCTracer* tracer) { |
| 810 | gc_state_ = MARK_COMPACT; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 811 | LOG(isolate_, ResourceEvent("markcompact", "begin")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 812 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 813 | mark_compact_collector_.Prepare(tracer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 814 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 815 | bool is_compacting = mark_compact_collector_.IsCompacting(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 816 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 817 | if (is_compacting) { |
| 818 | mc_count_++; |
| 819 | } else { |
| 820 | ms_count_++; |
| 821 | } |
| 822 | tracer->set_full_gc_count(mc_count_ + ms_count_); |
| 823 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 824 | MarkCompactPrologue(is_compacting); |
| 825 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 826 | is_safe_to_read_maps_ = false; |
| 827 | mark_compact_collector_.CollectGarbage(); |
| 828 | is_safe_to_read_maps_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 829 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 830 | LOG(isolate_, ResourceEvent("markcompact", "end")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 831 | |
| 832 | gc_state_ = NOT_IN_GC; |
| 833 | |
| 834 | Shrink(); |
| 835 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 836 | isolate_->counters()->objs_since_last_full()->Set(0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 837 | |
| 838 | contexts_disposed_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | |
| 842 | void Heap::MarkCompactPrologue(bool is_compacting) { |
| 843 | // At any old GC clear the keyed lookup cache to enable collection of unused |
| 844 | // maps. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 845 | isolate_->keyed_lookup_cache()->Clear(); |
| 846 | isolate_->context_slot_cache()->Clear(); |
| 847 | isolate_->descriptor_lookup_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 848 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 849 | isolate_->compilation_cache()->MarkCompactPrologue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 850 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 851 | CompletelyClearInstanceofCache(); |
| 852 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 853 | if (is_compacting) FlushNumberStringCache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 854 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 855 | ClearNormalizedMapCaches(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | |
| 859 | Object* Heap::FindCodeObject(Address a) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 860 | Object* obj = NULL; // Initialization to please compiler. |
| 861 | { MaybeObject* maybe_obj = code_space_->FindObject(a); |
| 862 | if (!maybe_obj->ToObject(&obj)) { |
| 863 | obj = lo_space_->FindObject(a)->ToObjectUnchecked(); |
| 864 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 865 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 866 | return obj; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | // Helper class for copying HeapObjects |
| 871 | class ScavengeVisitor: public ObjectVisitor { |
| 872 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 873 | explicit ScavengeVisitor(Heap* heap) : heap_(heap) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 874 | |
| 875 | void VisitPointer(Object** p) { ScavengePointer(p); } |
| 876 | |
| 877 | void VisitPointers(Object** start, Object** end) { |
| 878 | // Copy all HeapObject pointers in [start, end) |
| 879 | for (Object** p = start; p < end; p++) ScavengePointer(p); |
| 880 | } |
| 881 | |
| 882 | private: |
| 883 | void ScavengePointer(Object** p) { |
| 884 | Object* object = *p; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 885 | if (!heap_->InNewSpace(object)) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 886 | Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p), |
| 887 | reinterpret_cast<HeapObject*>(object)); |
| 888 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 889 | |
| 890 | Heap* heap_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 891 | }; |
| 892 | |
| 893 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 894 | #ifdef DEBUG |
| 895 | // Visitor class to verify pointers in code or data space do not point into |
| 896 | // new space. |
| 897 | class VerifyNonPointerSpacePointersVisitor: public ObjectVisitor { |
| 898 | public: |
| 899 | void VisitPointers(Object** start, Object**end) { |
| 900 | for (Object** current = start; current < end; current++) { |
| 901 | if ((*current)->IsHeapObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 902 | ASSERT(!HEAP->InNewSpace(HeapObject::cast(*current))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } |
| 906 | }; |
| 907 | |
| 908 | |
| 909 | static void VerifyNonPointerSpacePointers() { |
| 910 | // Verify that there are no pointers to new space in spaces where we |
| 911 | // do not expect them. |
| 912 | VerifyNonPointerSpacePointersVisitor v; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 913 | HeapObjectIterator code_it(HEAP->code_space()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 914 | for (HeapObject* object = code_it.next(); |
| 915 | object != NULL; object = code_it.next()) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 916 | object->Iterate(&v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 917 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 918 | HeapObjectIterator data_it(HEAP->old_data_space()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 919 | for (HeapObject* object = data_it.next(); |
| 920 | object != NULL; object = data_it.next()) |
| 921 | object->Iterate(&v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 922 | } |
| 923 | #endif |
| 924 | |
| 925 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 926 | void Heap::CheckNewSpaceExpansionCriteria() { |
| 927 | if (new_space_.Capacity() < new_space_.MaximumCapacity() && |
| 928 | survived_since_last_expansion_ > new_space_.Capacity()) { |
| 929 | // Grow the size of new space if there is room to grow and enough |
| 930 | // data has survived scavenge since the last expansion. |
| 931 | new_space_.Grow(); |
| 932 | survived_since_last_expansion_ = 0; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 937 | void Heap::Scavenge() { |
| 938 | #ifdef DEBUG |
| 939 | if (FLAG_enable_slow_asserts) VerifyNonPointerSpacePointers(); |
| 940 | #endif |
| 941 | |
| 942 | gc_state_ = SCAVENGE; |
| 943 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 944 | SwitchScavengingVisitorsTableIfProfilingWasEnabled(); |
| 945 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 946 | Page::FlipMeaningOfInvalidatedWatermarkFlag(this); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 947 | #ifdef DEBUG |
| 948 | VerifyPageWatermarkValidity(old_pointer_space_, ALL_VALID); |
| 949 | VerifyPageWatermarkValidity(map_space_, ALL_VALID); |
| 950 | #endif |
| 951 | |
| 952 | // We do not update an allocation watermark of the top page during linear |
| 953 | // allocation to avoid overhead. So to maintain the watermark invariant |
| 954 | // we have to manually cache the watermark and mark the top page as having an |
| 955 | // invalid watermark. This guarantees that dirty regions iteration will use a |
| 956 | // correct watermark even if a linear allocation happens. |
| 957 | old_pointer_space_->FlushTopPageWatermark(); |
| 958 | map_space_->FlushTopPageWatermark(); |
| 959 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 960 | // Implements Cheney's copying algorithm |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 961 | LOG(isolate_, ResourceEvent("scavenge", "begin")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 962 | |
| 963 | // Clear descriptor cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 964 | isolate_->descriptor_lookup_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 965 | |
| 966 | // Used for updating survived_since_last_expansion_ at function end. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 967 | intptr_t survived_watermark = PromotedSpaceSize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 968 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 969 | CheckNewSpaceExpansionCriteria(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 970 | |
| 971 | // Flip the semispaces. After flipping, to space is empty, from space has |
| 972 | // live objects. |
| 973 | new_space_.Flip(); |
| 974 | new_space_.ResetAllocationInfo(); |
| 975 | |
| 976 | // We need to sweep newly copied objects which can be either in the |
| 977 | // to space or promoted to the old generation. For to-space |
| 978 | // objects, we treat the bottom of the to space as a queue. Newly |
| 979 | // copied and unswept objects lie between a 'front' mark and the |
| 980 | // allocation pointer. |
| 981 | // |
| 982 | // Promoted objects can go into various old-generation spaces, and |
| 983 | // can be allocated internally in the spaces (from the free list). |
| 984 | // We treat the top of the to space as a queue of addresses of |
| 985 | // promoted objects. The addresses of newly promoted and unswept |
| 986 | // objects lie between a 'front' mark and a 'rear' mark that is |
| 987 | // updated as a side effect of promoting an object. |
| 988 | // |
| 989 | // There is guaranteed to be enough room at the top of the to space |
| 990 | // for the addresses of promoted objects: every object promoted |
| 991 | // frees up its size in bytes from the top of the new space, and |
| 992 | // objects are at least one pointer in size. |
| 993 | Address new_space_front = new_space_.ToSpaceLow(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 994 | promotion_queue_.Initialize(new_space_.ToSpaceHigh()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 995 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 996 | is_safe_to_read_maps_ = false; |
| 997 | ScavengeVisitor scavenge_visitor(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 998 | // Copy roots. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 999 | IterateRoots(&scavenge_visitor, VISIT_ALL_IN_SCAVENGE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Copy objects reachable from the old generation. By definition, |
| 1002 | // there are no intergenerational pointers in code or data spaces. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1003 | IterateDirtyRegions(old_pointer_space_, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1004 | &Heap::IteratePointersInDirtyRegion, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1005 | &ScavengePointer, |
| 1006 | WATERMARK_CAN_BE_INVALID); |
| 1007 | |
| 1008 | IterateDirtyRegions(map_space_, |
| 1009 | &IteratePointersInDirtyMapsRegion, |
| 1010 | &ScavengePointer, |
| 1011 | WATERMARK_CAN_BE_INVALID); |
| 1012 | |
| 1013 | lo_space_->IterateDirtyRegions(&ScavengePointer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1014 | |
| 1015 | // Copy objects reachable from cells by scavenging cell values directly. |
| 1016 | HeapObjectIterator cell_iterator(cell_space_); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1017 | for (HeapObject* cell = cell_iterator.next(); |
| 1018 | cell != NULL; cell = cell_iterator.next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1019 | if (cell->IsJSGlobalPropertyCell()) { |
| 1020 | Address value_address = |
| 1021 | reinterpret_cast<Address>(cell) + |
| 1022 | (JSGlobalPropertyCell::kValueOffset - kHeapObjectTag); |
| 1023 | scavenge_visitor.VisitPointer(reinterpret_cast<Object**>(value_address)); |
| 1024 | } |
| 1025 | } |
| 1026 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1027 | // Scavenge object reachable from the global contexts list directly. |
| 1028 | scavenge_visitor.VisitPointer(BitCast<Object**>(&global_contexts_list_)); |
| 1029 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1030 | new_space_front = DoScavenge(&scavenge_visitor, new_space_front); |
| 1031 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1032 | UpdateNewSpaceReferencesInExternalStringTable( |
| 1033 | &UpdateNewSpaceReferenceInExternalStringTableEntry); |
| 1034 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1035 | LiveObjectList::UpdateReferencesForScavengeGC(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1036 | isolate()->runtime_profiler()->UpdateSamplesAfterScavenge(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1037 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1038 | ASSERT(new_space_front == new_space_.top()); |
| 1039 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1040 | is_safe_to_read_maps_ = true; |
| 1041 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1042 | // Set age mark. |
| 1043 | new_space_.set_age_mark(new_space_.top()); |
| 1044 | |
| 1045 | // Update how much has survived scavenge. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1046 | IncrementYoungSurvivorsCounter(static_cast<int>( |
| 1047 | (PromotedSpaceSize() - survived_watermark) + new_space_.Size())); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1048 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1049 | LOG(isolate_, ResourceEvent("scavenge", "end")); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1050 | |
| 1051 | gc_state_ = NOT_IN_GC; |
| 1052 | } |
| 1053 | |
| 1054 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1055 | String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap, |
| 1056 | Object** p) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1057 | MapWord first_word = HeapObject::cast(*p)->map_word(); |
| 1058 | |
| 1059 | if (!first_word.IsForwardingAddress()) { |
| 1060 | // Unreachable external string can be finalized. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1061 | heap->FinalizeExternalString(String::cast(*p)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1062 | return NULL; |
| 1063 | } |
| 1064 | |
| 1065 | // String is still reachable. |
| 1066 | return String::cast(first_word.ToForwardingAddress()); |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | void Heap::UpdateNewSpaceReferencesInExternalStringTable( |
| 1071 | ExternalStringTableUpdaterCallback updater_func) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1072 | external_string_table_.Verify(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1073 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1074 | if (external_string_table_.new_space_strings_.is_empty()) return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1075 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1076 | Object** start = &external_string_table_.new_space_strings_[0]; |
| 1077 | Object** end = start + external_string_table_.new_space_strings_.length(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1078 | Object** last = start; |
| 1079 | |
| 1080 | for (Object** p = start; p < end; ++p) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1081 | ASSERT(InFromSpace(*p)); |
| 1082 | String* target = updater_func(this, p); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1083 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1084 | if (target == NULL) continue; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1085 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1086 | ASSERT(target->IsExternalString()); |
| 1087 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1088 | if (InNewSpace(target)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1089 | // String is still in new space. Update the table entry. |
| 1090 | *last = target; |
| 1091 | ++last; |
| 1092 | } else { |
| 1093 | // String got promoted. Move it to the old string list. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1094 | external_string_table_.AddOldString(target); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | ASSERT(last <= end); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1099 | external_string_table_.ShrinkNewStrings(static_cast<int>(last - start)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1103 | static Object* ProcessFunctionWeakReferences(Heap* heap, |
| 1104 | Object* function, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1105 | WeakObjectRetainer* retainer) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1106 | Object* head = heap->undefined_value(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1107 | JSFunction* tail = NULL; |
| 1108 | Object* candidate = function; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1109 | while (candidate != heap->undefined_value()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1110 | // Check whether to keep the candidate in the list. |
| 1111 | JSFunction* candidate_function = reinterpret_cast<JSFunction*>(candidate); |
| 1112 | Object* retain = retainer->RetainAs(candidate); |
| 1113 | if (retain != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1114 | if (head == heap->undefined_value()) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1115 | // First element in the list. |
| 1116 | head = candidate_function; |
| 1117 | } else { |
| 1118 | // Subsequent elements in the list. |
| 1119 | ASSERT(tail != NULL); |
| 1120 | tail->set_next_function_link(candidate_function); |
| 1121 | } |
| 1122 | // Retained function is new tail. |
| 1123 | tail = candidate_function; |
| 1124 | } |
| 1125 | // Move to next element in the list. |
| 1126 | candidate = candidate_function->next_function_link(); |
| 1127 | } |
| 1128 | |
| 1129 | // Terminate the list if there is one or more elements. |
| 1130 | if (tail != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1131 | tail->set_next_function_link(heap->undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | return head; |
| 1135 | } |
| 1136 | |
| 1137 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1138 | void Heap::ProcessWeakReferences(WeakObjectRetainer* retainer) { |
| 1139 | Object* head = undefined_value(); |
| 1140 | Context* tail = NULL; |
| 1141 | Object* candidate = global_contexts_list_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1142 | while (candidate != undefined_value()) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1143 | // Check whether to keep the candidate in the list. |
| 1144 | Context* candidate_context = reinterpret_cast<Context*>(candidate); |
| 1145 | Object* retain = retainer->RetainAs(candidate); |
| 1146 | if (retain != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1147 | if (head == undefined_value()) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1148 | // First element in the list. |
| 1149 | head = candidate_context; |
| 1150 | } else { |
| 1151 | // Subsequent elements in the list. |
| 1152 | ASSERT(tail != NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1153 | tail->set_unchecked(this, |
| 1154 | Context::NEXT_CONTEXT_LINK, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1155 | candidate_context, |
| 1156 | UPDATE_WRITE_BARRIER); |
| 1157 | } |
| 1158 | // Retained context is new tail. |
| 1159 | tail = candidate_context; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1160 | |
| 1161 | // Process the weak list of optimized functions for the context. |
| 1162 | Object* function_list_head = |
| 1163 | ProcessFunctionWeakReferences( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1164 | this, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1165 | candidate_context->get(Context::OPTIMIZED_FUNCTIONS_LIST), |
| 1166 | retainer); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1167 | candidate_context->set_unchecked(this, |
| 1168 | Context::OPTIMIZED_FUNCTIONS_LIST, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1169 | function_list_head, |
| 1170 | UPDATE_WRITE_BARRIER); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1171 | } |
| 1172 | // Move to next element in the list. |
| 1173 | candidate = candidate_context->get(Context::NEXT_CONTEXT_LINK); |
| 1174 | } |
| 1175 | |
| 1176 | // Terminate the list if there is one or more elements. |
| 1177 | if (tail != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1178 | tail->set_unchecked(this, |
| 1179 | Context::NEXT_CONTEXT_LINK, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1180 | Heap::undefined_value(), |
| 1181 | UPDATE_WRITE_BARRIER); |
| 1182 | } |
| 1183 | |
| 1184 | // Update the head of the list of contexts. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1185 | global_contexts_list_ = head; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1189 | class NewSpaceScavenger : public StaticNewSpaceVisitor<NewSpaceScavenger> { |
| 1190 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1191 | static inline void VisitPointer(Heap* heap, Object** p) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1192 | Object* object = *p; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1193 | if (!heap->InNewSpace(object)) return; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1194 | Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p), |
| 1195 | reinterpret_cast<HeapObject*>(object)); |
| 1196 | } |
| 1197 | }; |
| 1198 | |
| 1199 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1200 | Address Heap::DoScavenge(ObjectVisitor* scavenge_visitor, |
| 1201 | Address new_space_front) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1202 | do { |
| 1203 | ASSERT(new_space_front <= new_space_.top()); |
| 1204 | |
| 1205 | // The addresses new_space_front and new_space_.top() define a |
| 1206 | // queue of unprocessed copied objects. Process them until the |
| 1207 | // queue is empty. |
| 1208 | while (new_space_front < new_space_.top()) { |
| 1209 | HeapObject* object = HeapObject::FromAddress(new_space_front); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1210 | new_space_front += NewSpaceScavenger::IterateBody(object->map(), object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | // Promote and process all the to-be-promoted objects. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1214 | while (!promotion_queue_.is_empty()) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1215 | HeapObject* target; |
| 1216 | int size; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1217 | promotion_queue_.remove(&target, &size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1218 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1219 | // Promoted object might be already partially visited |
| 1220 | // during dirty regions iteration. Thus we search specificly |
| 1221 | // for pointers to from semispace instead of looking for pointers |
| 1222 | // to new space. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1223 | ASSERT(!target->IsMap()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1224 | IterateAndMarkPointersToFromSpace(target->address(), |
| 1225 | target->address() + size, |
| 1226 | &ScavengePointer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | // Take another spin if there are now unswept objects in new space |
| 1230 | // (there are currently no more unswept promoted objects). |
| 1231 | } while (new_space_front < new_space_.top()); |
| 1232 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1233 | return new_space_front; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1237 | enum LoggingAndProfiling { |
| 1238 | LOGGING_AND_PROFILING_ENABLED, |
| 1239 | LOGGING_AND_PROFILING_DISABLED |
| 1240 | }; |
| 1241 | |
| 1242 | |
| 1243 | typedef void (*ScavengingCallback)(Map* map, |
| 1244 | HeapObject** slot, |
| 1245 | HeapObject* object); |
| 1246 | |
| 1247 | |
| 1248 | static Atomic32 scavenging_visitors_table_mode_; |
| 1249 | static VisitorDispatchTable<ScavengingCallback> scavenging_visitors_table_; |
| 1250 | |
| 1251 | |
| 1252 | INLINE(static void DoScavengeObject(Map* map, |
| 1253 | HeapObject** slot, |
| 1254 | HeapObject* obj)); |
| 1255 | |
| 1256 | |
| 1257 | void DoScavengeObject(Map* map, HeapObject** slot, HeapObject* obj) { |
| 1258 | scavenging_visitors_table_.GetVisitor(map)(map, slot, obj); |
| 1259 | } |
| 1260 | |
| 1261 | |
| 1262 | template<LoggingAndProfiling logging_and_profiling_mode> |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1263 | class ScavengingVisitor : public StaticVisitorBase { |
| 1264 | public: |
| 1265 | static void Initialize() { |
| 1266 | table_.Register(kVisitSeqAsciiString, &EvacuateSeqAsciiString); |
| 1267 | table_.Register(kVisitSeqTwoByteString, &EvacuateSeqTwoByteString); |
| 1268 | table_.Register(kVisitShortcutCandidate, &EvacuateShortcutCandidate); |
| 1269 | table_.Register(kVisitByteArray, &EvacuateByteArray); |
| 1270 | table_.Register(kVisitFixedArray, &EvacuateFixedArray); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1271 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1272 | table_.Register(kVisitGlobalContext, |
| 1273 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1274 | template VisitSpecialized<Context::kSize>); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1275 | |
| 1276 | table_.Register(kVisitConsString, |
| 1277 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1278 | template VisitSpecialized<ConsString::kSize>); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1279 | |
| 1280 | table_.Register(kVisitSharedFunctionInfo, |
| 1281 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1282 | template VisitSpecialized<SharedFunctionInfo::kSize>); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1283 | |
| 1284 | table_.Register(kVisitJSFunction, |
| 1285 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1286 | template VisitSpecialized<JSFunction::kSize>); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1287 | |
| 1288 | table_.RegisterSpecializations<ObjectEvacuationStrategy<DATA_OBJECT>, |
| 1289 | kVisitDataObject, |
| 1290 | kVisitDataObjectGeneric>(); |
| 1291 | |
| 1292 | table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>, |
| 1293 | kVisitJSObject, |
| 1294 | kVisitJSObjectGeneric>(); |
| 1295 | |
| 1296 | table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>, |
| 1297 | kVisitStruct, |
| 1298 | kVisitStructGeneric>(); |
| 1299 | } |
| 1300 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1301 | static VisitorDispatchTable<ScavengingCallback>* GetTable() { |
| 1302 | return &table_; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1303 | } |
| 1304 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1305 | private: |
| 1306 | enum ObjectContents { DATA_OBJECT, POINTER_OBJECT }; |
| 1307 | enum SizeRestriction { SMALL, UNKNOWN_SIZE }; |
| 1308 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1309 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1310 | static void RecordCopiedObject(Heap* heap, HeapObject* obj) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1311 | bool should_record = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1312 | #ifdef DEBUG |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1313 | should_record = FLAG_heap_stats; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1314 | #endif |
| 1315 | #ifdef ENABLE_LOGGING_AND_PROFILING |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1316 | should_record = should_record || FLAG_log_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1317 | #endif |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1318 | if (should_record) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1319 | if (heap->new_space()->Contains(obj)) { |
| 1320 | heap->new_space()->RecordAllocation(obj); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1321 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1322 | heap->new_space()->RecordPromotion(obj); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1323 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1326 | #endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1327 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1328 | // Helper function used by CopyObject to copy a source object to an |
| 1329 | // allocated target object and update the forwarding pointer in the source |
| 1330 | // object. Returns the target object. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1331 | INLINE(static HeapObject* MigrateObject(Heap* heap, |
| 1332 | HeapObject* source, |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1333 | HeapObject* target, |
| 1334 | int size)) { |
| 1335 | // Copy the content of source to target. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1336 | heap->CopyBlock(target->address(), source->address(), size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1337 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1338 | // Set the forwarding address. |
| 1339 | source->set_map_word(MapWord::FromForwardingAddress(target)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1340 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1341 | if (logging_and_profiling_mode == LOGGING_AND_PROFILING_ENABLED) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1342 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1343 | // Update NewSpace stats if necessary. |
| 1344 | RecordCopiedObject(heap, target); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1345 | #endif |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1346 | HEAP_PROFILE(heap, ObjectMoveEvent(source->address(), target->address())); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1347 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1348 | Isolate* isolate = heap->isolate(); |
| 1349 | if (isolate->logger()->is_logging() || |
| 1350 | isolate->cpu_profiler()->is_profiling()) { |
| 1351 | if (target->IsSharedFunctionInfo()) { |
| 1352 | PROFILE(isolate, SharedFunctionInfoMoveEvent( |
| 1353 | source->address(), target->address())); |
| 1354 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1355 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1356 | #endif |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1359 | return target; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1360 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1361 | |
| 1362 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1363 | template<ObjectContents object_contents, SizeRestriction size_restriction> |
| 1364 | static inline void EvacuateObject(Map* map, |
| 1365 | HeapObject** slot, |
| 1366 | HeapObject* object, |
| 1367 | int object_size) { |
| 1368 | ASSERT((size_restriction != SMALL) || |
| 1369 | (object_size <= Page::kMaxHeapObjectSize)); |
| 1370 | ASSERT(object->Size() == object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1371 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1372 | Heap* heap = map->heap(); |
| 1373 | if (heap->ShouldBePromoted(object->address(), object_size)) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1374 | MaybeObject* maybe_result; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1375 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1376 | if ((size_restriction != SMALL) && |
| 1377 | (object_size > Page::kMaxHeapObjectSize)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1378 | maybe_result = heap->lo_space()->AllocateRawFixedArray(object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1379 | } else { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1380 | if (object_contents == DATA_OBJECT) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1381 | maybe_result = heap->old_data_space()->AllocateRaw(object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1382 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1383 | maybe_result = heap->old_pointer_space()->AllocateRaw(object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1384 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1385 | } |
| 1386 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1387 | Object* result = NULL; // Initialization to please compiler. |
| 1388 | if (maybe_result->ToObject(&result)) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1389 | HeapObject* target = HeapObject::cast(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1390 | *slot = MigrateObject(heap, object , target, object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1391 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1392 | if (object_contents == POINTER_OBJECT) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1393 | heap->promotion_queue()->insert(target, object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1394 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1395 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1396 | heap->tracer()->increment_promoted_objects_size(object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1397 | return; |
| 1398 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1399 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1400 | Object* result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1401 | heap->new_space()->AllocateRaw(object_size)->ToObjectUnchecked(); |
| 1402 | *slot = MigrateObject(heap, object, HeapObject::cast(result), object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1406 | |
| 1407 | static inline void EvacuateFixedArray(Map* map, |
| 1408 | HeapObject** slot, |
| 1409 | HeapObject* object) { |
| 1410 | int object_size = FixedArray::BodyDescriptor::SizeOf(map, object); |
| 1411 | EvacuateObject<POINTER_OBJECT, UNKNOWN_SIZE>(map, |
| 1412 | slot, |
| 1413 | object, |
| 1414 | object_size); |
| 1415 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1416 | |
| 1417 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1418 | static inline void EvacuateByteArray(Map* map, |
| 1419 | HeapObject** slot, |
| 1420 | HeapObject* object) { |
| 1421 | int object_size = reinterpret_cast<ByteArray*>(object)->ByteArraySize(); |
| 1422 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1423 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1424 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1425 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1426 | static inline void EvacuateSeqAsciiString(Map* map, |
| 1427 | HeapObject** slot, |
| 1428 | HeapObject* object) { |
| 1429 | int object_size = SeqAsciiString::cast(object)-> |
| 1430 | SeqAsciiStringSize(map->instance_type()); |
| 1431 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | static inline void EvacuateSeqTwoByteString(Map* map, |
| 1436 | HeapObject** slot, |
| 1437 | HeapObject* object) { |
| 1438 | int object_size = SeqTwoByteString::cast(object)-> |
| 1439 | SeqTwoByteStringSize(map->instance_type()); |
| 1440 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1441 | } |
| 1442 | |
| 1443 | |
| 1444 | static inline bool IsShortcutCandidate(int type) { |
| 1445 | return ((type & kShortcutTypeMask) == kShortcutTypeTag); |
| 1446 | } |
| 1447 | |
| 1448 | static inline void EvacuateShortcutCandidate(Map* map, |
| 1449 | HeapObject** slot, |
| 1450 | HeapObject* object) { |
| 1451 | ASSERT(IsShortcutCandidate(map->instance_type())); |
| 1452 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1453 | if (ConsString::cast(object)->unchecked_second() == |
| 1454 | map->heap()->empty_string()) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1455 | HeapObject* first = |
| 1456 | HeapObject::cast(ConsString::cast(object)->unchecked_first()); |
| 1457 | |
| 1458 | *slot = first; |
| 1459 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1460 | if (!map->heap()->InNewSpace(first)) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1461 | object->set_map_word(MapWord::FromForwardingAddress(first)); |
| 1462 | return; |
| 1463 | } |
| 1464 | |
| 1465 | MapWord first_word = first->map_word(); |
| 1466 | if (first_word.IsForwardingAddress()) { |
| 1467 | HeapObject* target = first_word.ToForwardingAddress(); |
| 1468 | |
| 1469 | *slot = target; |
| 1470 | object->set_map_word(MapWord::FromForwardingAddress(target)); |
| 1471 | return; |
| 1472 | } |
| 1473 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1474 | DoScavengeObject(first->map(), slot, first); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1475 | object->set_map_word(MapWord::FromForwardingAddress(*slot)); |
| 1476 | return; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1477 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1478 | |
| 1479 | int object_size = ConsString::kSize; |
| 1480 | EvacuateObject<POINTER_OBJECT, SMALL>(map, slot, object, object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1483 | template<ObjectContents object_contents> |
| 1484 | class ObjectEvacuationStrategy { |
| 1485 | public: |
| 1486 | template<int object_size> |
| 1487 | static inline void VisitSpecialized(Map* map, |
| 1488 | HeapObject** slot, |
| 1489 | HeapObject* object) { |
| 1490 | EvacuateObject<object_contents, SMALL>(map, slot, object, object_size); |
| 1491 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1492 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1493 | static inline void Visit(Map* map, |
| 1494 | HeapObject** slot, |
| 1495 | HeapObject* object) { |
| 1496 | int object_size = map->instance_size(); |
| 1497 | EvacuateObject<object_contents, SMALL>(map, slot, object, object_size); |
| 1498 | } |
| 1499 | }; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1500 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1501 | static VisitorDispatchTable<ScavengingCallback> table_; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1502 | }; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1503 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1504 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1505 | template<LoggingAndProfiling logging_and_profiling_mode> |
| 1506 | VisitorDispatchTable<ScavengingCallback> |
| 1507 | ScavengingVisitor<logging_and_profiling_mode>::table_; |
| 1508 | |
| 1509 | |
| 1510 | static void InitializeScavengingVisitorsTables() { |
| 1511 | ScavengingVisitor<LOGGING_AND_PROFILING_DISABLED>::Initialize(); |
| 1512 | ScavengingVisitor<LOGGING_AND_PROFILING_ENABLED>::Initialize(); |
| 1513 | scavenging_visitors_table_.CopyFrom( |
| 1514 | ScavengingVisitor<LOGGING_AND_PROFILING_DISABLED>::GetTable()); |
| 1515 | scavenging_visitors_table_mode_ = LOGGING_AND_PROFILING_DISABLED; |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | void Heap::SwitchScavengingVisitorsTableIfProfilingWasEnabled() { |
Ben Murdoch | bea578b | 2011-06-08 20:04:28 +0100 | [diff] [blame^] | 1520 | #ifdef ENABLE_LOGGING_AND_PROFILING |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1521 | if (scavenging_visitors_table_mode_ == LOGGING_AND_PROFILING_ENABLED) { |
| 1522 | // Table was already updated by some isolate. |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | if (isolate()->logger()->is_logging() || |
| 1527 | isolate()->cpu_profiler()->is_profiling() || |
| 1528 | (isolate()->heap_profiler() != NULL && |
| 1529 | isolate()->heap_profiler()->is_profiling())) { |
| 1530 | // If one of the isolates is doing scavenge at this moment of time |
| 1531 | // it might see this table in an inconsitent state when |
| 1532 | // some of the callbacks point to |
| 1533 | // ScavengingVisitor<LOGGING_AND_PROFILING_ENABLED> and others |
| 1534 | // to ScavengingVisitor<LOGGING_AND_PROFILING_DISABLED>. |
| 1535 | // However this does not lead to any bugs as such isolate does not have |
| 1536 | // profiling enabled and any isolate with enabled profiling is guaranteed |
| 1537 | // to see the table in the consistent state. |
| 1538 | scavenging_visitors_table_.CopyFrom( |
| 1539 | ScavengingVisitor<LOGGING_AND_PROFILING_ENABLED>::GetTable()); |
| 1540 | |
| 1541 | // We use Release_Store to prevent reordering of this write before writes |
| 1542 | // to the table. |
| 1543 | Release_Store(&scavenging_visitors_table_mode_, |
| 1544 | LOGGING_AND_PROFILING_ENABLED); |
| 1545 | } |
Ben Murdoch | bea578b | 2011-06-08 20:04:28 +0100 | [diff] [blame^] | 1546 | #endif |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1547 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1548 | |
| 1549 | |
| 1550 | void Heap::ScavengeObjectSlow(HeapObject** p, HeapObject* object) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1551 | ASSERT(HEAP->InFromSpace(object)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1552 | MapWord first_word = object->map_word(); |
| 1553 | ASSERT(!first_word.IsForwardingAddress()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1554 | Map* map = first_word.ToMap(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1555 | DoScavengeObject(map, p, object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1559 | MaybeObject* Heap::AllocatePartialMap(InstanceType instance_type, |
| 1560 | int instance_size) { |
| 1561 | Object* result; |
| 1562 | { MaybeObject* maybe_result = AllocateRawMap(); |
| 1563 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1564 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1565 | |
| 1566 | // Map::cast cannot be used due to uninitialized map field. |
| 1567 | reinterpret_cast<Map*>(result)->set_map(raw_unchecked_meta_map()); |
| 1568 | reinterpret_cast<Map*>(result)->set_instance_type(instance_type); |
| 1569 | reinterpret_cast<Map*>(result)->set_instance_size(instance_size); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1570 | reinterpret_cast<Map*>(result)->set_visitor_id( |
| 1571 | StaticVisitorBase::GetVisitorId(instance_type, instance_size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1572 | reinterpret_cast<Map*>(result)->set_inobject_properties(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1573 | reinterpret_cast<Map*>(result)->set_pre_allocated_property_fields(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1574 | reinterpret_cast<Map*>(result)->set_unused_property_fields(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1575 | reinterpret_cast<Map*>(result)->set_bit_field(0); |
| 1576 | reinterpret_cast<Map*>(result)->set_bit_field2(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1577 | return result; |
| 1578 | } |
| 1579 | |
| 1580 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1581 | MaybeObject* Heap::AllocateMap(InstanceType instance_type, int instance_size) { |
| 1582 | Object* result; |
| 1583 | { MaybeObject* maybe_result = AllocateRawMap(); |
| 1584 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1585 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1586 | |
| 1587 | Map* map = reinterpret_cast<Map*>(result); |
| 1588 | map->set_map(meta_map()); |
| 1589 | map->set_instance_type(instance_type); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1590 | map->set_visitor_id( |
| 1591 | StaticVisitorBase::GetVisitorId(instance_type, instance_size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1592 | map->set_prototype(null_value()); |
| 1593 | map->set_constructor(null_value()); |
| 1594 | map->set_instance_size(instance_size); |
| 1595 | map->set_inobject_properties(0); |
| 1596 | map->set_pre_allocated_property_fields(0); |
| 1597 | map->set_instance_descriptors(empty_descriptor_array()); |
| 1598 | map->set_code_cache(empty_fixed_array()); |
| 1599 | map->set_unused_property_fields(0); |
| 1600 | map->set_bit_field(0); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1601 | map->set_bit_field2((1 << Map::kIsExtensible) | (1 << Map::kHasFastElements)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1602 | |
| 1603 | // If the map object is aligned fill the padding area with Smi 0 objects. |
| 1604 | if (Map::kPadStart < Map::kSize) { |
| 1605 | memset(reinterpret_cast<byte*>(map) + Map::kPadStart - kHeapObjectTag, |
| 1606 | 0, |
| 1607 | Map::kSize - Map::kPadStart); |
| 1608 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1609 | return map; |
| 1610 | } |
| 1611 | |
| 1612 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1613 | MaybeObject* Heap::AllocateCodeCache() { |
| 1614 | Object* result; |
| 1615 | { MaybeObject* maybe_result = AllocateStruct(CODE_CACHE_TYPE); |
| 1616 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1617 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1618 | CodeCache* code_cache = CodeCache::cast(result); |
| 1619 | code_cache->set_default_cache(empty_fixed_array()); |
| 1620 | code_cache->set_normal_type_cache(undefined_value()); |
| 1621 | return code_cache; |
| 1622 | } |
| 1623 | |
| 1624 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1625 | const Heap::StringTypeTable Heap::string_type_table[] = { |
| 1626 | #define STRING_TYPE_ELEMENT(type, size, name, camel_name) \ |
| 1627 | {type, size, k##camel_name##MapRootIndex}, |
| 1628 | STRING_TYPE_LIST(STRING_TYPE_ELEMENT) |
| 1629 | #undef STRING_TYPE_ELEMENT |
| 1630 | }; |
| 1631 | |
| 1632 | |
| 1633 | const Heap::ConstantSymbolTable Heap::constant_symbol_table[] = { |
| 1634 | #define CONSTANT_SYMBOL_ELEMENT(name, contents) \ |
| 1635 | {contents, k##name##RootIndex}, |
| 1636 | SYMBOL_LIST(CONSTANT_SYMBOL_ELEMENT) |
| 1637 | #undef CONSTANT_SYMBOL_ELEMENT |
| 1638 | }; |
| 1639 | |
| 1640 | |
| 1641 | const Heap::StructTable Heap::struct_table[] = { |
| 1642 | #define STRUCT_TABLE_ELEMENT(NAME, Name, name) \ |
| 1643 | { NAME##_TYPE, Name::kSize, k##Name##MapRootIndex }, |
| 1644 | STRUCT_LIST(STRUCT_TABLE_ELEMENT) |
| 1645 | #undef STRUCT_TABLE_ELEMENT |
| 1646 | }; |
| 1647 | |
| 1648 | |
| 1649 | bool Heap::CreateInitialMaps() { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1650 | Object* obj; |
| 1651 | { MaybeObject* maybe_obj = AllocatePartialMap(MAP_TYPE, Map::kSize); |
| 1652 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1653 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1654 | // Map::cast cannot be used due to uninitialized map field. |
| 1655 | Map* new_meta_map = reinterpret_cast<Map*>(obj); |
| 1656 | set_meta_map(new_meta_map); |
| 1657 | new_meta_map->set_map(new_meta_map); |
| 1658 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1659 | { MaybeObject* maybe_obj = |
| 1660 | AllocatePartialMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1661 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1662 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1663 | set_fixed_array_map(Map::cast(obj)); |
| 1664 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1665 | { MaybeObject* maybe_obj = AllocatePartialMap(ODDBALL_TYPE, Oddball::kSize); |
| 1666 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1667 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1668 | set_oddball_map(Map::cast(obj)); |
| 1669 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1670 | // Allocate the empty array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1671 | { MaybeObject* maybe_obj = AllocateEmptyFixedArray(); |
| 1672 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1673 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1674 | set_empty_fixed_array(FixedArray::cast(obj)); |
| 1675 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1676 | { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_DATA_SPACE); |
| 1677 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1678 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1679 | set_null_value(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1680 | Oddball::cast(obj)->set_kind(Oddball::kNull); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1681 | |
| 1682 | // Allocate the empty descriptor array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1683 | { MaybeObject* maybe_obj = AllocateEmptyFixedArray(); |
| 1684 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1685 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1686 | set_empty_descriptor_array(DescriptorArray::cast(obj)); |
| 1687 | |
| 1688 | // Fix the instance_descriptors for the existing maps. |
| 1689 | meta_map()->set_instance_descriptors(empty_descriptor_array()); |
| 1690 | meta_map()->set_code_cache(empty_fixed_array()); |
| 1691 | |
| 1692 | fixed_array_map()->set_instance_descriptors(empty_descriptor_array()); |
| 1693 | fixed_array_map()->set_code_cache(empty_fixed_array()); |
| 1694 | |
| 1695 | oddball_map()->set_instance_descriptors(empty_descriptor_array()); |
| 1696 | oddball_map()->set_code_cache(empty_fixed_array()); |
| 1697 | |
| 1698 | // Fix prototype object for existing maps. |
| 1699 | meta_map()->set_prototype(null_value()); |
| 1700 | meta_map()->set_constructor(null_value()); |
| 1701 | |
| 1702 | fixed_array_map()->set_prototype(null_value()); |
| 1703 | fixed_array_map()->set_constructor(null_value()); |
| 1704 | |
| 1705 | oddball_map()->set_prototype(null_value()); |
| 1706 | oddball_map()->set_constructor(null_value()); |
| 1707 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1708 | { MaybeObject* maybe_obj = |
| 1709 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1710 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1711 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1712 | set_fixed_cow_array_map(Map::cast(obj)); |
| 1713 | ASSERT(fixed_array_map() != fixed_cow_array_map()); |
| 1714 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1715 | { MaybeObject* maybe_obj = AllocateMap(HEAP_NUMBER_TYPE, HeapNumber::kSize); |
| 1716 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1717 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1718 | set_heap_number_map(Map::cast(obj)); |
| 1719 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1720 | { MaybeObject* maybe_obj = AllocateMap(PROXY_TYPE, Proxy::kSize); |
| 1721 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1722 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1723 | set_proxy_map(Map::cast(obj)); |
| 1724 | |
| 1725 | for (unsigned i = 0; i < ARRAY_SIZE(string_type_table); i++) { |
| 1726 | const StringTypeTable& entry = string_type_table[i]; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1727 | { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size); |
| 1728 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1729 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1730 | roots_[entry.index] = Map::cast(obj); |
| 1731 | } |
| 1732 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1733 | { MaybeObject* maybe_obj = AllocateMap(STRING_TYPE, kVariableSizeSentinel); |
| 1734 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1735 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1736 | set_undetectable_string_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1737 | Map::cast(obj)->set_is_undetectable(); |
| 1738 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1739 | { MaybeObject* maybe_obj = |
| 1740 | AllocateMap(ASCII_STRING_TYPE, kVariableSizeSentinel); |
| 1741 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1742 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1743 | set_undetectable_ascii_string_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1744 | Map::cast(obj)->set_is_undetectable(); |
| 1745 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1746 | { MaybeObject* maybe_obj = |
| 1747 | AllocateMap(BYTE_ARRAY_TYPE, kVariableSizeSentinel); |
| 1748 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1749 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1750 | set_byte_array_map(Map::cast(obj)); |
| 1751 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1752 | { MaybeObject* maybe_obj = AllocateByteArray(0, TENURED); |
| 1753 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1754 | } |
| 1755 | set_empty_byte_array(ByteArray::cast(obj)); |
| 1756 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1757 | { MaybeObject* maybe_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1758 | AllocateMap(EXTERNAL_PIXEL_ARRAY_TYPE, ExternalArray::kAlignedSize); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1759 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1760 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1761 | set_external_pixel_array_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1762 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1763 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_BYTE_ARRAY_TYPE, |
| 1764 | ExternalArray::kAlignedSize); |
| 1765 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1766 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1767 | set_external_byte_array_map(Map::cast(obj)); |
| 1768 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1769 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE, |
| 1770 | ExternalArray::kAlignedSize); |
| 1771 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1772 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1773 | set_external_unsigned_byte_array_map(Map::cast(obj)); |
| 1774 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1775 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_SHORT_ARRAY_TYPE, |
| 1776 | ExternalArray::kAlignedSize); |
| 1777 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1778 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1779 | set_external_short_array_map(Map::cast(obj)); |
| 1780 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1781 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE, |
| 1782 | ExternalArray::kAlignedSize); |
| 1783 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1784 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1785 | set_external_unsigned_short_array_map(Map::cast(obj)); |
| 1786 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1787 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_INT_ARRAY_TYPE, |
| 1788 | ExternalArray::kAlignedSize); |
| 1789 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1790 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1791 | set_external_int_array_map(Map::cast(obj)); |
| 1792 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1793 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE, |
| 1794 | ExternalArray::kAlignedSize); |
| 1795 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1796 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1797 | set_external_unsigned_int_array_map(Map::cast(obj)); |
| 1798 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1799 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_FLOAT_ARRAY_TYPE, |
| 1800 | ExternalArray::kAlignedSize); |
| 1801 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1802 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1803 | set_external_float_array_map(Map::cast(obj)); |
| 1804 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1805 | { MaybeObject* maybe_obj = AllocateMap(CODE_TYPE, kVariableSizeSentinel); |
| 1806 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1807 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1808 | set_code_map(Map::cast(obj)); |
| 1809 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1810 | { MaybeObject* maybe_obj = AllocateMap(JS_GLOBAL_PROPERTY_CELL_TYPE, |
| 1811 | JSGlobalPropertyCell::kSize); |
| 1812 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1813 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1814 | set_global_property_cell_map(Map::cast(obj)); |
| 1815 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1816 | { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, kPointerSize); |
| 1817 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1818 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1819 | set_one_pointer_filler_map(Map::cast(obj)); |
| 1820 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1821 | { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, 2 * kPointerSize); |
| 1822 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1823 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1824 | set_two_pointer_filler_map(Map::cast(obj)); |
| 1825 | |
| 1826 | for (unsigned i = 0; i < ARRAY_SIZE(struct_table); i++) { |
| 1827 | const StructTable& entry = struct_table[i]; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1828 | { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size); |
| 1829 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1830 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1831 | roots_[entry.index] = Map::cast(obj); |
| 1832 | } |
| 1833 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1834 | { MaybeObject* maybe_obj = |
| 1835 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1836 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1837 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1838 | set_hash_table_map(Map::cast(obj)); |
| 1839 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1840 | { MaybeObject* maybe_obj = |
| 1841 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1842 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1843 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1844 | set_context_map(Map::cast(obj)); |
| 1845 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1846 | { MaybeObject* maybe_obj = |
| 1847 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1848 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1849 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1850 | set_catch_context_map(Map::cast(obj)); |
| 1851 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1852 | { MaybeObject* maybe_obj = |
| 1853 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1854 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1855 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1856 | Map* global_context_map = Map::cast(obj); |
| 1857 | global_context_map->set_visitor_id(StaticVisitorBase::kVisitGlobalContext); |
| 1858 | set_global_context_map(global_context_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1859 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1860 | { MaybeObject* maybe_obj = AllocateMap(SHARED_FUNCTION_INFO_TYPE, |
| 1861 | SharedFunctionInfo::kAlignedSize); |
| 1862 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1863 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1864 | set_shared_function_info_map(Map::cast(obj)); |
| 1865 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1866 | { MaybeObject* maybe_obj = AllocateMap(JS_MESSAGE_OBJECT_TYPE, |
| 1867 | JSMessageObject::kSize); |
| 1868 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1869 | } |
| 1870 | set_message_object_map(Map::cast(obj)); |
| 1871 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1872 | ASSERT(!InNewSpace(empty_fixed_array())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1873 | return true; |
| 1874 | } |
| 1875 | |
| 1876 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1877 | MaybeObject* Heap::AllocateHeapNumber(double value, PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1878 | // Statically ensure that it is safe to allocate heap numbers in paged |
| 1879 | // spaces. |
| 1880 | STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize); |
| 1881 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 1882 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1883 | Object* result; |
| 1884 | { MaybeObject* maybe_result = |
| 1885 | AllocateRaw(HeapNumber::kSize, space, OLD_DATA_SPACE); |
| 1886 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1887 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1888 | |
| 1889 | HeapObject::cast(result)->set_map(heap_number_map()); |
| 1890 | HeapNumber::cast(result)->set_value(value); |
| 1891 | return result; |
| 1892 | } |
| 1893 | |
| 1894 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1895 | MaybeObject* Heap::AllocateHeapNumber(double value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1896 | // Use general version, if we're forced to always allocate. |
| 1897 | if (always_allocate()) return AllocateHeapNumber(value, TENURED); |
| 1898 | |
| 1899 | // This version of AllocateHeapNumber is optimized for |
| 1900 | // allocation in new space. |
| 1901 | STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize); |
| 1902 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1903 | Object* result; |
| 1904 | { MaybeObject* maybe_result = new_space_.AllocateRaw(HeapNumber::kSize); |
| 1905 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1906 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1907 | HeapObject::cast(result)->set_map(heap_number_map()); |
| 1908 | HeapNumber::cast(result)->set_value(value); |
| 1909 | return result; |
| 1910 | } |
| 1911 | |
| 1912 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1913 | MaybeObject* Heap::AllocateJSGlobalPropertyCell(Object* value) { |
| 1914 | Object* result; |
| 1915 | { MaybeObject* maybe_result = AllocateRawCell(); |
| 1916 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1917 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1918 | HeapObject::cast(result)->set_map(global_property_cell_map()); |
| 1919 | JSGlobalPropertyCell::cast(result)->set_value(value); |
| 1920 | return result; |
| 1921 | } |
| 1922 | |
| 1923 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1924 | MaybeObject* Heap::CreateOddball(const char* to_string, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1925 | Object* to_number, |
| 1926 | byte kind) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1927 | Object* result; |
| 1928 | { MaybeObject* maybe_result = Allocate(oddball_map(), OLD_DATA_SPACE); |
| 1929 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1930 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1931 | return Oddball::cast(result)->Initialize(to_string, to_number, kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | |
| 1935 | bool Heap::CreateApiObjects() { |
| 1936 | Object* obj; |
| 1937 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1938 | { MaybeObject* maybe_obj = AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 1939 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1940 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1941 | set_neander_map(Map::cast(obj)); |
| 1942 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1943 | { MaybeObject* maybe_obj = AllocateJSObjectFromMap(neander_map()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1944 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1945 | } |
| 1946 | Object* elements; |
| 1947 | { MaybeObject* maybe_elements = AllocateFixedArray(2); |
| 1948 | if (!maybe_elements->ToObject(&elements)) return false; |
| 1949 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1950 | FixedArray::cast(elements)->set(0, Smi::FromInt(0)); |
| 1951 | JSObject::cast(obj)->set_elements(FixedArray::cast(elements)); |
| 1952 | set_message_listeners(JSObject::cast(obj)); |
| 1953 | |
| 1954 | return true; |
| 1955 | } |
| 1956 | |
| 1957 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1958 | void Heap::CreateJSEntryStub() { |
| 1959 | JSEntryStub stub; |
| 1960 | set_js_entry_code(*stub.GetCode()); |
| 1961 | } |
| 1962 | |
| 1963 | |
| 1964 | void Heap::CreateJSConstructEntryStub() { |
| 1965 | JSConstructEntryStub stub; |
| 1966 | set_js_construct_entry_code(*stub.GetCode()); |
| 1967 | } |
| 1968 | |
| 1969 | |
| 1970 | void Heap::CreateFixedStubs() { |
| 1971 | // Here we create roots for fixed stubs. They are needed at GC |
| 1972 | // for cooking and uncooking (check out frames.cc). |
| 1973 | // The eliminates the need for doing dictionary lookup in the |
| 1974 | // stub cache for these stubs. |
| 1975 | HandleScope scope; |
| 1976 | // gcc-4.4 has problem generating correct code of following snippet: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1977 | // { JSEntryStub stub; |
| 1978 | // js_entry_code_ = *stub.GetCode(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1979 | // } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1980 | // { JSConstructEntryStub stub; |
| 1981 | // js_construct_entry_code_ = *stub.GetCode(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1982 | // } |
| 1983 | // To workaround the problem, make separate functions without inlining. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1984 | Heap::CreateJSEntryStub(); |
| 1985 | Heap::CreateJSConstructEntryStub(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | bool Heap::CreateInitialObjects() { |
| 1990 | Object* obj; |
| 1991 | |
| 1992 | // The -0 value must be set before NumberFromDouble works. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1993 | { MaybeObject* maybe_obj = AllocateHeapNumber(-0.0, TENURED); |
| 1994 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1995 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1996 | set_minus_zero_value(obj); |
| 1997 | ASSERT(signbit(minus_zero_value()->Number()) != 0); |
| 1998 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1999 | { MaybeObject* maybe_obj = AllocateHeapNumber(OS::nan_value(), TENURED); |
| 2000 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2001 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2002 | set_nan_value(obj); |
| 2003 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2004 | { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_DATA_SPACE); |
| 2005 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2006 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2007 | set_undefined_value(obj); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2008 | Oddball::cast(obj)->set_kind(Oddball::kUndefined); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2009 | ASSERT(!InNewSpace(undefined_value())); |
| 2010 | |
| 2011 | // Allocate initial symbol table. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2012 | { MaybeObject* maybe_obj = SymbolTable::Allocate(kInitialSymbolTableSize); |
| 2013 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2014 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2015 | // Don't use set_symbol_table() due to asserts. |
| 2016 | roots_[kSymbolTableRootIndex] = obj; |
| 2017 | |
| 2018 | // Assign the print strings for oddballs after creating symboltable. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2019 | Object* symbol; |
| 2020 | { MaybeObject* maybe_symbol = LookupAsciiSymbol("undefined"); |
| 2021 | if (!maybe_symbol->ToObject(&symbol)) return false; |
| 2022 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2023 | Oddball::cast(undefined_value())->set_to_string(String::cast(symbol)); |
| 2024 | Oddball::cast(undefined_value())->set_to_number(nan_value()); |
| 2025 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2026 | // Allocate the null_value |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2027 | { MaybeObject* maybe_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2028 | Oddball::cast(null_value())->Initialize("null", |
| 2029 | Smi::FromInt(0), |
| 2030 | Oddball::kNull); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2031 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2032 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2033 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2034 | { MaybeObject* maybe_obj = CreateOddball("true", |
| 2035 | Smi::FromInt(1), |
| 2036 | Oddball::kTrue); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2037 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2038 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2039 | set_true_value(obj); |
| 2040 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2041 | { MaybeObject* maybe_obj = CreateOddball("false", |
| 2042 | Smi::FromInt(0), |
| 2043 | Oddball::kFalse); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2044 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2045 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2046 | set_false_value(obj); |
| 2047 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2048 | { MaybeObject* maybe_obj = CreateOddball("hole", |
| 2049 | Smi::FromInt(-1), |
| 2050 | Oddball::kTheHole); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2051 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2052 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2053 | set_the_hole_value(obj); |
| 2054 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2055 | { MaybeObject* maybe_obj = CreateOddball("arguments_marker", |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2056 | Smi::FromInt(-4), |
| 2057 | Oddball::kArgumentMarker); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2058 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2059 | } |
| 2060 | set_arguments_marker(obj); |
| 2061 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2062 | { MaybeObject* maybe_obj = CreateOddball("no_interceptor_result_sentinel", |
| 2063 | Smi::FromInt(-2), |
| 2064 | Oddball::kOther); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2065 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2066 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2067 | set_no_interceptor_result_sentinel(obj); |
| 2068 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2069 | { MaybeObject* maybe_obj = CreateOddball("termination_exception", |
| 2070 | Smi::FromInt(-3), |
| 2071 | Oddball::kOther); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2072 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2073 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2074 | set_termination_exception(obj); |
| 2075 | |
| 2076 | // Allocate the empty string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2077 | { MaybeObject* maybe_obj = AllocateRawAsciiString(0, TENURED); |
| 2078 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2079 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2080 | set_empty_string(String::cast(obj)); |
| 2081 | |
| 2082 | for (unsigned i = 0; i < ARRAY_SIZE(constant_symbol_table); i++) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2083 | { MaybeObject* maybe_obj = |
| 2084 | LookupAsciiSymbol(constant_symbol_table[i].contents); |
| 2085 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2086 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2087 | roots_[constant_symbol_table[i].index] = String::cast(obj); |
| 2088 | } |
| 2089 | |
| 2090 | // Allocate the hidden symbol which is used to identify the hidden properties |
| 2091 | // in JSObjects. The hash code has a special value so that it will not match |
| 2092 | // the empty string when searching for the property. It cannot be part of the |
| 2093 | // loop above because it needs to be allocated manually with the special |
| 2094 | // hash code in place. The hash code for the hidden_symbol is zero to ensure |
| 2095 | // that it will always be at the first entry in property descriptors. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2096 | { MaybeObject* maybe_obj = |
| 2097 | AllocateSymbol(CStrVector(""), 0, String::kZeroHash); |
| 2098 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2099 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2100 | hidden_symbol_ = String::cast(obj); |
| 2101 | |
| 2102 | // Allocate the proxy for __proto__. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2103 | { MaybeObject* maybe_obj = |
| 2104 | AllocateProxy((Address) &Accessors::ObjectPrototype); |
| 2105 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2106 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2107 | set_prototype_accessors(Proxy::cast(obj)); |
| 2108 | |
| 2109 | // Allocate the code_stubs dictionary. The initial size is set to avoid |
| 2110 | // expanding the dictionary during bootstrapping. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2111 | { MaybeObject* maybe_obj = NumberDictionary::Allocate(128); |
| 2112 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2113 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2114 | set_code_stubs(NumberDictionary::cast(obj)); |
| 2115 | |
| 2116 | // Allocate the non_monomorphic_cache used in stub-cache.cc. The initial size |
| 2117 | // is set to avoid expanding the dictionary during bootstrapping. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2118 | { MaybeObject* maybe_obj = NumberDictionary::Allocate(64); |
| 2119 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2120 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2121 | set_non_monomorphic_cache(NumberDictionary::cast(obj)); |
| 2122 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2123 | set_instanceof_cache_function(Smi::FromInt(0)); |
| 2124 | set_instanceof_cache_map(Smi::FromInt(0)); |
| 2125 | set_instanceof_cache_answer(Smi::FromInt(0)); |
| 2126 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2127 | CreateFixedStubs(); |
| 2128 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2129 | // Allocate the dictionary of intrinsic function names. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2130 | { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions); |
| 2131 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2132 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2133 | { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(this, |
| 2134 | obj); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2135 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2136 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2137 | set_intrinsic_function_names(StringDictionary::cast(obj)); |
| 2138 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2139 | if (InitializeNumberStringCache()->IsFailure()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2140 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2141 | // Allocate cache for single character ASCII strings. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2142 | { MaybeObject* maybe_obj = |
| 2143 | AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED); |
| 2144 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2145 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2146 | set_single_character_string_cache(FixedArray::cast(obj)); |
| 2147 | |
| 2148 | // Allocate cache for external strings pointing to native source code. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2149 | { MaybeObject* maybe_obj = AllocateFixedArray(Natives::GetBuiltinsCount()); |
| 2150 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2151 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2152 | set_natives_source_cache(FixedArray::cast(obj)); |
| 2153 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2154 | // Handling of script id generation is in FACTORY->NewScript. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2155 | set_last_script_id(undefined_value()); |
| 2156 | |
| 2157 | // Initialize keyed lookup cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2158 | isolate_->keyed_lookup_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Initialize context slot cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2161 | isolate_->context_slot_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2162 | |
| 2163 | // Initialize descriptor cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2164 | isolate_->descriptor_lookup_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2165 | |
| 2166 | // Initialize compilation cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2167 | isolate_->compilation_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2168 | |
| 2169 | return true; |
| 2170 | } |
| 2171 | |
| 2172 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2173 | MaybeObject* Heap::InitializeNumberStringCache() { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2174 | // Compute the size of the number string cache based on the max heap size. |
| 2175 | // max_semispace_size_ == 512 KB => number_string_cache_size = 32. |
| 2176 | // max_semispace_size_ == 8 MB => number_string_cache_size = 16KB. |
| 2177 | int number_string_cache_size = max_semispace_size_ / 512; |
| 2178 | number_string_cache_size = Max(32, Min(16*KB, number_string_cache_size)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2179 | Object* obj; |
| 2180 | MaybeObject* maybe_obj = |
| 2181 | AllocateFixedArray(number_string_cache_size * 2, TENURED); |
| 2182 | if (maybe_obj->ToObject(&obj)) set_number_string_cache(FixedArray::cast(obj)); |
| 2183 | return maybe_obj; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | |
| 2187 | void Heap::FlushNumberStringCache() { |
| 2188 | // Flush the number to string cache. |
| 2189 | int len = number_string_cache()->length(); |
| 2190 | for (int i = 0; i < len; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2191 | number_string_cache()->set_undefined(this, i); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2196 | static inline int double_get_hash(double d) { |
| 2197 | DoubleRepresentation rep(d); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2198 | return static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | |
| 2202 | static inline int smi_get_hash(Smi* smi) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2203 | return smi->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2207 | Object* Heap::GetNumberStringCache(Object* number) { |
| 2208 | int hash; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2209 | int mask = (number_string_cache()->length() >> 1) - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2210 | if (number->IsSmi()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2211 | hash = smi_get_hash(Smi::cast(number)) & mask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2212 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2213 | hash = double_get_hash(number->Number()) & mask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2214 | } |
| 2215 | Object* key = number_string_cache()->get(hash * 2); |
| 2216 | if (key == number) { |
| 2217 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
| 2218 | } else if (key->IsHeapNumber() && |
| 2219 | number->IsHeapNumber() && |
| 2220 | key->Number() == number->Number()) { |
| 2221 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
| 2222 | } |
| 2223 | return undefined_value(); |
| 2224 | } |
| 2225 | |
| 2226 | |
| 2227 | void Heap::SetNumberStringCache(Object* number, String* string) { |
| 2228 | int hash; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2229 | int mask = (number_string_cache()->length() >> 1) - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2230 | if (number->IsSmi()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2231 | hash = smi_get_hash(Smi::cast(number)) & mask; |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2232 | number_string_cache()->set(hash * 2, Smi::cast(number)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2233 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2234 | hash = double_get_hash(number->Number()) & mask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2235 | number_string_cache()->set(hash * 2, number); |
| 2236 | } |
| 2237 | number_string_cache()->set(hash * 2 + 1, string); |
| 2238 | } |
| 2239 | |
| 2240 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2241 | MaybeObject* Heap::NumberToString(Object* number, |
| 2242 | bool check_number_string_cache) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2243 | isolate_->counters()->number_to_string_runtime()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2244 | if (check_number_string_cache) { |
| 2245 | Object* cached = GetNumberStringCache(number); |
| 2246 | if (cached != undefined_value()) { |
| 2247 | return cached; |
| 2248 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | char arr[100]; |
| 2252 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 2253 | const char* str; |
| 2254 | if (number->IsSmi()) { |
| 2255 | int num = Smi::cast(number)->value(); |
| 2256 | str = IntToCString(num, buffer); |
| 2257 | } else { |
| 2258 | double num = HeapNumber::cast(number)->value(); |
| 2259 | str = DoubleToCString(num, buffer); |
| 2260 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2261 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2262 | Object* js_string; |
| 2263 | MaybeObject* maybe_js_string = AllocateStringFromAscii(CStrVector(str)); |
| 2264 | if (maybe_js_string->ToObject(&js_string)) { |
| 2265 | SetNumberStringCache(number, String::cast(js_string)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2266 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2267 | return maybe_js_string; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2271 | Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) { |
| 2272 | return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]); |
| 2273 | } |
| 2274 | |
| 2275 | |
| 2276 | Heap::RootListIndex Heap::RootIndexForExternalArrayType( |
| 2277 | ExternalArrayType array_type) { |
| 2278 | switch (array_type) { |
| 2279 | case kExternalByteArray: |
| 2280 | return kExternalByteArrayMapRootIndex; |
| 2281 | case kExternalUnsignedByteArray: |
| 2282 | return kExternalUnsignedByteArrayMapRootIndex; |
| 2283 | case kExternalShortArray: |
| 2284 | return kExternalShortArrayMapRootIndex; |
| 2285 | case kExternalUnsignedShortArray: |
| 2286 | return kExternalUnsignedShortArrayMapRootIndex; |
| 2287 | case kExternalIntArray: |
| 2288 | return kExternalIntArrayMapRootIndex; |
| 2289 | case kExternalUnsignedIntArray: |
| 2290 | return kExternalUnsignedIntArrayMapRootIndex; |
| 2291 | case kExternalFloatArray: |
| 2292 | return kExternalFloatArrayMapRootIndex; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2293 | case kExternalPixelArray: |
| 2294 | return kExternalPixelArrayMapRootIndex; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2295 | default: |
| 2296 | UNREACHABLE(); |
| 2297 | return kUndefinedValueRootIndex; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2302 | MaybeObject* Heap::NumberFromDouble(double value, PretenureFlag pretenure) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2303 | // We need to distinguish the minus zero value and this cannot be |
| 2304 | // done after conversion to int. Doing this by comparing bit |
| 2305 | // patterns is faster than using fpclassify() et al. |
| 2306 | static const DoubleRepresentation minus_zero(-0.0); |
| 2307 | |
| 2308 | DoubleRepresentation rep(value); |
| 2309 | if (rep.bits == minus_zero.bits) { |
| 2310 | return AllocateHeapNumber(-0.0, pretenure); |
| 2311 | } |
| 2312 | |
| 2313 | int int_value = FastD2I(value); |
| 2314 | if (value == int_value && Smi::IsValid(int_value)) { |
| 2315 | return Smi::FromInt(int_value); |
| 2316 | } |
| 2317 | |
| 2318 | // Materialize the value in the heap. |
| 2319 | return AllocateHeapNumber(value, pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2323 | MaybeObject* Heap::AllocateProxy(Address proxy, PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2324 | // Statically ensure that it is safe to allocate proxies in paged spaces. |
| 2325 | STATIC_ASSERT(Proxy::kSize <= Page::kMaxHeapObjectSize); |
| 2326 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2327 | Object* result; |
| 2328 | { MaybeObject* maybe_result = Allocate(proxy_map(), space); |
| 2329 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2330 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2331 | |
| 2332 | Proxy::cast(result)->set_proxy(proxy); |
| 2333 | return result; |
| 2334 | } |
| 2335 | |
| 2336 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2337 | MaybeObject* Heap::AllocateSharedFunctionInfo(Object* name) { |
| 2338 | Object* result; |
| 2339 | { MaybeObject* maybe_result = |
| 2340 | Allocate(shared_function_info_map(), OLD_POINTER_SPACE); |
| 2341 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2342 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2343 | |
| 2344 | SharedFunctionInfo* share = SharedFunctionInfo::cast(result); |
| 2345 | share->set_name(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2346 | Code* illegal = isolate_->builtins()->builtin(Builtins::kIllegal); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2347 | share->set_code(illegal); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2348 | share->set_scope_info(SerializedScopeInfo::Empty()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2349 | Code* construct_stub = isolate_->builtins()->builtin( |
| 2350 | Builtins::kJSConstructStubGeneric); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2351 | share->set_construct_stub(construct_stub); |
| 2352 | share->set_expected_nof_properties(0); |
| 2353 | share->set_length(0); |
| 2354 | share->set_formal_parameter_count(0); |
| 2355 | share->set_instance_class_name(Object_symbol()); |
| 2356 | share->set_function_data(undefined_value()); |
| 2357 | share->set_script(undefined_value()); |
| 2358 | share->set_start_position_and_type(0); |
| 2359 | share->set_debug_info(undefined_value()); |
| 2360 | share->set_inferred_name(empty_string()); |
| 2361 | share->set_compiler_hints(0); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2362 | share->set_deopt_counter(Smi::FromInt(FLAG_deopt_every_n_times)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2363 | share->set_initial_map(undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2364 | share->set_this_property_assignments_count(0); |
| 2365 | share->set_this_property_assignments(undefined_value()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2366 | share->set_opt_count(0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2367 | share->set_num_literals(0); |
| 2368 | share->set_end_position(0); |
| 2369 | share->set_function_token_position(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2370 | return result; |
| 2371 | } |
| 2372 | |
| 2373 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2374 | MaybeObject* Heap::AllocateJSMessageObject(String* type, |
| 2375 | JSArray* arguments, |
| 2376 | int start_position, |
| 2377 | int end_position, |
| 2378 | Object* script, |
| 2379 | Object* stack_trace, |
| 2380 | Object* stack_frames) { |
| 2381 | Object* result; |
| 2382 | { MaybeObject* maybe_result = Allocate(message_object_map(), NEW_SPACE); |
| 2383 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2384 | } |
| 2385 | JSMessageObject* message = JSMessageObject::cast(result); |
| 2386 | message->set_properties(Heap::empty_fixed_array()); |
| 2387 | message->set_elements(Heap::empty_fixed_array()); |
| 2388 | message->set_type(type); |
| 2389 | message->set_arguments(arguments); |
| 2390 | message->set_start_position(start_position); |
| 2391 | message->set_end_position(end_position); |
| 2392 | message->set_script(script); |
| 2393 | message->set_stack_trace(stack_trace); |
| 2394 | message->set_stack_frames(stack_frames); |
| 2395 | return result; |
| 2396 | } |
| 2397 | |
| 2398 | |
| 2399 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2400 | // Returns true for a character in a range. Both limits are inclusive. |
| 2401 | static inline bool Between(uint32_t character, uint32_t from, uint32_t to) { |
| 2402 | // This makes uses of the the unsigned wraparound. |
| 2403 | return character - from <= to - from; |
| 2404 | } |
| 2405 | |
| 2406 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2407 | MUST_USE_RESULT static inline MaybeObject* MakeOrFindTwoCharacterString( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2408 | Heap* heap, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2409 | uint32_t c1, |
| 2410 | uint32_t c2) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2411 | String* symbol; |
| 2412 | // Numeric strings have a different hash algorithm not known by |
| 2413 | // LookupTwoCharsSymbolIfExists, so we skip this step for such strings. |
| 2414 | if ((!Between(c1, '0', '9') || !Between(c2, '0', '9')) && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2415 | heap->symbol_table()->LookupTwoCharsSymbolIfExists(c1, c2, &symbol)) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2416 | return symbol; |
| 2417 | // Now we know the length is 2, we might as well make use of that fact |
| 2418 | // when building the new string. |
| 2419 | } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this |
| 2420 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2421 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2422 | { MaybeObject* maybe_result = heap->AllocateRawAsciiString(2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2423 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2424 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2425 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 2426 | dest[0] = c1; |
| 2427 | dest[1] = c2; |
| 2428 | return result; |
| 2429 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2430 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2431 | { MaybeObject* maybe_result = heap->AllocateRawTwoByteString(2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2432 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2433 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2434 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 2435 | dest[0] = c1; |
| 2436 | dest[1] = c2; |
| 2437 | return result; |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2442 | MaybeObject* Heap::AllocateConsString(String* first, String* second) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2443 | int first_length = first->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2444 | if (first_length == 0) { |
| 2445 | return second; |
| 2446 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2447 | |
| 2448 | int second_length = second->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2449 | if (second_length == 0) { |
| 2450 | return first; |
| 2451 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2452 | |
| 2453 | int length = first_length + second_length; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2454 | |
| 2455 | // Optimization for 2-byte strings often used as keys in a decompression |
| 2456 | // dictionary. Check whether we already have the string in the symbol |
| 2457 | // table to prevent creation of many unneccesary strings. |
| 2458 | if (length == 2) { |
| 2459 | unsigned c1 = first->Get(0); |
| 2460 | unsigned c2 = second->Get(0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2461 | return MakeOrFindTwoCharacterString(this, c1, c2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2464 | bool first_is_ascii = first->IsAsciiRepresentation(); |
| 2465 | bool second_is_ascii = second->IsAsciiRepresentation(); |
| 2466 | bool is_ascii = first_is_ascii && second_is_ascii; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2467 | |
| 2468 | // Make sure that an out of memory exception is thrown if the length |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2469 | // of the new cons string is too large. |
| 2470 | if (length > String::kMaxLength || length < 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2471 | isolate()->context()->mark_out_of_memory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2472 | return Failure::OutOfMemoryException(); |
| 2473 | } |
| 2474 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2475 | bool is_ascii_data_in_two_byte_string = false; |
| 2476 | if (!is_ascii) { |
| 2477 | // At least one of the strings uses two-byte representation so we |
| 2478 | // can't use the fast case code for short ascii strings below, but |
| 2479 | // we can try to save memory if all chars actually fit in ascii. |
| 2480 | is_ascii_data_in_two_byte_string = |
| 2481 | first->HasOnlyAsciiChars() && second->HasOnlyAsciiChars(); |
| 2482 | if (is_ascii_data_in_two_byte_string) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2483 | isolate_->counters()->string_add_runtime_ext_to_ascii()->Increment(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2487 | // If the resulting string is small make a flat string. |
| 2488 | if (length < String::kMinNonFlatLength) { |
| 2489 | ASSERT(first->IsFlat()); |
| 2490 | ASSERT(second->IsFlat()); |
| 2491 | if (is_ascii) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2492 | Object* result; |
| 2493 | { MaybeObject* maybe_result = AllocateRawAsciiString(length); |
| 2494 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2495 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2496 | // Copy the characters into the new object. |
| 2497 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 2498 | // Copy first part. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2499 | const char* src; |
| 2500 | if (first->IsExternalString()) { |
| 2501 | src = ExternalAsciiString::cast(first)->resource()->data(); |
| 2502 | } else { |
| 2503 | src = SeqAsciiString::cast(first)->GetChars(); |
| 2504 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2505 | for (int i = 0; i < first_length; i++) *dest++ = src[i]; |
| 2506 | // Copy second part. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2507 | if (second->IsExternalString()) { |
| 2508 | src = ExternalAsciiString::cast(second)->resource()->data(); |
| 2509 | } else { |
| 2510 | src = SeqAsciiString::cast(second)->GetChars(); |
| 2511 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2512 | for (int i = 0; i < second_length; i++) *dest++ = src[i]; |
| 2513 | return result; |
| 2514 | } else { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2515 | if (is_ascii_data_in_two_byte_string) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2516 | Object* result; |
| 2517 | { MaybeObject* maybe_result = AllocateRawAsciiString(length); |
| 2518 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2519 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2520 | // Copy the characters into the new object. |
| 2521 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 2522 | String::WriteToFlat(first, dest, 0, first_length); |
| 2523 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2524 | isolate_->counters()->string_add_runtime_ext_to_ascii()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2525 | return result; |
| 2526 | } |
| 2527 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2528 | Object* result; |
| 2529 | { MaybeObject* maybe_result = AllocateRawTwoByteString(length); |
| 2530 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2531 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2532 | // Copy the characters into the new object. |
| 2533 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 2534 | String::WriteToFlat(first, dest, 0, first_length); |
| 2535 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
| 2536 | return result; |
| 2537 | } |
| 2538 | } |
| 2539 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2540 | Map* map = (is_ascii || is_ascii_data_in_two_byte_string) ? |
| 2541 | cons_ascii_string_map() : cons_string_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2542 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2543 | Object* result; |
| 2544 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 2545 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2546 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2547 | |
| 2548 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2549 | ConsString* cons_string = ConsString::cast(result); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2550 | WriteBarrierMode mode = cons_string->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2551 | cons_string->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2552 | cons_string->set_hash_field(String::kEmptyHashField); |
| 2553 | cons_string->set_first(first, mode); |
| 2554 | cons_string->set_second(second, mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2555 | return result; |
| 2556 | } |
| 2557 | |
| 2558 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2559 | MaybeObject* Heap::AllocateSubString(String* buffer, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2560 | int start, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2561 | int end, |
| 2562 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2563 | int length = end - start; |
| 2564 | |
| 2565 | if (length == 1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2566 | return LookupSingleCharacterStringFromCode(buffer->Get(start)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2567 | } else if (length == 2) { |
| 2568 | // Optimization for 2-byte strings often used as keys in a decompression |
| 2569 | // dictionary. Check whether we already have the string in the symbol |
| 2570 | // table to prevent creation of many unneccesary strings. |
| 2571 | unsigned c1 = buffer->Get(start); |
| 2572 | unsigned c2 = buffer->Get(start + 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2573 | return MakeOrFindTwoCharacterString(this, c1, c2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | // Make an attempt to flatten the buffer to reduce access time. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 2577 | buffer = buffer->TryFlattenGetString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2578 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2579 | Object* result; |
| 2580 | { MaybeObject* maybe_result = buffer->IsAsciiRepresentation() |
| 2581 | ? AllocateRawAsciiString(length, pretenure ) |
| 2582 | : AllocateRawTwoByteString(length, pretenure); |
| 2583 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2584 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2585 | String* string_result = String::cast(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2586 | // Copy the characters into the new object. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2587 | if (buffer->IsAsciiRepresentation()) { |
| 2588 | ASSERT(string_result->IsAsciiRepresentation()); |
| 2589 | char* dest = SeqAsciiString::cast(string_result)->GetChars(); |
| 2590 | String::WriteToFlat(buffer, dest, start, end); |
| 2591 | } else { |
| 2592 | ASSERT(string_result->IsTwoByteRepresentation()); |
| 2593 | uc16* dest = SeqTwoByteString::cast(string_result)->GetChars(); |
| 2594 | String::WriteToFlat(buffer, dest, start, end); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2595 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2596 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2597 | return result; |
| 2598 | } |
| 2599 | |
| 2600 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2601 | MaybeObject* Heap::AllocateExternalStringFromAscii( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2602 | ExternalAsciiString::Resource* resource) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2603 | size_t length = resource->length(); |
| 2604 | if (length > static_cast<size_t>(String::kMaxLength)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2605 | isolate()->context()->mark_out_of_memory(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2606 | return Failure::OutOfMemoryException(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2609 | Map* map = external_ascii_string_map(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2610 | Object* result; |
| 2611 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 2612 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2613 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2614 | |
| 2615 | ExternalAsciiString* external_string = ExternalAsciiString::cast(result); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2616 | external_string->set_length(static_cast<int>(length)); |
| 2617 | external_string->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2618 | external_string->set_resource(resource); |
| 2619 | |
| 2620 | return result; |
| 2621 | } |
| 2622 | |
| 2623 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2624 | MaybeObject* Heap::AllocateExternalStringFromTwoByte( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2625 | ExternalTwoByteString::Resource* resource) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2626 | size_t length = resource->length(); |
| 2627 | if (length > static_cast<size_t>(String::kMaxLength)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2628 | isolate()->context()->mark_out_of_memory(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2629 | return Failure::OutOfMemoryException(); |
| 2630 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2631 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2632 | // For small strings we check whether the resource contains only |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 2633 | // ASCII characters. If yes, we use a different string map. |
| 2634 | static const size_t kAsciiCheckLengthLimit = 32; |
| 2635 | bool is_ascii = length <= kAsciiCheckLengthLimit && |
| 2636 | String::IsAscii(resource->data(), static_cast<int>(length)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2637 | Map* map = is_ascii ? |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2638 | external_string_with_ascii_data_map() : external_string_map(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2639 | Object* result; |
| 2640 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 2641 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2642 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2643 | |
| 2644 | ExternalTwoByteString* external_string = ExternalTwoByteString::cast(result); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2645 | external_string->set_length(static_cast<int>(length)); |
| 2646 | external_string->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2647 | external_string->set_resource(resource); |
| 2648 | |
| 2649 | return result; |
| 2650 | } |
| 2651 | |
| 2652 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2653 | MaybeObject* Heap::LookupSingleCharacterStringFromCode(uint16_t code) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2654 | if (code <= String::kMaxAsciiCharCode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2655 | Object* value = single_character_string_cache()->get(code); |
| 2656 | if (value != undefined_value()) return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2657 | |
| 2658 | char buffer[1]; |
| 2659 | buffer[0] = static_cast<char>(code); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2660 | Object* result; |
| 2661 | MaybeObject* maybe_result = LookupSymbol(Vector<const char>(buffer, 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2662 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2663 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2664 | single_character_string_cache()->set(code, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2665 | return result; |
| 2666 | } |
| 2667 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2668 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2669 | { MaybeObject* maybe_result = AllocateRawTwoByteString(1); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2670 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2671 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2672 | String* answer = String::cast(result); |
| 2673 | answer->Set(0, code); |
| 2674 | return answer; |
| 2675 | } |
| 2676 | |
| 2677 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2678 | MaybeObject* Heap::AllocateByteArray(int length, PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2679 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 2680 | return Failure::OutOfMemoryException(); |
| 2681 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2682 | if (pretenure == NOT_TENURED) { |
| 2683 | return AllocateByteArray(length); |
| 2684 | } |
| 2685 | int size = ByteArray::SizeFor(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2686 | Object* result; |
| 2687 | { MaybeObject* maybe_result = (size <= MaxObjectSizeInPagedSpace()) |
| 2688 | ? old_data_space_->AllocateRaw(size) |
| 2689 | : lo_space_->AllocateRaw(size); |
| 2690 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2691 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2692 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2693 | reinterpret_cast<ByteArray*>(result)->set_map(byte_array_map()); |
| 2694 | reinterpret_cast<ByteArray*>(result)->set_length(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2695 | return result; |
| 2696 | } |
| 2697 | |
| 2698 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2699 | MaybeObject* Heap::AllocateByteArray(int length) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2700 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 2701 | return Failure::OutOfMemoryException(); |
| 2702 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2703 | int size = ByteArray::SizeFor(length); |
| 2704 | AllocationSpace space = |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2705 | (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : NEW_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2706 | Object* result; |
| 2707 | { MaybeObject* maybe_result = AllocateRaw(size, space, OLD_DATA_SPACE); |
| 2708 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2709 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2710 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2711 | reinterpret_cast<ByteArray*>(result)->set_map(byte_array_map()); |
| 2712 | reinterpret_cast<ByteArray*>(result)->set_length(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2713 | return result; |
| 2714 | } |
| 2715 | |
| 2716 | |
| 2717 | void Heap::CreateFillerObjectAt(Address addr, int size) { |
| 2718 | if (size == 0) return; |
| 2719 | HeapObject* filler = HeapObject::FromAddress(addr); |
| 2720 | if (size == kPointerSize) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2721 | filler->set_map(one_pointer_filler_map()); |
| 2722 | } else if (size == 2 * kPointerSize) { |
| 2723 | filler->set_map(two_pointer_filler_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2724 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2725 | filler->set_map(byte_array_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2726 | ByteArray::cast(filler)->set_length(ByteArray::LengthFor(size)); |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2731 | MaybeObject* Heap::AllocateExternalArray(int length, |
| 2732 | ExternalArrayType array_type, |
| 2733 | void* external_pointer, |
| 2734 | PretenureFlag pretenure) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2735 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2736 | Object* result; |
| 2737 | { MaybeObject* maybe_result = AllocateRaw(ExternalArray::kAlignedSize, |
| 2738 | space, |
| 2739 | OLD_DATA_SPACE); |
| 2740 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2741 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2742 | |
| 2743 | reinterpret_cast<ExternalArray*>(result)->set_map( |
| 2744 | MapForExternalArrayType(array_type)); |
| 2745 | reinterpret_cast<ExternalArray*>(result)->set_length(length); |
| 2746 | reinterpret_cast<ExternalArray*>(result)->set_external_pointer( |
| 2747 | external_pointer); |
| 2748 | |
| 2749 | return result; |
| 2750 | } |
| 2751 | |
| 2752 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2753 | MaybeObject* Heap::CreateCode(const CodeDesc& desc, |
| 2754 | Code::Flags flags, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2755 | Handle<Object> self_reference, |
| 2756 | bool immovable) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2757 | // Allocate ByteArray before the Code object, so that we do not risk |
| 2758 | // leaving uninitialized Code object (and breaking the heap). |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2759 | Object* reloc_info; |
| 2760 | { MaybeObject* maybe_reloc_info = AllocateByteArray(desc.reloc_size, TENURED); |
| 2761 | if (!maybe_reloc_info->ToObject(&reloc_info)) return maybe_reloc_info; |
| 2762 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2763 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2764 | // Compute size. |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2765 | int body_size = RoundUp(desc.instr_size, kObjectAlignment); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2766 | int obj_size = Code::SizeFor(body_size); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2767 | ASSERT(IsAligned(static_cast<intptr_t>(obj_size), kCodeAlignment)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2768 | MaybeObject* maybe_result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2769 | // Large code objects and code objects which should stay at a fixed address |
| 2770 | // are allocated in large object space. |
| 2771 | if (obj_size > MaxObjectSizeInPagedSpace() || immovable) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2772 | maybe_result = lo_space_->AllocateRawCode(obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2773 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2774 | maybe_result = code_space_->AllocateRaw(obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2777 | Object* result; |
| 2778 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2779 | |
| 2780 | // Initialize the object |
| 2781 | HeapObject::cast(result)->set_map(code_map()); |
| 2782 | Code* code = Code::cast(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2783 | ASSERT(!isolate_->code_range()->exists() || |
| 2784 | isolate_->code_range()->contains(code->address())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2785 | code->set_instruction_size(desc.instr_size); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2786 | code->set_relocation_info(ByteArray::cast(reloc_info)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2787 | code->set_flags(flags); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 2788 | if (code->is_call_stub() || code->is_keyed_call_stub()) { |
| 2789 | code->set_check_type(RECEIVER_MAP_CHECK); |
| 2790 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2791 | code->set_deoptimization_data(empty_fixed_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2792 | // Allow self references to created code object by patching the handle to |
| 2793 | // point to the newly allocated Code object. |
| 2794 | if (!self_reference.is_null()) { |
| 2795 | *(self_reference.location()) = code; |
| 2796 | } |
| 2797 | // Migrate generated code. |
| 2798 | // The generated code can contain Object** values (typically from handles) |
| 2799 | // that are dereferenced during the copy to point directly to the actual heap |
| 2800 | // objects. These pointers can include references to the code object itself, |
| 2801 | // through the self_reference parameter. |
| 2802 | code->CopyFrom(desc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2803 | |
| 2804 | #ifdef DEBUG |
| 2805 | code->Verify(); |
| 2806 | #endif |
| 2807 | return code; |
| 2808 | } |
| 2809 | |
| 2810 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2811 | MaybeObject* Heap::CopyCode(Code* code) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2812 | // Allocate an object the same size as the code object. |
| 2813 | int obj_size = code->Size(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2814 | MaybeObject* maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2815 | if (obj_size > MaxObjectSizeInPagedSpace()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2816 | maybe_result = lo_space_->AllocateRawCode(obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2817 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2818 | maybe_result = code_space_->AllocateRaw(obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2821 | Object* result; |
| 2822 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2823 | |
| 2824 | // Copy code object. |
| 2825 | Address old_addr = code->address(); |
| 2826 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2827 | CopyBlock(new_addr, old_addr, obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2828 | // Relocate the copy. |
| 2829 | Code* new_code = Code::cast(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2830 | ASSERT(!isolate_->code_range()->exists() || |
| 2831 | isolate_->code_range()->contains(code->address())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2832 | new_code->Relocate(new_addr - old_addr); |
| 2833 | return new_code; |
| 2834 | } |
| 2835 | |
| 2836 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2837 | MaybeObject* Heap::CopyCode(Code* code, Vector<byte> reloc_info) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2838 | // Allocate ByteArray before the Code object, so that we do not risk |
| 2839 | // leaving uninitialized Code object (and breaking the heap). |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2840 | Object* reloc_info_array; |
| 2841 | { MaybeObject* maybe_reloc_info_array = |
| 2842 | AllocateByteArray(reloc_info.length(), TENURED); |
| 2843 | if (!maybe_reloc_info_array->ToObject(&reloc_info_array)) { |
| 2844 | return maybe_reloc_info_array; |
| 2845 | } |
| 2846 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2847 | |
| 2848 | int new_body_size = RoundUp(code->instruction_size(), kObjectAlignment); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2849 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2850 | int new_obj_size = Code::SizeFor(new_body_size); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2851 | |
| 2852 | Address old_addr = code->address(); |
| 2853 | |
| 2854 | size_t relocation_offset = |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2855 | static_cast<size_t>(code->instruction_end() - old_addr); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2856 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2857 | MaybeObject* maybe_result; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2858 | if (new_obj_size > MaxObjectSizeInPagedSpace()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2859 | maybe_result = lo_space_->AllocateRawCode(new_obj_size); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2860 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2861 | maybe_result = code_space_->AllocateRaw(new_obj_size); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2862 | } |
| 2863 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2864 | Object* result; |
| 2865 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2866 | |
| 2867 | // Copy code object. |
| 2868 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
| 2869 | |
| 2870 | // Copy header and instructions. |
| 2871 | memcpy(new_addr, old_addr, relocation_offset); |
| 2872 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2873 | Code* new_code = Code::cast(result); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2874 | new_code->set_relocation_info(ByteArray::cast(reloc_info_array)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2875 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2876 | // Copy patched rinfo. |
| 2877 | memcpy(new_code->relocation_start(), reloc_info.start(), reloc_info.length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2878 | |
| 2879 | // Relocate the copy. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2880 | ASSERT(!isolate_->code_range()->exists() || |
| 2881 | isolate_->code_range()->contains(code->address())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2882 | new_code->Relocate(new_addr - old_addr); |
| 2883 | |
| 2884 | #ifdef DEBUG |
| 2885 | code->Verify(); |
| 2886 | #endif |
| 2887 | return new_code; |
| 2888 | } |
| 2889 | |
| 2890 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2891 | MaybeObject* Heap::Allocate(Map* map, AllocationSpace space) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2892 | ASSERT(gc_state_ == NOT_IN_GC); |
| 2893 | ASSERT(map->instance_type() != MAP_TYPE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2894 | // If allocation failures are disallowed, we may allocate in a different |
| 2895 | // space when new space is full and the object is not a large object. |
| 2896 | AllocationSpace retry_space = |
| 2897 | (space != NEW_SPACE) ? space : TargetSpaceId(map->instance_type()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2898 | Object* result; |
| 2899 | { MaybeObject* maybe_result = |
| 2900 | AllocateRaw(map->instance_size(), space, retry_space); |
| 2901 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2902 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2903 | HeapObject::cast(result)->set_map(map); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2904 | #ifdef ENABLE_LOGGING_AND_PROFILING |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2905 | isolate_->producer_heap_profile()->RecordJSObjectAllocation(result); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2906 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2907 | return result; |
| 2908 | } |
| 2909 | |
| 2910 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2911 | MaybeObject* Heap::InitializeFunction(JSFunction* function, |
| 2912 | SharedFunctionInfo* shared, |
| 2913 | Object* prototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2914 | ASSERT(!prototype->IsMap()); |
| 2915 | function->initialize_properties(); |
| 2916 | function->initialize_elements(); |
| 2917 | function->set_shared(shared); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2918 | function->set_code(shared->code()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2919 | function->set_prototype_or_initial_map(prototype); |
| 2920 | function->set_context(undefined_value()); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 2921 | function->set_literals(empty_fixed_array()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2922 | function->set_next_function_link(undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2923 | return function; |
| 2924 | } |
| 2925 | |
| 2926 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2927 | MaybeObject* Heap::AllocateFunctionPrototype(JSFunction* function) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2928 | // Allocate the prototype. Make sure to use the object function |
| 2929 | // from the function's context, since the function can be from a |
| 2930 | // different context. |
| 2931 | JSFunction* object_function = |
| 2932 | function->context()->global_context()->object_function(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2933 | Object* prototype; |
| 2934 | { MaybeObject* maybe_prototype = AllocateJSObject(object_function); |
| 2935 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 2936 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2937 | // When creating the prototype for the function we must set its |
| 2938 | // constructor to the function. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2939 | Object* result; |
| 2940 | { MaybeObject* maybe_result = |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 2941 | JSObject::cast(prototype)->SetLocalPropertyIgnoreAttributes( |
| 2942 | constructor_symbol(), function, DONT_ENUM); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2943 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2944 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2945 | return prototype; |
| 2946 | } |
| 2947 | |
| 2948 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2949 | MaybeObject* Heap::AllocateFunction(Map* function_map, |
| 2950 | SharedFunctionInfo* shared, |
| 2951 | Object* prototype, |
| 2952 | PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2953 | AllocationSpace space = |
| 2954 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2955 | Object* result; |
| 2956 | { MaybeObject* maybe_result = Allocate(function_map, space); |
| 2957 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2958 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2959 | return InitializeFunction(JSFunction::cast(result), shared, prototype); |
| 2960 | } |
| 2961 | |
| 2962 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2963 | MaybeObject* Heap::AllocateArgumentsObject(Object* callee, int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2964 | // To get fast allocation and map sharing for arguments objects we |
| 2965 | // allocate them based on an arguments boilerplate. |
| 2966 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2967 | JSObject* boilerplate; |
| 2968 | int arguments_object_size; |
| 2969 | bool strict_mode_callee = callee->IsJSFunction() && |
| 2970 | JSFunction::cast(callee)->shared()->strict_mode(); |
| 2971 | if (strict_mode_callee) { |
| 2972 | boilerplate = |
| 2973 | isolate()->context()->global_context()-> |
| 2974 | strict_mode_arguments_boilerplate(); |
| 2975 | arguments_object_size = kArgumentsObjectSizeStrict; |
| 2976 | } else { |
| 2977 | boilerplate = |
| 2978 | isolate()->context()->global_context()->arguments_boilerplate(); |
| 2979 | arguments_object_size = kArgumentsObjectSize; |
| 2980 | } |
| 2981 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2982 | // This calls Copy directly rather than using Heap::AllocateRaw so we |
| 2983 | // duplicate the check here. |
| 2984 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 2985 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2986 | // Check that the size of the boilerplate matches our |
| 2987 | // expectations. The ArgumentsAccessStub::GenerateNewObject relies |
| 2988 | // on the size being a known constant. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2989 | ASSERT(arguments_object_size == boilerplate->map()->instance_size()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2990 | |
| 2991 | // Do the allocation. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2992 | Object* result; |
| 2993 | { MaybeObject* maybe_result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2994 | AllocateRaw(arguments_object_size, NEW_SPACE, OLD_POINTER_SPACE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2995 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2996 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2997 | |
| 2998 | // Copy the content. The arguments boilerplate doesn't have any |
| 2999 | // fields that point to new space so it's safe to skip the write |
| 3000 | // barrier here. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3001 | CopyBlock(HeapObject::cast(result)->address(), |
| 3002 | boilerplate->address(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3003 | JSObject::kHeaderSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3004 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3005 | // Set the length property. |
| 3006 | JSObject::cast(result)->InObjectPropertyAtPut(kArgumentsLengthIndex, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3007 | Smi::FromInt(length), |
| 3008 | SKIP_WRITE_BARRIER); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3009 | // Set the callee property for non-strict mode arguments object only. |
| 3010 | if (!strict_mode_callee) { |
| 3011 | JSObject::cast(result)->InObjectPropertyAtPut(kArgumentsCalleeIndex, |
| 3012 | callee); |
| 3013 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3014 | |
| 3015 | // Check the state of the object |
| 3016 | ASSERT(JSObject::cast(result)->HasFastProperties()); |
| 3017 | ASSERT(JSObject::cast(result)->HasFastElements()); |
| 3018 | |
| 3019 | return result; |
| 3020 | } |
| 3021 | |
| 3022 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3023 | static bool HasDuplicates(DescriptorArray* descriptors) { |
| 3024 | int count = descriptors->number_of_descriptors(); |
| 3025 | if (count > 1) { |
| 3026 | String* prev_key = descriptors->GetKey(0); |
| 3027 | for (int i = 1; i != count; i++) { |
| 3028 | String* current_key = descriptors->GetKey(i); |
| 3029 | if (prev_key == current_key) return true; |
| 3030 | prev_key = current_key; |
| 3031 | } |
| 3032 | } |
| 3033 | return false; |
| 3034 | } |
| 3035 | |
| 3036 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3037 | MaybeObject* Heap::AllocateInitialMap(JSFunction* fun) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3038 | ASSERT(!fun->has_initial_map()); |
| 3039 | |
| 3040 | // First create a new map with the size and number of in-object properties |
| 3041 | // suggested by the function. |
| 3042 | int instance_size = fun->shared()->CalculateInstanceSize(); |
| 3043 | int in_object_properties = fun->shared()->CalculateInObjectProperties(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3044 | Object* map_obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3045 | { MaybeObject* maybe_map_obj = AllocateMap(JS_OBJECT_TYPE, instance_size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3046 | if (!maybe_map_obj->ToObject(&map_obj)) return maybe_map_obj; |
| 3047 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3048 | |
| 3049 | // Fetch or allocate prototype. |
| 3050 | Object* prototype; |
| 3051 | if (fun->has_instance_prototype()) { |
| 3052 | prototype = fun->instance_prototype(); |
| 3053 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3054 | { MaybeObject* maybe_prototype = AllocateFunctionPrototype(fun); |
| 3055 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 3056 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3057 | } |
| 3058 | Map* map = Map::cast(map_obj); |
| 3059 | map->set_inobject_properties(in_object_properties); |
| 3060 | map->set_unused_property_fields(in_object_properties); |
| 3061 | map->set_prototype(prototype); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3062 | ASSERT(map->has_fast_elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3063 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3064 | // If the function has only simple this property assignments add |
| 3065 | // field descriptors for these to the initial map as the object |
| 3066 | // cannot be constructed without having these properties. Guard by |
| 3067 | // the inline_new flag so we only change the map if we generate a |
| 3068 | // specialized construct stub. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3069 | ASSERT(in_object_properties <= Map::kMaxPreAllocatedPropertyFields); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3070 | if (fun->shared()->CanGenerateInlineConstructor(prototype)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3071 | int count = fun->shared()->this_property_assignments_count(); |
| 3072 | if (count > in_object_properties) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3073 | // Inline constructor can only handle inobject properties. |
| 3074 | fun->shared()->ForbidInlineConstructor(); |
| 3075 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3076 | Object* descriptors_obj; |
| 3077 | { MaybeObject* maybe_descriptors_obj = DescriptorArray::Allocate(count); |
| 3078 | if (!maybe_descriptors_obj->ToObject(&descriptors_obj)) { |
| 3079 | return maybe_descriptors_obj; |
| 3080 | } |
| 3081 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3082 | DescriptorArray* descriptors = DescriptorArray::cast(descriptors_obj); |
| 3083 | for (int i = 0; i < count; i++) { |
| 3084 | String* name = fun->shared()->GetThisPropertyAssignmentName(i); |
| 3085 | ASSERT(name->IsSymbol()); |
| 3086 | FieldDescriptor field(name, i, NONE); |
| 3087 | field.SetEnumerationIndex(i); |
| 3088 | descriptors->Set(i, &field); |
| 3089 | } |
| 3090 | descriptors->SetNextEnumerationIndex(count); |
| 3091 | descriptors->SortUnchecked(); |
| 3092 | |
| 3093 | // The descriptors may contain duplicates because the compiler does not |
| 3094 | // guarantee the uniqueness of property names (it would have required |
| 3095 | // quadratic time). Once the descriptors are sorted we can check for |
| 3096 | // duplicates in linear time. |
| 3097 | if (HasDuplicates(descriptors)) { |
| 3098 | fun->shared()->ForbidInlineConstructor(); |
| 3099 | } else { |
| 3100 | map->set_instance_descriptors(descriptors); |
| 3101 | map->set_pre_allocated_property_fields(count); |
| 3102 | map->set_unused_property_fields(in_object_properties - count); |
| 3103 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3104 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3105 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3106 | |
| 3107 | fun->shared()->StartInobjectSlackTracking(map); |
| 3108 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3109 | return map; |
| 3110 | } |
| 3111 | |
| 3112 | |
| 3113 | void Heap::InitializeJSObjectFromMap(JSObject* obj, |
| 3114 | FixedArray* properties, |
| 3115 | Map* map) { |
| 3116 | obj->set_properties(properties); |
| 3117 | obj->initialize_elements(); |
| 3118 | // TODO(1240798): Initialize the object's body using valid initial values |
| 3119 | // according to the object's initial map. For example, if the map's |
| 3120 | // instance type is JS_ARRAY_TYPE, the length field should be initialized |
| 3121 | // to a number (eg, Smi::FromInt(0)) and the elements initialized to a |
| 3122 | // fixed array (eg, Heap::empty_fixed_array()). Currently, the object |
| 3123 | // verification code has to cope with (temporarily) invalid objects. See |
| 3124 | // for example, JSArray::JSArrayVerify). |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3125 | Object* filler; |
| 3126 | // We cannot always fill with one_pointer_filler_map because objects |
| 3127 | // created from API functions expect their internal fields to be initialized |
| 3128 | // with undefined_value. |
| 3129 | if (map->constructor()->IsJSFunction() && |
| 3130 | JSFunction::cast(map->constructor())->shared()-> |
| 3131 | IsInobjectSlackTrackingInProgress()) { |
| 3132 | // We might want to shrink the object later. |
| 3133 | ASSERT(obj->GetInternalFieldCount() == 0); |
| 3134 | filler = Heap::one_pointer_filler_map(); |
| 3135 | } else { |
| 3136 | filler = Heap::undefined_value(); |
| 3137 | } |
| 3138 | obj->InitializeBody(map->instance_size(), filler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3142 | MaybeObject* Heap::AllocateJSObjectFromMap(Map* map, PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3143 | // JSFunctions should be allocated using AllocateFunction to be |
| 3144 | // properly initialized. |
| 3145 | ASSERT(map->instance_type() != JS_FUNCTION_TYPE); |
| 3146 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3147 | // Both types of global objects should be allocated using |
| 3148 | // AllocateGlobalObject to be properly initialized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3149 | ASSERT(map->instance_type() != JS_GLOBAL_OBJECT_TYPE); |
| 3150 | ASSERT(map->instance_type() != JS_BUILTINS_OBJECT_TYPE); |
| 3151 | |
| 3152 | // Allocate the backing storage for the properties. |
| 3153 | int prop_size = |
| 3154 | map->pre_allocated_property_fields() + |
| 3155 | map->unused_property_fields() - |
| 3156 | map->inobject_properties(); |
| 3157 | ASSERT(prop_size >= 0); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3158 | Object* properties; |
| 3159 | { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, pretenure); |
| 3160 | if (!maybe_properties->ToObject(&properties)) return maybe_properties; |
| 3161 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3162 | |
| 3163 | // Allocate the JSObject. |
| 3164 | AllocationSpace space = |
| 3165 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
| 3166 | if (map->instance_size() > MaxObjectSizeInPagedSpace()) space = LO_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3167 | Object* obj; |
| 3168 | { MaybeObject* maybe_obj = Allocate(map, space); |
| 3169 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3170 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3171 | |
| 3172 | // Initialize the JSObject. |
| 3173 | InitializeJSObjectFromMap(JSObject::cast(obj), |
| 3174 | FixedArray::cast(properties), |
| 3175 | map); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3176 | ASSERT(JSObject::cast(obj)->HasFastElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3177 | return obj; |
| 3178 | } |
| 3179 | |
| 3180 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3181 | MaybeObject* Heap::AllocateJSObject(JSFunction* constructor, |
| 3182 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3183 | // Allocate the initial map if absent. |
| 3184 | if (!constructor->has_initial_map()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3185 | Object* initial_map; |
| 3186 | { MaybeObject* maybe_initial_map = AllocateInitialMap(constructor); |
| 3187 | if (!maybe_initial_map->ToObject(&initial_map)) return maybe_initial_map; |
| 3188 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3189 | constructor->set_initial_map(Map::cast(initial_map)); |
| 3190 | Map::cast(initial_map)->set_constructor(constructor); |
| 3191 | } |
| 3192 | // Allocate the object based on the constructors initial map. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3193 | MaybeObject* result = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3194 | AllocateJSObjectFromMap(constructor->initial_map(), pretenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3195 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3196 | // Make sure result is NOT a global object if valid. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3197 | Object* non_failure; |
| 3198 | ASSERT(!result->ToObject(&non_failure) || !non_failure->IsGlobalObject()); |
| 3199 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3200 | return result; |
| 3201 | } |
| 3202 | |
| 3203 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3204 | MaybeObject* Heap::AllocateGlobalObject(JSFunction* constructor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3205 | ASSERT(constructor->has_initial_map()); |
| 3206 | Map* map = constructor->initial_map(); |
| 3207 | |
| 3208 | // Make sure no field properties are described in the initial map. |
| 3209 | // This guarantees us that normalizing the properties does not |
| 3210 | // require us to change property values to JSGlobalPropertyCells. |
| 3211 | ASSERT(map->NextFreePropertyIndex() == 0); |
| 3212 | |
| 3213 | // Make sure we don't have a ton of pre-allocated slots in the |
| 3214 | // global objects. They will be unused once we normalize the object. |
| 3215 | ASSERT(map->unused_property_fields() == 0); |
| 3216 | ASSERT(map->inobject_properties() == 0); |
| 3217 | |
| 3218 | // Initial size of the backing store to avoid resize of the storage during |
| 3219 | // bootstrapping. The size differs between the JS global object ad the |
| 3220 | // builtins object. |
| 3221 | int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512; |
| 3222 | |
| 3223 | // Allocate a dictionary object for backing storage. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3224 | Object* obj; |
| 3225 | { MaybeObject* maybe_obj = |
| 3226 | StringDictionary::Allocate( |
| 3227 | map->NumberOfDescribedProperties() * 2 + initial_size); |
| 3228 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3229 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3230 | StringDictionary* dictionary = StringDictionary::cast(obj); |
| 3231 | |
| 3232 | // The global object might be created from an object template with accessors. |
| 3233 | // Fill these accessors into the dictionary. |
| 3234 | DescriptorArray* descs = map->instance_descriptors(); |
| 3235 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3236 | PropertyDetails details(descs->GetDetails(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3237 | ASSERT(details.type() == CALLBACKS); // Only accessors are expected. |
| 3238 | PropertyDetails d = |
| 3239 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 3240 | Object* value = descs->GetCallbacksObject(i); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3241 | { MaybeObject* maybe_value = AllocateJSGlobalPropertyCell(value); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3242 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 3243 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3244 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3245 | Object* result; |
| 3246 | { MaybeObject* maybe_result = dictionary->Add(descs->GetKey(i), value, d); |
| 3247 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3248 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3249 | dictionary = StringDictionary::cast(result); |
| 3250 | } |
| 3251 | |
| 3252 | // Allocate the global object and initialize it with the backing store. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3253 | { MaybeObject* maybe_obj = Allocate(map, OLD_POINTER_SPACE); |
| 3254 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3255 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3256 | JSObject* global = JSObject::cast(obj); |
| 3257 | InitializeJSObjectFromMap(global, dictionary, map); |
| 3258 | |
| 3259 | // Create a new map for the global object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3260 | { MaybeObject* maybe_obj = map->CopyDropDescriptors(); |
| 3261 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3262 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3263 | Map* new_map = Map::cast(obj); |
| 3264 | |
| 3265 | // Setup the global object as a normalized object. |
| 3266 | global->set_map(new_map); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3267 | global->map()->set_instance_descriptors(empty_descriptor_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3268 | global->set_properties(dictionary); |
| 3269 | |
| 3270 | // Make sure result is a global object with properties in dictionary. |
| 3271 | ASSERT(global->IsGlobalObject()); |
| 3272 | ASSERT(!global->HasFastProperties()); |
| 3273 | return global; |
| 3274 | } |
| 3275 | |
| 3276 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3277 | MaybeObject* Heap::CopyJSObject(JSObject* source) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3278 | // Never used to copy functions. If functions need to be copied we |
| 3279 | // have to be careful to clear the literals array. |
| 3280 | ASSERT(!source->IsJSFunction()); |
| 3281 | |
| 3282 | // Make the clone. |
| 3283 | Map* map = source->map(); |
| 3284 | int object_size = map->instance_size(); |
| 3285 | Object* clone; |
| 3286 | |
| 3287 | // If we're forced to always allocate, we use the general allocation |
| 3288 | // functions which may leave us with an object in old space. |
| 3289 | if (always_allocate()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3290 | { MaybeObject* maybe_clone = |
| 3291 | AllocateRaw(object_size, NEW_SPACE, OLD_POINTER_SPACE); |
| 3292 | if (!maybe_clone->ToObject(&clone)) return maybe_clone; |
| 3293 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3294 | Address clone_address = HeapObject::cast(clone)->address(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3295 | CopyBlock(clone_address, |
| 3296 | source->address(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3297 | object_size); |
| 3298 | // Update write barrier for all fields that lie beyond the header. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3299 | RecordWrites(clone_address, |
| 3300 | JSObject::kHeaderSize, |
| 3301 | (object_size - JSObject::kHeaderSize) / kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3302 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3303 | { MaybeObject* maybe_clone = new_space_.AllocateRaw(object_size); |
| 3304 | if (!maybe_clone->ToObject(&clone)) return maybe_clone; |
| 3305 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3306 | ASSERT(InNewSpace(clone)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3307 | // Since we know the clone is allocated in new space, we can copy |
| 3308 | // the contents without worrying about updating the write barrier. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3309 | CopyBlock(HeapObject::cast(clone)->address(), |
| 3310 | source->address(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3311 | object_size); |
| 3312 | } |
| 3313 | |
| 3314 | FixedArray* elements = FixedArray::cast(source->elements()); |
| 3315 | FixedArray* properties = FixedArray::cast(source->properties()); |
| 3316 | // Update elements if necessary. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3317 | if (elements->length() > 0) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3318 | Object* elem; |
| 3319 | { MaybeObject* maybe_elem = |
| 3320 | (elements->map() == fixed_cow_array_map()) ? |
| 3321 | elements : CopyFixedArray(elements); |
| 3322 | if (!maybe_elem->ToObject(&elem)) return maybe_elem; |
| 3323 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3324 | JSObject::cast(clone)->set_elements(FixedArray::cast(elem)); |
| 3325 | } |
| 3326 | // Update properties if necessary. |
| 3327 | if (properties->length() > 0) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3328 | Object* prop; |
| 3329 | { MaybeObject* maybe_prop = CopyFixedArray(properties); |
| 3330 | if (!maybe_prop->ToObject(&prop)) return maybe_prop; |
| 3331 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3332 | JSObject::cast(clone)->set_properties(FixedArray::cast(prop)); |
| 3333 | } |
| 3334 | // Return the new clone. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3335 | #ifdef ENABLE_LOGGING_AND_PROFILING |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3336 | isolate_->producer_heap_profile()->RecordJSObjectAllocation(clone); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3337 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3338 | return clone; |
| 3339 | } |
| 3340 | |
| 3341 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3342 | MaybeObject* Heap::ReinitializeJSGlobalProxy(JSFunction* constructor, |
| 3343 | JSGlobalProxy* object) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3344 | ASSERT(constructor->has_initial_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3345 | Map* map = constructor->initial_map(); |
| 3346 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3347 | // Check that the already allocated object has the same size and type as |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3348 | // objects allocated using the constructor. |
| 3349 | ASSERT(map->instance_size() == object->map()->instance_size()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3350 | ASSERT(map->instance_type() == object->map()->instance_type()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3351 | |
| 3352 | // Allocate the backing storage for the properties. |
| 3353 | int prop_size = map->unused_property_fields() - map->inobject_properties(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3354 | Object* properties; |
| 3355 | { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, TENURED); |
| 3356 | if (!maybe_properties->ToObject(&properties)) return maybe_properties; |
| 3357 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3358 | |
| 3359 | // Reset the map for the object. |
| 3360 | object->set_map(constructor->initial_map()); |
| 3361 | |
| 3362 | // Reinitialize the object from the constructor map. |
| 3363 | InitializeJSObjectFromMap(object, FixedArray::cast(properties), map); |
| 3364 | return object; |
| 3365 | } |
| 3366 | |
| 3367 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3368 | MaybeObject* Heap::AllocateStringFromAscii(Vector<const char> string, |
| 3369 | PretenureFlag pretenure) { |
| 3370 | Object* result; |
| 3371 | { MaybeObject* maybe_result = |
| 3372 | AllocateRawAsciiString(string.length(), pretenure); |
| 3373 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3374 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3375 | |
| 3376 | // Copy the characters into the new object. |
| 3377 | SeqAsciiString* string_result = SeqAsciiString::cast(result); |
| 3378 | for (int i = 0; i < string.length(); i++) { |
| 3379 | string_result->SeqAsciiStringSet(i, string[i]); |
| 3380 | } |
| 3381 | return result; |
| 3382 | } |
| 3383 | |
| 3384 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3385 | MaybeObject* Heap::AllocateStringFromUtf8Slow(Vector<const char> string, |
| 3386 | PretenureFlag pretenure) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3387 | // V8 only supports characters in the Basic Multilingual Plane. |
| 3388 | const uc32 kMaxSupportedChar = 0xFFFF; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3389 | // Count the number of characters in the UTF-8 string and check if |
| 3390 | // it is an ASCII string. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3391 | Access<UnicodeCache::Utf8Decoder> |
| 3392 | decoder(isolate_->unicode_cache()->utf8_decoder()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3393 | decoder->Reset(string.start(), string.length()); |
| 3394 | int chars = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3395 | while (decoder->has_more()) { |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3396 | decoder->GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3397 | chars++; |
| 3398 | } |
| 3399 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3400 | Object* result; |
| 3401 | { MaybeObject* maybe_result = AllocateRawTwoByteString(chars, pretenure); |
| 3402 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3403 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3404 | |
| 3405 | // Convert and copy the characters into the new object. |
| 3406 | String* string_result = String::cast(result); |
| 3407 | decoder->Reset(string.start(), string.length()); |
| 3408 | for (int i = 0; i < chars; i++) { |
| 3409 | uc32 r = decoder->GetNext(); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3410 | if (r > kMaxSupportedChar) { r = unibrow::Utf8::kBadChar; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3411 | string_result->Set(i, r); |
| 3412 | } |
| 3413 | return result; |
| 3414 | } |
| 3415 | |
| 3416 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3417 | MaybeObject* Heap::AllocateStringFromTwoByte(Vector<const uc16> string, |
| 3418 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3419 | // Check if the string is an ASCII string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3420 | MaybeObject* maybe_result; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3421 | if (String::IsAscii(string.start(), string.length())) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3422 | maybe_result = AllocateRawAsciiString(string.length(), pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3423 | } else { // It's not an ASCII string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3424 | maybe_result = AllocateRawTwoByteString(string.length(), pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3425 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3426 | Object* result; |
| 3427 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3428 | |
| 3429 | // Copy the characters into the new object, which may be either ASCII or |
| 3430 | // UTF-16. |
| 3431 | String* string_result = String::cast(result); |
| 3432 | for (int i = 0; i < string.length(); i++) { |
| 3433 | string_result->Set(i, string[i]); |
| 3434 | } |
| 3435 | return result; |
| 3436 | } |
| 3437 | |
| 3438 | |
| 3439 | Map* Heap::SymbolMapForString(String* string) { |
| 3440 | // If the string is in new space it cannot be used as a symbol. |
| 3441 | if (InNewSpace(string)) return NULL; |
| 3442 | |
| 3443 | // Find the corresponding symbol map for strings. |
| 3444 | Map* map = string->map(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3445 | if (map == ascii_string_map()) { |
| 3446 | return ascii_symbol_map(); |
| 3447 | } |
| 3448 | if (map == string_map()) { |
| 3449 | return symbol_map(); |
| 3450 | } |
| 3451 | if (map == cons_string_map()) { |
| 3452 | return cons_symbol_map(); |
| 3453 | } |
| 3454 | if (map == cons_ascii_string_map()) { |
| 3455 | return cons_ascii_symbol_map(); |
| 3456 | } |
| 3457 | if (map == external_string_map()) { |
| 3458 | return external_symbol_map(); |
| 3459 | } |
| 3460 | if (map == external_ascii_string_map()) { |
| 3461 | return external_ascii_symbol_map(); |
| 3462 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3463 | if (map == external_string_with_ascii_data_map()) { |
| 3464 | return external_symbol_with_ascii_data_map(); |
| 3465 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3466 | |
| 3467 | // No match found. |
| 3468 | return NULL; |
| 3469 | } |
| 3470 | |
| 3471 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3472 | MaybeObject* Heap::AllocateInternalSymbol(unibrow::CharacterStream* buffer, |
| 3473 | int chars, |
| 3474 | uint32_t hash_field) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3475 | ASSERT(chars >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3476 | // Ensure the chars matches the number of characters in the buffer. |
| 3477 | ASSERT(static_cast<unsigned>(chars) == buffer->Length()); |
| 3478 | // Determine whether the string is ascii. |
| 3479 | bool is_ascii = true; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3480 | while (buffer->has_more()) { |
| 3481 | if (buffer->GetNext() > unibrow::Utf8::kMaxOneByteChar) { |
| 3482 | is_ascii = false; |
| 3483 | break; |
| 3484 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3485 | } |
| 3486 | buffer->Rewind(); |
| 3487 | |
| 3488 | // Compute map and object size. |
| 3489 | int size; |
| 3490 | Map* map; |
| 3491 | |
| 3492 | if (is_ascii) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3493 | if (chars > SeqAsciiString::kMaxLength) { |
| 3494 | return Failure::OutOfMemoryException(); |
| 3495 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3496 | map = ascii_symbol_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3497 | size = SeqAsciiString::SizeFor(chars); |
| 3498 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3499 | if (chars > SeqTwoByteString::kMaxLength) { |
| 3500 | return Failure::OutOfMemoryException(); |
| 3501 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3502 | map = symbol_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3503 | size = SeqTwoByteString::SizeFor(chars); |
| 3504 | } |
| 3505 | |
| 3506 | // Allocate string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3507 | Object* result; |
| 3508 | { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace()) |
| 3509 | ? lo_space_->AllocateRaw(size) |
| 3510 | : old_data_space_->AllocateRaw(size); |
| 3511 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3512 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3513 | |
| 3514 | reinterpret_cast<HeapObject*>(result)->set_map(map); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3515 | // Set length and hash fields of the allocated string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3516 | String* answer = String::cast(result); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3517 | answer->set_length(chars); |
| 3518 | answer->set_hash_field(hash_field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3519 | |
| 3520 | ASSERT_EQ(size, answer->Size()); |
| 3521 | |
| 3522 | // Fill in the characters. |
| 3523 | for (int i = 0; i < chars; i++) { |
| 3524 | answer->Set(i, buffer->GetNext()); |
| 3525 | } |
| 3526 | return answer; |
| 3527 | } |
| 3528 | |
| 3529 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3530 | MaybeObject* Heap::AllocateRawAsciiString(int length, PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3531 | if (length < 0 || length > SeqAsciiString::kMaxLength) { |
| 3532 | return Failure::OutOfMemoryException(); |
| 3533 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3534 | |
| 3535 | int size = SeqAsciiString::SizeFor(length); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3536 | ASSERT(size <= SeqAsciiString::kMaxSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3537 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3538 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 3539 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 3540 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3541 | if (space == NEW_SPACE) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3542 | if (size > kMaxObjectSizeInNewSpace) { |
| 3543 | // Allocate in large object space, retry space will be ignored. |
| 3544 | space = LO_SPACE; |
| 3545 | } else if (size > MaxObjectSizeInPagedSpace()) { |
| 3546 | // Allocate in new space, retry in large object space. |
| 3547 | retry_space = LO_SPACE; |
| 3548 | } |
| 3549 | } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) { |
| 3550 | space = LO_SPACE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3551 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3552 | Object* result; |
| 3553 | { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space); |
| 3554 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3555 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3556 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3557 | // Partially initialize the object. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3558 | HeapObject::cast(result)->set_map(ascii_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3559 | String::cast(result)->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3560 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3561 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 3562 | return result; |
| 3563 | } |
| 3564 | |
| 3565 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3566 | MaybeObject* Heap::AllocateRawTwoByteString(int length, |
| 3567 | PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3568 | if (length < 0 || length > SeqTwoByteString::kMaxLength) { |
| 3569 | return Failure::OutOfMemoryException(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3570 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3571 | int size = SeqTwoByteString::SizeFor(length); |
| 3572 | ASSERT(size <= SeqTwoByteString::kMaxSize); |
| 3573 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 3574 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 3575 | |
| 3576 | if (space == NEW_SPACE) { |
| 3577 | if (size > kMaxObjectSizeInNewSpace) { |
| 3578 | // Allocate in large object space, retry space will be ignored. |
| 3579 | space = LO_SPACE; |
| 3580 | } else if (size > MaxObjectSizeInPagedSpace()) { |
| 3581 | // Allocate in new space, retry in large object space. |
| 3582 | retry_space = LO_SPACE; |
| 3583 | } |
| 3584 | } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) { |
| 3585 | space = LO_SPACE; |
| 3586 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3587 | Object* result; |
| 3588 | { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space); |
| 3589 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3590 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3591 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3592 | // Partially initialize the object. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3593 | HeapObject::cast(result)->set_map(string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3594 | String::cast(result)->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3595 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3596 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 3597 | return result; |
| 3598 | } |
| 3599 | |
| 3600 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3601 | MaybeObject* Heap::AllocateEmptyFixedArray() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3602 | int size = FixedArray::SizeFor(0); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3603 | Object* result; |
| 3604 | { MaybeObject* maybe_result = |
| 3605 | AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE); |
| 3606 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3607 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3608 | // Initialize the object. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3609 | reinterpret_cast<FixedArray*>(result)->set_map(fixed_array_map()); |
| 3610 | reinterpret_cast<FixedArray*>(result)->set_length(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3611 | return result; |
| 3612 | } |
| 3613 | |
| 3614 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3615 | MaybeObject* Heap::AllocateRawFixedArray(int length) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3616 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 3617 | return Failure::OutOfMemoryException(); |
| 3618 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3619 | ASSERT(length > 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3620 | // Use the general function if we're forced to always allocate. |
| 3621 | if (always_allocate()) return AllocateFixedArray(length, TENURED); |
| 3622 | // Allocate the raw data for a fixed array. |
| 3623 | int size = FixedArray::SizeFor(length); |
| 3624 | return size <= kMaxObjectSizeInNewSpace |
| 3625 | ? new_space_.AllocateRaw(size) |
| 3626 | : lo_space_->AllocateRawFixedArray(size); |
| 3627 | } |
| 3628 | |
| 3629 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3630 | MaybeObject* Heap::CopyFixedArrayWithMap(FixedArray* src, Map* map) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3631 | int len = src->length(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3632 | Object* obj; |
| 3633 | { MaybeObject* maybe_obj = AllocateRawFixedArray(len); |
| 3634 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3635 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3636 | if (InNewSpace(obj)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3637 | HeapObject* dst = HeapObject::cast(obj); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3638 | dst->set_map(map); |
| 3639 | CopyBlock(dst->address() + kPointerSize, |
| 3640 | src->address() + kPointerSize, |
| 3641 | FixedArray::SizeFor(len) - kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3642 | return obj; |
| 3643 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3644 | HeapObject::cast(obj)->set_map(map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3645 | FixedArray* result = FixedArray::cast(obj); |
| 3646 | result->set_length(len); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3647 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3648 | // Copy the content |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3649 | AssertNoAllocation no_gc; |
| 3650 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3651 | for (int i = 0; i < len; i++) result->set(i, src->get(i), mode); |
| 3652 | return result; |
| 3653 | } |
| 3654 | |
| 3655 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3656 | MaybeObject* Heap::AllocateFixedArray(int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3657 | ASSERT(length >= 0); |
| 3658 | if (length == 0) return empty_fixed_array(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3659 | Object* result; |
| 3660 | { MaybeObject* maybe_result = AllocateRawFixedArray(length); |
| 3661 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3662 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3663 | // Initialize header. |
| 3664 | FixedArray* array = reinterpret_cast<FixedArray*>(result); |
| 3665 | array->set_map(fixed_array_map()); |
| 3666 | array->set_length(length); |
| 3667 | // Initialize body. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3668 | ASSERT(!InNewSpace(undefined_value())); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3669 | MemsetPointer(array->data_start(), undefined_value(), length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3670 | return result; |
| 3671 | } |
| 3672 | |
| 3673 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3674 | MaybeObject* Heap::AllocateRawFixedArray(int length, PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3675 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 3676 | return Failure::OutOfMemoryException(); |
| 3677 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3678 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3679 | AllocationSpace space = |
| 3680 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3681 | int size = FixedArray::SizeFor(length); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3682 | if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) { |
| 3683 | // Too big for new space. |
| 3684 | space = LO_SPACE; |
| 3685 | } else if (space == OLD_POINTER_SPACE && |
| 3686 | size > MaxObjectSizeInPagedSpace()) { |
| 3687 | // Too big for old pointer space. |
| 3688 | space = LO_SPACE; |
| 3689 | } |
| 3690 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3691 | AllocationSpace retry_space = |
| 3692 | (size <= MaxObjectSizeInPagedSpace()) ? OLD_POINTER_SPACE : LO_SPACE; |
| 3693 | |
| 3694 | return AllocateRaw(size, space, retry_space); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3698 | MUST_USE_RESULT static MaybeObject* AllocateFixedArrayWithFiller( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3699 | Heap* heap, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3700 | int length, |
| 3701 | PretenureFlag pretenure, |
| 3702 | Object* filler) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3703 | ASSERT(length >= 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3704 | ASSERT(heap->empty_fixed_array()->IsFixedArray()); |
| 3705 | if (length == 0) return heap->empty_fixed_array(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3706 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3707 | ASSERT(!heap->InNewSpace(filler)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3708 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3709 | { MaybeObject* maybe_result = heap->AllocateRawFixedArray(length, pretenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3710 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3711 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3712 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3713 | HeapObject::cast(result)->set_map(heap->fixed_array_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3714 | FixedArray* array = FixedArray::cast(result); |
| 3715 | array->set_length(length); |
| 3716 | MemsetPointer(array->data_start(), filler, length); |
| 3717 | return array; |
| 3718 | } |
| 3719 | |
| 3720 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3721 | MaybeObject* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3722 | return AllocateFixedArrayWithFiller(this, |
| 3723 | length, |
| 3724 | pretenure, |
| 3725 | undefined_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3729 | MaybeObject* Heap::AllocateFixedArrayWithHoles(int length, |
| 3730 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3731 | return AllocateFixedArrayWithFiller(this, |
| 3732 | length, |
| 3733 | pretenure, |
| 3734 | the_hole_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3738 | MaybeObject* Heap::AllocateUninitializedFixedArray(int length) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3739 | if (length == 0) return empty_fixed_array(); |
| 3740 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3741 | Object* obj; |
| 3742 | { MaybeObject* maybe_obj = AllocateRawFixedArray(length); |
| 3743 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3744 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3745 | |
| 3746 | reinterpret_cast<FixedArray*>(obj)->set_map(fixed_array_map()); |
| 3747 | FixedArray::cast(obj)->set_length(length); |
| 3748 | return obj; |
| 3749 | } |
| 3750 | |
| 3751 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3752 | MaybeObject* Heap::AllocateHashTable(int length, PretenureFlag pretenure) { |
| 3753 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3754 | { MaybeObject* maybe_result = AllocateFixedArray(length, pretenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3755 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3756 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3757 | reinterpret_cast<HeapObject*>(result)->set_map(hash_table_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3758 | ASSERT(result->IsHashTable()); |
| 3759 | return result; |
| 3760 | } |
| 3761 | |
| 3762 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3763 | MaybeObject* Heap::AllocateGlobalContext() { |
| 3764 | Object* result; |
| 3765 | { MaybeObject* maybe_result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3766 | AllocateFixedArray(Context::GLOBAL_CONTEXT_SLOTS); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3767 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3768 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3769 | Context* context = reinterpret_cast<Context*>(result); |
| 3770 | context->set_map(global_context_map()); |
| 3771 | ASSERT(context->IsGlobalContext()); |
| 3772 | ASSERT(result->IsContext()); |
| 3773 | return result; |
| 3774 | } |
| 3775 | |
| 3776 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3777 | MaybeObject* Heap::AllocateFunctionContext(int length, JSFunction* function) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3778 | ASSERT(length >= Context::MIN_CONTEXT_SLOTS); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3779 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3780 | { MaybeObject* maybe_result = AllocateFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3781 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3782 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3783 | Context* context = reinterpret_cast<Context*>(result); |
| 3784 | context->set_map(context_map()); |
| 3785 | context->set_closure(function); |
| 3786 | context->set_fcontext(context); |
| 3787 | context->set_previous(NULL); |
| 3788 | context->set_extension(NULL); |
| 3789 | context->set_global(function->context()->global()); |
| 3790 | ASSERT(!context->IsGlobalContext()); |
| 3791 | ASSERT(context->is_function_context()); |
| 3792 | ASSERT(result->IsContext()); |
| 3793 | return result; |
| 3794 | } |
| 3795 | |
| 3796 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3797 | MaybeObject* Heap::AllocateWithContext(Context* previous, |
| 3798 | JSObject* extension, |
| 3799 | bool is_catch_context) { |
| 3800 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3801 | { MaybeObject* maybe_result = AllocateFixedArray(Context::MIN_CONTEXT_SLOTS); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3802 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3803 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3804 | Context* context = reinterpret_cast<Context*>(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3805 | context->set_map(is_catch_context ? catch_context_map() : |
| 3806 | context_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3807 | context->set_closure(previous->closure()); |
| 3808 | context->set_fcontext(previous->fcontext()); |
| 3809 | context->set_previous(previous); |
| 3810 | context->set_extension(extension); |
| 3811 | context->set_global(previous->global()); |
| 3812 | ASSERT(!context->IsGlobalContext()); |
| 3813 | ASSERT(!context->is_function_context()); |
| 3814 | ASSERT(result->IsContext()); |
| 3815 | return result; |
| 3816 | } |
| 3817 | |
| 3818 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3819 | MaybeObject* Heap::AllocateStruct(InstanceType type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3820 | Map* map; |
| 3821 | switch (type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3822 | #define MAKE_CASE(NAME, Name, name) \ |
| 3823 | case NAME##_TYPE: map = name##_map(); break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3824 | STRUCT_LIST(MAKE_CASE) |
| 3825 | #undef MAKE_CASE |
| 3826 | default: |
| 3827 | UNREACHABLE(); |
| 3828 | return Failure::InternalError(); |
| 3829 | } |
| 3830 | int size = map->instance_size(); |
| 3831 | AllocationSpace space = |
| 3832 | (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : OLD_POINTER_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3833 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3834 | { MaybeObject* maybe_result = Allocate(map, space); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3835 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3836 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3837 | Struct::cast(result)->InitializeBody(size); |
| 3838 | return result; |
| 3839 | } |
| 3840 | |
| 3841 | |
| 3842 | bool Heap::IdleNotification() { |
| 3843 | static const int kIdlesBeforeScavenge = 4; |
| 3844 | static const int kIdlesBeforeMarkSweep = 7; |
| 3845 | static const int kIdlesBeforeMarkCompact = 8; |
Ben Murdoch | db5a90a | 2011-01-06 18:27:03 +0000 | [diff] [blame] | 3846 | static const int kMaxIdleCount = kIdlesBeforeMarkCompact + 1; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 3847 | static const unsigned int kGCsBetweenCleanup = 4; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3848 | |
| 3849 | if (!last_idle_notification_gc_count_init_) { |
| 3850 | last_idle_notification_gc_count_ = gc_count_; |
| 3851 | last_idle_notification_gc_count_init_ = true; |
| 3852 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3853 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3854 | bool uncommit = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3855 | bool finished = false; |
| 3856 | |
Ben Murdoch | db5a90a | 2011-01-06 18:27:03 +0000 | [diff] [blame] | 3857 | // Reset the number of idle notifications received when a number of |
| 3858 | // GCs have taken place. This allows another round of cleanup based |
| 3859 | // on idle notifications if enough work has been carried out to |
| 3860 | // provoke a number of garbage collections. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3861 | if (gc_count_ - last_idle_notification_gc_count_ < kGCsBetweenCleanup) { |
| 3862 | number_idle_notifications_ = |
| 3863 | Min(number_idle_notifications_ + 1, kMaxIdleCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3864 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3865 | number_idle_notifications_ = 0; |
| 3866 | last_idle_notification_gc_count_ = gc_count_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3869 | if (number_idle_notifications_ == kIdlesBeforeScavenge) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3870 | if (contexts_disposed_ > 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3871 | HistogramTimerScope scope(isolate_->counters()->gc_context()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3872 | CollectAllGarbage(false); |
| 3873 | } else { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3874 | CollectGarbage(NEW_SPACE); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3875 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3876 | new_space_.Shrink(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3877 | last_idle_notification_gc_count_ = gc_count_; |
| 3878 | } else if (number_idle_notifications_ == kIdlesBeforeMarkSweep) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3879 | // Before doing the mark-sweep collections we clear the |
| 3880 | // compilation cache to avoid hanging on to source code and |
| 3881 | // generated code for cached functions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3882 | isolate_->compilation_cache()->Clear(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3883 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3884 | CollectAllGarbage(false); |
| 3885 | new_space_.Shrink(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3886 | last_idle_notification_gc_count_ = gc_count_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3887 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3888 | } else if (number_idle_notifications_ == kIdlesBeforeMarkCompact) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3889 | CollectAllGarbage(true); |
| 3890 | new_space_.Shrink(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3891 | last_idle_notification_gc_count_ = gc_count_; |
| 3892 | number_idle_notifications_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3893 | finished = true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3894 | } else if (contexts_disposed_ > 0) { |
| 3895 | if (FLAG_expose_gc) { |
| 3896 | contexts_disposed_ = 0; |
| 3897 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3898 | HistogramTimerScope scope(isolate_->counters()->gc_context()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3899 | CollectAllGarbage(false); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3900 | last_idle_notification_gc_count_ = gc_count_; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3901 | } |
| 3902 | // If this is the first idle notification, we reset the |
| 3903 | // notification count to avoid letting idle notifications for |
| 3904 | // context disposal garbage collections start a potentially too |
| 3905 | // aggressive idle GC cycle. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3906 | if (number_idle_notifications_ <= 1) { |
| 3907 | number_idle_notifications_ = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3908 | uncommit = false; |
| 3909 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3910 | } else if (number_idle_notifications_ > kIdlesBeforeMarkCompact) { |
Ben Murdoch | db5a90a | 2011-01-06 18:27:03 +0000 | [diff] [blame] | 3911 | // If we have received more than kIdlesBeforeMarkCompact idle |
| 3912 | // notifications we do not perform any cleanup because we don't |
| 3913 | // expect to gain much by doing so. |
| 3914 | finished = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3917 | // Make sure that we have no pending context disposals and |
| 3918 | // conditionally uncommit from space. |
| 3919 | ASSERT(contexts_disposed_ == 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3920 | if (uncommit) UncommitFromSpace(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3921 | return finished; |
| 3922 | } |
| 3923 | |
| 3924 | |
| 3925 | #ifdef DEBUG |
| 3926 | |
| 3927 | void Heap::Print() { |
| 3928 | if (!HasBeenSetup()) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3929 | isolate()->PrintStack(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3930 | AllSpaces spaces; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3931 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 3932 | space->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | |
| 3936 | void Heap::ReportCodeStatistics(const char* title) { |
| 3937 | PrintF(">>>>>> Code Stats (%s) >>>>>>\n", title); |
| 3938 | PagedSpace::ResetCodeStatistics(); |
| 3939 | // We do not look for code in new space, map space, or old space. If code |
| 3940 | // somehow ends up in those spaces, we would miss it here. |
| 3941 | code_space_->CollectCodeStatistics(); |
| 3942 | lo_space_->CollectCodeStatistics(); |
| 3943 | PagedSpace::ReportCodeStatistics(); |
| 3944 | } |
| 3945 | |
| 3946 | |
| 3947 | // This function expects that NewSpace's allocated objects histogram is |
| 3948 | // populated (via a call to CollectStatistics or else as a side effect of a |
| 3949 | // just-completed scavenge collection). |
| 3950 | void Heap::ReportHeapStatistics(const char* title) { |
| 3951 | USE(title); |
| 3952 | PrintF(">>>>>> =============== %s (%d) =============== >>>>>>\n", |
| 3953 | title, gc_count_); |
| 3954 | PrintF("mark-compact GC : %d\n", mc_count_); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 3955 | PrintF("old_gen_promotion_limit_ %" V8_PTR_PREFIX "d\n", |
| 3956 | old_gen_promotion_limit_); |
| 3957 | PrintF("old_gen_allocation_limit_ %" V8_PTR_PREFIX "d\n", |
| 3958 | old_gen_allocation_limit_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3959 | |
| 3960 | PrintF("\n"); |
| 3961 | PrintF("Number of handles : %d\n", HandleScope::NumberOfHandles()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3962 | isolate_->global_handles()->PrintStats(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3963 | PrintF("\n"); |
| 3964 | |
| 3965 | PrintF("Heap statistics : "); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3966 | isolate_->memory_allocator()->ReportStatistics(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3967 | PrintF("To space : "); |
| 3968 | new_space_.ReportStatistics(); |
| 3969 | PrintF("Old pointer space : "); |
| 3970 | old_pointer_space_->ReportStatistics(); |
| 3971 | PrintF("Old data space : "); |
| 3972 | old_data_space_->ReportStatistics(); |
| 3973 | PrintF("Code space : "); |
| 3974 | code_space_->ReportStatistics(); |
| 3975 | PrintF("Map space : "); |
| 3976 | map_space_->ReportStatistics(); |
| 3977 | PrintF("Cell space : "); |
| 3978 | cell_space_->ReportStatistics(); |
| 3979 | PrintF("Large object space : "); |
| 3980 | lo_space_->ReportStatistics(); |
| 3981 | PrintF(">>>>>> ========================================= >>>>>>\n"); |
| 3982 | } |
| 3983 | |
| 3984 | #endif // DEBUG |
| 3985 | |
| 3986 | bool Heap::Contains(HeapObject* value) { |
| 3987 | return Contains(value->address()); |
| 3988 | } |
| 3989 | |
| 3990 | |
| 3991 | bool Heap::Contains(Address addr) { |
| 3992 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
| 3993 | return HasBeenSetup() && |
| 3994 | (new_space_.ToSpaceContains(addr) || |
| 3995 | old_pointer_space_->Contains(addr) || |
| 3996 | old_data_space_->Contains(addr) || |
| 3997 | code_space_->Contains(addr) || |
| 3998 | map_space_->Contains(addr) || |
| 3999 | cell_space_->Contains(addr) || |
| 4000 | lo_space_->SlowContains(addr)); |
| 4001 | } |
| 4002 | |
| 4003 | |
| 4004 | bool Heap::InSpace(HeapObject* value, AllocationSpace space) { |
| 4005 | return InSpace(value->address(), space); |
| 4006 | } |
| 4007 | |
| 4008 | |
| 4009 | bool Heap::InSpace(Address addr, AllocationSpace space) { |
| 4010 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
| 4011 | if (!HasBeenSetup()) return false; |
| 4012 | |
| 4013 | switch (space) { |
| 4014 | case NEW_SPACE: |
| 4015 | return new_space_.ToSpaceContains(addr); |
| 4016 | case OLD_POINTER_SPACE: |
| 4017 | return old_pointer_space_->Contains(addr); |
| 4018 | case OLD_DATA_SPACE: |
| 4019 | return old_data_space_->Contains(addr); |
| 4020 | case CODE_SPACE: |
| 4021 | return code_space_->Contains(addr); |
| 4022 | case MAP_SPACE: |
| 4023 | return map_space_->Contains(addr); |
| 4024 | case CELL_SPACE: |
| 4025 | return cell_space_->Contains(addr); |
| 4026 | case LO_SPACE: |
| 4027 | return lo_space_->SlowContains(addr); |
| 4028 | } |
| 4029 | |
| 4030 | return false; |
| 4031 | } |
| 4032 | |
| 4033 | |
| 4034 | #ifdef DEBUG |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4035 | static void DummyScavengePointer(HeapObject** p) { |
| 4036 | } |
| 4037 | |
| 4038 | |
| 4039 | static void VerifyPointersUnderWatermark( |
| 4040 | PagedSpace* space, |
| 4041 | DirtyRegionCallback visit_dirty_region) { |
| 4042 | PageIterator it(space, PageIterator::PAGES_IN_USE); |
| 4043 | |
| 4044 | while (it.has_next()) { |
| 4045 | Page* page = it.next(); |
| 4046 | Address start = page->ObjectAreaStart(); |
| 4047 | Address end = page->AllocationWatermark(); |
| 4048 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4049 | HEAP->IterateDirtyRegions(Page::kAllRegionsDirtyMarks, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4050 | start, |
| 4051 | end, |
| 4052 | visit_dirty_region, |
| 4053 | &DummyScavengePointer); |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | |
| 4058 | static void VerifyPointersUnderWatermark(LargeObjectSpace* space) { |
| 4059 | LargeObjectIterator it(space); |
| 4060 | for (HeapObject* object = it.next(); object != NULL; object = it.next()) { |
| 4061 | if (object->IsFixedArray()) { |
| 4062 | Address slot_address = object->address(); |
| 4063 | Address end = object->address() + object->Size(); |
| 4064 | |
| 4065 | while (slot_address < end) { |
| 4066 | HeapObject** slot = reinterpret_cast<HeapObject**>(slot_address); |
| 4067 | // When we are not in GC the Heap::InNewSpace() predicate |
| 4068 | // checks that pointers which satisfy predicate point into |
| 4069 | // the active semispace. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4070 | HEAP->InNewSpace(*slot); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4071 | slot_address += kPointerSize; |
| 4072 | } |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4078 | void Heap::Verify() { |
| 4079 | ASSERT(HasBeenSetup()); |
| 4080 | |
| 4081 | VerifyPointersVisitor visitor; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4082 | IterateRoots(&visitor, VISIT_ONLY_STRONG); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4083 | |
| 4084 | new_space_.Verify(); |
| 4085 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4086 | VerifyPointersAndDirtyRegionsVisitor dirty_regions_visitor; |
| 4087 | old_pointer_space_->Verify(&dirty_regions_visitor); |
| 4088 | map_space_->Verify(&dirty_regions_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4089 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4090 | VerifyPointersUnderWatermark(old_pointer_space_, |
| 4091 | &IteratePointersInDirtyRegion); |
| 4092 | VerifyPointersUnderWatermark(map_space_, |
| 4093 | &IteratePointersInDirtyMapsRegion); |
| 4094 | VerifyPointersUnderWatermark(lo_space_); |
| 4095 | |
| 4096 | VerifyPageWatermarkValidity(old_pointer_space_, ALL_INVALID); |
| 4097 | VerifyPageWatermarkValidity(map_space_, ALL_INVALID); |
| 4098 | |
| 4099 | VerifyPointersVisitor no_dirty_regions_visitor; |
| 4100 | old_data_space_->Verify(&no_dirty_regions_visitor); |
| 4101 | code_space_->Verify(&no_dirty_regions_visitor); |
| 4102 | cell_space_->Verify(&no_dirty_regions_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4103 | |
| 4104 | lo_space_->Verify(); |
| 4105 | } |
| 4106 | #endif // DEBUG |
| 4107 | |
| 4108 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4109 | MaybeObject* Heap::LookupSymbol(Vector<const char> string) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4110 | Object* symbol = NULL; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4111 | Object* new_table; |
| 4112 | { MaybeObject* maybe_new_table = |
| 4113 | symbol_table()->LookupSymbol(string, &symbol); |
| 4114 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 4115 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4116 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 4117 | // SymbolTable is a singleton and checks for identity. |
| 4118 | roots_[kSymbolTableRootIndex] = new_table; |
| 4119 | ASSERT(symbol != NULL); |
| 4120 | return symbol; |
| 4121 | } |
| 4122 | |
| 4123 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 4124 | MaybeObject* Heap::LookupAsciiSymbol(Vector<const char> string) { |
| 4125 | Object* symbol = NULL; |
| 4126 | Object* new_table; |
| 4127 | { MaybeObject* maybe_new_table = |
| 4128 | symbol_table()->LookupAsciiSymbol(string, &symbol); |
| 4129 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 4130 | } |
| 4131 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 4132 | // SymbolTable is a singleton and checks for identity. |
| 4133 | roots_[kSymbolTableRootIndex] = new_table; |
| 4134 | ASSERT(symbol != NULL); |
| 4135 | return symbol; |
| 4136 | } |
| 4137 | |
| 4138 | |
| 4139 | MaybeObject* Heap::LookupTwoByteSymbol(Vector<const uc16> string) { |
| 4140 | Object* symbol = NULL; |
| 4141 | Object* new_table; |
| 4142 | { MaybeObject* maybe_new_table = |
| 4143 | symbol_table()->LookupTwoByteSymbol(string, &symbol); |
| 4144 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 4145 | } |
| 4146 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 4147 | // SymbolTable is a singleton and checks for identity. |
| 4148 | roots_[kSymbolTableRootIndex] = new_table; |
| 4149 | ASSERT(symbol != NULL); |
| 4150 | return symbol; |
| 4151 | } |
| 4152 | |
| 4153 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4154 | MaybeObject* Heap::LookupSymbol(String* string) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4155 | if (string->IsSymbol()) return string; |
| 4156 | Object* symbol = NULL; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4157 | Object* new_table; |
| 4158 | { MaybeObject* maybe_new_table = |
| 4159 | symbol_table()->LookupString(string, &symbol); |
| 4160 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 4161 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4162 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 4163 | // SymbolTable is a singleton and checks for identity. |
| 4164 | roots_[kSymbolTableRootIndex] = new_table; |
| 4165 | ASSERT(symbol != NULL); |
| 4166 | return symbol; |
| 4167 | } |
| 4168 | |
| 4169 | |
| 4170 | bool Heap::LookupSymbolIfExists(String* string, String** symbol) { |
| 4171 | if (string->IsSymbol()) { |
| 4172 | *symbol = string; |
| 4173 | return true; |
| 4174 | } |
| 4175 | return symbol_table()->LookupSymbolIfExists(string, symbol); |
| 4176 | } |
| 4177 | |
| 4178 | |
| 4179 | #ifdef DEBUG |
| 4180 | void Heap::ZapFromSpace() { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 4181 | ASSERT(reinterpret_cast<Object*>(kFromSpaceZapValue)->IsFailure()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4182 | for (Address a = new_space_.FromSpaceLow(); |
| 4183 | a < new_space_.FromSpaceHigh(); |
| 4184 | a += kPointerSize) { |
| 4185 | Memory::Address_at(a) = kFromSpaceZapValue; |
| 4186 | } |
| 4187 | } |
| 4188 | #endif // DEBUG |
| 4189 | |
| 4190 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4191 | bool Heap::IteratePointersInDirtyRegion(Heap* heap, |
| 4192 | Address start, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4193 | Address end, |
| 4194 | ObjectSlotCallback copy_object_func) { |
| 4195 | Address slot_address = start; |
| 4196 | bool pointers_to_new_space_found = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4197 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4198 | while (slot_address < end) { |
| 4199 | Object** slot = reinterpret_cast<Object**>(slot_address); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4200 | if (heap->InNewSpace(*slot)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4201 | ASSERT((*slot)->IsHeapObject()); |
| 4202 | copy_object_func(reinterpret_cast<HeapObject**>(slot)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4203 | if (heap->InNewSpace(*slot)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4204 | ASSERT((*slot)->IsHeapObject()); |
| 4205 | pointers_to_new_space_found = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4206 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4207 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4208 | slot_address += kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4209 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4210 | return pointers_to_new_space_found; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4214 | // Compute start address of the first map following given addr. |
| 4215 | static inline Address MapStartAlign(Address addr) { |
| 4216 | Address page = Page::FromAddress(addr)->ObjectAreaStart(); |
| 4217 | return page + (((addr - page) + (Map::kSize - 1)) / Map::kSize * Map::kSize); |
| 4218 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4219 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4220 | |
| 4221 | // Compute end address of the first map preceding given addr. |
| 4222 | static inline Address MapEndAlign(Address addr) { |
| 4223 | Address page = Page::FromAllocationTop(addr)->ObjectAreaStart(); |
| 4224 | return page + ((addr - page) / Map::kSize * Map::kSize); |
| 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | static bool IteratePointersInDirtyMaps(Address start, |
| 4229 | Address end, |
| 4230 | ObjectSlotCallback copy_object_func) { |
| 4231 | ASSERT(MapStartAlign(start) == start); |
| 4232 | ASSERT(MapEndAlign(end) == end); |
| 4233 | |
| 4234 | Address map_address = start; |
| 4235 | bool pointers_to_new_space_found = false; |
| 4236 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4237 | Heap* heap = HEAP; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4238 | while (map_address < end) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4239 | ASSERT(!heap->InNewSpace(Memory::Object_at(map_address))); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4240 | ASSERT(Memory::Object_at(map_address)->IsMap()); |
| 4241 | |
| 4242 | Address pointer_fields_start = map_address + Map::kPointerFieldsBeginOffset; |
| 4243 | Address pointer_fields_end = map_address + Map::kPointerFieldsEndOffset; |
| 4244 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4245 | if (Heap::IteratePointersInDirtyRegion(heap, |
| 4246 | pointer_fields_start, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4247 | pointer_fields_end, |
| 4248 | copy_object_func)) { |
| 4249 | pointers_to_new_space_found = true; |
| 4250 | } |
| 4251 | |
| 4252 | map_address += Map::kSize; |
| 4253 | } |
| 4254 | |
| 4255 | return pointers_to_new_space_found; |
| 4256 | } |
| 4257 | |
| 4258 | |
| 4259 | bool Heap::IteratePointersInDirtyMapsRegion( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4260 | Heap* heap, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4261 | Address start, |
| 4262 | Address end, |
| 4263 | ObjectSlotCallback copy_object_func) { |
| 4264 | Address map_aligned_start = MapStartAlign(start); |
| 4265 | Address map_aligned_end = MapEndAlign(end); |
| 4266 | |
| 4267 | bool contains_pointers_to_new_space = false; |
| 4268 | |
| 4269 | if (map_aligned_start != start) { |
| 4270 | Address prev_map = map_aligned_start - Map::kSize; |
| 4271 | ASSERT(Memory::Object_at(prev_map)->IsMap()); |
| 4272 | |
| 4273 | Address pointer_fields_start = |
| 4274 | Max(start, prev_map + Map::kPointerFieldsBeginOffset); |
| 4275 | |
| 4276 | Address pointer_fields_end = |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4277 | Min(prev_map + Map::kPointerFieldsEndOffset, end); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4278 | |
| 4279 | contains_pointers_to_new_space = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4280 | IteratePointersInDirtyRegion(heap, |
| 4281 | pointer_fields_start, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4282 | pointer_fields_end, |
| 4283 | copy_object_func) |
| 4284 | || contains_pointers_to_new_space; |
| 4285 | } |
| 4286 | |
| 4287 | contains_pointers_to_new_space = |
| 4288 | IteratePointersInDirtyMaps(map_aligned_start, |
| 4289 | map_aligned_end, |
| 4290 | copy_object_func) |
| 4291 | || contains_pointers_to_new_space; |
| 4292 | |
| 4293 | if (map_aligned_end != end) { |
| 4294 | ASSERT(Memory::Object_at(map_aligned_end)->IsMap()); |
| 4295 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4296 | Address pointer_fields_start = |
| 4297 | map_aligned_end + Map::kPointerFieldsBeginOffset; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4298 | |
| 4299 | Address pointer_fields_end = |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4300 | Min(end, map_aligned_end + Map::kPointerFieldsEndOffset); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4301 | |
| 4302 | contains_pointers_to_new_space = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4303 | IteratePointersInDirtyRegion(heap, |
| 4304 | pointer_fields_start, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4305 | pointer_fields_end, |
| 4306 | copy_object_func) |
| 4307 | || contains_pointers_to_new_space; |
| 4308 | } |
| 4309 | |
| 4310 | return contains_pointers_to_new_space; |
| 4311 | } |
| 4312 | |
| 4313 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 4314 | void Heap::IterateAndMarkPointersToFromSpace(Address start, |
| 4315 | Address end, |
| 4316 | ObjectSlotCallback callback) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4317 | Address slot_address = start; |
| 4318 | Page* page = Page::FromAddress(start); |
| 4319 | |
| 4320 | uint32_t marks = page->GetRegionMarks(); |
| 4321 | |
| 4322 | while (slot_address < end) { |
| 4323 | Object** slot = reinterpret_cast<Object**>(slot_address); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4324 | if (InFromSpace(*slot)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4325 | ASSERT((*slot)->IsHeapObject()); |
| 4326 | callback(reinterpret_cast<HeapObject**>(slot)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4327 | if (InNewSpace(*slot)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4328 | ASSERT((*slot)->IsHeapObject()); |
| 4329 | marks |= page->GetRegionMaskForAddress(slot_address); |
| 4330 | } |
| 4331 | } |
| 4332 | slot_address += kPointerSize; |
| 4333 | } |
| 4334 | |
| 4335 | page->SetRegionMarks(marks); |
| 4336 | } |
| 4337 | |
| 4338 | |
| 4339 | uint32_t Heap::IterateDirtyRegions( |
| 4340 | uint32_t marks, |
| 4341 | Address area_start, |
| 4342 | Address area_end, |
| 4343 | DirtyRegionCallback visit_dirty_region, |
| 4344 | ObjectSlotCallback copy_object_func) { |
| 4345 | uint32_t newmarks = 0; |
| 4346 | uint32_t mask = 1; |
| 4347 | |
| 4348 | if (area_start >= area_end) { |
| 4349 | return newmarks; |
| 4350 | } |
| 4351 | |
| 4352 | Address region_start = area_start; |
| 4353 | |
| 4354 | // area_start does not necessarily coincide with start of the first region. |
| 4355 | // Thus to calculate the beginning of the next region we have to align |
| 4356 | // area_start by Page::kRegionSize. |
| 4357 | Address second_region = |
| 4358 | reinterpret_cast<Address>( |
| 4359 | reinterpret_cast<intptr_t>(area_start + Page::kRegionSize) & |
| 4360 | ~Page::kRegionAlignmentMask); |
| 4361 | |
| 4362 | // Next region might be beyond area_end. |
| 4363 | Address region_end = Min(second_region, area_end); |
| 4364 | |
| 4365 | if (marks & mask) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4366 | if (visit_dirty_region(this, region_start, region_end, copy_object_func)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4367 | newmarks |= mask; |
| 4368 | } |
| 4369 | } |
| 4370 | mask <<= 1; |
| 4371 | |
| 4372 | // Iterate subsequent regions which fully lay inside [area_start, area_end[. |
| 4373 | region_start = region_end; |
| 4374 | region_end = region_start + Page::kRegionSize; |
| 4375 | |
| 4376 | while (region_end <= area_end) { |
| 4377 | if (marks & mask) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4378 | if (visit_dirty_region(this, |
| 4379 | region_start, |
| 4380 | region_end, |
| 4381 | copy_object_func)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4382 | newmarks |= mask; |
| 4383 | } |
| 4384 | } |
| 4385 | |
| 4386 | region_start = region_end; |
| 4387 | region_end = region_start + Page::kRegionSize; |
| 4388 | |
| 4389 | mask <<= 1; |
| 4390 | } |
| 4391 | |
| 4392 | if (region_start != area_end) { |
| 4393 | // A small piece of area left uniterated because area_end does not coincide |
| 4394 | // with region end. Check whether region covering last part of area is |
| 4395 | // dirty. |
| 4396 | if (marks & mask) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4397 | if (visit_dirty_region(this, region_start, area_end, copy_object_func)) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4398 | newmarks |= mask; |
| 4399 | } |
| 4400 | } |
| 4401 | } |
| 4402 | |
| 4403 | return newmarks; |
| 4404 | } |
| 4405 | |
| 4406 | |
| 4407 | |
| 4408 | void Heap::IterateDirtyRegions( |
| 4409 | PagedSpace* space, |
| 4410 | DirtyRegionCallback visit_dirty_region, |
| 4411 | ObjectSlotCallback copy_object_func, |
| 4412 | ExpectedPageWatermarkState expected_page_watermark_state) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4413 | |
| 4414 | PageIterator it(space, PageIterator::PAGES_IN_USE); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4415 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4416 | while (it.has_next()) { |
| 4417 | Page* page = it.next(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4418 | uint32_t marks = page->GetRegionMarks(); |
| 4419 | |
| 4420 | if (marks != Page::kAllRegionsCleanMarks) { |
| 4421 | Address start = page->ObjectAreaStart(); |
| 4422 | |
| 4423 | // Do not try to visit pointers beyond page allocation watermark. |
| 4424 | // Page can contain garbage pointers there. |
| 4425 | Address end; |
| 4426 | |
| 4427 | if ((expected_page_watermark_state == WATERMARK_SHOULD_BE_VALID) || |
| 4428 | page->IsWatermarkValid()) { |
| 4429 | end = page->AllocationWatermark(); |
| 4430 | } else { |
| 4431 | end = page->CachedAllocationWatermark(); |
| 4432 | } |
| 4433 | |
| 4434 | ASSERT(space == old_pointer_space_ || |
| 4435 | (space == map_space_ && |
| 4436 | ((page->ObjectAreaStart() - end) % Map::kSize == 0))); |
| 4437 | |
| 4438 | page->SetRegionMarks(IterateDirtyRegions(marks, |
| 4439 | start, |
| 4440 | end, |
| 4441 | visit_dirty_region, |
| 4442 | copy_object_func)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4443 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4444 | |
| 4445 | // Mark page watermark as invalid to maintain watermark validity invariant. |
| 4446 | // See Page::FlipMeaningOfInvalidatedWatermarkFlag() for details. |
| 4447 | page->InvalidateWatermark(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | |
| 4451 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4452 | void Heap::IterateRoots(ObjectVisitor* v, VisitMode mode) { |
| 4453 | IterateStrongRoots(v, mode); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4454 | IterateWeakRoots(v, mode); |
| 4455 | } |
| 4456 | |
| 4457 | |
| 4458 | void Heap::IterateWeakRoots(ObjectVisitor* v, VisitMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4459 | v->VisitPointer(reinterpret_cast<Object**>(&roots_[kSymbolTableRootIndex])); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4460 | v->Synchronize("symbol_table"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4461 | if (mode != VISIT_ALL_IN_SCAVENGE) { |
| 4462 | // Scavenge collections have special processing for this. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4463 | external_string_table_.Iterate(v); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4464 | } |
| 4465 | v->Synchronize("external_string_table"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
| 4468 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4469 | void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4470 | v->VisitPointers(&roots_[0], &roots_[kStrongRootListLength]); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4471 | v->Synchronize("strong_root_list"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4472 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4473 | v->VisitPointer(BitCast<Object**>(&hidden_symbol_)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4474 | v->Synchronize("symbol"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4475 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4476 | isolate_->bootstrapper()->Iterate(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4477 | v->Synchronize("bootstrapper"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4478 | isolate_->Iterate(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4479 | v->Synchronize("top"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4480 | Relocatable::Iterate(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4481 | v->Synchronize("relocatable"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4482 | |
| 4483 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4484 | isolate_->debug()->Iterate(v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4485 | #endif |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4486 | v->Synchronize("debug"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4487 | isolate_->compilation_cache()->Iterate(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4488 | v->Synchronize("compilationcache"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4489 | |
| 4490 | // Iterate over local handles in handle scopes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4491 | isolate_->handle_scope_implementer()->Iterate(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4492 | v->Synchronize("handlescope"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4493 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4494 | // Iterate over the builtin code objects and code stubs in the |
| 4495 | // heap. Note that it is not necessary to iterate over code objects |
| 4496 | // on scavenge collections. |
| 4497 | if (mode != VISIT_ALL_IN_SCAVENGE) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4498 | isolate_->builtins()->IterateBuiltins(v); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4499 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4500 | v->Synchronize("builtins"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4501 | |
| 4502 | // Iterate over global handles. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4503 | if (mode == VISIT_ONLY_STRONG) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4504 | isolate_->global_handles()->IterateStrongRoots(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4505 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4506 | isolate_->global_handles()->IterateAllRoots(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4507 | } |
| 4508 | v->Synchronize("globalhandles"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4509 | |
| 4510 | // Iterate over pointers being held by inactive threads. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4511 | isolate_->thread_manager()->Iterate(v); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4512 | v->Synchronize("threadmanager"); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4513 | |
| 4514 | // Iterate over the pointers the Serialization/Deserialization code is |
| 4515 | // holding. |
| 4516 | // During garbage collection this keeps the partial snapshot cache alive. |
| 4517 | // During deserialization of the startup snapshot this creates the partial |
| 4518 | // snapshot cache and deserializes the objects it refers to. During |
| 4519 | // serialization this does nothing, since the partial snapshot cache is |
| 4520 | // empty. However the next thing we do is create the partial snapshot, |
| 4521 | // filling up the partial snapshot cache with objects it needs as we go. |
| 4522 | SerializerDeserializer::Iterate(v); |
| 4523 | // We don't do a v->Synchronize call here, because in debug mode that will |
| 4524 | // output a flag to the snapshot. However at this point the serializer and |
| 4525 | // deserializer are deliberately a little unsynchronized (see above) so the |
| 4526 | // checking of the sync flag in the snapshot would fail. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4527 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4528 | |
| 4529 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4530 | // TODO(1236194): Since the heap size is configurable on the command line |
| 4531 | // and through the API, we should gracefully handle the case that the heap |
| 4532 | // size is not big enough to fit all the initial objects. |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 4533 | bool Heap::ConfigureHeap(int max_semispace_size, |
| 4534 | int max_old_gen_size, |
| 4535 | int max_executable_size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4536 | if (HasBeenSetup()) return false; |
| 4537 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4538 | if (max_semispace_size > 0) max_semispace_size_ = max_semispace_size; |
| 4539 | |
| 4540 | if (Snapshot::IsEnabled()) { |
| 4541 | // If we are using a snapshot we always reserve the default amount |
| 4542 | // of memory for each semispace because code in the snapshot has |
| 4543 | // write-barrier code that relies on the size and alignment of new |
| 4544 | // space. We therefore cannot use a larger max semispace size |
| 4545 | // than the default reserved semispace size. |
| 4546 | if (max_semispace_size_ > reserved_semispace_size_) { |
| 4547 | max_semispace_size_ = reserved_semispace_size_; |
| 4548 | } |
| 4549 | } else { |
| 4550 | // If we are not using snapshots we reserve space for the actual |
| 4551 | // max semispace size. |
| 4552 | reserved_semispace_size_ = max_semispace_size_; |
| 4553 | } |
| 4554 | |
| 4555 | if (max_old_gen_size > 0) max_old_generation_size_ = max_old_gen_size; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 4556 | if (max_executable_size > 0) { |
| 4557 | max_executable_size_ = RoundUp(max_executable_size, Page::kPageSize); |
| 4558 | } |
| 4559 | |
| 4560 | // The max executable size must be less than or equal to the max old |
| 4561 | // generation size. |
| 4562 | if (max_executable_size_ > max_old_generation_size_) { |
| 4563 | max_executable_size_ = max_old_generation_size_; |
| 4564 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4565 | |
| 4566 | // The new space size must be a power of two to support single-bit testing |
| 4567 | // for containment. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4568 | max_semispace_size_ = RoundUpToPowerOf2(max_semispace_size_); |
| 4569 | reserved_semispace_size_ = RoundUpToPowerOf2(reserved_semispace_size_); |
| 4570 | initial_semispace_size_ = Min(initial_semispace_size_, max_semispace_size_); |
| 4571 | external_allocation_limit_ = 10 * max_semispace_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4572 | |
| 4573 | // The old generation is paged. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4574 | max_old_generation_size_ = RoundUp(max_old_generation_size_, Page::kPageSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4575 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4576 | configured_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4577 | return true; |
| 4578 | } |
| 4579 | |
| 4580 | |
| 4581 | bool Heap::ConfigureHeapDefault() { |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 4582 | return ConfigureHeap(FLAG_max_new_space_size / 2 * KB, |
| 4583 | FLAG_max_old_space_size * MB, |
| 4584 | FLAG_max_executable_size * MB); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4588 | void Heap::RecordStats(HeapStats* stats, bool take_snapshot) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4589 | *stats->start_marker = HeapStats::kStartMarker; |
| 4590 | *stats->end_marker = HeapStats::kEndMarker; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4591 | *stats->new_space_size = new_space_.SizeAsInt(); |
| 4592 | *stats->new_space_capacity = static_cast<int>(new_space_.Capacity()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4593 | *stats->old_pointer_space_size = old_pointer_space_->Size(); |
| 4594 | *stats->old_pointer_space_capacity = old_pointer_space_->Capacity(); |
| 4595 | *stats->old_data_space_size = old_data_space_->Size(); |
| 4596 | *stats->old_data_space_capacity = old_data_space_->Capacity(); |
| 4597 | *stats->code_space_size = code_space_->Size(); |
| 4598 | *stats->code_space_capacity = code_space_->Capacity(); |
| 4599 | *stats->map_space_size = map_space_->Size(); |
| 4600 | *stats->map_space_capacity = map_space_->Capacity(); |
| 4601 | *stats->cell_space_size = cell_space_->Size(); |
| 4602 | *stats->cell_space_capacity = cell_space_->Capacity(); |
| 4603 | *stats->lo_space_size = lo_space_->Size(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4604 | isolate_->global_handles()->RecordStats(stats); |
| 4605 | *stats->memory_allocator_size = isolate()->memory_allocator()->Size(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4606 | *stats->memory_allocator_capacity = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4607 | isolate()->memory_allocator()->Size() + |
| 4608 | isolate()->memory_allocator()->Available(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4609 | *stats->os_error = OS::GetLastError(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4610 | isolate()->memory_allocator()->Available(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4611 | if (take_snapshot) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 4612 | HeapIterator iterator(HeapIterator::kFilterFreeListNodes); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4613 | for (HeapObject* obj = iterator.next(); |
| 4614 | obj != NULL; |
| 4615 | obj = iterator.next()) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4616 | InstanceType type = obj->map()->instance_type(); |
| 4617 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 4618 | stats->objects_per_type[type]++; |
| 4619 | stats->size_per_type[type] += obj->Size(); |
| 4620 | } |
| 4621 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4625 | intptr_t Heap::PromotedSpaceSize() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4626 | return old_pointer_space_->Size() |
| 4627 | + old_data_space_->Size() |
| 4628 | + code_space_->Size() |
| 4629 | + map_space_->Size() |
| 4630 | + cell_space_->Size() |
| 4631 | + lo_space_->Size(); |
| 4632 | } |
| 4633 | |
| 4634 | |
| 4635 | int Heap::PromotedExternalMemorySize() { |
| 4636 | if (amount_of_external_allocated_memory_ |
| 4637 | <= amount_of_external_allocated_memory_at_last_global_gc_) return 0; |
| 4638 | return amount_of_external_allocated_memory_ |
| 4639 | - amount_of_external_allocated_memory_at_last_global_gc_; |
| 4640 | } |
| 4641 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4642 | #ifdef DEBUG |
| 4643 | |
| 4644 | // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject. |
| 4645 | static const int kMarkTag = 2; |
| 4646 | |
| 4647 | |
| 4648 | class HeapDebugUtils { |
| 4649 | public: |
| 4650 | explicit HeapDebugUtils(Heap* heap) |
| 4651 | : search_for_any_global_(false), |
| 4652 | search_target_(NULL), |
| 4653 | found_target_(false), |
| 4654 | object_stack_(20), |
| 4655 | heap_(heap) { |
| 4656 | } |
| 4657 | |
| 4658 | class MarkObjectVisitor : public ObjectVisitor { |
| 4659 | public: |
| 4660 | explicit MarkObjectVisitor(HeapDebugUtils* utils) : utils_(utils) { } |
| 4661 | |
| 4662 | void VisitPointers(Object** start, Object** end) { |
| 4663 | // Copy all HeapObject pointers in [start, end) |
| 4664 | for (Object** p = start; p < end; p++) { |
| 4665 | if ((*p)->IsHeapObject()) |
| 4666 | utils_->MarkObjectRecursively(p); |
| 4667 | } |
| 4668 | } |
| 4669 | |
| 4670 | HeapDebugUtils* utils_; |
| 4671 | }; |
| 4672 | |
| 4673 | void MarkObjectRecursively(Object** p) { |
| 4674 | if (!(*p)->IsHeapObject()) return; |
| 4675 | |
| 4676 | HeapObject* obj = HeapObject::cast(*p); |
| 4677 | |
| 4678 | Object* map = obj->map(); |
| 4679 | |
| 4680 | if (!map->IsHeapObject()) return; // visited before |
| 4681 | |
| 4682 | if (found_target_) return; // stop if target found |
| 4683 | object_stack_.Add(obj); |
| 4684 | if ((search_for_any_global_ && obj->IsJSGlobalObject()) || |
| 4685 | (!search_for_any_global_ && (obj == search_target_))) { |
| 4686 | found_target_ = true; |
| 4687 | return; |
| 4688 | } |
| 4689 | |
| 4690 | // not visited yet |
| 4691 | Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map)); |
| 4692 | |
| 4693 | Address map_addr = map_p->address(); |
| 4694 | |
| 4695 | obj->set_map(reinterpret_cast<Map*>(map_addr + kMarkTag)); |
| 4696 | |
| 4697 | MarkObjectRecursively(&map); |
| 4698 | |
| 4699 | MarkObjectVisitor mark_visitor(this); |
| 4700 | |
| 4701 | obj->IterateBody(map_p->instance_type(), obj->SizeFromMap(map_p), |
| 4702 | &mark_visitor); |
| 4703 | |
| 4704 | if (!found_target_) // don't pop if found the target |
| 4705 | object_stack_.RemoveLast(); |
| 4706 | } |
| 4707 | |
| 4708 | |
| 4709 | class UnmarkObjectVisitor : public ObjectVisitor { |
| 4710 | public: |
| 4711 | explicit UnmarkObjectVisitor(HeapDebugUtils* utils) : utils_(utils) { } |
| 4712 | |
| 4713 | void VisitPointers(Object** start, Object** end) { |
| 4714 | // Copy all HeapObject pointers in [start, end) |
| 4715 | for (Object** p = start; p < end; p++) { |
| 4716 | if ((*p)->IsHeapObject()) |
| 4717 | utils_->UnmarkObjectRecursively(p); |
| 4718 | } |
| 4719 | } |
| 4720 | |
| 4721 | HeapDebugUtils* utils_; |
| 4722 | }; |
| 4723 | |
| 4724 | |
| 4725 | void UnmarkObjectRecursively(Object** p) { |
| 4726 | if (!(*p)->IsHeapObject()) return; |
| 4727 | |
| 4728 | HeapObject* obj = HeapObject::cast(*p); |
| 4729 | |
| 4730 | Object* map = obj->map(); |
| 4731 | |
| 4732 | if (map->IsHeapObject()) return; // unmarked already |
| 4733 | |
| 4734 | Address map_addr = reinterpret_cast<Address>(map); |
| 4735 | |
| 4736 | map_addr -= kMarkTag; |
| 4737 | |
| 4738 | ASSERT_TAG_ALIGNED(map_addr); |
| 4739 | |
| 4740 | HeapObject* map_p = HeapObject::FromAddress(map_addr); |
| 4741 | |
| 4742 | obj->set_map(reinterpret_cast<Map*>(map_p)); |
| 4743 | |
| 4744 | UnmarkObjectRecursively(reinterpret_cast<Object**>(&map_p)); |
| 4745 | |
| 4746 | UnmarkObjectVisitor unmark_visitor(this); |
| 4747 | |
| 4748 | obj->IterateBody(Map::cast(map_p)->instance_type(), |
| 4749 | obj->SizeFromMap(Map::cast(map_p)), |
| 4750 | &unmark_visitor); |
| 4751 | } |
| 4752 | |
| 4753 | |
| 4754 | void MarkRootObjectRecursively(Object** root) { |
| 4755 | if (search_for_any_global_) { |
| 4756 | ASSERT(search_target_ == NULL); |
| 4757 | } else { |
| 4758 | ASSERT(search_target_->IsHeapObject()); |
| 4759 | } |
| 4760 | found_target_ = false; |
| 4761 | object_stack_.Clear(); |
| 4762 | |
| 4763 | MarkObjectRecursively(root); |
| 4764 | UnmarkObjectRecursively(root); |
| 4765 | |
| 4766 | if (found_target_) { |
| 4767 | PrintF("=====================================\n"); |
| 4768 | PrintF("==== Path to object ====\n"); |
| 4769 | PrintF("=====================================\n\n"); |
| 4770 | |
| 4771 | ASSERT(!object_stack_.is_empty()); |
| 4772 | for (int i = 0; i < object_stack_.length(); i++) { |
| 4773 | if (i > 0) PrintF("\n |\n |\n V\n\n"); |
| 4774 | Object* obj = object_stack_[i]; |
| 4775 | obj->Print(); |
| 4776 | } |
| 4777 | PrintF("=====================================\n"); |
| 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | // Helper class for visiting HeapObjects recursively. |
| 4782 | class MarkRootVisitor: public ObjectVisitor { |
| 4783 | public: |
| 4784 | explicit MarkRootVisitor(HeapDebugUtils* utils) : utils_(utils) { } |
| 4785 | |
| 4786 | void VisitPointers(Object** start, Object** end) { |
| 4787 | // Visit all HeapObject pointers in [start, end) |
| 4788 | for (Object** p = start; p < end; p++) { |
| 4789 | if ((*p)->IsHeapObject()) |
| 4790 | utils_->MarkRootObjectRecursively(p); |
| 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | HeapDebugUtils* utils_; |
| 4795 | }; |
| 4796 | |
| 4797 | bool search_for_any_global_; |
| 4798 | Object* search_target_; |
| 4799 | bool found_target_; |
| 4800 | List<Object*> object_stack_; |
| 4801 | Heap* heap_; |
| 4802 | |
| 4803 | friend class Heap; |
| 4804 | }; |
| 4805 | |
| 4806 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4807 | |
| 4808 | bool Heap::Setup(bool create_heap_objects) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4809 | #ifdef DEBUG |
| 4810 | debug_utils_ = new HeapDebugUtils(this); |
| 4811 | #endif |
| 4812 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4813 | // Initialize heap spaces and initial maps and objects. Whenever something |
| 4814 | // goes wrong, just return false. The caller should check the results and |
| 4815 | // call Heap::TearDown() to release allocated memory. |
| 4816 | // |
| 4817 | // If the heap is not yet configured (eg, through the API), configure it. |
| 4818 | // Configuration is based on the flags new-space-size (really the semispace |
| 4819 | // size) and old-space-size if set or the initial values of semispace_size_ |
| 4820 | // and old_generation_size_ otherwise. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4821 | if (!configured_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4822 | if (!ConfigureHeapDefault()) return false; |
| 4823 | } |
| 4824 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4825 | gc_initializer_mutex->Lock(); |
| 4826 | static bool initialized_gc = false; |
| 4827 | if (!initialized_gc) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4828 | initialized_gc = true; |
| 4829 | InitializeScavengingVisitorsTables(); |
| 4830 | NewSpaceScavenger::Initialize(); |
| 4831 | MarkCompactCollector::Initialize(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4832 | } |
| 4833 | gc_initializer_mutex->Unlock(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4834 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4835 | MarkMapPointersAsEncoded(false); |
| 4836 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4837 | // Setup memory allocator and reserve a chunk of memory for new |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4838 | // space. The chunk is double the size of the requested reserved |
| 4839 | // new space size to ensure that we can find a pair of semispaces that |
| 4840 | // are contiguous and aligned to their size. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4841 | if (!isolate_->memory_allocator()->Setup(MaxReserved(), MaxExecutableSize())) |
| 4842 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4843 | void* chunk = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4844 | isolate_->memory_allocator()->ReserveInitialChunk( |
| 4845 | 4 * reserved_semispace_size_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4846 | if (chunk == NULL) return false; |
| 4847 | |
| 4848 | // Align the pair of semispaces to their size, which must be a power |
| 4849 | // of 2. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4850 | Address new_space_start = |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4851 | RoundUp(reinterpret_cast<byte*>(chunk), 2 * reserved_semispace_size_); |
| 4852 | if (!new_space_.Setup(new_space_start, 2 * reserved_semispace_size_)) { |
| 4853 | return false; |
| 4854 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4855 | |
| 4856 | // Initialize old pointer space. |
| 4857 | old_pointer_space_ = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4858 | new OldSpace(this, |
| 4859 | max_old_generation_size_, |
| 4860 | OLD_POINTER_SPACE, |
| 4861 | NOT_EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4862 | if (old_pointer_space_ == NULL) return false; |
| 4863 | if (!old_pointer_space_->Setup(NULL, 0)) return false; |
| 4864 | |
| 4865 | // Initialize old data space. |
| 4866 | old_data_space_ = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4867 | new OldSpace(this, |
| 4868 | max_old_generation_size_, |
| 4869 | OLD_DATA_SPACE, |
| 4870 | NOT_EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4871 | if (old_data_space_ == NULL) return false; |
| 4872 | if (!old_data_space_->Setup(NULL, 0)) return false; |
| 4873 | |
| 4874 | // Initialize the code space, set its maximum capacity to the old |
| 4875 | // generation size. It needs executable memory. |
| 4876 | // On 64-bit platform(s), we put all code objects in a 2 GB range of |
| 4877 | // virtual address space, so that they can call each other with near calls. |
| 4878 | if (code_range_size_ > 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4879 | if (!isolate_->code_range()->Setup(code_range_size_)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4880 | return false; |
| 4881 | } |
| 4882 | } |
| 4883 | |
| 4884 | code_space_ = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4885 | new OldSpace(this, max_old_generation_size_, CODE_SPACE, EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4886 | if (code_space_ == NULL) return false; |
| 4887 | if (!code_space_->Setup(NULL, 0)) return false; |
| 4888 | |
| 4889 | // Initialize map space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4890 | map_space_ = new MapSpace(this, FLAG_use_big_map_space |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4891 | ? max_old_generation_size_ |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4892 | : MapSpace::kMaxMapPageIndex * Page::kPageSize, |
| 4893 | FLAG_max_map_space_pages, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4894 | MAP_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4895 | if (map_space_ == NULL) return false; |
| 4896 | if (!map_space_->Setup(NULL, 0)) return false; |
| 4897 | |
| 4898 | // Initialize global property cell space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4899 | cell_space_ = new CellSpace(this, max_old_generation_size_, CELL_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4900 | if (cell_space_ == NULL) return false; |
| 4901 | if (!cell_space_->Setup(NULL, 0)) return false; |
| 4902 | |
| 4903 | // The large object code space may contain code or data. We set the memory |
| 4904 | // to be non-executable here for safety, but this means we need to enable it |
| 4905 | // explicitly when allocating large code objects. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4906 | lo_space_ = new LargeObjectSpace(this, LO_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4907 | if (lo_space_ == NULL) return false; |
| 4908 | if (!lo_space_->Setup()) return false; |
| 4909 | |
| 4910 | if (create_heap_objects) { |
| 4911 | // Create initial maps. |
| 4912 | if (!CreateInitialMaps()) return false; |
| 4913 | if (!CreateApiObjects()) return false; |
| 4914 | |
| 4915 | // Create initial objects |
| 4916 | if (!CreateInitialObjects()) return false; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4917 | |
| 4918 | global_contexts_list_ = undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4919 | } |
| 4920 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4921 | LOG(isolate_, IntPtrTEvent("heap-capacity", Capacity())); |
| 4922 | LOG(isolate_, IntPtrTEvent("heap-available", Available())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4923 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4924 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 4925 | // This should be called only after initial objects have been created. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4926 | isolate_->producer_heap_profile()->Setup(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4927 | #endif |
| 4928 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4929 | return true; |
| 4930 | } |
| 4931 | |
| 4932 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4933 | void Heap::SetStackLimits() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4934 | ASSERT(isolate_ != NULL); |
| 4935 | ASSERT(isolate_ == isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4936 | // On 64 bit machines, pointers are generally out of range of Smis. We write |
| 4937 | // something that looks like an out of range Smi to the GC. |
| 4938 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4939 | // Set up the special root array entries containing the stack limits. |
| 4940 | // These are actually addresses, but the tag makes the GC ignore it. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4941 | roots_[kStackLimitRootIndex] = |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4942 | reinterpret_cast<Object*>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4943 | (isolate_->stack_guard()->jslimit() & ~kSmiTagMask) | kSmiTag); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4944 | roots_[kRealStackLimitRootIndex] = |
| 4945 | reinterpret_cast<Object*>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4946 | (isolate_->stack_guard()->real_jslimit() & ~kSmiTagMask) | kSmiTag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | |
| 4950 | void Heap::TearDown() { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4951 | if (FLAG_print_cumulative_gc_stat) { |
| 4952 | PrintF("\n\n"); |
| 4953 | PrintF("gc_count=%d ", gc_count_); |
| 4954 | PrintF("mark_sweep_count=%d ", ms_count_); |
| 4955 | PrintF("mark_compact_count=%d ", mc_count_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4956 | PrintF("max_gc_pause=%d ", get_max_gc_pause()); |
| 4957 | PrintF("min_in_mutator=%d ", get_min_in_mutator()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 4958 | PrintF("max_alive_after_gc=%" V8_PTR_PREFIX "d ", |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4959 | get_max_alive_after_gc()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4960 | PrintF("\n\n"); |
| 4961 | } |
| 4962 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4963 | isolate_->global_handles()->TearDown(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4964 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4965 | external_string_table_.TearDown(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4966 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4967 | new_space_.TearDown(); |
| 4968 | |
| 4969 | if (old_pointer_space_ != NULL) { |
| 4970 | old_pointer_space_->TearDown(); |
| 4971 | delete old_pointer_space_; |
| 4972 | old_pointer_space_ = NULL; |
| 4973 | } |
| 4974 | |
| 4975 | if (old_data_space_ != NULL) { |
| 4976 | old_data_space_->TearDown(); |
| 4977 | delete old_data_space_; |
| 4978 | old_data_space_ = NULL; |
| 4979 | } |
| 4980 | |
| 4981 | if (code_space_ != NULL) { |
| 4982 | code_space_->TearDown(); |
| 4983 | delete code_space_; |
| 4984 | code_space_ = NULL; |
| 4985 | } |
| 4986 | |
| 4987 | if (map_space_ != NULL) { |
| 4988 | map_space_->TearDown(); |
| 4989 | delete map_space_; |
| 4990 | map_space_ = NULL; |
| 4991 | } |
| 4992 | |
| 4993 | if (cell_space_ != NULL) { |
| 4994 | cell_space_->TearDown(); |
| 4995 | delete cell_space_; |
| 4996 | cell_space_ = NULL; |
| 4997 | } |
| 4998 | |
| 4999 | if (lo_space_ != NULL) { |
| 5000 | lo_space_->TearDown(); |
| 5001 | delete lo_space_; |
| 5002 | lo_space_ = NULL; |
| 5003 | } |
| 5004 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5005 | isolate_->memory_allocator()->TearDown(); |
| 5006 | |
| 5007 | #ifdef DEBUG |
| 5008 | delete debug_utils_; |
| 5009 | debug_utils_ = NULL; |
| 5010 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
| 5013 | |
| 5014 | void Heap::Shrink() { |
| 5015 | // Try to shrink all paged spaces. |
| 5016 | PagedSpaces spaces; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5017 | for (PagedSpace* space = spaces.next(); space != NULL; space = spaces.next()) |
| 5018 | space->Shrink(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
| 5021 | |
| 5022 | #ifdef ENABLE_HEAP_PROTECTION |
| 5023 | |
| 5024 | void Heap::Protect() { |
| 5025 | if (HasBeenSetup()) { |
| 5026 | AllSpaces spaces; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5027 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 5028 | space->Protect(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | |
| 5033 | void Heap::Unprotect() { |
| 5034 | if (HasBeenSetup()) { |
| 5035 | AllSpaces spaces; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5036 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 5037 | space->Unprotect(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | #endif |
| 5042 | |
| 5043 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5044 | void Heap::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) { |
| 5045 | ASSERT(callback != NULL); |
| 5046 | GCPrologueCallbackPair pair(callback, gc_type); |
| 5047 | ASSERT(!gc_prologue_callbacks_.Contains(pair)); |
| 5048 | return gc_prologue_callbacks_.Add(pair); |
| 5049 | } |
| 5050 | |
| 5051 | |
| 5052 | void Heap::RemoveGCPrologueCallback(GCPrologueCallback callback) { |
| 5053 | ASSERT(callback != NULL); |
| 5054 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 5055 | if (gc_prologue_callbacks_[i].callback == callback) { |
| 5056 | gc_prologue_callbacks_.Remove(i); |
| 5057 | return; |
| 5058 | } |
| 5059 | } |
| 5060 | UNREACHABLE(); |
| 5061 | } |
| 5062 | |
| 5063 | |
| 5064 | void Heap::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) { |
| 5065 | ASSERT(callback != NULL); |
| 5066 | GCEpilogueCallbackPair pair(callback, gc_type); |
| 5067 | ASSERT(!gc_epilogue_callbacks_.Contains(pair)); |
| 5068 | return gc_epilogue_callbacks_.Add(pair); |
| 5069 | } |
| 5070 | |
| 5071 | |
| 5072 | void Heap::RemoveGCEpilogueCallback(GCEpilogueCallback callback) { |
| 5073 | ASSERT(callback != NULL); |
| 5074 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 5075 | if (gc_epilogue_callbacks_[i].callback == callback) { |
| 5076 | gc_epilogue_callbacks_.Remove(i); |
| 5077 | return; |
| 5078 | } |
| 5079 | } |
| 5080 | UNREACHABLE(); |
| 5081 | } |
| 5082 | |
| 5083 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5084 | #ifdef DEBUG |
| 5085 | |
| 5086 | class PrintHandleVisitor: public ObjectVisitor { |
| 5087 | public: |
| 5088 | void VisitPointers(Object** start, Object** end) { |
| 5089 | for (Object** p = start; p < end; p++) |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5090 | PrintF(" handle %p to %p\n", |
| 5091 | reinterpret_cast<void*>(p), |
| 5092 | reinterpret_cast<void*>(*p)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5093 | } |
| 5094 | }; |
| 5095 | |
| 5096 | void Heap::PrintHandles() { |
| 5097 | PrintF("Handles:\n"); |
| 5098 | PrintHandleVisitor v; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5099 | isolate_->handle_scope_implementer()->Iterate(&v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5100 | } |
| 5101 | |
| 5102 | #endif |
| 5103 | |
| 5104 | |
| 5105 | Space* AllSpaces::next() { |
| 5106 | switch (counter_++) { |
| 5107 | case NEW_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5108 | return HEAP->new_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5109 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5110 | return HEAP->old_pointer_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5111 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5112 | return HEAP->old_data_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5113 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5114 | return HEAP->code_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5115 | case MAP_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5116 | return HEAP->map_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5117 | case CELL_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5118 | return HEAP->cell_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5119 | case LO_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5120 | return HEAP->lo_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5121 | default: |
| 5122 | return NULL; |
| 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | |
| 5127 | PagedSpace* PagedSpaces::next() { |
| 5128 | switch (counter_++) { |
| 5129 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5130 | return HEAP->old_pointer_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5131 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5132 | return HEAP->old_data_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5133 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5134 | return HEAP->code_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5135 | case MAP_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5136 | return HEAP->map_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5137 | case CELL_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5138 | return HEAP->cell_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5139 | default: |
| 5140 | return NULL; |
| 5141 | } |
| 5142 | } |
| 5143 | |
| 5144 | |
| 5145 | |
| 5146 | OldSpace* OldSpaces::next() { |
| 5147 | switch (counter_++) { |
| 5148 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5149 | return HEAP->old_pointer_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5150 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5151 | return HEAP->old_data_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5152 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5153 | return HEAP->code_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5154 | default: |
| 5155 | return NULL; |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5160 | SpaceIterator::SpaceIterator() |
| 5161 | : current_space_(FIRST_SPACE), |
| 5162 | iterator_(NULL), |
| 5163 | size_func_(NULL) { |
| 5164 | } |
| 5165 | |
| 5166 | |
| 5167 | SpaceIterator::SpaceIterator(HeapObjectCallback size_func) |
| 5168 | : current_space_(FIRST_SPACE), |
| 5169 | iterator_(NULL), |
| 5170 | size_func_(size_func) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | |
| 5174 | SpaceIterator::~SpaceIterator() { |
| 5175 | // Delete active iterator if any. |
| 5176 | delete iterator_; |
| 5177 | } |
| 5178 | |
| 5179 | |
| 5180 | bool SpaceIterator::has_next() { |
| 5181 | // Iterate until no more spaces. |
| 5182 | return current_space_ != LAST_SPACE; |
| 5183 | } |
| 5184 | |
| 5185 | |
| 5186 | ObjectIterator* SpaceIterator::next() { |
| 5187 | if (iterator_ != NULL) { |
| 5188 | delete iterator_; |
| 5189 | iterator_ = NULL; |
| 5190 | // Move to the next space |
| 5191 | current_space_++; |
| 5192 | if (current_space_ > LAST_SPACE) { |
| 5193 | return NULL; |
| 5194 | } |
| 5195 | } |
| 5196 | |
| 5197 | // Return iterator for the new current space. |
| 5198 | return CreateIterator(); |
| 5199 | } |
| 5200 | |
| 5201 | |
| 5202 | // Create an iterator for the space to iterate. |
| 5203 | ObjectIterator* SpaceIterator::CreateIterator() { |
| 5204 | ASSERT(iterator_ == NULL); |
| 5205 | |
| 5206 | switch (current_space_) { |
| 5207 | case NEW_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5208 | iterator_ = new SemiSpaceIterator(HEAP->new_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5209 | break; |
| 5210 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5211 | iterator_ = new HeapObjectIterator(HEAP->old_pointer_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5212 | break; |
| 5213 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5214 | iterator_ = new HeapObjectIterator(HEAP->old_data_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5215 | break; |
| 5216 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5217 | iterator_ = new HeapObjectIterator(HEAP->code_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5218 | break; |
| 5219 | case MAP_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5220 | iterator_ = new HeapObjectIterator(HEAP->map_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5221 | break; |
| 5222 | case CELL_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5223 | iterator_ = new HeapObjectIterator(HEAP->cell_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5224 | break; |
| 5225 | case LO_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5226 | iterator_ = new LargeObjectIterator(HEAP->lo_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5227 | break; |
| 5228 | } |
| 5229 | |
| 5230 | // Return the newly allocated iterator; |
| 5231 | ASSERT(iterator_ != NULL); |
| 5232 | return iterator_; |
| 5233 | } |
| 5234 | |
| 5235 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5236 | class HeapObjectsFilter { |
| 5237 | public: |
| 5238 | virtual ~HeapObjectsFilter() {} |
| 5239 | virtual bool SkipObject(HeapObject* object) = 0; |
| 5240 | }; |
| 5241 | |
| 5242 | |
| 5243 | class FreeListNodesFilter : public HeapObjectsFilter { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5244 | public: |
| 5245 | FreeListNodesFilter() { |
| 5246 | MarkFreeListNodes(); |
| 5247 | } |
| 5248 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5249 | bool SkipObject(HeapObject* object) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5250 | if (object->IsMarked()) { |
| 5251 | object->ClearMark(); |
| 5252 | return true; |
| 5253 | } else { |
| 5254 | return false; |
| 5255 | } |
| 5256 | } |
| 5257 | |
| 5258 | private: |
| 5259 | void MarkFreeListNodes() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5260 | Heap* heap = HEAP; |
| 5261 | heap->old_pointer_space()->MarkFreeListNodes(); |
| 5262 | heap->old_data_space()->MarkFreeListNodes(); |
| 5263 | MarkCodeSpaceFreeListNodes(heap); |
| 5264 | heap->map_space()->MarkFreeListNodes(); |
| 5265 | heap->cell_space()->MarkFreeListNodes(); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5266 | } |
| 5267 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5268 | void MarkCodeSpaceFreeListNodes(Heap* heap) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5269 | // For code space, using FreeListNode::IsFreeListNode is OK. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5270 | HeapObjectIterator iter(heap->code_space()); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5271 | for (HeapObject* obj = iter.next_object(); |
| 5272 | obj != NULL; |
| 5273 | obj = iter.next_object()) { |
| 5274 | if (FreeListNode::IsFreeListNode(obj)) obj->SetMark(); |
| 5275 | } |
| 5276 | } |
| 5277 | |
| 5278 | AssertNoAllocation no_alloc; |
| 5279 | }; |
| 5280 | |
| 5281 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5282 | class UnreachableObjectsFilter : public HeapObjectsFilter { |
| 5283 | public: |
| 5284 | UnreachableObjectsFilter() { |
| 5285 | MarkUnreachableObjects(); |
| 5286 | } |
| 5287 | |
| 5288 | bool SkipObject(HeapObject* object) { |
| 5289 | if (object->IsMarked()) { |
| 5290 | object->ClearMark(); |
| 5291 | return true; |
| 5292 | } else { |
| 5293 | return false; |
| 5294 | } |
| 5295 | } |
| 5296 | |
| 5297 | private: |
| 5298 | class UnmarkingVisitor : public ObjectVisitor { |
| 5299 | public: |
| 5300 | UnmarkingVisitor() : list_(10) {} |
| 5301 | |
| 5302 | void VisitPointers(Object** start, Object** end) { |
| 5303 | for (Object** p = start; p < end; p++) { |
| 5304 | if (!(*p)->IsHeapObject()) continue; |
| 5305 | HeapObject* obj = HeapObject::cast(*p); |
| 5306 | if (obj->IsMarked()) { |
| 5307 | obj->ClearMark(); |
| 5308 | list_.Add(obj); |
| 5309 | } |
| 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | bool can_process() { return !list_.is_empty(); } |
| 5314 | |
| 5315 | void ProcessNext() { |
| 5316 | HeapObject* obj = list_.RemoveLast(); |
| 5317 | obj->Iterate(this); |
| 5318 | } |
| 5319 | |
| 5320 | private: |
| 5321 | List<HeapObject*> list_; |
| 5322 | }; |
| 5323 | |
| 5324 | void MarkUnreachableObjects() { |
| 5325 | HeapIterator iterator; |
| 5326 | for (HeapObject* obj = iterator.next(); |
| 5327 | obj != NULL; |
| 5328 | obj = iterator.next()) { |
| 5329 | obj->SetMark(); |
| 5330 | } |
| 5331 | UnmarkingVisitor visitor; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5332 | HEAP->IterateRoots(&visitor, VISIT_ALL); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5333 | while (visitor.can_process()) |
| 5334 | visitor.ProcessNext(); |
| 5335 | } |
| 5336 | |
| 5337 | AssertNoAllocation no_alloc; |
| 5338 | }; |
| 5339 | |
| 5340 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5341 | HeapIterator::HeapIterator() |
| 5342 | : filtering_(HeapIterator::kNoFiltering), |
| 5343 | filter_(NULL) { |
| 5344 | Init(); |
| 5345 | } |
| 5346 | |
| 5347 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5348 | HeapIterator::HeapIterator(HeapIterator::HeapObjectsFiltering filtering) |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5349 | : filtering_(filtering), |
| 5350 | filter_(NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5351 | Init(); |
| 5352 | } |
| 5353 | |
| 5354 | |
| 5355 | HeapIterator::~HeapIterator() { |
| 5356 | Shutdown(); |
| 5357 | } |
| 5358 | |
| 5359 | |
| 5360 | void HeapIterator::Init() { |
| 5361 | // Start the iteration. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5362 | space_iterator_ = filtering_ == kNoFiltering ? new SpaceIterator : |
| 5363 | new SpaceIterator(MarkCompactCollector::SizeOfMarkedObject); |
| 5364 | switch (filtering_) { |
| 5365 | case kFilterFreeListNodes: |
| 5366 | filter_ = new FreeListNodesFilter; |
| 5367 | break; |
| 5368 | case kFilterUnreachable: |
| 5369 | filter_ = new UnreachableObjectsFilter; |
| 5370 | break; |
| 5371 | default: |
| 5372 | break; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5373 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5374 | object_iterator_ = space_iterator_->next(); |
| 5375 | } |
| 5376 | |
| 5377 | |
| 5378 | void HeapIterator::Shutdown() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5379 | #ifdef DEBUG |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5380 | // Assert that in filtering mode we have iterated through all |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5381 | // objects. Otherwise, heap will be left in an inconsistent state. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5382 | if (filtering_ != kNoFiltering) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5383 | ASSERT(object_iterator_ == NULL); |
| 5384 | } |
| 5385 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5386 | // Make sure the last iterator is deallocated. |
| 5387 | delete space_iterator_; |
| 5388 | space_iterator_ = NULL; |
| 5389 | object_iterator_ = NULL; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5390 | delete filter_; |
| 5391 | filter_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5395 | HeapObject* HeapIterator::next() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5396 | if (filter_ == NULL) return NextObject(); |
| 5397 | |
| 5398 | HeapObject* obj = NextObject(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 5399 | while (obj != NULL && filter_->SkipObject(obj)) obj = NextObject(); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 5400 | return obj; |
| 5401 | } |
| 5402 | |
| 5403 | |
| 5404 | HeapObject* HeapIterator::NextObject() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5405 | // No iterator means we are done. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5406 | if (object_iterator_ == NULL) return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5407 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5408 | if (HeapObject* obj = object_iterator_->next_object()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5409 | // If the current iterator has more objects we are fine. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5410 | return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5411 | } else { |
| 5412 | // Go though the spaces looking for one that has objects. |
| 5413 | while (space_iterator_->has_next()) { |
| 5414 | object_iterator_ = space_iterator_->next(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5415 | if (HeapObject* obj = object_iterator_->next_object()) { |
| 5416 | return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5417 | } |
| 5418 | } |
| 5419 | } |
| 5420 | // Done with the last space. |
| 5421 | object_iterator_ = NULL; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5422 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
| 5425 | |
| 5426 | void HeapIterator::reset() { |
| 5427 | // Restart the iterator. |
| 5428 | Shutdown(); |
| 5429 | Init(); |
| 5430 | } |
| 5431 | |
| 5432 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5433 | #if defined(DEBUG) || defined(LIVE_OBJECT_LIST) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5434 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5435 | Object* const PathTracer::kAnyGlobalObject = reinterpret_cast<Object*>(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5436 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5437 | class PathTracer::MarkVisitor: public ObjectVisitor { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5438 | public: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5439 | explicit MarkVisitor(PathTracer* tracer) : tracer_(tracer) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5440 | void VisitPointers(Object** start, Object** end) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5441 | // Scan all HeapObject pointers in [start, end) |
| 5442 | for (Object** p = start; !tracer_->found() && (p < end); p++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5443 | if ((*p)->IsHeapObject()) |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5444 | tracer_->MarkRecursively(p, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5445 | } |
| 5446 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5447 | |
| 5448 | private: |
| 5449 | PathTracer* tracer_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5450 | }; |
| 5451 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5452 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5453 | class PathTracer::UnmarkVisitor: public ObjectVisitor { |
| 5454 | public: |
| 5455 | explicit UnmarkVisitor(PathTracer* tracer) : tracer_(tracer) {} |
| 5456 | void VisitPointers(Object** start, Object** end) { |
| 5457 | // Scan all HeapObject pointers in [start, end) |
| 5458 | for (Object** p = start; p < end; p++) { |
| 5459 | if ((*p)->IsHeapObject()) |
| 5460 | tracer_->UnmarkRecursively(p, this); |
| 5461 | } |
| 5462 | } |
| 5463 | |
| 5464 | private: |
| 5465 | PathTracer* tracer_; |
| 5466 | }; |
| 5467 | |
| 5468 | |
| 5469 | void PathTracer::VisitPointers(Object** start, Object** end) { |
| 5470 | bool done = ((what_to_find_ == FIND_FIRST) && found_target_); |
| 5471 | // Visit all HeapObject pointers in [start, end) |
| 5472 | for (Object** p = start; !done && (p < end); p++) { |
| 5473 | if ((*p)->IsHeapObject()) { |
| 5474 | TracePathFrom(p); |
| 5475 | done = ((what_to_find_ == FIND_FIRST) && found_target_); |
| 5476 | } |
| 5477 | } |
| 5478 | } |
| 5479 | |
| 5480 | |
| 5481 | void PathTracer::Reset() { |
| 5482 | found_target_ = false; |
| 5483 | object_stack_.Clear(); |
| 5484 | } |
| 5485 | |
| 5486 | |
| 5487 | void PathTracer::TracePathFrom(Object** root) { |
| 5488 | ASSERT((search_target_ == kAnyGlobalObject) || |
| 5489 | search_target_->IsHeapObject()); |
| 5490 | found_target_in_trace_ = false; |
| 5491 | object_stack_.Clear(); |
| 5492 | |
| 5493 | MarkVisitor mark_visitor(this); |
| 5494 | MarkRecursively(root, &mark_visitor); |
| 5495 | |
| 5496 | UnmarkVisitor unmark_visitor(this); |
| 5497 | UnmarkRecursively(root, &unmark_visitor); |
| 5498 | |
| 5499 | ProcessResults(); |
| 5500 | } |
| 5501 | |
| 5502 | |
| 5503 | void PathTracer::MarkRecursively(Object** p, MarkVisitor* mark_visitor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5504 | if (!(*p)->IsHeapObject()) return; |
| 5505 | |
| 5506 | HeapObject* obj = HeapObject::cast(*p); |
| 5507 | |
| 5508 | Object* map = obj->map(); |
| 5509 | |
| 5510 | if (!map->IsHeapObject()) return; // visited before |
| 5511 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5512 | if (found_target_in_trace_) return; // stop if target found |
| 5513 | object_stack_.Add(obj); |
| 5514 | if (((search_target_ == kAnyGlobalObject) && obj->IsJSGlobalObject()) || |
| 5515 | (obj == search_target_)) { |
| 5516 | found_target_in_trace_ = true; |
| 5517 | found_target_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5518 | return; |
| 5519 | } |
| 5520 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5521 | bool is_global_context = obj->IsGlobalContext(); |
| 5522 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5523 | // not visited yet |
| 5524 | Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map)); |
| 5525 | |
| 5526 | Address map_addr = map_p->address(); |
| 5527 | |
| 5528 | obj->set_map(reinterpret_cast<Map*>(map_addr + kMarkTag)); |
| 5529 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5530 | // Scan the object body. |
| 5531 | if (is_global_context && (visit_mode_ == VISIT_ONLY_STRONG)) { |
| 5532 | // This is specialized to scan Context's properly. |
| 5533 | Object** start = reinterpret_cast<Object**>(obj->address() + |
| 5534 | Context::kHeaderSize); |
| 5535 | Object** end = reinterpret_cast<Object**>(obj->address() + |
| 5536 | Context::kHeaderSize + Context::FIRST_WEAK_SLOT * kPointerSize); |
| 5537 | mark_visitor->VisitPointers(start, end); |
| 5538 | } else { |
| 5539 | obj->IterateBody(map_p->instance_type(), |
| 5540 | obj->SizeFromMap(map_p), |
| 5541 | mark_visitor); |
| 5542 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5543 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5544 | // Scan the map after the body because the body is a lot more interesting |
| 5545 | // when doing leak detection. |
| 5546 | MarkRecursively(&map, mark_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5547 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5548 | if (!found_target_in_trace_) // don't pop if found the target |
| 5549 | object_stack_.RemoveLast(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5553 | void PathTracer::UnmarkRecursively(Object** p, UnmarkVisitor* unmark_visitor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5554 | if (!(*p)->IsHeapObject()) return; |
| 5555 | |
| 5556 | HeapObject* obj = HeapObject::cast(*p); |
| 5557 | |
| 5558 | Object* map = obj->map(); |
| 5559 | |
| 5560 | if (map->IsHeapObject()) return; // unmarked already |
| 5561 | |
| 5562 | Address map_addr = reinterpret_cast<Address>(map); |
| 5563 | |
| 5564 | map_addr -= kMarkTag; |
| 5565 | |
| 5566 | ASSERT_TAG_ALIGNED(map_addr); |
| 5567 | |
| 5568 | HeapObject* map_p = HeapObject::FromAddress(map_addr); |
| 5569 | |
| 5570 | obj->set_map(reinterpret_cast<Map*>(map_p)); |
| 5571 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5572 | UnmarkRecursively(reinterpret_cast<Object**>(&map_p), unmark_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5573 | |
| 5574 | obj->IterateBody(Map::cast(map_p)->instance_type(), |
| 5575 | obj->SizeFromMap(Map::cast(map_p)), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5576 | unmark_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5580 | void PathTracer::ProcessResults() { |
| 5581 | if (found_target_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5582 | PrintF("=====================================\n"); |
| 5583 | PrintF("==== Path to object ====\n"); |
| 5584 | PrintF("=====================================\n\n"); |
| 5585 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5586 | ASSERT(!object_stack_.is_empty()); |
| 5587 | for (int i = 0; i < object_stack_.length(); i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5588 | if (i > 0) PrintF("\n |\n |\n V\n\n"); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5589 | Object* obj = object_stack_[i]; |
| 5590 | #ifdef OBJECT_PRINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5591 | obj->Print(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5592 | #else |
| 5593 | obj->ShortPrint(); |
| 5594 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5595 | } |
| 5596 | PrintF("=====================================\n"); |
| 5597 | } |
| 5598 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5599 | #endif // DEBUG || LIVE_OBJECT_LIST |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5600 | |
| 5601 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5602 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5603 | // Triggers a depth-first traversal of reachable objects from roots |
| 5604 | // and finds a path to a specific heap object and prints it. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5605 | void Heap::TracePathToObject(Object* target) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5606 | PathTracer tracer(target, PathTracer::FIND_ALL, VISIT_ALL); |
| 5607 | IterateRoots(&tracer, VISIT_ONLY_STRONG); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | |
| 5611 | // Triggers a depth-first traversal of reachable objects from roots |
| 5612 | // and finds a path to any global object and prints it. Useful for |
| 5613 | // determining the source for leaks of global objects. |
| 5614 | void Heap::TracePathToGlobal() { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 5615 | PathTracer tracer(PathTracer::kAnyGlobalObject, |
| 5616 | PathTracer::FIND_ALL, |
| 5617 | VISIT_ALL); |
| 5618 | IterateRoots(&tracer, VISIT_ONLY_STRONG); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5619 | } |
| 5620 | #endif |
| 5621 | |
| 5622 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5623 | static intptr_t CountTotalHolesSize() { |
| 5624 | intptr_t holes_size = 0; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5625 | OldSpaces spaces; |
| 5626 | for (OldSpace* space = spaces.next(); |
| 5627 | space != NULL; |
| 5628 | space = spaces.next()) { |
| 5629 | holes_size += space->Waste() + space->AvailableFree(); |
| 5630 | } |
| 5631 | return holes_size; |
| 5632 | } |
| 5633 | |
| 5634 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5635 | GCTracer::GCTracer(Heap* heap) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5636 | : start_time_(0.0), |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5637 | start_size_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5638 | gc_count_(0), |
| 5639 | full_gc_count_(0), |
| 5640 | is_compacting_(false), |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5641 | marked_count_(0), |
| 5642 | allocated_since_last_gc_(0), |
| 5643 | spent_in_mutator_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5644 | promoted_objects_size_(0), |
| 5645 | heap_(heap) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5646 | // These two fields reflect the state of the previous full collection. |
| 5647 | // Set them before they are changed by the collector. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5648 | previous_has_compacted_ = heap_->mark_compact_collector_.HasCompacted(); |
| 5649 | previous_marked_count_ = |
| 5650 | heap_->mark_compact_collector_.previous_marked_count(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5651 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5652 | start_time_ = OS::TimeCurrentMillis(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5653 | start_size_ = heap_->SizeOfObjects(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5654 | |
| 5655 | for (int i = 0; i < Scope::kNumberOfScopes; i++) { |
| 5656 | scopes_[i] = 0; |
| 5657 | } |
| 5658 | |
| 5659 | in_free_list_or_wasted_before_gc_ = CountTotalHolesSize(); |
| 5660 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5661 | allocated_since_last_gc_ = |
| 5662 | heap_->SizeOfObjects() - heap_->alive_after_last_gc_; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5663 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5664 | if (heap_->last_gc_end_timestamp_ > 0) { |
| 5665 | spent_in_mutator_ = Max(start_time_ - heap_->last_gc_end_timestamp_, 0.0); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5666 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | |
| 5670 | GCTracer::~GCTracer() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5671 | // Printf ONE line iff flag is set. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5672 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
| 5673 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5674 | bool first_gc = (heap_->last_gc_end_timestamp_ == 0); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5675 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5676 | heap_->alive_after_last_gc_ = heap_->SizeOfObjects(); |
| 5677 | heap_->last_gc_end_timestamp_ = OS::TimeCurrentMillis(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5678 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5679 | int time = static_cast<int>(heap_->last_gc_end_timestamp_ - start_time_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5680 | |
| 5681 | // Update cumulative GC statistics if required. |
| 5682 | if (FLAG_print_cumulative_gc_stat) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5683 | heap_->max_gc_pause_ = Max(heap_->max_gc_pause_, time); |
| 5684 | heap_->max_alive_after_gc_ = Max(heap_->max_alive_after_gc_, |
| 5685 | heap_->alive_after_last_gc_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5686 | if (!first_gc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5687 | heap_->min_in_mutator_ = Min(heap_->min_in_mutator_, |
| 5688 | static_cast<int>(spent_in_mutator_)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5689 | } |
| 5690 | } |
| 5691 | |
| 5692 | if (!FLAG_trace_gc_nvp) { |
| 5693 | int external_time = static_cast<int>(scopes_[Scope::EXTERNAL]); |
| 5694 | |
| 5695 | PrintF("%s %.1f -> %.1f MB, ", |
| 5696 | CollectorString(), |
| 5697 | static_cast<double>(start_size_) / MB, |
| 5698 | SizeOfHeapObjects()); |
| 5699 | |
| 5700 | if (external_time > 0) PrintF("%d / ", external_time); |
| 5701 | PrintF("%d ms.\n", time); |
| 5702 | } else { |
| 5703 | PrintF("pause=%d ", time); |
| 5704 | PrintF("mutator=%d ", |
| 5705 | static_cast<int>(spent_in_mutator_)); |
| 5706 | |
| 5707 | PrintF("gc="); |
| 5708 | switch (collector_) { |
| 5709 | case SCAVENGER: |
| 5710 | PrintF("s"); |
| 5711 | break; |
| 5712 | case MARK_COMPACTOR: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5713 | PrintF("%s", |
| 5714 | heap_->mark_compact_collector_.HasCompacted() ? "mc" : "ms"); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5715 | break; |
| 5716 | default: |
| 5717 | UNREACHABLE(); |
| 5718 | } |
| 5719 | PrintF(" "); |
| 5720 | |
| 5721 | PrintF("external=%d ", static_cast<int>(scopes_[Scope::EXTERNAL])); |
| 5722 | PrintF("mark=%d ", static_cast<int>(scopes_[Scope::MC_MARK])); |
| 5723 | PrintF("sweep=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP])); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5724 | PrintF("sweepns=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP_NEWSPACE])); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5725 | PrintF("compact=%d ", static_cast<int>(scopes_[Scope::MC_COMPACT])); |
| 5726 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5727 | PrintF("total_size_before=%" V8_PTR_PREFIX "d ", start_size_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5728 | PrintF("total_size_after=%" V8_PTR_PREFIX "d ", heap_->SizeOfObjects()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5729 | PrintF("holes_size_before=%" V8_PTR_PREFIX "d ", |
| 5730 | in_free_list_or_wasted_before_gc_); |
| 5731 | PrintF("holes_size_after=%" V8_PTR_PREFIX "d ", CountTotalHolesSize()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5732 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5733 | PrintF("allocated=%" V8_PTR_PREFIX "d ", allocated_since_last_gc_); |
| 5734 | PrintF("promoted=%" V8_PTR_PREFIX "d ", promoted_objects_size_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5735 | |
| 5736 | PrintF("\n"); |
| 5737 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5738 | |
| 5739 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5740 | heap_->PrintShortHeapStatistics(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5741 | #endif |
| 5742 | } |
| 5743 | |
| 5744 | |
| 5745 | const char* GCTracer::CollectorString() { |
| 5746 | switch (collector_) { |
| 5747 | case SCAVENGER: |
| 5748 | return "Scavenge"; |
| 5749 | case MARK_COMPACTOR: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5750 | return heap_->mark_compact_collector_.HasCompacted() ? "Mark-compact" |
| 5751 | : "Mark-sweep"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5752 | } |
| 5753 | return "Unknown GC"; |
| 5754 | } |
| 5755 | |
| 5756 | |
| 5757 | int KeyedLookupCache::Hash(Map* map, String* name) { |
| 5758 | // Uses only lower 32 bits if pointers are larger. |
| 5759 | uintptr_t addr_hash = |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5760 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)) >> kMapHashShift; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 5761 | return static_cast<uint32_t>((addr_hash ^ name->Hash()) & kCapacityMask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | |
| 5765 | int KeyedLookupCache::Lookup(Map* map, String* name) { |
| 5766 | int index = Hash(map, name); |
| 5767 | Key& key = keys_[index]; |
| 5768 | if ((key.map == map) && key.name->Equals(name)) { |
| 5769 | return field_offsets_[index]; |
| 5770 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5771 | return kNotFound; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | |
| 5775 | void KeyedLookupCache::Update(Map* map, String* name, int field_offset) { |
| 5776 | String* symbol; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5777 | if (HEAP->LookupSymbolIfExists(name, &symbol)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5778 | int index = Hash(map, symbol); |
| 5779 | Key& key = keys_[index]; |
| 5780 | key.map = map; |
| 5781 | key.name = symbol; |
| 5782 | field_offsets_[index] = field_offset; |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | |
| 5787 | void KeyedLookupCache::Clear() { |
| 5788 | for (int index = 0; index < kLength; index++) keys_[index].map = NULL; |
| 5789 | } |
| 5790 | |
| 5791 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5792 | void DescriptorLookupCache::Clear() { |
| 5793 | for (int index = 0; index < kLength; index++) keys_[index].array = NULL; |
| 5794 | } |
| 5795 | |
| 5796 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5797 | #ifdef DEBUG |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5798 | void Heap::GarbageCollectionGreedyCheck() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5799 | ASSERT(FLAG_gc_greedy); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5800 | if (isolate_->bootstrapper()->IsActive()) return; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5801 | if (disallow_allocation_failure()) return; |
| 5802 | CollectGarbage(NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5803 | } |
| 5804 | #endif |
| 5805 | |
| 5806 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5807 | TranscendentalCache::SubCache::SubCache(Type t) |
| 5808 | : type_(t), |
| 5809 | isolate_(Isolate::Current()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5810 | uint32_t in0 = 0xffffffffu; // Bit-pattern for a NaN that isn't |
| 5811 | uint32_t in1 = 0xffffffffu; // generated by the FPU. |
| 5812 | for (int i = 0; i < kCacheSize; i++) { |
| 5813 | elements_[i].in[0] = in0; |
| 5814 | elements_[i].in[1] = in1; |
| 5815 | elements_[i].output = NULL; |
| 5816 | } |
| 5817 | } |
| 5818 | |
| 5819 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5820 | void TranscendentalCache::Clear() { |
| 5821 | for (int i = 0; i < kNumberOfCaches; i++) { |
| 5822 | if (caches_[i] != NULL) { |
| 5823 | delete caches_[i]; |
| 5824 | caches_[i] = NULL; |
| 5825 | } |
| 5826 | } |
| 5827 | } |
| 5828 | |
| 5829 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5830 | void ExternalStringTable::CleanUp() { |
| 5831 | int last = 0; |
| 5832 | for (int i = 0; i < new_space_strings_.length(); ++i) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5833 | if (new_space_strings_[i] == heap_->raw_unchecked_null_value()) continue; |
| 5834 | if (heap_->InNewSpace(new_space_strings_[i])) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5835 | new_space_strings_[last++] = new_space_strings_[i]; |
| 5836 | } else { |
| 5837 | old_space_strings_.Add(new_space_strings_[i]); |
| 5838 | } |
| 5839 | } |
| 5840 | new_space_strings_.Rewind(last); |
| 5841 | last = 0; |
| 5842 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5843 | if (old_space_strings_[i] == heap_->raw_unchecked_null_value()) continue; |
| 5844 | ASSERT(!heap_->InNewSpace(old_space_strings_[i])); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5845 | old_space_strings_[last++] = old_space_strings_[i]; |
| 5846 | } |
| 5847 | old_space_strings_.Rewind(last); |
| 5848 | Verify(); |
| 5849 | } |
| 5850 | |
| 5851 | |
| 5852 | void ExternalStringTable::TearDown() { |
| 5853 | new_space_strings_.Free(); |
| 5854 | old_space_strings_.Free(); |
| 5855 | } |
| 5856 | |
| 5857 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5858 | } } // namespace v8::internal |