Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 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" |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 36 | #include "deoptimizer.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | #include "global-handles.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 38 | #include "heap-profiler.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 39 | #include "incremental-marking.h" |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 40 | #include "liveobjectlist-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | #include "mark-compact.h" |
| 42 | #include "natives.h" |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 43 | #include "objects-visiting.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 44 | #include "objects-visiting-inl.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 45 | #include "runtime-profiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | #include "scopeinfo.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 47 | #include "snapshot.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 48 | #include "store-buffer.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | #include "v8threads.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 50 | #include "vm-state-inl.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 51 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | #include "regexp-macro-assembler.h" |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 53 | #include "arm/regexp-macro-assembler-arm.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 55 | #if V8_TARGET_ARCH_MIPS && !V8_INTERPRETED_REGEXP |
| 56 | #include "regexp-macro-assembler.h" |
| 57 | #include "mips/regexp-macro-assembler-mips.h" |
| 58 | #endif |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 59 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | namespace v8 { |
| 61 | namespace internal { |
| 62 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 63 | static LazyMutex gc_initializer_mutex = LAZY_MUTEX_INITIALIZER; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 66 | Heap::Heap() |
| 67 | : isolate_(NULL), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 68 | // semispace_size_ should be a power of 2 and old_generation_size_ should be |
| 69 | // a multiple of Page::kPageSize. |
Ben Murdoch | a28cdee | 2012-06-07 18:38:12 +0100 | [diff] [blame^] | 70 | |
| 71 | // ANDROID |
| 72 | //#if defined(ANDROID) |
| 73 | //#define LUMP_OF_MEMORY (128 * KB) |
| 74 | // code_range_size_(0), |
| 75 | //#elif defined(V8_TARGET_ARCH_X64) |
| 76 | // END ANDROID |
| 77 | #if defined(V8_TARGET_ARCH_X64) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 78 | #define LUMP_OF_MEMORY (2 * MB) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 79 | code_range_size_(512*MB), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | #else |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 81 | #define LUMP_OF_MEMORY MB |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 82 | code_range_size_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | #endif |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 84 | reserved_semispace_size_(8 * Max(LUMP_OF_MEMORY, Page::kPageSize)), |
| 85 | max_semispace_size_(8 * Max(LUMP_OF_MEMORY, Page::kPageSize)), |
| 86 | initial_semispace_size_(Page::kPageSize), |
| 87 | max_old_generation_size_(700ul * LUMP_OF_MEMORY), |
| 88 | max_executable_size_(256l * LUMP_OF_MEMORY), |
| 89 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | // Variables set based on semispace_size_ and old_generation_size_ in |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 91 | // ConfigureHeap (survived_since_last_expansion_, external_allocation_limit_) |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 92 | // Will be 4 * reserved_semispace_size_ to ensure that young |
| 93 | // generation can be aligned to its size. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 94 | survived_since_last_expansion_(0), |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 95 | sweep_generation_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 96 | always_allocate_scope_depth_(0), |
| 97 | linear_allocation_scope_depth_(0), |
| 98 | contexts_disposed_(0), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 99 | global_ic_age_(0), |
| 100 | scan_on_scavenge_pages_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 101 | new_space_(this), |
| 102 | old_pointer_space_(NULL), |
| 103 | old_data_space_(NULL), |
| 104 | code_space_(NULL), |
| 105 | map_space_(NULL), |
| 106 | cell_space_(NULL), |
| 107 | lo_space_(NULL), |
| 108 | gc_state_(NOT_IN_GC), |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 109 | gc_post_processing_depth_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 110 | ms_count_(0), |
| 111 | gc_count_(0), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 112 | remembered_unmapped_pages_index_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 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 |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 120 | new_space_high_promotion_mode_active_(false), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 121 | old_gen_promotion_limit_(kMinimumPromotionLimit), |
| 122 | old_gen_allocation_limit_(kMinimumAllocationLimit), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 123 | old_gen_limit_factor_(1), |
| 124 | size_of_old_gen_at_last_old_space_gc_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 125 | external_allocation_limit_(0), |
| 126 | amount_of_external_allocated_memory_(0), |
| 127 | amount_of_external_allocated_memory_at_last_global_gc_(0), |
| 128 | old_gen_exhausted_(false), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | store_buffer_rebuilder_(store_buffer()), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 130 | hidden_symbol_(NULL), |
| 131 | global_gc_prologue_callback_(NULL), |
| 132 | global_gc_epilogue_callback_(NULL), |
| 133 | gc_safe_size_of_old_object_(NULL), |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 134 | total_regexp_code_generated_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 135 | tracer_(NULL), |
| 136 | young_survivors_after_last_gc_(0), |
| 137 | high_survival_rate_period_length_(0), |
| 138 | survival_rate_(0), |
| 139 | previous_survival_rate_trend_(Heap::STABLE), |
| 140 | survival_rate_trend_(Heap::STABLE), |
| 141 | max_gc_pause_(0), |
| 142 | max_alive_after_gc_(0), |
| 143 | min_in_mutator_(kMaxInt), |
| 144 | alive_after_last_gc_(0), |
| 145 | last_gc_end_timestamp_(0.0), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 146 | store_buffer_(this), |
| 147 | marking_(this), |
| 148 | incremental_marking_(this), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 149 | number_idle_notifications_(0), |
| 150 | last_idle_notification_gc_count_(0), |
| 151 | last_idle_notification_gc_count_init_(false), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 152 | mark_sweeps_since_idle_round_started_(0), |
| 153 | ms_count_at_last_idle_notification_(0), |
| 154 | gc_count_at_last_idle_gc_(0), |
| 155 | scavenges_since_last_idle_round_(kIdleScavengeThreshold), |
| 156 | promotion_queue_(this), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 157 | configured_(false), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 158 | chunks_queued_for_free_(NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 159 | // Allow build-time customization of the max semispace size. Building |
| 160 | // V8 with snapshots and a non-default max semispace size is much |
| 161 | // easier if you can define it as part of the build environment. |
| 162 | #if defined(V8_MAX_SEMISPACE_SIZE) |
| 163 | max_semispace_size_ = reserved_semispace_size_ = V8_MAX_SEMISPACE_SIZE; |
| 164 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 165 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 166 | intptr_t max_virtual = OS::MaxVirtualMemory(); |
| 167 | |
| 168 | if (max_virtual > 0) { |
| 169 | if (code_range_size_ > 0) { |
| 170 | // Reserve no more than 1/8 of the memory for the code range. |
| 171 | code_range_size_ = Min(code_range_size_, max_virtual >> 3); |
| 172 | } |
| 173 | } |
| 174 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 175 | memset(roots_, 0, sizeof(roots_[0]) * kRootListLength); |
| 176 | global_contexts_list_ = NULL; |
| 177 | mark_compact_collector_.heap_ = this; |
| 178 | external_string_table_.heap_ = this; |
| 179 | } |
| 180 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 182 | intptr_t Heap::Capacity() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 183 | if (!HasBeenSetUp()) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | |
| 185 | return new_space_.Capacity() + |
| 186 | old_pointer_space_->Capacity() + |
| 187 | old_data_space_->Capacity() + |
| 188 | code_space_->Capacity() + |
| 189 | map_space_->Capacity() + |
| 190 | cell_space_->Capacity(); |
| 191 | } |
| 192 | |
| 193 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 194 | intptr_t Heap::CommittedMemory() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 195 | if (!HasBeenSetUp()) return 0; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 196 | |
| 197 | return new_space_.CommittedMemory() + |
| 198 | old_pointer_space_->CommittedMemory() + |
| 199 | old_data_space_->CommittedMemory() + |
| 200 | code_space_->CommittedMemory() + |
| 201 | map_space_->CommittedMemory() + |
| 202 | cell_space_->CommittedMemory() + |
| 203 | lo_space_->Size(); |
| 204 | } |
| 205 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 206 | intptr_t Heap::CommittedMemoryExecutable() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 207 | if (!HasBeenSetUp()) return 0; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 208 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 209 | return isolate()->memory_allocator()->SizeExecutable(); |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 212 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 213 | intptr_t Heap::Available() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 214 | if (!HasBeenSetUp()) return 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | |
| 216 | return new_space_.Available() + |
| 217 | old_pointer_space_->Available() + |
| 218 | old_data_space_->Available() + |
| 219 | code_space_->Available() + |
| 220 | map_space_->Available() + |
| 221 | cell_space_->Available(); |
| 222 | } |
| 223 | |
| 224 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 225 | bool Heap::HasBeenSetUp() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | return old_pointer_space_ != NULL && |
| 227 | old_data_space_ != NULL && |
| 228 | code_space_ != NULL && |
| 229 | map_space_ != NULL && |
| 230 | cell_space_ != NULL && |
| 231 | lo_space_ != NULL; |
| 232 | } |
| 233 | |
| 234 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 235 | int Heap::GcSafeSizeOfOldObject(HeapObject* object) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 236 | if (IntrusiveMarking::IsMarked(object)) { |
| 237 | return IntrusiveMarking::SizeOfMarkedObject(object); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 238 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 239 | return object->SizeFromMap(object->map()); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 243 | GarbageCollector Heap::SelectGarbageCollector(AllocationSpace space, |
| 244 | const char** reason) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | // Is global GC requested? |
| 246 | if (space != NEW_SPACE || FLAG_gc_global) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 247 | isolate_->counters()->gc_compactor_caused_by_request()->Increment(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 248 | *reason = "GC in old space requested"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 249 | return MARK_COMPACTOR; |
| 250 | } |
| 251 | |
| 252 | // Is enough data promoted to justify a global GC? |
| 253 | if (OldGenerationPromotionLimitReached()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 254 | isolate_->counters()->gc_compactor_caused_by_promoted_data()->Increment(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 255 | *reason = "promotion limit reached"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 256 | return MARK_COMPACTOR; |
| 257 | } |
| 258 | |
| 259 | // Have allocation in OLD and LO failed? |
| 260 | if (old_gen_exhausted_) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 261 | isolate_->counters()-> |
| 262 | gc_compactor_caused_by_oldspace_exhaustion()->Increment(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 263 | *reason = "old generations exhausted"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | return MARK_COMPACTOR; |
| 265 | } |
| 266 | |
| 267 | // Is there enough space left in OLD to guarantee that a scavenge can |
| 268 | // succeed? |
| 269 | // |
| 270 | // Note that MemoryAllocator->MaxAvailable() undercounts the memory available |
| 271 | // for object promotion. It counts only the bytes that the memory |
| 272 | // allocator has not yet allocated from the OS and assigned to any space, |
| 273 | // and does not count available bytes already in the old space or code |
| 274 | // space. Undercounting is safe---we may get an unrequested full GC when |
| 275 | // a scavenge would have succeeded. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 276 | if (isolate_->memory_allocator()->MaxAvailable() <= new_space_.Size()) { |
| 277 | isolate_->counters()-> |
| 278 | gc_compactor_caused_by_oldspace_exhaustion()->Increment(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 279 | *reason = "scavenge might not succeed"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | return MARK_COMPACTOR; |
| 281 | } |
| 282 | |
| 283 | // Default |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 284 | *reason = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 285 | return SCAVENGER; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 290 | // --log-gc to avoid the complicated preprocessor and flag testing. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 291 | void Heap::ReportStatisticsBeforeGC() { |
| 292 | // Heap::ReportHeapStatistics will also log NewSpace statistics when |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 293 | // compiled --log-gc is set. The following logic is used to avoid |
| 294 | // double logging. |
| 295 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 296 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.CollectStatistics(); |
| 297 | if (FLAG_heap_stats) { |
| 298 | ReportHeapStatistics("Before GC"); |
| 299 | } else if (FLAG_log_gc) { |
| 300 | new_space_.ReportStatistics(); |
| 301 | } |
| 302 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.ClearHistograms(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 303 | #else |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | if (FLAG_log_gc) { |
| 305 | new_space_.CollectStatistics(); |
| 306 | new_space_.ReportStatistics(); |
| 307 | new_space_.ClearHistograms(); |
| 308 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 309 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 361 | // --log-gc to avoid the complicated preprocessor and flag testing. |
| 362 | void Heap::ReportStatisticsAfterGC() { |
| 363 | // Similar to the before GC, we use some complicated logic to ensure that |
| 364 | // NewSpace statistics are logged exactly once when --log-gc is turned on. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 365 | #if defined(DEBUG) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | if (FLAG_heap_stats) { |
| 367 | new_space_.CollectStatistics(); |
| 368 | ReportHeapStatistics("After GC"); |
| 369 | } else if (FLAG_log_gc) { |
| 370 | new_space_.ReportStatistics(); |
| 371 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 372 | #else |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | if (FLAG_log_gc) new_space_.ReportStatistics(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 374 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 375 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 376 | |
| 377 | |
| 378 | void Heap::GarbageCollectionPrologue() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 379 | isolate_->transcendental_cache()->Clear(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 380 | ClearJSFunctionResultCaches(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 381 | gc_count_++; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 382 | unflattened_strings_length_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 383 | #ifdef DEBUG |
| 384 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 385 | allow_allocation(false); |
| 386 | |
| 387 | if (FLAG_verify_heap) { |
| 388 | Verify(); |
| 389 | } |
| 390 | |
| 391 | if (FLAG_gc_verbose) Print(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 392 | #endif // DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 393 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 394 | #if defined(DEBUG) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 395 | ReportStatisticsBeforeGC(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 396 | #endif // DEBUG |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 397 | |
| 398 | LiveObjectList::GCPrologue(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 399 | store_buffer()->GCPrologue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 402 | intptr_t Heap::SizeOfObjects() { |
| 403 | intptr_t total = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | AllSpaces spaces; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 405 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 406 | total += space->SizeOfObjects(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | } |
| 408 | return total; |
| 409 | } |
| 410 | |
| 411 | void Heap::GarbageCollectionEpilogue() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 412 | store_buffer()->GCEpilogue(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 413 | LiveObjectList::GCEpilogue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 414 | #ifdef DEBUG |
| 415 | allow_allocation(true); |
| 416 | ZapFromSpace(); |
| 417 | |
| 418 | if (FLAG_verify_heap) { |
| 419 | Verify(); |
| 420 | } |
| 421 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 422 | if (FLAG_print_global_handles) isolate_->global_handles()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 423 | if (FLAG_print_handles) PrintHandles(); |
| 424 | if (FLAG_gc_verbose) Print(); |
| 425 | if (FLAG_code_stats) ReportCodeStatistics("After GC"); |
| 426 | #endif |
| 427 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 428 | isolate_->counters()->alive_after_last_gc()->Set( |
| 429 | static_cast<int>(SizeOfObjects())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 431 | isolate_->counters()->symbol_table_capacity()->Set( |
| 432 | symbol_table()->Capacity()); |
| 433 | isolate_->counters()->number_of_symbols()->Set( |
| 434 | symbol_table()->NumberOfElements()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 435 | #if defined(DEBUG) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | ReportStatisticsAfterGC(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 437 | #endif // DEBUG |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 438 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 439 | isolate_->debug()->AfterGarbageCollection(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 440 | #endif // ENABLE_DEBUGGER_SUPPORT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 444 | void Heap::CollectAllGarbage(int flags, const char* gc_reason) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 445 | // Since we are ignoring the return value, the exact choice of space does |
| 446 | // not matter, so long as we do not specify NEW_SPACE, which would not |
| 447 | // cause a full GC. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 448 | mark_compact_collector_.SetFlags(flags); |
| 449 | CollectGarbage(OLD_POINTER_SPACE, gc_reason); |
| 450 | mark_compact_collector_.SetFlags(kNoGCFlags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 454 | void Heap::CollectAllAvailableGarbage(const char* gc_reason) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 455 | // Since we are ignoring the return value, the exact choice of space does |
| 456 | // not matter, so long as we do not specify NEW_SPACE, which would not |
| 457 | // cause a full GC. |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 458 | // Major GC would invoke weak handle callbacks on weakly reachable |
| 459 | // handles, but won't collect weakly reachable objects until next |
| 460 | // major GC. Therefore if we collect aggressively and weak handle callback |
| 461 | // has been invoked, we rerun major GC to release objects which become |
| 462 | // garbage. |
| 463 | // Note: as weak callbacks can execute arbitrary code, we cannot |
| 464 | // hope that eventually there will be no weak callbacks invocations. |
| 465 | // Therefore stop recollecting after several attempts. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 466 | mark_compact_collector()->SetFlags(kMakeHeapIterableMask | |
| 467 | kReduceMemoryFootprintMask); |
| 468 | isolate_->compilation_cache()->Clear(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 469 | const int kMaxNumberOfAttempts = 7; |
| 470 | for (int attempt = 0; attempt < kMaxNumberOfAttempts; attempt++) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 471 | if (!CollectGarbage(OLD_POINTER_SPACE, MARK_COMPACTOR, gc_reason, NULL)) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 475 | mark_compact_collector()->SetFlags(kNoGCFlags); |
| 476 | new_space_.Shrink(); |
| 477 | UncommitFromSpace(); |
| 478 | Shrink(); |
| 479 | incremental_marking()->UncommitMarkingDeque(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 483 | bool Heap::CollectGarbage(AllocationSpace space, |
| 484 | GarbageCollector collector, |
| 485 | const char* gc_reason, |
| 486 | const char* collector_reason) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 487 | // The VM is in the GC state until exiting this function. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 488 | VMState state(isolate_, GC); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 489 | |
| 490 | #ifdef DEBUG |
| 491 | // Reset the allocation timeout to the GC interval, but make sure to |
| 492 | // allow at least a few allocations after a collection. The reason |
| 493 | // for this is that we have a lot of allocation sequences and we |
| 494 | // assume that a garbage collection will allow the subsequent |
| 495 | // allocation attempts to go through. |
| 496 | allocation_timeout_ = Max(6, FLAG_gc_interval); |
| 497 | #endif |
| 498 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 499 | if (collector == SCAVENGER && !incremental_marking()->IsStopped()) { |
| 500 | if (FLAG_trace_incremental_marking) { |
| 501 | PrintF("[IncrementalMarking] Scavenge during marking.\n"); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (collector == MARK_COMPACTOR && |
| 506 | !mark_compact_collector()->abort_incremental_marking_ && |
| 507 | !incremental_marking()->IsStopped() && |
| 508 | !incremental_marking()->should_hurry() && |
| 509 | FLAG_incremental_marking_steps) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 510 | // Make progress in incremental marking. |
| 511 | const intptr_t kStepSizeWhenDelayedByScavenge = 1 * MB; |
| 512 | incremental_marking()->Step(kStepSizeWhenDelayedByScavenge, |
| 513 | IncrementalMarking::NO_GC_VIA_STACK_GUARD); |
| 514 | if (!incremental_marking()->IsComplete()) { |
| 515 | if (FLAG_trace_incremental_marking) { |
| 516 | PrintF("[IncrementalMarking] Delaying MarkSweep.\n"); |
| 517 | } |
| 518 | collector = SCAVENGER; |
| 519 | collector_reason = "incremental marking delaying mark-sweep"; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 520 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 521 | } |
| 522 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 523 | bool next_gc_likely_to_collect_more = false; |
| 524 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 525 | { GCTracer tracer(this, gc_reason, collector_reason); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 526 | GarbageCollectionPrologue(); |
| 527 | // The GC count was incremented in the prologue. Tell the tracer about |
| 528 | // it. |
| 529 | tracer.set_gc_count(gc_count_); |
| 530 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 531 | // Tell the tracer which collector we've selected. |
| 532 | tracer.set_collector(collector); |
| 533 | |
| 534 | HistogramTimer* rate = (collector == SCAVENGER) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 535 | ? isolate_->counters()->gc_scavenger() |
| 536 | : isolate_->counters()->gc_compactor(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 537 | rate->Start(); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 538 | next_gc_likely_to_collect_more = |
| 539 | PerformGarbageCollection(collector, &tracer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 540 | rate->Stop(); |
| 541 | |
| 542 | GarbageCollectionEpilogue(); |
| 543 | } |
| 544 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 545 | ASSERT(collector == SCAVENGER || incremental_marking()->IsStopped()); |
| 546 | if (incremental_marking()->IsStopped()) { |
| 547 | if (incremental_marking()->WorthActivating() && NextGCIsLikelyToBeFull()) { |
| 548 | incremental_marking()->Start(); |
| 549 | } |
| 550 | } |
| 551 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 552 | return next_gc_likely_to_collect_more; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
| 556 | void Heap::PerformScavenge() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 557 | GCTracer tracer(this, NULL, NULL); |
| 558 | if (incremental_marking()->IsStopped()) { |
| 559 | PerformGarbageCollection(SCAVENGER, &tracer); |
| 560 | } else { |
| 561 | PerformGarbageCollection(MARK_COMPACTOR, &tracer); |
| 562 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
| 566 | #ifdef DEBUG |
| 567 | // Helper class for verifying the symbol table. |
| 568 | class SymbolTableVerifier : public ObjectVisitor { |
| 569 | public: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 570 | void VisitPointers(Object** start, Object** end) { |
| 571 | // Visit all HeapObject pointers in [start, end). |
| 572 | for (Object** p = start; p < end; p++) { |
| 573 | if ((*p)->IsHeapObject()) { |
| 574 | // Check that the symbol is actually a symbol. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 575 | ASSERT((*p)->IsTheHole() || (*p)->IsUndefined() || (*p)->IsSymbol()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | } |
| 579 | }; |
| 580 | #endif // DEBUG |
| 581 | |
| 582 | |
| 583 | static void VerifySymbolTable() { |
| 584 | #ifdef DEBUG |
| 585 | SymbolTableVerifier verifier; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 586 | HEAP->symbol_table()->IterateElements(&verifier); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 587 | #endif // DEBUG |
| 588 | } |
| 589 | |
| 590 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 591 | static bool AbortIncrementalMarkingAndCollectGarbage( |
| 592 | Heap* heap, |
| 593 | AllocationSpace space, |
| 594 | const char* gc_reason = NULL) { |
| 595 | heap->mark_compact_collector()->SetFlags(Heap::kAbortIncrementalMarkingMask); |
| 596 | bool result = heap->CollectGarbage(space, gc_reason); |
| 597 | heap->mark_compact_collector()->SetFlags(Heap::kNoGCFlags); |
| 598 | return result; |
| 599 | } |
| 600 | |
| 601 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 602 | void Heap::ReserveSpace( |
| 603 | int new_space_size, |
| 604 | int pointer_space_size, |
| 605 | int data_space_size, |
| 606 | int code_space_size, |
| 607 | int map_space_size, |
| 608 | int cell_space_size, |
| 609 | int large_object_size) { |
| 610 | NewSpace* new_space = Heap::new_space(); |
| 611 | PagedSpace* old_pointer_space = Heap::old_pointer_space(); |
| 612 | PagedSpace* old_data_space = Heap::old_data_space(); |
| 613 | PagedSpace* code_space = Heap::code_space(); |
| 614 | PagedSpace* map_space = Heap::map_space(); |
| 615 | PagedSpace* cell_space = Heap::cell_space(); |
| 616 | LargeObjectSpace* lo_space = Heap::lo_space(); |
| 617 | bool gc_performed = true; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 618 | int counter = 0; |
| 619 | static const int kThreshold = 20; |
| 620 | while (gc_performed && counter++ < kThreshold) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 621 | gc_performed = false; |
| 622 | if (!new_space->ReserveSpace(new_space_size)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 623 | Heap::CollectGarbage(NEW_SPACE, |
| 624 | "failed to reserve space in the new space"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 625 | gc_performed = true; |
| 626 | } |
| 627 | if (!old_pointer_space->ReserveSpace(pointer_space_size)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 628 | AbortIncrementalMarkingAndCollectGarbage(this, OLD_POINTER_SPACE, |
| 629 | "failed to reserve space in the old pointer space"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 630 | gc_performed = true; |
| 631 | } |
| 632 | if (!(old_data_space->ReserveSpace(data_space_size))) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 633 | AbortIncrementalMarkingAndCollectGarbage(this, OLD_DATA_SPACE, |
| 634 | "failed to reserve space in the old data space"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 635 | gc_performed = true; |
| 636 | } |
| 637 | if (!(code_space->ReserveSpace(code_space_size))) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 638 | AbortIncrementalMarkingAndCollectGarbage(this, CODE_SPACE, |
| 639 | "failed to reserve space in the code space"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 640 | gc_performed = true; |
| 641 | } |
| 642 | if (!(map_space->ReserveSpace(map_space_size))) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 643 | AbortIncrementalMarkingAndCollectGarbage(this, MAP_SPACE, |
| 644 | "failed to reserve space in the map space"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 645 | gc_performed = true; |
| 646 | } |
| 647 | if (!(cell_space->ReserveSpace(cell_space_size))) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 648 | AbortIncrementalMarkingAndCollectGarbage(this, CELL_SPACE, |
| 649 | "failed to reserve space in the cell space"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 650 | gc_performed = true; |
| 651 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 652 | // We add a slack-factor of 2 in order to have space for a series of |
| 653 | // large-object allocations that are only just larger than the page size. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 654 | large_object_size *= 2; |
| 655 | // The ReserveSpace method on the large object space checks how much |
| 656 | // we can expand the old generation. This includes expansion caused by |
| 657 | // allocation in the other spaces. |
| 658 | large_object_size += cell_space_size + map_space_size + code_space_size + |
| 659 | data_space_size + pointer_space_size; |
| 660 | if (!(lo_space->ReserveSpace(large_object_size))) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 661 | AbortIncrementalMarkingAndCollectGarbage(this, LO_SPACE, |
| 662 | "failed to reserve space in the large object space"); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 663 | gc_performed = true; |
| 664 | } |
| 665 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 666 | |
| 667 | if (gc_performed) { |
| 668 | // Failed to reserve the space after several attempts. |
| 669 | V8::FatalProcessOutOfMemory("Heap::ReserveSpace"); |
| 670 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 674 | void Heap::EnsureFromSpaceIsCommitted() { |
| 675 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 676 | |
| 677 | // Committing memory to from space failed. |
| 678 | // Try shrinking and try again. |
| 679 | Shrink(); |
| 680 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 681 | |
| 682 | // Committing memory to from space failed again. |
| 683 | // Memory is exhausted and we will die. |
| 684 | V8::FatalProcessOutOfMemory("Committing semi space failed."); |
| 685 | } |
| 686 | |
| 687 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 688 | void Heap::ClearJSFunctionResultCaches() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 689 | if (isolate_->bootstrapper()->IsActive()) return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 690 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 691 | Object* context = global_contexts_list_; |
| 692 | while (!context->IsUndefined()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 693 | // Get the caches for this context. GC can happen when the context |
| 694 | // is not fully initialized, so the caches can be undefined. |
| 695 | Object* caches_or_undefined = |
| 696 | Context::cast(context)->get(Context::JSFUNCTION_RESULT_CACHES_INDEX); |
| 697 | if (!caches_or_undefined->IsUndefined()) { |
| 698 | FixedArray* caches = FixedArray::cast(caches_or_undefined); |
| 699 | // Clear the caches: |
| 700 | int length = caches->length(); |
| 701 | for (int i = 0; i < length; i++) { |
| 702 | JSFunctionResultCache::cast(caches->get(i))->Clear(); |
| 703 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 704 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 705 | // Get the next context: |
| 706 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 707 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 711 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 712 | void Heap::ClearNormalizedMapCaches() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 713 | if (isolate_->bootstrapper()->IsActive() && |
| 714 | !incremental_marking()->IsMarking()) { |
| 715 | return; |
| 716 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 717 | |
| 718 | Object* context = global_contexts_list_; |
| 719 | while (!context->IsUndefined()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 720 | // GC can happen when the context is not fully initialized, |
| 721 | // so the cache can be undefined. |
| 722 | Object* cache = |
| 723 | Context::cast(context)->get(Context::NORMALIZED_MAP_CACHE_INDEX); |
| 724 | if (!cache->IsUndefined()) { |
| 725 | NormalizedMapCache::cast(cache)->Clear(); |
| 726 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 727 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
| 728 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 732 | void Heap::UpdateSurvivalRateTrend(int start_new_space_size) { |
| 733 | double survival_rate = |
| 734 | (static_cast<double>(young_survivors_after_last_gc_) * 100) / |
| 735 | start_new_space_size; |
| 736 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 737 | if (survival_rate > kYoungSurvivalRateHighThreshold) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 738 | high_survival_rate_period_length_++; |
| 739 | } else { |
| 740 | high_survival_rate_period_length_ = 0; |
| 741 | } |
| 742 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 743 | if (survival_rate < kYoungSurvivalRateLowThreshold) { |
| 744 | low_survival_rate_period_length_++; |
| 745 | } else { |
| 746 | low_survival_rate_period_length_ = 0; |
| 747 | } |
| 748 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 749 | double survival_rate_diff = survival_rate_ - survival_rate; |
| 750 | |
| 751 | if (survival_rate_diff > kYoungSurvivalRateAllowedDeviation) { |
| 752 | set_survival_rate_trend(DECREASING); |
| 753 | } else if (survival_rate_diff < -kYoungSurvivalRateAllowedDeviation) { |
| 754 | set_survival_rate_trend(INCREASING); |
| 755 | } else { |
| 756 | set_survival_rate_trend(STABLE); |
| 757 | } |
| 758 | |
| 759 | survival_rate_ = survival_rate; |
| 760 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 761 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 762 | bool Heap::PerformGarbageCollection(GarbageCollector collector, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 763 | GCTracer* tracer) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 764 | bool next_gc_likely_to_collect_more = false; |
| 765 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 766 | if (collector != SCAVENGER) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 767 | PROFILE(isolate_, CodeMovingGCEvent()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 770 | if (FLAG_verify_heap) { |
| 771 | VerifySymbolTable(); |
| 772 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | if (collector == MARK_COMPACTOR && global_gc_prologue_callback_) { |
| 774 | ASSERT(!allocation_allowed_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 775 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 776 | global_gc_prologue_callback_(); |
| 777 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 778 | |
| 779 | GCType gc_type = |
| 780 | collector == MARK_COMPACTOR ? kGCTypeMarkSweepCompact : kGCTypeScavenge; |
| 781 | |
| 782 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 783 | if (gc_type & gc_prologue_callbacks_[i].gc_type) { |
| 784 | gc_prologue_callbacks_[i].callback(gc_type, kNoGCCallbackFlags); |
| 785 | } |
| 786 | } |
| 787 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 788 | EnsureFromSpaceIsCommitted(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 789 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 790 | int start_new_space_size = Heap::new_space()->SizeAsInt(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 791 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 792 | if (IsHighSurvivalRate()) { |
| 793 | // We speed up the incremental marker if it is running so that it |
| 794 | // does not fall behind the rate of promotion, which would cause a |
| 795 | // constantly growing old space. |
| 796 | incremental_marking()->NotifyOfHighPromotionRate(); |
| 797 | } |
| 798 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 799 | if (collector == MARK_COMPACTOR) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 800 | // Perform mark-sweep with optional compaction. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 801 | MarkCompact(tracer); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 802 | sweep_generation_++; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 803 | bool high_survival_rate_during_scavenges = IsHighSurvivalRate() && |
| 804 | IsStableOrIncreasingSurvivalTrend(); |
| 805 | |
| 806 | UpdateSurvivalRateTrend(start_new_space_size); |
| 807 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 808 | size_of_old_gen_at_last_old_space_gc_ = PromotedSpaceSize(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 809 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 810 | if (high_survival_rate_during_scavenges && |
| 811 | IsStableOrIncreasingSurvivalTrend()) { |
| 812 | // Stable high survival rates of young objects both during partial and |
| 813 | // full collection indicate that mutator is either building or modifying |
| 814 | // a structure with a long lifetime. |
| 815 | // In this case we aggressively raise old generation memory limits to |
| 816 | // postpone subsequent mark-sweep collection and thus trade memory |
| 817 | // space for the mutation speed. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 818 | old_gen_limit_factor_ = 2; |
| 819 | } else { |
| 820 | old_gen_limit_factor_ = 1; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 823 | old_gen_promotion_limit_ = |
| 824 | OldGenPromotionLimit(size_of_old_gen_at_last_old_space_gc_); |
| 825 | old_gen_allocation_limit_ = |
| 826 | OldGenAllocationLimit(size_of_old_gen_at_last_old_space_gc_); |
| 827 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 828 | old_gen_exhausted_ = false; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 829 | } else { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 830 | tracer_ = tracer; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 831 | Scavenge(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 832 | tracer_ = NULL; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 833 | |
| 834 | UpdateSurvivalRateTrend(start_new_space_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 835 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 836 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 837 | if (!new_space_high_promotion_mode_active_ && |
| 838 | new_space_.Capacity() == new_space_.MaximumCapacity() && |
| 839 | IsStableOrIncreasingSurvivalTrend() && |
| 840 | IsHighSurvivalRate()) { |
| 841 | // Stable high survival rates even though young generation is at |
| 842 | // maximum capacity indicates that most objects will be promoted. |
| 843 | // To decrease scavenger pauses and final mark-sweep pauses, we |
| 844 | // have to limit maximal capacity of the young generation. |
| 845 | new_space_high_promotion_mode_active_ = true; |
| 846 | if (FLAG_trace_gc) { |
| 847 | PrintF("Limited new space size due to high promotion rate: %d MB\n", |
| 848 | new_space_.InitialCapacity() / MB); |
| 849 | } |
| 850 | } else if (new_space_high_promotion_mode_active_ && |
| 851 | IsStableOrDecreasingSurvivalTrend() && |
| 852 | IsLowSurvivalRate()) { |
| 853 | // Decreasing low survival rates might indicate that the above high |
| 854 | // promotion mode is over and we should allow the young generation |
| 855 | // to grow again. |
| 856 | new_space_high_promotion_mode_active_ = false; |
| 857 | if (FLAG_trace_gc) { |
| 858 | PrintF("Unlimited new space size due to low promotion rate: %d MB\n", |
| 859 | new_space_.MaximumCapacity() / MB); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | if (new_space_high_promotion_mode_active_ && |
| 864 | new_space_.Capacity() > new_space_.InitialCapacity()) { |
| 865 | new_space_.Shrink(); |
| 866 | } |
| 867 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 868 | isolate_->counters()->objs_since_last_young()->Set(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 869 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 870 | gc_post_processing_depth_++; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 871 | { DisableAssertNoAllocation allow_allocation; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 872 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 873 | next_gc_likely_to_collect_more = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 874 | isolate_->global_handles()->PostGarbageCollectionProcessing(collector); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 875 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 876 | gc_post_processing_depth_--; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 877 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 878 | // Update relocatables. |
| 879 | Relocatable::PostGarbageCollectionProcessing(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 880 | |
| 881 | if (collector == MARK_COMPACTOR) { |
| 882 | // Register the amount of external allocated memory. |
| 883 | amount_of_external_allocated_memory_at_last_global_gc_ = |
| 884 | amount_of_external_allocated_memory_; |
| 885 | } |
| 886 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 887 | GCCallbackFlags callback_flags = kNoGCCallbackFlags; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 888 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 889 | if (gc_type & gc_epilogue_callbacks_[i].gc_type) { |
| 890 | gc_epilogue_callbacks_[i].callback(gc_type, callback_flags); |
| 891 | } |
| 892 | } |
| 893 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 894 | if (collector == MARK_COMPACTOR && global_gc_epilogue_callback_) { |
| 895 | ASSERT(!allocation_allowed_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 896 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 897 | global_gc_epilogue_callback_(); |
| 898 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 899 | if (FLAG_verify_heap) { |
| 900 | VerifySymbolTable(); |
| 901 | } |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 902 | |
| 903 | return next_gc_likely_to_collect_more; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 907 | void Heap::MarkCompact(GCTracer* tracer) { |
| 908 | gc_state_ = MARK_COMPACT; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 909 | LOG(isolate_, ResourceEvent("markcompact", "begin")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 910 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 911 | mark_compact_collector_.Prepare(tracer); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 912 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 913 | ms_count_++; |
| 914 | tracer->set_full_gc_count(ms_count_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 915 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 916 | MarkCompactPrologue(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 917 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 918 | mark_compact_collector_.CollectGarbage(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 919 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 920 | LOG(isolate_, ResourceEvent("markcompact", "end")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 921 | |
| 922 | gc_state_ = NOT_IN_GC; |
| 923 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 924 | isolate_->counters()->objs_since_last_full()->Set(0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 925 | |
| 926 | contexts_disposed_ = 0; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 927 | |
| 928 | isolate_->set_context_exit_happened(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 932 | void Heap::MarkCompactPrologue() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 933 | // At any old GC clear the keyed lookup cache to enable collection of unused |
| 934 | // maps. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 935 | isolate_->keyed_lookup_cache()->Clear(); |
| 936 | isolate_->context_slot_cache()->Clear(); |
| 937 | isolate_->descriptor_lookup_cache()->Clear(); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 938 | StringSplitCache::Clear(string_split_cache()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 939 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 940 | isolate_->compilation_cache()->MarkCompactPrologue(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 941 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 942 | CompletelyClearInstanceofCache(); |
| 943 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 944 | FlushNumberStringCache(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 945 | if (FLAG_cleanup_code_caches_at_gc) { |
| 946 | polymorphic_code_cache()->set_cache(undefined_value()); |
| 947 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 948 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 949 | ClearNormalizedMapCaches(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
| 953 | Object* Heap::FindCodeObject(Address a) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 954 | return isolate()->inner_pointer_to_code_cache()-> |
| 955 | GcSafeFindCodeForInnerPointer(a); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | |
| 959 | // Helper class for copying HeapObjects |
| 960 | class ScavengeVisitor: public ObjectVisitor { |
| 961 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 962 | explicit ScavengeVisitor(Heap* heap) : heap_(heap) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 963 | |
| 964 | void VisitPointer(Object** p) { ScavengePointer(p); } |
| 965 | |
| 966 | void VisitPointers(Object** start, Object** end) { |
| 967 | // Copy all HeapObject pointers in [start, end) |
| 968 | for (Object** p = start; p < end; p++) ScavengePointer(p); |
| 969 | } |
| 970 | |
| 971 | private: |
| 972 | void ScavengePointer(Object** p) { |
| 973 | Object* object = *p; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 974 | if (!heap_->InNewSpace(object)) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 975 | Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p), |
| 976 | reinterpret_cast<HeapObject*>(object)); |
| 977 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 978 | |
| 979 | Heap* heap_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 980 | }; |
| 981 | |
| 982 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 983 | #ifdef DEBUG |
| 984 | // Visitor class to verify pointers in code or data space do not point into |
| 985 | // new space. |
| 986 | class VerifyNonPointerSpacePointersVisitor: public ObjectVisitor { |
| 987 | public: |
| 988 | void VisitPointers(Object** start, Object**end) { |
| 989 | for (Object** current = start; current < end; current++) { |
| 990 | if ((*current)->IsHeapObject()) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 991 | ASSERT(!HEAP->InNewSpace(HeapObject::cast(*current))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | } |
| 995 | }; |
| 996 | |
| 997 | |
| 998 | static void VerifyNonPointerSpacePointers() { |
| 999 | // Verify that there are no pointers to new space in spaces where we |
| 1000 | // do not expect them. |
| 1001 | VerifyNonPointerSpacePointersVisitor v; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1002 | HeapObjectIterator code_it(HEAP->code_space()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1003 | for (HeapObject* object = code_it.Next(); |
| 1004 | object != NULL; object = code_it.Next()) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1005 | object->Iterate(&v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1006 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1007 | // The old data space was normally swept conservatively so that the iterator |
| 1008 | // doesn't work, so we normally skip the next bit. |
| 1009 | if (!HEAP->old_data_space()->was_swept_conservatively()) { |
| 1010 | HeapObjectIterator data_it(HEAP->old_data_space()); |
| 1011 | for (HeapObject* object = data_it.Next(); |
| 1012 | object != NULL; object = data_it.Next()) |
| 1013 | object->Iterate(&v); |
| 1014 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1015 | } |
| 1016 | #endif |
| 1017 | |
| 1018 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1019 | void Heap::CheckNewSpaceExpansionCriteria() { |
| 1020 | if (new_space_.Capacity() < new_space_.MaximumCapacity() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1021 | survived_since_last_expansion_ > new_space_.Capacity() && |
| 1022 | !new_space_high_promotion_mode_active_) { |
| 1023 | // Grow the size of new space if there is room to grow, enough data |
| 1024 | // has survived scavenge since the last expansion and we are not in |
| 1025 | // high promotion mode. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1026 | new_space_.Grow(); |
| 1027 | survived_since_last_expansion_ = 0; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1032 | static bool IsUnscavengedHeapObject(Heap* heap, Object** p) { |
| 1033 | return heap->InNewSpace(*p) && |
| 1034 | !HeapObject::cast(*p)->map_word().IsForwardingAddress(); |
| 1035 | } |
| 1036 | |
| 1037 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1038 | void Heap::ScavengeStoreBufferCallback( |
| 1039 | Heap* heap, |
| 1040 | MemoryChunk* page, |
| 1041 | StoreBufferEvent event) { |
| 1042 | heap->store_buffer_rebuilder_.Callback(page, event); |
| 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | void StoreBufferRebuilder::Callback(MemoryChunk* page, StoreBufferEvent event) { |
| 1047 | if (event == kStoreBufferStartScanningPagesEvent) { |
| 1048 | start_of_current_page_ = NULL; |
| 1049 | current_page_ = NULL; |
| 1050 | } else if (event == kStoreBufferScanningPageEvent) { |
| 1051 | if (current_page_ != NULL) { |
| 1052 | // If this page already overflowed the store buffer during this iteration. |
| 1053 | if (current_page_->scan_on_scavenge()) { |
| 1054 | // Then we should wipe out the entries that have been added for it. |
| 1055 | store_buffer_->SetTop(start_of_current_page_); |
| 1056 | } else if (store_buffer_->Top() - start_of_current_page_ >= |
| 1057 | (store_buffer_->Limit() - store_buffer_->Top()) >> 2) { |
| 1058 | // Did we find too many pointers in the previous page? The heuristic is |
| 1059 | // that no page can take more then 1/5 the remaining slots in the store |
| 1060 | // buffer. |
| 1061 | current_page_->set_scan_on_scavenge(true); |
| 1062 | store_buffer_->SetTop(start_of_current_page_); |
| 1063 | } else { |
| 1064 | // In this case the page we scanned took a reasonable number of slots in |
| 1065 | // the store buffer. It has now been rehabilitated and is no longer |
| 1066 | // marked scan_on_scavenge. |
| 1067 | ASSERT(!current_page_->scan_on_scavenge()); |
| 1068 | } |
| 1069 | } |
| 1070 | start_of_current_page_ = store_buffer_->Top(); |
| 1071 | current_page_ = page; |
| 1072 | } else if (event == kStoreBufferFullEvent) { |
| 1073 | // The current page overflowed the store buffer again. Wipe out its entries |
| 1074 | // in the store buffer and mark it scan-on-scavenge again. This may happen |
| 1075 | // several times while scanning. |
| 1076 | if (current_page_ == NULL) { |
| 1077 | // Store Buffer overflowed while scanning promoted objects. These are not |
| 1078 | // in any particular page, though they are likely to be clustered by the |
| 1079 | // allocation routines. |
| 1080 | store_buffer_->EnsureSpace(StoreBuffer::kStoreBufferSize); |
| 1081 | } else { |
| 1082 | // Store Buffer overflowed while scanning a particular old space page for |
| 1083 | // pointers to new space. |
| 1084 | ASSERT(current_page_ == page); |
| 1085 | ASSERT(page != NULL); |
| 1086 | current_page_->set_scan_on_scavenge(true); |
| 1087 | ASSERT(start_of_current_page_ != store_buffer_->Top()); |
| 1088 | store_buffer_->SetTop(start_of_current_page_); |
| 1089 | } |
| 1090 | } else { |
| 1091 | UNREACHABLE(); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | void PromotionQueue::Initialize() { |
| 1097 | // Assumes that a NewSpacePage exactly fits a number of promotion queue |
| 1098 | // entries (where each is a pair of intptr_t). This allows us to simplify |
| 1099 | // the test fpr when to switch pages. |
| 1100 | ASSERT((Page::kPageSize - MemoryChunk::kBodyOffset) % (2 * kPointerSize) |
| 1101 | == 0); |
| 1102 | limit_ = reinterpret_cast<intptr_t*>(heap_->new_space()->ToSpaceStart()); |
| 1103 | front_ = rear_ = |
| 1104 | reinterpret_cast<intptr_t*>(heap_->new_space()->ToSpaceEnd()); |
| 1105 | emergency_stack_ = NULL; |
| 1106 | guard_ = false; |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | void PromotionQueue::RelocateQueueHead() { |
| 1111 | ASSERT(emergency_stack_ == NULL); |
| 1112 | |
| 1113 | Page* p = Page::FromAllocationTop(reinterpret_cast<Address>(rear_)); |
| 1114 | intptr_t* head_start = rear_; |
| 1115 | intptr_t* head_end = |
| 1116 | Min(front_, reinterpret_cast<intptr_t*>(p->area_end())); |
| 1117 | |
| 1118 | int entries_count = |
| 1119 | static_cast<int>(head_end - head_start) / kEntrySizeInWords; |
| 1120 | |
| 1121 | emergency_stack_ = new List<Entry>(2 * entries_count); |
| 1122 | |
| 1123 | while (head_start != head_end) { |
| 1124 | int size = static_cast<int>(*(head_start++)); |
| 1125 | HeapObject* obj = reinterpret_cast<HeapObject*>(*(head_start++)); |
| 1126 | emergency_stack_->Add(Entry(obj, size)); |
| 1127 | } |
| 1128 | rear_ = head_end; |
| 1129 | } |
| 1130 | |
| 1131 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1132 | void Heap::Scavenge() { |
| 1133 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1134 | if (FLAG_verify_heap) VerifyNonPointerSpacePointers(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1135 | #endif |
| 1136 | |
| 1137 | gc_state_ = SCAVENGE; |
| 1138 | |
| 1139 | // Implements Cheney's copying algorithm |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1140 | LOG(isolate_, ResourceEvent("scavenge", "begin")); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1141 | |
| 1142 | // Clear descriptor cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1143 | isolate_->descriptor_lookup_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1144 | |
| 1145 | // Used for updating survived_since_last_expansion_ at function end. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1146 | intptr_t survived_watermark = PromotedSpaceSizeOfObjects(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1147 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1148 | CheckNewSpaceExpansionCriteria(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1149 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1150 | SelectScavengingVisitorsTable(); |
| 1151 | |
| 1152 | incremental_marking()->PrepareForScavenge(); |
| 1153 | |
| 1154 | AdvanceSweepers(static_cast<int>(new_space_.Size())); |
| 1155 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1156 | // Flip the semispaces. After flipping, to space is empty, from space has |
| 1157 | // live objects. |
| 1158 | new_space_.Flip(); |
| 1159 | new_space_.ResetAllocationInfo(); |
| 1160 | |
| 1161 | // We need to sweep newly copied objects which can be either in the |
| 1162 | // to space or promoted to the old generation. For to-space |
| 1163 | // objects, we treat the bottom of the to space as a queue. Newly |
| 1164 | // copied and unswept objects lie between a 'front' mark and the |
| 1165 | // allocation pointer. |
| 1166 | // |
| 1167 | // Promoted objects can go into various old-generation spaces, and |
| 1168 | // can be allocated internally in the spaces (from the free list). |
| 1169 | // We treat the top of the to space as a queue of addresses of |
| 1170 | // promoted objects. The addresses of newly promoted and unswept |
| 1171 | // objects lie between a 'front' mark and a 'rear' mark that is |
| 1172 | // updated as a side effect of promoting an object. |
| 1173 | // |
| 1174 | // There is guaranteed to be enough room at the top of the to space |
| 1175 | // for the addresses of promoted objects: every object promoted |
| 1176 | // frees up its size in bytes from the top of the new space, and |
| 1177 | // objects are at least one pointer in size. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1178 | Address new_space_front = new_space_.ToSpaceStart(); |
| 1179 | promotion_queue_.Initialize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1180 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1181 | #ifdef DEBUG |
| 1182 | store_buffer()->Clean(); |
| 1183 | #endif |
| 1184 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1185 | ScavengeVisitor scavenge_visitor(this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1186 | // Copy roots. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1187 | IterateRoots(&scavenge_visitor, VISIT_ALL_IN_SCAVENGE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1188 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1189 | // Copy objects reachable from the old generation. |
| 1190 | { |
| 1191 | StoreBufferRebuildScope scope(this, |
| 1192 | store_buffer(), |
| 1193 | &ScavengeStoreBufferCallback); |
| 1194 | store_buffer()->IteratePointersToNewSpace(&ScavengeObject); |
| 1195 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1196 | |
| 1197 | // Copy objects reachable from cells by scavenging cell values directly. |
| 1198 | HeapObjectIterator cell_iterator(cell_space_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1199 | for (HeapObject* cell = cell_iterator.Next(); |
| 1200 | cell != NULL; cell = cell_iterator.Next()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1201 | if (cell->IsJSGlobalPropertyCell()) { |
| 1202 | Address value_address = |
| 1203 | reinterpret_cast<Address>(cell) + |
| 1204 | (JSGlobalPropertyCell::kValueOffset - kHeapObjectTag); |
| 1205 | scavenge_visitor.VisitPointer(reinterpret_cast<Object**>(value_address)); |
| 1206 | } |
| 1207 | } |
| 1208 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1209 | // Scavenge object reachable from the global contexts list directly. |
| 1210 | scavenge_visitor.VisitPointer(BitCast<Object**>(&global_contexts_list_)); |
| 1211 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1212 | new_space_front = DoScavenge(&scavenge_visitor, new_space_front); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1213 | isolate_->global_handles()->IdentifyNewSpaceWeakIndependentHandles( |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1214 | &IsUnscavengedHeapObject); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1215 | isolate_->global_handles()->IterateNewSpaceWeakIndependentRoots( |
| 1216 | &scavenge_visitor); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1217 | new_space_front = DoScavenge(&scavenge_visitor, new_space_front); |
| 1218 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1219 | UpdateNewSpaceReferencesInExternalStringTable( |
| 1220 | &UpdateNewSpaceReferenceInExternalStringTableEntry); |
| 1221 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1222 | promotion_queue_.Destroy(); |
| 1223 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1224 | LiveObjectList::UpdateReferencesForScavengeGC(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1225 | if (!FLAG_watch_ic_patching) { |
| 1226 | isolate()->runtime_profiler()->UpdateSamplesAfterScavenge(); |
| 1227 | } |
| 1228 | incremental_marking()->UpdateMarkingDequeAfterScavenge(); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1229 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1230 | ASSERT(new_space_front == new_space_.top()); |
| 1231 | |
| 1232 | // Set age mark. |
| 1233 | new_space_.set_age_mark(new_space_.top()); |
| 1234 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1235 | new_space_.LowerInlineAllocationLimit( |
| 1236 | new_space_.inline_allocation_limit_step()); |
| 1237 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1238 | // Update how much has survived scavenge. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1239 | IncrementYoungSurvivorsCounter(static_cast<int>( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1240 | (PromotedSpaceSizeOfObjects() - survived_watermark) + new_space_.Size())); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1241 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1242 | LOG(isolate_, ResourceEvent("scavenge", "end")); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1243 | |
| 1244 | gc_state_ = NOT_IN_GC; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1245 | |
| 1246 | scavenges_since_last_idle_round_++; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1250 | String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Heap* heap, |
| 1251 | Object** p) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1252 | MapWord first_word = HeapObject::cast(*p)->map_word(); |
| 1253 | |
| 1254 | if (!first_word.IsForwardingAddress()) { |
| 1255 | // Unreachable external string can be finalized. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1256 | heap->FinalizeExternalString(String::cast(*p)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1257 | return NULL; |
| 1258 | } |
| 1259 | |
| 1260 | // String is still reachable. |
| 1261 | return String::cast(first_word.ToForwardingAddress()); |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | void Heap::UpdateNewSpaceReferencesInExternalStringTable( |
| 1266 | ExternalStringTableUpdaterCallback updater_func) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1267 | if (FLAG_verify_heap) { |
| 1268 | external_string_table_.Verify(); |
| 1269 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1270 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1271 | if (external_string_table_.new_space_strings_.is_empty()) return; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1272 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1273 | Object** start = &external_string_table_.new_space_strings_[0]; |
| 1274 | Object** end = start + external_string_table_.new_space_strings_.length(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1275 | Object** last = start; |
| 1276 | |
| 1277 | for (Object** p = start; p < end; ++p) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1278 | ASSERT(InFromSpace(*p)); |
| 1279 | String* target = updater_func(this, p); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1280 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1281 | if (target == NULL) continue; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1282 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1283 | ASSERT(target->IsExternalString()); |
| 1284 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1285 | if (InNewSpace(target)) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1286 | // String is still in new space. Update the table entry. |
| 1287 | *last = target; |
| 1288 | ++last; |
| 1289 | } else { |
| 1290 | // String got promoted. Move it to the old string list. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1291 | external_string_table_.AddOldString(target); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | ASSERT(last <= end); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1296 | external_string_table_.ShrinkNewStrings(static_cast<int>(last - start)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1300 | void Heap::UpdateReferencesInExternalStringTable( |
| 1301 | ExternalStringTableUpdaterCallback updater_func) { |
| 1302 | |
| 1303 | // Update old space string references. |
| 1304 | if (external_string_table_.old_space_strings_.length() > 0) { |
| 1305 | Object** start = &external_string_table_.old_space_strings_[0]; |
| 1306 | Object** end = start + external_string_table_.old_space_strings_.length(); |
| 1307 | for (Object** p = start; p < end; ++p) *p = updater_func(this, p); |
| 1308 | } |
| 1309 | |
| 1310 | UpdateNewSpaceReferencesInExternalStringTable(updater_func); |
| 1311 | } |
| 1312 | |
| 1313 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1314 | static Object* ProcessFunctionWeakReferences(Heap* heap, |
| 1315 | Object* function, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1316 | WeakObjectRetainer* retainer) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1317 | Object* undefined = heap->undefined_value(); |
| 1318 | Object* head = undefined; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1319 | JSFunction* tail = NULL; |
| 1320 | Object* candidate = function; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1321 | while (candidate != undefined) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1322 | // Check whether to keep the candidate in the list. |
| 1323 | JSFunction* candidate_function = reinterpret_cast<JSFunction*>(candidate); |
| 1324 | Object* retain = retainer->RetainAs(candidate); |
| 1325 | if (retain != NULL) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1326 | if (head == undefined) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1327 | // First element in the list. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1328 | head = retain; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1329 | } else { |
| 1330 | // Subsequent elements in the list. |
| 1331 | ASSERT(tail != NULL); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1332 | tail->set_next_function_link(retain); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1333 | } |
| 1334 | // Retained function is new tail. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1335 | candidate_function = reinterpret_cast<JSFunction*>(retain); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1336 | tail = candidate_function; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1337 | |
| 1338 | ASSERT(retain->IsUndefined() || retain->IsJSFunction()); |
| 1339 | |
| 1340 | if (retain == undefined) break; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1341 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1342 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1343 | // Move to next element in the list. |
| 1344 | candidate = candidate_function->next_function_link(); |
| 1345 | } |
| 1346 | |
| 1347 | // Terminate the list if there is one or more elements. |
| 1348 | if (tail != NULL) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1349 | tail->set_next_function_link(undefined); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | return head; |
| 1353 | } |
| 1354 | |
| 1355 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1356 | void Heap::ProcessWeakReferences(WeakObjectRetainer* retainer) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1357 | Object* undefined = undefined_value(); |
| 1358 | Object* head = undefined; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1359 | Context* tail = NULL; |
| 1360 | Object* candidate = global_contexts_list_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1361 | while (candidate != undefined) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1362 | // Check whether to keep the candidate in the list. |
| 1363 | Context* candidate_context = reinterpret_cast<Context*>(candidate); |
| 1364 | Object* retain = retainer->RetainAs(candidate); |
| 1365 | if (retain != NULL) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1366 | if (head == undefined) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1367 | // First element in the list. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1368 | head = retain; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1369 | } else { |
| 1370 | // Subsequent elements in the list. |
| 1371 | ASSERT(tail != NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1372 | tail->set_unchecked(this, |
| 1373 | Context::NEXT_CONTEXT_LINK, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1374 | retain, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1375 | UPDATE_WRITE_BARRIER); |
| 1376 | } |
| 1377 | // Retained context is new tail. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1378 | candidate_context = reinterpret_cast<Context*>(retain); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1379 | tail = candidate_context; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1380 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1381 | if (retain == undefined) break; |
| 1382 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1383 | // Process the weak list of optimized functions for the context. |
| 1384 | Object* function_list_head = |
| 1385 | ProcessFunctionWeakReferences( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1386 | this, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1387 | candidate_context->get(Context::OPTIMIZED_FUNCTIONS_LIST), |
| 1388 | retainer); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1389 | candidate_context->set_unchecked(this, |
| 1390 | Context::OPTIMIZED_FUNCTIONS_LIST, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1391 | function_list_head, |
| 1392 | UPDATE_WRITE_BARRIER); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1393 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1394 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1395 | // Move to next element in the list. |
| 1396 | candidate = candidate_context->get(Context::NEXT_CONTEXT_LINK); |
| 1397 | } |
| 1398 | |
| 1399 | // Terminate the list if there is one or more elements. |
| 1400 | if (tail != NULL) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1401 | tail->set_unchecked(this, |
| 1402 | Context::NEXT_CONTEXT_LINK, |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1403 | Heap::undefined_value(), |
| 1404 | UPDATE_WRITE_BARRIER); |
| 1405 | } |
| 1406 | |
| 1407 | // Update the head of the list of contexts. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1408 | global_contexts_list_ = head; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1412 | void Heap::VisitExternalResources(v8::ExternalResourceVisitor* visitor) { |
| 1413 | AssertNoAllocation no_allocation; |
| 1414 | |
| 1415 | class VisitorAdapter : public ObjectVisitor { |
| 1416 | public: |
| 1417 | explicit VisitorAdapter(v8::ExternalResourceVisitor* visitor) |
| 1418 | : visitor_(visitor) {} |
| 1419 | virtual void VisitPointers(Object** start, Object** end) { |
| 1420 | for (Object** p = start; p < end; p++) { |
| 1421 | if ((*p)->IsExternalString()) { |
| 1422 | visitor_->VisitExternalString(Utils::ToLocal( |
| 1423 | Handle<String>(String::cast(*p)))); |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | private: |
| 1428 | v8::ExternalResourceVisitor* visitor_; |
| 1429 | } visitor_adapter(visitor); |
| 1430 | external_string_table_.Iterate(&visitor_adapter); |
| 1431 | } |
| 1432 | |
| 1433 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1434 | class NewSpaceScavenger : public StaticNewSpaceVisitor<NewSpaceScavenger> { |
| 1435 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1436 | static inline void VisitPointer(Heap* heap, Object** p) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1437 | Object* object = *p; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1438 | if (!heap->InNewSpace(object)) return; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1439 | Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p), |
| 1440 | reinterpret_cast<HeapObject*>(object)); |
| 1441 | } |
| 1442 | }; |
| 1443 | |
| 1444 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1445 | Address Heap::DoScavenge(ObjectVisitor* scavenge_visitor, |
| 1446 | Address new_space_front) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1447 | do { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1448 | SemiSpace::AssertValidRange(new_space_front, new_space_.top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1449 | // The addresses new_space_front and new_space_.top() define a |
| 1450 | // queue of unprocessed copied objects. Process them until the |
| 1451 | // queue is empty. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1452 | while (new_space_front != new_space_.top()) { |
| 1453 | if (!NewSpacePage::IsAtEnd(new_space_front)) { |
| 1454 | HeapObject* object = HeapObject::FromAddress(new_space_front); |
| 1455 | new_space_front += |
| 1456 | NewSpaceScavenger::IterateBody(object->map(), object); |
| 1457 | } else { |
| 1458 | new_space_front = |
| 1459 | NewSpacePage::FromLimit(new_space_front)->next_page()->area_start(); |
| 1460 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | // Promote and process all the to-be-promoted objects. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1464 | { |
| 1465 | StoreBufferRebuildScope scope(this, |
| 1466 | store_buffer(), |
| 1467 | &ScavengeStoreBufferCallback); |
| 1468 | while (!promotion_queue()->is_empty()) { |
| 1469 | HeapObject* target; |
| 1470 | int size; |
| 1471 | promotion_queue()->remove(&target, &size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1472 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1473 | // Promoted object might be already partially visited |
| 1474 | // during old space pointer iteration. Thus we search specificly |
| 1475 | // for pointers to from semispace instead of looking for pointers |
| 1476 | // to new space. |
| 1477 | ASSERT(!target->IsMap()); |
| 1478 | IterateAndMarkPointersToFromSpace(target->address(), |
| 1479 | target->address() + size, |
| 1480 | &ScavengeObject); |
| 1481 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | // Take another spin if there are now unswept objects in new space |
| 1485 | // (there are currently no more unswept promoted objects). |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1486 | } while (new_space_front != new_space_.top()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1487 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1488 | return new_space_front; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1492 | enum LoggingAndProfiling { |
| 1493 | LOGGING_AND_PROFILING_ENABLED, |
| 1494 | LOGGING_AND_PROFILING_DISABLED |
| 1495 | }; |
| 1496 | |
| 1497 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1498 | enum MarksHandling { TRANSFER_MARKS, IGNORE_MARKS }; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1499 | |
| 1500 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1501 | template<MarksHandling marks_handling, |
| 1502 | LoggingAndProfiling logging_and_profiling_mode> |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1503 | class ScavengingVisitor : public StaticVisitorBase { |
| 1504 | public: |
| 1505 | static void Initialize() { |
| 1506 | table_.Register(kVisitSeqAsciiString, &EvacuateSeqAsciiString); |
| 1507 | table_.Register(kVisitSeqTwoByteString, &EvacuateSeqTwoByteString); |
| 1508 | table_.Register(kVisitShortcutCandidate, &EvacuateShortcutCandidate); |
| 1509 | table_.Register(kVisitByteArray, &EvacuateByteArray); |
| 1510 | table_.Register(kVisitFixedArray, &EvacuateFixedArray); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1511 | table_.Register(kVisitFixedDoubleArray, &EvacuateFixedDoubleArray); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1512 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1513 | table_.Register(kVisitGlobalContext, |
| 1514 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1515 | template VisitSpecialized<Context::kSize>); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1516 | |
| 1517 | table_.Register(kVisitConsString, |
| 1518 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1519 | template VisitSpecialized<ConsString::kSize>); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1520 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1521 | table_.Register(kVisitSlicedString, |
| 1522 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1523 | template VisitSpecialized<SlicedString::kSize>); |
| 1524 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1525 | table_.Register(kVisitSharedFunctionInfo, |
| 1526 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1527 | template VisitSpecialized<SharedFunctionInfo::kSize>); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1528 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1529 | table_.Register(kVisitJSWeakMap, |
| 1530 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1531 | Visit); |
| 1532 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1533 | table_.Register(kVisitJSRegExp, |
| 1534 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1535 | Visit); |
| 1536 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1537 | if (marks_handling == IGNORE_MARKS) { |
| 1538 | table_.Register(kVisitJSFunction, |
| 1539 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1540 | template VisitSpecialized<JSFunction::kSize>); |
| 1541 | } else { |
| 1542 | table_.Register(kVisitJSFunction, &EvacuateJSFunction); |
| 1543 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1544 | |
| 1545 | table_.RegisterSpecializations<ObjectEvacuationStrategy<DATA_OBJECT>, |
| 1546 | kVisitDataObject, |
| 1547 | kVisitDataObjectGeneric>(); |
| 1548 | |
| 1549 | table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>, |
| 1550 | kVisitJSObject, |
| 1551 | kVisitJSObjectGeneric>(); |
| 1552 | |
| 1553 | table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>, |
| 1554 | kVisitStruct, |
| 1555 | kVisitStructGeneric>(); |
| 1556 | } |
| 1557 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1558 | static VisitorDispatchTable<ScavengingCallback>* GetTable() { |
| 1559 | return &table_; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1560 | } |
| 1561 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1562 | private: |
| 1563 | enum ObjectContents { DATA_OBJECT, POINTER_OBJECT }; |
| 1564 | enum SizeRestriction { SMALL, UNKNOWN_SIZE }; |
| 1565 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1566 | static void RecordCopiedObject(Heap* heap, HeapObject* obj) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1567 | bool should_record = false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1568 | #ifdef DEBUG |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1569 | should_record = FLAG_heap_stats; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1570 | #endif |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1571 | should_record = should_record || FLAG_log_gc; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1572 | if (should_record) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1573 | if (heap->new_space()->Contains(obj)) { |
| 1574 | heap->new_space()->RecordAllocation(obj); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1575 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1576 | heap->new_space()->RecordPromotion(obj); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1577 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1578 | } |
| 1579 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1580 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1581 | // Helper function used by CopyObject to copy a source object to an |
| 1582 | // allocated target object and update the forwarding pointer in the source |
| 1583 | // object. Returns the target object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1584 | INLINE(static void MigrateObject(Heap* heap, |
| 1585 | HeapObject* source, |
| 1586 | HeapObject* target, |
| 1587 | int size)) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1588 | // Copy the content of source to target. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1589 | heap->CopyBlock(target->address(), source->address(), size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1590 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1591 | // Set the forwarding address. |
| 1592 | source->set_map_word(MapWord::FromForwardingAddress(target)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1593 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1594 | if (logging_and_profiling_mode == LOGGING_AND_PROFILING_ENABLED) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1595 | // Update NewSpace stats if necessary. |
| 1596 | RecordCopiedObject(heap, target); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1597 | HEAP_PROFILE(heap, ObjectMoveEvent(source->address(), target->address())); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1598 | Isolate* isolate = heap->isolate(); |
| 1599 | if (isolate->logger()->is_logging() || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1600 | CpuProfiler::is_profiling(isolate)) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1601 | if (target->IsSharedFunctionInfo()) { |
| 1602 | PROFILE(isolate, SharedFunctionInfoMoveEvent( |
| 1603 | source->address(), target->address())); |
| 1604 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1605 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1608 | if (marks_handling == TRANSFER_MARKS) { |
| 1609 | if (Marking::TransferColor(source, target)) { |
| 1610 | MemoryChunk::IncrementLiveBytesFromGC(target->address(), size); |
| 1611 | } |
| 1612 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1613 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1614 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1615 | template<ObjectContents object_contents, SizeRestriction size_restriction> |
| 1616 | static inline void EvacuateObject(Map* map, |
| 1617 | HeapObject** slot, |
| 1618 | HeapObject* object, |
| 1619 | int object_size) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1620 | SLOW_ASSERT((size_restriction != SMALL) || |
| 1621 | (object_size <= Page::kMaxNonCodeHeapObjectSize)); |
| 1622 | SLOW_ASSERT(object->Size() == object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1623 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1624 | Heap* heap = map->GetHeap(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1625 | if (heap->ShouldBePromoted(object->address(), object_size)) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1626 | MaybeObject* maybe_result; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1627 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1628 | if ((size_restriction != SMALL) && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1629 | (object_size > Page::kMaxNonCodeHeapObjectSize)) { |
| 1630 | maybe_result = heap->lo_space()->AllocateRaw(object_size, |
| 1631 | NOT_EXECUTABLE); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1632 | } else { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1633 | if (object_contents == DATA_OBJECT) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1634 | maybe_result = heap->old_data_space()->AllocateRaw(object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1635 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1636 | maybe_result = heap->old_pointer_space()->AllocateRaw(object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1637 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1638 | } |
| 1639 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1640 | Object* result = NULL; // Initialization to please compiler. |
| 1641 | if (maybe_result->ToObject(&result)) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1642 | HeapObject* target = HeapObject::cast(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1643 | |
| 1644 | // Order is important: slot might be inside of the target if target |
| 1645 | // was allocated over a dead object and slot comes from the store |
| 1646 | // buffer. |
| 1647 | *slot = target; |
| 1648 | MigrateObject(heap, object, target, object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1649 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1650 | if (object_contents == POINTER_OBJECT) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1651 | heap->promotion_queue()->insert(target, object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1652 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1653 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1654 | heap->tracer()->increment_promoted_objects_size(object_size); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1655 | return; |
| 1656 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1657 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1658 | MaybeObject* allocation = heap->new_space()->AllocateRaw(object_size); |
| 1659 | heap->promotion_queue()->SetNewLimit(heap->new_space()->top()); |
| 1660 | Object* result = allocation->ToObjectUnchecked(); |
| 1661 | HeapObject* target = HeapObject::cast(result); |
| 1662 | |
| 1663 | // Order is important: slot might be inside of the target if target |
| 1664 | // was allocated over a dead object and slot comes from the store |
| 1665 | // buffer. |
| 1666 | *slot = target; |
| 1667 | MigrateObject(heap, object, target, object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1668 | return; |
| 1669 | } |
| 1670 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1671 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1672 | static inline void EvacuateJSFunction(Map* map, |
| 1673 | HeapObject** slot, |
| 1674 | HeapObject* object) { |
| 1675 | ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1676 | template VisitSpecialized<JSFunction::kSize>(map, slot, object); |
| 1677 | |
| 1678 | HeapObject* target = *slot; |
| 1679 | MarkBit mark_bit = Marking::MarkBitFrom(target); |
| 1680 | if (Marking::IsBlack(mark_bit)) { |
| 1681 | // This object is black and it might not be rescanned by marker. |
| 1682 | // We should explicitly record code entry slot for compaction because |
| 1683 | // promotion queue processing (IterateAndMarkPointersToFromSpace) will |
| 1684 | // miss it as it is not HeapObject-tagged. |
| 1685 | Address code_entry_slot = |
| 1686 | target->address() + JSFunction::kCodeEntryOffset; |
| 1687 | Code* code = Code::cast(Code::GetObjectFromEntryAddress(code_entry_slot)); |
| 1688 | map->GetHeap()->mark_compact_collector()-> |
| 1689 | RecordCodeEntrySlot(code_entry_slot, code); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1694 | static inline void EvacuateFixedArray(Map* map, |
| 1695 | HeapObject** slot, |
| 1696 | HeapObject* object) { |
| 1697 | int object_size = FixedArray::BodyDescriptor::SizeOf(map, object); |
| 1698 | EvacuateObject<POINTER_OBJECT, UNKNOWN_SIZE>(map, |
| 1699 | slot, |
| 1700 | object, |
| 1701 | object_size); |
| 1702 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1703 | |
| 1704 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1705 | static inline void EvacuateFixedDoubleArray(Map* map, |
| 1706 | HeapObject** slot, |
| 1707 | HeapObject* object) { |
| 1708 | int length = reinterpret_cast<FixedDoubleArray*>(object)->length(); |
| 1709 | int object_size = FixedDoubleArray::SizeFor(length); |
| 1710 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, |
| 1711 | slot, |
| 1712 | object, |
| 1713 | object_size); |
| 1714 | } |
| 1715 | |
| 1716 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1717 | static inline void EvacuateByteArray(Map* map, |
| 1718 | HeapObject** slot, |
| 1719 | HeapObject* object) { |
| 1720 | int object_size = reinterpret_cast<ByteArray*>(object)->ByteArraySize(); |
| 1721 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1722 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1723 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1724 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1725 | static inline void EvacuateSeqAsciiString(Map* map, |
| 1726 | HeapObject** slot, |
| 1727 | HeapObject* object) { |
| 1728 | int object_size = SeqAsciiString::cast(object)-> |
| 1729 | SeqAsciiStringSize(map->instance_type()); |
| 1730 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1731 | } |
| 1732 | |
| 1733 | |
| 1734 | static inline void EvacuateSeqTwoByteString(Map* map, |
| 1735 | HeapObject** slot, |
| 1736 | HeapObject* object) { |
| 1737 | int object_size = SeqTwoByteString::cast(object)-> |
| 1738 | SeqTwoByteStringSize(map->instance_type()); |
| 1739 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | static inline bool IsShortcutCandidate(int type) { |
| 1744 | return ((type & kShortcutTypeMask) == kShortcutTypeTag); |
| 1745 | } |
| 1746 | |
| 1747 | static inline void EvacuateShortcutCandidate(Map* map, |
| 1748 | HeapObject** slot, |
| 1749 | HeapObject* object) { |
| 1750 | ASSERT(IsShortcutCandidate(map->instance_type())); |
| 1751 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1752 | Heap* heap = map->GetHeap(); |
| 1753 | |
| 1754 | if (marks_handling == IGNORE_MARKS && |
| 1755 | ConsString::cast(object)->unchecked_second() == |
| 1756 | heap->empty_string()) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1757 | HeapObject* first = |
| 1758 | HeapObject::cast(ConsString::cast(object)->unchecked_first()); |
| 1759 | |
| 1760 | *slot = first; |
| 1761 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1762 | if (!heap->InNewSpace(first)) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1763 | object->set_map_word(MapWord::FromForwardingAddress(first)); |
| 1764 | return; |
| 1765 | } |
| 1766 | |
| 1767 | MapWord first_word = first->map_word(); |
| 1768 | if (first_word.IsForwardingAddress()) { |
| 1769 | HeapObject* target = first_word.ToForwardingAddress(); |
| 1770 | |
| 1771 | *slot = target; |
| 1772 | object->set_map_word(MapWord::FromForwardingAddress(target)); |
| 1773 | return; |
| 1774 | } |
| 1775 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1776 | heap->DoScavengeObject(first->map(), slot, first); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1777 | object->set_map_word(MapWord::FromForwardingAddress(*slot)); |
| 1778 | return; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1779 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1780 | |
| 1781 | int object_size = ConsString::kSize; |
| 1782 | EvacuateObject<POINTER_OBJECT, SMALL>(map, slot, object, object_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1783 | } |
| 1784 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1785 | template<ObjectContents object_contents> |
| 1786 | class ObjectEvacuationStrategy { |
| 1787 | public: |
| 1788 | template<int object_size> |
| 1789 | static inline void VisitSpecialized(Map* map, |
| 1790 | HeapObject** slot, |
| 1791 | HeapObject* object) { |
| 1792 | EvacuateObject<object_contents, SMALL>(map, slot, object, object_size); |
| 1793 | } |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1794 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1795 | static inline void Visit(Map* map, |
| 1796 | HeapObject** slot, |
| 1797 | HeapObject* object) { |
| 1798 | int object_size = map->instance_size(); |
| 1799 | EvacuateObject<object_contents, SMALL>(map, slot, object, object_size); |
| 1800 | } |
| 1801 | }; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1802 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1803 | static VisitorDispatchTable<ScavengingCallback> table_; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1804 | }; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1805 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1806 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1807 | template<MarksHandling marks_handling, |
| 1808 | LoggingAndProfiling logging_and_profiling_mode> |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1809 | VisitorDispatchTable<ScavengingCallback> |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1810 | ScavengingVisitor<marks_handling, logging_and_profiling_mode>::table_; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1811 | |
| 1812 | |
| 1813 | static void InitializeScavengingVisitorsTables() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1814 | ScavengingVisitor<TRANSFER_MARKS, |
| 1815 | LOGGING_AND_PROFILING_DISABLED>::Initialize(); |
| 1816 | ScavengingVisitor<IGNORE_MARKS, LOGGING_AND_PROFILING_DISABLED>::Initialize(); |
| 1817 | ScavengingVisitor<TRANSFER_MARKS, |
| 1818 | LOGGING_AND_PROFILING_ENABLED>::Initialize(); |
| 1819 | ScavengingVisitor<IGNORE_MARKS, LOGGING_AND_PROFILING_ENABLED>::Initialize(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1823 | void Heap::SelectScavengingVisitorsTable() { |
| 1824 | bool logging_and_profiling = |
| 1825 | isolate()->logger()->is_logging() || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1826 | CpuProfiler::is_profiling(isolate()) || |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1827 | (isolate()->heap_profiler() != NULL && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1828 | isolate()->heap_profiler()->is_profiling()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1829 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1830 | if (!incremental_marking()->IsMarking()) { |
| 1831 | if (!logging_and_profiling) { |
| 1832 | scavenging_visitors_table_.CopyFrom( |
| 1833 | ScavengingVisitor<IGNORE_MARKS, |
| 1834 | LOGGING_AND_PROFILING_DISABLED>::GetTable()); |
| 1835 | } else { |
| 1836 | scavenging_visitors_table_.CopyFrom( |
| 1837 | ScavengingVisitor<IGNORE_MARKS, |
| 1838 | LOGGING_AND_PROFILING_ENABLED>::GetTable()); |
| 1839 | } |
| 1840 | } else { |
| 1841 | if (!logging_and_profiling) { |
| 1842 | scavenging_visitors_table_.CopyFrom( |
| 1843 | ScavengingVisitor<TRANSFER_MARKS, |
| 1844 | LOGGING_AND_PROFILING_DISABLED>::GetTable()); |
| 1845 | } else { |
| 1846 | scavenging_visitors_table_.CopyFrom( |
| 1847 | ScavengingVisitor<TRANSFER_MARKS, |
| 1848 | LOGGING_AND_PROFILING_ENABLED>::GetTable()); |
| 1849 | } |
| 1850 | |
| 1851 | if (incremental_marking()->IsCompacting()) { |
| 1852 | // When compacting forbid short-circuiting of cons-strings. |
| 1853 | // Scavenging code relies on the fact that new space object |
| 1854 | // can't be evacuated into evacuation candidate but |
| 1855 | // short-circuiting violates this assumption. |
| 1856 | scavenging_visitors_table_.Register( |
| 1857 | StaticVisitorBase::kVisitShortcutCandidate, |
| 1858 | scavenging_visitors_table_.GetVisitorById( |
| 1859 | StaticVisitorBase::kVisitConsString)); |
| 1860 | } |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1861 | } |
| 1862 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1863 | |
| 1864 | |
| 1865 | void Heap::ScavengeObjectSlow(HeapObject** p, HeapObject* object) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1866 | SLOW_ASSERT(HEAP->InFromSpace(object)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1867 | MapWord first_word = object->map_word(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1868 | SLOW_ASSERT(!first_word.IsForwardingAddress()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 1869 | Map* map = first_word.ToMap(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1870 | map->GetHeap()->DoScavengeObject(map, p, object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
| 1873 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1874 | MaybeObject* Heap::AllocatePartialMap(InstanceType instance_type, |
| 1875 | int instance_size) { |
| 1876 | Object* result; |
| 1877 | { MaybeObject* maybe_result = AllocateRawMap(); |
| 1878 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1879 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1880 | |
| 1881 | // Map::cast cannot be used due to uninitialized map field. |
| 1882 | reinterpret_cast<Map*>(result)->set_map(raw_unchecked_meta_map()); |
| 1883 | reinterpret_cast<Map*>(result)->set_instance_type(instance_type); |
| 1884 | reinterpret_cast<Map*>(result)->set_instance_size(instance_size); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1885 | reinterpret_cast<Map*>(result)->set_visitor_id( |
| 1886 | StaticVisitorBase::GetVisitorId(instance_type, instance_size)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1887 | reinterpret_cast<Map*>(result)->set_inobject_properties(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1888 | reinterpret_cast<Map*>(result)->set_pre_allocated_property_fields(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1889 | reinterpret_cast<Map*>(result)->set_unused_property_fields(0); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1890 | reinterpret_cast<Map*>(result)->set_bit_field(0); |
| 1891 | reinterpret_cast<Map*>(result)->set_bit_field2(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1892 | return result; |
| 1893 | } |
| 1894 | |
| 1895 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1896 | MaybeObject* Heap::AllocateMap(InstanceType instance_type, |
| 1897 | int instance_size, |
| 1898 | ElementsKind elements_kind) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1899 | Object* result; |
| 1900 | { MaybeObject* maybe_result = AllocateRawMap(); |
| 1901 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1902 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1903 | |
| 1904 | Map* map = reinterpret_cast<Map*>(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1905 | map->set_map_no_write_barrier(meta_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1906 | map->set_instance_type(instance_type); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1907 | map->set_visitor_id( |
| 1908 | StaticVisitorBase::GetVisitorId(instance_type, instance_size)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1909 | map->set_prototype(null_value(), SKIP_WRITE_BARRIER); |
| 1910 | map->set_constructor(null_value(), SKIP_WRITE_BARRIER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1911 | map->set_instance_size(instance_size); |
| 1912 | map->set_inobject_properties(0); |
| 1913 | map->set_pre_allocated_property_fields(0); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1914 | map->init_instance_descriptors(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1915 | map->set_code_cache(empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 1916 | map->set_prototype_transitions(empty_fixed_array(), SKIP_WRITE_BARRIER); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1917 | map->set_unused_property_fields(0); |
| 1918 | map->set_bit_field(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1919 | map->set_bit_field2(1 << Map::kIsExtensible); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1920 | map->set_elements_kind(elements_kind); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1921 | |
| 1922 | // If the map object is aligned fill the padding area with Smi 0 objects. |
| 1923 | if (Map::kPadStart < Map::kSize) { |
| 1924 | memset(reinterpret_cast<byte*>(map) + Map::kPadStart - kHeapObjectTag, |
| 1925 | 0, |
| 1926 | Map::kSize - Map::kPadStart); |
| 1927 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1928 | return map; |
| 1929 | } |
| 1930 | |
| 1931 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1932 | MaybeObject* Heap::AllocateCodeCache() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1933 | CodeCache* code_cache; |
| 1934 | { MaybeObject* maybe_code_cache = AllocateStruct(CODE_CACHE_TYPE); |
| 1935 | if (!maybe_code_cache->To(&code_cache)) return maybe_code_cache; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 1936 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1937 | code_cache->set_default_cache(empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 1938 | code_cache->set_normal_type_cache(undefined_value(), SKIP_WRITE_BARRIER); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1939 | return code_cache; |
| 1940 | } |
| 1941 | |
| 1942 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1943 | MaybeObject* Heap::AllocatePolymorphicCodeCache() { |
| 1944 | return AllocateStruct(POLYMORPHIC_CODE_CACHE_TYPE); |
| 1945 | } |
| 1946 | |
| 1947 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1948 | MaybeObject* Heap::AllocateAccessorPair() { |
| 1949 | AccessorPair* accessors; |
| 1950 | { MaybeObject* maybe_accessors = AllocateStruct(ACCESSOR_PAIR_TYPE); |
| 1951 | if (!maybe_accessors->To(&accessors)) return maybe_accessors; |
| 1952 | } |
| 1953 | accessors->set_getter(the_hole_value(), SKIP_WRITE_BARRIER); |
| 1954 | accessors->set_setter(the_hole_value(), SKIP_WRITE_BARRIER); |
| 1955 | return accessors; |
| 1956 | } |
| 1957 | |
| 1958 | |
| 1959 | MaybeObject* Heap::AllocateTypeFeedbackInfo() { |
| 1960 | TypeFeedbackInfo* info; |
| 1961 | { MaybeObject* maybe_info = AllocateStruct(TYPE_FEEDBACK_INFO_TYPE); |
| 1962 | if (!maybe_info->To(&info)) return maybe_info; |
| 1963 | } |
| 1964 | info->set_ic_total_count(0); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 1965 | info->set_ic_with_type_info_count(0); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1966 | info->set_type_feedback_cells(TypeFeedbackCells::cast(empty_fixed_array()), |
| 1967 | SKIP_WRITE_BARRIER); |
| 1968 | return info; |
| 1969 | } |
| 1970 | |
| 1971 | |
| 1972 | MaybeObject* Heap::AllocateAliasedArgumentsEntry(int aliased_context_slot) { |
| 1973 | AliasedArgumentsEntry* entry; |
| 1974 | { MaybeObject* maybe_entry = AllocateStruct(ALIASED_ARGUMENTS_ENTRY_TYPE); |
| 1975 | if (!maybe_entry->To(&entry)) return maybe_entry; |
| 1976 | } |
| 1977 | entry->set_aliased_context_slot(aliased_context_slot); |
| 1978 | return entry; |
| 1979 | } |
| 1980 | |
| 1981 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1982 | const Heap::StringTypeTable Heap::string_type_table[] = { |
| 1983 | #define STRING_TYPE_ELEMENT(type, size, name, camel_name) \ |
| 1984 | {type, size, k##camel_name##MapRootIndex}, |
| 1985 | STRING_TYPE_LIST(STRING_TYPE_ELEMENT) |
| 1986 | #undef STRING_TYPE_ELEMENT |
| 1987 | }; |
| 1988 | |
| 1989 | |
| 1990 | const Heap::ConstantSymbolTable Heap::constant_symbol_table[] = { |
| 1991 | #define CONSTANT_SYMBOL_ELEMENT(name, contents) \ |
| 1992 | {contents, k##name##RootIndex}, |
| 1993 | SYMBOL_LIST(CONSTANT_SYMBOL_ELEMENT) |
| 1994 | #undef CONSTANT_SYMBOL_ELEMENT |
| 1995 | }; |
| 1996 | |
| 1997 | |
| 1998 | const Heap::StructTable Heap::struct_table[] = { |
| 1999 | #define STRUCT_TABLE_ELEMENT(NAME, Name, name) \ |
| 2000 | { NAME##_TYPE, Name::kSize, k##Name##MapRootIndex }, |
| 2001 | STRUCT_LIST(STRUCT_TABLE_ELEMENT) |
| 2002 | #undef STRUCT_TABLE_ELEMENT |
| 2003 | }; |
| 2004 | |
| 2005 | |
| 2006 | bool Heap::CreateInitialMaps() { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2007 | Object* obj; |
| 2008 | { MaybeObject* maybe_obj = AllocatePartialMap(MAP_TYPE, Map::kSize); |
| 2009 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2010 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2011 | // Map::cast cannot be used due to uninitialized map field. |
| 2012 | Map* new_meta_map = reinterpret_cast<Map*>(obj); |
| 2013 | set_meta_map(new_meta_map); |
| 2014 | new_meta_map->set_map(new_meta_map); |
| 2015 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2016 | { MaybeObject* maybe_obj = |
| 2017 | AllocatePartialMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2018 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2019 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2020 | set_fixed_array_map(Map::cast(obj)); |
| 2021 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2022 | { MaybeObject* maybe_obj = AllocatePartialMap(ODDBALL_TYPE, Oddball::kSize); |
| 2023 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2024 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2025 | set_oddball_map(Map::cast(obj)); |
| 2026 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2027 | // Allocate the empty array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2028 | { MaybeObject* maybe_obj = AllocateEmptyFixedArray(); |
| 2029 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2030 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2031 | set_empty_fixed_array(FixedArray::cast(obj)); |
| 2032 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2033 | { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_POINTER_SPACE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2034 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2035 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2036 | set_null_value(Oddball::cast(obj)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2037 | Oddball::cast(obj)->set_kind(Oddball::kNull); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2038 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2039 | { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_POINTER_SPACE); |
| 2040 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2041 | } |
| 2042 | set_undefined_value(Oddball::cast(obj)); |
| 2043 | Oddball::cast(obj)->set_kind(Oddball::kUndefined); |
| 2044 | ASSERT(!InNewSpace(undefined_value())); |
| 2045 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2046 | // Allocate the empty descriptor array. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2047 | { MaybeObject* maybe_obj = AllocateEmptyFixedArray(); |
| 2048 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2049 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2050 | set_empty_descriptor_array(DescriptorArray::cast(obj)); |
| 2051 | |
| 2052 | // Fix the instance_descriptors for the existing maps. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2053 | meta_map()->init_instance_descriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2054 | meta_map()->set_code_cache(empty_fixed_array()); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 2055 | meta_map()->set_prototype_transitions(empty_fixed_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2056 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2057 | fixed_array_map()->init_instance_descriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2058 | fixed_array_map()->set_code_cache(empty_fixed_array()); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 2059 | fixed_array_map()->set_prototype_transitions(empty_fixed_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2060 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2061 | oddball_map()->init_instance_descriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2062 | oddball_map()->set_code_cache(empty_fixed_array()); |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 2063 | oddball_map()->set_prototype_transitions(empty_fixed_array()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2064 | |
| 2065 | // Fix prototype object for existing maps. |
| 2066 | meta_map()->set_prototype(null_value()); |
| 2067 | meta_map()->set_constructor(null_value()); |
| 2068 | |
| 2069 | fixed_array_map()->set_prototype(null_value()); |
| 2070 | fixed_array_map()->set_constructor(null_value()); |
| 2071 | |
| 2072 | oddball_map()->set_prototype(null_value()); |
| 2073 | oddball_map()->set_constructor(null_value()); |
| 2074 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2075 | { MaybeObject* maybe_obj = |
| 2076 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2077 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2078 | } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2079 | set_fixed_cow_array_map(Map::cast(obj)); |
| 2080 | ASSERT(fixed_array_map() != fixed_cow_array_map()); |
| 2081 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2082 | { MaybeObject* maybe_obj = |
| 2083 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2084 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2085 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2086 | set_scope_info_map(Map::cast(obj)); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2087 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2088 | { MaybeObject* maybe_obj = AllocateMap(HEAP_NUMBER_TYPE, HeapNumber::kSize); |
| 2089 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2090 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2091 | set_heap_number_map(Map::cast(obj)); |
| 2092 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2093 | { MaybeObject* maybe_obj = AllocateMap(FOREIGN_TYPE, Foreign::kSize); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2094 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2095 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2096 | set_foreign_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2097 | |
| 2098 | for (unsigned i = 0; i < ARRAY_SIZE(string_type_table); i++) { |
| 2099 | const StringTypeTable& entry = string_type_table[i]; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2100 | { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size); |
| 2101 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2102 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2103 | roots_[entry.index] = Map::cast(obj); |
| 2104 | } |
| 2105 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2106 | { MaybeObject* maybe_obj = AllocateMap(STRING_TYPE, kVariableSizeSentinel); |
| 2107 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2108 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2109 | set_undetectable_string_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2110 | Map::cast(obj)->set_is_undetectable(); |
| 2111 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2112 | { MaybeObject* maybe_obj = |
| 2113 | AllocateMap(ASCII_STRING_TYPE, kVariableSizeSentinel); |
| 2114 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2115 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2116 | set_undetectable_ascii_string_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2117 | Map::cast(obj)->set_is_undetectable(); |
| 2118 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2119 | { MaybeObject* maybe_obj = |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2120 | AllocateMap(FIXED_DOUBLE_ARRAY_TYPE, kVariableSizeSentinel); |
| 2121 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2122 | } |
| 2123 | set_fixed_double_array_map(Map::cast(obj)); |
| 2124 | |
| 2125 | { MaybeObject* maybe_obj = |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2126 | AllocateMap(BYTE_ARRAY_TYPE, kVariableSizeSentinel); |
| 2127 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2128 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2129 | set_byte_array_map(Map::cast(obj)); |
| 2130 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2131 | { MaybeObject* maybe_obj = |
| 2132 | AllocateMap(FREE_SPACE_TYPE, kVariableSizeSentinel); |
| 2133 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2134 | } |
| 2135 | set_free_space_map(Map::cast(obj)); |
| 2136 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2137 | { MaybeObject* maybe_obj = AllocateByteArray(0, TENURED); |
| 2138 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2139 | } |
| 2140 | set_empty_byte_array(ByteArray::cast(obj)); |
| 2141 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2142 | { MaybeObject* maybe_obj = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2143 | AllocateMap(EXTERNAL_PIXEL_ARRAY_TYPE, ExternalArray::kAlignedSize); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2144 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2145 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2146 | set_external_pixel_array_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2147 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2148 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_BYTE_ARRAY_TYPE, |
| 2149 | ExternalArray::kAlignedSize); |
| 2150 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2151 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2152 | set_external_byte_array_map(Map::cast(obj)); |
| 2153 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2154 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE, |
| 2155 | ExternalArray::kAlignedSize); |
| 2156 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2157 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2158 | set_external_unsigned_byte_array_map(Map::cast(obj)); |
| 2159 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2160 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_SHORT_ARRAY_TYPE, |
| 2161 | ExternalArray::kAlignedSize); |
| 2162 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2163 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2164 | set_external_short_array_map(Map::cast(obj)); |
| 2165 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2166 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE, |
| 2167 | ExternalArray::kAlignedSize); |
| 2168 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2169 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2170 | set_external_unsigned_short_array_map(Map::cast(obj)); |
| 2171 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2172 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_INT_ARRAY_TYPE, |
| 2173 | ExternalArray::kAlignedSize); |
| 2174 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2175 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2176 | set_external_int_array_map(Map::cast(obj)); |
| 2177 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2178 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE, |
| 2179 | ExternalArray::kAlignedSize); |
| 2180 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2181 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2182 | set_external_unsigned_int_array_map(Map::cast(obj)); |
| 2183 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2184 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_FLOAT_ARRAY_TYPE, |
| 2185 | ExternalArray::kAlignedSize); |
| 2186 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2187 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2188 | set_external_float_array_map(Map::cast(obj)); |
| 2189 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2190 | { MaybeObject* maybe_obj = |
| 2191 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2192 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2193 | } |
| 2194 | set_non_strict_arguments_elements_map(Map::cast(obj)); |
| 2195 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2196 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_DOUBLE_ARRAY_TYPE, |
| 2197 | ExternalArray::kAlignedSize); |
| 2198 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2199 | } |
| 2200 | set_external_double_array_map(Map::cast(obj)); |
| 2201 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2202 | { MaybeObject* maybe_obj = AllocateMap(CODE_TYPE, kVariableSizeSentinel); |
| 2203 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2204 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2205 | set_code_map(Map::cast(obj)); |
| 2206 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2207 | { MaybeObject* maybe_obj = AllocateMap(JS_GLOBAL_PROPERTY_CELL_TYPE, |
| 2208 | JSGlobalPropertyCell::kSize); |
| 2209 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2210 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2211 | set_global_property_cell_map(Map::cast(obj)); |
| 2212 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2213 | { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, kPointerSize); |
| 2214 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2215 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2216 | set_one_pointer_filler_map(Map::cast(obj)); |
| 2217 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2218 | { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, 2 * kPointerSize); |
| 2219 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2220 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2221 | set_two_pointer_filler_map(Map::cast(obj)); |
| 2222 | |
| 2223 | for (unsigned i = 0; i < ARRAY_SIZE(struct_table); i++) { |
| 2224 | const StructTable& entry = struct_table[i]; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2225 | { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size); |
| 2226 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2227 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2228 | roots_[entry.index] = Map::cast(obj); |
| 2229 | } |
| 2230 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2231 | { MaybeObject* maybe_obj = |
| 2232 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2233 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2234 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2235 | set_hash_table_map(Map::cast(obj)); |
| 2236 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2237 | { MaybeObject* maybe_obj = |
| 2238 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2239 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2240 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2241 | set_function_context_map(Map::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2242 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2243 | { MaybeObject* maybe_obj = |
| 2244 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2245 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2246 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2247 | set_catch_context_map(Map::cast(obj)); |
| 2248 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2249 | { MaybeObject* maybe_obj = |
| 2250 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2251 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2252 | } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2253 | set_with_context_map(Map::cast(obj)); |
| 2254 | |
| 2255 | { MaybeObject* maybe_obj = |
| 2256 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2257 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2258 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 2259 | set_block_context_map(Map::cast(obj)); |
| 2260 | |
| 2261 | { MaybeObject* maybe_obj = |
| 2262 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2263 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2264 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2265 | set_module_context_map(Map::cast(obj)); |
| 2266 | |
| 2267 | { MaybeObject* maybe_obj = |
| 2268 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 2269 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2270 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 2271 | Map* global_context_map = Map::cast(obj); |
| 2272 | global_context_map->set_visitor_id(StaticVisitorBase::kVisitGlobalContext); |
| 2273 | set_global_context_map(global_context_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2274 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2275 | { MaybeObject* maybe_obj = AllocateMap(SHARED_FUNCTION_INFO_TYPE, |
| 2276 | SharedFunctionInfo::kAlignedSize); |
| 2277 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2278 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2279 | set_shared_function_info_map(Map::cast(obj)); |
| 2280 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2281 | { MaybeObject* maybe_obj = AllocateMap(JS_MESSAGE_OBJECT_TYPE, |
| 2282 | JSMessageObject::kSize); |
| 2283 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2284 | } |
| 2285 | set_message_object_map(Map::cast(obj)); |
| 2286 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2287 | ASSERT(!InNewSpace(empty_fixed_array())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2288 | return true; |
| 2289 | } |
| 2290 | |
| 2291 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2292 | MaybeObject* Heap::AllocateHeapNumber(double value, PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2293 | // Statically ensure that it is safe to allocate heap numbers in paged |
| 2294 | // spaces. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2295 | STATIC_ASSERT(HeapNumber::kSize <= Page::kNonCodeObjectAreaSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2296 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 2297 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2298 | Object* result; |
| 2299 | { MaybeObject* maybe_result = |
| 2300 | AllocateRaw(HeapNumber::kSize, space, OLD_DATA_SPACE); |
| 2301 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2302 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2303 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2304 | HeapObject::cast(result)->set_map_no_write_barrier(heap_number_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2305 | HeapNumber::cast(result)->set_value(value); |
| 2306 | return result; |
| 2307 | } |
| 2308 | |
| 2309 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2310 | MaybeObject* Heap::AllocateHeapNumber(double value) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2311 | // Use general version, if we're forced to always allocate. |
| 2312 | if (always_allocate()) return AllocateHeapNumber(value, TENURED); |
| 2313 | |
| 2314 | // This version of AllocateHeapNumber is optimized for |
| 2315 | // allocation in new space. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2316 | STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxNonCodeHeapObjectSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2317 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2318 | Object* result; |
| 2319 | { MaybeObject* maybe_result = new_space_.AllocateRaw(HeapNumber::kSize); |
| 2320 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2321 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2322 | HeapObject::cast(result)->set_map_no_write_barrier(heap_number_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2323 | HeapNumber::cast(result)->set_value(value); |
| 2324 | return result; |
| 2325 | } |
| 2326 | |
| 2327 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2328 | MaybeObject* Heap::AllocateJSGlobalPropertyCell(Object* value) { |
| 2329 | Object* result; |
| 2330 | { MaybeObject* maybe_result = AllocateRawCell(); |
| 2331 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2332 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2333 | HeapObject::cast(result)->set_map_no_write_barrier( |
| 2334 | global_property_cell_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2335 | JSGlobalPropertyCell::cast(result)->set_value(value); |
| 2336 | return result; |
| 2337 | } |
| 2338 | |
| 2339 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2340 | MaybeObject* Heap::CreateOddball(const char* to_string, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2341 | Object* to_number, |
| 2342 | byte kind) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2343 | Object* result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2344 | { MaybeObject* maybe_result = Allocate(oddball_map(), OLD_POINTER_SPACE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2345 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2346 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2347 | return Oddball::cast(result)->Initialize(to_string, to_number, kind); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | |
| 2351 | bool Heap::CreateApiObjects() { |
| 2352 | Object* obj; |
| 2353 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2354 | { MaybeObject* maybe_obj = AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 2355 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2356 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2357 | // Don't use Smi-only elements optimizations for objects with the neander |
| 2358 | // map. There are too many cases where element values are set directly with a |
| 2359 | // bottleneck to trap the Smi-only -> fast elements transition, and there |
| 2360 | // appears to be no benefit for optimize this case. |
| 2361 | Map* new_neander_map = Map::cast(obj); |
| 2362 | new_neander_map->set_elements_kind(FAST_ELEMENTS); |
| 2363 | set_neander_map(new_neander_map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2364 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2365 | { MaybeObject* maybe_obj = AllocateJSObjectFromMap(neander_map()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2366 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2367 | } |
| 2368 | Object* elements; |
| 2369 | { MaybeObject* maybe_elements = AllocateFixedArray(2); |
| 2370 | if (!maybe_elements->ToObject(&elements)) return false; |
| 2371 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2372 | FixedArray::cast(elements)->set(0, Smi::FromInt(0)); |
| 2373 | JSObject::cast(obj)->set_elements(FixedArray::cast(elements)); |
| 2374 | set_message_listeners(JSObject::cast(obj)); |
| 2375 | |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2380 | void Heap::CreateJSEntryStub() { |
| 2381 | JSEntryStub stub; |
| 2382 | set_js_entry_code(*stub.GetCode()); |
| 2383 | } |
| 2384 | |
| 2385 | |
| 2386 | void Heap::CreateJSConstructEntryStub() { |
| 2387 | JSConstructEntryStub stub; |
| 2388 | set_js_construct_entry_code(*stub.GetCode()); |
| 2389 | } |
| 2390 | |
| 2391 | |
| 2392 | void Heap::CreateFixedStubs() { |
| 2393 | // Here we create roots for fixed stubs. They are needed at GC |
| 2394 | // for cooking and uncooking (check out frames.cc). |
| 2395 | // The eliminates the need for doing dictionary lookup in the |
| 2396 | // stub cache for these stubs. |
| 2397 | HandleScope scope; |
| 2398 | // gcc-4.4 has problem generating correct code of following snippet: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2399 | // { JSEntryStub stub; |
| 2400 | // js_entry_code_ = *stub.GetCode(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2401 | // } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2402 | // { JSConstructEntryStub stub; |
| 2403 | // js_construct_entry_code_ = *stub.GetCode(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2404 | // } |
| 2405 | // To workaround the problem, make separate functions without inlining. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2406 | Heap::CreateJSEntryStub(); |
| 2407 | Heap::CreateJSConstructEntryStub(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2408 | |
| 2409 | // Create stubs that should be there, so we don't unexpectedly have to |
| 2410 | // create them if we need them during the creation of another stub. |
| 2411 | // Stub creation mixes raw pointers and handles in an unsafe manner so |
| 2412 | // we cannot create stubs while we are creating stubs. |
| 2413 | CodeStub::GenerateStubsAheadOfTime(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | |
| 2417 | bool Heap::CreateInitialObjects() { |
| 2418 | Object* obj; |
| 2419 | |
| 2420 | // The -0 value must be set before NumberFromDouble works. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2421 | { MaybeObject* maybe_obj = AllocateHeapNumber(-0.0, TENURED); |
| 2422 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2423 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2424 | set_minus_zero_value(HeapNumber::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2425 | ASSERT(signbit(minus_zero_value()->Number()) != 0); |
| 2426 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2427 | { MaybeObject* maybe_obj = AllocateHeapNumber(OS::nan_value(), TENURED); |
| 2428 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2429 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2430 | set_nan_value(HeapNumber::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2431 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2432 | { MaybeObject* maybe_obj = AllocateHeapNumber(V8_INFINITY, TENURED); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2433 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2434 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2435 | set_infinity_value(HeapNumber::cast(obj)); |
| 2436 | |
| 2437 | // The hole has not been created yet, but we want to put something |
| 2438 | // predictable in the gaps in the symbol table, so lets make that Smi zero. |
| 2439 | set_the_hole_value(reinterpret_cast<Oddball*>(Smi::FromInt(0))); |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 2440 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2441 | // Allocate initial symbol table. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2442 | { MaybeObject* maybe_obj = SymbolTable::Allocate(kInitialSymbolTableSize); |
| 2443 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2444 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2445 | // Don't use set_symbol_table() due to asserts. |
| 2446 | roots_[kSymbolTableRootIndex] = obj; |
| 2447 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2448 | // Finish initializing oddballs after creating symboltable. |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 2449 | { MaybeObject* maybe_obj = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2450 | undefined_value()->Initialize("undefined", |
| 2451 | nan_value(), |
| 2452 | Oddball::kUndefined); |
| 2453 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2454 | } |
| 2455 | |
| 2456 | // Initialize the null_value. |
| 2457 | { MaybeObject* maybe_obj = |
| 2458 | null_value()->Initialize("null", Smi::FromInt(0), Oddball::kNull); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2459 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2460 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2461 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2462 | { MaybeObject* maybe_obj = CreateOddball("true", |
| 2463 | Smi::FromInt(1), |
| 2464 | Oddball::kTrue); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2465 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2466 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2467 | set_true_value(Oddball::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2468 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2469 | { MaybeObject* maybe_obj = CreateOddball("false", |
| 2470 | Smi::FromInt(0), |
| 2471 | Oddball::kFalse); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2472 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2473 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2474 | set_false_value(Oddball::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2475 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2476 | { MaybeObject* maybe_obj = CreateOddball("hole", |
| 2477 | Smi::FromInt(-1), |
| 2478 | Oddball::kTheHole); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2479 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2480 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2481 | set_the_hole_value(Oddball::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2482 | |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2483 | { MaybeObject* maybe_obj = CreateOddball("arguments_marker", |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 2484 | Smi::FromInt(-4), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2485 | Oddball::kArgumentMarker); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2486 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2487 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2488 | set_arguments_marker(Oddball::cast(obj)); |
Ben Murdoch | 086aeea | 2011-05-13 15:57:08 +0100 | [diff] [blame] | 2489 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2490 | { MaybeObject* maybe_obj = CreateOddball("no_interceptor_result_sentinel", |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 2491 | Smi::FromInt(-2), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2492 | Oddball::kOther); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2493 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2494 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2495 | set_no_interceptor_result_sentinel(obj); |
| 2496 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2497 | { MaybeObject* maybe_obj = CreateOddball("termination_exception", |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 2498 | Smi::FromInt(-3), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2499 | Oddball::kOther); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2500 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2501 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2502 | set_termination_exception(obj); |
| 2503 | |
| 2504 | // Allocate the empty string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2505 | { MaybeObject* maybe_obj = AllocateRawAsciiString(0, TENURED); |
| 2506 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2507 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2508 | set_empty_string(String::cast(obj)); |
| 2509 | |
| 2510 | for (unsigned i = 0; i < ARRAY_SIZE(constant_symbol_table); i++) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2511 | { MaybeObject* maybe_obj = |
| 2512 | LookupAsciiSymbol(constant_symbol_table[i].contents); |
| 2513 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2514 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2515 | roots_[constant_symbol_table[i].index] = String::cast(obj); |
| 2516 | } |
| 2517 | |
| 2518 | // Allocate the hidden symbol which is used to identify the hidden properties |
| 2519 | // in JSObjects. The hash code has a special value so that it will not match |
| 2520 | // the empty string when searching for the property. It cannot be part of the |
| 2521 | // loop above because it needs to be allocated manually with the special |
| 2522 | // hash code in place. The hash code for the hidden_symbol is zero to ensure |
| 2523 | // that it will always be at the first entry in property descriptors. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2524 | { MaybeObject* maybe_obj = |
| 2525 | AllocateSymbol(CStrVector(""), 0, String::kZeroHash); |
| 2526 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2527 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2528 | hidden_symbol_ = String::cast(obj); |
| 2529 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2530 | // Allocate the foreign for __proto__. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2531 | { MaybeObject* maybe_obj = |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2532 | AllocateForeign((Address) &Accessors::ObjectPrototype); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2533 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2534 | } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2535 | set_prototype_accessors(Foreign::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2536 | |
| 2537 | // Allocate the code_stubs dictionary. The initial size is set to avoid |
| 2538 | // expanding the dictionary during bootstrapping. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 2539 | { MaybeObject* maybe_obj = UnseededNumberDictionary::Allocate(128); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2540 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2541 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 2542 | set_code_stubs(UnseededNumberDictionary::cast(obj)); |
| 2543 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2544 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2545 | // Allocate the non_monomorphic_cache used in stub-cache.cc. The initial size |
| 2546 | // is set to avoid expanding the dictionary during bootstrapping. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 2547 | { MaybeObject* maybe_obj = UnseededNumberDictionary::Allocate(64); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2548 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2549 | } |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 2550 | set_non_monomorphic_cache(UnseededNumberDictionary::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2551 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2552 | { MaybeObject* maybe_obj = AllocatePolymorphicCodeCache(); |
| 2553 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2554 | } |
| 2555 | set_polymorphic_code_cache(PolymorphicCodeCache::cast(obj)); |
| 2556 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2557 | set_instanceof_cache_function(Smi::FromInt(0)); |
| 2558 | set_instanceof_cache_map(Smi::FromInt(0)); |
| 2559 | set_instanceof_cache_answer(Smi::FromInt(0)); |
| 2560 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2561 | CreateFixedStubs(); |
| 2562 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2563 | // Allocate the dictionary of intrinsic function names. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2564 | { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions); |
| 2565 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2566 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2567 | { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(this, |
| 2568 | obj); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2569 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2570 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 2571 | set_intrinsic_function_names(StringDictionary::cast(obj)); |
| 2572 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2573 | { MaybeObject* maybe_obj = AllocateInitialNumberStringCache(); |
| 2574 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2575 | } |
| 2576 | set_number_string_cache(FixedArray::cast(obj)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2577 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2578 | // Allocate cache for single character ASCII strings. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2579 | { MaybeObject* maybe_obj = |
| 2580 | AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED); |
| 2581 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2582 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2583 | set_single_character_string_cache(FixedArray::cast(obj)); |
| 2584 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2585 | // Allocate cache for string split. |
| 2586 | { MaybeObject* maybe_obj = |
| 2587 | AllocateFixedArray(StringSplitCache::kStringSplitCacheSize, TENURED); |
| 2588 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2589 | } |
| 2590 | set_string_split_cache(FixedArray::cast(obj)); |
| 2591 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2592 | // Allocate cache for external strings pointing to native source code. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2593 | { MaybeObject* maybe_obj = AllocateFixedArray(Natives::GetBuiltinsCount()); |
| 2594 | if (!maybe_obj->ToObject(&obj)) return false; |
| 2595 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2596 | set_natives_source_cache(FixedArray::cast(obj)); |
| 2597 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2598 | // Handling of script id generation is in FACTORY->NewScript. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2599 | set_last_script_id(undefined_value()); |
| 2600 | |
| 2601 | // Initialize keyed lookup cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2602 | isolate_->keyed_lookup_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2603 | |
| 2604 | // Initialize context slot cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2605 | isolate_->context_slot_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2606 | |
| 2607 | // Initialize descriptor cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2608 | isolate_->descriptor_lookup_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2609 | |
| 2610 | // Initialize compilation cache. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2611 | isolate_->compilation_cache()->Clear(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2612 | |
| 2613 | return true; |
| 2614 | } |
| 2615 | |
| 2616 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2617 | Object* StringSplitCache::Lookup( |
| 2618 | FixedArray* cache, String* string, String* pattern) { |
| 2619 | if (!string->IsSymbol() || !pattern->IsSymbol()) return Smi::FromInt(0); |
| 2620 | uint32_t hash = string->Hash(); |
| 2621 | uint32_t index = ((hash & (kStringSplitCacheSize - 1)) & |
| 2622 | ~(kArrayEntriesPerCacheEntry - 1)); |
| 2623 | if (cache->get(index + kStringOffset) == string && |
| 2624 | cache->get(index + kPatternOffset) == pattern) { |
| 2625 | return cache->get(index + kArrayOffset); |
| 2626 | } |
| 2627 | index = ((index + kArrayEntriesPerCacheEntry) & (kStringSplitCacheSize - 1)); |
| 2628 | if (cache->get(index + kStringOffset) == string && |
| 2629 | cache->get(index + kPatternOffset) == pattern) { |
| 2630 | return cache->get(index + kArrayOffset); |
| 2631 | } |
| 2632 | return Smi::FromInt(0); |
| 2633 | } |
| 2634 | |
| 2635 | |
| 2636 | void StringSplitCache::Enter(Heap* heap, |
| 2637 | FixedArray* cache, |
| 2638 | String* string, |
| 2639 | String* pattern, |
| 2640 | FixedArray* array) { |
| 2641 | if (!string->IsSymbol() || !pattern->IsSymbol()) return; |
| 2642 | uint32_t hash = string->Hash(); |
| 2643 | uint32_t index = ((hash & (kStringSplitCacheSize - 1)) & |
| 2644 | ~(kArrayEntriesPerCacheEntry - 1)); |
| 2645 | if (cache->get(index + kStringOffset) == Smi::FromInt(0)) { |
| 2646 | cache->set(index + kStringOffset, string); |
| 2647 | cache->set(index + kPatternOffset, pattern); |
| 2648 | cache->set(index + kArrayOffset, array); |
| 2649 | } else { |
| 2650 | uint32_t index2 = |
| 2651 | ((index + kArrayEntriesPerCacheEntry) & (kStringSplitCacheSize - 1)); |
| 2652 | if (cache->get(index2 + kStringOffset) == Smi::FromInt(0)) { |
| 2653 | cache->set(index2 + kStringOffset, string); |
| 2654 | cache->set(index2 + kPatternOffset, pattern); |
| 2655 | cache->set(index2 + kArrayOffset, array); |
| 2656 | } else { |
| 2657 | cache->set(index2 + kStringOffset, Smi::FromInt(0)); |
| 2658 | cache->set(index2 + kPatternOffset, Smi::FromInt(0)); |
| 2659 | cache->set(index2 + kArrayOffset, Smi::FromInt(0)); |
| 2660 | cache->set(index + kStringOffset, string); |
| 2661 | cache->set(index + kPatternOffset, pattern); |
| 2662 | cache->set(index + kArrayOffset, array); |
| 2663 | } |
| 2664 | } |
| 2665 | if (array->length() < 100) { // Limit how many new symbols we want to make. |
| 2666 | for (int i = 0; i < array->length(); i++) { |
| 2667 | String* str = String::cast(array->get(i)); |
| 2668 | Object* symbol; |
| 2669 | MaybeObject* maybe_symbol = heap->LookupSymbol(str); |
| 2670 | if (maybe_symbol->ToObject(&symbol)) { |
| 2671 | array->set(i, symbol); |
| 2672 | } |
| 2673 | } |
| 2674 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2675 | array->set_map_no_write_barrier(heap->fixed_cow_array_map()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | |
| 2679 | void StringSplitCache::Clear(FixedArray* cache) { |
| 2680 | for (int i = 0; i < kStringSplitCacheSize; i++) { |
| 2681 | cache->set(i, Smi::FromInt(0)); |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2686 | MaybeObject* Heap::AllocateInitialNumberStringCache() { |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 2687 | MaybeObject* maybe_obj = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2688 | AllocateFixedArray(kInitialNumberStringCacheSize * 2, TENURED); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 2689 | return maybe_obj; |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2693 | int Heap::FullSizeNumberStringCacheLength() { |
| 2694 | // Compute the size of the number string cache based on the max newspace size. |
| 2695 | // The number string cache has a minimum size based on twice the initial cache |
| 2696 | // size to ensure that it is bigger after being made 'full size'. |
| 2697 | int number_string_cache_size = max_semispace_size_ / 512; |
| 2698 | number_string_cache_size = Max(kInitialNumberStringCacheSize * 2, |
| 2699 | Min(0x4000, number_string_cache_size)); |
| 2700 | // There is a string and a number per entry so the length is twice the number |
| 2701 | // of entries. |
| 2702 | return number_string_cache_size * 2; |
| 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | void Heap::AllocateFullSizeNumberStringCache() { |
| 2707 | // The idea is to have a small number string cache in the snapshot to keep |
| 2708 | // boot-time memory usage down. If we expand the number string cache already |
| 2709 | // while creating the snapshot then that didn't work out. |
| 2710 | ASSERT(!Serializer::enabled()); |
| 2711 | MaybeObject* maybe_obj = |
| 2712 | AllocateFixedArray(FullSizeNumberStringCacheLength(), TENURED); |
| 2713 | Object* new_cache; |
| 2714 | if (maybe_obj->ToObject(&new_cache)) { |
| 2715 | // We don't bother to repopulate the cache with entries from the old cache. |
| 2716 | // It will be repopulated soon enough with new strings. |
| 2717 | set_number_string_cache(FixedArray::cast(new_cache)); |
| 2718 | } |
| 2719 | // If allocation fails then we just return without doing anything. It is only |
| 2720 | // a cache, so best effort is OK here. |
| 2721 | } |
| 2722 | |
| 2723 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2724 | void Heap::FlushNumberStringCache() { |
| 2725 | // Flush the number to string cache. |
| 2726 | int len = number_string_cache()->length(); |
| 2727 | for (int i = 0; i < len; i++) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2728 | number_string_cache()->set_undefined(this, i); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2733 | static inline int double_get_hash(double d) { |
| 2734 | DoubleRepresentation rep(d); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2735 | return static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2736 | } |
| 2737 | |
| 2738 | |
| 2739 | static inline int smi_get_hash(Smi* smi) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2740 | return smi->value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2744 | Object* Heap::GetNumberStringCache(Object* number) { |
| 2745 | int hash; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2746 | int mask = (number_string_cache()->length() >> 1) - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2747 | if (number->IsSmi()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2748 | hash = smi_get_hash(Smi::cast(number)) & mask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2749 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2750 | hash = double_get_hash(number->Number()) & mask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2751 | } |
| 2752 | Object* key = number_string_cache()->get(hash * 2); |
| 2753 | if (key == number) { |
| 2754 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
| 2755 | } else if (key->IsHeapNumber() && |
| 2756 | number->IsHeapNumber() && |
| 2757 | key->Number() == number->Number()) { |
| 2758 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
| 2759 | } |
| 2760 | return undefined_value(); |
| 2761 | } |
| 2762 | |
| 2763 | |
| 2764 | void Heap::SetNumberStringCache(Object* number, String* string) { |
| 2765 | int hash; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2766 | int mask = (number_string_cache()->length() >> 1) - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2767 | if (number->IsSmi()) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2768 | hash = smi_get_hash(Smi::cast(number)) & mask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2769 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2770 | hash = double_get_hash(number->Number()) & mask; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2771 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2772 | if (number_string_cache()->get(hash * 2) != undefined_value() && |
| 2773 | number_string_cache()->length() != FullSizeNumberStringCacheLength()) { |
| 2774 | // The first time we have a hash collision, we move to the full sized |
| 2775 | // number string cache. |
| 2776 | AllocateFullSizeNumberStringCache(); |
| 2777 | return; |
| 2778 | } |
| 2779 | number_string_cache()->set(hash * 2, number); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2780 | number_string_cache()->set(hash * 2 + 1, string); |
| 2781 | } |
| 2782 | |
| 2783 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2784 | MaybeObject* Heap::NumberToString(Object* number, |
| 2785 | bool check_number_string_cache) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2786 | isolate_->counters()->number_to_string_runtime()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2787 | if (check_number_string_cache) { |
| 2788 | Object* cached = GetNumberStringCache(number); |
| 2789 | if (cached != undefined_value()) { |
| 2790 | return cached; |
| 2791 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | char arr[100]; |
| 2795 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 2796 | const char* str; |
| 2797 | if (number->IsSmi()) { |
| 2798 | int num = Smi::cast(number)->value(); |
| 2799 | str = IntToCString(num, buffer); |
| 2800 | } else { |
| 2801 | double num = HeapNumber::cast(number)->value(); |
| 2802 | str = DoubleToCString(num, buffer); |
| 2803 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2804 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2805 | Object* js_string; |
| 2806 | MaybeObject* maybe_js_string = AllocateStringFromAscii(CStrVector(str)); |
| 2807 | if (maybe_js_string->ToObject(&js_string)) { |
| 2808 | SetNumberStringCache(number, String::cast(js_string)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2809 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2810 | return maybe_js_string; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2814 | MaybeObject* Heap::Uint32ToString(uint32_t value, |
| 2815 | bool check_number_string_cache) { |
| 2816 | Object* number; |
| 2817 | MaybeObject* maybe = NumberFromUint32(value); |
| 2818 | if (!maybe->To<Object>(&number)) return maybe; |
| 2819 | return NumberToString(number, check_number_string_cache); |
| 2820 | } |
| 2821 | |
| 2822 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2823 | Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) { |
| 2824 | return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]); |
| 2825 | } |
| 2826 | |
| 2827 | |
| 2828 | Heap::RootListIndex Heap::RootIndexForExternalArrayType( |
| 2829 | ExternalArrayType array_type) { |
| 2830 | switch (array_type) { |
| 2831 | case kExternalByteArray: |
| 2832 | return kExternalByteArrayMapRootIndex; |
| 2833 | case kExternalUnsignedByteArray: |
| 2834 | return kExternalUnsignedByteArrayMapRootIndex; |
| 2835 | case kExternalShortArray: |
| 2836 | return kExternalShortArrayMapRootIndex; |
| 2837 | case kExternalUnsignedShortArray: |
| 2838 | return kExternalUnsignedShortArrayMapRootIndex; |
| 2839 | case kExternalIntArray: |
| 2840 | return kExternalIntArrayMapRootIndex; |
| 2841 | case kExternalUnsignedIntArray: |
| 2842 | return kExternalUnsignedIntArrayMapRootIndex; |
| 2843 | case kExternalFloatArray: |
| 2844 | return kExternalFloatArrayMapRootIndex; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2845 | case kExternalDoubleArray: |
| 2846 | return kExternalDoubleArrayMapRootIndex; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2847 | case kExternalPixelArray: |
| 2848 | return kExternalPixelArrayMapRootIndex; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2849 | default: |
| 2850 | UNREACHABLE(); |
| 2851 | return kUndefinedValueRootIndex; |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2856 | MaybeObject* Heap::NumberFromDouble(double value, PretenureFlag pretenure) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2857 | // We need to distinguish the minus zero value and this cannot be |
| 2858 | // done after conversion to int. Doing this by comparing bit |
| 2859 | // patterns is faster than using fpclassify() et al. |
| 2860 | static const DoubleRepresentation minus_zero(-0.0); |
| 2861 | |
| 2862 | DoubleRepresentation rep(value); |
| 2863 | if (rep.bits == minus_zero.bits) { |
| 2864 | return AllocateHeapNumber(-0.0, pretenure); |
| 2865 | } |
| 2866 | |
| 2867 | int int_value = FastD2I(value); |
| 2868 | if (value == int_value && Smi::IsValid(int_value)) { |
| 2869 | return Smi::FromInt(int_value); |
| 2870 | } |
| 2871 | |
| 2872 | // Materialize the value in the heap. |
| 2873 | return AllocateHeapNumber(value, pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 2877 | MaybeObject* Heap::AllocateForeign(Address address, PretenureFlag pretenure) { |
| 2878 | // Statically ensure that it is safe to allocate foreigns in paged spaces. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2879 | STATIC_ASSERT(Foreign::kSize <= Page::kMaxNonCodeHeapObjectSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2880 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2881 | Foreign* result; |
| 2882 | MaybeObject* maybe_result = Allocate(foreign_map(), space); |
| 2883 | if (!maybe_result->To(&result)) return maybe_result; |
| 2884 | result->set_foreign_address(address); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2885 | return result; |
| 2886 | } |
| 2887 | |
| 2888 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2889 | MaybeObject* Heap::AllocateSharedFunctionInfo(Object* name) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2890 | SharedFunctionInfo* share; |
| 2891 | MaybeObject* maybe = Allocate(shared_function_info_map(), OLD_POINTER_SPACE); |
| 2892 | if (!maybe->To<SharedFunctionInfo>(&share)) return maybe; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2893 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2894 | // Set pointer fields. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2895 | share->set_name(name); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2896 | Code* illegal = isolate_->builtins()->builtin(Builtins::kIllegal); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2897 | share->set_code(illegal); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2898 | share->set_scope_info(ScopeInfo::Empty()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2899 | Code* construct_stub = |
| 2900 | isolate_->builtins()->builtin(Builtins::kJSConstructStubGeneric); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2901 | share->set_construct_stub(construct_stub); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2902 | share->set_instance_class_name(Object_symbol()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2903 | share->set_function_data(undefined_value(), SKIP_WRITE_BARRIER); |
| 2904 | share->set_script(undefined_value(), SKIP_WRITE_BARRIER); |
| 2905 | share->set_debug_info(undefined_value(), SKIP_WRITE_BARRIER); |
| 2906 | share->set_inferred_name(empty_string(), SKIP_WRITE_BARRIER); |
| 2907 | share->set_initial_map(undefined_value(), SKIP_WRITE_BARRIER); |
| 2908 | share->set_this_property_assignments(undefined_value(), SKIP_WRITE_BARRIER); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2909 | share->set_ast_node_count(0); |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 2910 | share->set_deopt_counter(FLAG_deopt_every_n_times); |
| 2911 | share->set_ic_age(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2912 | |
| 2913 | // Set integer fields (smi or int, depending on the architecture). |
| 2914 | share->set_length(0); |
| 2915 | share->set_formal_parameter_count(0); |
| 2916 | share->set_expected_nof_properties(0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2917 | share->set_num_literals(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2918 | share->set_start_position_and_type(0); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2919 | share->set_end_position(0); |
| 2920 | share->set_function_token_position(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 2921 | // All compiler hints default to false or 0. |
| 2922 | share->set_compiler_hints(0); |
| 2923 | share->set_this_property_assignments_count(0); |
| 2924 | share->set_opt_count(0); |
| 2925 | |
| 2926 | return share; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2930 | MaybeObject* Heap::AllocateJSMessageObject(String* type, |
| 2931 | JSArray* arguments, |
| 2932 | int start_position, |
| 2933 | int end_position, |
| 2934 | Object* script, |
| 2935 | Object* stack_trace, |
| 2936 | Object* stack_frames) { |
| 2937 | Object* result; |
| 2938 | { MaybeObject* maybe_result = Allocate(message_object_map(), NEW_SPACE); |
| 2939 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2940 | } |
| 2941 | JSMessageObject* message = JSMessageObject::cast(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2942 | message->set_properties(Heap::empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 2943 | message->set_elements(Heap::empty_fixed_array(), SKIP_WRITE_BARRIER); |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 2944 | message->set_type(type); |
| 2945 | message->set_arguments(arguments); |
| 2946 | message->set_start_position(start_position); |
| 2947 | message->set_end_position(end_position); |
| 2948 | message->set_script(script); |
| 2949 | message->set_stack_trace(stack_trace); |
| 2950 | message->set_stack_frames(stack_frames); |
| 2951 | return result; |
| 2952 | } |
| 2953 | |
| 2954 | |
| 2955 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2956 | // Returns true for a character in a range. Both limits are inclusive. |
| 2957 | static inline bool Between(uint32_t character, uint32_t from, uint32_t to) { |
| 2958 | // This makes uses of the the unsigned wraparound. |
| 2959 | return character - from <= to - from; |
| 2960 | } |
| 2961 | |
| 2962 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2963 | MUST_USE_RESULT static inline MaybeObject* MakeOrFindTwoCharacterString( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2964 | Heap* heap, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2965 | uint32_t c1, |
| 2966 | uint32_t c2) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2967 | String* symbol; |
| 2968 | // Numeric strings have a different hash algorithm not known by |
| 2969 | // LookupTwoCharsSymbolIfExists, so we skip this step for such strings. |
| 2970 | if ((!Between(c1, '0', '9') || !Between(c2, '0', '9')) && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2971 | heap->symbol_table()->LookupTwoCharsSymbolIfExists(c1, c2, &symbol)) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2972 | return symbol; |
| 2973 | // Now we know the length is 2, we might as well make use of that fact |
| 2974 | // when building the new string. |
| 2975 | } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this |
| 2976 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2977 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2978 | { MaybeObject* maybe_result = heap->AllocateRawAsciiString(2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2979 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2980 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2981 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 2982 | dest[0] = c1; |
| 2983 | dest[1] = c2; |
| 2984 | return result; |
| 2985 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2986 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 2987 | { MaybeObject* maybe_result = heap->AllocateRawTwoByteString(2); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2988 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2989 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2990 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 2991 | dest[0] = c1; |
| 2992 | dest[1] = c2; |
| 2993 | return result; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 2998 | MaybeObject* Heap::AllocateConsString(String* first, String* second) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2999 | int first_length = first->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3000 | if (first_length == 0) { |
| 3001 | return second; |
| 3002 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3003 | |
| 3004 | int second_length = second->length(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3005 | if (second_length == 0) { |
| 3006 | return first; |
| 3007 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3008 | |
| 3009 | int length = first_length + second_length; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3010 | |
| 3011 | // Optimization for 2-byte strings often used as keys in a decompression |
| 3012 | // dictionary. Check whether we already have the string in the symbol |
| 3013 | // table to prevent creation of many unneccesary strings. |
| 3014 | if (length == 2) { |
| 3015 | unsigned c1 = first->Get(0); |
| 3016 | unsigned c2 = second->Get(0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3017 | return MakeOrFindTwoCharacterString(this, c1, c2); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3020 | bool first_is_ascii = first->IsAsciiRepresentation(); |
| 3021 | bool second_is_ascii = second->IsAsciiRepresentation(); |
| 3022 | bool is_ascii = first_is_ascii && second_is_ascii; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3023 | |
| 3024 | // 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] | 3025 | // of the new cons string is too large. |
| 3026 | if (length > String::kMaxLength || length < 0) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3027 | isolate()->context()->mark_out_of_memory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3028 | return Failure::OutOfMemoryException(); |
| 3029 | } |
| 3030 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3031 | bool is_ascii_data_in_two_byte_string = false; |
| 3032 | if (!is_ascii) { |
| 3033 | // At least one of the strings uses two-byte representation so we |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3034 | // can't use the fast case code for short ASCII strings below, but |
| 3035 | // we can try to save memory if all chars actually fit in ASCII. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3036 | is_ascii_data_in_two_byte_string = |
| 3037 | first->HasOnlyAsciiChars() && second->HasOnlyAsciiChars(); |
| 3038 | if (is_ascii_data_in_two_byte_string) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3039 | isolate_->counters()->string_add_runtime_ext_to_ascii()->Increment(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3040 | } |
| 3041 | } |
| 3042 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3043 | // If the resulting string is small make a flat string. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3044 | if (length < ConsString::kMinLength) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3045 | // Note that neither of the two inputs can be a slice because: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3046 | STATIC_ASSERT(ConsString::kMinLength <= SlicedString::kMinLength); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3047 | ASSERT(first->IsFlat()); |
| 3048 | ASSERT(second->IsFlat()); |
| 3049 | if (is_ascii) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3050 | Object* result; |
| 3051 | { MaybeObject* maybe_result = AllocateRawAsciiString(length); |
| 3052 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3053 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3054 | // Copy the characters into the new object. |
| 3055 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 3056 | // Copy first part. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3057 | const char* src; |
| 3058 | if (first->IsExternalString()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3059 | src = ExternalAsciiString::cast(first)->GetChars(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3060 | } else { |
| 3061 | src = SeqAsciiString::cast(first)->GetChars(); |
| 3062 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3063 | for (int i = 0; i < first_length; i++) *dest++ = src[i]; |
| 3064 | // Copy second part. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3065 | if (second->IsExternalString()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3066 | src = ExternalAsciiString::cast(second)->GetChars(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3067 | } else { |
| 3068 | src = SeqAsciiString::cast(second)->GetChars(); |
| 3069 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3070 | for (int i = 0; i < second_length; i++) *dest++ = src[i]; |
| 3071 | return result; |
| 3072 | } else { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3073 | if (is_ascii_data_in_two_byte_string) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3074 | Object* result; |
| 3075 | { MaybeObject* maybe_result = AllocateRawAsciiString(length); |
| 3076 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3077 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3078 | // Copy the characters into the new object. |
| 3079 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 3080 | String::WriteToFlat(first, dest, 0, first_length); |
| 3081 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3082 | isolate_->counters()->string_add_runtime_ext_to_ascii()->Increment(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3083 | return result; |
| 3084 | } |
| 3085 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3086 | Object* result; |
| 3087 | { MaybeObject* maybe_result = AllocateRawTwoByteString(length); |
| 3088 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3089 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3090 | // Copy the characters into the new object. |
| 3091 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 3092 | String::WriteToFlat(first, dest, 0, first_length); |
| 3093 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
| 3094 | return result; |
| 3095 | } |
| 3096 | } |
| 3097 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3098 | Map* map = (is_ascii || is_ascii_data_in_two_byte_string) ? |
| 3099 | cons_ascii_string_map() : cons_string_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3100 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3101 | Object* result; |
| 3102 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 3103 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3104 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3105 | |
| 3106 | AssertNoAllocation no_gc; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3107 | ConsString* cons_string = ConsString::cast(result); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 3108 | WriteBarrierMode mode = cons_string->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3109 | cons_string->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3110 | cons_string->set_hash_field(String::kEmptyHashField); |
| 3111 | cons_string->set_first(first, mode); |
| 3112 | cons_string->set_second(second, mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3113 | return result; |
| 3114 | } |
| 3115 | |
| 3116 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3117 | MaybeObject* Heap::AllocateSubString(String* buffer, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3118 | int start, |
| 3119 | int end, |
| 3120 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3121 | int length = end - start; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3122 | if (length <= 0) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 3123 | return empty_string(); |
| 3124 | } else if (length == 1) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3125 | return LookupSingleCharacterStringFromCode(buffer->Get(start)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3126 | } else if (length == 2) { |
| 3127 | // Optimization for 2-byte strings often used as keys in a decompression |
| 3128 | // dictionary. Check whether we already have the string in the symbol |
| 3129 | // table to prevent creation of many unneccesary strings. |
| 3130 | unsigned c1 = buffer->Get(start); |
| 3131 | unsigned c2 = buffer->Get(start + 1); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3132 | return MakeOrFindTwoCharacterString(this, c1, c2); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | // Make an attempt to flatten the buffer to reduce access time. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 3136 | buffer = buffer->TryFlattenGetString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3137 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3138 | if (!FLAG_string_slices || |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3139 | !buffer->IsFlat() || |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3140 | length < SlicedString::kMinLength || |
| 3141 | pretenure == TENURED) { |
| 3142 | Object* result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3143 | // WriteToFlat takes care of the case when an indirect string has a |
| 3144 | // different encoding from its underlying string. These encodings may |
| 3145 | // differ because of externalization. |
| 3146 | bool is_ascii = buffer->IsAsciiRepresentation(); |
| 3147 | { MaybeObject* maybe_result = is_ascii |
| 3148 | ? AllocateRawAsciiString(length, pretenure) |
| 3149 | : AllocateRawTwoByteString(length, pretenure); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3150 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3151 | } |
| 3152 | String* string_result = String::cast(result); |
| 3153 | // Copy the characters into the new object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3154 | if (is_ascii) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3155 | ASSERT(string_result->IsAsciiRepresentation()); |
| 3156 | char* dest = SeqAsciiString::cast(string_result)->GetChars(); |
| 3157 | String::WriteToFlat(buffer, dest, start, end); |
| 3158 | } else { |
| 3159 | ASSERT(string_result->IsTwoByteRepresentation()); |
| 3160 | uc16* dest = SeqTwoByteString::cast(string_result)->GetChars(); |
| 3161 | String::WriteToFlat(buffer, dest, start, end); |
| 3162 | } |
| 3163 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3164 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3165 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3166 | ASSERT(buffer->IsFlat()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3167 | #if DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3168 | if (FLAG_verify_heap) { |
| 3169 | buffer->StringVerify(); |
| 3170 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3171 | #endif |
| 3172 | |
| 3173 | Object* result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3174 | // When slicing an indirect string we use its encoding for a newly created |
| 3175 | // slice and don't check the encoding of the underlying string. This is safe |
| 3176 | // even if the encodings are different because of externalization. If an |
| 3177 | // indirect ASCII string is pointing to a two-byte string, the two-byte char |
| 3178 | // codes of the underlying string must still fit into ASCII (because |
| 3179 | // externalization must not change char codes). |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 3180 | { Map* map = buffer->IsAsciiRepresentation() |
| 3181 | ? sliced_ascii_string_map() |
| 3182 | : sliced_string_map(); |
| 3183 | MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 3184 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3185 | } |
| 3186 | |
| 3187 | AssertNoAllocation no_gc; |
| 3188 | SlicedString* sliced_string = SlicedString::cast(result); |
| 3189 | sliced_string->set_length(length); |
| 3190 | sliced_string->set_hash_field(String::kEmptyHashField); |
| 3191 | if (buffer->IsConsString()) { |
| 3192 | ConsString* cons = ConsString::cast(buffer); |
| 3193 | ASSERT(cons->second()->length() == 0); |
| 3194 | sliced_string->set_parent(cons->first()); |
| 3195 | sliced_string->set_offset(start); |
| 3196 | } else if (buffer->IsSlicedString()) { |
| 3197 | // Prevent nesting sliced strings. |
| 3198 | SlicedString* parent_slice = SlicedString::cast(buffer); |
| 3199 | sliced_string->set_parent(parent_slice->parent()); |
| 3200 | sliced_string->set_offset(start + parent_slice->offset()); |
| 3201 | } else { |
| 3202 | sliced_string->set_parent(buffer); |
| 3203 | sliced_string->set_offset(start); |
| 3204 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3205 | ASSERT(sliced_string->parent()->IsSeqString() || |
| 3206 | sliced_string->parent()->IsExternalString()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3207 | return result; |
| 3208 | } |
| 3209 | |
| 3210 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3211 | MaybeObject* Heap::AllocateExternalStringFromAscii( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3212 | const ExternalAsciiString::Resource* resource) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3213 | size_t length = resource->length(); |
| 3214 | if (length > static_cast<size_t>(String::kMaxLength)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3215 | isolate()->context()->mark_out_of_memory(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3216 | return Failure::OutOfMemoryException(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3219 | Map* map = external_ascii_string_map(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3220 | Object* result; |
| 3221 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 3222 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3223 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3224 | |
| 3225 | ExternalAsciiString* external_string = ExternalAsciiString::cast(result); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3226 | external_string->set_length(static_cast<int>(length)); |
| 3227 | external_string->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3228 | external_string->set_resource(resource); |
| 3229 | |
| 3230 | return result; |
| 3231 | } |
| 3232 | |
| 3233 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3234 | MaybeObject* Heap::AllocateExternalStringFromTwoByte( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3235 | const ExternalTwoByteString::Resource* resource) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3236 | size_t length = resource->length(); |
| 3237 | if (length > static_cast<size_t>(String::kMaxLength)) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3238 | isolate()->context()->mark_out_of_memory(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3239 | return Failure::OutOfMemoryException(); |
| 3240 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3241 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3242 | // For small strings we check whether the resource contains only |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 3243 | // ASCII characters. If yes, we use a different string map. |
| 3244 | static const size_t kAsciiCheckLengthLimit = 32; |
| 3245 | bool is_ascii = length <= kAsciiCheckLengthLimit && |
| 3246 | String::IsAscii(resource->data(), static_cast<int>(length)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3247 | Map* map = is_ascii ? |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3248 | external_string_with_ascii_data_map() : external_string_map(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3249 | Object* result; |
| 3250 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 3251 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3252 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3253 | |
| 3254 | ExternalTwoByteString* external_string = ExternalTwoByteString::cast(result); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3255 | external_string->set_length(static_cast<int>(length)); |
| 3256 | external_string->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3257 | external_string->set_resource(resource); |
| 3258 | |
| 3259 | return result; |
| 3260 | } |
| 3261 | |
| 3262 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3263 | MaybeObject* Heap::LookupSingleCharacterStringFromCode(uint16_t code) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3264 | if (code <= String::kMaxAsciiCharCode) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3265 | Object* value = single_character_string_cache()->get(code); |
| 3266 | if (value != undefined_value()) return value; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3267 | |
| 3268 | char buffer[1]; |
| 3269 | buffer[0] = static_cast<char>(code); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3270 | Object* result; |
| 3271 | MaybeObject* maybe_result = LookupSymbol(Vector<const char>(buffer, 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3272 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3273 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3274 | single_character_string_cache()->set(code, result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3275 | return result; |
| 3276 | } |
| 3277 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3278 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3279 | { MaybeObject* maybe_result = AllocateRawTwoByteString(1); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3280 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3281 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3282 | String* answer = String::cast(result); |
| 3283 | answer->Set(0, code); |
| 3284 | return answer; |
| 3285 | } |
| 3286 | |
| 3287 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3288 | MaybeObject* Heap::AllocateByteArray(int length, PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3289 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 3290 | return Failure::OutOfMemoryException(); |
| 3291 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3292 | if (pretenure == NOT_TENURED) { |
| 3293 | return AllocateByteArray(length); |
| 3294 | } |
| 3295 | int size = ByteArray::SizeFor(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3296 | Object* result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3297 | { MaybeObject* maybe_result = (size <= Page::kMaxNonCodeHeapObjectSize) |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3298 | ? old_data_space_->AllocateRaw(size) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3299 | : lo_space_->AllocateRaw(size, NOT_EXECUTABLE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3300 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3301 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3302 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3303 | reinterpret_cast<ByteArray*>(result)->set_map_no_write_barrier( |
| 3304 | byte_array_map()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3305 | reinterpret_cast<ByteArray*>(result)->set_length(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3306 | return result; |
| 3307 | } |
| 3308 | |
| 3309 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3310 | MaybeObject* Heap::AllocateByteArray(int length) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3311 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 3312 | return Failure::OutOfMemoryException(); |
| 3313 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3314 | int size = ByteArray::SizeFor(length); |
| 3315 | AllocationSpace space = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3316 | (size > Page::kMaxNonCodeHeapObjectSize) ? LO_SPACE : NEW_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3317 | Object* result; |
| 3318 | { MaybeObject* maybe_result = AllocateRaw(size, space, OLD_DATA_SPACE); |
| 3319 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3320 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3321 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3322 | reinterpret_cast<ByteArray*>(result)->set_map_no_write_barrier( |
| 3323 | byte_array_map()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3324 | reinterpret_cast<ByteArray*>(result)->set_length(length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3325 | return result; |
| 3326 | } |
| 3327 | |
| 3328 | |
| 3329 | void Heap::CreateFillerObjectAt(Address addr, int size) { |
| 3330 | if (size == 0) return; |
| 3331 | HeapObject* filler = HeapObject::FromAddress(addr); |
| 3332 | if (size == kPointerSize) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3333 | filler->set_map_no_write_barrier(one_pointer_filler_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3334 | } else if (size == 2 * kPointerSize) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3335 | filler->set_map_no_write_barrier(two_pointer_filler_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3336 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3337 | filler->set_map_no_write_barrier(free_space_map()); |
| 3338 | FreeSpace::cast(filler)->set_size(size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
| 3342 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3343 | MaybeObject* Heap::AllocateExternalArray(int length, |
| 3344 | ExternalArrayType array_type, |
| 3345 | void* external_pointer, |
| 3346 | PretenureFlag pretenure) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3347 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3348 | Object* result; |
| 3349 | { MaybeObject* maybe_result = AllocateRaw(ExternalArray::kAlignedSize, |
| 3350 | space, |
| 3351 | OLD_DATA_SPACE); |
| 3352 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3353 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3354 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3355 | reinterpret_cast<ExternalArray*>(result)->set_map_no_write_barrier( |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3356 | MapForExternalArrayType(array_type)); |
| 3357 | reinterpret_cast<ExternalArray*>(result)->set_length(length); |
| 3358 | reinterpret_cast<ExternalArray*>(result)->set_external_pointer( |
| 3359 | external_pointer); |
| 3360 | |
| 3361 | return result; |
| 3362 | } |
| 3363 | |
| 3364 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3365 | MaybeObject* Heap::CreateCode(const CodeDesc& desc, |
| 3366 | Code::Flags flags, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3367 | Handle<Object> self_reference, |
| 3368 | bool immovable) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3369 | // Allocate ByteArray before the Code object, so that we do not risk |
| 3370 | // leaving uninitialized Code object (and breaking the heap). |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3371 | ByteArray* reloc_info; |
| 3372 | MaybeObject* maybe_reloc_info = AllocateByteArray(desc.reloc_size, TENURED); |
| 3373 | if (!maybe_reloc_info->To(&reloc_info)) return maybe_reloc_info; |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3374 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3375 | // Compute size. |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3376 | int body_size = RoundUp(desc.instr_size, kObjectAlignment); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 3377 | int obj_size = Code::SizeFor(body_size); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3378 | ASSERT(IsAligned(static_cast<intptr_t>(obj_size), kCodeAlignment)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3379 | MaybeObject* maybe_result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3380 | // Large code objects and code objects which should stay at a fixed address |
| 3381 | // are allocated in large object space. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3382 | if (obj_size > code_space()->AreaSize() || immovable) { |
| 3383 | maybe_result = lo_space_->AllocateRaw(obj_size, EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3384 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3385 | maybe_result = code_space_->AllocateRaw(obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3388 | Object* result; |
| 3389 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3390 | |
| 3391 | // Initialize the object |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3392 | HeapObject::cast(result)->set_map_no_write_barrier(code_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3393 | Code* code = Code::cast(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3394 | ASSERT(!isolate_->code_range()->exists() || |
| 3395 | isolate_->code_range()->contains(code->address())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3396 | code->set_instruction_size(desc.instr_size); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3397 | code->set_relocation_info(reloc_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3398 | code->set_flags(flags); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 3399 | if (code->is_call_stub() || code->is_keyed_call_stub()) { |
| 3400 | code->set_check_type(RECEIVER_MAP_CHECK); |
| 3401 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3402 | code->set_deoptimization_data(empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 3403 | code->set_type_feedback_info(undefined_value(), SKIP_WRITE_BARRIER); |
| 3404 | code->set_handler_table(empty_fixed_array(), SKIP_WRITE_BARRIER); |
| 3405 | code->set_gc_metadata(Smi::FromInt(0)); |
| 3406 | code->set_ic_age(global_ic_age_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3407 | // Allow self references to created code object by patching the handle to |
| 3408 | // point to the newly allocated Code object. |
| 3409 | if (!self_reference.is_null()) { |
| 3410 | *(self_reference.location()) = code; |
| 3411 | } |
| 3412 | // Migrate generated code. |
| 3413 | // The generated code can contain Object** values (typically from handles) |
| 3414 | // that are dereferenced during the copy to point directly to the actual heap |
| 3415 | // objects. These pointers can include references to the code object itself, |
| 3416 | // through the self_reference parameter. |
| 3417 | code->CopyFrom(desc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3418 | |
| 3419 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3420 | if (FLAG_verify_heap) { |
| 3421 | code->Verify(); |
| 3422 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3423 | #endif |
| 3424 | return code; |
| 3425 | } |
| 3426 | |
| 3427 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3428 | MaybeObject* Heap::CopyCode(Code* code) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3429 | // Allocate an object the same size as the code object. |
| 3430 | int obj_size = code->Size(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3431 | MaybeObject* maybe_result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3432 | if (obj_size > code_space()->AreaSize()) { |
| 3433 | maybe_result = lo_space_->AllocateRaw(obj_size, EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3434 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3435 | maybe_result = code_space_->AllocateRaw(obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3438 | Object* result; |
| 3439 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3440 | |
| 3441 | // Copy code object. |
| 3442 | Address old_addr = code->address(); |
| 3443 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3444 | CopyBlock(new_addr, old_addr, obj_size); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3445 | // Relocate the copy. |
| 3446 | Code* new_code = Code::cast(result); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3447 | ASSERT(!isolate_->code_range()->exists() || |
| 3448 | isolate_->code_range()->contains(code->address())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3449 | new_code->Relocate(new_addr - old_addr); |
| 3450 | return new_code; |
| 3451 | } |
| 3452 | |
| 3453 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3454 | MaybeObject* Heap::CopyCode(Code* code, Vector<byte> reloc_info) { |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3455 | // Allocate ByteArray before the Code object, so that we do not risk |
| 3456 | // leaving uninitialized Code object (and breaking the heap). |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3457 | Object* reloc_info_array; |
| 3458 | { MaybeObject* maybe_reloc_info_array = |
| 3459 | AllocateByteArray(reloc_info.length(), TENURED); |
| 3460 | if (!maybe_reloc_info_array->ToObject(&reloc_info_array)) { |
| 3461 | return maybe_reloc_info_array; |
| 3462 | } |
| 3463 | } |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3464 | |
| 3465 | int new_body_size = RoundUp(code->instruction_size(), kObjectAlignment); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3466 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 3467 | int new_obj_size = Code::SizeFor(new_body_size); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3468 | |
| 3469 | Address old_addr = code->address(); |
| 3470 | |
| 3471 | size_t relocation_offset = |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3472 | static_cast<size_t>(code->instruction_end() - old_addr); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3473 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3474 | MaybeObject* maybe_result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3475 | if (new_obj_size > code_space()->AreaSize()) { |
| 3476 | maybe_result = lo_space_->AllocateRaw(new_obj_size, EXECUTABLE); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3477 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3478 | maybe_result = code_space_->AllocateRaw(new_obj_size); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3479 | } |
| 3480 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3481 | Object* result; |
| 3482 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3483 | |
| 3484 | // Copy code object. |
| 3485 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
| 3486 | |
| 3487 | // Copy header and instructions. |
| 3488 | memcpy(new_addr, old_addr, relocation_offset); |
| 3489 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3490 | Code* new_code = Code::cast(result); |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3491 | new_code->set_relocation_info(ByteArray::cast(reloc_info_array)); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3492 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3493 | // Copy patched rinfo. |
| 3494 | memcpy(new_code->relocation_start(), reloc_info.start(), reloc_info.length()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3495 | |
| 3496 | // Relocate the copy. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3497 | ASSERT(!isolate_->code_range()->exists() || |
| 3498 | isolate_->code_range()->contains(code->address())); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3499 | new_code->Relocate(new_addr - old_addr); |
| 3500 | |
| 3501 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3502 | if (FLAG_verify_heap) { |
| 3503 | code->Verify(); |
| 3504 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3505 | #endif |
| 3506 | return new_code; |
| 3507 | } |
| 3508 | |
| 3509 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3510 | MaybeObject* Heap::Allocate(Map* map, AllocationSpace space) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3511 | ASSERT(gc_state_ == NOT_IN_GC); |
| 3512 | ASSERT(map->instance_type() != MAP_TYPE); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3513 | // If allocation failures are disallowed, we may allocate in a different |
| 3514 | // space when new space is full and the object is not a large object. |
| 3515 | AllocationSpace retry_space = |
| 3516 | (space != NEW_SPACE) ? space : TargetSpaceId(map->instance_type()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3517 | Object* result; |
| 3518 | { MaybeObject* maybe_result = |
| 3519 | AllocateRaw(map->instance_size(), space, retry_space); |
| 3520 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3521 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3522 | // No need for write barrier since object is white and map is in old space. |
| 3523 | HeapObject::cast(result)->set_map_no_write_barrier(map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3524 | return result; |
| 3525 | } |
| 3526 | |
| 3527 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3528 | void Heap::InitializeFunction(JSFunction* function, |
| 3529 | SharedFunctionInfo* shared, |
| 3530 | Object* prototype) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3531 | ASSERT(!prototype->IsMap()); |
| 3532 | function->initialize_properties(); |
| 3533 | function->initialize_elements(); |
| 3534 | function->set_shared(shared); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3535 | function->set_code(shared->code()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3536 | function->set_prototype_or_initial_map(prototype); |
| 3537 | function->set_context(undefined_value()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3538 | function->set_literals_or_bindings(empty_fixed_array()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 3539 | function->set_next_function_link(undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3543 | MaybeObject* Heap::AllocateFunctionPrototype(JSFunction* function) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3544 | // Allocate the prototype. Make sure to use the object function |
| 3545 | // from the function's context, since the function can be from a |
| 3546 | // different context. |
| 3547 | JSFunction* object_function = |
| 3548 | function->context()->global_context()->object_function(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3549 | |
| 3550 | // Each function prototype gets a copy of the object function map. |
| 3551 | // This avoid unwanted sharing of maps between prototypes of different |
| 3552 | // constructors. |
| 3553 | Map* new_map; |
| 3554 | ASSERT(object_function->has_initial_map()); |
| 3555 | { MaybeObject* maybe_map = |
| 3556 | object_function->initial_map()->CopyDropTransitions(); |
| 3557 | if (!maybe_map->To<Map>(&new_map)) return maybe_map; |
| 3558 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3559 | Object* prototype; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3560 | { MaybeObject* maybe_prototype = AllocateJSObjectFromMap(new_map); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3561 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 3562 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3563 | // When creating the prototype for the function we must set its |
| 3564 | // constructor to the function. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3565 | Object* result; |
| 3566 | { MaybeObject* maybe_result = |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 3567 | JSObject::cast(prototype)->SetLocalPropertyIgnoreAttributes( |
| 3568 | constructor_symbol(), function, DONT_ENUM); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3569 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3570 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3571 | return prototype; |
| 3572 | } |
| 3573 | |
| 3574 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3575 | MaybeObject* Heap::AllocateFunction(Map* function_map, |
| 3576 | SharedFunctionInfo* shared, |
| 3577 | Object* prototype, |
| 3578 | PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3579 | AllocationSpace space = |
| 3580 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3581 | Object* result; |
| 3582 | { MaybeObject* maybe_result = Allocate(function_map, space); |
| 3583 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3584 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3585 | InitializeFunction(JSFunction::cast(result), shared, prototype); |
| 3586 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3590 | MaybeObject* Heap::AllocateArgumentsObject(Object* callee, int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3591 | // To get fast allocation and map sharing for arguments objects we |
| 3592 | // allocate them based on an arguments boilerplate. |
| 3593 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3594 | JSObject* boilerplate; |
| 3595 | int arguments_object_size; |
| 3596 | bool strict_mode_callee = callee->IsJSFunction() && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3597 | !JSFunction::cast(callee)->shared()->is_classic_mode(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3598 | if (strict_mode_callee) { |
| 3599 | boilerplate = |
| 3600 | isolate()->context()->global_context()-> |
| 3601 | strict_mode_arguments_boilerplate(); |
| 3602 | arguments_object_size = kArgumentsObjectSizeStrict; |
| 3603 | } else { |
| 3604 | boilerplate = |
| 3605 | isolate()->context()->global_context()->arguments_boilerplate(); |
| 3606 | arguments_object_size = kArgumentsObjectSize; |
| 3607 | } |
| 3608 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3609 | // This calls Copy directly rather than using Heap::AllocateRaw so we |
| 3610 | // duplicate the check here. |
| 3611 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 3612 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3613 | // Check that the size of the boilerplate matches our |
| 3614 | // expectations. The ArgumentsAccessStub::GenerateNewObject relies |
| 3615 | // on the size being a known constant. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3616 | ASSERT(arguments_object_size == boilerplate->map()->instance_size()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3617 | |
| 3618 | // Do the allocation. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3619 | Object* result; |
| 3620 | { MaybeObject* maybe_result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3621 | AllocateRaw(arguments_object_size, NEW_SPACE, OLD_POINTER_SPACE); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3622 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3623 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3624 | |
| 3625 | // Copy the content. The arguments boilerplate doesn't have any |
| 3626 | // fields that point to new space so it's safe to skip the write |
| 3627 | // barrier here. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3628 | CopyBlock(HeapObject::cast(result)->address(), |
| 3629 | boilerplate->address(), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3630 | JSObject::kHeaderSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3631 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3632 | // Set the length property. |
| 3633 | JSObject::cast(result)->InObjectPropertyAtPut(kArgumentsLengthIndex, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3634 | Smi::FromInt(length), |
| 3635 | SKIP_WRITE_BARRIER); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3636 | // Set the callee property for non-strict mode arguments object only. |
| 3637 | if (!strict_mode_callee) { |
| 3638 | JSObject::cast(result)->InObjectPropertyAtPut(kArgumentsCalleeIndex, |
| 3639 | callee); |
| 3640 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3641 | |
| 3642 | // Check the state of the object |
| 3643 | ASSERT(JSObject::cast(result)->HasFastProperties()); |
| 3644 | ASSERT(JSObject::cast(result)->HasFastElements()); |
| 3645 | |
| 3646 | return result; |
| 3647 | } |
| 3648 | |
| 3649 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3650 | static bool HasDuplicates(DescriptorArray* descriptors) { |
| 3651 | int count = descriptors->number_of_descriptors(); |
| 3652 | if (count > 1) { |
| 3653 | String* prev_key = descriptors->GetKey(0); |
| 3654 | for (int i = 1; i != count; i++) { |
| 3655 | String* current_key = descriptors->GetKey(i); |
| 3656 | if (prev_key == current_key) return true; |
| 3657 | prev_key = current_key; |
| 3658 | } |
| 3659 | } |
| 3660 | return false; |
| 3661 | } |
| 3662 | |
| 3663 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3664 | MaybeObject* Heap::AllocateInitialMap(JSFunction* fun) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3665 | ASSERT(!fun->has_initial_map()); |
| 3666 | |
| 3667 | // First create a new map with the size and number of in-object properties |
| 3668 | // suggested by the function. |
| 3669 | int instance_size = fun->shared()->CalculateInstanceSize(); |
| 3670 | int in_object_properties = fun->shared()->CalculateInObjectProperties(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3671 | Object* map_obj; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3672 | { MaybeObject* maybe_map_obj = AllocateMap(JS_OBJECT_TYPE, instance_size); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3673 | if (!maybe_map_obj->ToObject(&map_obj)) return maybe_map_obj; |
| 3674 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3675 | |
| 3676 | // Fetch or allocate prototype. |
| 3677 | Object* prototype; |
| 3678 | if (fun->has_instance_prototype()) { |
| 3679 | prototype = fun->instance_prototype(); |
| 3680 | } else { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3681 | { MaybeObject* maybe_prototype = AllocateFunctionPrototype(fun); |
| 3682 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 3683 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3684 | } |
| 3685 | Map* map = Map::cast(map_obj); |
| 3686 | map->set_inobject_properties(in_object_properties); |
| 3687 | map->set_unused_property_fields(in_object_properties); |
| 3688 | map->set_prototype(prototype); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3689 | ASSERT(map->has_fast_elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3690 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3691 | // If the function has only simple this property assignments add |
| 3692 | // field descriptors for these to the initial map as the object |
| 3693 | // cannot be constructed without having these properties. Guard by |
| 3694 | // the inline_new flag so we only change the map if we generate a |
| 3695 | // specialized construct stub. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3696 | ASSERT(in_object_properties <= Map::kMaxPreAllocatedPropertyFields); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3697 | if (fun->shared()->CanGenerateInlineConstructor(prototype)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3698 | int count = fun->shared()->this_property_assignments_count(); |
| 3699 | if (count > in_object_properties) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3700 | // Inline constructor can only handle inobject properties. |
| 3701 | fun->shared()->ForbidInlineConstructor(); |
| 3702 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3703 | DescriptorArray* descriptors; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3704 | { MaybeObject* maybe_descriptors_obj = DescriptorArray::Allocate(count); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3705 | if (!maybe_descriptors_obj->To<DescriptorArray>(&descriptors)) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3706 | return maybe_descriptors_obj; |
| 3707 | } |
| 3708 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3709 | DescriptorArray::WhitenessWitness witness(descriptors); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3710 | for (int i = 0; i < count; i++) { |
| 3711 | String* name = fun->shared()->GetThisPropertyAssignmentName(i); |
| 3712 | ASSERT(name->IsSymbol()); |
| 3713 | FieldDescriptor field(name, i, NONE); |
| 3714 | field.SetEnumerationIndex(i); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3715 | descriptors->Set(i, &field, witness); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3716 | } |
| 3717 | descriptors->SetNextEnumerationIndex(count); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3718 | descriptors->SortUnchecked(witness); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3719 | |
| 3720 | // The descriptors may contain duplicates because the compiler does not |
| 3721 | // guarantee the uniqueness of property names (it would have required |
| 3722 | // quadratic time). Once the descriptors are sorted we can check for |
| 3723 | // duplicates in linear time. |
| 3724 | if (HasDuplicates(descriptors)) { |
| 3725 | fun->shared()->ForbidInlineConstructor(); |
| 3726 | } else { |
| 3727 | map->set_instance_descriptors(descriptors); |
| 3728 | map->set_pre_allocated_property_fields(count); |
| 3729 | map->set_unused_property_fields(in_object_properties - count); |
| 3730 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3731 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3732 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3733 | |
| 3734 | fun->shared()->StartInobjectSlackTracking(map); |
| 3735 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3736 | return map; |
| 3737 | } |
| 3738 | |
| 3739 | |
| 3740 | void Heap::InitializeJSObjectFromMap(JSObject* obj, |
| 3741 | FixedArray* properties, |
| 3742 | Map* map) { |
| 3743 | obj->set_properties(properties); |
| 3744 | obj->initialize_elements(); |
| 3745 | // TODO(1240798): Initialize the object's body using valid initial values |
| 3746 | // according to the object's initial map. For example, if the map's |
| 3747 | // instance type is JS_ARRAY_TYPE, the length field should be initialized |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3748 | // to a number (e.g. Smi::FromInt(0)) and the elements initialized to a |
| 3749 | // fixed array (e.g. Heap::empty_fixed_array()). Currently, the object |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3750 | // verification code has to cope with (temporarily) invalid objects. See |
| 3751 | // for example, JSArray::JSArrayVerify). |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3752 | Object* filler; |
| 3753 | // We cannot always fill with one_pointer_filler_map because objects |
| 3754 | // created from API functions expect their internal fields to be initialized |
| 3755 | // with undefined_value. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3756 | // Pre-allocated fields need to be initialized with undefined_value as well |
| 3757 | // so that object accesses before the constructor completes (e.g. in the |
| 3758 | // debugger) will not cause a crash. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 3759 | if (map->constructor()->IsJSFunction() && |
| 3760 | JSFunction::cast(map->constructor())->shared()-> |
| 3761 | IsInobjectSlackTrackingInProgress()) { |
| 3762 | // We might want to shrink the object later. |
| 3763 | ASSERT(obj->GetInternalFieldCount() == 0); |
| 3764 | filler = Heap::one_pointer_filler_map(); |
| 3765 | } else { |
| 3766 | filler = Heap::undefined_value(); |
| 3767 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3768 | obj->InitializeBody(map, Heap::undefined_value(), filler); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3772 | MaybeObject* Heap::AllocateJSObjectFromMap(Map* map, PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3773 | // JSFunctions should be allocated using AllocateFunction to be |
| 3774 | // properly initialized. |
| 3775 | ASSERT(map->instance_type() != JS_FUNCTION_TYPE); |
| 3776 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3777 | // Both types of global objects should be allocated using |
| 3778 | // AllocateGlobalObject to be properly initialized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3779 | ASSERT(map->instance_type() != JS_GLOBAL_OBJECT_TYPE); |
| 3780 | ASSERT(map->instance_type() != JS_BUILTINS_OBJECT_TYPE); |
| 3781 | |
| 3782 | // Allocate the backing storage for the properties. |
| 3783 | int prop_size = |
| 3784 | map->pre_allocated_property_fields() + |
| 3785 | map->unused_property_fields() - |
| 3786 | map->inobject_properties(); |
| 3787 | ASSERT(prop_size >= 0); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3788 | Object* properties; |
| 3789 | { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, pretenure); |
| 3790 | if (!maybe_properties->ToObject(&properties)) return maybe_properties; |
| 3791 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3792 | |
| 3793 | // Allocate the JSObject. |
| 3794 | AllocationSpace space = |
| 3795 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3796 | if (map->instance_size() > Page::kMaxNonCodeHeapObjectSize) space = LO_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3797 | Object* obj; |
| 3798 | { MaybeObject* maybe_obj = Allocate(map, space); |
| 3799 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3800 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3801 | |
| 3802 | // Initialize the JSObject. |
| 3803 | InitializeJSObjectFromMap(JSObject::cast(obj), |
| 3804 | FixedArray::cast(properties), |
| 3805 | map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3806 | ASSERT(JSObject::cast(obj)->HasFastSmiOnlyElements() || |
| 3807 | JSObject::cast(obj)->HasFastElements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3808 | return obj; |
| 3809 | } |
| 3810 | |
| 3811 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3812 | MaybeObject* Heap::AllocateJSObject(JSFunction* constructor, |
| 3813 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3814 | // Allocate the initial map if absent. |
| 3815 | if (!constructor->has_initial_map()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3816 | Object* initial_map; |
| 3817 | { MaybeObject* maybe_initial_map = AllocateInitialMap(constructor); |
| 3818 | if (!maybe_initial_map->ToObject(&initial_map)) return maybe_initial_map; |
| 3819 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3820 | constructor->set_initial_map(Map::cast(initial_map)); |
| 3821 | Map::cast(initial_map)->set_constructor(constructor); |
| 3822 | } |
| 3823 | // Allocate the object based on the constructors initial map. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3824 | MaybeObject* result = AllocateJSObjectFromMap( |
| 3825 | constructor->initial_map(), pretenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3826 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3827 | // Make sure result is NOT a global object if valid. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3828 | Object* non_failure; |
| 3829 | ASSERT(!result->ToObject(&non_failure) || !non_failure->IsGlobalObject()); |
| 3830 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3831 | return result; |
| 3832 | } |
| 3833 | |
| 3834 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3835 | MaybeObject* Heap::AllocateJSArrayAndStorage( |
| 3836 | ElementsKind elements_kind, |
| 3837 | int length, |
| 3838 | int capacity, |
| 3839 | ArrayStorageAllocationMode mode, |
| 3840 | PretenureFlag pretenure) { |
| 3841 | ASSERT(capacity >= length); |
| 3842 | MaybeObject* maybe_array = AllocateJSArray(elements_kind, pretenure); |
| 3843 | JSArray* array; |
| 3844 | if (!maybe_array->To(&array)) return maybe_array; |
| 3845 | |
| 3846 | if (capacity == 0) { |
| 3847 | array->set_length(Smi::FromInt(0)); |
| 3848 | array->set_elements(empty_fixed_array()); |
| 3849 | return array; |
| 3850 | } |
| 3851 | |
| 3852 | FixedArrayBase* elms; |
| 3853 | MaybeObject* maybe_elms = NULL; |
| 3854 | if (elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 3855 | if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) { |
| 3856 | maybe_elms = AllocateUninitializedFixedDoubleArray(capacity); |
| 3857 | } else { |
| 3858 | ASSERT(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
| 3859 | maybe_elms = AllocateFixedDoubleArrayWithHoles(capacity); |
| 3860 | } |
| 3861 | } else { |
| 3862 | ASSERT(elements_kind == FAST_ELEMENTS || |
| 3863 | elements_kind == FAST_SMI_ONLY_ELEMENTS); |
| 3864 | if (mode == DONT_INITIALIZE_ARRAY_ELEMENTS) { |
| 3865 | maybe_elms = AllocateUninitializedFixedArray(capacity); |
| 3866 | } else { |
| 3867 | ASSERT(mode == INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); |
| 3868 | maybe_elms = AllocateFixedArrayWithHoles(capacity); |
| 3869 | } |
| 3870 | } |
| 3871 | if (!maybe_elms->To(&elms)) return maybe_elms; |
| 3872 | |
| 3873 | array->set_elements(elms); |
| 3874 | array->set_length(Smi::FromInt(length)); |
| 3875 | return array; |
| 3876 | } |
| 3877 | |
| 3878 | |
| 3879 | MaybeObject* Heap::AllocateJSArrayWithElements( |
| 3880 | FixedArrayBase* elements, |
| 3881 | ElementsKind elements_kind, |
| 3882 | PretenureFlag pretenure) { |
| 3883 | MaybeObject* maybe_array = AllocateJSArray(elements_kind, pretenure); |
| 3884 | JSArray* array; |
| 3885 | if (!maybe_array->To(&array)) return maybe_array; |
| 3886 | |
| 3887 | array->set_elements(elements); |
| 3888 | array->set_length(Smi::FromInt(elements->length())); |
| 3889 | return array; |
| 3890 | } |
| 3891 | |
| 3892 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3893 | MaybeObject* Heap::AllocateJSProxy(Object* handler, Object* prototype) { |
| 3894 | // Allocate map. |
| 3895 | // TODO(rossberg): Once we optimize proxies, think about a scheme to share |
| 3896 | // maps. Will probably depend on the identity of the handler object, too. |
| 3897 | Map* map; |
| 3898 | MaybeObject* maybe_map_obj = AllocateMap(JS_PROXY_TYPE, JSProxy::kSize); |
| 3899 | if (!maybe_map_obj->To<Map>(&map)) return maybe_map_obj; |
| 3900 | map->set_prototype(prototype); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3901 | |
| 3902 | // Allocate the proxy object. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3903 | JSProxy* result; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3904 | MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3905 | if (!maybe_result->To<JSProxy>(&result)) return maybe_result; |
| 3906 | result->InitializeBody(map->instance_size(), Smi::FromInt(0)); |
| 3907 | result->set_handler(handler); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3908 | result->set_hash(undefined_value(), SKIP_WRITE_BARRIER); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3909 | return result; |
| 3910 | } |
| 3911 | |
| 3912 | |
| 3913 | MaybeObject* Heap::AllocateJSFunctionProxy(Object* handler, |
| 3914 | Object* call_trap, |
| 3915 | Object* construct_trap, |
| 3916 | Object* prototype) { |
| 3917 | // Allocate map. |
| 3918 | // TODO(rossberg): Once we optimize proxies, think about a scheme to share |
| 3919 | // maps. Will probably depend on the identity of the handler object, too. |
| 3920 | Map* map; |
| 3921 | MaybeObject* maybe_map_obj = |
| 3922 | AllocateMap(JS_FUNCTION_PROXY_TYPE, JSFunctionProxy::kSize); |
| 3923 | if (!maybe_map_obj->To<Map>(&map)) return maybe_map_obj; |
| 3924 | map->set_prototype(prototype); |
| 3925 | |
| 3926 | // Allocate the proxy object. |
| 3927 | JSFunctionProxy* result; |
| 3928 | MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 3929 | if (!maybe_result->To<JSFunctionProxy>(&result)) return maybe_result; |
| 3930 | result->InitializeBody(map->instance_size(), Smi::FromInt(0)); |
| 3931 | result->set_handler(handler); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 3932 | result->set_hash(undefined_value(), SKIP_WRITE_BARRIER); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 3933 | result->set_call_trap(call_trap); |
| 3934 | result->set_construct_trap(construct_trap); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 3935 | return result; |
| 3936 | } |
| 3937 | |
| 3938 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3939 | MaybeObject* Heap::AllocateGlobalObject(JSFunction* constructor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3940 | ASSERT(constructor->has_initial_map()); |
| 3941 | Map* map = constructor->initial_map(); |
| 3942 | |
| 3943 | // Make sure no field properties are described in the initial map. |
| 3944 | // This guarantees us that normalizing the properties does not |
| 3945 | // require us to change property values to JSGlobalPropertyCells. |
| 3946 | ASSERT(map->NextFreePropertyIndex() == 0); |
| 3947 | |
| 3948 | // Make sure we don't have a ton of pre-allocated slots in the |
| 3949 | // global objects. They will be unused once we normalize the object. |
| 3950 | ASSERT(map->unused_property_fields() == 0); |
| 3951 | ASSERT(map->inobject_properties() == 0); |
| 3952 | |
| 3953 | // Initial size of the backing store to avoid resize of the storage during |
| 3954 | // bootstrapping. The size differs between the JS global object ad the |
| 3955 | // builtins object. |
| 3956 | int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512; |
| 3957 | |
| 3958 | // Allocate a dictionary object for backing storage. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3959 | Object* obj; |
| 3960 | { MaybeObject* maybe_obj = |
| 3961 | StringDictionary::Allocate( |
| 3962 | map->NumberOfDescribedProperties() * 2 + initial_size); |
| 3963 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3964 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3965 | StringDictionary* dictionary = StringDictionary::cast(obj); |
| 3966 | |
| 3967 | // The global object might be created from an object template with accessors. |
| 3968 | // Fill these accessors into the dictionary. |
| 3969 | DescriptorArray* descs = map->instance_descriptors(); |
| 3970 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 3971 | PropertyDetails details(descs->GetDetails(i)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3972 | ASSERT(details.type() == CALLBACKS); // Only accessors are expected. |
| 3973 | PropertyDetails d = |
| 3974 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 3975 | Object* value = descs->GetCallbacksObject(i); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 3976 | { MaybeObject* maybe_value = AllocateJSGlobalPropertyCell(value); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3977 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 3978 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3979 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3980 | Object* result; |
| 3981 | { MaybeObject* maybe_result = dictionary->Add(descs->GetKey(i), value, d); |
| 3982 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3983 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3984 | dictionary = StringDictionary::cast(result); |
| 3985 | } |
| 3986 | |
| 3987 | // Allocate the global object and initialize it with the backing store. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3988 | { MaybeObject* maybe_obj = Allocate(map, OLD_POINTER_SPACE); |
| 3989 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3990 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3991 | JSObject* global = JSObject::cast(obj); |
| 3992 | InitializeJSObjectFromMap(global, dictionary, map); |
| 3993 | |
| 3994 | // Create a new map for the global object. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 3995 | { MaybeObject* maybe_obj = map->CopyDropDescriptors(); |
| 3996 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3997 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3998 | Map* new_map = Map::cast(obj); |
| 3999 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4000 | // Set up the global object as a normalized object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4001 | global->set_map(new_map); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 4002 | global->map()->clear_instance_descriptors(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4003 | global->set_properties(dictionary); |
| 4004 | |
| 4005 | // Make sure result is a global object with properties in dictionary. |
| 4006 | ASSERT(global->IsGlobalObject()); |
| 4007 | ASSERT(!global->HasFastProperties()); |
| 4008 | return global; |
| 4009 | } |
| 4010 | |
| 4011 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4012 | MaybeObject* Heap::CopyJSObject(JSObject* source) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4013 | // Never used to copy functions. If functions need to be copied we |
| 4014 | // have to be careful to clear the literals array. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4015 | SLOW_ASSERT(!source->IsJSFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4016 | |
| 4017 | // Make the clone. |
| 4018 | Map* map = source->map(); |
| 4019 | int object_size = map->instance_size(); |
| 4020 | Object* clone; |
| 4021 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4022 | WriteBarrierMode wb_mode = UPDATE_WRITE_BARRIER; |
| 4023 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4024 | // If we're forced to always allocate, we use the general allocation |
| 4025 | // functions which may leave us with an object in old space. |
| 4026 | if (always_allocate()) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4027 | { MaybeObject* maybe_clone = |
| 4028 | AllocateRaw(object_size, NEW_SPACE, OLD_POINTER_SPACE); |
| 4029 | if (!maybe_clone->ToObject(&clone)) return maybe_clone; |
| 4030 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4031 | Address clone_address = HeapObject::cast(clone)->address(); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4032 | CopyBlock(clone_address, |
| 4033 | source->address(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4034 | object_size); |
| 4035 | // Update write barrier for all fields that lie beyond the header. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4036 | RecordWrites(clone_address, |
| 4037 | JSObject::kHeaderSize, |
| 4038 | (object_size - JSObject::kHeaderSize) / kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4039 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4040 | wb_mode = SKIP_WRITE_BARRIER; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4041 | { MaybeObject* maybe_clone = new_space_.AllocateRaw(object_size); |
| 4042 | if (!maybe_clone->ToObject(&clone)) return maybe_clone; |
| 4043 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4044 | SLOW_ASSERT(InNewSpace(clone)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4045 | // Since we know the clone is allocated in new space, we can copy |
| 4046 | // the contents without worrying about updating the write barrier. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4047 | CopyBlock(HeapObject::cast(clone)->address(), |
| 4048 | source->address(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4049 | object_size); |
| 4050 | } |
| 4051 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4052 | SLOW_ASSERT( |
| 4053 | JSObject::cast(clone)->GetElementsKind() == source->GetElementsKind()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4054 | FixedArrayBase* elements = FixedArrayBase::cast(source->elements()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4055 | FixedArray* properties = FixedArray::cast(source->properties()); |
| 4056 | // Update elements if necessary. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4057 | if (elements->length() > 0) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4058 | Object* elem; |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4059 | { MaybeObject* maybe_elem; |
| 4060 | if (elements->map() == fixed_cow_array_map()) { |
| 4061 | maybe_elem = FixedArray::cast(elements); |
| 4062 | } else if (source->HasFastDoubleElements()) { |
| 4063 | maybe_elem = CopyFixedDoubleArray(FixedDoubleArray::cast(elements)); |
| 4064 | } else { |
| 4065 | maybe_elem = CopyFixedArray(FixedArray::cast(elements)); |
| 4066 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4067 | if (!maybe_elem->ToObject(&elem)) return maybe_elem; |
| 4068 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4069 | JSObject::cast(clone)->set_elements(FixedArrayBase::cast(elem), wb_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4070 | } |
| 4071 | // Update properties if necessary. |
| 4072 | if (properties->length() > 0) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4073 | Object* prop; |
| 4074 | { MaybeObject* maybe_prop = CopyFixedArray(properties); |
| 4075 | if (!maybe_prop->ToObject(&prop)) return maybe_prop; |
| 4076 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4077 | JSObject::cast(clone)->set_properties(FixedArray::cast(prop), wb_mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4078 | } |
| 4079 | // Return the new clone. |
| 4080 | return clone; |
| 4081 | } |
| 4082 | |
| 4083 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 4084 | MaybeObject* Heap::ReinitializeJSReceiver( |
| 4085 | JSReceiver* object, InstanceType type, int size) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4086 | ASSERT(type >= FIRST_JS_OBJECT_TYPE); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 4087 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4088 | // Allocate fresh map. |
| 4089 | // TODO(rossberg): Once we optimize proxies, cache these maps. |
| 4090 | Map* map; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4091 | MaybeObject* maybe = AllocateMap(type, size); |
| 4092 | if (!maybe->To<Map>(&map)) return maybe; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4093 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 4094 | // Check that the receiver has at least the size of the fresh object. |
| 4095 | int size_difference = object->map()->instance_size() - map->instance_size(); |
| 4096 | ASSERT(size_difference >= 0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4097 | |
| 4098 | map->set_prototype(object->map()->prototype()); |
| 4099 | |
| 4100 | // Allocate the backing storage for the properties. |
| 4101 | int prop_size = map->unused_property_fields() - map->inobject_properties(); |
| 4102 | Object* properties; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4103 | maybe = AllocateFixedArray(prop_size, TENURED); |
| 4104 | if (!maybe->ToObject(&properties)) return maybe; |
| 4105 | |
| 4106 | // Functions require some allocation, which might fail here. |
| 4107 | SharedFunctionInfo* shared = NULL; |
| 4108 | if (type == JS_FUNCTION_TYPE) { |
| 4109 | String* name; |
| 4110 | maybe = LookupAsciiSymbol("<freezing call trap>"); |
| 4111 | if (!maybe->To<String>(&name)) return maybe; |
| 4112 | maybe = AllocateSharedFunctionInfo(name); |
| 4113 | if (!maybe->To<SharedFunctionInfo>(&shared)) return maybe; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4116 | // Because of possible retries of this function after failure, |
| 4117 | // we must NOT fail after this point, where we have changed the type! |
| 4118 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4119 | // Reset the map for the object. |
| 4120 | object->set_map(map); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4121 | JSObject* jsobj = JSObject::cast(object); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4122 | |
| 4123 | // Reinitialize the object from the constructor map. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4124 | InitializeJSObjectFromMap(jsobj, FixedArray::cast(properties), map); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 4125 | |
| 4126 | // Functions require some minimal initialization. |
| 4127 | if (type == JS_FUNCTION_TYPE) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4128 | map->set_function_with_prototype(true); |
| 4129 | InitializeFunction(JSFunction::cast(object), shared, the_hole_value()); |
| 4130 | JSFunction::cast(object)->set_context( |
| 4131 | isolate()->context()->global_context()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | // Put in filler if the new object is smaller than the old. |
| 4135 | if (size_difference > 0) { |
| 4136 | CreateFillerObjectAt( |
| 4137 | object->address() + map->instance_size(), size_difference); |
| 4138 | } |
| 4139 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4140 | return object; |
| 4141 | } |
| 4142 | |
| 4143 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4144 | MaybeObject* Heap::ReinitializeJSGlobalProxy(JSFunction* constructor, |
| 4145 | JSGlobalProxy* object) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4146 | ASSERT(constructor->has_initial_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4147 | Map* map = constructor->initial_map(); |
| 4148 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4149 | // 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] | 4150 | // objects allocated using the constructor. |
| 4151 | ASSERT(map->instance_size() == object->map()->instance_size()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4152 | ASSERT(map->instance_type() == object->map()->instance_type()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4153 | |
| 4154 | // Allocate the backing storage for the properties. |
| 4155 | int prop_size = map->unused_property_fields() - map->inobject_properties(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4156 | Object* properties; |
| 4157 | { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, TENURED); |
| 4158 | if (!maybe_properties->ToObject(&properties)) return maybe_properties; |
| 4159 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4160 | |
| 4161 | // Reset the map for the object. |
| 4162 | object->set_map(constructor->initial_map()); |
| 4163 | |
| 4164 | // Reinitialize the object from the constructor map. |
| 4165 | InitializeJSObjectFromMap(object, FixedArray::cast(properties), map); |
| 4166 | return object; |
| 4167 | } |
| 4168 | |
| 4169 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4170 | MaybeObject* Heap::AllocateStringFromAscii(Vector<const char> string, |
| 4171 | PretenureFlag pretenure) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 4172 | if (string.length() == 1) { |
| 4173 | return Heap::LookupSingleCharacterStringFromCode(string[0]); |
| 4174 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4175 | Object* result; |
| 4176 | { MaybeObject* maybe_result = |
| 4177 | AllocateRawAsciiString(string.length(), pretenure); |
| 4178 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4179 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4180 | |
| 4181 | // Copy the characters into the new object. |
| 4182 | SeqAsciiString* string_result = SeqAsciiString::cast(result); |
| 4183 | for (int i = 0; i < string.length(); i++) { |
| 4184 | string_result->SeqAsciiStringSet(i, string[i]); |
| 4185 | } |
| 4186 | return result; |
| 4187 | } |
| 4188 | |
| 4189 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 4190 | MaybeObject* Heap::AllocateStringFromUtf8Slow(Vector<const char> string, |
| 4191 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4192 | // Count the number of characters in the UTF-8 string and check if |
| 4193 | // it is an ASCII string. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 4194 | Access<UnicodeCache::Utf8Decoder> |
| 4195 | decoder(isolate_->unicode_cache()->utf8_decoder()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4196 | decoder->Reset(string.start(), string.length()); |
| 4197 | int chars = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4198 | while (decoder->has_more()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4199 | uint32_t r = decoder->GetNext(); |
| 4200 | if (r <= unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 4201 | chars++; |
| 4202 | } else { |
| 4203 | chars += 2; |
| 4204 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4207 | Object* result; |
| 4208 | { MaybeObject* maybe_result = AllocateRawTwoByteString(chars, pretenure); |
| 4209 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4210 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4211 | |
| 4212 | // Convert and copy the characters into the new object. |
| 4213 | String* string_result = String::cast(result); |
| 4214 | decoder->Reset(string.start(), string.length()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4215 | int i = 0; |
| 4216 | while (i < chars) { |
| 4217 | uint32_t r = decoder->GetNext(); |
| 4218 | if (r > unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 4219 | string_result->Set(i++, unibrow::Utf16::LeadSurrogate(r)); |
| 4220 | string_result->Set(i++, unibrow::Utf16::TrailSurrogate(r)); |
| 4221 | } else { |
| 4222 | string_result->Set(i++, r); |
| 4223 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4224 | } |
| 4225 | return result; |
| 4226 | } |
| 4227 | |
| 4228 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4229 | MaybeObject* Heap::AllocateStringFromTwoByte(Vector<const uc16> string, |
| 4230 | PretenureFlag pretenure) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4231 | // Check if the string is an ASCII string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4232 | MaybeObject* maybe_result; |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 4233 | if (String::IsAscii(string.start(), string.length())) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4234 | maybe_result = AllocateRawAsciiString(string.length(), pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4235 | } else { // It's not an ASCII string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4236 | maybe_result = AllocateRawTwoByteString(string.length(), pretenure); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4237 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4238 | Object* result; |
| 4239 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4240 | |
| 4241 | // Copy the characters into the new object, which may be either ASCII or |
| 4242 | // UTF-16. |
| 4243 | String* string_result = String::cast(result); |
| 4244 | for (int i = 0; i < string.length(); i++) { |
| 4245 | string_result->Set(i, string[i]); |
| 4246 | } |
| 4247 | return result; |
| 4248 | } |
| 4249 | |
| 4250 | |
| 4251 | Map* Heap::SymbolMapForString(String* string) { |
| 4252 | // If the string is in new space it cannot be used as a symbol. |
| 4253 | if (InNewSpace(string)) return NULL; |
| 4254 | |
| 4255 | // Find the corresponding symbol map for strings. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4256 | switch (string->map()->instance_type()) { |
| 4257 | case STRING_TYPE: return symbol_map(); |
| 4258 | case ASCII_STRING_TYPE: return ascii_symbol_map(); |
| 4259 | case CONS_STRING_TYPE: return cons_symbol_map(); |
| 4260 | case CONS_ASCII_STRING_TYPE: return cons_ascii_symbol_map(); |
| 4261 | case EXTERNAL_STRING_TYPE: return external_symbol_map(); |
| 4262 | case EXTERNAL_ASCII_STRING_TYPE: return external_ascii_symbol_map(); |
| 4263 | case EXTERNAL_STRING_WITH_ASCII_DATA_TYPE: |
| 4264 | return external_symbol_with_ascii_data_map(); |
| 4265 | case SHORT_EXTERNAL_STRING_TYPE: return short_external_symbol_map(); |
| 4266 | case SHORT_EXTERNAL_ASCII_STRING_TYPE: |
| 4267 | return short_external_ascii_symbol_map(); |
| 4268 | case SHORT_EXTERNAL_STRING_WITH_ASCII_DATA_TYPE: |
| 4269 | return short_external_symbol_with_ascii_data_map(); |
| 4270 | default: return NULL; // No match found. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4271 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4275 | MaybeObject* Heap::AllocateInternalSymbol(unibrow::CharacterStream* buffer, |
| 4276 | int chars, |
| 4277 | uint32_t hash_field) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4278 | ASSERT(chars >= 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4279 | // Ensure the chars matches the number of characters in the buffer. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4280 | ASSERT(static_cast<unsigned>(chars) == buffer->Utf16Length()); |
| 4281 | // Determine whether the string is ASCII. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4282 | bool is_ascii = true; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4283 | while (buffer->has_more()) { |
| 4284 | if (buffer->GetNext() > unibrow::Utf8::kMaxOneByteChar) { |
| 4285 | is_ascii = false; |
| 4286 | break; |
| 4287 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4288 | } |
| 4289 | buffer->Rewind(); |
| 4290 | |
| 4291 | // Compute map and object size. |
| 4292 | int size; |
| 4293 | Map* map; |
| 4294 | |
| 4295 | if (is_ascii) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4296 | if (chars > SeqAsciiString::kMaxLength) { |
| 4297 | return Failure::OutOfMemoryException(); |
| 4298 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4299 | map = ascii_symbol_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4300 | size = SeqAsciiString::SizeFor(chars); |
| 4301 | } else { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4302 | if (chars > SeqTwoByteString::kMaxLength) { |
| 4303 | return Failure::OutOfMemoryException(); |
| 4304 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4305 | map = symbol_map(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4306 | size = SeqTwoByteString::SizeFor(chars); |
| 4307 | } |
| 4308 | |
| 4309 | // Allocate string. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4310 | Object* result; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4311 | { MaybeObject* maybe_result = (size > Page::kMaxNonCodeHeapObjectSize) |
| 4312 | ? lo_space_->AllocateRaw(size, NOT_EXECUTABLE) |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4313 | : old_data_space_->AllocateRaw(size); |
| 4314 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4315 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4316 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4317 | reinterpret_cast<HeapObject*>(result)->set_map_no_write_barrier(map); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4318 | // Set length and hash fields of the allocated string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4319 | String* answer = String::cast(result); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4320 | answer->set_length(chars); |
| 4321 | answer->set_hash_field(hash_field); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4322 | |
| 4323 | ASSERT_EQ(size, answer->Size()); |
| 4324 | |
| 4325 | // Fill in the characters. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4326 | int i = 0; |
| 4327 | while (i < chars) { |
| 4328 | uint32_t character = buffer->GetNext(); |
| 4329 | if (character > unibrow::Utf16::kMaxNonSurrogateCharCode) { |
| 4330 | answer->Set(i++, unibrow::Utf16::LeadSurrogate(character)); |
| 4331 | answer->Set(i++, unibrow::Utf16::TrailSurrogate(character)); |
| 4332 | } else { |
| 4333 | answer->Set(i++, character); |
| 4334 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4335 | } |
| 4336 | return answer; |
| 4337 | } |
| 4338 | |
| 4339 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4340 | MaybeObject* Heap::AllocateRawAsciiString(int length, PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4341 | if (length < 0 || length > SeqAsciiString::kMaxLength) { |
| 4342 | return Failure::OutOfMemoryException(); |
| 4343 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4344 | |
| 4345 | int size = SeqAsciiString::SizeFor(length); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4346 | ASSERT(size <= SeqAsciiString::kMaxSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4347 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4348 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 4349 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 4350 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4351 | if (space == NEW_SPACE) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4352 | if (size > kMaxObjectSizeInNewSpace) { |
| 4353 | // Allocate in large object space, retry space will be ignored. |
| 4354 | space = LO_SPACE; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4355 | } else if (size > Page::kMaxNonCodeHeapObjectSize) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4356 | // Allocate in new space, retry in large object space. |
| 4357 | retry_space = LO_SPACE; |
| 4358 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4359 | } else if (space == OLD_DATA_SPACE && |
| 4360 | size > Page::kMaxNonCodeHeapObjectSize) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4361 | space = LO_SPACE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4362 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4363 | Object* result; |
| 4364 | { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space); |
| 4365 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4366 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4367 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4368 | // Partially initialize the object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4369 | HeapObject::cast(result)->set_map_no_write_barrier(ascii_string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4370 | String::cast(result)->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4371 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4372 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 4373 | return result; |
| 4374 | } |
| 4375 | |
| 4376 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4377 | MaybeObject* Heap::AllocateRawTwoByteString(int length, |
| 4378 | PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4379 | if (length < 0 || length > SeqTwoByteString::kMaxLength) { |
| 4380 | return Failure::OutOfMemoryException(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4381 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4382 | int size = SeqTwoByteString::SizeFor(length); |
| 4383 | ASSERT(size <= SeqTwoByteString::kMaxSize); |
| 4384 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 4385 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 4386 | |
| 4387 | if (space == NEW_SPACE) { |
| 4388 | if (size > kMaxObjectSizeInNewSpace) { |
| 4389 | // Allocate in large object space, retry space will be ignored. |
| 4390 | space = LO_SPACE; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4391 | } else if (size > Page::kMaxNonCodeHeapObjectSize) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4392 | // Allocate in new space, retry in large object space. |
| 4393 | retry_space = LO_SPACE; |
| 4394 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4395 | } else if (space == OLD_DATA_SPACE && |
| 4396 | size > Page::kMaxNonCodeHeapObjectSize) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4397 | space = LO_SPACE; |
| 4398 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4399 | Object* result; |
| 4400 | { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space); |
| 4401 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4402 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4403 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4404 | // Partially initialize the object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4405 | HeapObject::cast(result)->set_map_no_write_barrier(string_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4406 | String::cast(result)->set_length(length); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4407 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4408 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 4409 | return result; |
| 4410 | } |
| 4411 | |
| 4412 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4413 | MaybeObject* Heap::AllocateJSArray( |
| 4414 | ElementsKind elements_kind, |
| 4415 | PretenureFlag pretenure) { |
| 4416 | Context* global_context = isolate()->context()->global_context(); |
| 4417 | JSFunction* array_function = global_context->array_function(); |
| 4418 | Map* map = array_function->initial_map(); |
| 4419 | if (elements_kind == FAST_DOUBLE_ELEMENTS) { |
| 4420 | map = Map::cast(global_context->double_js_array_map()); |
| 4421 | } else if (elements_kind == FAST_ELEMENTS || !FLAG_smi_only_arrays) { |
| 4422 | map = Map::cast(global_context->object_js_array_map()); |
| 4423 | } else { |
| 4424 | ASSERT(elements_kind == FAST_SMI_ONLY_ELEMENTS); |
| 4425 | ASSERT(map == global_context->smi_js_array_map()); |
| 4426 | } |
| 4427 | |
| 4428 | return AllocateJSObjectFromMap(map, pretenure); |
| 4429 | } |
| 4430 | |
| 4431 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4432 | MaybeObject* Heap::AllocateEmptyFixedArray() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4433 | int size = FixedArray::SizeFor(0); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4434 | Object* result; |
| 4435 | { MaybeObject* maybe_result = |
| 4436 | AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE); |
| 4437 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4438 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4439 | // Initialize the object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4440 | reinterpret_cast<FixedArray*>(result)->set_map_no_write_barrier( |
| 4441 | fixed_array_map()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4442 | reinterpret_cast<FixedArray*>(result)->set_length(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4443 | return result; |
| 4444 | } |
| 4445 | |
| 4446 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4447 | MaybeObject* Heap::AllocateRawFixedArray(int length) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4448 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 4449 | return Failure::OutOfMemoryException(); |
| 4450 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4451 | ASSERT(length > 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4452 | // Use the general function if we're forced to always allocate. |
| 4453 | if (always_allocate()) return AllocateFixedArray(length, TENURED); |
| 4454 | // Allocate the raw data for a fixed array. |
| 4455 | int size = FixedArray::SizeFor(length); |
| 4456 | return size <= kMaxObjectSizeInNewSpace |
| 4457 | ? new_space_.AllocateRaw(size) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4458 | : lo_space_->AllocateRaw(size, NOT_EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4462 | MaybeObject* Heap::CopyFixedArrayWithMap(FixedArray* src, Map* map) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4463 | int len = src->length(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4464 | Object* obj; |
| 4465 | { MaybeObject* maybe_obj = AllocateRawFixedArray(len); |
| 4466 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4467 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4468 | if (InNewSpace(obj)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4469 | HeapObject* dst = HeapObject::cast(obj); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4470 | dst->set_map_no_write_barrier(map); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 4471 | CopyBlock(dst->address() + kPointerSize, |
| 4472 | src->address() + kPointerSize, |
| 4473 | FixedArray::SizeFor(len) - kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4474 | return obj; |
| 4475 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4476 | HeapObject::cast(obj)->set_map_no_write_barrier(map); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4477 | FixedArray* result = FixedArray::cast(obj); |
| 4478 | result->set_length(len); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4479 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4480 | // Copy the content |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 4481 | AssertNoAllocation no_gc; |
| 4482 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4483 | for (int i = 0; i < len; i++) result->set(i, src->get(i), mode); |
| 4484 | return result; |
| 4485 | } |
| 4486 | |
| 4487 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4488 | MaybeObject* Heap::CopyFixedDoubleArrayWithMap(FixedDoubleArray* src, |
| 4489 | Map* map) { |
| 4490 | int len = src->length(); |
| 4491 | Object* obj; |
| 4492 | { MaybeObject* maybe_obj = AllocateRawFixedDoubleArray(len, NOT_TENURED); |
| 4493 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4494 | } |
| 4495 | HeapObject* dst = HeapObject::cast(obj); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4496 | dst->set_map_no_write_barrier(map); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4497 | CopyBlock( |
| 4498 | dst->address() + FixedDoubleArray::kLengthOffset, |
| 4499 | src->address() + FixedDoubleArray::kLengthOffset, |
| 4500 | FixedDoubleArray::SizeFor(len) - FixedDoubleArray::kLengthOffset); |
| 4501 | return obj; |
| 4502 | } |
| 4503 | |
| 4504 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4505 | MaybeObject* Heap::AllocateFixedArray(int length) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4506 | ASSERT(length >= 0); |
| 4507 | if (length == 0) return empty_fixed_array(); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4508 | Object* result; |
| 4509 | { MaybeObject* maybe_result = AllocateRawFixedArray(length); |
| 4510 | if (!maybe_result->ToObject(&result)) return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4511 | } |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4512 | // Initialize header. |
| 4513 | FixedArray* array = reinterpret_cast<FixedArray*>(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4514 | array->set_map_no_write_barrier(fixed_array_map()); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4515 | array->set_length(length); |
| 4516 | // Initialize body. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4517 | ASSERT(!InNewSpace(undefined_value())); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4518 | MemsetPointer(array->data_start(), undefined_value(), length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4519 | return result; |
| 4520 | } |
| 4521 | |
| 4522 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4523 | MaybeObject* Heap::AllocateRawFixedArray(int length, PretenureFlag pretenure) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4524 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 4525 | return Failure::OutOfMemoryException(); |
| 4526 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4527 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4528 | AllocationSpace space = |
| 4529 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4530 | int size = FixedArray::SizeFor(length); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4531 | if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) { |
| 4532 | // Too big for new space. |
| 4533 | space = LO_SPACE; |
| 4534 | } else if (space == OLD_POINTER_SPACE && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4535 | size > Page::kMaxNonCodeHeapObjectSize) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4536 | // Too big for old pointer space. |
| 4537 | space = LO_SPACE; |
| 4538 | } |
| 4539 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4540 | AllocationSpace retry_space = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4541 | (size <= Page::kMaxNonCodeHeapObjectSize) ? OLD_POINTER_SPACE : LO_SPACE; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4542 | |
| 4543 | return AllocateRaw(size, space, retry_space); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4547 | MUST_USE_RESULT static MaybeObject* AllocateFixedArrayWithFiller( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4548 | Heap* heap, |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4549 | int length, |
| 4550 | PretenureFlag pretenure, |
| 4551 | Object* filler) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4552 | ASSERT(length >= 0); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4553 | ASSERT(heap->empty_fixed_array()->IsFixedArray()); |
| 4554 | if (length == 0) return heap->empty_fixed_array(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4555 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4556 | ASSERT(!heap->InNewSpace(filler)); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4557 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4558 | { MaybeObject* maybe_result = heap->AllocateRawFixedArray(length, pretenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4559 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4560 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4561 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4562 | HeapObject::cast(result)->set_map_no_write_barrier(heap->fixed_array_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4563 | FixedArray* array = FixedArray::cast(result); |
| 4564 | array->set_length(length); |
| 4565 | MemsetPointer(array->data_start(), filler, length); |
| 4566 | return array; |
| 4567 | } |
| 4568 | |
| 4569 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4570 | MaybeObject* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4571 | return AllocateFixedArrayWithFiller(this, |
| 4572 | length, |
| 4573 | pretenure, |
| 4574 | undefined_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4578 | MaybeObject* Heap::AllocateFixedArrayWithHoles(int length, |
| 4579 | PretenureFlag pretenure) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4580 | return AllocateFixedArrayWithFiller(this, |
| 4581 | length, |
| 4582 | pretenure, |
| 4583 | the_hole_value()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4587 | MaybeObject* Heap::AllocateUninitializedFixedArray(int length) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4588 | if (length == 0) return empty_fixed_array(); |
| 4589 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4590 | Object* obj; |
| 4591 | { MaybeObject* maybe_obj = AllocateRawFixedArray(length); |
| 4592 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 4593 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4594 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4595 | reinterpret_cast<FixedArray*>(obj)->set_map_no_write_barrier( |
| 4596 | fixed_array_map()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4597 | FixedArray::cast(obj)->set_length(length); |
| 4598 | return obj; |
| 4599 | } |
| 4600 | |
| 4601 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4602 | MaybeObject* Heap::AllocateEmptyFixedDoubleArray() { |
| 4603 | int size = FixedDoubleArray::SizeFor(0); |
| 4604 | Object* result; |
| 4605 | { MaybeObject* maybe_result = |
| 4606 | AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE); |
| 4607 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4608 | } |
| 4609 | // Initialize the object. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4610 | reinterpret_cast<FixedDoubleArray*>(result)->set_map_no_write_barrier( |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4611 | fixed_double_array_map()); |
| 4612 | reinterpret_cast<FixedDoubleArray*>(result)->set_length(0); |
| 4613 | return result; |
| 4614 | } |
| 4615 | |
| 4616 | |
| 4617 | MaybeObject* Heap::AllocateUninitializedFixedDoubleArray( |
| 4618 | int length, |
| 4619 | PretenureFlag pretenure) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4620 | if (length == 0) return empty_fixed_array(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4621 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4622 | Object* elements_object; |
| 4623 | MaybeObject* maybe_obj = AllocateRawFixedDoubleArray(length, pretenure); |
| 4624 | if (!maybe_obj->ToObject(&elements_object)) return maybe_obj; |
| 4625 | FixedDoubleArray* elements = |
| 4626 | reinterpret_cast<FixedDoubleArray*>(elements_object); |
| 4627 | |
| 4628 | elements->set_map_no_write_barrier(fixed_double_array_map()); |
| 4629 | elements->set_length(length); |
| 4630 | return elements; |
| 4631 | } |
| 4632 | |
| 4633 | |
| 4634 | MaybeObject* Heap::AllocateFixedDoubleArrayWithHoles( |
| 4635 | int length, |
| 4636 | PretenureFlag pretenure) { |
| 4637 | if (length == 0) return empty_fixed_array(); |
| 4638 | |
| 4639 | Object* elements_object; |
| 4640 | MaybeObject* maybe_obj = AllocateRawFixedDoubleArray(length, pretenure); |
| 4641 | if (!maybe_obj->ToObject(&elements_object)) return maybe_obj; |
| 4642 | FixedDoubleArray* elements = |
| 4643 | reinterpret_cast<FixedDoubleArray*>(elements_object); |
| 4644 | |
| 4645 | for (int i = 0; i < length; ++i) { |
| 4646 | elements->set_the_hole(i); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4649 | elements->set_map_no_write_barrier(fixed_double_array_map()); |
| 4650 | elements->set_length(length); |
| 4651 | return elements; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | |
| 4655 | MaybeObject* Heap::AllocateRawFixedDoubleArray(int length, |
| 4656 | PretenureFlag pretenure) { |
| 4657 | if (length < 0 || length > FixedDoubleArray::kMaxLength) { |
| 4658 | return Failure::OutOfMemoryException(); |
| 4659 | } |
| 4660 | |
| 4661 | AllocationSpace space = |
| 4662 | (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 4663 | int size = FixedDoubleArray::SizeFor(length); |
| 4664 | if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) { |
| 4665 | // Too big for new space. |
| 4666 | space = LO_SPACE; |
| 4667 | } else if (space == OLD_DATA_SPACE && |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4668 | size > Page::kMaxNonCodeHeapObjectSize) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4669 | // Too big for old data space. |
| 4670 | space = LO_SPACE; |
| 4671 | } |
| 4672 | |
| 4673 | AllocationSpace retry_space = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4674 | (size <= Page::kMaxNonCodeHeapObjectSize) ? OLD_DATA_SPACE : LO_SPACE; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4675 | |
| 4676 | return AllocateRaw(size, space, retry_space); |
| 4677 | } |
| 4678 | |
| 4679 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4680 | MaybeObject* Heap::AllocateHashTable(int length, PretenureFlag pretenure) { |
| 4681 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4682 | { MaybeObject* maybe_result = AllocateFixedArray(length, pretenure); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4683 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4684 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4685 | reinterpret_cast<HeapObject*>(result)->set_map_no_write_barrier( |
| 4686 | hash_table_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4687 | ASSERT(result->IsHashTable()); |
| 4688 | return result; |
| 4689 | } |
| 4690 | |
| 4691 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4692 | MaybeObject* Heap::AllocateGlobalContext() { |
| 4693 | Object* result; |
| 4694 | { MaybeObject* maybe_result = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4695 | AllocateFixedArray(Context::GLOBAL_CONTEXT_SLOTS); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4696 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4697 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4698 | Context* context = reinterpret_cast<Context*>(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4699 | context->set_map_no_write_barrier(global_context_map()); |
| 4700 | context->set_smi_js_array_map(undefined_value()); |
| 4701 | context->set_double_js_array_map(undefined_value()); |
| 4702 | context->set_object_js_array_map(undefined_value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4703 | ASSERT(context->IsGlobalContext()); |
| 4704 | ASSERT(result->IsContext()); |
| 4705 | return result; |
| 4706 | } |
| 4707 | |
| 4708 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4709 | MaybeObject* Heap::AllocateFunctionContext(int length, JSFunction* function) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4710 | ASSERT(length >= Context::MIN_CONTEXT_SLOTS); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4711 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4712 | { MaybeObject* maybe_result = AllocateFixedArray(length); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4713 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4714 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4715 | Context* context = reinterpret_cast<Context*>(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4716 | context->set_map_no_write_barrier(function_context_map()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4717 | context->set_closure(function); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4718 | context->set_previous(function->context()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4719 | context->set_extension(NULL); |
| 4720 | context->set_global(function->context()->global()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4721 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4725 | MaybeObject* Heap::AllocateCatchContext(JSFunction* function, |
| 4726 | Context* previous, |
| 4727 | String* name, |
| 4728 | Object* thrown_object) { |
| 4729 | STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == Context::THROWN_OBJECT_INDEX); |
| 4730 | Object* result; |
| 4731 | { MaybeObject* maybe_result = |
| 4732 | AllocateFixedArray(Context::MIN_CONTEXT_SLOTS + 1); |
| 4733 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4734 | } |
| 4735 | Context* context = reinterpret_cast<Context*>(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4736 | context->set_map_no_write_barrier(catch_context_map()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4737 | context->set_closure(function); |
| 4738 | context->set_previous(previous); |
| 4739 | context->set_extension(name); |
| 4740 | context->set_global(previous->global()); |
| 4741 | context->set(Context::THROWN_OBJECT_INDEX, thrown_object); |
| 4742 | return context; |
| 4743 | } |
| 4744 | |
| 4745 | |
| 4746 | MaybeObject* Heap::AllocateWithContext(JSFunction* function, |
| 4747 | Context* previous, |
| 4748 | JSObject* extension) { |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4749 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4750 | { MaybeObject* maybe_result = AllocateFixedArray(Context::MIN_CONTEXT_SLOTS); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4751 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4752 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4753 | Context* context = reinterpret_cast<Context*>(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4754 | context->set_map_no_write_barrier(with_context_map()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4755 | context->set_closure(function); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4756 | context->set_previous(previous); |
| 4757 | context->set_extension(extension); |
| 4758 | context->set_global(previous->global()); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 4759 | return context; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4763 | MaybeObject* Heap::AllocateBlockContext(JSFunction* function, |
| 4764 | Context* previous, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4765 | ScopeInfo* scope_info) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4766 | Object* result; |
| 4767 | { MaybeObject* maybe_result = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4768 | AllocateFixedArrayWithHoles(scope_info->ContextLength()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4769 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4770 | } |
| 4771 | Context* context = reinterpret_cast<Context*>(result); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4772 | context->set_map_no_write_barrier(block_context_map()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4773 | context->set_closure(function); |
| 4774 | context->set_previous(previous); |
| 4775 | context->set_extension(scope_info); |
| 4776 | context->set_global(previous->global()); |
| 4777 | return context; |
| 4778 | } |
| 4779 | |
| 4780 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4781 | MaybeObject* Heap::AllocateScopeInfo(int length) { |
| 4782 | FixedArray* scope_info; |
| 4783 | MaybeObject* maybe_scope_info = AllocateFixedArray(length, TENURED); |
| 4784 | if (!maybe_scope_info->To(&scope_info)) return maybe_scope_info; |
| 4785 | scope_info->set_map_no_write_barrier(scope_info_map()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 4786 | return scope_info; |
| 4787 | } |
| 4788 | |
| 4789 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4790 | MaybeObject* Heap::AllocateStruct(InstanceType type) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4791 | Map* map; |
| 4792 | switch (type) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4793 | #define MAKE_CASE(NAME, Name, name) \ |
| 4794 | case NAME##_TYPE: map = name##_map(); break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4795 | STRUCT_LIST(MAKE_CASE) |
| 4796 | #undef MAKE_CASE |
| 4797 | default: |
| 4798 | UNREACHABLE(); |
| 4799 | return Failure::InternalError(); |
| 4800 | } |
| 4801 | int size = map->instance_size(); |
| 4802 | AllocationSpace space = |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4803 | (size > Page::kMaxNonCodeHeapObjectSize) ? LO_SPACE : OLD_POINTER_SPACE; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4804 | Object* result; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4805 | { MaybeObject* maybe_result = Allocate(map, space); |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 4806 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4807 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4808 | Struct::cast(result)->InitializeBody(size); |
| 4809 | return result; |
| 4810 | } |
| 4811 | |
| 4812 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4813 | bool Heap::IsHeapIterable() { |
| 4814 | return (!old_pointer_space()->was_swept_conservatively() && |
| 4815 | !old_data_space()->was_swept_conservatively()); |
| 4816 | } |
| 4817 | |
| 4818 | |
| 4819 | void Heap::EnsureHeapIsIterable() { |
| 4820 | ASSERT(IsAllocationAllowed()); |
| 4821 | if (!IsHeapIterable()) { |
| 4822 | CollectAllGarbage(kMakeHeapIterableMask, "Heap::EnsureHeapIsIterable"); |
| 4823 | } |
| 4824 | ASSERT(IsHeapIterable()); |
| 4825 | } |
| 4826 | |
| 4827 | |
| 4828 | void Heap::AdvanceIdleIncrementalMarking(intptr_t step_size) { |
Ben Murdoch | 8f9999f | 2012-04-23 10:39:17 +0100 | [diff] [blame] | 4829 | incremental_marking()->Step(step_size, |
| 4830 | IncrementalMarking::NO_GC_VIA_STACK_GUARD); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4831 | |
| 4832 | if (incremental_marking()->IsComplete()) { |
| 4833 | bool uncommit = false; |
| 4834 | if (gc_count_at_last_idle_gc_ == gc_count_) { |
| 4835 | // No GC since the last full GC, the mutator is probably not active. |
| 4836 | isolate_->compilation_cache()->Clear(); |
| 4837 | uncommit = true; |
| 4838 | } |
| 4839 | CollectAllGarbage(kNoGCFlags, "idle notification: finalize incremental"); |
| 4840 | gc_count_at_last_idle_gc_ = gc_count_; |
| 4841 | if (uncommit) { |
| 4842 | new_space_.Shrink(); |
| 4843 | UncommitFromSpace(); |
| 4844 | } |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | |
| 4849 | bool Heap::IdleNotification(int hint) { |
| 4850 | const int kMaxHint = 1000; |
| 4851 | intptr_t size_factor = Min(Max(hint, 30), kMaxHint) / 10; |
| 4852 | // The size factor is in range [3..100]. |
| 4853 | intptr_t step_size = size_factor * IncrementalMarking::kAllocatedThreshold; |
| 4854 | |
| 4855 | if (contexts_disposed_ > 0) { |
| 4856 | if (hint >= kMaxHint) { |
| 4857 | // The embedder is requesting a lot of GC work after context disposal, |
| 4858 | // we age inline caches so that they don't keep objects from |
| 4859 | // the old context alive. |
| 4860 | AgeInlineCaches(); |
| 4861 | } |
| 4862 | int mark_sweep_time = Min(TimeMarkSweepWouldTakeInMs(), 1000); |
| 4863 | if (hint >= mark_sweep_time && !FLAG_expose_gc && |
| 4864 | incremental_marking()->IsStopped()) { |
| 4865 | HistogramTimerScope scope(isolate_->counters()->gc_context()); |
| 4866 | CollectAllGarbage(kReduceMemoryFootprintMask, |
| 4867 | "idle notification: contexts disposed"); |
| 4868 | } else { |
| 4869 | AdvanceIdleIncrementalMarking(step_size); |
| 4870 | contexts_disposed_ = 0; |
| 4871 | } |
| 4872 | // Make sure that we have no pending context disposals. |
| 4873 | // Take into account that we might have decided to delay full collection |
| 4874 | // because incremental marking is in progress. |
| 4875 | ASSERT((contexts_disposed_ == 0) || !incremental_marking()->IsStopped()); |
| 4876 | return false; |
| 4877 | } |
| 4878 | |
| 4879 | if (hint >= kMaxHint || !FLAG_incremental_marking || |
| 4880 | FLAG_expose_gc || Serializer::enabled()) { |
| 4881 | return IdleGlobalGC(); |
| 4882 | } |
| 4883 | |
| 4884 | // By doing small chunks of GC work in each IdleNotification, |
| 4885 | // perform a round of incremental GCs and after that wait until |
| 4886 | // the mutator creates enough garbage to justify a new round. |
| 4887 | // An incremental GC progresses as follows: |
| 4888 | // 1. many incremental marking steps, |
| 4889 | // 2. one old space mark-sweep-compact, |
| 4890 | // 3. many lazy sweep steps. |
| 4891 | // Use mark-sweep-compact events to count incremental GCs in a round. |
| 4892 | |
| 4893 | |
| 4894 | if (incremental_marking()->IsStopped()) { |
| 4895 | if (!IsSweepingComplete() && |
| 4896 | !AdvanceSweepers(static_cast<int>(step_size))) { |
| 4897 | return false; |
| 4898 | } |
| 4899 | } |
| 4900 | |
| 4901 | if (mark_sweeps_since_idle_round_started_ >= kMaxMarkSweepsInIdleRound) { |
| 4902 | if (EnoughGarbageSinceLastIdleRound()) { |
| 4903 | StartIdleRound(); |
| 4904 | } else { |
| 4905 | return true; |
| 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | int new_mark_sweeps = ms_count_ - ms_count_at_last_idle_notification_; |
| 4910 | mark_sweeps_since_idle_round_started_ += new_mark_sweeps; |
| 4911 | ms_count_at_last_idle_notification_ = ms_count_; |
| 4912 | |
| 4913 | if (mark_sweeps_since_idle_round_started_ >= kMaxMarkSweepsInIdleRound) { |
| 4914 | FinishIdleRound(); |
| 4915 | return true; |
| 4916 | } |
| 4917 | |
| 4918 | if (incremental_marking()->IsStopped()) { |
| 4919 | if (!WorthStartingGCWhenIdle()) { |
| 4920 | FinishIdleRound(); |
| 4921 | return true; |
| 4922 | } |
| 4923 | incremental_marking()->Start(); |
| 4924 | } |
| 4925 | |
| 4926 | AdvanceIdleIncrementalMarking(step_size); |
| 4927 | return false; |
| 4928 | } |
| 4929 | |
| 4930 | |
| 4931 | bool Heap::IdleGlobalGC() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4932 | static const int kIdlesBeforeScavenge = 4; |
| 4933 | static const int kIdlesBeforeMarkSweep = 7; |
| 4934 | static const int kIdlesBeforeMarkCompact = 8; |
Ben Murdoch | db5a90a | 2011-01-06 18:27:03 +0000 | [diff] [blame] | 4935 | static const int kMaxIdleCount = kIdlesBeforeMarkCompact + 1; |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 4936 | static const unsigned int kGCsBetweenCleanup = 4; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4937 | |
| 4938 | if (!last_idle_notification_gc_count_init_) { |
| 4939 | last_idle_notification_gc_count_ = gc_count_; |
| 4940 | last_idle_notification_gc_count_init_ = true; |
| 4941 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4942 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4943 | bool uncommit = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4944 | bool finished = false; |
| 4945 | |
Ben Murdoch | db5a90a | 2011-01-06 18:27:03 +0000 | [diff] [blame] | 4946 | // Reset the number of idle notifications received when a number of |
| 4947 | // GCs have taken place. This allows another round of cleanup based |
| 4948 | // on idle notifications if enough work has been carried out to |
| 4949 | // provoke a number of garbage collections. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4950 | if (gc_count_ - last_idle_notification_gc_count_ < kGCsBetweenCleanup) { |
| 4951 | number_idle_notifications_ = |
| 4952 | Min(number_idle_notifications_ + 1, kMaxIdleCount); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4953 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4954 | number_idle_notifications_ = 0; |
| 4955 | last_idle_notification_gc_count_ = gc_count_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4958 | if (number_idle_notifications_ == kIdlesBeforeScavenge) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4959 | CollectGarbage(NEW_SPACE, "idle notification"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4960 | new_space_.Shrink(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4961 | last_idle_notification_gc_count_ = gc_count_; |
| 4962 | } else if (number_idle_notifications_ == kIdlesBeforeMarkSweep) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4963 | // Before doing the mark-sweep collections we clear the |
| 4964 | // compilation cache to avoid hanging on to source code and |
| 4965 | // generated code for cached functions. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4966 | isolate_->compilation_cache()->Clear(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4967 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4968 | CollectAllGarbage(kReduceMemoryFootprintMask, "idle notification"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4969 | new_space_.Shrink(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4970 | last_idle_notification_gc_count_ = gc_count_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4971 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4972 | } else if (number_idle_notifications_ == kIdlesBeforeMarkCompact) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4973 | CollectAllGarbage(kReduceMemoryFootprintMask, "idle notification"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4974 | new_space_.Shrink(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4975 | last_idle_notification_gc_count_ = gc_count_; |
| 4976 | number_idle_notifications_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4977 | finished = true; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4978 | } else if (number_idle_notifications_ > kIdlesBeforeMarkCompact) { |
Ben Murdoch | db5a90a | 2011-01-06 18:27:03 +0000 | [diff] [blame] | 4979 | // If we have received more than kIdlesBeforeMarkCompact idle |
| 4980 | // notifications we do not perform any cleanup because we don't |
| 4981 | // expect to gain much by doing so. |
| 4982 | finished = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4985 | if (uncommit) UncommitFromSpace(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4986 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4987 | return finished; |
| 4988 | } |
| 4989 | |
| 4990 | |
| 4991 | #ifdef DEBUG |
| 4992 | |
| 4993 | void Heap::Print() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 4994 | if (!HasBeenSetUp()) return; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 4995 | isolate()->PrintStack(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4996 | AllSpaces spaces; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4997 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 4998 | space->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4999 | } |
| 5000 | |
| 5001 | |
| 5002 | void Heap::ReportCodeStatistics(const char* title) { |
| 5003 | PrintF(">>>>>> Code Stats (%s) >>>>>>\n", title); |
| 5004 | PagedSpace::ResetCodeStatistics(); |
| 5005 | // We do not look for code in new space, map space, or old space. If code |
| 5006 | // somehow ends up in those spaces, we would miss it here. |
| 5007 | code_space_->CollectCodeStatistics(); |
| 5008 | lo_space_->CollectCodeStatistics(); |
| 5009 | PagedSpace::ReportCodeStatistics(); |
| 5010 | } |
| 5011 | |
| 5012 | |
| 5013 | // This function expects that NewSpace's allocated objects histogram is |
| 5014 | // populated (via a call to CollectStatistics or else as a side effect of a |
| 5015 | // just-completed scavenge collection). |
| 5016 | void Heap::ReportHeapStatistics(const char* title) { |
| 5017 | USE(title); |
| 5018 | PrintF(">>>>>> =============== %s (%d) =============== >>>>>>\n", |
| 5019 | title, gc_count_); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5020 | PrintF("old_gen_promotion_limit_ %" V8_PTR_PREFIX "d\n", |
| 5021 | old_gen_promotion_limit_); |
| 5022 | PrintF("old_gen_allocation_limit_ %" V8_PTR_PREFIX "d\n", |
| 5023 | old_gen_allocation_limit_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5024 | PrintF("old_gen_limit_factor_ %d\n", old_gen_limit_factor_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5025 | |
| 5026 | PrintF("\n"); |
| 5027 | PrintF("Number of handles : %d\n", HandleScope::NumberOfHandles()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5028 | isolate_->global_handles()->PrintStats(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5029 | PrintF("\n"); |
| 5030 | |
| 5031 | PrintF("Heap statistics : "); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5032 | isolate_->memory_allocator()->ReportStatistics(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5033 | PrintF("To space : "); |
| 5034 | new_space_.ReportStatistics(); |
| 5035 | PrintF("Old pointer space : "); |
| 5036 | old_pointer_space_->ReportStatistics(); |
| 5037 | PrintF("Old data space : "); |
| 5038 | old_data_space_->ReportStatistics(); |
| 5039 | PrintF("Code space : "); |
| 5040 | code_space_->ReportStatistics(); |
| 5041 | PrintF("Map space : "); |
| 5042 | map_space_->ReportStatistics(); |
| 5043 | PrintF("Cell space : "); |
| 5044 | cell_space_->ReportStatistics(); |
| 5045 | PrintF("Large object space : "); |
| 5046 | lo_space_->ReportStatistics(); |
| 5047 | PrintF(">>>>>> ========================================= >>>>>>\n"); |
| 5048 | } |
| 5049 | |
| 5050 | #endif // DEBUG |
| 5051 | |
| 5052 | bool Heap::Contains(HeapObject* value) { |
| 5053 | return Contains(value->address()); |
| 5054 | } |
| 5055 | |
| 5056 | |
| 5057 | bool Heap::Contains(Address addr) { |
| 5058 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5059 | return HasBeenSetUp() && |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5060 | (new_space_.ToSpaceContains(addr) || |
| 5061 | old_pointer_space_->Contains(addr) || |
| 5062 | old_data_space_->Contains(addr) || |
| 5063 | code_space_->Contains(addr) || |
| 5064 | map_space_->Contains(addr) || |
| 5065 | cell_space_->Contains(addr) || |
| 5066 | lo_space_->SlowContains(addr)); |
| 5067 | } |
| 5068 | |
| 5069 | |
| 5070 | bool Heap::InSpace(HeapObject* value, AllocationSpace space) { |
| 5071 | return InSpace(value->address(), space); |
| 5072 | } |
| 5073 | |
| 5074 | |
| 5075 | bool Heap::InSpace(Address addr, AllocationSpace space) { |
| 5076 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5077 | if (!HasBeenSetUp()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5078 | |
| 5079 | switch (space) { |
| 5080 | case NEW_SPACE: |
| 5081 | return new_space_.ToSpaceContains(addr); |
| 5082 | case OLD_POINTER_SPACE: |
| 5083 | return old_pointer_space_->Contains(addr); |
| 5084 | case OLD_DATA_SPACE: |
| 5085 | return old_data_space_->Contains(addr); |
| 5086 | case CODE_SPACE: |
| 5087 | return code_space_->Contains(addr); |
| 5088 | case MAP_SPACE: |
| 5089 | return map_space_->Contains(addr); |
| 5090 | case CELL_SPACE: |
| 5091 | return cell_space_->Contains(addr); |
| 5092 | case LO_SPACE: |
| 5093 | return lo_space_->SlowContains(addr); |
| 5094 | } |
| 5095 | |
| 5096 | return false; |
| 5097 | } |
| 5098 | |
| 5099 | |
| 5100 | #ifdef DEBUG |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 5101 | void Heap::Verify() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5102 | ASSERT(HasBeenSetUp()); |
| 5103 | |
| 5104 | store_buffer()->Verify(); |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5105 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5106 | VerifyPointersVisitor visitor; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5107 | IterateRoots(&visitor, VISIT_ONLY_STRONG); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5108 | |
| 5109 | new_space_.Verify(); |
| 5110 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5111 | old_pointer_space_->Verify(&visitor); |
| 5112 | map_space_->Verify(&visitor); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5113 | |
| 5114 | VerifyPointersVisitor no_dirty_regions_visitor; |
| 5115 | old_data_space_->Verify(&no_dirty_regions_visitor); |
| 5116 | code_space_->Verify(&no_dirty_regions_visitor); |
| 5117 | cell_space_->Verify(&no_dirty_regions_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5118 | |
| 5119 | lo_space_->Verify(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5120 | |
| 5121 | VerifyNoAccessorPairSharing(); |
| 5122 | } |
| 5123 | |
| 5124 | |
| 5125 | void Heap::VerifyNoAccessorPairSharing() { |
| 5126 | // Verification is done in 2 phases: First we mark all AccessorPairs, checking |
| 5127 | // that we mark only unmarked pairs, then we clear all marks, restoring the |
| 5128 | // initial state. We use the Smi tag of the AccessorPair's getter as the |
| 5129 | // marking bit, because we can never see a Smi as the getter. |
| 5130 | for (int phase = 0; phase < 2; phase++) { |
| 5131 | HeapObjectIterator iter(map_space()); |
| 5132 | for (HeapObject* obj = iter.Next(); obj != NULL; obj = iter.Next()) { |
| 5133 | if (obj->IsMap()) { |
| 5134 | DescriptorArray* descs = Map::cast(obj)->instance_descriptors(); |
| 5135 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 5136 | if (descs->GetType(i) == CALLBACKS && |
| 5137 | descs->GetValue(i)->IsAccessorPair()) { |
| 5138 | AccessorPair* accessors = AccessorPair::cast(descs->GetValue(i)); |
| 5139 | uintptr_t before = reinterpret_cast<intptr_t>(accessors->getter()); |
| 5140 | uintptr_t after = (phase == 0) ? |
| 5141 | ((before & ~kSmiTagMask) | kSmiTag) : |
| 5142 | ((before & ~kHeapObjectTag) | kHeapObjectTag); |
| 5143 | CHECK(before != after); |
| 5144 | accessors->set_getter(reinterpret_cast<Object*>(after)); |
| 5145 | } |
| 5146 | } |
| 5147 | } |
| 5148 | } |
| 5149 | } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 5150 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5151 | #endif // DEBUG |
| 5152 | |
| 5153 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5154 | MaybeObject* Heap::LookupSymbol(Vector<const char> string) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5155 | Object* symbol = NULL; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5156 | Object* new_table; |
| 5157 | { MaybeObject* maybe_new_table = |
| 5158 | symbol_table()->LookupSymbol(string, &symbol); |
| 5159 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 5160 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5161 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 5162 | // SymbolTable is a singleton and checks for identity. |
| 5163 | roots_[kSymbolTableRootIndex] = new_table; |
| 5164 | ASSERT(symbol != NULL); |
| 5165 | return symbol; |
| 5166 | } |
| 5167 | |
| 5168 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 5169 | MaybeObject* Heap::LookupAsciiSymbol(Vector<const char> string) { |
| 5170 | Object* symbol = NULL; |
| 5171 | Object* new_table; |
| 5172 | { MaybeObject* maybe_new_table = |
| 5173 | symbol_table()->LookupAsciiSymbol(string, &symbol); |
| 5174 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 5175 | } |
| 5176 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 5177 | // SymbolTable is a singleton and checks for identity. |
| 5178 | roots_[kSymbolTableRootIndex] = new_table; |
| 5179 | ASSERT(symbol != NULL); |
| 5180 | return symbol; |
| 5181 | } |
| 5182 | |
| 5183 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5184 | MaybeObject* Heap::LookupAsciiSymbol(Handle<SeqAsciiString> string, |
| 5185 | int from, |
| 5186 | int length) { |
| 5187 | Object* symbol = NULL; |
| 5188 | Object* new_table; |
| 5189 | { MaybeObject* maybe_new_table = |
| 5190 | symbol_table()->LookupSubStringAsciiSymbol(string, |
| 5191 | from, |
| 5192 | length, |
| 5193 | &symbol); |
| 5194 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 5195 | } |
| 5196 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 5197 | // SymbolTable is a singleton and checks for identity. |
| 5198 | roots_[kSymbolTableRootIndex] = new_table; |
| 5199 | ASSERT(symbol != NULL); |
| 5200 | return symbol; |
| 5201 | } |
| 5202 | |
| 5203 | |
Steve Block | 9fac840 | 2011-05-12 15:51:54 +0100 | [diff] [blame] | 5204 | MaybeObject* Heap::LookupTwoByteSymbol(Vector<const uc16> string) { |
| 5205 | Object* symbol = NULL; |
| 5206 | Object* new_table; |
| 5207 | { MaybeObject* maybe_new_table = |
| 5208 | symbol_table()->LookupTwoByteSymbol(string, &symbol); |
| 5209 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 5210 | } |
| 5211 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 5212 | // SymbolTable is a singleton and checks for identity. |
| 5213 | roots_[kSymbolTableRootIndex] = new_table; |
| 5214 | ASSERT(symbol != NULL); |
| 5215 | return symbol; |
| 5216 | } |
| 5217 | |
| 5218 | |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5219 | MaybeObject* Heap::LookupSymbol(String* string) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5220 | if (string->IsSymbol()) return string; |
| 5221 | Object* symbol = NULL; |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 5222 | Object* new_table; |
| 5223 | { MaybeObject* maybe_new_table = |
| 5224 | symbol_table()->LookupString(string, &symbol); |
| 5225 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 5226 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5227 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 5228 | // SymbolTable is a singleton and checks for identity. |
| 5229 | roots_[kSymbolTableRootIndex] = new_table; |
| 5230 | ASSERT(symbol != NULL); |
| 5231 | return symbol; |
| 5232 | } |
| 5233 | |
| 5234 | |
| 5235 | bool Heap::LookupSymbolIfExists(String* string, String** symbol) { |
| 5236 | if (string->IsSymbol()) { |
| 5237 | *symbol = string; |
| 5238 | return true; |
| 5239 | } |
| 5240 | return symbol_table()->LookupSymbolIfExists(string, symbol); |
| 5241 | } |
| 5242 | |
| 5243 | |
| 5244 | #ifdef DEBUG |
| 5245 | void Heap::ZapFromSpace() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5246 | NewSpacePageIterator it(new_space_.FromSpaceStart(), |
| 5247 | new_space_.FromSpaceEnd()); |
| 5248 | while (it.has_next()) { |
| 5249 | NewSpacePage* page = it.next(); |
| 5250 | for (Address cursor = page->area_start(), limit = page->area_end(); |
| 5251 | cursor < limit; |
| 5252 | cursor += kPointerSize) { |
| 5253 | Memory::Address_at(cursor) = kFromSpaceZapValue; |
| 5254 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | #endif // DEBUG |
| 5258 | |
| 5259 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 5260 | void Heap::IterateAndMarkPointersToFromSpace(Address start, |
| 5261 | Address end, |
| 5262 | ObjectSlotCallback callback) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5263 | Address slot_address = start; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5264 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5265 | // We are not collecting slots on new space objects during mutation |
| 5266 | // thus we have to scan for pointers to evacuation candidates when we |
| 5267 | // promote objects. But we should not record any slots in non-black |
| 5268 | // objects. Grey object's slots would be rescanned. |
| 5269 | // White object might not survive until the end of collection |
| 5270 | // it would be a violation of the invariant to record it's slots. |
| 5271 | bool record_slots = false; |
| 5272 | if (incremental_marking()->IsCompacting()) { |
| 5273 | MarkBit mark_bit = Marking::MarkBitFrom(HeapObject::FromAddress(start)); |
| 5274 | record_slots = Marking::IsBlack(mark_bit); |
| 5275 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5276 | |
| 5277 | while (slot_address < end) { |
| 5278 | Object** slot = reinterpret_cast<Object**>(slot_address); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5279 | Object* object = *slot; |
| 5280 | // If the store buffer becomes overfull we mark pages as being exempt from |
| 5281 | // the store buffer. These pages are scanned to find pointers that point |
| 5282 | // to the new space. In that case we may hit newly promoted objects and |
| 5283 | // fix the pointers before the promotion queue gets to them. Thus the 'if'. |
| 5284 | if (object->IsHeapObject()) { |
| 5285 | if (Heap::InFromSpace(object)) { |
| 5286 | callback(reinterpret_cast<HeapObject**>(slot), |
| 5287 | HeapObject::cast(object)); |
| 5288 | Object* new_object = *slot; |
| 5289 | if (InNewSpace(new_object)) { |
| 5290 | SLOW_ASSERT(Heap::InToSpace(new_object)); |
| 5291 | SLOW_ASSERT(new_object->IsHeapObject()); |
| 5292 | store_buffer_.EnterDirectlyIntoStoreBuffer( |
| 5293 | reinterpret_cast<Address>(slot)); |
| 5294 | } |
| 5295 | SLOW_ASSERT(!MarkCompactCollector::IsOnEvacuationCandidate(new_object)); |
| 5296 | } else if (record_slots && |
| 5297 | MarkCompactCollector::IsOnEvacuationCandidate(object)) { |
| 5298 | mark_compact_collector()->RecordSlot(slot, slot, object); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5299 | } |
| 5300 | } |
| 5301 | slot_address += kPointerSize; |
| 5302 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5303 | } |
| 5304 | |
| 5305 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5306 | #ifdef DEBUG |
| 5307 | typedef bool (*CheckStoreBufferFilter)(Object** addr); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5308 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 5309 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5310 | bool IsAMapPointerAddress(Object** addr) { |
| 5311 | uintptr_t a = reinterpret_cast<uintptr_t>(addr); |
| 5312 | int mod = a % Map::kSize; |
| 5313 | return mod >= Map::kPointerFieldsBeginOffset && |
| 5314 | mod < Map::kPointerFieldsEndOffset; |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5315 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 5316 | |
| 5317 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5318 | bool EverythingsAPointer(Object** addr) { |
| 5319 | return true; |
| 5320 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 5321 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 5322 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5323 | static void CheckStoreBuffer(Heap* heap, |
| 5324 | Object** current, |
| 5325 | Object** limit, |
| 5326 | Object**** store_buffer_position, |
| 5327 | Object*** store_buffer_top, |
| 5328 | CheckStoreBufferFilter filter, |
| 5329 | Address special_garbage_start, |
| 5330 | Address special_garbage_end) { |
| 5331 | Map* free_space_map = heap->free_space_map(); |
| 5332 | for ( ; current < limit; current++) { |
| 5333 | Object* o = *current; |
| 5334 | Address current_address = reinterpret_cast<Address>(current); |
| 5335 | // Skip free space. |
| 5336 | if (o == free_space_map) { |
| 5337 | Address current_address = reinterpret_cast<Address>(current); |
| 5338 | FreeSpace* free_space = |
| 5339 | FreeSpace::cast(HeapObject::FromAddress(current_address)); |
| 5340 | int skip = free_space->Size(); |
| 5341 | ASSERT(current_address + skip <= reinterpret_cast<Address>(limit)); |
| 5342 | ASSERT(skip > 0); |
| 5343 | current_address += skip - kPointerSize; |
| 5344 | current = reinterpret_cast<Object**>(current_address); |
| 5345 | continue; |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 5346 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5347 | // Skip the current linear allocation space between top and limit which is |
| 5348 | // unmarked with the free space map, but can contain junk. |
| 5349 | if (current_address == special_garbage_start && |
| 5350 | special_garbage_end != special_garbage_start) { |
| 5351 | current_address = special_garbage_end - kPointerSize; |
| 5352 | current = reinterpret_cast<Object**>(current_address); |
| 5353 | continue; |
| 5354 | } |
| 5355 | if (!(*filter)(current)) continue; |
| 5356 | ASSERT(current_address < special_garbage_start || |
| 5357 | current_address >= special_garbage_end); |
| 5358 | ASSERT(reinterpret_cast<uintptr_t>(o) != kFreeListZapValue); |
| 5359 | // We have to check that the pointer does not point into new space |
| 5360 | // without trying to cast it to a heap object since the hash field of |
| 5361 | // a string can contain values like 1 and 3 which are tagged null |
| 5362 | // pointers. |
| 5363 | if (!heap->InNewSpace(o)) continue; |
| 5364 | while (**store_buffer_position < current && |
| 5365 | *store_buffer_position < store_buffer_top) { |
| 5366 | (*store_buffer_position)++; |
| 5367 | } |
| 5368 | if (**store_buffer_position != current || |
| 5369 | *store_buffer_position == store_buffer_top) { |
| 5370 | Object** obj_start = current; |
| 5371 | while (!(*obj_start)->IsMap()) obj_start--; |
| 5372 | UNREACHABLE(); |
| 5373 | } |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 5374 | } |
| 5375 | } |
Ben Murdoch | 592a9fc | 2012-03-05 11:04:45 +0000 | [diff] [blame] | 5376 | |
| 5377 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5378 | // Check that the store buffer contains all intergenerational pointers by |
| 5379 | // scanning a page and ensuring that all pointers to young space are in the |
| 5380 | // store buffer. |
| 5381 | void Heap::OldPointerSpaceCheckStoreBuffer() { |
| 5382 | OldSpace* space = old_pointer_space(); |
| 5383 | PageIterator pages(space); |
| 5384 | |
| 5385 | store_buffer()->SortUniq(); |
| 5386 | |
| 5387 | while (pages.has_next()) { |
| 5388 | Page* page = pages.next(); |
| 5389 | Object** current = reinterpret_cast<Object**>(page->area_start()); |
| 5390 | |
| 5391 | Address end = page->area_end(); |
| 5392 | |
| 5393 | Object*** store_buffer_position = store_buffer()->Start(); |
| 5394 | Object*** store_buffer_top = store_buffer()->Top(); |
| 5395 | |
| 5396 | Object** limit = reinterpret_cast<Object**>(end); |
| 5397 | CheckStoreBuffer(this, |
| 5398 | current, |
| 5399 | limit, |
| 5400 | &store_buffer_position, |
| 5401 | store_buffer_top, |
| 5402 | &EverythingsAPointer, |
| 5403 | space->top(), |
| 5404 | space->limit()); |
| 5405 | } |
| 5406 | } |
| 5407 | |
| 5408 | |
| 5409 | void Heap::MapSpaceCheckStoreBuffer() { |
| 5410 | MapSpace* space = map_space(); |
| 5411 | PageIterator pages(space); |
| 5412 | |
| 5413 | store_buffer()->SortUniq(); |
| 5414 | |
| 5415 | while (pages.has_next()) { |
| 5416 | Page* page = pages.next(); |
| 5417 | Object** current = reinterpret_cast<Object**>(page->area_start()); |
| 5418 | |
| 5419 | Address end = page->area_end(); |
| 5420 | |
| 5421 | Object*** store_buffer_position = store_buffer()->Start(); |
| 5422 | Object*** store_buffer_top = store_buffer()->Top(); |
| 5423 | |
| 5424 | Object** limit = reinterpret_cast<Object**>(end); |
| 5425 | CheckStoreBuffer(this, |
| 5426 | current, |
| 5427 | limit, |
| 5428 | &store_buffer_position, |
| 5429 | store_buffer_top, |
| 5430 | &IsAMapPointerAddress, |
| 5431 | space->top(), |
| 5432 | space->limit()); |
| 5433 | } |
| 5434 | } |
| 5435 | |
| 5436 | |
| 5437 | void Heap::LargeObjectSpaceCheckStoreBuffer() { |
| 5438 | LargeObjectIterator it(lo_space()); |
| 5439 | for (HeapObject* object = it.Next(); object != NULL; object = it.Next()) { |
| 5440 | // We only have code, sequential strings, or fixed arrays in large |
| 5441 | // object space, and only fixed arrays can possibly contain pointers to |
| 5442 | // the young generation. |
| 5443 | if (object->IsFixedArray()) { |
| 5444 | Object*** store_buffer_position = store_buffer()->Start(); |
| 5445 | Object*** store_buffer_top = store_buffer()->Top(); |
| 5446 | Object** current = reinterpret_cast<Object**>(object->address()); |
| 5447 | Object** limit = |
| 5448 | reinterpret_cast<Object**>(object->address() + object->Size()); |
| 5449 | CheckStoreBuffer(this, |
| 5450 | current, |
| 5451 | limit, |
| 5452 | &store_buffer_position, |
| 5453 | store_buffer_top, |
| 5454 | &EverythingsAPointer, |
| 5455 | NULL, |
| 5456 | NULL); |
| 5457 | } |
| 5458 | } |
| 5459 | } |
| 5460 | #endif |
| 5461 | |
| 5462 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5463 | void Heap::IterateRoots(ObjectVisitor* v, VisitMode mode) { |
| 5464 | IterateStrongRoots(v, mode); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5465 | IterateWeakRoots(v, mode); |
| 5466 | } |
| 5467 | |
| 5468 | |
| 5469 | void Heap::IterateWeakRoots(ObjectVisitor* v, VisitMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5470 | v->VisitPointer(reinterpret_cast<Object**>(&roots_[kSymbolTableRootIndex])); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5471 | v->Synchronize(VisitorSynchronization::kSymbolTable); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5472 | if (mode != VISIT_ALL_IN_SCAVENGE && |
| 5473 | mode != VISIT_ALL_IN_SWEEP_NEWSPACE) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5474 | // Scavenge collections have special processing for this. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5475 | external_string_table_.Iterate(v); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5476 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5477 | v->Synchronize(VisitorSynchronization::kExternalStringsTable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5481 | void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5482 | v->VisitPointers(&roots_[0], &roots_[kStrongRootListLength]); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5483 | v->Synchronize(VisitorSynchronization::kStrongRootList); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5484 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5485 | v->VisitPointer(BitCast<Object**>(&hidden_symbol_)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5486 | v->Synchronize(VisitorSynchronization::kSymbol); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5487 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5488 | isolate_->bootstrapper()->Iterate(v); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5489 | v->Synchronize(VisitorSynchronization::kBootstrapper); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5490 | isolate_->Iterate(v); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5491 | v->Synchronize(VisitorSynchronization::kTop); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5492 | Relocatable::Iterate(v); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5493 | v->Synchronize(VisitorSynchronization::kRelocatable); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5494 | |
| 5495 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5496 | isolate_->debug()->Iterate(v); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5497 | if (isolate_->deoptimizer_data() != NULL) { |
| 5498 | isolate_->deoptimizer_data()->Iterate(v); |
| 5499 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5500 | #endif |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5501 | v->Synchronize(VisitorSynchronization::kDebug); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5502 | isolate_->compilation_cache()->Iterate(v); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5503 | v->Synchronize(VisitorSynchronization::kCompilationCache); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5504 | |
| 5505 | // Iterate over local handles in handle scopes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5506 | isolate_->handle_scope_implementer()->Iterate(v); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5507 | v->Synchronize(VisitorSynchronization::kHandleScope); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5508 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5509 | // Iterate over the builtin code objects and code stubs in the |
| 5510 | // heap. Note that it is not necessary to iterate over code objects |
| 5511 | // on scavenge collections. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5512 | if (mode != VISIT_ALL_IN_SCAVENGE) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5513 | isolate_->builtins()->IterateBuiltins(v); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 5514 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5515 | v->Synchronize(VisitorSynchronization::kBuiltins); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5516 | |
| 5517 | // Iterate over global handles. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5518 | switch (mode) { |
| 5519 | case VISIT_ONLY_STRONG: |
| 5520 | isolate_->global_handles()->IterateStrongRoots(v); |
| 5521 | break; |
| 5522 | case VISIT_ALL_IN_SCAVENGE: |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 5523 | isolate_->global_handles()->IterateNewSpaceStrongAndDependentRoots(v); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 5524 | break; |
| 5525 | case VISIT_ALL_IN_SWEEP_NEWSPACE: |
| 5526 | case VISIT_ALL: |
| 5527 | isolate_->global_handles()->IterateAllRoots(v); |
| 5528 | break; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5529 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5530 | v->Synchronize(VisitorSynchronization::kGlobalHandles); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5531 | |
| 5532 | // Iterate over pointers being held by inactive threads. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5533 | isolate_->thread_manager()->Iterate(v); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5534 | v->Synchronize(VisitorSynchronization::kThreadManager); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 5535 | |
| 5536 | // Iterate over the pointers the Serialization/Deserialization code is |
| 5537 | // holding. |
| 5538 | // During garbage collection this keeps the partial snapshot cache alive. |
| 5539 | // During deserialization of the startup snapshot this creates the partial |
| 5540 | // snapshot cache and deserializes the objects it refers to. During |
| 5541 | // serialization this does nothing, since the partial snapshot cache is |
| 5542 | // empty. However the next thing we do is create the partial snapshot, |
| 5543 | // filling up the partial snapshot cache with objects it needs as we go. |
| 5544 | SerializerDeserializer::Iterate(v); |
| 5545 | // We don't do a v->Synchronize call here, because in debug mode that will |
| 5546 | // output a flag to the snapshot. However at this point the serializer and |
| 5547 | // deserializer are deliberately a little unsynchronized (see above) so the |
| 5548 | // checking of the sync flag in the snapshot would fail. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5549 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5550 | |
| 5551 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5552 | // TODO(1236194): Since the heap size is configurable on the command line |
| 5553 | // and through the API, we should gracefully handle the case that the heap |
| 5554 | // size is not big enough to fit all the initial objects. |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 5555 | bool Heap::ConfigureHeap(int max_semispace_size, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5556 | intptr_t max_old_gen_size, |
| 5557 | intptr_t max_executable_size) { |
| 5558 | if (HasBeenSetUp()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5559 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5560 | if (max_semispace_size > 0) { |
| 5561 | if (max_semispace_size < Page::kPageSize) { |
| 5562 | max_semispace_size = Page::kPageSize; |
| 5563 | if (FLAG_trace_gc) { |
| 5564 | PrintF("Max semispace size cannot be less than %dkbytes\n", |
| 5565 | Page::kPageSize >> 10); |
| 5566 | } |
| 5567 | } |
| 5568 | max_semispace_size_ = max_semispace_size; |
| 5569 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5570 | |
| 5571 | if (Snapshot::IsEnabled()) { |
| 5572 | // If we are using a snapshot we always reserve the default amount |
| 5573 | // of memory for each semispace because code in the snapshot has |
| 5574 | // write-barrier code that relies on the size and alignment of new |
| 5575 | // space. We therefore cannot use a larger max semispace size |
| 5576 | // than the default reserved semispace size. |
| 5577 | if (max_semispace_size_ > reserved_semispace_size_) { |
| 5578 | max_semispace_size_ = reserved_semispace_size_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5579 | if (FLAG_trace_gc) { |
| 5580 | PrintF("Max semispace size cannot be more than %dkbytes\n", |
| 5581 | reserved_semispace_size_ >> 10); |
| 5582 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5583 | } |
| 5584 | } else { |
| 5585 | // If we are not using snapshots we reserve space for the actual |
| 5586 | // max semispace size. |
| 5587 | reserved_semispace_size_ = max_semispace_size_; |
| 5588 | } |
| 5589 | |
| 5590 | if (max_old_gen_size > 0) max_old_generation_size_ = max_old_gen_size; |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 5591 | if (max_executable_size > 0) { |
| 5592 | max_executable_size_ = RoundUp(max_executable_size, Page::kPageSize); |
| 5593 | } |
| 5594 | |
| 5595 | // The max executable size must be less than or equal to the max old |
| 5596 | // generation size. |
| 5597 | if (max_executable_size_ > max_old_generation_size_) { |
| 5598 | max_executable_size_ = max_old_generation_size_; |
| 5599 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5600 | |
| 5601 | // The new space size must be a power of two to support single-bit testing |
| 5602 | // for containment. |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5603 | max_semispace_size_ = RoundUpToPowerOf2(max_semispace_size_); |
| 5604 | reserved_semispace_size_ = RoundUpToPowerOf2(reserved_semispace_size_); |
| 5605 | initial_semispace_size_ = Min(initial_semispace_size_, max_semispace_size_); |
| 5606 | external_allocation_limit_ = 10 * max_semispace_size_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5607 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5608 | // The old generation is paged and needs at least one page for each space. |
| 5609 | int paged_space_count = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1; |
| 5610 | max_old_generation_size_ = Max(static_cast<intptr_t>(paged_space_count * |
| 5611 | Page::kPageSize), |
| 5612 | RoundUp(max_old_generation_size_, |
| 5613 | Page::kPageSize)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5614 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5615 | configured_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5616 | return true; |
| 5617 | } |
| 5618 | |
| 5619 | |
| 5620 | bool Heap::ConfigureHeapDefault() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5621 | return ConfigureHeap(static_cast<intptr_t>(FLAG_max_new_space_size / 2) * KB, |
| 5622 | static_cast<intptr_t>(FLAG_max_old_space_size) * MB, |
| 5623 | static_cast<intptr_t>(FLAG_max_executable_size) * MB); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5627 | void Heap::RecordStats(HeapStats* stats, bool take_snapshot) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5628 | *stats->start_marker = HeapStats::kStartMarker; |
| 5629 | *stats->end_marker = HeapStats::kEndMarker; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5630 | *stats->new_space_size = new_space_.SizeAsInt(); |
| 5631 | *stats->new_space_capacity = static_cast<int>(new_space_.Capacity()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5632 | *stats->old_pointer_space_size = old_pointer_space_->SizeOfObjects(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5633 | *stats->old_pointer_space_capacity = old_pointer_space_->Capacity(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5634 | *stats->old_data_space_size = old_data_space_->SizeOfObjects(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5635 | *stats->old_data_space_capacity = old_data_space_->Capacity(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5636 | *stats->code_space_size = code_space_->SizeOfObjects(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5637 | *stats->code_space_capacity = code_space_->Capacity(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5638 | *stats->map_space_size = map_space_->SizeOfObjects(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5639 | *stats->map_space_capacity = map_space_->Capacity(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5640 | *stats->cell_space_size = cell_space_->SizeOfObjects(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5641 | *stats->cell_space_capacity = cell_space_->Capacity(); |
| 5642 | *stats->lo_space_size = lo_space_->Size(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5643 | isolate_->global_handles()->RecordStats(stats); |
| 5644 | *stats->memory_allocator_size = isolate()->memory_allocator()->Size(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5645 | *stats->memory_allocator_capacity = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5646 | isolate()->memory_allocator()->Size() + |
| 5647 | isolate()->memory_allocator()->Available(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5648 | *stats->os_error = OS::GetLastError(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5649 | isolate()->memory_allocator()->Available(); |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5650 | if (take_snapshot) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5651 | HeapIterator iterator; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5652 | for (HeapObject* obj = iterator.next(); |
| 5653 | obj != NULL; |
| 5654 | obj = iterator.next()) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 5655 | InstanceType type = obj->map()->instance_type(); |
| 5656 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 5657 | stats->objects_per_type[type]++; |
| 5658 | stats->size_per_type[type] += obj->Size(); |
| 5659 | } |
| 5660 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5661 | } |
| 5662 | |
| 5663 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5664 | intptr_t Heap::PromotedSpaceSize() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5665 | return old_pointer_space_->Size() |
| 5666 | + old_data_space_->Size() |
| 5667 | + code_space_->Size() |
| 5668 | + map_space_->Size() |
| 5669 | + cell_space_->Size() |
| 5670 | + lo_space_->Size(); |
| 5671 | } |
| 5672 | |
| 5673 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5674 | intptr_t Heap::PromotedSpaceSizeOfObjects() { |
| 5675 | return old_pointer_space_->SizeOfObjects() |
| 5676 | + old_data_space_->SizeOfObjects() |
| 5677 | + code_space_->SizeOfObjects() |
| 5678 | + map_space_->SizeOfObjects() |
| 5679 | + cell_space_->SizeOfObjects() |
| 5680 | + lo_space_->SizeOfObjects(); |
| 5681 | } |
| 5682 | |
| 5683 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5684 | int Heap::PromotedExternalMemorySize() { |
| 5685 | if (amount_of_external_allocated_memory_ |
| 5686 | <= amount_of_external_allocated_memory_at_last_global_gc_) return 0; |
| 5687 | return amount_of_external_allocated_memory_ |
| 5688 | - amount_of_external_allocated_memory_at_last_global_gc_; |
| 5689 | } |
| 5690 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5691 | #ifdef DEBUG |
| 5692 | |
| 5693 | // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject. |
| 5694 | static const int kMarkTag = 2; |
| 5695 | |
| 5696 | |
| 5697 | class HeapDebugUtils { |
| 5698 | public: |
| 5699 | explicit HeapDebugUtils(Heap* heap) |
| 5700 | : search_for_any_global_(false), |
| 5701 | search_target_(NULL), |
| 5702 | found_target_(false), |
| 5703 | object_stack_(20), |
| 5704 | heap_(heap) { |
| 5705 | } |
| 5706 | |
| 5707 | class MarkObjectVisitor : public ObjectVisitor { |
| 5708 | public: |
| 5709 | explicit MarkObjectVisitor(HeapDebugUtils* utils) : utils_(utils) { } |
| 5710 | |
| 5711 | void VisitPointers(Object** start, Object** end) { |
| 5712 | // Copy all HeapObject pointers in [start, end) |
| 5713 | for (Object** p = start; p < end; p++) { |
| 5714 | if ((*p)->IsHeapObject()) |
| 5715 | utils_->MarkObjectRecursively(p); |
| 5716 | } |
| 5717 | } |
| 5718 | |
| 5719 | HeapDebugUtils* utils_; |
| 5720 | }; |
| 5721 | |
| 5722 | void MarkObjectRecursively(Object** p) { |
| 5723 | if (!(*p)->IsHeapObject()) return; |
| 5724 | |
| 5725 | HeapObject* obj = HeapObject::cast(*p); |
| 5726 | |
| 5727 | Object* map = obj->map(); |
| 5728 | |
| 5729 | if (!map->IsHeapObject()) return; // visited before |
| 5730 | |
| 5731 | if (found_target_) return; // stop if target found |
| 5732 | object_stack_.Add(obj); |
| 5733 | if ((search_for_any_global_ && obj->IsJSGlobalObject()) || |
| 5734 | (!search_for_any_global_ && (obj == search_target_))) { |
| 5735 | found_target_ = true; |
| 5736 | return; |
| 5737 | } |
| 5738 | |
| 5739 | // not visited yet |
| 5740 | Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map)); |
| 5741 | |
| 5742 | Address map_addr = map_p->address(); |
| 5743 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5744 | obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_addr + kMarkTag)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5745 | |
| 5746 | MarkObjectRecursively(&map); |
| 5747 | |
| 5748 | MarkObjectVisitor mark_visitor(this); |
| 5749 | |
| 5750 | obj->IterateBody(map_p->instance_type(), obj->SizeFromMap(map_p), |
| 5751 | &mark_visitor); |
| 5752 | |
| 5753 | if (!found_target_) // don't pop if found the target |
| 5754 | object_stack_.RemoveLast(); |
| 5755 | } |
| 5756 | |
| 5757 | |
| 5758 | class UnmarkObjectVisitor : public ObjectVisitor { |
| 5759 | public: |
| 5760 | explicit UnmarkObjectVisitor(HeapDebugUtils* utils) : utils_(utils) { } |
| 5761 | |
| 5762 | void VisitPointers(Object** start, Object** end) { |
| 5763 | // Copy all HeapObject pointers in [start, end) |
| 5764 | for (Object** p = start; p < end; p++) { |
| 5765 | if ((*p)->IsHeapObject()) |
| 5766 | utils_->UnmarkObjectRecursively(p); |
| 5767 | } |
| 5768 | } |
| 5769 | |
| 5770 | HeapDebugUtils* utils_; |
| 5771 | }; |
| 5772 | |
| 5773 | |
| 5774 | void UnmarkObjectRecursively(Object** p) { |
| 5775 | if (!(*p)->IsHeapObject()) return; |
| 5776 | |
| 5777 | HeapObject* obj = HeapObject::cast(*p); |
| 5778 | |
| 5779 | Object* map = obj->map(); |
| 5780 | |
| 5781 | if (map->IsHeapObject()) return; // unmarked already |
| 5782 | |
| 5783 | Address map_addr = reinterpret_cast<Address>(map); |
| 5784 | |
| 5785 | map_addr -= kMarkTag; |
| 5786 | |
| 5787 | ASSERT_TAG_ALIGNED(map_addr); |
| 5788 | |
| 5789 | HeapObject* map_p = HeapObject::FromAddress(map_addr); |
| 5790 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5791 | obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_p)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5792 | |
| 5793 | UnmarkObjectRecursively(reinterpret_cast<Object**>(&map_p)); |
| 5794 | |
| 5795 | UnmarkObjectVisitor unmark_visitor(this); |
| 5796 | |
| 5797 | obj->IterateBody(Map::cast(map_p)->instance_type(), |
| 5798 | obj->SizeFromMap(Map::cast(map_p)), |
| 5799 | &unmark_visitor); |
| 5800 | } |
| 5801 | |
| 5802 | |
| 5803 | void MarkRootObjectRecursively(Object** root) { |
| 5804 | if (search_for_any_global_) { |
| 5805 | ASSERT(search_target_ == NULL); |
| 5806 | } else { |
| 5807 | ASSERT(search_target_->IsHeapObject()); |
| 5808 | } |
| 5809 | found_target_ = false; |
| 5810 | object_stack_.Clear(); |
| 5811 | |
| 5812 | MarkObjectRecursively(root); |
| 5813 | UnmarkObjectRecursively(root); |
| 5814 | |
| 5815 | if (found_target_) { |
| 5816 | PrintF("=====================================\n"); |
| 5817 | PrintF("==== Path to object ====\n"); |
| 5818 | PrintF("=====================================\n\n"); |
| 5819 | |
| 5820 | ASSERT(!object_stack_.is_empty()); |
| 5821 | for (int i = 0; i < object_stack_.length(); i++) { |
| 5822 | if (i > 0) PrintF("\n |\n |\n V\n\n"); |
| 5823 | Object* obj = object_stack_[i]; |
| 5824 | obj->Print(); |
| 5825 | } |
| 5826 | PrintF("=====================================\n"); |
| 5827 | } |
| 5828 | } |
| 5829 | |
| 5830 | // Helper class for visiting HeapObjects recursively. |
| 5831 | class MarkRootVisitor: public ObjectVisitor { |
| 5832 | public: |
| 5833 | explicit MarkRootVisitor(HeapDebugUtils* utils) : utils_(utils) { } |
| 5834 | |
| 5835 | void VisitPointers(Object** start, Object** end) { |
| 5836 | // Visit all HeapObject pointers in [start, end) |
| 5837 | for (Object** p = start; p < end; p++) { |
| 5838 | if ((*p)->IsHeapObject()) |
| 5839 | utils_->MarkRootObjectRecursively(p); |
| 5840 | } |
| 5841 | } |
| 5842 | |
| 5843 | HeapDebugUtils* utils_; |
| 5844 | }; |
| 5845 | |
| 5846 | bool search_for_any_global_; |
| 5847 | Object* search_target_; |
| 5848 | bool found_target_; |
| 5849 | List<Object*> object_stack_; |
| 5850 | Heap* heap_; |
| 5851 | |
| 5852 | friend class Heap; |
| 5853 | }; |
| 5854 | |
| 5855 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5856 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5857 | bool Heap::SetUp(bool create_heap_objects) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5858 | #ifdef DEBUG |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5859 | allocation_timeout_ = FLAG_gc_interval; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5860 | debug_utils_ = new HeapDebugUtils(this); |
| 5861 | #endif |
| 5862 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5863 | // Initialize heap spaces and initial maps and objects. Whenever something |
| 5864 | // goes wrong, just return false. The caller should check the results and |
| 5865 | // call Heap::TearDown() to release allocated memory. |
| 5866 | // |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5867 | // If the heap is not yet configured (e.g. through the API), configure it. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5868 | // Configuration is based on the flags new-space-size (really the semispace |
| 5869 | // size) and old-space-size if set or the initial values of semispace_size_ |
| 5870 | // and old_generation_size_ otherwise. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5871 | if (!configured_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5872 | if (!ConfigureHeapDefault()) return false; |
| 5873 | } |
| 5874 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5875 | gc_initializer_mutex.Pointer()->Lock(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5876 | static bool initialized_gc = false; |
| 5877 | if (!initialized_gc) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5878 | initialized_gc = true; |
| 5879 | InitializeScavengingVisitorsTables(); |
| 5880 | NewSpaceScavenger::Initialize(); |
| 5881 | MarkCompactCollector::Initialize(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5882 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5883 | gc_initializer_mutex.Pointer()->Unlock(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5884 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5885 | MarkMapPointersAsEncoded(false); |
| 5886 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5887 | // Set up memory allocator. |
| 5888 | if (!isolate_->memory_allocator()->SetUp(MaxReserved(), MaxExecutableSize())) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5889 | return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5890 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5891 | // Set up new space. |
| 5892 | if (!new_space_.SetUp(reserved_semispace_size_, max_semispace_size_)) { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 5893 | return false; |
| 5894 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5895 | |
| 5896 | // Initialize old pointer space. |
| 5897 | old_pointer_space_ = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5898 | new OldSpace(this, |
| 5899 | max_old_generation_size_, |
| 5900 | OLD_POINTER_SPACE, |
| 5901 | NOT_EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5902 | if (old_pointer_space_ == NULL) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5903 | if (!old_pointer_space_->SetUp()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5904 | |
| 5905 | // Initialize old data space. |
| 5906 | old_data_space_ = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5907 | new OldSpace(this, |
| 5908 | max_old_generation_size_, |
| 5909 | OLD_DATA_SPACE, |
| 5910 | NOT_EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5911 | if (old_data_space_ == NULL) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5912 | if (!old_data_space_->SetUp()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5913 | |
| 5914 | // Initialize the code space, set its maximum capacity to the old |
| 5915 | // generation size. It needs executable memory. |
| 5916 | // On 64-bit platform(s), we put all code objects in a 2 GB range of |
| 5917 | // virtual address space, so that they can call each other with near calls. |
| 5918 | if (code_range_size_ > 0) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5919 | if (!isolate_->code_range()->SetUp(code_range_size_)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5920 | return false; |
| 5921 | } |
| 5922 | } |
| 5923 | |
| 5924 | code_space_ = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5925 | new OldSpace(this, max_old_generation_size_, CODE_SPACE, EXECUTABLE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5926 | if (code_space_ == NULL) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5927 | if (!code_space_->SetUp()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5928 | |
| 5929 | // Initialize map space. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5930 | map_space_ = new MapSpace(this, max_old_generation_size_, MAP_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5931 | if (map_space_ == NULL) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5932 | if (!map_space_->SetUp()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5933 | |
| 5934 | // Initialize global property cell space. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5935 | cell_space_ = new CellSpace(this, max_old_generation_size_, CELL_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5936 | if (cell_space_ == NULL) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5937 | if (!cell_space_->SetUp()) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5938 | |
| 5939 | // The large object code space may contain code or data. We set the memory |
| 5940 | // to be non-executable here for safety, but this means we need to enable it |
| 5941 | // explicitly when allocating large code objects. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5942 | lo_space_ = new LargeObjectSpace(this, max_old_generation_size_, LO_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5943 | if (lo_space_ == NULL) return false; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5944 | if (!lo_space_->SetUp()) return false; |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 5945 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5946 | // Set up the seed that is used to randomize the string hash function. |
Ben Murdoch | c7cc028 | 2012-03-05 14:35:55 +0000 | [diff] [blame] | 5947 | ASSERT(hash_seed() == 0); |
| 5948 | if (FLAG_randomize_hashes) { |
| 5949 | if (FLAG_hash_seed == 0) { |
| 5950 | set_hash_seed( |
| 5951 | Smi::FromInt(V8::RandomPrivate(isolate()) & 0x3fffffff)); |
| 5952 | } else { |
| 5953 | set_hash_seed(Smi::FromInt(FLAG_hash_seed)); |
| 5954 | } |
| 5955 | } |
| 5956 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5957 | if (create_heap_objects) { |
| 5958 | // Create initial maps. |
| 5959 | if (!CreateInitialMaps()) return false; |
| 5960 | if (!CreateApiObjects()) return false; |
| 5961 | |
| 5962 | // Create initial objects |
| 5963 | if (!CreateInitialObjects()) return false; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 5964 | |
| 5965 | global_contexts_list_ = undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5968 | LOG(isolate_, IntPtrTEvent("heap-capacity", Capacity())); |
| 5969 | LOG(isolate_, IntPtrTEvent("heap-available", Available())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5970 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 5971 | store_buffer()->SetUp(); |
| 5972 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5973 | return true; |
| 5974 | } |
| 5975 | |
| 5976 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5977 | void Heap::SetStackLimits() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5978 | ASSERT(isolate_ != NULL); |
| 5979 | ASSERT(isolate_ == isolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5980 | // On 64 bit machines, pointers are generally out of range of Smis. We write |
| 5981 | // something that looks like an out of range Smi to the GC. |
| 5982 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5983 | // Set up the special root array entries containing the stack limits. |
| 5984 | // These are actually addresses, but the tag makes the GC ignore it. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5985 | roots_[kStackLimitRootIndex] = |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5986 | reinterpret_cast<Object*>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5987 | (isolate_->stack_guard()->jslimit() & ~kSmiTagMask) | kSmiTag); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5988 | roots_[kRealStackLimitRootIndex] = |
| 5989 | reinterpret_cast<Object*>( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5990 | (isolate_->stack_guard()->real_jslimit() & ~kSmiTagMask) | kSmiTag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | |
| 5994 | void Heap::TearDown() { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 5995 | if (FLAG_print_cumulative_gc_stat) { |
| 5996 | PrintF("\n\n"); |
| 5997 | PrintF("gc_count=%d ", gc_count_); |
| 5998 | PrintF("mark_sweep_count=%d ", ms_count_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 5999 | PrintF("max_gc_pause=%d ", get_max_gc_pause()); |
| 6000 | PrintF("min_in_mutator=%d ", get_min_in_mutator()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6001 | PrintF("max_alive_after_gc=%" V8_PTR_PREFIX "d ", |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6002 | get_max_alive_after_gc()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6003 | PrintF("\n\n"); |
| 6004 | } |
| 6005 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6006 | isolate_->global_handles()->TearDown(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6007 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6008 | external_string_table_.TearDown(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6009 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6010 | new_space_.TearDown(); |
| 6011 | |
| 6012 | if (old_pointer_space_ != NULL) { |
| 6013 | old_pointer_space_->TearDown(); |
| 6014 | delete old_pointer_space_; |
| 6015 | old_pointer_space_ = NULL; |
| 6016 | } |
| 6017 | |
| 6018 | if (old_data_space_ != NULL) { |
| 6019 | old_data_space_->TearDown(); |
| 6020 | delete old_data_space_; |
| 6021 | old_data_space_ = NULL; |
| 6022 | } |
| 6023 | |
| 6024 | if (code_space_ != NULL) { |
| 6025 | code_space_->TearDown(); |
| 6026 | delete code_space_; |
| 6027 | code_space_ = NULL; |
| 6028 | } |
| 6029 | |
| 6030 | if (map_space_ != NULL) { |
| 6031 | map_space_->TearDown(); |
| 6032 | delete map_space_; |
| 6033 | map_space_ = NULL; |
| 6034 | } |
| 6035 | |
| 6036 | if (cell_space_ != NULL) { |
| 6037 | cell_space_->TearDown(); |
| 6038 | delete cell_space_; |
| 6039 | cell_space_ = NULL; |
| 6040 | } |
| 6041 | |
| 6042 | if (lo_space_ != NULL) { |
| 6043 | lo_space_->TearDown(); |
| 6044 | delete lo_space_; |
| 6045 | lo_space_ = NULL; |
| 6046 | } |
| 6047 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6048 | store_buffer()->TearDown(); |
| 6049 | incremental_marking()->TearDown(); |
| 6050 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6051 | isolate_->memory_allocator()->TearDown(); |
| 6052 | |
| 6053 | #ifdef DEBUG |
| 6054 | delete debug_utils_; |
| 6055 | debug_utils_ = NULL; |
| 6056 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | |
| 6060 | void Heap::Shrink() { |
| 6061 | // Try to shrink all paged spaces. |
| 6062 | PagedSpaces spaces; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6063 | for (PagedSpace* space = spaces.next(); |
| 6064 | space != NULL; |
| 6065 | space = spaces.next()) { |
| 6066 | space->ReleaseAllUnusedPages(); |
| 6067 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 6071 | void Heap::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) { |
| 6072 | ASSERT(callback != NULL); |
| 6073 | GCPrologueCallbackPair pair(callback, gc_type); |
| 6074 | ASSERT(!gc_prologue_callbacks_.Contains(pair)); |
| 6075 | return gc_prologue_callbacks_.Add(pair); |
| 6076 | } |
| 6077 | |
| 6078 | |
| 6079 | void Heap::RemoveGCPrologueCallback(GCPrologueCallback callback) { |
| 6080 | ASSERT(callback != NULL); |
| 6081 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 6082 | if (gc_prologue_callbacks_[i].callback == callback) { |
| 6083 | gc_prologue_callbacks_.Remove(i); |
| 6084 | return; |
| 6085 | } |
| 6086 | } |
| 6087 | UNREACHABLE(); |
| 6088 | } |
| 6089 | |
| 6090 | |
| 6091 | void Heap::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) { |
| 6092 | ASSERT(callback != NULL); |
| 6093 | GCEpilogueCallbackPair pair(callback, gc_type); |
| 6094 | ASSERT(!gc_epilogue_callbacks_.Contains(pair)); |
| 6095 | return gc_epilogue_callbacks_.Add(pair); |
| 6096 | } |
| 6097 | |
| 6098 | |
| 6099 | void Heap::RemoveGCEpilogueCallback(GCEpilogueCallback callback) { |
| 6100 | ASSERT(callback != NULL); |
| 6101 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 6102 | if (gc_epilogue_callbacks_[i].callback == callback) { |
| 6103 | gc_epilogue_callbacks_.Remove(i); |
| 6104 | return; |
| 6105 | } |
| 6106 | } |
| 6107 | UNREACHABLE(); |
| 6108 | } |
| 6109 | |
| 6110 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6111 | #ifdef DEBUG |
| 6112 | |
| 6113 | class PrintHandleVisitor: public ObjectVisitor { |
| 6114 | public: |
| 6115 | void VisitPointers(Object** start, Object** end) { |
| 6116 | for (Object** p = start; p < end; p++) |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6117 | PrintF(" handle %p to %p\n", |
| 6118 | reinterpret_cast<void*>(p), |
| 6119 | reinterpret_cast<void*>(*p)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6120 | } |
| 6121 | }; |
| 6122 | |
| 6123 | void Heap::PrintHandles() { |
| 6124 | PrintF("Handles:\n"); |
| 6125 | PrintHandleVisitor v; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6126 | isolate_->handle_scope_implementer()->Iterate(&v); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
| 6129 | #endif |
| 6130 | |
| 6131 | |
| 6132 | Space* AllSpaces::next() { |
| 6133 | switch (counter_++) { |
| 6134 | case NEW_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6135 | return HEAP->new_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6136 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6137 | return HEAP->old_pointer_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6138 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6139 | return HEAP->old_data_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6140 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6141 | return HEAP->code_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6142 | case MAP_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6143 | return HEAP->map_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6144 | case CELL_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6145 | return HEAP->cell_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6146 | case LO_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6147 | return HEAP->lo_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6148 | default: |
| 6149 | return NULL; |
| 6150 | } |
| 6151 | } |
| 6152 | |
| 6153 | |
| 6154 | PagedSpace* PagedSpaces::next() { |
| 6155 | switch (counter_++) { |
| 6156 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6157 | return HEAP->old_pointer_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6158 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6159 | return HEAP->old_data_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6160 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6161 | return HEAP->code_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6162 | case MAP_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6163 | return HEAP->map_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6164 | case CELL_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6165 | return HEAP->cell_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6166 | default: |
| 6167 | return NULL; |
| 6168 | } |
| 6169 | } |
| 6170 | |
| 6171 | |
| 6172 | |
| 6173 | OldSpace* OldSpaces::next() { |
| 6174 | switch (counter_++) { |
| 6175 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6176 | return HEAP->old_pointer_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6177 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6178 | return HEAP->old_data_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6179 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6180 | return HEAP->code_space(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6181 | default: |
| 6182 | return NULL; |
| 6183 | } |
| 6184 | } |
| 6185 | |
| 6186 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6187 | SpaceIterator::SpaceIterator() |
| 6188 | : current_space_(FIRST_SPACE), |
| 6189 | iterator_(NULL), |
| 6190 | size_func_(NULL) { |
| 6191 | } |
| 6192 | |
| 6193 | |
| 6194 | SpaceIterator::SpaceIterator(HeapObjectCallback size_func) |
| 6195 | : current_space_(FIRST_SPACE), |
| 6196 | iterator_(NULL), |
| 6197 | size_func_(size_func) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
| 6200 | |
| 6201 | SpaceIterator::~SpaceIterator() { |
| 6202 | // Delete active iterator if any. |
| 6203 | delete iterator_; |
| 6204 | } |
| 6205 | |
| 6206 | |
| 6207 | bool SpaceIterator::has_next() { |
| 6208 | // Iterate until no more spaces. |
| 6209 | return current_space_ != LAST_SPACE; |
| 6210 | } |
| 6211 | |
| 6212 | |
| 6213 | ObjectIterator* SpaceIterator::next() { |
| 6214 | if (iterator_ != NULL) { |
| 6215 | delete iterator_; |
| 6216 | iterator_ = NULL; |
| 6217 | // Move to the next space |
| 6218 | current_space_++; |
| 6219 | if (current_space_ > LAST_SPACE) { |
| 6220 | return NULL; |
| 6221 | } |
| 6222 | } |
| 6223 | |
| 6224 | // Return iterator for the new current space. |
| 6225 | return CreateIterator(); |
| 6226 | } |
| 6227 | |
| 6228 | |
| 6229 | // Create an iterator for the space to iterate. |
| 6230 | ObjectIterator* SpaceIterator::CreateIterator() { |
| 6231 | ASSERT(iterator_ == NULL); |
| 6232 | |
| 6233 | switch (current_space_) { |
| 6234 | case NEW_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6235 | iterator_ = new SemiSpaceIterator(HEAP->new_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6236 | break; |
| 6237 | case OLD_POINTER_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6238 | iterator_ = new HeapObjectIterator(HEAP->old_pointer_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6239 | break; |
| 6240 | case OLD_DATA_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6241 | iterator_ = new HeapObjectIterator(HEAP->old_data_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6242 | break; |
| 6243 | case CODE_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6244 | iterator_ = new HeapObjectIterator(HEAP->code_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6245 | break; |
| 6246 | case MAP_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6247 | iterator_ = new HeapObjectIterator(HEAP->map_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6248 | break; |
| 6249 | case CELL_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6250 | iterator_ = new HeapObjectIterator(HEAP->cell_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6251 | break; |
| 6252 | case LO_SPACE: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6253 | iterator_ = new LargeObjectIterator(HEAP->lo_space(), size_func_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6254 | break; |
| 6255 | } |
| 6256 | |
| 6257 | // Return the newly allocated iterator; |
| 6258 | ASSERT(iterator_ != NULL); |
| 6259 | return iterator_; |
| 6260 | } |
| 6261 | |
| 6262 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6263 | class HeapObjectsFilter { |
| 6264 | public: |
| 6265 | virtual ~HeapObjectsFilter() {} |
| 6266 | virtual bool SkipObject(HeapObject* object) = 0; |
| 6267 | }; |
| 6268 | |
| 6269 | |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 6270 | class UnreachableObjectsFilter : public HeapObjectsFilter { |
| 6271 | public: |
| 6272 | UnreachableObjectsFilter() { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6273 | MarkReachableObjects(); |
| 6274 | } |
| 6275 | |
| 6276 | ~UnreachableObjectsFilter() { |
| 6277 | Isolate::Current()->heap()->mark_compact_collector()->ClearMarkbits(); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 6278 | } |
| 6279 | |
| 6280 | bool SkipObject(HeapObject* object) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6281 | MarkBit mark_bit = Marking::MarkBitFrom(object); |
| 6282 | return !mark_bit.Get(); |
Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame] | 6283 | } |
| 6284 | |
| 6285 | private: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6286 | class MarkingVisitor : public ObjectVisitor { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6287 | public: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6288 | MarkingVisitor() : marking_stack_(10) {} |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6289 | |
| 6290 | void VisitPointers(Object** start, Object** end) { |
| 6291 | for (Object** p = start; p < end; p++) { |
| 6292 | if (!(*p)->IsHeapObject()) continue; |
| 6293 | HeapObject* obj = HeapObject::cast(*p); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6294 | MarkBit mark_bit = Marking::MarkBitFrom(obj); |
| 6295 | if (!mark_bit.Get()) { |
| 6296 | mark_bit.Set(); |
| 6297 | marking_stack_.Add(obj); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6298 | } |
| 6299 | } |
| 6300 | } |
| 6301 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6302 | void TransitiveClosure() { |
| 6303 | while (!marking_stack_.is_empty()) { |
| 6304 | HeapObject* obj = marking_stack_.RemoveLast(); |
| 6305 | obj->Iterate(this); |
| 6306 | } |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6307 | } |
| 6308 | |
| 6309 | private: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6310 | List<HeapObject*> marking_stack_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6311 | }; |
| 6312 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6313 | void MarkReachableObjects() { |
| 6314 | Heap* heap = Isolate::Current()->heap(); |
| 6315 | MarkingVisitor visitor; |
| 6316 | heap->IterateRoots(&visitor, VISIT_ALL); |
| 6317 | visitor.TransitiveClosure(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6318 | } |
| 6319 | |
| 6320 | AssertNoAllocation no_alloc; |
| 6321 | }; |
| 6322 | |
| 6323 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6324 | HeapIterator::HeapIterator() |
| 6325 | : filtering_(HeapIterator::kNoFiltering), |
| 6326 | filter_(NULL) { |
| 6327 | Init(); |
| 6328 | } |
| 6329 | |
| 6330 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6331 | HeapIterator::HeapIterator(HeapIterator::HeapObjectsFiltering filtering) |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6332 | : filtering_(filtering), |
| 6333 | filter_(NULL) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6334 | Init(); |
| 6335 | } |
| 6336 | |
| 6337 | |
| 6338 | HeapIterator::~HeapIterator() { |
| 6339 | Shutdown(); |
| 6340 | } |
| 6341 | |
| 6342 | |
| 6343 | void HeapIterator::Init() { |
| 6344 | // Start the iteration. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6345 | space_iterator_ = new SpaceIterator; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6346 | switch (filtering_) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6347 | case kFilterUnreachable: |
| 6348 | filter_ = new UnreachableObjectsFilter; |
| 6349 | break; |
| 6350 | default: |
| 6351 | break; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6352 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6353 | object_iterator_ = space_iterator_->next(); |
| 6354 | } |
| 6355 | |
| 6356 | |
| 6357 | void HeapIterator::Shutdown() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6358 | #ifdef DEBUG |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6359 | // Assert that in filtering mode we have iterated through all |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6360 | // objects. Otherwise, heap will be left in an inconsistent state. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6361 | if (filtering_ != kNoFiltering) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6362 | ASSERT(object_iterator_ == NULL); |
| 6363 | } |
| 6364 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6365 | // Make sure the last iterator is deallocated. |
| 6366 | delete space_iterator_; |
| 6367 | space_iterator_ = NULL; |
| 6368 | object_iterator_ = NULL; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6369 | delete filter_; |
| 6370 | filter_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6374 | HeapObject* HeapIterator::next() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6375 | if (filter_ == NULL) return NextObject(); |
| 6376 | |
| 6377 | HeapObject* obj = NextObject(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 6378 | while (obj != NULL && filter_->SkipObject(obj)) obj = NextObject(); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 6379 | return obj; |
| 6380 | } |
| 6381 | |
| 6382 | |
| 6383 | HeapObject* HeapIterator::NextObject() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6384 | // No iterator means we are done. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6385 | if (object_iterator_ == NULL) return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6386 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6387 | if (HeapObject* obj = object_iterator_->next_object()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6388 | // If the current iterator has more objects we are fine. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6389 | return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6390 | } else { |
| 6391 | // Go though the spaces looking for one that has objects. |
| 6392 | while (space_iterator_->has_next()) { |
| 6393 | object_iterator_ = space_iterator_->next(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6394 | if (HeapObject* obj = object_iterator_->next_object()) { |
| 6395 | return obj; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6396 | } |
| 6397 | } |
| 6398 | } |
| 6399 | // Done with the last space. |
| 6400 | object_iterator_ = NULL; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 6401 | return NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6402 | } |
| 6403 | |
| 6404 | |
| 6405 | void HeapIterator::reset() { |
| 6406 | // Restart the iterator. |
| 6407 | Shutdown(); |
| 6408 | Init(); |
| 6409 | } |
| 6410 | |
| 6411 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6412 | #if defined(DEBUG) || defined(LIVE_OBJECT_LIST) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6413 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6414 | Object* const PathTracer::kAnyGlobalObject = reinterpret_cast<Object*>(NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6415 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6416 | class PathTracer::MarkVisitor: public ObjectVisitor { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6417 | public: |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6418 | explicit MarkVisitor(PathTracer* tracer) : tracer_(tracer) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6419 | void VisitPointers(Object** start, Object** end) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6420 | // Scan all HeapObject pointers in [start, end) |
| 6421 | for (Object** p = start; !tracer_->found() && (p < end); p++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6422 | if ((*p)->IsHeapObject()) |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6423 | tracer_->MarkRecursively(p, this); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6424 | } |
| 6425 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6426 | |
| 6427 | private: |
| 6428 | PathTracer* tracer_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6429 | }; |
| 6430 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6431 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6432 | class PathTracer::UnmarkVisitor: public ObjectVisitor { |
| 6433 | public: |
| 6434 | explicit UnmarkVisitor(PathTracer* tracer) : tracer_(tracer) {} |
| 6435 | void VisitPointers(Object** start, Object** end) { |
| 6436 | // Scan all HeapObject pointers in [start, end) |
| 6437 | for (Object** p = start; p < end; p++) { |
| 6438 | if ((*p)->IsHeapObject()) |
| 6439 | tracer_->UnmarkRecursively(p, this); |
| 6440 | } |
| 6441 | } |
| 6442 | |
| 6443 | private: |
| 6444 | PathTracer* tracer_; |
| 6445 | }; |
| 6446 | |
| 6447 | |
| 6448 | void PathTracer::VisitPointers(Object** start, Object** end) { |
| 6449 | bool done = ((what_to_find_ == FIND_FIRST) && found_target_); |
| 6450 | // Visit all HeapObject pointers in [start, end) |
| 6451 | for (Object** p = start; !done && (p < end); p++) { |
| 6452 | if ((*p)->IsHeapObject()) { |
| 6453 | TracePathFrom(p); |
| 6454 | done = ((what_to_find_ == FIND_FIRST) && found_target_); |
| 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | |
| 6459 | |
| 6460 | void PathTracer::Reset() { |
| 6461 | found_target_ = false; |
| 6462 | object_stack_.Clear(); |
| 6463 | } |
| 6464 | |
| 6465 | |
| 6466 | void PathTracer::TracePathFrom(Object** root) { |
| 6467 | ASSERT((search_target_ == kAnyGlobalObject) || |
| 6468 | search_target_->IsHeapObject()); |
| 6469 | found_target_in_trace_ = false; |
| 6470 | object_stack_.Clear(); |
| 6471 | |
| 6472 | MarkVisitor mark_visitor(this); |
| 6473 | MarkRecursively(root, &mark_visitor); |
| 6474 | |
| 6475 | UnmarkVisitor unmark_visitor(this); |
| 6476 | UnmarkRecursively(root, &unmark_visitor); |
| 6477 | |
| 6478 | ProcessResults(); |
| 6479 | } |
| 6480 | |
| 6481 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6482 | static bool SafeIsGlobalContext(HeapObject* obj) { |
| 6483 | return obj->map() == obj->GetHeap()->raw_unchecked_global_context_map(); |
| 6484 | } |
| 6485 | |
| 6486 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6487 | void PathTracer::MarkRecursively(Object** p, MarkVisitor* mark_visitor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6488 | if (!(*p)->IsHeapObject()) return; |
| 6489 | |
| 6490 | HeapObject* obj = HeapObject::cast(*p); |
| 6491 | |
| 6492 | Object* map = obj->map(); |
| 6493 | |
| 6494 | if (!map->IsHeapObject()) return; // visited before |
| 6495 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6496 | if (found_target_in_trace_) return; // stop if target found |
| 6497 | object_stack_.Add(obj); |
| 6498 | if (((search_target_ == kAnyGlobalObject) && obj->IsJSGlobalObject()) || |
| 6499 | (obj == search_target_)) { |
| 6500 | found_target_in_trace_ = true; |
| 6501 | found_target_ = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6502 | return; |
| 6503 | } |
| 6504 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6505 | bool is_global_context = SafeIsGlobalContext(obj); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6506 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6507 | // not visited yet |
| 6508 | Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map)); |
| 6509 | |
| 6510 | Address map_addr = map_p->address(); |
| 6511 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6512 | obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_addr + kMarkTag)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6513 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6514 | // Scan the object body. |
| 6515 | if (is_global_context && (visit_mode_ == VISIT_ONLY_STRONG)) { |
| 6516 | // This is specialized to scan Context's properly. |
| 6517 | Object** start = reinterpret_cast<Object**>(obj->address() + |
| 6518 | Context::kHeaderSize); |
| 6519 | Object** end = reinterpret_cast<Object**>(obj->address() + |
| 6520 | Context::kHeaderSize + Context::FIRST_WEAK_SLOT * kPointerSize); |
| 6521 | mark_visitor->VisitPointers(start, end); |
| 6522 | } else { |
| 6523 | obj->IterateBody(map_p->instance_type(), |
| 6524 | obj->SizeFromMap(map_p), |
| 6525 | mark_visitor); |
| 6526 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6527 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6528 | // Scan the map after the body because the body is a lot more interesting |
| 6529 | // when doing leak detection. |
| 6530 | MarkRecursively(&map, mark_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6531 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6532 | if (!found_target_in_trace_) // don't pop if found the target |
| 6533 | object_stack_.RemoveLast(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
| 6536 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6537 | void PathTracer::UnmarkRecursively(Object** p, UnmarkVisitor* unmark_visitor) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6538 | if (!(*p)->IsHeapObject()) return; |
| 6539 | |
| 6540 | HeapObject* obj = HeapObject::cast(*p); |
| 6541 | |
| 6542 | Object* map = obj->map(); |
| 6543 | |
| 6544 | if (map->IsHeapObject()) return; // unmarked already |
| 6545 | |
| 6546 | Address map_addr = reinterpret_cast<Address>(map); |
| 6547 | |
| 6548 | map_addr -= kMarkTag; |
| 6549 | |
| 6550 | ASSERT_TAG_ALIGNED(map_addr); |
| 6551 | |
| 6552 | HeapObject* map_p = HeapObject::FromAddress(map_addr); |
| 6553 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6554 | obj->set_map_no_write_barrier(reinterpret_cast<Map*>(map_p)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6555 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6556 | UnmarkRecursively(reinterpret_cast<Object**>(&map_p), unmark_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6557 | |
| 6558 | obj->IterateBody(Map::cast(map_p)->instance_type(), |
| 6559 | obj->SizeFromMap(Map::cast(map_p)), |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6560 | unmark_visitor); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6561 | } |
| 6562 | |
| 6563 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6564 | void PathTracer::ProcessResults() { |
| 6565 | if (found_target_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6566 | PrintF("=====================================\n"); |
| 6567 | PrintF("==== Path to object ====\n"); |
| 6568 | PrintF("=====================================\n\n"); |
| 6569 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6570 | ASSERT(!object_stack_.is_empty()); |
| 6571 | for (int i = 0; i < object_stack_.length(); i++) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6572 | if (i > 0) PrintF("\n |\n |\n V\n\n"); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6573 | Object* obj = object_stack_[i]; |
| 6574 | #ifdef OBJECT_PRINT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6575 | obj->Print(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6576 | #else |
| 6577 | obj->ShortPrint(); |
| 6578 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6579 | } |
| 6580 | PrintF("=====================================\n"); |
| 6581 | } |
| 6582 | } |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6583 | #endif // DEBUG || LIVE_OBJECT_LIST |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6584 | |
| 6585 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6586 | #ifdef DEBUG |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6587 | // Triggers a depth-first traversal of reachable objects from roots |
| 6588 | // and finds a path to a specific heap object and prints it. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6589 | void Heap::TracePathToObject(Object* target) { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6590 | PathTracer tracer(target, PathTracer::FIND_ALL, VISIT_ALL); |
| 6591 | IterateRoots(&tracer, VISIT_ONLY_STRONG); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | |
| 6595 | // Triggers a depth-first traversal of reachable objects from roots |
| 6596 | // and finds a path to any global object and prints it. Useful for |
| 6597 | // determining the source for leaks of global objects. |
| 6598 | void Heap::TracePathToGlobal() { |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 6599 | PathTracer tracer(PathTracer::kAnyGlobalObject, |
| 6600 | PathTracer::FIND_ALL, |
| 6601 | VISIT_ALL); |
| 6602 | IterateRoots(&tracer, VISIT_ONLY_STRONG); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6603 | } |
| 6604 | #endif |
| 6605 | |
| 6606 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6607 | static intptr_t CountTotalHolesSize() { |
| 6608 | intptr_t holes_size = 0; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6609 | OldSpaces spaces; |
| 6610 | for (OldSpace* space = spaces.next(); |
| 6611 | space != NULL; |
| 6612 | space = spaces.next()) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6613 | holes_size += space->Waste() + space->Available(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6614 | } |
| 6615 | return holes_size; |
| 6616 | } |
| 6617 | |
| 6618 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6619 | GCTracer::GCTracer(Heap* heap, |
| 6620 | const char* gc_reason, |
| 6621 | const char* collector_reason) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6622 | : start_time_(0.0), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6623 | start_object_size_(0), |
| 6624 | start_memory_size_(0), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6625 | gc_count_(0), |
| 6626 | full_gc_count_(0), |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6627 | allocated_since_last_gc_(0), |
| 6628 | spent_in_mutator_(0), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6629 | promoted_objects_size_(0), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6630 | heap_(heap), |
| 6631 | gc_reason_(gc_reason), |
| 6632 | collector_reason_(collector_reason) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6633 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6634 | start_time_ = OS::TimeCurrentMillis(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6635 | start_object_size_ = heap_->SizeOfObjects(); |
| 6636 | start_memory_size_ = heap_->isolate()->memory_allocator()->Size(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6637 | |
| 6638 | for (int i = 0; i < Scope::kNumberOfScopes; i++) { |
| 6639 | scopes_[i] = 0; |
| 6640 | } |
| 6641 | |
| 6642 | in_free_list_or_wasted_before_gc_ = CountTotalHolesSize(); |
| 6643 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6644 | allocated_since_last_gc_ = |
| 6645 | heap_->SizeOfObjects() - heap_->alive_after_last_gc_; |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6646 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6647 | if (heap_->last_gc_end_timestamp_ > 0) { |
| 6648 | 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] | 6649 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6650 | |
| 6651 | steps_count_ = heap_->incremental_marking()->steps_count(); |
| 6652 | steps_took_ = heap_->incremental_marking()->steps_took(); |
| 6653 | longest_step_ = heap_->incremental_marking()->longest_step(); |
| 6654 | steps_count_since_last_gc_ = |
| 6655 | heap_->incremental_marking()->steps_count_since_last_gc(); |
| 6656 | steps_took_since_last_gc_ = |
| 6657 | heap_->incremental_marking()->steps_took_since_last_gc(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6658 | } |
| 6659 | |
| 6660 | |
| 6661 | GCTracer::~GCTracer() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6662 | // Printf ONE line iff flag is set. |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6663 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
| 6664 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6665 | bool first_gc = (heap_->last_gc_end_timestamp_ == 0); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6666 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6667 | heap_->alive_after_last_gc_ = heap_->SizeOfObjects(); |
| 6668 | heap_->last_gc_end_timestamp_ = OS::TimeCurrentMillis(); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6669 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6670 | int time = static_cast<int>(heap_->last_gc_end_timestamp_ - start_time_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6671 | |
| 6672 | // Update cumulative GC statistics if required. |
| 6673 | if (FLAG_print_cumulative_gc_stat) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6674 | heap_->max_gc_pause_ = Max(heap_->max_gc_pause_, time); |
| 6675 | heap_->max_alive_after_gc_ = Max(heap_->max_alive_after_gc_, |
| 6676 | heap_->alive_after_last_gc_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6677 | if (!first_gc) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6678 | heap_->min_in_mutator_ = Min(heap_->min_in_mutator_, |
| 6679 | static_cast<int>(spent_in_mutator_)); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6680 | } |
| 6681 | } |
| 6682 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6683 | PrintF("%8.0f ms: ", heap_->isolate()->time_millis_since_init()); |
| 6684 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6685 | if (!FLAG_trace_gc_nvp) { |
| 6686 | int external_time = static_cast<int>(scopes_[Scope::EXTERNAL]); |
| 6687 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6688 | double end_memory_size_mb = |
| 6689 | static_cast<double>(heap_->isolate()->memory_allocator()->Size()) / MB; |
| 6690 | |
| 6691 | PrintF("%s %.1f (%.1f) -> %.1f (%.1f) MB, ", |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6692 | CollectorString(), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6693 | static_cast<double>(start_object_size_) / MB, |
| 6694 | static_cast<double>(start_memory_size_) / MB, |
| 6695 | SizeOfHeapObjects(), |
| 6696 | end_memory_size_mb); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6697 | |
| 6698 | if (external_time > 0) PrintF("%d / ", external_time); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6699 | PrintF("%d ms", time); |
| 6700 | if (steps_count_ > 0) { |
| 6701 | if (collector_ == SCAVENGER) { |
| 6702 | PrintF(" (+ %d ms in %d steps since last GC)", |
| 6703 | static_cast<int>(steps_took_since_last_gc_), |
| 6704 | steps_count_since_last_gc_); |
| 6705 | } else { |
| 6706 | PrintF(" (+ %d ms in %d steps since start of marking, " |
| 6707 | "biggest step %f ms)", |
| 6708 | static_cast<int>(steps_took_), |
| 6709 | steps_count_, |
| 6710 | longest_step_); |
| 6711 | } |
| 6712 | } |
| 6713 | |
| 6714 | if (gc_reason_ != NULL) { |
| 6715 | PrintF(" [%s]", gc_reason_); |
| 6716 | } |
| 6717 | |
| 6718 | if (collector_reason_ != NULL) { |
| 6719 | PrintF(" [%s]", collector_reason_); |
| 6720 | } |
| 6721 | |
| 6722 | PrintF(".\n"); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6723 | } else { |
| 6724 | PrintF("pause=%d ", time); |
| 6725 | PrintF("mutator=%d ", |
| 6726 | static_cast<int>(spent_in_mutator_)); |
| 6727 | |
| 6728 | PrintF("gc="); |
| 6729 | switch (collector_) { |
| 6730 | case SCAVENGER: |
| 6731 | PrintF("s"); |
| 6732 | break; |
| 6733 | case MARK_COMPACTOR: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6734 | PrintF("ms"); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6735 | break; |
| 6736 | default: |
| 6737 | UNREACHABLE(); |
| 6738 | } |
| 6739 | PrintF(" "); |
| 6740 | |
| 6741 | PrintF("external=%d ", static_cast<int>(scopes_[Scope::EXTERNAL])); |
| 6742 | PrintF("mark=%d ", static_cast<int>(scopes_[Scope::MC_MARK])); |
| 6743 | PrintF("sweep=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP])); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 6744 | PrintF("sweepns=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP_NEWSPACE])); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6745 | PrintF("evacuate=%d ", static_cast<int>(scopes_[Scope::MC_EVACUATE_PAGES])); |
| 6746 | PrintF("new_new=%d ", |
| 6747 | static_cast<int>(scopes_[Scope::MC_UPDATE_NEW_TO_NEW_POINTERS])); |
| 6748 | PrintF("root_new=%d ", |
| 6749 | static_cast<int>(scopes_[Scope::MC_UPDATE_ROOT_TO_NEW_POINTERS])); |
| 6750 | PrintF("old_new=%d ", |
| 6751 | static_cast<int>(scopes_[Scope::MC_UPDATE_OLD_TO_NEW_POINTERS])); |
| 6752 | PrintF("compaction_ptrs=%d ", |
| 6753 | static_cast<int>(scopes_[Scope::MC_UPDATE_POINTERS_TO_EVACUATED])); |
| 6754 | PrintF("intracompaction_ptrs=%d ", static_cast<int>(scopes_[ |
| 6755 | Scope::MC_UPDATE_POINTERS_BETWEEN_EVACUATED])); |
| 6756 | PrintF("misc_compaction=%d ", |
| 6757 | static_cast<int>(scopes_[Scope::MC_UPDATE_MISC_POINTERS])); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6758 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6759 | PrintF("total_size_before=%" V8_PTR_PREFIX "d ", start_object_size_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6760 | PrintF("total_size_after=%" V8_PTR_PREFIX "d ", heap_->SizeOfObjects()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6761 | PrintF("holes_size_before=%" V8_PTR_PREFIX "d ", |
| 6762 | in_free_list_or_wasted_before_gc_); |
| 6763 | PrintF("holes_size_after=%" V8_PTR_PREFIX "d ", CountTotalHolesSize()); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6764 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6765 | PrintF("allocated=%" V8_PTR_PREFIX "d ", allocated_since_last_gc_); |
| 6766 | PrintF("promoted=%" V8_PTR_PREFIX "d ", promoted_objects_size_); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6767 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6768 | if (collector_ == SCAVENGER) { |
| 6769 | PrintF("stepscount=%d ", steps_count_since_last_gc_); |
| 6770 | PrintF("stepstook=%d ", static_cast<int>(steps_took_since_last_gc_)); |
| 6771 | } else { |
| 6772 | PrintF("stepscount=%d ", steps_count_); |
| 6773 | PrintF("stepstook=%d ", static_cast<int>(steps_took_)); |
| 6774 | } |
| 6775 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 6776 | PrintF("\n"); |
| 6777 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6778 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6779 | heap_->PrintShortHeapStatistics(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | |
| 6783 | const char* GCTracer::CollectorString() { |
| 6784 | switch (collector_) { |
| 6785 | case SCAVENGER: |
| 6786 | return "Scavenge"; |
| 6787 | case MARK_COMPACTOR: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6788 | return "Mark-sweep"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6789 | } |
| 6790 | return "Unknown GC"; |
| 6791 | } |
| 6792 | |
| 6793 | |
| 6794 | int KeyedLookupCache::Hash(Map* map, String* name) { |
| 6795 | // Uses only lower 32 bits if pointers are larger. |
| 6796 | uintptr_t addr_hash = |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6797 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)) >> kMapHashShift; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 6798 | return static_cast<uint32_t>((addr_hash ^ name->Hash()) & kCapacityMask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6799 | } |
| 6800 | |
| 6801 | |
| 6802 | int KeyedLookupCache::Lookup(Map* map, String* name) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6803 | int index = (Hash(map, name) & kHashMask); |
| 6804 | for (int i = 0; i < kEntriesPerBucket; i++) { |
| 6805 | Key& key = keys_[index + i]; |
| 6806 | if ((key.map == map) && key.name->Equals(name)) { |
| 6807 | return field_offsets_[index + i]; |
| 6808 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6809 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6810 | return kNotFound; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6811 | } |
| 6812 | |
| 6813 | |
| 6814 | void KeyedLookupCache::Update(Map* map, String* name, int field_offset) { |
| 6815 | String* symbol; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6816 | if (HEAP->LookupSymbolIfExists(name, &symbol)) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6817 | int index = (Hash(map, symbol) & kHashMask); |
| 6818 | // After a GC there will be free slots, so we use them in order (this may |
| 6819 | // help to get the most frequently used one in position 0). |
| 6820 | for (int i = 0; i< kEntriesPerBucket; i++) { |
| 6821 | Key& key = keys_[index]; |
| 6822 | Object* free_entry_indicator = NULL; |
| 6823 | if (key.map == free_entry_indicator) { |
| 6824 | key.map = map; |
| 6825 | key.name = symbol; |
| 6826 | field_offsets_[index + i] = field_offset; |
| 6827 | return; |
| 6828 | } |
| 6829 | } |
| 6830 | // No free entry found in this bucket, so we move them all down one and |
| 6831 | // put the new entry at position zero. |
| 6832 | for (int i = kEntriesPerBucket - 1; i > 0; i--) { |
| 6833 | Key& key = keys_[index + i]; |
| 6834 | Key& key2 = keys_[index + i - 1]; |
| 6835 | key = key2; |
| 6836 | field_offsets_[index + i] = field_offsets_[index + i - 1]; |
| 6837 | } |
| 6838 | |
| 6839 | // Write the new first entry. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6840 | Key& key = keys_[index]; |
| 6841 | key.map = map; |
| 6842 | key.name = symbol; |
| 6843 | field_offsets_[index] = field_offset; |
| 6844 | } |
| 6845 | } |
| 6846 | |
| 6847 | |
| 6848 | void KeyedLookupCache::Clear() { |
| 6849 | for (int index = 0; index < kLength; index++) keys_[index].map = NULL; |
| 6850 | } |
| 6851 | |
| 6852 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6853 | void DescriptorLookupCache::Clear() { |
| 6854 | for (int index = 0; index < kLength; index++) keys_[index].array = NULL; |
| 6855 | } |
| 6856 | |
| 6857 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6858 | #ifdef DEBUG |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6859 | void Heap::GarbageCollectionGreedyCheck() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6860 | ASSERT(FLAG_gc_greedy); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6861 | if (isolate_->bootstrapper()->IsActive()) return; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6862 | if (disallow_allocation_failure()) return; |
| 6863 | CollectGarbage(NEW_SPACE); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6864 | } |
| 6865 | #endif |
| 6866 | |
| 6867 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6868 | TranscendentalCache::SubCache::SubCache(Type t) |
| 6869 | : type_(t), |
| 6870 | isolate_(Isolate::Current()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6871 | uint32_t in0 = 0xffffffffu; // Bit-pattern for a NaN that isn't |
| 6872 | uint32_t in1 = 0xffffffffu; // generated by the FPU. |
| 6873 | for (int i = 0; i < kCacheSize; i++) { |
| 6874 | elements_[i].in[0] = in0; |
| 6875 | elements_[i].in[1] = in1; |
| 6876 | elements_[i].output = NULL; |
| 6877 | } |
| 6878 | } |
| 6879 | |
| 6880 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6881 | void TranscendentalCache::Clear() { |
| 6882 | for (int i = 0; i < kNumberOfCaches; i++) { |
| 6883 | if (caches_[i] != NULL) { |
| 6884 | delete caches_[i]; |
| 6885 | caches_[i] = NULL; |
| 6886 | } |
| 6887 | } |
| 6888 | } |
| 6889 | |
| 6890 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6891 | void ExternalStringTable::CleanUp() { |
| 6892 | int last = 0; |
| 6893 | for (int i = 0; i < new_space_strings_.length(); ++i) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6894 | if (new_space_strings_[i] == heap_->raw_unchecked_the_hole_value()) { |
| 6895 | continue; |
| 6896 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6897 | if (heap_->InNewSpace(new_space_strings_[i])) { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6898 | new_space_strings_[last++] = new_space_strings_[i]; |
| 6899 | } else { |
| 6900 | old_space_strings_.Add(new_space_strings_[i]); |
| 6901 | } |
| 6902 | } |
| 6903 | new_space_strings_.Rewind(last); |
| 6904 | last = 0; |
| 6905 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6906 | if (old_space_strings_[i] == heap_->raw_unchecked_the_hole_value()) { |
| 6907 | continue; |
| 6908 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 6909 | ASSERT(!heap_->InNewSpace(old_space_strings_[i])); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6910 | old_space_strings_[last++] = old_space_strings_[i]; |
| 6911 | } |
| 6912 | old_space_strings_.Rewind(last); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6913 | if (FLAG_verify_heap) { |
| 6914 | Verify(); |
| 6915 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 6916 | } |
| 6917 | |
| 6918 | |
| 6919 | void ExternalStringTable::TearDown() { |
| 6920 | new_space_strings_.Free(); |
| 6921 | old_space_strings_.Free(); |
| 6922 | } |
| 6923 | |
| 6924 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6925 | void Heap::QueueMemoryChunkForFree(MemoryChunk* chunk) { |
| 6926 | chunk->set_next_chunk(chunks_queued_for_free_); |
| 6927 | chunks_queued_for_free_ = chunk; |
| 6928 | } |
| 6929 | |
| 6930 | |
| 6931 | void Heap::FreeQueuedChunks() { |
| 6932 | if (chunks_queued_for_free_ == NULL) return; |
| 6933 | MemoryChunk* next; |
| 6934 | MemoryChunk* chunk; |
| 6935 | for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) { |
| 6936 | next = chunk->next_chunk(); |
| 6937 | chunk->SetFlag(MemoryChunk::ABOUT_TO_BE_FREED); |
| 6938 | |
| 6939 | if (chunk->owner()->identity() == LO_SPACE) { |
| 6940 | // StoreBuffer::Filter relies on MemoryChunk::FromAnyPointerAddress. |
| 6941 | // If FromAnyPointerAddress encounters a slot that belongs to a large |
| 6942 | // chunk queued for deletion it will fail to find the chunk because |
| 6943 | // it try to perform a search in the list of pages owned by of the large |
| 6944 | // object space and queued chunks were detached from that list. |
| 6945 | // To work around this we split large chunk into normal kPageSize aligned |
| 6946 | // pieces and initialize size, owner and flags field of every piece. |
| 6947 | // If FromAnyPointerAddress encounters a slot that belongs to one of |
| 6948 | // these smaller pieces it will treat it as a slot on a normal Page. |
| 6949 | Address chunk_end = chunk->address() + chunk->size(); |
| 6950 | MemoryChunk* inner = MemoryChunk::FromAddress( |
| 6951 | chunk->address() + Page::kPageSize); |
| 6952 | MemoryChunk* inner_last = MemoryChunk::FromAddress(chunk_end - 1); |
| 6953 | while (inner <= inner_last) { |
| 6954 | // Size of a large chunk is always a multiple of |
| 6955 | // OS::AllocateAlignment() so there is always |
| 6956 | // enough space for a fake MemoryChunk header. |
| 6957 | Address area_end = Min(inner->address() + Page::kPageSize, chunk_end); |
| 6958 | // Guard against overflow. |
| 6959 | if (area_end < inner->address()) area_end = chunk_end; |
| 6960 | inner->SetArea(inner->address(), area_end); |
| 6961 | inner->set_size(Page::kPageSize); |
| 6962 | inner->set_owner(lo_space()); |
| 6963 | inner->SetFlag(MemoryChunk::ABOUT_TO_BE_FREED); |
| 6964 | inner = MemoryChunk::FromAddress( |
| 6965 | inner->address() + Page::kPageSize); |
| 6966 | } |
| 6967 | } |
| 6968 | } |
| 6969 | isolate_->heap()->store_buffer()->Compact(); |
| 6970 | isolate_->heap()->store_buffer()->Filter(MemoryChunk::ABOUT_TO_BE_FREED); |
| 6971 | for (chunk = chunks_queued_for_free_; chunk != NULL; chunk = next) { |
| 6972 | next = chunk->next_chunk(); |
| 6973 | isolate_->memory_allocator()->Free(chunk); |
| 6974 | } |
| 6975 | chunks_queued_for_free_ = NULL; |
| 6976 | } |
| 6977 | |
| 6978 | |
| 6979 | void Heap::RememberUnmappedPage(Address page, bool compacted) { |
| 6980 | uintptr_t p = reinterpret_cast<uintptr_t>(page); |
| 6981 | // Tag the page pointer to make it findable in the dump file. |
| 6982 | if (compacted) { |
| 6983 | p ^= 0xc1ead & (Page::kPageSize - 1); // Cleared. |
| 6984 | } else { |
| 6985 | p ^= 0x1d1ed & (Page::kPageSize - 1); // I died. |
| 6986 | } |
| 6987 | remembered_unmapped_pages_[remembered_unmapped_pages_index_] = |
| 6988 | reinterpret_cast<Address>(p); |
| 6989 | remembered_unmapped_pages_index_++; |
| 6990 | remembered_unmapped_pages_index_ %= kRememberedUnmappedPages; |
| 6991 | } |
| 6992 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6993 | } } // namespace v8::internal |