ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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" |
| 33 | #include "codegen-inl.h" |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 34 | #include "compilation-cache.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "debug.h" |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 36 | #include "heap-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include "global-handles.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | #include "mark-compact.h" |
| 39 | #include "natives.h" |
| 40 | #include "scanner.h" |
| 41 | #include "scopeinfo.h" |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 42 | #include "snapshot.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | #include "v8threads.h" |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 44 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 45 | #include "regexp-macro-assembler.h" |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 46 | #include "arm/regexp-macro-assembler-arm.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 47 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 49 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 50 | namespace v8 { |
| 51 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 52 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 54 | String* Heap::hidden_symbol_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 55 | Object* Heap::roots_[Heap::kRootListLength]; |
| 56 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 57 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 58 | NewSpace Heap::new_space_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 59 | OldSpace* Heap::old_pointer_space_ = NULL; |
| 60 | OldSpace* Heap::old_data_space_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | OldSpace* Heap::code_space_ = NULL; |
| 62 | MapSpace* Heap::map_space_ = NULL; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 63 | CellSpace* Heap::cell_space_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 64 | LargeObjectSpace* Heap::lo_space_ = NULL; |
| 65 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 66 | static const int kMinimumPromotionLimit = 2*MB; |
| 67 | static const int kMinimumAllocationLimit = 8*MB; |
| 68 | |
| 69 | int Heap::old_gen_promotion_limit_ = kMinimumPromotionLimit; |
| 70 | int Heap::old_gen_allocation_limit_ = kMinimumAllocationLimit; |
| 71 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 72 | int Heap::old_gen_exhausted_ = false; |
| 73 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 74 | int Heap::amount_of_external_allocated_memory_ = 0; |
| 75 | int Heap::amount_of_external_allocated_memory_at_last_global_gc_ = 0; |
| 76 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | // semispace_size_ should be a power of 2 and old_generation_size_ should be |
| 78 | // a multiple of Page::kPageSize. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 79 | #if defined(ANDROID) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 80 | int Heap::max_semispace_size_ = 2*MB; |
| 81 | int Heap::max_old_generation_size_ = 192*MB; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 82 | int Heap::initial_semispace_size_ = 128*KB; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 83 | size_t Heap::code_range_size_ = 0; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 84 | #elif defined(V8_TARGET_ARCH_X64) |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 85 | int Heap::max_semispace_size_ = 16*MB; |
| 86 | int Heap::max_old_generation_size_ = 1*GB; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 87 | int Heap::initial_semispace_size_ = 1*MB; |
sgjesse@chromium.org | 152a0b0 | 2009-10-07 13:50:16 +0000 | [diff] [blame] | 88 | size_t Heap::code_range_size_ = 512*MB; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 89 | #else |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 90 | int Heap::max_semispace_size_ = 8*MB; |
| 91 | int Heap::max_old_generation_size_ = 512*MB; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 92 | int Heap::initial_semispace_size_ = 512*KB; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 93 | size_t Heap::code_range_size_ = 0; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 94 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 96 | // The snapshot semispace size will be the default semispace size if |
| 97 | // snapshotting is used and will be the requested semispace size as |
| 98 | // set up by ConfigureHeap otherwise. |
| 99 | int Heap::reserved_semispace_size_ = Heap::max_semispace_size_; |
| 100 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 101 | List<Heap::GCPrologueCallbackPair> Heap::gc_prologue_callbacks_; |
| 102 | List<Heap::GCEpilogueCallbackPair> Heap::gc_epilogue_callbacks_; |
| 103 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 104 | GCCallback Heap::global_gc_prologue_callback_ = NULL; |
| 105 | GCCallback Heap::global_gc_epilogue_callback_ = NULL; |
| 106 | |
| 107 | // Variables set based on semispace_size_ and old_generation_size_ in |
| 108 | // ConfigureHeap. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 109 | |
| 110 | // Will be 4 * reserved_semispace_size_ to ensure that young |
| 111 | // generation can be aligned to its size. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 112 | int Heap::survived_since_last_expansion_ = 0; |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 113 | int Heap::external_allocation_limit_ = 0; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 114 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | Heap::HeapState Heap::gc_state_ = NOT_IN_GC; |
| 116 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | int Heap::mc_count_ = 0; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 118 | int Heap::ms_count_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | int Heap::gc_count_ = 0; |
| 120 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 121 | GCTracer* Heap::tracer_ = NULL; |
| 122 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 123 | int Heap::unflattened_strings_length_ = 0; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 124 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 125 | int Heap::always_allocate_scope_depth_ = 0; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 126 | int Heap::linear_allocation_scope_depth_ = 0; |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 127 | int Heap::contexts_disposed_ = 0; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 128 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 129 | #ifdef DEBUG |
| 130 | bool Heap::allocation_allowed_ = true; |
| 131 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | int Heap::allocation_timeout_ = 0; |
| 133 | bool Heap::disallow_allocation_failure_ = false; |
| 134 | #endif // DEBUG |
| 135 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 136 | int GCTracer::alive_after_last_gc_ = 0; |
| 137 | double GCTracer::last_gc_end_timestamp_ = 0.0; |
| 138 | int GCTracer::max_gc_pause_ = 0; |
| 139 | int GCTracer::max_alive_after_gc_ = 0; |
| 140 | int GCTracer::min_in_mutator_ = kMaxInt; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | |
| 142 | int Heap::Capacity() { |
| 143 | if (!HasBeenSetup()) return 0; |
| 144 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 145 | return new_space_.Capacity() + |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 146 | old_pointer_space_->Capacity() + |
| 147 | old_data_space_->Capacity() + |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | code_space_->Capacity() + |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 149 | map_space_->Capacity() + |
| 150 | cell_space_->Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 154 | int Heap::CommittedMemory() { |
| 155 | if (!HasBeenSetup()) return 0; |
| 156 | |
| 157 | return new_space_.CommittedMemory() + |
| 158 | old_pointer_space_->CommittedMemory() + |
| 159 | old_data_space_->CommittedMemory() + |
| 160 | code_space_->CommittedMemory() + |
| 161 | map_space_->CommittedMemory() + |
| 162 | cell_space_->CommittedMemory() + |
| 163 | lo_space_->Size(); |
| 164 | } |
| 165 | |
| 166 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 167 | int Heap::Available() { |
| 168 | if (!HasBeenSetup()) return 0; |
| 169 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 170 | return new_space_.Available() + |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 171 | old_pointer_space_->Available() + |
| 172 | old_data_space_->Available() + |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | code_space_->Available() + |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 174 | map_space_->Available() + |
| 175 | cell_space_->Available(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
| 179 | bool Heap::HasBeenSetup() { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 180 | return old_pointer_space_ != NULL && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 181 | old_data_space_ != NULL && |
| 182 | code_space_ != NULL && |
| 183 | map_space_ != NULL && |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 184 | cell_space_ != NULL && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 185 | lo_space_ != NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
| 189 | GarbageCollector Heap::SelectGarbageCollector(AllocationSpace space) { |
| 190 | // Is global GC requested? |
| 191 | if (space != NEW_SPACE || FLAG_gc_global) { |
| 192 | Counters::gc_compactor_caused_by_request.Increment(); |
| 193 | return MARK_COMPACTOR; |
| 194 | } |
| 195 | |
| 196 | // Is enough data promoted to justify a global GC? |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 197 | if (OldGenerationPromotionLimitReached()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | Counters::gc_compactor_caused_by_promoted_data.Increment(); |
| 199 | return MARK_COMPACTOR; |
| 200 | } |
| 201 | |
| 202 | // Have allocation in OLD and LO failed? |
| 203 | if (old_gen_exhausted_) { |
| 204 | Counters::gc_compactor_caused_by_oldspace_exhaustion.Increment(); |
| 205 | return MARK_COMPACTOR; |
| 206 | } |
| 207 | |
| 208 | // Is there enough space left in OLD to guarantee that a scavenge can |
| 209 | // succeed? |
| 210 | // |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 211 | // Note that MemoryAllocator->MaxAvailable() undercounts the memory available |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | // for object promotion. It counts only the bytes that the memory |
| 213 | // allocator has not yet allocated from the OS and assigned to any space, |
| 214 | // and does not count available bytes already in the old space or code |
| 215 | // space. Undercounting is safe---we may get an unrequested full GC when |
| 216 | // a scavenge would have succeeded. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 217 | if (MemoryAllocator::MaxAvailable() <= new_space_.Size()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | Counters::gc_compactor_caused_by_oldspace_exhaustion.Increment(); |
| 219 | return MARK_COMPACTOR; |
| 220 | } |
| 221 | |
| 222 | // Default |
| 223 | return SCAVENGER; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 228 | // --log-gc to avoid the complicated preprocessor and flag testing. |
| 229 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 230 | void Heap::ReportStatisticsBeforeGC() { |
| 231 | // Heap::ReportHeapStatistics will also log NewSpace statistics when |
| 232 | // compiled with ENABLE_LOGGING_AND_PROFILING and --log-gc is set. The |
| 233 | // following logic is used to avoid double logging. |
| 234 | #if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 235 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.CollectStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 236 | if (FLAG_heap_stats) { |
| 237 | ReportHeapStatistics("Before GC"); |
| 238 | } else if (FLAG_log_gc) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 239 | new_space_.ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 240 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 241 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.ClearHistograms(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | #elif defined(DEBUG) |
| 243 | if (FLAG_heap_stats) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 244 | new_space_.CollectStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | ReportHeapStatistics("Before GC"); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 246 | new_space_.ClearHistograms(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 247 | } |
| 248 | #elif defined(ENABLE_LOGGING_AND_PROFILING) |
| 249 | if (FLAG_log_gc) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 250 | new_space_.CollectStatistics(); |
| 251 | new_space_.ReportStatistics(); |
| 252 | new_space_.ClearHistograms(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | } |
| 254 | #endif |
| 255 | } |
| 256 | |
| 257 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 258 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
| 259 | void Heap::PrintShortHeapStatistics() { |
| 260 | if (!FLAG_trace_gc_verbose) return; |
| 261 | PrintF("Memory allocator, used: %8d, available: %8d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 262 | MemoryAllocator::Size(), |
| 263 | MemoryAllocator::Available()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 264 | PrintF("New space, used: %8d, available: %8d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 265 | Heap::new_space_.Size(), |
| 266 | new_space_.Available()); |
| 267 | PrintF("Old pointers, used: %8d, available: %8d, waste: %8d\n", |
| 268 | old_pointer_space_->Size(), |
| 269 | old_pointer_space_->Available(), |
| 270 | old_pointer_space_->Waste()); |
| 271 | PrintF("Old data space, used: %8d, available: %8d, waste: %8d\n", |
| 272 | old_data_space_->Size(), |
| 273 | old_data_space_->Available(), |
| 274 | old_data_space_->Waste()); |
| 275 | PrintF("Code space, used: %8d, available: %8d, waste: %8d\n", |
| 276 | code_space_->Size(), |
| 277 | code_space_->Available(), |
| 278 | code_space_->Waste()); |
| 279 | PrintF("Map space, used: %8d, available: %8d, waste: %8d\n", |
| 280 | map_space_->Size(), |
| 281 | map_space_->Available(), |
| 282 | map_space_->Waste()); |
| 283 | PrintF("Cell space, used: %8d, available: %8d, waste: %8d\n", |
| 284 | cell_space_->Size(), |
| 285 | cell_space_->Available(), |
| 286 | cell_space_->Waste()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 287 | PrintF("Large object space, used: %8d, avaialble: %8d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 288 | lo_space_->Size(), |
| 289 | lo_space_->Available()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 290 | } |
| 291 | #endif |
| 292 | |
| 293 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 295 | // --log-gc to avoid the complicated preprocessor and flag testing. |
| 296 | void Heap::ReportStatisticsAfterGC() { |
| 297 | // Similar to the before GC, we use some complicated logic to ensure that |
| 298 | // NewSpace statistics are logged exactly once when --log-gc is turned on. |
| 299 | #if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING) |
| 300 | if (FLAG_heap_stats) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 301 | new_space_.CollectStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 302 | ReportHeapStatistics("After GC"); |
| 303 | } else if (FLAG_log_gc) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 304 | new_space_.ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 305 | } |
| 306 | #elif defined(DEBUG) |
| 307 | if (FLAG_heap_stats) ReportHeapStatistics("After GC"); |
| 308 | #elif defined(ENABLE_LOGGING_AND_PROFILING) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 309 | if (FLAG_log_gc) new_space_.ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 310 | #endif |
| 311 | } |
| 312 | #endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 313 | |
| 314 | |
| 315 | void Heap::GarbageCollectionPrologue() { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 316 | TranscendentalCache::Clear(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 317 | ClearJSFunctionResultCaches(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 318 | gc_count_++; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 319 | unflattened_strings_length_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 320 | #ifdef DEBUG |
| 321 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 322 | allow_allocation(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 323 | |
| 324 | if (FLAG_verify_heap) { |
| 325 | Verify(); |
| 326 | } |
| 327 | |
| 328 | if (FLAG_gc_verbose) Print(); |
| 329 | |
| 330 | if (FLAG_print_rset) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 331 | // Not all spaces have remembered set bits that we care about. |
| 332 | old_pointer_space_->PrintRSet(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 333 | map_space_->PrintRSet(); |
| 334 | lo_space_->PrintRSet(); |
| 335 | } |
| 336 | #endif |
| 337 | |
| 338 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 339 | ReportStatisticsBeforeGC(); |
| 340 | #endif |
| 341 | } |
| 342 | |
| 343 | int Heap::SizeOfObjects() { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 344 | int total = 0; |
| 345 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 346 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 347 | total += space->Size(); |
| 348 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 349 | return total; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void Heap::GarbageCollectionEpilogue() { |
| 353 | #ifdef DEBUG |
| 354 | allow_allocation(true); |
| 355 | ZapFromSpace(); |
| 356 | |
| 357 | if (FLAG_verify_heap) { |
| 358 | Verify(); |
| 359 | } |
| 360 | |
| 361 | if (FLAG_print_global_handles) GlobalHandles::Print(); |
| 362 | if (FLAG_print_handles) PrintHandles(); |
| 363 | if (FLAG_gc_verbose) Print(); |
| 364 | if (FLAG_code_stats) ReportCodeStatistics("After GC"); |
| 365 | #endif |
| 366 | |
| 367 | Counters::alive_after_last_gc.Set(SizeOfObjects()); |
| 368 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 369 | Counters::symbol_table_capacity.Set(symbol_table()->Capacity()); |
| 370 | Counters::number_of_symbols.Set(symbol_table()->NumberOfElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 372 | ReportStatisticsAfterGC(); |
| 373 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 374 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 375 | Debug::AfterGarbageCollection(); |
| 376 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 380 | void Heap::CollectAllGarbage(bool force_compaction) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 381 | // Since we are ignoring the return value, the exact choice of space does |
| 382 | // not matter, so long as we do not specify NEW_SPACE, which would not |
| 383 | // cause a full GC. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 384 | MarkCompactCollector::SetForceCompaction(force_compaction); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 385 | CollectGarbage(0, OLD_POINTER_SPACE); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 386 | MarkCompactCollector::SetForceCompaction(false); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 390 | bool Heap::CollectGarbage(int requested_size, AllocationSpace space) { |
| 391 | // The VM is in the GC state until exiting this function. |
| 392 | VMState state(GC); |
| 393 | |
| 394 | #ifdef DEBUG |
| 395 | // Reset the allocation timeout to the GC interval, but make sure to |
| 396 | // allow at least a few allocations after a collection. The reason |
| 397 | // for this is that we have a lot of allocation sequences and we |
| 398 | // assume that a garbage collection will allow the subsequent |
| 399 | // allocation attempts to go through. |
| 400 | allocation_timeout_ = Max(6, FLAG_gc_interval); |
| 401 | #endif |
| 402 | |
| 403 | { GCTracer tracer; |
| 404 | GarbageCollectionPrologue(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 405 | // The GC count was incremented in the prologue. Tell the tracer about |
| 406 | // it. |
| 407 | tracer.set_gc_count(gc_count_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 408 | |
| 409 | GarbageCollector collector = SelectGarbageCollector(space); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 410 | // Tell the tracer which collector we've selected. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 411 | tracer.set_collector(collector); |
| 412 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 413 | HistogramTimer* rate = (collector == SCAVENGER) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 414 | ? &Counters::gc_scavenger |
| 415 | : &Counters::gc_compactor; |
| 416 | rate->Start(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 417 | PerformGarbageCollection(space, collector, &tracer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 418 | rate->Stop(); |
| 419 | |
| 420 | GarbageCollectionEpilogue(); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 425 | if (FLAG_log_gc) HeapProfiler::WriteSample(); |
| 426 | #endif |
| 427 | |
| 428 | switch (space) { |
| 429 | case NEW_SPACE: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 430 | return new_space_.Available() >= requested_size; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 431 | case OLD_POINTER_SPACE: |
| 432 | return old_pointer_space_->Available() >= requested_size; |
| 433 | case OLD_DATA_SPACE: |
| 434 | return old_data_space_->Available() >= requested_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 435 | case CODE_SPACE: |
| 436 | return code_space_->Available() >= requested_size; |
| 437 | case MAP_SPACE: |
| 438 | return map_space_->Available() >= requested_size; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 439 | case CELL_SPACE: |
| 440 | return cell_space_->Available() >= requested_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 441 | case LO_SPACE: |
| 442 | return lo_space_->Available() >= requested_size; |
| 443 | } |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 448 | void Heap::PerformScavenge() { |
| 449 | GCTracer tracer; |
| 450 | PerformGarbageCollection(NEW_SPACE, SCAVENGER, &tracer); |
| 451 | } |
| 452 | |
| 453 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 454 | #ifdef DEBUG |
kasperl@chromium.org | 416c5b0 | 2009-04-14 14:03:52 +0000 | [diff] [blame] | 455 | // Helper class for verifying the symbol table. |
| 456 | class SymbolTableVerifier : public ObjectVisitor { |
| 457 | public: |
| 458 | SymbolTableVerifier() { } |
| 459 | void VisitPointers(Object** start, Object** end) { |
| 460 | // Visit all HeapObject pointers in [start, end). |
| 461 | for (Object** p = start; p < end; p++) { |
| 462 | if ((*p)->IsHeapObject()) { |
| 463 | // Check that the symbol is actually a symbol. |
| 464 | ASSERT((*p)->IsNull() || (*p)->IsUndefined() || (*p)->IsSymbol()); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
kasperl@chromium.org | 416c5b0 | 2009-04-14 14:03:52 +0000 | [diff] [blame] | 467 | } |
| 468 | }; |
| 469 | #endif // DEBUG |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 470 | |
kasperl@chromium.org | 416c5b0 | 2009-04-14 14:03:52 +0000 | [diff] [blame] | 471 | |
| 472 | static void VerifySymbolTable() { |
| 473 | #ifdef DEBUG |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 474 | SymbolTableVerifier verifier; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 475 | Heap::symbol_table()->IterateElements(&verifier); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 476 | #endif // DEBUG |
| 477 | } |
| 478 | |
| 479 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 480 | void Heap::ReserveSpace( |
| 481 | int new_space_size, |
| 482 | int pointer_space_size, |
| 483 | int data_space_size, |
| 484 | int code_space_size, |
| 485 | int map_space_size, |
| 486 | int cell_space_size, |
| 487 | int large_object_size) { |
| 488 | NewSpace* new_space = Heap::new_space(); |
| 489 | PagedSpace* old_pointer_space = Heap::old_pointer_space(); |
| 490 | PagedSpace* old_data_space = Heap::old_data_space(); |
| 491 | PagedSpace* code_space = Heap::code_space(); |
| 492 | PagedSpace* map_space = Heap::map_space(); |
| 493 | PagedSpace* cell_space = Heap::cell_space(); |
| 494 | LargeObjectSpace* lo_space = Heap::lo_space(); |
| 495 | bool gc_performed = true; |
| 496 | while (gc_performed) { |
| 497 | gc_performed = false; |
| 498 | if (!new_space->ReserveSpace(new_space_size)) { |
| 499 | Heap::CollectGarbage(new_space_size, NEW_SPACE); |
| 500 | gc_performed = true; |
| 501 | } |
| 502 | if (!old_pointer_space->ReserveSpace(pointer_space_size)) { |
| 503 | Heap::CollectGarbage(pointer_space_size, OLD_POINTER_SPACE); |
| 504 | gc_performed = true; |
| 505 | } |
| 506 | if (!(old_data_space->ReserveSpace(data_space_size))) { |
| 507 | Heap::CollectGarbage(data_space_size, OLD_DATA_SPACE); |
| 508 | gc_performed = true; |
| 509 | } |
| 510 | if (!(code_space->ReserveSpace(code_space_size))) { |
| 511 | Heap::CollectGarbage(code_space_size, CODE_SPACE); |
| 512 | gc_performed = true; |
| 513 | } |
| 514 | if (!(map_space->ReserveSpace(map_space_size))) { |
| 515 | Heap::CollectGarbage(map_space_size, MAP_SPACE); |
| 516 | gc_performed = true; |
| 517 | } |
| 518 | if (!(cell_space->ReserveSpace(cell_space_size))) { |
| 519 | Heap::CollectGarbage(cell_space_size, CELL_SPACE); |
| 520 | gc_performed = true; |
| 521 | } |
| 522 | // We add a slack-factor of 2 in order to have space for the remembered |
| 523 | // set and a series of large-object allocations that are only just larger |
| 524 | // than the page size. |
| 525 | large_object_size *= 2; |
| 526 | // The ReserveSpace method on the large object space checks how much |
| 527 | // we can expand the old generation. This includes expansion caused by |
| 528 | // allocation in the other spaces. |
| 529 | large_object_size += cell_space_size + map_space_size + code_space_size + |
| 530 | data_space_size + pointer_space_size; |
| 531 | if (!(lo_space->ReserveSpace(large_object_size))) { |
| 532 | Heap::CollectGarbage(large_object_size, LO_SPACE); |
| 533 | gc_performed = true; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 539 | void Heap::EnsureFromSpaceIsCommitted() { |
| 540 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 541 | |
| 542 | // Committing memory to from space failed. |
| 543 | // Try shrinking and try again. |
| 544 | Shrink(); |
| 545 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 546 | |
| 547 | // Committing memory to from space failed again. |
| 548 | // Memory is exhausted and we will die. |
| 549 | V8::FatalProcessOutOfMemory("Committing semi space failed."); |
| 550 | } |
| 551 | |
| 552 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 553 | class ClearThreadJSFunctionResultCachesVisitor: public ThreadVisitor { |
| 554 | virtual void VisitThread(ThreadLocalTop* top) { |
| 555 | Context* context = top->context_; |
| 556 | if (context == NULL) return; |
| 557 | |
| 558 | FixedArray* caches = |
| 559 | context->global()->global_context()->jsfunction_result_caches(); |
| 560 | int length = caches->length(); |
| 561 | for (int i = 0; i < length; i++) { |
| 562 | JSFunctionResultCache::cast(caches->get(i))->Clear(); |
| 563 | } |
| 564 | } |
| 565 | }; |
| 566 | |
| 567 | |
| 568 | void Heap::ClearJSFunctionResultCaches() { |
| 569 | if (Bootstrapper::IsActive()) return; |
| 570 | ClearThreadJSFunctionResultCachesVisitor visitor; |
| 571 | ThreadManager::IterateThreads(&visitor); |
| 572 | } |
| 573 | |
| 574 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 575 | void Heap::PerformGarbageCollection(AllocationSpace space, |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 576 | GarbageCollector collector, |
| 577 | GCTracer* tracer) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 578 | VerifySymbolTable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 579 | if (collector == MARK_COMPACTOR && global_gc_prologue_callback_) { |
| 580 | ASSERT(!allocation_allowed_); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 581 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | global_gc_prologue_callback_(); |
| 583 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 584 | |
| 585 | GCType gc_type = |
| 586 | collector == MARK_COMPACTOR ? kGCTypeMarkSweepCompact : kGCTypeScavenge; |
| 587 | |
| 588 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 589 | if (gc_type & gc_prologue_callbacks_[i].gc_type) { |
| 590 | gc_prologue_callbacks_[i].callback(gc_type, kNoGCCallbackFlags); |
| 591 | } |
| 592 | } |
| 593 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 594 | EnsureFromSpaceIsCommitted(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 595 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 596 | if (collector == MARK_COMPACTOR) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 597 | // Perform mark-sweep with optional compaction. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 598 | MarkCompact(tracer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 599 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 600 | int old_gen_size = PromotedSpaceSize(); |
| 601 | old_gen_promotion_limit_ = |
| 602 | old_gen_size + Max(kMinimumPromotionLimit, old_gen_size / 3); |
| 603 | old_gen_allocation_limit_ = |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 604 | old_gen_size + Max(kMinimumAllocationLimit, old_gen_size / 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 605 | old_gen_exhausted_ = false; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 606 | } else { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 607 | tracer_ = tracer; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 608 | Scavenge(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 609 | tracer_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 610 | } |
ager@chromium.org | 439e85a | 2009-08-26 13:15:29 +0000 | [diff] [blame] | 611 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 612 | Counters::objs_since_last_young.Set(0); |
| 613 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 614 | if (collector == MARK_COMPACTOR) { |
| 615 | DisableAssertNoAllocation allow_allocation; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 616 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 617 | GlobalHandles::PostGarbageCollectionProcessing(); |
| 618 | } |
| 619 | |
| 620 | // Update relocatables. |
| 621 | Relocatable::PostGarbageCollectionProcessing(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 622 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 623 | if (collector == MARK_COMPACTOR) { |
| 624 | // Register the amount of external allocated memory. |
| 625 | amount_of_external_allocated_memory_at_last_global_gc_ = |
| 626 | amount_of_external_allocated_memory_; |
| 627 | } |
| 628 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 629 | GCCallbackFlags callback_flags = tracer->is_compacting() |
| 630 | ? kGCCallbackFlagCompacted |
| 631 | : kNoGCCallbackFlags; |
| 632 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 633 | if (gc_type & gc_epilogue_callbacks_[i].gc_type) { |
| 634 | gc_epilogue_callbacks_[i].callback(gc_type, callback_flags); |
| 635 | } |
| 636 | } |
| 637 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 638 | if (collector == MARK_COMPACTOR && global_gc_epilogue_callback_) { |
| 639 | ASSERT(!allocation_allowed_); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 640 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 641 | global_gc_epilogue_callback_(); |
| 642 | } |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 643 | VerifySymbolTable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 647 | void Heap::MarkCompact(GCTracer* tracer) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 648 | gc_state_ = MARK_COMPACT; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 649 | if (MarkCompactCollector::IsCompacting()) { |
| 650 | mc_count_++; |
| 651 | } else { |
| 652 | ms_count_++; |
| 653 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 654 | tracer->set_full_gc_count(mc_count_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 655 | LOG(ResourceEvent("markcompact", "begin")); |
| 656 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 657 | MarkCompactCollector::Prepare(tracer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 658 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 659 | bool is_compacting = MarkCompactCollector::IsCompacting(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 660 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 661 | MarkCompactPrologue(is_compacting); |
| 662 | |
| 663 | MarkCompactCollector::CollectGarbage(); |
| 664 | |
| 665 | MarkCompactEpilogue(is_compacting); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 666 | |
| 667 | LOG(ResourceEvent("markcompact", "end")); |
| 668 | |
| 669 | gc_state_ = NOT_IN_GC; |
| 670 | |
| 671 | Shrink(); |
| 672 | |
| 673 | Counters::objs_since_last_full.Set(0); |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 674 | |
| 675 | contexts_disposed_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 679 | void Heap::MarkCompactPrologue(bool is_compacting) { |
| 680 | // At any old GC clear the keyed lookup cache to enable collection of unused |
| 681 | // maps. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 682 | KeyedLookupCache::Clear(); |
| 683 | ContextSlotCache::Clear(); |
| 684 | DescriptorLookupCache::Clear(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 685 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 686 | CompilationCache::MarkCompactPrologue(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 687 | |
| 688 | Top::MarkCompactPrologue(is_compacting); |
| 689 | ThreadManager::MarkCompactPrologue(is_compacting); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 690 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 691 | CompletelyClearInstanceofCache(); |
| 692 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 693 | if (is_compacting) FlushNumberStringCache(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 697 | void Heap::MarkCompactEpilogue(bool is_compacting) { |
| 698 | Top::MarkCompactEpilogue(is_compacting); |
| 699 | ThreadManager::MarkCompactEpilogue(is_compacting); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | |
| 703 | Object* Heap::FindCodeObject(Address a) { |
| 704 | Object* obj = code_space_->FindObject(a); |
| 705 | if (obj->IsFailure()) { |
| 706 | obj = lo_space_->FindObject(a); |
| 707 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 708 | ASSERT(!obj->IsFailure()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 709 | return obj; |
| 710 | } |
| 711 | |
| 712 | |
| 713 | // Helper class for copying HeapObjects |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 714 | class ScavengeVisitor: public ObjectVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 715 | public: |
| 716 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 717 | void VisitPointer(Object** p) { ScavengePointer(p); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 718 | |
| 719 | void VisitPointers(Object** start, Object** end) { |
| 720 | // Copy all HeapObject pointers in [start, end) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 721 | for (Object** p = start; p < end; p++) ScavengePointer(p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | private: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 725 | void ScavengePointer(Object** p) { |
| 726 | Object* object = *p; |
| 727 | if (!Heap::InNewSpace(object)) return; |
| 728 | Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p), |
| 729 | reinterpret_cast<HeapObject*>(object)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 730 | } |
| 731 | }; |
| 732 | |
| 733 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 734 | // A queue of pointers and maps of to-be-promoted objects during a |
| 735 | // scavenge collection. |
| 736 | class PromotionQueue { |
| 737 | public: |
| 738 | void Initialize(Address start_address) { |
| 739 | front_ = rear_ = reinterpret_cast<HeapObject**>(start_address); |
| 740 | } |
| 741 | |
| 742 | bool is_empty() { return front_ <= rear_; } |
| 743 | |
| 744 | void insert(HeapObject* object, Map* map) { |
| 745 | *(--rear_) = object; |
| 746 | *(--rear_) = map; |
| 747 | // Assert no overflow into live objects. |
| 748 | ASSERT(reinterpret_cast<Address>(rear_) >= Heap::new_space()->top()); |
| 749 | } |
| 750 | |
| 751 | void remove(HeapObject** object, Map** map) { |
| 752 | *object = *(--front_); |
| 753 | *map = Map::cast(*(--front_)); |
| 754 | // Assert no underflow. |
| 755 | ASSERT(front_ >= rear_); |
| 756 | } |
| 757 | |
| 758 | private: |
| 759 | // The front of the queue is higher in memory than the rear. |
| 760 | HeapObject** front_; |
| 761 | HeapObject** rear_; |
| 762 | }; |
| 763 | |
| 764 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 765 | // Shared state read by the scavenge collector and set by ScavengeObject. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 766 | static PromotionQueue promotion_queue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 767 | |
| 768 | |
| 769 | #ifdef DEBUG |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 770 | // Visitor class to verify pointers in code or data space do not point into |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 771 | // new space. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 772 | class VerifyNonPointerSpacePointersVisitor: public ObjectVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 773 | public: |
| 774 | void VisitPointers(Object** start, Object**end) { |
| 775 | for (Object** current = start; current < end; current++) { |
| 776 | if ((*current)->IsHeapObject()) { |
| 777 | ASSERT(!Heap::InNewSpace(HeapObject::cast(*current))); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | }; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 782 | |
| 783 | |
| 784 | static void VerifyNonPointerSpacePointers() { |
| 785 | // Verify that there are no pointers to new space in spaces where we |
| 786 | // do not expect them. |
| 787 | VerifyNonPointerSpacePointersVisitor v; |
| 788 | HeapObjectIterator code_it(Heap::code_space()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 789 | for (HeapObject* object = code_it.next(); |
| 790 | object != NULL; object = code_it.next()) |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 791 | object->Iterate(&v); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 792 | |
| 793 | HeapObjectIterator data_it(Heap::old_data_space()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 794 | for (HeapObject* object = data_it.next(); |
| 795 | object != NULL; object = data_it.next()) |
| 796 | object->Iterate(&v); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 797 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 798 | #endif |
| 799 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 800 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 801 | void Heap::CheckNewSpaceExpansionCriteria() { |
| 802 | if (new_space_.Capacity() < new_space_.MaximumCapacity() && |
| 803 | survived_since_last_expansion_ > new_space_.Capacity()) { |
| 804 | // Grow the size of new space if there is room to grow and enough |
| 805 | // data has survived scavenge since the last expansion. |
| 806 | new_space_.Grow(); |
| 807 | survived_since_last_expansion_ = 0; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 812 | void Heap::Scavenge() { |
| 813 | #ifdef DEBUG |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 814 | if (FLAG_enable_slow_asserts) VerifyNonPointerSpacePointers(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 815 | #endif |
| 816 | |
| 817 | gc_state_ = SCAVENGE; |
| 818 | |
| 819 | // Implements Cheney's copying algorithm |
| 820 | LOG(ResourceEvent("scavenge", "begin")); |
| 821 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 822 | // Clear descriptor cache. |
| 823 | DescriptorLookupCache::Clear(); |
| 824 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 825 | // Used for updating survived_since_last_expansion_ at function end. |
| 826 | int survived_watermark = PromotedSpaceSize(); |
| 827 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 828 | CheckNewSpaceExpansionCriteria(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 829 | |
| 830 | // Flip the semispaces. After flipping, to space is empty, from space has |
| 831 | // live objects. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 832 | new_space_.Flip(); |
| 833 | new_space_.ResetAllocationInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 834 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 835 | // We need to sweep newly copied objects which can be either in the |
| 836 | // to space or promoted to the old generation. For to-space |
| 837 | // objects, we treat the bottom of the to space as a queue. Newly |
| 838 | // copied and unswept objects lie between a 'front' mark and the |
| 839 | // allocation pointer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | // |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 841 | // Promoted objects can go into various old-generation spaces, and |
| 842 | // can be allocated internally in the spaces (from the free list). |
| 843 | // We treat the top of the to space as a queue of addresses of |
| 844 | // promoted objects. The addresses of newly promoted and unswept |
| 845 | // objects lie between a 'front' mark and a 'rear' mark that is |
| 846 | // updated as a side effect of promoting an object. |
| 847 | // |
| 848 | // There is guaranteed to be enough room at the top of the to space |
| 849 | // for the addresses of promoted objects: every object promoted |
| 850 | // frees up its size in bytes from the top of the new space, and |
| 851 | // objects are at least one pointer in size. |
| 852 | Address new_space_front = new_space_.ToSpaceLow(); |
| 853 | promotion_queue.Initialize(new_space_.ToSpaceHigh()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 854 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 855 | ScavengeVisitor scavenge_visitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 856 | // Copy roots. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 857 | IterateRoots(&scavenge_visitor, VISIT_ALL_IN_SCAVENGE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 858 | |
| 859 | // Copy objects reachable from the old generation. By definition, |
| 860 | // there are no intergenerational pointers in code or data spaces. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 861 | IterateRSet(old_pointer_space_, &ScavengePointer); |
| 862 | IterateRSet(map_space_, &ScavengePointer); |
| 863 | lo_space_->IterateRSet(&ScavengePointer); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 864 | |
| 865 | // Copy objects reachable from cells by scavenging cell values directly. |
| 866 | HeapObjectIterator cell_iterator(cell_space_); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 867 | for (HeapObject* cell = cell_iterator.next(); |
| 868 | cell != NULL; cell = cell_iterator.next()) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 869 | if (cell->IsJSGlobalPropertyCell()) { |
| 870 | Address value_address = |
| 871 | reinterpret_cast<Address>(cell) + |
| 872 | (JSGlobalPropertyCell::kValueOffset - kHeapObjectTag); |
| 873 | scavenge_visitor.VisitPointer(reinterpret_cast<Object**>(value_address)); |
| 874 | } |
| 875 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 876 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 877 | new_space_front = DoScavenge(&scavenge_visitor, new_space_front); |
| 878 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 879 | UpdateNewSpaceReferencesInExternalStringTable( |
| 880 | &UpdateNewSpaceReferenceInExternalStringTableEntry); |
| 881 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 882 | ASSERT(new_space_front == new_space_.top()); |
| 883 | |
| 884 | // Set age mark. |
| 885 | new_space_.set_age_mark(new_space_.top()); |
| 886 | |
| 887 | // Update how much has survived scavenge. |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 888 | IncrementYoungSurvivorsCounter( |
| 889 | (PromotedSpaceSize() - survived_watermark) + new_space_.Size()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 890 | |
| 891 | LOG(ResourceEvent("scavenge", "end")); |
| 892 | |
| 893 | gc_state_ = NOT_IN_GC; |
| 894 | } |
| 895 | |
| 896 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 897 | String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Object** p) { |
| 898 | MapWord first_word = HeapObject::cast(*p)->map_word(); |
| 899 | |
| 900 | if (!first_word.IsForwardingAddress()) { |
| 901 | // Unreachable external string can be finalized. |
| 902 | FinalizeExternalString(String::cast(*p)); |
| 903 | return NULL; |
| 904 | } |
| 905 | |
| 906 | // String is still reachable. |
| 907 | return String::cast(first_word.ToForwardingAddress()); |
| 908 | } |
| 909 | |
| 910 | |
| 911 | void Heap::UpdateNewSpaceReferencesInExternalStringTable( |
| 912 | ExternalStringTableUpdaterCallback updater_func) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 913 | ExternalStringTable::Verify(); |
| 914 | |
| 915 | if (ExternalStringTable::new_space_strings_.is_empty()) return; |
| 916 | |
| 917 | Object** start = &ExternalStringTable::new_space_strings_[0]; |
| 918 | Object** end = start + ExternalStringTable::new_space_strings_.length(); |
| 919 | Object** last = start; |
| 920 | |
| 921 | for (Object** p = start; p < end; ++p) { |
| 922 | ASSERT(Heap::InFromSpace(*p)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 923 | String* target = updater_func(p); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 924 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 925 | if (target == NULL) continue; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 926 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 927 | ASSERT(target->IsExternalString()); |
| 928 | |
| 929 | if (Heap::InNewSpace(target)) { |
| 930 | // String is still in new space. Update the table entry. |
| 931 | *last = target; |
| 932 | ++last; |
| 933 | } else { |
| 934 | // String got promoted. Move it to the old string list. |
| 935 | ExternalStringTable::AddOldString(target); |
| 936 | } |
| 937 | } |
| 938 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 939 | ASSERT(last <= end); |
| 940 | ExternalStringTable::ShrinkNewStrings(static_cast<int>(last - start)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | |
| 944 | Address Heap::DoScavenge(ObjectVisitor* scavenge_visitor, |
| 945 | Address new_space_front) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 946 | do { |
| 947 | ASSERT(new_space_front <= new_space_.top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 948 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 949 | // The addresses new_space_front and new_space_.top() define a |
| 950 | // queue of unprocessed copied objects. Process them until the |
| 951 | // queue is empty. |
| 952 | while (new_space_front < new_space_.top()) { |
| 953 | HeapObject* object = HeapObject::FromAddress(new_space_front); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 954 | object->Iterate(scavenge_visitor); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 955 | new_space_front += object->Size(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 956 | } |
| 957 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 958 | // Promote and process all the to-be-promoted objects. |
| 959 | while (!promotion_queue.is_empty()) { |
| 960 | HeapObject* source; |
| 961 | Map* map; |
| 962 | promotion_queue.remove(&source, &map); |
| 963 | // Copy the from-space object to its new location (given by the |
| 964 | // forwarding address) and fix its map. |
| 965 | HeapObject* target = source->map_word().ToForwardingAddress(); |
| 966 | CopyBlock(reinterpret_cast<Object**>(target->address()), |
| 967 | reinterpret_cast<Object**>(source->address()), |
| 968 | source->SizeFromMap(map)); |
| 969 | target->set_map(map); |
| 970 | |
| 971 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 972 | // Update NewSpace stats if necessary. |
| 973 | RecordCopiedObject(target); |
| 974 | #endif |
| 975 | // Visit the newly copied object for pointers to new space. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 976 | target->Iterate(scavenge_visitor); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 977 | UpdateRSet(target); |
| 978 | } |
| 979 | |
| 980 | // Take another spin if there are now unswept objects in new space |
| 981 | // (there are currently no more unswept promoted objects). |
| 982 | } while (new_space_front < new_space_.top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 983 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 984 | return new_space_front; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | |
| 988 | void Heap::ClearRSetRange(Address start, int size_in_bytes) { |
| 989 | uint32_t start_bit; |
| 990 | Address start_word_address = |
| 991 | Page::ComputeRSetBitPosition(start, 0, &start_bit); |
| 992 | uint32_t end_bit; |
| 993 | Address end_word_address = |
| 994 | Page::ComputeRSetBitPosition(start + size_in_bytes - kIntSize, |
| 995 | 0, |
| 996 | &end_bit); |
| 997 | |
| 998 | // We want to clear the bits in the starting word starting with the |
| 999 | // first bit, and in the ending word up to and including the last |
| 1000 | // bit. Build a pair of bitmasks to do that. |
| 1001 | uint32_t start_bitmask = start_bit - 1; |
| 1002 | uint32_t end_bitmask = ~((end_bit << 1) - 1); |
| 1003 | |
| 1004 | // If the start address and end address are the same, we mask that |
| 1005 | // word once, otherwise mask the starting and ending word |
| 1006 | // separately and all the ones in between. |
| 1007 | if (start_word_address == end_word_address) { |
| 1008 | Memory::uint32_at(start_word_address) &= (start_bitmask | end_bitmask); |
| 1009 | } else { |
| 1010 | Memory::uint32_at(start_word_address) &= start_bitmask; |
| 1011 | Memory::uint32_at(end_word_address) &= end_bitmask; |
| 1012 | start_word_address += kIntSize; |
| 1013 | memset(start_word_address, 0, end_word_address - start_word_address); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | class UpdateRSetVisitor: public ObjectVisitor { |
| 1019 | public: |
| 1020 | |
| 1021 | void VisitPointer(Object** p) { |
| 1022 | UpdateRSet(p); |
| 1023 | } |
| 1024 | |
| 1025 | void VisitPointers(Object** start, Object** end) { |
| 1026 | // Update a store into slots [start, end), used (a) to update remembered |
| 1027 | // set when promoting a young object to old space or (b) to rebuild |
| 1028 | // remembered sets after a mark-compact collection. |
| 1029 | for (Object** p = start; p < end; p++) UpdateRSet(p); |
| 1030 | } |
| 1031 | private: |
| 1032 | |
| 1033 | void UpdateRSet(Object** p) { |
| 1034 | // The remembered set should not be set. It should be clear for objects |
| 1035 | // newly copied to old space, and it is cleared before rebuilding in the |
| 1036 | // mark-compact collector. |
| 1037 | ASSERT(!Page::IsRSetSet(reinterpret_cast<Address>(p), 0)); |
| 1038 | if (Heap::InNewSpace(*p)) { |
| 1039 | Page::SetRSet(reinterpret_cast<Address>(p), 0); |
| 1040 | } |
| 1041 | } |
| 1042 | }; |
| 1043 | |
| 1044 | |
| 1045 | int Heap::UpdateRSet(HeapObject* obj) { |
| 1046 | ASSERT(!InNewSpace(obj)); |
| 1047 | // Special handling of fixed arrays to iterate the body based on the start |
| 1048 | // address and offset. Just iterating the pointers as in UpdateRSetVisitor |
| 1049 | // will not work because Page::SetRSet needs to have the start of the |
sgjesse@chromium.org | b9d7da1 | 2009-08-05 08:38:10 +0000 | [diff] [blame] | 1050 | // object for large object pages. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1051 | if (obj->IsFixedArray()) { |
| 1052 | FixedArray* array = FixedArray::cast(obj); |
| 1053 | int length = array->length(); |
| 1054 | for (int i = 0; i < length; i++) { |
| 1055 | int offset = FixedArray::kHeaderSize + i * kPointerSize; |
| 1056 | ASSERT(!Page::IsRSetSet(obj->address(), offset)); |
| 1057 | if (Heap::InNewSpace(array->get(i))) { |
| 1058 | Page::SetRSet(obj->address(), offset); |
| 1059 | } |
| 1060 | } |
| 1061 | } else if (!obj->IsCode()) { |
| 1062 | // Skip code object, we know it does not contain inter-generational |
| 1063 | // pointers. |
| 1064 | UpdateRSetVisitor v; |
| 1065 | obj->Iterate(&v); |
| 1066 | } |
| 1067 | return obj->Size(); |
| 1068 | } |
| 1069 | |
| 1070 | |
| 1071 | void Heap::RebuildRSets() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1072 | // By definition, we do not care about remembered set bits in code, |
| 1073 | // data, or cell spaces. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1074 | map_space_->ClearRSet(); |
| 1075 | RebuildRSets(map_space_); |
| 1076 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1077 | old_pointer_space_->ClearRSet(); |
| 1078 | RebuildRSets(old_pointer_space_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1079 | |
| 1080 | Heap::lo_space_->ClearRSet(); |
| 1081 | RebuildRSets(lo_space_); |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | void Heap::RebuildRSets(PagedSpace* space) { |
| 1086 | HeapObjectIterator it(space); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1087 | for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) |
| 1088 | Heap::UpdateRSet(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | void Heap::RebuildRSets(LargeObjectSpace* space) { |
| 1093 | LargeObjectIterator it(space); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1094 | for (HeapObject* obj = it.next(); obj != NULL; obj = it.next()) |
| 1095 | Heap::UpdateRSet(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1100 | void Heap::RecordCopiedObject(HeapObject* obj) { |
| 1101 | bool should_record = false; |
| 1102 | #ifdef DEBUG |
| 1103 | should_record = FLAG_heap_stats; |
| 1104 | #endif |
| 1105 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 1106 | should_record = should_record || FLAG_log_gc; |
| 1107 | #endif |
| 1108 | if (should_record) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1109 | if (new_space_.Contains(obj)) { |
| 1110 | new_space_.RecordAllocation(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1111 | } else { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1112 | new_space_.RecordPromotion(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | #endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1117 | |
| 1118 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1119 | |
| 1120 | HeapObject* Heap::MigrateObject(HeapObject* source, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1121 | HeapObject* target, |
| 1122 | int size) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1123 | // Copy the content of source to target. |
| 1124 | CopyBlock(reinterpret_cast<Object**>(target->address()), |
| 1125 | reinterpret_cast<Object**>(source->address()), |
| 1126 | size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1127 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1128 | // Set the forwarding address. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1129 | source->set_map_word(MapWord::FromForwardingAddress(target)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1130 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1131 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1132 | // Update NewSpace stats if necessary. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1133 | RecordCopiedObject(target); |
| 1134 | #endif |
| 1135 | |
| 1136 | return target; |
| 1137 | } |
| 1138 | |
| 1139 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1140 | static inline bool IsShortcutCandidate(HeapObject* object, Map* map) { |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1141 | STATIC_ASSERT(kNotStringTag != 0 && kSymbolTag != 0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1142 | ASSERT(object->map() == map); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1143 | InstanceType type = map->instance_type(); |
| 1144 | if ((type & kShortcutTypeMask) != kShortcutTypeTag) return false; |
| 1145 | ASSERT(object->IsString() && !object->IsSymbol()); |
| 1146 | return ConsString::cast(object)->unchecked_second() == Heap::empty_string(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | void Heap::ScavengeObjectSlow(HeapObject** p, HeapObject* object) { |
| 1151 | ASSERT(InFromSpace(object)); |
| 1152 | MapWord first_word = object->map_word(); |
| 1153 | ASSERT(!first_word.IsForwardingAddress()); |
| 1154 | |
| 1155 | // Optimization: Bypass flattened ConsString objects. |
| 1156 | if (IsShortcutCandidate(object, first_word.ToMap())) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 1157 | object = HeapObject::cast(ConsString::cast(object)->unchecked_first()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1158 | *p = object; |
| 1159 | // After patching *p we have to repeat the checks that object is in the |
| 1160 | // active semispace of the young generation and not already copied. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1161 | if (!InNewSpace(object)) return; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1162 | first_word = object->map_word(); |
| 1163 | if (first_word.IsForwardingAddress()) { |
| 1164 | *p = first_word.ToForwardingAddress(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1165 | return; |
| 1166 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 1169 | int object_size = object->SizeFromMap(first_word.ToMap()); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1170 | // We rely on live objects in new space to be at least two pointers, |
| 1171 | // so we can store the from-space address and map pointer of promoted |
| 1172 | // objects in the to space. |
| 1173 | ASSERT(object_size >= 2 * kPointerSize); |
| 1174 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1175 | // If the object should be promoted, we try to copy it to old space. |
| 1176 | if (ShouldBePromoted(object->address(), object_size)) { |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1177 | Object* result; |
| 1178 | if (object_size > MaxObjectSizeInPagedSpace()) { |
| 1179 | result = lo_space_->AllocateRawFixedArray(object_size); |
| 1180 | if (!result->IsFailure()) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1181 | // Save the from-space object pointer and its map pointer at the |
| 1182 | // top of the to space to be swept and copied later. Write the |
| 1183 | // forwarding address over the map word of the from-space |
| 1184 | // object. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1185 | HeapObject* target = HeapObject::cast(result); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1186 | promotion_queue.insert(object, first_word.ToMap()); |
| 1187 | object->set_map_word(MapWord::FromForwardingAddress(target)); |
| 1188 | |
| 1189 | // Give the space allocated for the result a proper map by |
| 1190 | // treating it as a free list node (not linked into the free |
| 1191 | // list). |
| 1192 | FreeListNode* node = FreeListNode::FromAddress(target->address()); |
| 1193 | node->set_size(object_size); |
| 1194 | |
| 1195 | *p = target; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1196 | tracer()->increment_promoted_objects_size(object_size); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1197 | return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1198 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1199 | } else { |
| 1200 | OldSpace* target_space = Heap::TargetSpace(object); |
| 1201 | ASSERT(target_space == Heap::old_pointer_space_ || |
| 1202 | target_space == Heap::old_data_space_); |
| 1203 | result = target_space->AllocateRaw(object_size); |
| 1204 | if (!result->IsFailure()) { |
| 1205 | HeapObject* target = HeapObject::cast(result); |
| 1206 | if (target_space == Heap::old_pointer_space_) { |
| 1207 | // Save the from-space object pointer and its map pointer at the |
| 1208 | // top of the to space to be swept and copied later. Write the |
| 1209 | // forwarding address over the map word of the from-space |
| 1210 | // object. |
| 1211 | promotion_queue.insert(object, first_word.ToMap()); |
| 1212 | object->set_map_word(MapWord::FromForwardingAddress(target)); |
| 1213 | |
| 1214 | // Give the space allocated for the result a proper map by |
| 1215 | // treating it as a free list node (not linked into the free |
| 1216 | // list). |
| 1217 | FreeListNode* node = FreeListNode::FromAddress(target->address()); |
| 1218 | node->set_size(object_size); |
| 1219 | |
| 1220 | *p = target; |
| 1221 | } else { |
| 1222 | // Objects promoted to the data space can be copied immediately |
| 1223 | // and not revisited---we will never sweep that space for |
| 1224 | // pointers and the copied objects do not contain pointers to |
| 1225 | // new space objects. |
| 1226 | *p = MigrateObject(object, target, object_size); |
| 1227 | #ifdef DEBUG |
| 1228 | VerifyNonPointerSpacePointersVisitor v; |
| 1229 | (*p)->Iterate(&v); |
| 1230 | #endif |
| 1231 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1232 | tracer()->increment_promoted_objects_size(object_size); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1233 | return; |
| 1234 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1235 | } |
| 1236 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1237 | // The object should remain in new space or the old space allocation failed. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1238 | Object* result = new_space_.AllocateRaw(object_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1239 | // Failed allocation at this point is utterly unexpected. |
| 1240 | ASSERT(!result->IsFailure()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1241 | *p = MigrateObject(object, HeapObject::cast(result), object_size); |
| 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | void Heap::ScavengePointer(HeapObject** p) { |
| 1246 | ScavengeObject(p, *p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | Object* Heap::AllocatePartialMap(InstanceType instance_type, |
| 1251 | int instance_size) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1252 | Object* result = AllocateRawMap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1253 | if (result->IsFailure()) return result; |
| 1254 | |
| 1255 | // Map::cast cannot be used due to uninitialized map field. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1256 | reinterpret_cast<Map*>(result)->set_map(raw_unchecked_meta_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | reinterpret_cast<Map*>(result)->set_instance_type(instance_type); |
| 1258 | reinterpret_cast<Map*>(result)->set_instance_size(instance_size); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1259 | reinterpret_cast<Map*>(result)->set_inobject_properties(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1260 | reinterpret_cast<Map*>(result)->set_pre_allocated_property_fields(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1261 | reinterpret_cast<Map*>(result)->set_unused_property_fields(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1262 | reinterpret_cast<Map*>(result)->set_bit_field(0); |
| 1263 | reinterpret_cast<Map*>(result)->set_bit_field2(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1264 | return result; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | Object* Heap::AllocateMap(InstanceType instance_type, int instance_size) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1269 | Object* result = AllocateRawMap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1270 | if (result->IsFailure()) return result; |
| 1271 | |
| 1272 | Map* map = reinterpret_cast<Map*>(result); |
| 1273 | map->set_map(meta_map()); |
| 1274 | map->set_instance_type(instance_type); |
| 1275 | map->set_prototype(null_value()); |
| 1276 | map->set_constructor(null_value()); |
| 1277 | map->set_instance_size(instance_size); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1278 | map->set_inobject_properties(0); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1279 | map->set_pre_allocated_property_fields(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1280 | map->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1281 | map->set_code_cache(empty_fixed_array()); |
| 1282 | map->set_unused_property_fields(0); |
| 1283 | map->set_bit_field(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1284 | map->set_bit_field2(1 << Map::kIsExtensible); |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 1285 | |
| 1286 | // If the map object is aligned fill the padding area with Smi 0 objects. |
| 1287 | if (Map::kPadStart < Map::kSize) { |
| 1288 | memset(reinterpret_cast<byte*>(map) + Map::kPadStart - kHeapObjectTag, |
| 1289 | 0, |
| 1290 | Map::kSize - Map::kPadStart); |
| 1291 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1292 | return map; |
| 1293 | } |
| 1294 | |
| 1295 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1296 | Object* Heap::AllocateCodeCache() { |
| 1297 | Object* result = AllocateStruct(CODE_CACHE_TYPE); |
| 1298 | if (result->IsFailure()) return result; |
| 1299 | CodeCache* code_cache = CodeCache::cast(result); |
| 1300 | code_cache->set_default_cache(empty_fixed_array()); |
| 1301 | code_cache->set_normal_type_cache(undefined_value()); |
| 1302 | return code_cache; |
| 1303 | } |
| 1304 | |
| 1305 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1306 | const Heap::StringTypeTable Heap::string_type_table[] = { |
| 1307 | #define STRING_TYPE_ELEMENT(type, size, name, camel_name) \ |
| 1308 | {type, size, k##camel_name##MapRootIndex}, |
| 1309 | STRING_TYPE_LIST(STRING_TYPE_ELEMENT) |
| 1310 | #undef STRING_TYPE_ELEMENT |
| 1311 | }; |
| 1312 | |
| 1313 | |
| 1314 | const Heap::ConstantSymbolTable Heap::constant_symbol_table[] = { |
| 1315 | #define CONSTANT_SYMBOL_ELEMENT(name, contents) \ |
| 1316 | {contents, k##name##RootIndex}, |
| 1317 | SYMBOL_LIST(CONSTANT_SYMBOL_ELEMENT) |
| 1318 | #undef CONSTANT_SYMBOL_ELEMENT |
| 1319 | }; |
| 1320 | |
| 1321 | |
| 1322 | const Heap::StructTable Heap::struct_table[] = { |
| 1323 | #define STRUCT_TABLE_ELEMENT(NAME, Name, name) \ |
| 1324 | { NAME##_TYPE, Name::kSize, k##Name##MapRootIndex }, |
| 1325 | STRUCT_LIST(STRUCT_TABLE_ELEMENT) |
| 1326 | #undef STRUCT_TABLE_ELEMENT |
| 1327 | }; |
| 1328 | |
| 1329 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1330 | bool Heap::CreateInitialMaps() { |
| 1331 | Object* obj = AllocatePartialMap(MAP_TYPE, Map::kSize); |
| 1332 | if (obj->IsFailure()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1333 | // Map::cast cannot be used due to uninitialized map field. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1334 | Map* new_meta_map = reinterpret_cast<Map*>(obj); |
| 1335 | set_meta_map(new_meta_map); |
| 1336 | new_meta_map->set_map(new_meta_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1337 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 1338 | obj = AllocatePartialMap(FIXED_ARRAY_TYPE, FixedArray::kHeaderSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1339 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1340 | set_fixed_array_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1341 | |
| 1342 | obj = AllocatePartialMap(ODDBALL_TYPE, Oddball::kSize); |
| 1343 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1344 | set_oddball_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1345 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1346 | // Allocate the empty array. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1347 | obj = AllocateEmptyFixedArray(); |
| 1348 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1349 | set_empty_fixed_array(FixedArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1350 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1351 | obj = Allocate(oddball_map(), OLD_DATA_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1352 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1353 | set_null_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1354 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1355 | // Allocate the empty descriptor array. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1356 | obj = AllocateEmptyFixedArray(); |
| 1357 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1358 | set_empty_descriptor_array(DescriptorArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1359 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1360 | // Fix the instance_descriptors for the existing maps. |
| 1361 | meta_map()->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1362 | meta_map()->set_code_cache(empty_fixed_array()); |
| 1363 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1364 | fixed_array_map()->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1365 | fixed_array_map()->set_code_cache(empty_fixed_array()); |
| 1366 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1367 | oddball_map()->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1368 | oddball_map()->set_code_cache(empty_fixed_array()); |
| 1369 | |
| 1370 | // Fix prototype object for existing maps. |
| 1371 | meta_map()->set_prototype(null_value()); |
| 1372 | meta_map()->set_constructor(null_value()); |
| 1373 | |
| 1374 | fixed_array_map()->set_prototype(null_value()); |
| 1375 | fixed_array_map()->set_constructor(null_value()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1376 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1377 | oddball_map()->set_prototype(null_value()); |
| 1378 | oddball_map()->set_constructor(null_value()); |
| 1379 | |
| 1380 | obj = AllocateMap(HEAP_NUMBER_TYPE, HeapNumber::kSize); |
| 1381 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1382 | set_heap_number_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1383 | |
| 1384 | obj = AllocateMap(PROXY_TYPE, Proxy::kSize); |
| 1385 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1386 | set_proxy_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1387 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1388 | for (unsigned i = 0; i < ARRAY_SIZE(string_type_table); i++) { |
| 1389 | const StringTypeTable& entry = string_type_table[i]; |
| 1390 | obj = AllocateMap(entry.type, entry.size); |
| 1391 | if (obj->IsFailure()) return false; |
| 1392 | roots_[entry.index] = Map::cast(obj); |
| 1393 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1394 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1395 | obj = AllocateMap(STRING_TYPE, SeqTwoByteString::kAlignedSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1396 | if (obj->IsFailure()) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1397 | set_undetectable_string_map(Map::cast(obj)); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1398 | Map::cast(obj)->set_is_undetectable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1399 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1400 | obj = AllocateMap(ASCII_STRING_TYPE, SeqAsciiString::kAlignedSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1401 | if (obj->IsFailure()) return false; |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1402 | set_undetectable_ascii_string_map(Map::cast(obj)); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1403 | Map::cast(obj)->set_is_undetectable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1404 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 1405 | obj = AllocateMap(BYTE_ARRAY_TYPE, ByteArray::kAlignedSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1406 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1407 | set_byte_array_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1408 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1409 | obj = AllocateMap(PIXEL_ARRAY_TYPE, PixelArray::kAlignedSize); |
| 1410 | if (obj->IsFailure()) return false; |
| 1411 | set_pixel_array_map(Map::cast(obj)); |
| 1412 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1413 | obj = AllocateMap(EXTERNAL_BYTE_ARRAY_TYPE, |
| 1414 | ExternalArray::kAlignedSize); |
| 1415 | if (obj->IsFailure()) return false; |
| 1416 | set_external_byte_array_map(Map::cast(obj)); |
| 1417 | |
| 1418 | obj = AllocateMap(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE, |
| 1419 | ExternalArray::kAlignedSize); |
| 1420 | if (obj->IsFailure()) return false; |
| 1421 | set_external_unsigned_byte_array_map(Map::cast(obj)); |
| 1422 | |
| 1423 | obj = AllocateMap(EXTERNAL_SHORT_ARRAY_TYPE, |
| 1424 | ExternalArray::kAlignedSize); |
| 1425 | if (obj->IsFailure()) return false; |
| 1426 | set_external_short_array_map(Map::cast(obj)); |
| 1427 | |
| 1428 | obj = AllocateMap(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE, |
| 1429 | ExternalArray::kAlignedSize); |
| 1430 | if (obj->IsFailure()) return false; |
| 1431 | set_external_unsigned_short_array_map(Map::cast(obj)); |
| 1432 | |
| 1433 | obj = AllocateMap(EXTERNAL_INT_ARRAY_TYPE, |
| 1434 | ExternalArray::kAlignedSize); |
| 1435 | if (obj->IsFailure()) return false; |
| 1436 | set_external_int_array_map(Map::cast(obj)); |
| 1437 | |
| 1438 | obj = AllocateMap(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE, |
| 1439 | ExternalArray::kAlignedSize); |
| 1440 | if (obj->IsFailure()) return false; |
| 1441 | set_external_unsigned_int_array_map(Map::cast(obj)); |
| 1442 | |
| 1443 | obj = AllocateMap(EXTERNAL_FLOAT_ARRAY_TYPE, |
| 1444 | ExternalArray::kAlignedSize); |
| 1445 | if (obj->IsFailure()) return false; |
| 1446 | set_external_float_array_map(Map::cast(obj)); |
| 1447 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1448 | obj = AllocateMap(CODE_TYPE, Code::kHeaderSize); |
| 1449 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1450 | set_code_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1451 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1452 | obj = AllocateMap(JS_GLOBAL_PROPERTY_CELL_TYPE, |
| 1453 | JSGlobalPropertyCell::kSize); |
| 1454 | if (obj->IsFailure()) return false; |
| 1455 | set_global_property_cell_map(Map::cast(obj)); |
| 1456 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1457 | obj = AllocateMap(FILLER_TYPE, kPointerSize); |
| 1458 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1459 | set_one_pointer_filler_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1460 | |
| 1461 | obj = AllocateMap(FILLER_TYPE, 2 * kPointerSize); |
| 1462 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1463 | set_two_pointer_filler_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1465 | for (unsigned i = 0; i < ARRAY_SIZE(struct_table); i++) { |
| 1466 | const StructTable& entry = struct_table[i]; |
| 1467 | obj = AllocateMap(entry.type, entry.size); |
| 1468 | if (obj->IsFailure()) return false; |
| 1469 | roots_[entry.index] = Map::cast(obj); |
| 1470 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1471 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1472 | obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1473 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1474 | set_hash_table_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1475 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1476 | obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1477 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1478 | set_context_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1479 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 1480 | obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1481 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1482 | set_catch_context_map(Map::cast(obj)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1483 | |
| 1484 | obj = AllocateMap(FIXED_ARRAY_TYPE, HeapObject::kHeaderSize); |
| 1485 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1486 | set_global_context_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1487 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1488 | obj = AllocateMap(SHARED_FUNCTION_INFO_TYPE, |
| 1489 | SharedFunctionInfo::kAlignedSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1490 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1491 | set_shared_function_info_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1492 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1493 | ASSERT(!Heap::InNewSpace(Heap::empty_fixed_array())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1494 | return true; |
| 1495 | } |
| 1496 | |
| 1497 | |
| 1498 | Object* Heap::AllocateHeapNumber(double value, PretenureFlag pretenure) { |
| 1499 | // Statically ensure that it is safe to allocate heap numbers in paged |
| 1500 | // spaces. |
| 1501 | STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1502 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1503 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1504 | Object* result = AllocateRaw(HeapNumber::kSize, space, OLD_DATA_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1505 | if (result->IsFailure()) return result; |
| 1506 | |
| 1507 | HeapObject::cast(result)->set_map(heap_number_map()); |
| 1508 | HeapNumber::cast(result)->set_value(value); |
| 1509 | return result; |
| 1510 | } |
| 1511 | |
| 1512 | |
| 1513 | Object* Heap::AllocateHeapNumber(double value) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1514 | // Use general version, if we're forced to always allocate. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1515 | if (always_allocate()) return AllocateHeapNumber(value, TENURED); |
| 1516 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1517 | // This version of AllocateHeapNumber is optimized for |
| 1518 | // allocation in new space. |
| 1519 | STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize); |
| 1520 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1521 | Object* result = new_space_.AllocateRaw(HeapNumber::kSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1522 | if (result->IsFailure()) return result; |
| 1523 | HeapObject::cast(result)->set_map(heap_number_map()); |
| 1524 | HeapNumber::cast(result)->set_value(value); |
| 1525 | return result; |
| 1526 | } |
| 1527 | |
| 1528 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1529 | Object* Heap::AllocateJSGlobalPropertyCell(Object* value) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1530 | Object* result = AllocateRawCell(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1531 | if (result->IsFailure()) return result; |
| 1532 | HeapObject::cast(result)->set_map(global_property_cell_map()); |
| 1533 | JSGlobalPropertyCell::cast(result)->set_value(value); |
| 1534 | return result; |
| 1535 | } |
| 1536 | |
| 1537 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1538 | Object* Heap::CreateOddball(const char* to_string, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1539 | Object* to_number) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1540 | Object* result = Allocate(oddball_map(), OLD_DATA_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1541 | if (result->IsFailure()) return result; |
| 1542 | return Oddball::cast(result)->Initialize(to_string, to_number); |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | bool Heap::CreateApiObjects() { |
| 1547 | Object* obj; |
| 1548 | |
| 1549 | obj = AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 1550 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1551 | set_neander_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1552 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1553 | obj = Heap::AllocateJSObjectFromMap(neander_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1554 | if (obj->IsFailure()) return false; |
| 1555 | Object* elements = AllocateFixedArray(2); |
| 1556 | if (elements->IsFailure()) return false; |
| 1557 | FixedArray::cast(elements)->set(0, Smi::FromInt(0)); |
| 1558 | JSObject::cast(obj)->set_elements(FixedArray::cast(elements)); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1559 | set_message_listeners(JSObject::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1560 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1561 | return true; |
| 1562 | } |
| 1563 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1564 | |
| 1565 | void Heap::CreateCEntryStub() { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1566 | CEntryStub stub(1); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1567 | set_c_entry_code(*stub.GetCode()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1571 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1572 | void Heap::CreateRegExpCEntryStub() { |
| 1573 | RegExpCEntryStub stub; |
| 1574 | set_re_c_entry_code(*stub.GetCode()); |
| 1575 | } |
| 1576 | #endif |
| 1577 | |
| 1578 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1579 | void Heap::CreateJSEntryStub() { |
| 1580 | JSEntryStub stub; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1581 | set_js_entry_code(*stub.GetCode()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | |
| 1585 | void Heap::CreateJSConstructEntryStub() { |
| 1586 | JSConstructEntryStub stub; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1587 | set_js_construct_entry_code(*stub.GetCode()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1591 | void Heap::CreateFixedStubs() { |
| 1592 | // Here we create roots for fixed stubs. They are needed at GC |
| 1593 | // for cooking and uncooking (check out frames.cc). |
| 1594 | // The eliminates the need for doing dictionary lookup in the |
| 1595 | // stub cache for these stubs. |
| 1596 | HandleScope scope; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1597 | // gcc-4.4 has problem generating correct code of following snippet: |
| 1598 | // { CEntryStub stub; |
| 1599 | // c_entry_code_ = *stub.GetCode(); |
| 1600 | // } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1601 | // { DebuggerStatementStub stub; |
| 1602 | // debugger_statement_code_ = *stub.GetCode(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1603 | // } |
| 1604 | // To workaround the problem, make separate functions without inlining. |
| 1605 | Heap::CreateCEntryStub(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1606 | Heap::CreateJSEntryStub(); |
| 1607 | Heap::CreateJSConstructEntryStub(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1608 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1609 | Heap::CreateRegExpCEntryStub(); |
| 1610 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | bool Heap::CreateInitialObjects() { |
| 1615 | Object* obj; |
| 1616 | |
| 1617 | // The -0 value must be set before NumberFromDouble works. |
| 1618 | obj = AllocateHeapNumber(-0.0, TENURED); |
| 1619 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1620 | set_minus_zero_value(obj); |
| 1621 | ASSERT(signbit(minus_zero_value()->Number()) != 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1622 | |
| 1623 | obj = AllocateHeapNumber(OS::nan_value(), TENURED); |
| 1624 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1625 | set_nan_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1626 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1627 | obj = Allocate(oddball_map(), OLD_DATA_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1628 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1629 | set_undefined_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1630 | ASSERT(!InNewSpace(undefined_value())); |
| 1631 | |
| 1632 | // Allocate initial symbol table. |
| 1633 | obj = SymbolTable::Allocate(kInitialSymbolTableSize); |
| 1634 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1635 | // Don't use set_symbol_table() due to asserts. |
| 1636 | roots_[kSymbolTableRootIndex] = obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1637 | |
| 1638 | // Assign the print strings for oddballs after creating symboltable. |
| 1639 | Object* symbol = LookupAsciiSymbol("undefined"); |
| 1640 | if (symbol->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1641 | Oddball::cast(undefined_value())->set_to_string(String::cast(symbol)); |
| 1642 | Oddball::cast(undefined_value())->set_to_number(nan_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1643 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1644 | // Allocate the null_value |
| 1645 | obj = Oddball::cast(null_value())->Initialize("null", Smi::FromInt(0)); |
| 1646 | if (obj->IsFailure()) return false; |
| 1647 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1648 | obj = CreateOddball("true", Smi::FromInt(1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1649 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1650 | set_true_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1651 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1652 | obj = CreateOddball("false", Smi::FromInt(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1653 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1654 | set_false_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1655 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1656 | obj = CreateOddball("hole", Smi::FromInt(-1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1657 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1658 | set_the_hole_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1659 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1660 | obj = CreateOddball("no_interceptor_result_sentinel", Smi::FromInt(-2)); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1661 | if (obj->IsFailure()) return false; |
| 1662 | set_no_interceptor_result_sentinel(obj); |
| 1663 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1664 | obj = CreateOddball("termination_exception", Smi::FromInt(-3)); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 1665 | if (obj->IsFailure()) return false; |
| 1666 | set_termination_exception(obj); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1667 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1668 | // Allocate the empty string. |
| 1669 | obj = AllocateRawAsciiString(0, TENURED); |
| 1670 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1671 | set_empty_string(String::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1672 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1673 | for (unsigned i = 0; i < ARRAY_SIZE(constant_symbol_table); i++) { |
| 1674 | obj = LookupAsciiSymbol(constant_symbol_table[i].contents); |
| 1675 | if (obj->IsFailure()) return false; |
| 1676 | roots_[constant_symbol_table[i].index] = String::cast(obj); |
| 1677 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1678 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1679 | // Allocate the hidden symbol which is used to identify the hidden properties |
| 1680 | // in JSObjects. The hash code has a special value so that it will not match |
| 1681 | // the empty string when searching for the property. It cannot be part of the |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1682 | // loop above because it needs to be allocated manually with the special |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1683 | // hash code in place. The hash code for the hidden_symbol is zero to ensure |
| 1684 | // that it will always be at the first entry in property descriptors. |
| 1685 | obj = AllocateSymbol(CStrVector(""), 0, String::kHashComputedMask); |
| 1686 | if (obj->IsFailure()) return false; |
| 1687 | hidden_symbol_ = String::cast(obj); |
| 1688 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1689 | // Allocate the proxy for __proto__. |
| 1690 | obj = AllocateProxy((Address) &Accessors::ObjectPrototype); |
| 1691 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1692 | set_prototype_accessors(Proxy::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1693 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1694 | // Allocate the code_stubs dictionary. The initial size is set to avoid |
| 1695 | // expanding the dictionary during bootstrapping. |
| 1696 | obj = NumberDictionary::Allocate(128); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1697 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1698 | set_code_stubs(NumberDictionary::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1699 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1700 | // Allocate the non_monomorphic_cache used in stub-cache.cc. The initial size |
| 1701 | // is set to avoid expanding the dictionary during bootstrapping. |
| 1702 | obj = NumberDictionary::Allocate(64); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1703 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1704 | set_non_monomorphic_cache(NumberDictionary::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1705 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1706 | set_instanceof_cache_function(Smi::FromInt(0)); |
| 1707 | set_instanceof_cache_map(Smi::FromInt(0)); |
| 1708 | set_instanceof_cache_answer(Smi::FromInt(0)); |
| 1709 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1710 | CreateFixedStubs(); |
| 1711 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1712 | if (InitializeNumberStringCache()->IsFailure()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1713 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1714 | // Allocate cache for single character ASCII strings. |
| 1715 | obj = AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1716 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1717 | set_single_character_string_cache(FixedArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1718 | |
| 1719 | // Allocate cache for external strings pointing to native source code. |
| 1720 | obj = AllocateFixedArray(Natives::GetBuiltinsCount()); |
| 1721 | if (obj->IsFailure()) return false; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1722 | set_natives_source_cache(FixedArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1723 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1724 | // Handling of script id generation is in Factory::NewScript. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1725 | set_last_script_id(undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1726 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1727 | // Initialize keyed lookup cache. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1728 | KeyedLookupCache::Clear(); |
| 1729 | |
| 1730 | // Initialize context slot cache. |
| 1731 | ContextSlotCache::Clear(); |
| 1732 | |
| 1733 | // Initialize descriptor cache. |
| 1734 | DescriptorLookupCache::Clear(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1735 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1736 | // Initialize compilation cache. |
| 1737 | CompilationCache::Clear(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1738 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1743 | Object* Heap::InitializeNumberStringCache() { |
| 1744 | // Compute the size of the number string cache based on the max heap size. |
| 1745 | // max_semispace_size_ == 512 KB => number_string_cache_size = 32. |
| 1746 | // max_semispace_size_ == 8 MB => number_string_cache_size = 16KB. |
| 1747 | int number_string_cache_size = max_semispace_size_ / 512; |
| 1748 | number_string_cache_size = Max(32, Min(16*KB, number_string_cache_size)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1749 | Object* obj = AllocateFixedArray(number_string_cache_size * 2, TENURED); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1750 | if (!obj->IsFailure()) set_number_string_cache(FixedArray::cast(obj)); |
| 1751 | return obj; |
| 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | void Heap::FlushNumberStringCache() { |
| 1756 | // Flush the number to string cache. |
| 1757 | int len = number_string_cache()->length(); |
| 1758 | for (int i = 0; i < len; i++) { |
| 1759 | number_string_cache()->set_undefined(i); |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1764 | static inline int double_get_hash(double d) { |
| 1765 | DoubleRepresentation rep(d); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1766 | return static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | static inline int smi_get_hash(Smi* smi) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1771 | return smi->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1775 | Object* Heap::GetNumberStringCache(Object* number) { |
| 1776 | int hash; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1777 | int mask = (number_string_cache()->length() >> 1) - 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1778 | if (number->IsSmi()) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1779 | hash = smi_get_hash(Smi::cast(number)) & mask; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1780 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1781 | hash = double_get_hash(number->Number()) & mask; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1782 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1783 | Object* key = number_string_cache()->get(hash * 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1784 | if (key == number) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1785 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1786 | } else if (key->IsHeapNumber() && |
| 1787 | number->IsHeapNumber() && |
| 1788 | key->Number() == number->Number()) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1789 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1790 | } |
| 1791 | return undefined_value(); |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | void Heap::SetNumberStringCache(Object* number, String* string) { |
| 1796 | int hash; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1797 | int mask = (number_string_cache()->length() >> 1) - 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1798 | if (number->IsSmi()) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1799 | hash = smi_get_hash(Smi::cast(number)) & mask; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1800 | number_string_cache()->set(hash * 2, Smi::cast(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1801 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1802 | hash = double_get_hash(number->Number()) & mask; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1803 | number_string_cache()->set(hash * 2, number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1804 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1805 | number_string_cache()->set(hash * 2 + 1, string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1809 | Object* Heap::NumberToString(Object* number, bool check_number_string_cache) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1810 | Counters::number_to_string_runtime.Increment(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 1811 | if (check_number_string_cache) { |
| 1812 | Object* cached = GetNumberStringCache(number); |
| 1813 | if (cached != undefined_value()) { |
| 1814 | return cached; |
| 1815 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | char arr[100]; |
| 1819 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 1820 | const char* str; |
| 1821 | if (number->IsSmi()) { |
| 1822 | int num = Smi::cast(number)->value(); |
| 1823 | str = IntToCString(num, buffer); |
| 1824 | } else { |
| 1825 | double num = HeapNumber::cast(number)->value(); |
| 1826 | str = DoubleToCString(num, buffer); |
| 1827 | } |
| 1828 | Object* result = AllocateStringFromAscii(CStrVector(str)); |
| 1829 | |
| 1830 | if (!result->IsFailure()) { |
| 1831 | SetNumberStringCache(number, String::cast(result)); |
| 1832 | } |
| 1833 | return result; |
| 1834 | } |
| 1835 | |
| 1836 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1837 | Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) { |
| 1838 | return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]); |
| 1839 | } |
| 1840 | |
| 1841 | |
| 1842 | Heap::RootListIndex Heap::RootIndexForExternalArrayType( |
| 1843 | ExternalArrayType array_type) { |
| 1844 | switch (array_type) { |
| 1845 | case kExternalByteArray: |
| 1846 | return kExternalByteArrayMapRootIndex; |
| 1847 | case kExternalUnsignedByteArray: |
| 1848 | return kExternalUnsignedByteArrayMapRootIndex; |
| 1849 | case kExternalShortArray: |
| 1850 | return kExternalShortArrayMapRootIndex; |
| 1851 | case kExternalUnsignedShortArray: |
| 1852 | return kExternalUnsignedShortArrayMapRootIndex; |
| 1853 | case kExternalIntArray: |
| 1854 | return kExternalIntArrayMapRootIndex; |
| 1855 | case kExternalUnsignedIntArray: |
| 1856 | return kExternalUnsignedIntArrayMapRootIndex; |
| 1857 | case kExternalFloatArray: |
| 1858 | return kExternalFloatArrayMapRootIndex; |
| 1859 | default: |
| 1860 | UNREACHABLE(); |
| 1861 | return kUndefinedValueRootIndex; |
| 1862 | } |
| 1863 | } |
| 1864 | |
| 1865 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1866 | Object* Heap::NumberFromDouble(double value, PretenureFlag pretenure) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1867 | // We need to distinguish the minus zero value and this cannot be |
| 1868 | // done after conversion to int. Doing this by comparing bit |
| 1869 | // patterns is faster than using fpclassify() et al. |
| 1870 | static const DoubleRepresentation minus_zero(-0.0); |
| 1871 | |
| 1872 | DoubleRepresentation rep(value); |
| 1873 | if (rep.bits == minus_zero.bits) { |
| 1874 | return AllocateHeapNumber(-0.0, pretenure); |
| 1875 | } |
| 1876 | |
| 1877 | int int_value = FastD2I(value); |
| 1878 | if (value == int_value && Smi::IsValid(int_value)) { |
| 1879 | return Smi::FromInt(int_value); |
| 1880 | } |
| 1881 | |
| 1882 | // Materialize the value in the heap. |
| 1883 | return AllocateHeapNumber(value, pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | |
| 1887 | Object* Heap::AllocateProxy(Address proxy, PretenureFlag pretenure) { |
| 1888 | // Statically ensure that it is safe to allocate proxies in paged spaces. |
| 1889 | STATIC_ASSERT(Proxy::kSize <= Page::kMaxHeapObjectSize); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1890 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1891 | Object* result = Allocate(proxy_map(), space); |
| 1892 | if (result->IsFailure()) return result; |
| 1893 | |
| 1894 | Proxy::cast(result)->set_proxy(proxy); |
| 1895 | return result; |
| 1896 | } |
| 1897 | |
| 1898 | |
| 1899 | Object* Heap::AllocateSharedFunctionInfo(Object* name) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1900 | Object* result = Allocate(shared_function_info_map(), OLD_POINTER_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1901 | if (result->IsFailure()) return result; |
| 1902 | |
| 1903 | SharedFunctionInfo* share = SharedFunctionInfo::cast(result); |
| 1904 | share->set_name(name); |
| 1905 | Code* illegal = Builtins::builtin(Builtins::Illegal); |
| 1906 | share->set_code(illegal); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1907 | Code* construct_stub = Builtins::builtin(Builtins::JSConstructStubGeneric); |
| 1908 | share->set_construct_stub(construct_stub); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1909 | share->set_expected_nof_properties(0); |
| 1910 | share->set_length(0); |
| 1911 | share->set_formal_parameter_count(0); |
| 1912 | share->set_instance_class_name(Object_symbol()); |
| 1913 | share->set_function_data(undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1914 | share->set_script(undefined_value()); |
| 1915 | share->set_start_position_and_type(0); |
| 1916 | share->set_debug_info(undefined_value()); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 1917 | share->set_inferred_name(empty_string()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1918 | share->set_compiler_hints(0); |
| 1919 | share->set_this_property_assignments_count(0); |
| 1920 | share->set_this_property_assignments(undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1921 | return result; |
| 1922 | } |
| 1923 | |
| 1924 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 1925 | // Returns true for a character in a range. Both limits are inclusive. |
| 1926 | static inline bool Between(uint32_t character, uint32_t from, uint32_t to) { |
| 1927 | // This makes uses of the the unsigned wraparound. |
| 1928 | return character - from <= to - from; |
| 1929 | } |
| 1930 | |
| 1931 | |
| 1932 | static inline Object* MakeOrFindTwoCharacterString(uint32_t c1, uint32_t c2) { |
| 1933 | String* symbol; |
| 1934 | // Numeric strings have a different hash algorithm not known by |
| 1935 | // LookupTwoCharsSymbolIfExists, so we skip this step for such strings. |
| 1936 | if ((!Between(c1, '0', '9') || !Between(c2, '0', '9')) && |
| 1937 | Heap::symbol_table()->LookupTwoCharsSymbolIfExists(c1, c2, &symbol)) { |
| 1938 | return symbol; |
| 1939 | // Now we know the length is 2, we might as well make use of that fact |
| 1940 | // when building the new string. |
| 1941 | } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this |
| 1942 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this. |
| 1943 | Object* result = Heap::AllocateRawAsciiString(2); |
| 1944 | if (result->IsFailure()) return result; |
| 1945 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 1946 | dest[0] = c1; |
| 1947 | dest[1] = c2; |
| 1948 | return result; |
| 1949 | } else { |
| 1950 | Object* result = Heap::AllocateRawTwoByteString(2); |
| 1951 | if (result->IsFailure()) return result; |
| 1952 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 1953 | dest[0] = c1; |
| 1954 | dest[1] = c2; |
| 1955 | return result; |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1960 | Object* Heap::AllocateConsString(String* first, String* second) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1961 | int first_length = first->length(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1962 | if (first_length == 0) { |
| 1963 | return second; |
| 1964 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1965 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1966 | int second_length = second->length(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 1967 | if (second_length == 0) { |
| 1968 | return first; |
| 1969 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1970 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1971 | int length = first_length + second_length; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 1972 | |
| 1973 | // Optimization for 2-byte strings often used as keys in a decompression |
| 1974 | // dictionary. Check whether we already have the string in the symbol |
| 1975 | // table to prevent creation of many unneccesary strings. |
| 1976 | if (length == 2) { |
| 1977 | unsigned c1 = first->Get(0); |
| 1978 | unsigned c2 = second->Get(0); |
| 1979 | return MakeOrFindTwoCharacterString(c1, c2); |
| 1980 | } |
| 1981 | |
ricow@chromium.org | aa1b616 | 2010-03-29 07:44:58 +0000 | [diff] [blame] | 1982 | bool first_is_ascii = first->IsAsciiRepresentation(); |
| 1983 | bool second_is_ascii = second->IsAsciiRepresentation(); |
| 1984 | bool is_ascii = first_is_ascii && second_is_ascii; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1985 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1986 | // Make sure that an out of memory exception is thrown if the length |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1987 | // of the new cons string is too large. |
| 1988 | if (length > String::kMaxLength || length < 0) { |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 1989 | Top::context()->mark_out_of_memory(); |
| 1990 | return Failure::OutOfMemoryException(); |
| 1991 | } |
| 1992 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1993 | // If the resulting string is small make a flat string. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1994 | if (length < String::kMinNonFlatLength) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 1995 | ASSERT(first->IsFlat()); |
| 1996 | ASSERT(second->IsFlat()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1997 | if (is_ascii) { |
| 1998 | Object* result = AllocateRawAsciiString(length); |
| 1999 | if (result->IsFailure()) return result; |
| 2000 | // Copy the characters into the new object. |
| 2001 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2002 | // Copy first part. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2003 | const char* src; |
| 2004 | if (first->IsExternalString()) { |
| 2005 | src = ExternalAsciiString::cast(first)->resource()->data(); |
| 2006 | } else { |
| 2007 | src = SeqAsciiString::cast(first)->GetChars(); |
| 2008 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2009 | for (int i = 0; i < first_length; i++) *dest++ = src[i]; |
| 2010 | // Copy second part. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2011 | if (second->IsExternalString()) { |
| 2012 | src = ExternalAsciiString::cast(second)->resource()->data(); |
| 2013 | } else { |
| 2014 | src = SeqAsciiString::cast(second)->GetChars(); |
| 2015 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2016 | for (int i = 0; i < second_length; i++) *dest++ = src[i]; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2017 | return result; |
| 2018 | } else { |
ricow@chromium.org | aa1b616 | 2010-03-29 07:44:58 +0000 | [diff] [blame] | 2019 | // For short external two-byte strings we check whether they can |
| 2020 | // be represented using ascii. |
| 2021 | if (!first_is_ascii) { |
| 2022 | first_is_ascii = first->IsExternalTwoByteStringWithAsciiChars(); |
| 2023 | } |
| 2024 | if (first_is_ascii && !second_is_ascii) { |
| 2025 | second_is_ascii = second->IsExternalTwoByteStringWithAsciiChars(); |
| 2026 | } |
| 2027 | if (first_is_ascii && second_is_ascii) { |
| 2028 | Object* result = AllocateRawAsciiString(length); |
| 2029 | if (result->IsFailure()) return result; |
| 2030 | // Copy the characters into the new object. |
| 2031 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 2032 | String::WriteToFlat(first, dest, 0, first_length); |
| 2033 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
| 2034 | Counters::string_add_runtime_ext_to_ascii.Increment(); |
| 2035 | return result; |
| 2036 | } |
| 2037 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2038 | Object* result = AllocateRawTwoByteString(length); |
| 2039 | if (result->IsFailure()) return result; |
| 2040 | // Copy the characters into the new object. |
| 2041 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2042 | String::WriteToFlat(first, dest, 0, first_length); |
| 2043 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2044 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2048 | Map* map = is_ascii ? cons_ascii_string_map() : cons_string_map(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2049 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2050 | Object* result = Allocate(map, NEW_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | if (result->IsFailure()) return result; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2052 | |
| 2053 | AssertNoAllocation no_gc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2054 | ConsString* cons_string = ConsString::cast(result); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2055 | WriteBarrierMode mode = cons_string->GetWriteBarrierMode(no_gc); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2056 | cons_string->set_length(length); |
| 2057 | cons_string->set_hash_field(String::kEmptyHashField); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2058 | cons_string->set_first(first, mode); |
| 2059 | cons_string->set_second(second, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2060 | return result; |
| 2061 | } |
| 2062 | |
| 2063 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2064 | Object* Heap::AllocateSubString(String* buffer, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2065 | int start, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2066 | int end, |
| 2067 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2068 | int length = end - start; |
| 2069 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2070 | if (length == 1) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2071 | return Heap::LookupSingleCharacterStringFromCode( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2072 | buffer->Get(start)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2073 | } else if (length == 2) { |
| 2074 | // Optimization for 2-byte strings often used as keys in a decompression |
| 2075 | // dictionary. Check whether we already have the string in the symbol |
| 2076 | // table to prevent creation of many unneccesary strings. |
| 2077 | unsigned c1 = buffer->Get(start); |
| 2078 | unsigned c2 = buffer->Get(start + 1); |
| 2079 | return MakeOrFindTwoCharacterString(c1, c2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2082 | // Make an attempt to flatten the buffer to reduce access time. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 2083 | buffer = buffer->TryFlattenGetString(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2084 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 2085 | Object* result = buffer->IsAsciiRepresentation() |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2086 | ? AllocateRawAsciiString(length, pretenure ) |
| 2087 | : AllocateRawTwoByteString(length, pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2088 | if (result->IsFailure()) return result; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2089 | String* string_result = String::cast(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2090 | // Copy the characters into the new object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2091 | if (buffer->IsAsciiRepresentation()) { |
| 2092 | ASSERT(string_result->IsAsciiRepresentation()); |
| 2093 | char* dest = SeqAsciiString::cast(string_result)->GetChars(); |
| 2094 | String::WriteToFlat(buffer, dest, start, end); |
| 2095 | } else { |
| 2096 | ASSERT(string_result->IsTwoByteRepresentation()); |
| 2097 | uc16* dest = SeqTwoByteString::cast(string_result)->GetChars(); |
| 2098 | String::WriteToFlat(buffer, dest, start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2099 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2100 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2101 | return result; |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | Object* Heap::AllocateExternalStringFromAscii( |
| 2106 | ExternalAsciiString::Resource* resource) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2107 | size_t length = resource->length(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2108 | if (length > static_cast<size_t>(String::kMaxLength)) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2109 | Top::context()->mark_out_of_memory(); |
| 2110 | return Failure::OutOfMemoryException(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2113 | Map* map = external_ascii_string_map(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2114 | Object* result = Allocate(map, NEW_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2115 | if (result->IsFailure()) return result; |
| 2116 | |
| 2117 | ExternalAsciiString* external_string = ExternalAsciiString::cast(result); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2118 | external_string->set_length(static_cast<int>(length)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2119 | external_string->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2120 | external_string->set_resource(resource); |
| 2121 | |
| 2122 | return result; |
| 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | Object* Heap::AllocateExternalStringFromTwoByte( |
| 2127 | ExternalTwoByteString::Resource* resource) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2128 | size_t length = resource->length(); |
| 2129 | if (length > static_cast<size_t>(String::kMaxLength)) { |
| 2130 | Top::context()->mark_out_of_memory(); |
| 2131 | return Failure::OutOfMemoryException(); |
| 2132 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2133 | |
| 2134 | Map* map = Heap::external_string_map(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2135 | Object* result = Allocate(map, NEW_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2136 | if (result->IsFailure()) return result; |
| 2137 | |
| 2138 | ExternalTwoByteString* external_string = ExternalTwoByteString::cast(result); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2139 | external_string->set_length(static_cast<int>(length)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2140 | external_string->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2141 | external_string->set_resource(resource); |
| 2142 | |
| 2143 | return result; |
| 2144 | } |
| 2145 | |
| 2146 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2147 | Object* Heap::LookupSingleCharacterStringFromCode(uint16_t code) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2148 | if (code <= String::kMaxAsciiCharCode) { |
| 2149 | Object* value = Heap::single_character_string_cache()->get(code); |
| 2150 | if (value != Heap::undefined_value()) return value; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2151 | |
| 2152 | char buffer[1]; |
| 2153 | buffer[0] = static_cast<char>(code); |
| 2154 | Object* result = LookupSymbol(Vector<const char>(buffer, 1)); |
| 2155 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2156 | if (result->IsFailure()) return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2157 | Heap::single_character_string_cache()->set(code, result); |
| 2158 | return result; |
| 2159 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2160 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2161 | Object* result = Heap::AllocateRawTwoByteString(1); |
| 2162 | if (result->IsFailure()) return result; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2163 | String* answer = String::cast(result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2164 | answer->Set(0, code); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2165 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2169 | Object* Heap::AllocateByteArray(int length, PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2170 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 2171 | return Failure::OutOfMemoryException(); |
| 2172 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2173 | if (pretenure == NOT_TENURED) { |
| 2174 | return AllocateByteArray(length); |
| 2175 | } |
| 2176 | int size = ByteArray::SizeFor(length); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2177 | Object* result = (size <= MaxObjectSizeInPagedSpace()) |
| 2178 | ? old_data_space_->AllocateRaw(size) |
| 2179 | : lo_space_->AllocateRaw(size); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2180 | if (result->IsFailure()) return result; |
| 2181 | |
| 2182 | reinterpret_cast<Array*>(result)->set_map(byte_array_map()); |
| 2183 | reinterpret_cast<Array*>(result)->set_length(length); |
| 2184 | return result; |
| 2185 | } |
| 2186 | |
| 2187 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2188 | Object* Heap::AllocateByteArray(int length) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2189 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 2190 | return Failure::OutOfMemoryException(); |
| 2191 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2192 | int size = ByteArray::SizeFor(length); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2193 | AllocationSpace space = |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2194 | (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : NEW_SPACE; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2195 | Object* result = AllocateRaw(size, space, OLD_DATA_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2196 | if (result->IsFailure()) return result; |
| 2197 | |
| 2198 | reinterpret_cast<Array*>(result)->set_map(byte_array_map()); |
| 2199 | reinterpret_cast<Array*>(result)->set_length(length); |
| 2200 | return result; |
| 2201 | } |
| 2202 | |
| 2203 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 2204 | void Heap::CreateFillerObjectAt(Address addr, int size) { |
| 2205 | if (size == 0) return; |
| 2206 | HeapObject* filler = HeapObject::FromAddress(addr); |
| 2207 | if (size == kPointerSize) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2208 | filler->set_map(one_pointer_filler_map()); |
| 2209 | } else if (size == 2 * kPointerSize) { |
| 2210 | filler->set_map(two_pointer_filler_map()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 2211 | } else { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2212 | filler->set_map(byte_array_map()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 2213 | ByteArray::cast(filler)->set_length(ByteArray::LengthFor(size)); |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2218 | Object* Heap::AllocatePixelArray(int length, |
| 2219 | uint8_t* external_pointer, |
| 2220 | PretenureFlag pretenure) { |
| 2221 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2222 | Object* result = AllocateRaw(PixelArray::kAlignedSize, space, OLD_DATA_SPACE); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2223 | if (result->IsFailure()) return result; |
| 2224 | |
| 2225 | reinterpret_cast<PixelArray*>(result)->set_map(pixel_array_map()); |
| 2226 | reinterpret_cast<PixelArray*>(result)->set_length(length); |
| 2227 | reinterpret_cast<PixelArray*>(result)->set_external_pointer(external_pointer); |
| 2228 | |
| 2229 | return result; |
| 2230 | } |
| 2231 | |
| 2232 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2233 | Object* Heap::AllocateExternalArray(int length, |
| 2234 | ExternalArrayType array_type, |
| 2235 | void* external_pointer, |
| 2236 | PretenureFlag pretenure) { |
| 2237 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2238 | Object* result = AllocateRaw(ExternalArray::kAlignedSize, |
| 2239 | space, |
| 2240 | OLD_DATA_SPACE); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2241 | if (result->IsFailure()) return result; |
| 2242 | |
| 2243 | reinterpret_cast<ExternalArray*>(result)->set_map( |
| 2244 | MapForExternalArrayType(array_type)); |
| 2245 | reinterpret_cast<ExternalArray*>(result)->set_length(length); |
| 2246 | reinterpret_cast<ExternalArray*>(result)->set_external_pointer( |
| 2247 | external_pointer); |
| 2248 | |
| 2249 | return result; |
| 2250 | } |
| 2251 | |
| 2252 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2253 | Object* Heap::CreateCode(const CodeDesc& desc, |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 2254 | ZoneScopeInfo* sinfo, |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2255 | Code::Flags flags, |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 2256 | Handle<Object> self_reference) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2257 | // Compute size |
| 2258 | int body_size = RoundUp(desc.instr_size + desc.reloc_size, kObjectAlignment); |
| 2259 | int sinfo_size = 0; |
| 2260 | if (sinfo != NULL) sinfo_size = sinfo->Serialize(NULL); |
| 2261 | int obj_size = Code::SizeFor(body_size, sinfo_size); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 2262 | ASSERT(IsAligned(obj_size, Code::kCodeAlignment)); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2263 | Object* result; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2264 | if (obj_size > MaxObjectSizeInPagedSpace()) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2265 | result = lo_space_->AllocateRawCode(obj_size); |
| 2266 | } else { |
| 2267 | result = code_space_->AllocateRaw(obj_size); |
| 2268 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2269 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2270 | if (result->IsFailure()) return result; |
| 2271 | |
| 2272 | // Initialize the object |
| 2273 | HeapObject::cast(result)->set_map(code_map()); |
| 2274 | Code* code = Code::cast(result); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2275 | ASSERT(!CodeRange::exists() || CodeRange::contains(code->address())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2276 | code->set_instruction_size(desc.instr_size); |
| 2277 | code->set_relocation_size(desc.reloc_size); |
| 2278 | code->set_sinfo_size(sinfo_size); |
| 2279 | code->set_flags(flags); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 2280 | // Allow self references to created code object by patching the handle to |
| 2281 | // point to the newly allocated Code object. |
| 2282 | if (!self_reference.is_null()) { |
| 2283 | *(self_reference.location()) = code; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2284 | } |
| 2285 | // Migrate generated code. |
| 2286 | // The generated code can contain Object** values (typically from handles) |
| 2287 | // that are dereferenced during the copy to point directly to the actual heap |
| 2288 | // objects. These pointers can include references to the code object itself, |
| 2289 | // through the self_reference parameter. |
| 2290 | code->CopyFrom(desc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2291 | if (sinfo != NULL) sinfo->Serialize(code); // write scope info |
| 2292 | |
| 2293 | #ifdef DEBUG |
| 2294 | code->Verify(); |
| 2295 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2296 | return code; |
| 2297 | } |
| 2298 | |
| 2299 | |
| 2300 | Object* Heap::CopyCode(Code* code) { |
| 2301 | // Allocate an object the same size as the code object. |
| 2302 | int obj_size = code->Size(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2303 | Object* result; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2304 | if (obj_size > MaxObjectSizeInPagedSpace()) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2305 | result = lo_space_->AllocateRawCode(obj_size); |
| 2306 | } else { |
| 2307 | result = code_space_->AllocateRaw(obj_size); |
| 2308 | } |
| 2309 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2310 | if (result->IsFailure()) return result; |
| 2311 | |
| 2312 | // Copy code object. |
| 2313 | Address old_addr = code->address(); |
| 2314 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2315 | CopyBlock(reinterpret_cast<Object**>(new_addr), |
| 2316 | reinterpret_cast<Object**>(old_addr), |
| 2317 | obj_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2318 | // Relocate the copy. |
| 2319 | Code* new_code = Code::cast(result); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2320 | ASSERT(!CodeRange::exists() || CodeRange::contains(code->address())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2321 | new_code->Relocate(new_addr - old_addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2322 | return new_code; |
| 2323 | } |
| 2324 | |
| 2325 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2326 | Object* Heap::CopyCode(Code* code, Vector<byte> reloc_info) { |
| 2327 | int new_body_size = RoundUp(code->instruction_size() + reloc_info.length(), |
| 2328 | kObjectAlignment); |
| 2329 | |
| 2330 | int sinfo_size = code->sinfo_size(); |
| 2331 | |
| 2332 | int new_obj_size = Code::SizeFor(new_body_size, sinfo_size); |
| 2333 | |
| 2334 | Address old_addr = code->address(); |
| 2335 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2336 | size_t relocation_offset = |
| 2337 | static_cast<size_t>(code->relocation_start() - old_addr); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2338 | |
| 2339 | Object* result; |
| 2340 | if (new_obj_size > MaxObjectSizeInPagedSpace()) { |
| 2341 | result = lo_space_->AllocateRawCode(new_obj_size); |
| 2342 | } else { |
| 2343 | result = code_space_->AllocateRaw(new_obj_size); |
| 2344 | } |
| 2345 | |
| 2346 | if (result->IsFailure()) return result; |
| 2347 | |
| 2348 | // Copy code object. |
| 2349 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
| 2350 | |
| 2351 | // Copy header and instructions. |
| 2352 | memcpy(new_addr, old_addr, relocation_offset); |
| 2353 | |
| 2354 | // Copy patched rinfo. |
| 2355 | memcpy(new_addr + relocation_offset, |
| 2356 | reloc_info.start(), |
| 2357 | reloc_info.length()); |
| 2358 | |
| 2359 | Code* new_code = Code::cast(result); |
| 2360 | new_code->set_relocation_size(reloc_info.length()); |
| 2361 | |
| 2362 | // Copy sinfo. |
| 2363 | memcpy(new_code->sinfo_start(), code->sinfo_start(), code->sinfo_size()); |
| 2364 | |
| 2365 | // Relocate the copy. |
| 2366 | ASSERT(!CodeRange::exists() || CodeRange::contains(code->address())); |
| 2367 | new_code->Relocate(new_addr - old_addr); |
| 2368 | |
| 2369 | #ifdef DEBUG |
| 2370 | code->Verify(); |
| 2371 | #endif |
| 2372 | return new_code; |
| 2373 | } |
| 2374 | |
| 2375 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2376 | Object* Heap::Allocate(Map* map, AllocationSpace space) { |
| 2377 | ASSERT(gc_state_ == NOT_IN_GC); |
| 2378 | ASSERT(map->instance_type() != MAP_TYPE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2379 | // If allocation failures are disallowed, we may allocate in a different |
| 2380 | // space when new space is full and the object is not a large object. |
| 2381 | AllocationSpace retry_space = |
| 2382 | (space != NEW_SPACE) ? space : TargetSpaceId(map->instance_type()); |
| 2383 | Object* result = |
| 2384 | AllocateRaw(map->instance_size(), space, retry_space); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2385 | if (result->IsFailure()) return result; |
| 2386 | HeapObject::cast(result)->set_map(map); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2387 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2388 | ProducerHeapProfile::RecordJSObjectAllocation(result); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2389 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2390 | return result; |
| 2391 | } |
| 2392 | |
| 2393 | |
| 2394 | Object* Heap::InitializeFunction(JSFunction* function, |
| 2395 | SharedFunctionInfo* shared, |
| 2396 | Object* prototype) { |
| 2397 | ASSERT(!prototype->IsMap()); |
| 2398 | function->initialize_properties(); |
| 2399 | function->initialize_elements(); |
| 2400 | function->set_shared(shared); |
| 2401 | function->set_prototype_or_initial_map(prototype); |
| 2402 | function->set_context(undefined_value()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2403 | function->set_literals(empty_fixed_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2404 | return function; |
| 2405 | } |
| 2406 | |
| 2407 | |
| 2408 | Object* Heap::AllocateFunctionPrototype(JSFunction* function) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2409 | // Allocate the prototype. Make sure to use the object function |
| 2410 | // from the function's context, since the function can be from a |
| 2411 | // different context. |
| 2412 | JSFunction* object_function = |
| 2413 | function->context()->global_context()->object_function(); |
| 2414 | Object* prototype = AllocateJSObject(object_function); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2415 | if (prototype->IsFailure()) return prototype; |
| 2416 | // When creating the prototype for the function we must set its |
| 2417 | // constructor to the function. |
| 2418 | Object* result = |
| 2419 | JSObject::cast(prototype)->SetProperty(constructor_symbol(), |
| 2420 | function, |
| 2421 | DONT_ENUM); |
| 2422 | if (result->IsFailure()) return result; |
| 2423 | return prototype; |
| 2424 | } |
| 2425 | |
| 2426 | |
| 2427 | Object* Heap::AllocateFunction(Map* function_map, |
| 2428 | SharedFunctionInfo* shared, |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 2429 | Object* prototype, |
| 2430 | PretenureFlag pretenure) { |
| 2431 | AllocationSpace space = |
| 2432 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
| 2433 | Object* result = Allocate(function_map, space); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2434 | if (result->IsFailure()) return result; |
| 2435 | return InitializeFunction(JSFunction::cast(result), shared, prototype); |
| 2436 | } |
| 2437 | |
| 2438 | |
| 2439 | Object* Heap::AllocateArgumentsObject(Object* callee, int length) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2440 | // To get fast allocation and map sharing for arguments objects we |
| 2441 | // allocate them based on an arguments boilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2442 | |
| 2443 | // This calls Copy directly rather than using Heap::AllocateRaw so we |
| 2444 | // duplicate the check here. |
| 2445 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 2446 | |
| 2447 | JSObject* boilerplate = |
| 2448 | Top::context()->global_context()->arguments_boilerplate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2449 | |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 2450 | // Check that the size of the boilerplate matches our |
| 2451 | // expectations. The ArgumentsAccessStub::GenerateNewObject relies |
| 2452 | // on the size being a known constant. |
| 2453 | ASSERT(kArgumentsObjectSize == boilerplate->map()->instance_size()); |
| 2454 | |
| 2455 | // Do the allocation. |
| 2456 | Object* result = |
| 2457 | AllocateRaw(kArgumentsObjectSize, NEW_SPACE, OLD_POINTER_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2458 | if (result->IsFailure()) return result; |
| 2459 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2460 | // Copy the content. The arguments boilerplate doesn't have any |
| 2461 | // fields that point to new space so it's safe to skip the write |
| 2462 | // barrier here. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2463 | CopyBlock(reinterpret_cast<Object**>(HeapObject::cast(result)->address()), |
| 2464 | reinterpret_cast<Object**>(boilerplate->address()), |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 2465 | kArgumentsObjectSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2466 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2467 | // Set the two properties. |
| 2468 | JSObject::cast(result)->InObjectPropertyAtPut(arguments_callee_index, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2469 | callee); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2470 | JSObject::cast(result)->InObjectPropertyAtPut(arguments_length_index, |
| 2471 | Smi::FromInt(length), |
| 2472 | SKIP_WRITE_BARRIER); |
| 2473 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2474 | // Check the state of the object |
| 2475 | ASSERT(JSObject::cast(result)->HasFastProperties()); |
| 2476 | ASSERT(JSObject::cast(result)->HasFastElements()); |
| 2477 | |
| 2478 | return result; |
| 2479 | } |
| 2480 | |
| 2481 | |
| 2482 | Object* Heap::AllocateInitialMap(JSFunction* fun) { |
| 2483 | ASSERT(!fun->has_initial_map()); |
| 2484 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2485 | // First create a new map with the size and number of in-object properties |
| 2486 | // suggested by the function. |
| 2487 | int instance_size = fun->shared()->CalculateInstanceSize(); |
| 2488 | int in_object_properties = fun->shared()->CalculateInObjectProperties(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2489 | Object* map_obj = Heap::AllocateMap(JS_OBJECT_TYPE, instance_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2490 | if (map_obj->IsFailure()) return map_obj; |
| 2491 | |
| 2492 | // Fetch or allocate prototype. |
| 2493 | Object* prototype; |
| 2494 | if (fun->has_instance_prototype()) { |
| 2495 | prototype = fun->instance_prototype(); |
| 2496 | } else { |
| 2497 | prototype = AllocateFunctionPrototype(fun); |
| 2498 | if (prototype->IsFailure()) return prototype; |
| 2499 | } |
| 2500 | Map* map = Map::cast(map_obj); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2501 | map->set_inobject_properties(in_object_properties); |
| 2502 | map->set_unused_property_fields(in_object_properties); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2503 | map->set_prototype(prototype); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2504 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2505 | // If the function has only simple this property assignments add |
| 2506 | // field descriptors for these to the initial map as the object |
| 2507 | // cannot be constructed without having these properties. Guard by |
| 2508 | // the inline_new flag so we only change the map if we generate a |
| 2509 | // specialized construct stub. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2510 | ASSERT(in_object_properties <= Map::kMaxPreAllocatedPropertyFields); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2511 | if (fun->shared()->CanGenerateInlineConstructor(prototype)) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2512 | int count = fun->shared()->this_property_assignments_count(); |
| 2513 | if (count > in_object_properties) { |
| 2514 | count = in_object_properties; |
| 2515 | } |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 2516 | Object* descriptors_obj = DescriptorArray::Allocate(count); |
| 2517 | if (descriptors_obj->IsFailure()) return descriptors_obj; |
| 2518 | DescriptorArray* descriptors = DescriptorArray::cast(descriptors_obj); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2519 | for (int i = 0; i < count; i++) { |
| 2520 | String* name = fun->shared()->GetThisPropertyAssignmentName(i); |
| 2521 | ASSERT(name->IsSymbol()); |
| 2522 | FieldDescriptor field(name, i, NONE); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2523 | field.SetEnumerationIndex(i); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2524 | descriptors->Set(i, &field); |
| 2525 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2526 | descriptors->SetNextEnumerationIndex(count); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2527 | descriptors->Sort(); |
| 2528 | map->set_instance_descriptors(descriptors); |
| 2529 | map->set_pre_allocated_property_fields(count); |
| 2530 | map->set_unused_property_fields(in_object_properties - count); |
| 2531 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2532 | return map; |
| 2533 | } |
| 2534 | |
| 2535 | |
| 2536 | void Heap::InitializeJSObjectFromMap(JSObject* obj, |
| 2537 | FixedArray* properties, |
| 2538 | Map* map) { |
| 2539 | obj->set_properties(properties); |
| 2540 | obj->initialize_elements(); |
| 2541 | // TODO(1240798): Initialize the object's body using valid initial values |
| 2542 | // according to the object's initial map. For example, if the map's |
| 2543 | // instance type is JS_ARRAY_TYPE, the length field should be initialized |
| 2544 | // to a number (eg, Smi::FromInt(0)) and the elements initialized to a |
| 2545 | // fixed array (eg, Heap::empty_fixed_array()). Currently, the object |
| 2546 | // verification code has to cope with (temporarily) invalid objects. See |
| 2547 | // for example, JSArray::JSArrayVerify). |
| 2548 | obj->InitializeBody(map->instance_size()); |
| 2549 | } |
| 2550 | |
| 2551 | |
| 2552 | Object* Heap::AllocateJSObjectFromMap(Map* map, PretenureFlag pretenure) { |
| 2553 | // JSFunctions should be allocated using AllocateFunction to be |
| 2554 | // properly initialized. |
| 2555 | ASSERT(map->instance_type() != JS_FUNCTION_TYPE); |
| 2556 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2557 | // Both types of globla objects should be allocated using |
| 2558 | // AllocateGloblaObject to be properly initialized. |
| 2559 | ASSERT(map->instance_type() != JS_GLOBAL_OBJECT_TYPE); |
| 2560 | ASSERT(map->instance_type() != JS_BUILTINS_OBJECT_TYPE); |
| 2561 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2562 | // Allocate the backing storage for the properties. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2563 | int prop_size = |
| 2564 | map->pre_allocated_property_fields() + |
| 2565 | map->unused_property_fields() - |
| 2566 | map->inobject_properties(); |
| 2567 | ASSERT(prop_size >= 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2568 | Object* properties = AllocateFixedArray(prop_size, pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2569 | if (properties->IsFailure()) return properties; |
| 2570 | |
| 2571 | // Allocate the JSObject. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2572 | AllocationSpace space = |
| 2573 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2574 | if (map->instance_size() > MaxObjectSizeInPagedSpace()) space = LO_SPACE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2575 | Object* obj = Allocate(map, space); |
| 2576 | if (obj->IsFailure()) return obj; |
| 2577 | |
| 2578 | // Initialize the JSObject. |
| 2579 | InitializeJSObjectFromMap(JSObject::cast(obj), |
| 2580 | FixedArray::cast(properties), |
| 2581 | map); |
| 2582 | return obj; |
| 2583 | } |
| 2584 | |
| 2585 | |
| 2586 | Object* Heap::AllocateJSObject(JSFunction* constructor, |
| 2587 | PretenureFlag pretenure) { |
| 2588 | // Allocate the initial map if absent. |
| 2589 | if (!constructor->has_initial_map()) { |
| 2590 | Object* initial_map = AllocateInitialMap(constructor); |
| 2591 | if (initial_map->IsFailure()) return initial_map; |
| 2592 | constructor->set_initial_map(Map::cast(initial_map)); |
| 2593 | Map::cast(initial_map)->set_constructor(constructor); |
| 2594 | } |
| 2595 | // Allocate the object based on the constructors initial map. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2596 | Object* result = |
| 2597 | AllocateJSObjectFromMap(constructor->initial_map(), pretenure); |
| 2598 | // Make sure result is NOT a global object if valid. |
| 2599 | ASSERT(result->IsFailure() || !result->IsGlobalObject()); |
| 2600 | return result; |
| 2601 | } |
| 2602 | |
| 2603 | |
| 2604 | Object* Heap::AllocateGlobalObject(JSFunction* constructor) { |
| 2605 | ASSERT(constructor->has_initial_map()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2606 | Map* map = constructor->initial_map(); |
| 2607 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2608 | // Make sure no field properties are described in the initial map. |
| 2609 | // This guarantees us that normalizing the properties does not |
| 2610 | // require us to change property values to JSGlobalPropertyCells. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2611 | ASSERT(map->NextFreePropertyIndex() == 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2612 | |
| 2613 | // Make sure we don't have a ton of pre-allocated slots in the |
| 2614 | // global objects. They will be unused once we normalize the object. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2615 | ASSERT(map->unused_property_fields() == 0); |
| 2616 | ASSERT(map->inobject_properties() == 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2617 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2618 | // Initial size of the backing store to avoid resize of the storage during |
| 2619 | // bootstrapping. The size differs between the JS global object ad the |
| 2620 | // builtins object. |
| 2621 | int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2622 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2623 | // Allocate a dictionary object for backing storage. |
| 2624 | Object* obj = |
| 2625 | StringDictionary::Allocate( |
| 2626 | map->NumberOfDescribedProperties() * 2 + initial_size); |
| 2627 | if (obj->IsFailure()) return obj; |
| 2628 | StringDictionary* dictionary = StringDictionary::cast(obj); |
| 2629 | |
| 2630 | // The global object might be created from an object template with accessors. |
| 2631 | // Fill these accessors into the dictionary. |
| 2632 | DescriptorArray* descs = map->instance_descriptors(); |
| 2633 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2634 | PropertyDetails details = descs->GetDetails(i); |
| 2635 | ASSERT(details.type() == CALLBACKS); // Only accessors are expected. |
| 2636 | PropertyDetails d = |
| 2637 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 2638 | Object* value = descs->GetCallbacksObject(i); |
| 2639 | value = Heap::AllocateJSGlobalPropertyCell(value); |
| 2640 | if (value->IsFailure()) return value; |
| 2641 | |
| 2642 | Object* result = dictionary->Add(descs->GetKey(i), value, d); |
| 2643 | if (result->IsFailure()) return result; |
| 2644 | dictionary = StringDictionary::cast(result); |
| 2645 | } |
| 2646 | |
| 2647 | // Allocate the global object and initialize it with the backing store. |
| 2648 | obj = Allocate(map, OLD_POINTER_SPACE); |
| 2649 | if (obj->IsFailure()) return obj; |
| 2650 | JSObject* global = JSObject::cast(obj); |
| 2651 | InitializeJSObjectFromMap(global, dictionary, map); |
| 2652 | |
| 2653 | // Create a new map for the global object. |
| 2654 | obj = map->CopyDropDescriptors(); |
| 2655 | if (obj->IsFailure()) return obj; |
| 2656 | Map* new_map = Map::cast(obj); |
| 2657 | |
| 2658 | // Setup the global object as a normalized object. |
| 2659 | global->set_map(new_map); |
| 2660 | global->map()->set_instance_descriptors(Heap::empty_descriptor_array()); |
| 2661 | global->set_properties(dictionary); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2662 | |
| 2663 | // Make sure result is a global object with properties in dictionary. |
| 2664 | ASSERT(global->IsGlobalObject()); |
| 2665 | ASSERT(!global->HasFastProperties()); |
| 2666 | return global; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2670 | Object* Heap::CopyJSObject(JSObject* source) { |
| 2671 | // Never used to copy functions. If functions need to be copied we |
| 2672 | // have to be careful to clear the literals array. |
| 2673 | ASSERT(!source->IsJSFunction()); |
| 2674 | |
| 2675 | // Make the clone. |
| 2676 | Map* map = source->map(); |
| 2677 | int object_size = map->instance_size(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2678 | Object* clone; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2679 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2680 | // If we're forced to always allocate, we use the general allocation |
| 2681 | // functions which may leave us with an object in old space. |
| 2682 | if (always_allocate()) { |
| 2683 | clone = AllocateRaw(object_size, NEW_SPACE, OLD_POINTER_SPACE); |
| 2684 | if (clone->IsFailure()) return clone; |
| 2685 | Address clone_address = HeapObject::cast(clone)->address(); |
| 2686 | CopyBlock(reinterpret_cast<Object**>(clone_address), |
| 2687 | reinterpret_cast<Object**>(source->address()), |
| 2688 | object_size); |
| 2689 | // Update write barrier for all fields that lie beyond the header. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 2690 | RecordWrites(clone_address, |
| 2691 | JSObject::kHeaderSize, |
antonm@chromium.org | 8e5e338 | 2010-03-24 09:56:30 +0000 | [diff] [blame] | 2692 | (object_size - JSObject::kHeaderSize) / kPointerSize); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2693 | } else { |
| 2694 | clone = new_space_.AllocateRaw(object_size); |
| 2695 | if (clone->IsFailure()) return clone; |
| 2696 | ASSERT(Heap::InNewSpace(clone)); |
| 2697 | // Since we know the clone is allocated in new space, we can copy |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 2698 | // the contents without worrying about updating the write barrier. |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2699 | CopyBlock(reinterpret_cast<Object**>(HeapObject::cast(clone)->address()), |
| 2700 | reinterpret_cast<Object**>(source->address()), |
| 2701 | object_size); |
| 2702 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2703 | |
| 2704 | FixedArray* elements = FixedArray::cast(source->elements()); |
| 2705 | FixedArray* properties = FixedArray::cast(source->properties()); |
| 2706 | // Update elements if necessary. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 2707 | if (elements->length() > 0) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2708 | Object* elem = CopyFixedArray(elements); |
| 2709 | if (elem->IsFailure()) return elem; |
| 2710 | JSObject::cast(clone)->set_elements(FixedArray::cast(elem)); |
| 2711 | } |
| 2712 | // Update properties if necessary. |
| 2713 | if (properties->length() > 0) { |
| 2714 | Object* prop = CopyFixedArray(properties); |
| 2715 | if (prop->IsFailure()) return prop; |
| 2716 | JSObject::cast(clone)->set_properties(FixedArray::cast(prop)); |
| 2717 | } |
| 2718 | // Return the new clone. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2719 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2720 | ProducerHeapProfile::RecordJSObjectAllocation(clone); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2721 | #endif |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2722 | return clone; |
| 2723 | } |
| 2724 | |
| 2725 | |
| 2726 | Object* Heap::ReinitializeJSGlobalProxy(JSFunction* constructor, |
| 2727 | JSGlobalProxy* object) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2728 | // Allocate initial map if absent. |
| 2729 | if (!constructor->has_initial_map()) { |
| 2730 | Object* initial_map = AllocateInitialMap(constructor); |
| 2731 | if (initial_map->IsFailure()) return initial_map; |
| 2732 | constructor->set_initial_map(Map::cast(initial_map)); |
| 2733 | Map::cast(initial_map)->set_constructor(constructor); |
| 2734 | } |
| 2735 | |
| 2736 | Map* map = constructor->initial_map(); |
| 2737 | |
| 2738 | // Check that the already allocated object has the same size as |
| 2739 | // objects allocated using the constructor. |
| 2740 | ASSERT(map->instance_size() == object->map()->instance_size()); |
| 2741 | |
| 2742 | // Allocate the backing storage for the properties. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2743 | int prop_size = map->unused_property_fields() - map->inobject_properties(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 2744 | Object* properties = AllocateFixedArray(prop_size, TENURED); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2745 | if (properties->IsFailure()) return properties; |
| 2746 | |
| 2747 | // Reset the map for the object. |
| 2748 | object->set_map(constructor->initial_map()); |
| 2749 | |
| 2750 | // Reinitialize the object from the constructor map. |
| 2751 | InitializeJSObjectFromMap(object, FixedArray::cast(properties), map); |
| 2752 | return object; |
| 2753 | } |
| 2754 | |
| 2755 | |
| 2756 | Object* Heap::AllocateStringFromAscii(Vector<const char> string, |
| 2757 | PretenureFlag pretenure) { |
| 2758 | Object* result = AllocateRawAsciiString(string.length(), pretenure); |
| 2759 | if (result->IsFailure()) return result; |
| 2760 | |
| 2761 | // Copy the characters into the new object. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2762 | SeqAsciiString* string_result = SeqAsciiString::cast(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2763 | for (int i = 0; i < string.length(); i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2764 | string_result->SeqAsciiStringSet(i, string[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2765 | } |
| 2766 | return result; |
| 2767 | } |
| 2768 | |
| 2769 | |
| 2770 | Object* Heap::AllocateStringFromUtf8(Vector<const char> string, |
| 2771 | PretenureFlag pretenure) { |
| 2772 | // Count the number of characters in the UTF-8 string and check if |
| 2773 | // it is an ASCII string. |
| 2774 | Access<Scanner::Utf8Decoder> decoder(Scanner::utf8_decoder()); |
| 2775 | decoder->Reset(string.start(), string.length()); |
| 2776 | int chars = 0; |
| 2777 | bool is_ascii = true; |
| 2778 | while (decoder->has_more()) { |
| 2779 | uc32 r = decoder->GetNext(); |
| 2780 | if (r > String::kMaxAsciiCharCode) is_ascii = false; |
| 2781 | chars++; |
| 2782 | } |
| 2783 | |
| 2784 | // If the string is ascii, we do not need to convert the characters |
| 2785 | // since UTF8 is backwards compatible with ascii. |
| 2786 | if (is_ascii) return AllocateStringFromAscii(string, pretenure); |
| 2787 | |
| 2788 | Object* result = AllocateRawTwoByteString(chars, pretenure); |
| 2789 | if (result->IsFailure()) return result; |
| 2790 | |
| 2791 | // Convert and copy the characters into the new object. |
| 2792 | String* string_result = String::cast(result); |
| 2793 | decoder->Reset(string.start(), string.length()); |
| 2794 | for (int i = 0; i < chars; i++) { |
| 2795 | uc32 r = decoder->GetNext(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2796 | string_result->Set(i, r); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2797 | } |
| 2798 | return result; |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | Object* Heap::AllocateStringFromTwoByte(Vector<const uc16> string, |
| 2803 | PretenureFlag pretenure) { |
| 2804 | // Check if the string is an ASCII string. |
| 2805 | int i = 0; |
| 2806 | while (i < string.length() && string[i] <= String::kMaxAsciiCharCode) i++; |
| 2807 | |
| 2808 | Object* result; |
| 2809 | if (i == string.length()) { // It's an ASCII string. |
| 2810 | result = AllocateRawAsciiString(string.length(), pretenure); |
| 2811 | } else { // It's not an ASCII string. |
| 2812 | result = AllocateRawTwoByteString(string.length(), pretenure); |
| 2813 | } |
| 2814 | if (result->IsFailure()) return result; |
| 2815 | |
| 2816 | // Copy the characters into the new object, which may be either ASCII or |
| 2817 | // UTF-16. |
| 2818 | String* string_result = String::cast(result); |
| 2819 | for (int i = 0; i < string.length(); i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2820 | string_result->Set(i, string[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2821 | } |
| 2822 | return result; |
| 2823 | } |
| 2824 | |
| 2825 | |
| 2826 | Map* Heap::SymbolMapForString(String* string) { |
| 2827 | // If the string is in new space it cannot be used as a symbol. |
| 2828 | if (InNewSpace(string)) return NULL; |
| 2829 | |
| 2830 | // Find the corresponding symbol map for strings. |
| 2831 | Map* map = string->map(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2832 | if (map == ascii_string_map()) return ascii_symbol_map(); |
| 2833 | if (map == string_map()) return symbol_map(); |
| 2834 | if (map == cons_string_map()) return cons_symbol_map(); |
| 2835 | if (map == cons_ascii_string_map()) return cons_ascii_symbol_map(); |
| 2836 | if (map == external_string_map()) return external_symbol_map(); |
| 2837 | if (map == external_ascii_string_map()) return external_ascii_symbol_map(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2838 | |
| 2839 | // No match found. |
| 2840 | return NULL; |
| 2841 | } |
| 2842 | |
| 2843 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2844 | Object* Heap::AllocateInternalSymbol(unibrow::CharacterStream* buffer, |
| 2845 | int chars, |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2846 | uint32_t hash_field) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2847 | ASSERT(chars >= 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2848 | // Ensure the chars matches the number of characters in the buffer. |
| 2849 | ASSERT(static_cast<unsigned>(chars) == buffer->Length()); |
| 2850 | // Determine whether the string is ascii. |
| 2851 | bool is_ascii = true; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2852 | while (buffer->has_more()) { |
| 2853 | if (buffer->GetNext() > unibrow::Utf8::kMaxOneByteChar) { |
| 2854 | is_ascii = false; |
| 2855 | break; |
| 2856 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2857 | } |
| 2858 | buffer->Rewind(); |
| 2859 | |
| 2860 | // Compute map and object size. |
| 2861 | int size; |
| 2862 | Map* map; |
| 2863 | |
| 2864 | if (is_ascii) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2865 | if (chars > SeqAsciiString::kMaxLength) { |
| 2866 | return Failure::OutOfMemoryException(); |
| 2867 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2868 | map = ascii_symbol_map(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2869 | size = SeqAsciiString::SizeFor(chars); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2870 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2871 | if (chars > SeqTwoByteString::kMaxLength) { |
| 2872 | return Failure::OutOfMemoryException(); |
| 2873 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2874 | map = symbol_map(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2875 | size = SeqTwoByteString::SizeFor(chars); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | // Allocate string. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2879 | Object* result = (size > MaxObjectSizeInPagedSpace()) |
| 2880 | ? lo_space_->AllocateRaw(size) |
| 2881 | : old_data_space_->AllocateRaw(size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2882 | if (result->IsFailure()) return result; |
| 2883 | |
| 2884 | reinterpret_cast<HeapObject*>(result)->set_map(map); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2885 | // Set length and hash fields of the allocated string. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2886 | String* answer = String::cast(result); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2887 | answer->set_length(chars); |
| 2888 | answer->set_hash_field(hash_field); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2889 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2890 | ASSERT_EQ(size, answer->Size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2891 | |
| 2892 | // Fill in the characters. |
| 2893 | for (int i = 0; i < chars; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2894 | answer->Set(i, buffer->GetNext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2895 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2896 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | |
| 2900 | Object* Heap::AllocateRawAsciiString(int length, PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2901 | if (length < 0 || length > SeqAsciiString::kMaxLength) { |
| 2902 | return Failure::OutOfMemoryException(); |
| 2903 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2904 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2905 | int size = SeqAsciiString::SizeFor(length); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2906 | ASSERT(size <= SeqAsciiString::kMaxSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2907 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2908 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 2909 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 2910 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2911 | if (space == NEW_SPACE) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2912 | if (size > kMaxObjectSizeInNewSpace) { |
| 2913 | // Allocate in large object space, retry space will be ignored. |
| 2914 | space = LO_SPACE; |
| 2915 | } else if (size > MaxObjectSizeInPagedSpace()) { |
| 2916 | // Allocate in new space, retry in large object space. |
| 2917 | retry_space = LO_SPACE; |
| 2918 | } |
| 2919 | } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) { |
| 2920 | space = LO_SPACE; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2921 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2922 | Object* result = AllocateRaw(size, space, retry_space); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2923 | if (result->IsFailure()) return result; |
| 2924 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2925 | // Partially initialize the object. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2926 | HeapObject::cast(result)->set_map(ascii_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2927 | String::cast(result)->set_length(length); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2928 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2929 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 2930 | return result; |
| 2931 | } |
| 2932 | |
| 2933 | |
| 2934 | Object* Heap::AllocateRawTwoByteString(int length, PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2935 | if (length < 0 || length > SeqTwoByteString::kMaxLength) { |
| 2936 | return Failure::OutOfMemoryException(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2937 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2938 | int size = SeqTwoByteString::SizeFor(length); |
| 2939 | ASSERT(size <= SeqTwoByteString::kMaxSize); |
| 2940 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 2941 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 2942 | |
| 2943 | if (space == NEW_SPACE) { |
| 2944 | if (size > kMaxObjectSizeInNewSpace) { |
| 2945 | // Allocate in large object space, retry space will be ignored. |
| 2946 | space = LO_SPACE; |
| 2947 | } else if (size > MaxObjectSizeInPagedSpace()) { |
| 2948 | // Allocate in new space, retry in large object space. |
| 2949 | retry_space = LO_SPACE; |
| 2950 | } |
| 2951 | } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) { |
| 2952 | space = LO_SPACE; |
| 2953 | } |
| 2954 | Object* result = AllocateRaw(size, space, retry_space); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2955 | if (result->IsFailure()) return result; |
| 2956 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2957 | // Partially initialize the object. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2958 | HeapObject::cast(result)->set_map(string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2959 | String::cast(result)->set_length(length); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2960 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2961 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 2962 | return result; |
| 2963 | } |
| 2964 | |
| 2965 | |
| 2966 | Object* Heap::AllocateEmptyFixedArray() { |
| 2967 | int size = FixedArray::SizeFor(0); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2968 | Object* result = AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2969 | if (result->IsFailure()) return result; |
| 2970 | // Initialize the object. |
| 2971 | reinterpret_cast<Array*>(result)->set_map(fixed_array_map()); |
| 2972 | reinterpret_cast<Array*>(result)->set_length(0); |
| 2973 | return result; |
| 2974 | } |
| 2975 | |
| 2976 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2977 | Object* Heap::AllocateRawFixedArray(int length) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2978 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 2979 | return Failure::OutOfMemoryException(); |
| 2980 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2981 | // Use the general function if we're forced to always allocate. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2982 | if (always_allocate()) return AllocateFixedArray(length, TENURED); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2983 | // Allocate the raw data for a fixed array. |
| 2984 | int size = FixedArray::SizeFor(length); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2985 | return size <= kMaxObjectSizeInNewSpace |
| 2986 | ? new_space_.AllocateRaw(size) |
| 2987 | : lo_space_->AllocateRawFixedArray(size); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | |
| 2991 | Object* Heap::CopyFixedArray(FixedArray* src) { |
| 2992 | int len = src->length(); |
| 2993 | Object* obj = AllocateRawFixedArray(len); |
| 2994 | if (obj->IsFailure()) return obj; |
| 2995 | if (Heap::InNewSpace(obj)) { |
| 2996 | HeapObject* dst = HeapObject::cast(obj); |
| 2997 | CopyBlock(reinterpret_cast<Object**>(dst->address()), |
| 2998 | reinterpret_cast<Object**>(src->address()), |
| 2999 | FixedArray::SizeFor(len)); |
| 3000 | return obj; |
| 3001 | } |
| 3002 | HeapObject::cast(obj)->set_map(src->map()); |
| 3003 | FixedArray* result = FixedArray::cast(obj); |
| 3004 | result->set_length(len); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3005 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3006 | // Copy the content |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3007 | AssertNoAllocation no_gc; |
| 3008 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3009 | for (int i = 0; i < len; i++) result->set(i, src->get(i), mode); |
| 3010 | return result; |
| 3011 | } |
| 3012 | |
| 3013 | |
| 3014 | Object* Heap::AllocateFixedArray(int length) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 3015 | ASSERT(length >= 0); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3016 | if (length == 0) return empty_fixed_array(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3017 | Object* result = AllocateRawFixedArray(length); |
| 3018 | if (!result->IsFailure()) { |
| 3019 | // Initialize header. |
| 3020 | reinterpret_cast<Array*>(result)->set_map(fixed_array_map()); |
| 3021 | FixedArray* array = FixedArray::cast(result); |
| 3022 | array->set_length(length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3023 | // Initialize body. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3024 | ASSERT(!Heap::InNewSpace(undefined_value())); |
| 3025 | MemsetPointer(array->data_start(), undefined_value(), length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3026 | } |
| 3027 | return result; |
| 3028 | } |
| 3029 | |
| 3030 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3031 | Object* Heap::AllocateRawFixedArray(int length, PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3032 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 3033 | return Failure::OutOfMemoryException(); |
| 3034 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3035 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3036 | AllocationSpace space = |
| 3037 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3038 | int size = FixedArray::SizeFor(length); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3039 | if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) { |
| 3040 | // Too big for new space. |
| 3041 | space = LO_SPACE; |
| 3042 | } else if (space == OLD_POINTER_SPACE && |
| 3043 | size > MaxObjectSizeInPagedSpace()) { |
| 3044 | // Too big for old pointer space. |
| 3045 | space = LO_SPACE; |
| 3046 | } |
| 3047 | |
| 3048 | // Specialize allocation for the space. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3049 | Object* result = Failure::OutOfMemoryException(); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3050 | if (space == NEW_SPACE) { |
| 3051 | // We cannot use Heap::AllocateRaw() because it will not properly |
| 3052 | // allocate extra remembered set bits if always_allocate() is true and |
| 3053 | // new space allocation fails. |
| 3054 | result = new_space_.AllocateRaw(size); |
| 3055 | if (result->IsFailure() && always_allocate()) { |
| 3056 | if (size <= MaxObjectSizeInPagedSpace()) { |
| 3057 | result = old_pointer_space_->AllocateRaw(size); |
| 3058 | } else { |
| 3059 | result = lo_space_->AllocateRawFixedArray(size); |
| 3060 | } |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3061 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3062 | } else if (space == OLD_POINTER_SPACE) { |
| 3063 | result = old_pointer_space_->AllocateRaw(size); |
| 3064 | } else { |
| 3065 | ASSERT(space == LO_SPACE); |
| 3066 | result = lo_space_->AllocateRawFixedArray(size); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3067 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3068 | return result; |
| 3069 | } |
| 3070 | |
| 3071 | |
| 3072 | static Object* AllocateFixedArrayWithFiller(int length, |
| 3073 | PretenureFlag pretenure, |
| 3074 | Object* filler) { |
| 3075 | ASSERT(length >= 0); |
| 3076 | ASSERT(Heap::empty_fixed_array()->IsFixedArray()); |
| 3077 | if (length == 0) return Heap::empty_fixed_array(); |
| 3078 | |
| 3079 | ASSERT(!Heap::InNewSpace(filler)); |
| 3080 | Object* result = Heap::AllocateRawFixedArray(length, pretenure); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3081 | if (result->IsFailure()) return result; |
| 3082 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3083 | HeapObject::cast(result)->set_map(Heap::fixed_array_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3084 | FixedArray* array = FixedArray::cast(result); |
| 3085 | array->set_length(length); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3086 | MemsetPointer(array->data_start(), filler, length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3087 | return array; |
| 3088 | } |
| 3089 | |
| 3090 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3091 | Object* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) { |
| 3092 | return AllocateFixedArrayWithFiller(length, pretenure, undefined_value()); |
| 3093 | } |
| 3094 | |
| 3095 | |
| 3096 | Object* Heap::AllocateFixedArrayWithHoles(int length, PretenureFlag pretenure) { |
| 3097 | return AllocateFixedArrayWithFiller(length, pretenure, the_hole_value()); |
| 3098 | } |
| 3099 | |
| 3100 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3101 | Object* Heap::AllocateUninitializedFixedArray(int length) { |
| 3102 | if (length == 0) return empty_fixed_array(); |
| 3103 | |
| 3104 | Object* obj = AllocateRawFixedArray(length); |
| 3105 | if (obj->IsFailure()) return obj; |
| 3106 | |
| 3107 | reinterpret_cast<FixedArray*>(obj)->set_map(fixed_array_map()); |
| 3108 | FixedArray::cast(obj)->set_length(length); |
| 3109 | return obj; |
| 3110 | } |
| 3111 | |
| 3112 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3113 | Object* Heap::AllocateHashTable(int length, PretenureFlag pretenure) { |
| 3114 | Object* result = Heap::AllocateFixedArray(length, pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3115 | if (result->IsFailure()) return result; |
| 3116 | reinterpret_cast<Array*>(result)->set_map(hash_table_map()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3117 | ASSERT(result->IsHashTable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3118 | return result; |
| 3119 | } |
| 3120 | |
| 3121 | |
| 3122 | Object* Heap::AllocateGlobalContext() { |
| 3123 | Object* result = Heap::AllocateFixedArray(Context::GLOBAL_CONTEXT_SLOTS); |
| 3124 | if (result->IsFailure()) return result; |
| 3125 | Context* context = reinterpret_cast<Context*>(result); |
| 3126 | context->set_map(global_context_map()); |
| 3127 | ASSERT(context->IsGlobalContext()); |
| 3128 | ASSERT(result->IsContext()); |
| 3129 | return result; |
| 3130 | } |
| 3131 | |
| 3132 | |
| 3133 | Object* Heap::AllocateFunctionContext(int length, JSFunction* function) { |
| 3134 | ASSERT(length >= Context::MIN_CONTEXT_SLOTS); |
| 3135 | Object* result = Heap::AllocateFixedArray(length); |
| 3136 | if (result->IsFailure()) return result; |
| 3137 | Context* context = reinterpret_cast<Context*>(result); |
| 3138 | context->set_map(context_map()); |
| 3139 | context->set_closure(function); |
| 3140 | context->set_fcontext(context); |
| 3141 | context->set_previous(NULL); |
| 3142 | context->set_extension(NULL); |
| 3143 | context->set_global(function->context()->global()); |
| 3144 | ASSERT(!context->IsGlobalContext()); |
| 3145 | ASSERT(context->is_function_context()); |
| 3146 | ASSERT(result->IsContext()); |
| 3147 | return result; |
| 3148 | } |
| 3149 | |
| 3150 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3151 | Object* Heap::AllocateWithContext(Context* previous, |
| 3152 | JSObject* extension, |
| 3153 | bool is_catch_context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3154 | Object* result = Heap::AllocateFixedArray(Context::MIN_CONTEXT_SLOTS); |
| 3155 | if (result->IsFailure()) return result; |
| 3156 | Context* context = reinterpret_cast<Context*>(result); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3157 | context->set_map(is_catch_context ? catch_context_map() : context_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3158 | context->set_closure(previous->closure()); |
| 3159 | context->set_fcontext(previous->fcontext()); |
| 3160 | context->set_previous(previous); |
| 3161 | context->set_extension(extension); |
| 3162 | context->set_global(previous->global()); |
| 3163 | ASSERT(!context->IsGlobalContext()); |
| 3164 | ASSERT(!context->is_function_context()); |
| 3165 | ASSERT(result->IsContext()); |
| 3166 | return result; |
| 3167 | } |
| 3168 | |
| 3169 | |
| 3170 | Object* Heap::AllocateStruct(InstanceType type) { |
| 3171 | Map* map; |
| 3172 | switch (type) { |
| 3173 | #define MAKE_CASE(NAME, Name, name) case NAME##_TYPE: map = name##_map(); break; |
| 3174 | STRUCT_LIST(MAKE_CASE) |
| 3175 | #undef MAKE_CASE |
| 3176 | default: |
| 3177 | UNREACHABLE(); |
| 3178 | return Failure::InternalError(); |
| 3179 | } |
| 3180 | int size = map->instance_size(); |
| 3181 | AllocationSpace space = |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3182 | (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : OLD_POINTER_SPACE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3183 | Object* result = Heap::Allocate(map, space); |
| 3184 | if (result->IsFailure()) return result; |
| 3185 | Struct::cast(result)->InitializeBody(size); |
| 3186 | return result; |
| 3187 | } |
| 3188 | |
| 3189 | |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3190 | bool Heap::IdleNotification() { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3191 | static const int kIdlesBeforeScavenge = 4; |
| 3192 | static const int kIdlesBeforeMarkSweep = 7; |
| 3193 | static const int kIdlesBeforeMarkCompact = 8; |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3194 | static int number_idle_notifications = 0; |
| 3195 | static int last_gc_count = gc_count_; |
| 3196 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3197 | bool uncommit = true; |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3198 | bool finished = false; |
| 3199 | |
| 3200 | if (last_gc_count == gc_count_) { |
| 3201 | number_idle_notifications++; |
| 3202 | } else { |
| 3203 | number_idle_notifications = 0; |
| 3204 | last_gc_count = gc_count_; |
| 3205 | } |
| 3206 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3207 | if (number_idle_notifications == kIdlesBeforeScavenge) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3208 | if (contexts_disposed_ > 0) { |
| 3209 | HistogramTimerScope scope(&Counters::gc_context); |
| 3210 | CollectAllGarbage(false); |
| 3211 | } else { |
| 3212 | CollectGarbage(0, NEW_SPACE); |
| 3213 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3214 | new_space_.Shrink(); |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3215 | last_gc_count = gc_count_; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3216 | |
| 3217 | } else if (number_idle_notifications == kIdlesBeforeMarkSweep) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3218 | // Before doing the mark-sweep collections we clear the |
| 3219 | // compilation cache to avoid hanging on to source code and |
| 3220 | // generated code for cached functions. |
| 3221 | CompilationCache::Clear(); |
| 3222 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3223 | CollectAllGarbage(false); |
| 3224 | new_space_.Shrink(); |
| 3225 | last_gc_count = gc_count_; |
| 3226 | |
| 3227 | } else if (number_idle_notifications == kIdlesBeforeMarkCompact) { |
| 3228 | CollectAllGarbage(true); |
| 3229 | new_space_.Shrink(); |
| 3230 | last_gc_count = gc_count_; |
| 3231 | number_idle_notifications = 0; |
| 3232 | finished = true; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3233 | |
| 3234 | } else if (contexts_disposed_ > 0) { |
| 3235 | if (FLAG_expose_gc) { |
| 3236 | contexts_disposed_ = 0; |
| 3237 | } else { |
| 3238 | HistogramTimerScope scope(&Counters::gc_context); |
| 3239 | CollectAllGarbage(false); |
| 3240 | last_gc_count = gc_count_; |
| 3241 | } |
| 3242 | // If this is the first idle notification, we reset the |
| 3243 | // notification count to avoid letting idle notifications for |
| 3244 | // context disposal garbage collections start a potentially too |
| 3245 | // aggressive idle GC cycle. |
| 3246 | if (number_idle_notifications <= 1) { |
| 3247 | number_idle_notifications = 0; |
| 3248 | uncommit = false; |
| 3249 | } |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3252 | // Make sure that we have no pending context disposals and |
| 3253 | // conditionally uncommit from space. |
| 3254 | ASSERT(contexts_disposed_ == 0); |
| 3255 | if (uncommit) Heap::UncommitFromSpace(); |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3256 | return finished; |
| 3257 | } |
| 3258 | |
| 3259 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3260 | #ifdef DEBUG |
| 3261 | |
| 3262 | void Heap::Print() { |
| 3263 | if (!HasBeenSetup()) return; |
| 3264 | Top::PrintStack(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3265 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3266 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 3267 | space->Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | |
| 3271 | void Heap::ReportCodeStatistics(const char* title) { |
| 3272 | PrintF(">>>>>> Code Stats (%s) >>>>>>\n", title); |
| 3273 | PagedSpace::ResetCodeStatistics(); |
| 3274 | // We do not look for code in new space, map space, or old space. If code |
| 3275 | // somehow ends up in those spaces, we would miss it here. |
| 3276 | code_space_->CollectCodeStatistics(); |
| 3277 | lo_space_->CollectCodeStatistics(); |
| 3278 | PagedSpace::ReportCodeStatistics(); |
| 3279 | } |
| 3280 | |
| 3281 | |
| 3282 | // This function expects that NewSpace's allocated objects histogram is |
| 3283 | // populated (via a call to CollectStatistics or else as a side effect of a |
| 3284 | // just-completed scavenge collection). |
| 3285 | void Heap::ReportHeapStatistics(const char* title) { |
| 3286 | USE(title); |
| 3287 | PrintF(">>>>>> =============== %s (%d) =============== >>>>>>\n", |
| 3288 | title, gc_count_); |
| 3289 | PrintF("mark-compact GC : %d\n", mc_count_); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3290 | PrintF("old_gen_promotion_limit_ %d\n", old_gen_promotion_limit_); |
| 3291 | PrintF("old_gen_allocation_limit_ %d\n", old_gen_allocation_limit_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3292 | |
| 3293 | PrintF("\n"); |
| 3294 | PrintF("Number of handles : %d\n", HandleScope::NumberOfHandles()); |
| 3295 | GlobalHandles::PrintStats(); |
| 3296 | PrintF("\n"); |
| 3297 | |
| 3298 | PrintF("Heap statistics : "); |
| 3299 | MemoryAllocator::ReportStatistics(); |
| 3300 | PrintF("To space : "); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3301 | new_space_.ReportStatistics(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3302 | PrintF("Old pointer space : "); |
| 3303 | old_pointer_space_->ReportStatistics(); |
| 3304 | PrintF("Old data space : "); |
| 3305 | old_data_space_->ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3306 | PrintF("Code space : "); |
| 3307 | code_space_->ReportStatistics(); |
| 3308 | PrintF("Map space : "); |
| 3309 | map_space_->ReportStatistics(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3310 | PrintF("Cell space : "); |
| 3311 | cell_space_->ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3312 | PrintF("Large object space : "); |
| 3313 | lo_space_->ReportStatistics(); |
| 3314 | PrintF(">>>>>> ========================================= >>>>>>\n"); |
| 3315 | } |
| 3316 | |
| 3317 | #endif // DEBUG |
| 3318 | |
| 3319 | bool Heap::Contains(HeapObject* value) { |
| 3320 | return Contains(value->address()); |
| 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | bool Heap::Contains(Address addr) { |
| 3325 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
| 3326 | return HasBeenSetup() && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3327 | (new_space_.ToSpaceContains(addr) || |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3328 | old_pointer_space_->Contains(addr) || |
| 3329 | old_data_space_->Contains(addr) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3330 | code_space_->Contains(addr) || |
| 3331 | map_space_->Contains(addr) || |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3332 | cell_space_->Contains(addr) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3333 | lo_space_->SlowContains(addr)); |
| 3334 | } |
| 3335 | |
| 3336 | |
| 3337 | bool Heap::InSpace(HeapObject* value, AllocationSpace space) { |
| 3338 | return InSpace(value->address(), space); |
| 3339 | } |
| 3340 | |
| 3341 | |
| 3342 | bool Heap::InSpace(Address addr, AllocationSpace space) { |
| 3343 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
| 3344 | if (!HasBeenSetup()) return false; |
| 3345 | |
| 3346 | switch (space) { |
| 3347 | case NEW_SPACE: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3348 | return new_space_.ToSpaceContains(addr); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3349 | case OLD_POINTER_SPACE: |
| 3350 | return old_pointer_space_->Contains(addr); |
| 3351 | case OLD_DATA_SPACE: |
| 3352 | return old_data_space_->Contains(addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3353 | case CODE_SPACE: |
| 3354 | return code_space_->Contains(addr); |
| 3355 | case MAP_SPACE: |
| 3356 | return map_space_->Contains(addr); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3357 | case CELL_SPACE: |
| 3358 | return cell_space_->Contains(addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3359 | case LO_SPACE: |
| 3360 | return lo_space_->SlowContains(addr); |
| 3361 | } |
| 3362 | |
| 3363 | return false; |
| 3364 | } |
| 3365 | |
| 3366 | |
| 3367 | #ifdef DEBUG |
| 3368 | void Heap::Verify() { |
| 3369 | ASSERT(HasBeenSetup()); |
| 3370 | |
| 3371 | VerifyPointersVisitor visitor; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3372 | IterateRoots(&visitor, VISIT_ONLY_STRONG); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3373 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3374 | new_space_.Verify(); |
| 3375 | |
| 3376 | VerifyPointersAndRSetVisitor rset_visitor; |
| 3377 | old_pointer_space_->Verify(&rset_visitor); |
| 3378 | map_space_->Verify(&rset_visitor); |
| 3379 | |
| 3380 | VerifyPointersVisitor no_rset_visitor; |
| 3381 | old_data_space_->Verify(&no_rset_visitor); |
| 3382 | code_space_->Verify(&no_rset_visitor); |
| 3383 | cell_space_->Verify(&no_rset_visitor); |
| 3384 | |
| 3385 | lo_space_->Verify(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3386 | } |
| 3387 | #endif // DEBUG |
| 3388 | |
| 3389 | |
| 3390 | Object* Heap::LookupSymbol(Vector<const char> string) { |
| 3391 | Object* symbol = NULL; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3392 | Object* new_table = symbol_table()->LookupSymbol(string, &symbol); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3393 | if (new_table->IsFailure()) return new_table; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3394 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 3395 | // SymbolTable is a singleton and checks for identity. |
| 3396 | roots_[kSymbolTableRootIndex] = new_table; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3397 | ASSERT(symbol != NULL); |
| 3398 | return symbol; |
| 3399 | } |
| 3400 | |
| 3401 | |
| 3402 | Object* Heap::LookupSymbol(String* string) { |
| 3403 | if (string->IsSymbol()) return string; |
| 3404 | Object* symbol = NULL; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3405 | Object* new_table = symbol_table()->LookupString(string, &symbol); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3406 | if (new_table->IsFailure()) return new_table; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3407 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 3408 | // SymbolTable is a singleton and checks for identity. |
| 3409 | roots_[kSymbolTableRootIndex] = new_table; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3410 | ASSERT(symbol != NULL); |
| 3411 | return symbol; |
| 3412 | } |
| 3413 | |
| 3414 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3415 | bool Heap::LookupSymbolIfExists(String* string, String** symbol) { |
| 3416 | if (string->IsSymbol()) { |
| 3417 | *symbol = string; |
| 3418 | return true; |
| 3419 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3420 | return symbol_table()->LookupSymbolIfExists(string, symbol); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3424 | #ifdef DEBUG |
| 3425 | void Heap::ZapFromSpace() { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3426 | ASSERT(reinterpret_cast<Object*>(kFromSpaceZapValue)->IsHeapObject()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3427 | for (Address a = new_space_.FromSpaceLow(); |
| 3428 | a < new_space_.FromSpaceHigh(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3429 | a += kPointerSize) { |
| 3430 | Memory::Address_at(a) = kFromSpaceZapValue; |
| 3431 | } |
| 3432 | } |
| 3433 | #endif // DEBUG |
| 3434 | |
| 3435 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3436 | int Heap::IterateRSetRange(Address object_start, |
| 3437 | Address object_end, |
| 3438 | Address rset_start, |
| 3439 | ObjectSlotCallback copy_object_func) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3440 | Address object_address = object_start; |
| 3441 | Address rset_address = rset_start; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3442 | int set_bits_count = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3443 | |
| 3444 | // Loop over all the pointers in [object_start, object_end). |
| 3445 | while (object_address < object_end) { |
| 3446 | uint32_t rset_word = Memory::uint32_at(rset_address); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3447 | if (rset_word != 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3448 | uint32_t result_rset = rset_word; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3449 | for (uint32_t bitmask = 1; bitmask != 0; bitmask = bitmask << 1) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3450 | // Do not dereference pointers at or past object_end. |
| 3451 | if ((rset_word & bitmask) != 0 && object_address < object_end) { |
| 3452 | Object** object_p = reinterpret_cast<Object**>(object_address); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3453 | if (Heap::InNewSpace(*object_p)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3454 | copy_object_func(reinterpret_cast<HeapObject**>(object_p)); |
| 3455 | } |
| 3456 | // If this pointer does not need to be remembered anymore, clear |
| 3457 | // the remembered set bit. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3458 | if (!Heap::InNewSpace(*object_p)) result_rset &= ~bitmask; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3459 | set_bits_count++; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3460 | } |
| 3461 | object_address += kPointerSize; |
| 3462 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3463 | // Update the remembered set if it has changed. |
| 3464 | if (result_rset != rset_word) { |
| 3465 | Memory::uint32_at(rset_address) = result_rset; |
| 3466 | } |
| 3467 | } else { |
| 3468 | // No bits in the word were set. This is the common case. |
| 3469 | object_address += kPointerSize * kBitsPerInt; |
| 3470 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3471 | rset_address += kIntSize; |
| 3472 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3473 | return set_bits_count; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
| 3476 | |
| 3477 | void Heap::IterateRSet(PagedSpace* space, ObjectSlotCallback copy_object_func) { |
| 3478 | ASSERT(Page::is_rset_in_use()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3479 | ASSERT(space == old_pointer_space_ || space == map_space_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3480 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3481 | static void* paged_rset_histogram = StatsTable::CreateHistogram( |
| 3482 | "V8.RSetPaged", |
| 3483 | 0, |
| 3484 | Page::kObjectAreaSize / kPointerSize, |
| 3485 | 30); |
| 3486 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3487 | PageIterator it(space, PageIterator::PAGES_IN_USE); |
| 3488 | while (it.has_next()) { |
| 3489 | Page* page = it.next(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 3490 | int count = IterateRSetRange(page->ObjectAreaStart(), page->AllocationTop(), |
| 3491 | page->RSetStart(), copy_object_func); |
| 3492 | if (paged_rset_histogram != NULL) { |
| 3493 | StatsTable::AddHistogramSample(paged_rset_histogram, count); |
| 3494 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3499 | void Heap::IterateRoots(ObjectVisitor* v, VisitMode mode) { |
| 3500 | IterateStrongRoots(v, mode); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3501 | IterateWeakRoots(v, mode); |
| 3502 | } |
| 3503 | |
| 3504 | |
| 3505 | void Heap::IterateWeakRoots(ObjectVisitor* v, VisitMode mode) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3506 | v->VisitPointer(reinterpret_cast<Object**>(&roots_[kSymbolTableRootIndex])); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3507 | v->Synchronize("symbol_table"); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3508 | if (mode != VISIT_ALL_IN_SCAVENGE) { |
| 3509 | // Scavenge collections have special processing for this. |
| 3510 | ExternalStringTable::Iterate(v); |
| 3511 | } |
| 3512 | v->Synchronize("external_string_table"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3516 | void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3517 | v->VisitPointers(&roots_[0], &roots_[kStrongRootListLength]); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3518 | v->Synchronize("strong_root_list"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3519 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3520 | v->VisitPointer(BitCast<Object**, String**>(&hidden_symbol_)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3521 | v->Synchronize("symbol"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3522 | |
| 3523 | Bootstrapper::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3524 | v->Synchronize("bootstrapper"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3525 | Top::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3526 | v->Synchronize("top"); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3527 | Relocatable::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3528 | v->Synchronize("relocatable"); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3529 | |
| 3530 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3531 | Debug::Iterate(v); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3532 | #endif |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3533 | v->Synchronize("debug"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 3534 | CompilationCache::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3535 | v->Synchronize("compilationcache"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3536 | |
| 3537 | // Iterate over local handles in handle scopes. |
| 3538 | HandleScopeImplementer::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3539 | v->Synchronize("handlescope"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3540 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3541 | // Iterate over the builtin code objects and code stubs in the |
| 3542 | // heap. Note that it is not necessary to iterate over code objects |
| 3543 | // on scavenge collections. |
| 3544 | if (mode != VISIT_ALL_IN_SCAVENGE) { |
| 3545 | Builtins::IterateBuiltins(v); |
| 3546 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3547 | v->Synchronize("builtins"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3548 | |
| 3549 | // Iterate over global handles. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3550 | if (mode == VISIT_ONLY_STRONG) { |
| 3551 | GlobalHandles::IterateStrongRoots(v); |
| 3552 | } else { |
| 3553 | GlobalHandles::IterateAllRoots(v); |
| 3554 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3555 | v->Synchronize("globalhandles"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3556 | |
| 3557 | // Iterate over pointers being held by inactive threads. |
| 3558 | ThreadManager::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3559 | v->Synchronize("threadmanager"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3560 | |
| 3561 | // Iterate over the pointers the Serialization/Deserialization code is |
| 3562 | // holding. |
| 3563 | // During garbage collection this keeps the partial snapshot cache alive. |
| 3564 | // During deserialization of the startup snapshot this creates the partial |
| 3565 | // snapshot cache and deserializes the objects it refers to. During |
| 3566 | // serialization this does nothing, since the partial snapshot cache is |
| 3567 | // empty. However the next thing we do is create the partial snapshot, |
| 3568 | // filling up the partial snapshot cache with objects it needs as we go. |
| 3569 | SerializerDeserializer::Iterate(v); |
| 3570 | // We don't do a v->Synchronize call here, because in debug mode that will |
| 3571 | // output a flag to the snapshot. However at this point the serializer and |
| 3572 | // deserializer are deliberately a little unsynchronized (see above) so the |
| 3573 | // checking of the sync flag in the snapshot would fail. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3574 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3575 | |
| 3576 | |
| 3577 | // Flag is set when the heap has been configured. The heap can be repeatedly |
| 3578 | // configured through the API until it is setup. |
| 3579 | static bool heap_configured = false; |
| 3580 | |
| 3581 | // TODO(1236194): Since the heap size is configurable on the command line |
| 3582 | // and through the API, we should gracefully handle the case that the heap |
| 3583 | // size is not big enough to fit all the initial objects. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3584 | bool Heap::ConfigureHeap(int max_semispace_size, int max_old_gen_size) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3585 | if (HasBeenSetup()) return false; |
| 3586 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3587 | if (max_semispace_size > 0) max_semispace_size_ = max_semispace_size; |
| 3588 | |
| 3589 | if (Snapshot::IsEnabled()) { |
| 3590 | // If we are using a snapshot we always reserve the default amount |
| 3591 | // of memory for each semispace because code in the snapshot has |
| 3592 | // write-barrier code that relies on the size and alignment of new |
| 3593 | // space. We therefore cannot use a larger max semispace size |
| 3594 | // than the default reserved semispace size. |
| 3595 | if (max_semispace_size_ > reserved_semispace_size_) { |
| 3596 | max_semispace_size_ = reserved_semispace_size_; |
| 3597 | } |
| 3598 | } else { |
| 3599 | // If we are not using snapshots we reserve space for the actual |
| 3600 | // max semispace size. |
| 3601 | reserved_semispace_size_ = max_semispace_size_; |
| 3602 | } |
| 3603 | |
| 3604 | if (max_old_gen_size > 0) max_old_generation_size_ = max_old_gen_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3605 | |
| 3606 | // The new space size must be a power of two to support single-bit testing |
| 3607 | // for containment. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3608 | max_semispace_size_ = RoundUpToPowerOf2(max_semispace_size_); |
| 3609 | reserved_semispace_size_ = RoundUpToPowerOf2(reserved_semispace_size_); |
| 3610 | initial_semispace_size_ = Min(initial_semispace_size_, max_semispace_size_); |
| 3611 | external_allocation_limit_ = 10 * max_semispace_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3612 | |
| 3613 | // The old generation is paged. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3614 | max_old_generation_size_ = RoundUp(max_old_generation_size_, Page::kPageSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3615 | |
| 3616 | heap_configured = true; |
| 3617 | return true; |
| 3618 | } |
| 3619 | |
| 3620 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3621 | bool Heap::ConfigureHeapDefault() { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3622 | return ConfigureHeap(FLAG_max_new_space_size / 2, FLAG_max_old_space_size); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 3626 | void Heap::RecordStats(HeapStats* stats) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3627 | *stats->start_marker = 0xDECADE00; |
| 3628 | *stats->end_marker = 0xDECADE01; |
| 3629 | *stats->new_space_size = new_space_.Size(); |
| 3630 | *stats->new_space_capacity = new_space_.Capacity(); |
| 3631 | *stats->old_pointer_space_size = old_pointer_space_->Size(); |
| 3632 | *stats->old_pointer_space_capacity = old_pointer_space_->Capacity(); |
| 3633 | *stats->old_data_space_size = old_data_space_->Size(); |
| 3634 | *stats->old_data_space_capacity = old_data_space_->Capacity(); |
| 3635 | *stats->code_space_size = code_space_->Size(); |
| 3636 | *stats->code_space_capacity = code_space_->Capacity(); |
| 3637 | *stats->map_space_size = map_space_->Size(); |
| 3638 | *stats->map_space_capacity = map_space_->Capacity(); |
| 3639 | *stats->cell_space_size = cell_space_->Size(); |
| 3640 | *stats->cell_space_capacity = cell_space_->Capacity(); |
| 3641 | *stats->lo_space_size = lo_space_->Size(); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 3642 | GlobalHandles::RecordStats(stats); |
| 3643 | } |
| 3644 | |
| 3645 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3646 | int Heap::PromotedSpaceSize() { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3647 | return old_pointer_space_->Size() |
| 3648 | + old_data_space_->Size() |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3649 | + code_space_->Size() |
| 3650 | + map_space_->Size() |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3651 | + cell_space_->Size() |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3652 | + lo_space_->Size(); |
| 3653 | } |
| 3654 | |
| 3655 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3656 | int Heap::PromotedExternalMemorySize() { |
| 3657 | if (amount_of_external_allocated_memory_ |
| 3658 | <= amount_of_external_allocated_memory_at_last_global_gc_) return 0; |
| 3659 | return amount_of_external_allocated_memory_ |
| 3660 | - amount_of_external_allocated_memory_at_last_global_gc_; |
| 3661 | } |
| 3662 | |
| 3663 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3664 | bool Heap::Setup(bool create_heap_objects) { |
| 3665 | // Initialize heap spaces and initial maps and objects. Whenever something |
| 3666 | // goes wrong, just return false. The caller should check the results and |
| 3667 | // call Heap::TearDown() to release allocated memory. |
| 3668 | // |
| 3669 | // If the heap is not yet configured (eg, through the API), configure it. |
| 3670 | // Configuration is based on the flags new-space-size (really the semispace |
| 3671 | // size) and old-space-size if set or the initial values of semispace_size_ |
| 3672 | // and old_generation_size_ otherwise. |
| 3673 | if (!heap_configured) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3674 | if (!ConfigureHeapDefault()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3677 | // Setup memory allocator and reserve a chunk of memory for new |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3678 | // space. The chunk is double the size of the requested reserved |
| 3679 | // new space size to ensure that we can find a pair of semispaces that |
| 3680 | // are contiguous and aligned to their size. |
| 3681 | if (!MemoryAllocator::Setup(MaxReserved())) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3682 | void* chunk = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3683 | MemoryAllocator::ReserveInitialChunk(4 * reserved_semispace_size_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3684 | if (chunk == NULL) return false; |
| 3685 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3686 | // Align the pair of semispaces to their size, which must be a power |
| 3687 | // of 2. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3688 | Address new_space_start = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3689 | RoundUp(reinterpret_cast<byte*>(chunk), 2 * reserved_semispace_size_); |
| 3690 | if (!new_space_.Setup(new_space_start, 2 * reserved_semispace_size_)) { |
| 3691 | return false; |
| 3692 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3693 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3694 | // Initialize old pointer space. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3695 | old_pointer_space_ = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3696 | new OldSpace(max_old_generation_size_, OLD_POINTER_SPACE, NOT_EXECUTABLE); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3697 | if (old_pointer_space_ == NULL) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3698 | if (!old_pointer_space_->Setup(NULL, 0)) return false; |
| 3699 | |
| 3700 | // Initialize old data space. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3701 | old_data_space_ = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3702 | new OldSpace(max_old_generation_size_, OLD_DATA_SPACE, NOT_EXECUTABLE); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3703 | if (old_data_space_ == NULL) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3704 | if (!old_data_space_->Setup(NULL, 0)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3705 | |
| 3706 | // Initialize the code space, set its maximum capacity to the old |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3707 | // generation size. It needs executable memory. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3708 | // On 64-bit platform(s), we put all code objects in a 2 GB range of |
| 3709 | // virtual address space, so that they can call each other with near calls. |
| 3710 | if (code_range_size_ > 0) { |
| 3711 | if (!CodeRange::Setup(code_range_size_)) { |
| 3712 | return false; |
| 3713 | } |
| 3714 | } |
| 3715 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3716 | code_space_ = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3717 | new OldSpace(max_old_generation_size_, CODE_SPACE, EXECUTABLE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3718 | if (code_space_ == NULL) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3719 | if (!code_space_->Setup(NULL, 0)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3720 | |
| 3721 | // Initialize map space. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3722 | map_space_ = new MapSpace(FLAG_use_big_map_space |
| 3723 | ? max_old_generation_size_ |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 3724 | : MapSpace::kMaxMapPageIndex * Page::kPageSize, |
| 3725 | FLAG_max_map_space_pages, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3726 | MAP_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3727 | if (map_space_ == NULL) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3728 | if (!map_space_->Setup(NULL, 0)) return false; |
| 3729 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3730 | // Initialize global property cell space. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3731 | cell_space_ = new CellSpace(max_old_generation_size_, CELL_SPACE); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3732 | if (cell_space_ == NULL) return false; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3733 | if (!cell_space_->Setup(NULL, 0)) return false; |
| 3734 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3735 | // The large object code space may contain code or data. We set the memory |
| 3736 | // to be non-executable here for safety, but this means we need to enable it |
| 3737 | // explicitly when allocating large code objects. |
| 3738 | lo_space_ = new LargeObjectSpace(LO_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3739 | if (lo_space_ == NULL) return false; |
| 3740 | if (!lo_space_->Setup()) return false; |
| 3741 | |
| 3742 | if (create_heap_objects) { |
| 3743 | // Create initial maps. |
| 3744 | if (!CreateInitialMaps()) return false; |
| 3745 | if (!CreateApiObjects()) return false; |
| 3746 | |
| 3747 | // Create initial objects |
| 3748 | if (!CreateInitialObjects()) return false; |
| 3749 | } |
| 3750 | |
| 3751 | LOG(IntEvent("heap-capacity", Capacity())); |
| 3752 | LOG(IntEvent("heap-available", Available())); |
| 3753 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3754 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3755 | // This should be called only after initial objects have been created. |
| 3756 | ProducerHeapProfile::Setup(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3757 | #endif |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3758 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3759 | return true; |
| 3760 | } |
| 3761 | |
| 3762 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3763 | void Heap::SetStackLimits() { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3764 | // On 64 bit machines, pointers are generally out of range of Smis. We write |
| 3765 | // something that looks like an out of range Smi to the GC. |
| 3766 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3767 | // Set up the special root array entries containing the stack limits. |
| 3768 | // These are actually addresses, but the tag makes the GC ignore it. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3769 | roots_[kStackLimitRootIndex] = |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3770 | reinterpret_cast<Object*>( |
| 3771 | (StackGuard::jslimit() & ~kSmiTagMask) | kSmiTag); |
| 3772 | roots_[kRealStackLimitRootIndex] = |
| 3773 | reinterpret_cast<Object*>( |
| 3774 | (StackGuard::real_jslimit() & ~kSmiTagMask) | kSmiTag); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3778 | void Heap::TearDown() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 3779 | if (FLAG_print_cumulative_gc_stat) { |
| 3780 | PrintF("\n\n"); |
| 3781 | PrintF("gc_count=%d ", gc_count_); |
| 3782 | PrintF("mark_sweep_count=%d ", ms_count_); |
| 3783 | PrintF("mark_compact_count=%d ", mc_count_); |
| 3784 | PrintF("max_gc_pause=%d ", GCTracer::get_max_gc_pause()); |
| 3785 | PrintF("min_in_mutator=%d ", GCTracer::get_min_in_mutator()); |
| 3786 | PrintF("max_alive_after_gc=%d ", GCTracer::get_max_alive_after_gc()); |
| 3787 | PrintF("\n\n"); |
| 3788 | } |
| 3789 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3790 | GlobalHandles::TearDown(); |
| 3791 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 3792 | ExternalStringTable::TearDown(); |
| 3793 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3794 | new_space_.TearDown(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3795 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3796 | if (old_pointer_space_ != NULL) { |
| 3797 | old_pointer_space_->TearDown(); |
| 3798 | delete old_pointer_space_; |
| 3799 | old_pointer_space_ = NULL; |
| 3800 | } |
| 3801 | |
| 3802 | if (old_data_space_ != NULL) { |
| 3803 | old_data_space_->TearDown(); |
| 3804 | delete old_data_space_; |
| 3805 | old_data_space_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | if (code_space_ != NULL) { |
| 3809 | code_space_->TearDown(); |
| 3810 | delete code_space_; |
| 3811 | code_space_ = NULL; |
| 3812 | } |
| 3813 | |
| 3814 | if (map_space_ != NULL) { |
| 3815 | map_space_->TearDown(); |
| 3816 | delete map_space_; |
| 3817 | map_space_ = NULL; |
| 3818 | } |
| 3819 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3820 | if (cell_space_ != NULL) { |
| 3821 | cell_space_->TearDown(); |
| 3822 | delete cell_space_; |
| 3823 | cell_space_ = NULL; |
| 3824 | } |
| 3825 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3826 | if (lo_space_ != NULL) { |
| 3827 | lo_space_->TearDown(); |
| 3828 | delete lo_space_; |
| 3829 | lo_space_ = NULL; |
| 3830 | } |
| 3831 | |
| 3832 | MemoryAllocator::TearDown(); |
| 3833 | } |
| 3834 | |
| 3835 | |
| 3836 | void Heap::Shrink() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3837 | // Try to shrink all paged spaces. |
| 3838 | PagedSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3839 | for (PagedSpace* space = spaces.next(); space != NULL; space = spaces.next()) |
| 3840 | space->Shrink(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 3844 | #ifdef ENABLE_HEAP_PROTECTION |
| 3845 | |
| 3846 | void Heap::Protect() { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 3847 | if (HasBeenSetup()) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3848 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3849 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 3850 | space->Protect(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 3851 | } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | |
| 3855 | void Heap::Unprotect() { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 3856 | if (HasBeenSetup()) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3857 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3858 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 3859 | space->Unprotect(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 3860 | } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | #endif |
| 3864 | |
| 3865 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 3866 | void Heap::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) { |
| 3867 | ASSERT(callback != NULL); |
| 3868 | GCPrologueCallbackPair pair(callback, gc_type); |
| 3869 | ASSERT(!gc_prologue_callbacks_.Contains(pair)); |
| 3870 | return gc_prologue_callbacks_.Add(pair); |
| 3871 | } |
| 3872 | |
| 3873 | |
| 3874 | void Heap::RemoveGCPrologueCallback(GCPrologueCallback callback) { |
| 3875 | ASSERT(callback != NULL); |
| 3876 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 3877 | if (gc_prologue_callbacks_[i].callback == callback) { |
| 3878 | gc_prologue_callbacks_.Remove(i); |
| 3879 | return; |
| 3880 | } |
| 3881 | } |
| 3882 | UNREACHABLE(); |
| 3883 | } |
| 3884 | |
| 3885 | |
| 3886 | void Heap::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) { |
| 3887 | ASSERT(callback != NULL); |
| 3888 | GCEpilogueCallbackPair pair(callback, gc_type); |
| 3889 | ASSERT(!gc_epilogue_callbacks_.Contains(pair)); |
| 3890 | return gc_epilogue_callbacks_.Add(pair); |
| 3891 | } |
| 3892 | |
| 3893 | |
| 3894 | void Heap::RemoveGCEpilogueCallback(GCEpilogueCallback callback) { |
| 3895 | ASSERT(callback != NULL); |
| 3896 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 3897 | if (gc_epilogue_callbacks_[i].callback == callback) { |
| 3898 | gc_epilogue_callbacks_.Remove(i); |
| 3899 | return; |
| 3900 | } |
| 3901 | } |
| 3902 | UNREACHABLE(); |
| 3903 | } |
| 3904 | |
| 3905 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3906 | #ifdef DEBUG |
| 3907 | |
| 3908 | class PrintHandleVisitor: public ObjectVisitor { |
| 3909 | public: |
| 3910 | void VisitPointers(Object** start, Object** end) { |
| 3911 | for (Object** p = start; p < end; p++) |
| 3912 | PrintF(" handle %p to %p\n", p, *p); |
| 3913 | } |
| 3914 | }; |
| 3915 | |
| 3916 | void Heap::PrintHandles() { |
| 3917 | PrintF("Handles:\n"); |
| 3918 | PrintHandleVisitor v; |
| 3919 | HandleScopeImplementer::Iterate(&v); |
| 3920 | } |
| 3921 | |
| 3922 | #endif |
| 3923 | |
| 3924 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3925 | Space* AllSpaces::next() { |
| 3926 | switch (counter_++) { |
| 3927 | case NEW_SPACE: |
| 3928 | return Heap::new_space(); |
| 3929 | case OLD_POINTER_SPACE: |
| 3930 | return Heap::old_pointer_space(); |
| 3931 | case OLD_DATA_SPACE: |
| 3932 | return Heap::old_data_space(); |
| 3933 | case CODE_SPACE: |
| 3934 | return Heap::code_space(); |
| 3935 | case MAP_SPACE: |
| 3936 | return Heap::map_space(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3937 | case CELL_SPACE: |
| 3938 | return Heap::cell_space(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3939 | case LO_SPACE: |
| 3940 | return Heap::lo_space(); |
| 3941 | default: |
| 3942 | return NULL; |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | |
| 3947 | PagedSpace* PagedSpaces::next() { |
| 3948 | switch (counter_++) { |
| 3949 | case OLD_POINTER_SPACE: |
| 3950 | return Heap::old_pointer_space(); |
| 3951 | case OLD_DATA_SPACE: |
| 3952 | return Heap::old_data_space(); |
| 3953 | case CODE_SPACE: |
| 3954 | return Heap::code_space(); |
| 3955 | case MAP_SPACE: |
| 3956 | return Heap::map_space(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3957 | case CELL_SPACE: |
| 3958 | return Heap::cell_space(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3959 | default: |
| 3960 | return NULL; |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | |
| 3965 | |
| 3966 | OldSpace* OldSpaces::next() { |
| 3967 | switch (counter_++) { |
| 3968 | case OLD_POINTER_SPACE: |
| 3969 | return Heap::old_pointer_space(); |
| 3970 | case OLD_DATA_SPACE: |
| 3971 | return Heap::old_data_space(); |
| 3972 | case CODE_SPACE: |
| 3973 | return Heap::code_space(); |
| 3974 | default: |
| 3975 | return NULL; |
| 3976 | } |
| 3977 | } |
| 3978 | |
| 3979 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 3980 | SpaceIterator::SpaceIterator() : current_space_(FIRST_SPACE), iterator_(NULL) { |
| 3981 | } |
| 3982 | |
| 3983 | |
| 3984 | SpaceIterator::~SpaceIterator() { |
| 3985 | // Delete active iterator if any. |
| 3986 | delete iterator_; |
| 3987 | } |
| 3988 | |
| 3989 | |
| 3990 | bool SpaceIterator::has_next() { |
| 3991 | // Iterate until no more spaces. |
| 3992 | return current_space_ != LAST_SPACE; |
| 3993 | } |
| 3994 | |
| 3995 | |
| 3996 | ObjectIterator* SpaceIterator::next() { |
| 3997 | if (iterator_ != NULL) { |
| 3998 | delete iterator_; |
| 3999 | iterator_ = NULL; |
| 4000 | // Move to the next space |
| 4001 | current_space_++; |
| 4002 | if (current_space_ > LAST_SPACE) { |
| 4003 | return NULL; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | // Return iterator for the new current space. |
| 4008 | return CreateIterator(); |
| 4009 | } |
| 4010 | |
| 4011 | |
| 4012 | // Create an iterator for the space to iterate. |
| 4013 | ObjectIterator* SpaceIterator::CreateIterator() { |
| 4014 | ASSERT(iterator_ == NULL); |
| 4015 | |
| 4016 | switch (current_space_) { |
| 4017 | case NEW_SPACE: |
| 4018 | iterator_ = new SemiSpaceIterator(Heap::new_space()); |
| 4019 | break; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4020 | case OLD_POINTER_SPACE: |
| 4021 | iterator_ = new HeapObjectIterator(Heap::old_pointer_space()); |
| 4022 | break; |
| 4023 | case OLD_DATA_SPACE: |
| 4024 | iterator_ = new HeapObjectIterator(Heap::old_data_space()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4025 | break; |
| 4026 | case CODE_SPACE: |
| 4027 | iterator_ = new HeapObjectIterator(Heap::code_space()); |
| 4028 | break; |
| 4029 | case MAP_SPACE: |
| 4030 | iterator_ = new HeapObjectIterator(Heap::map_space()); |
| 4031 | break; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4032 | case CELL_SPACE: |
| 4033 | iterator_ = new HeapObjectIterator(Heap::cell_space()); |
| 4034 | break; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4035 | case LO_SPACE: |
| 4036 | iterator_ = new LargeObjectIterator(Heap::lo_space()); |
| 4037 | break; |
| 4038 | } |
| 4039 | |
| 4040 | // Return the newly allocated iterator; |
| 4041 | ASSERT(iterator_ != NULL); |
| 4042 | return iterator_; |
| 4043 | } |
| 4044 | |
| 4045 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4046 | HeapIterator::HeapIterator() { |
| 4047 | Init(); |
| 4048 | } |
| 4049 | |
| 4050 | |
| 4051 | HeapIterator::~HeapIterator() { |
| 4052 | Shutdown(); |
| 4053 | } |
| 4054 | |
| 4055 | |
| 4056 | void HeapIterator::Init() { |
| 4057 | // Start the iteration. |
| 4058 | space_iterator_ = new SpaceIterator(); |
| 4059 | object_iterator_ = space_iterator_->next(); |
| 4060 | } |
| 4061 | |
| 4062 | |
| 4063 | void HeapIterator::Shutdown() { |
| 4064 | // Make sure the last iterator is deallocated. |
| 4065 | delete space_iterator_; |
| 4066 | space_iterator_ = NULL; |
| 4067 | object_iterator_ = NULL; |
| 4068 | } |
| 4069 | |
| 4070 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4071 | HeapObject* HeapIterator::next() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4072 | // No iterator means we are done. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4073 | if (object_iterator_ == NULL) return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4074 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4075 | if (HeapObject* obj = object_iterator_->next_object()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4076 | // If the current iterator has more objects we are fine. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4077 | return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4078 | } else { |
| 4079 | // Go though the spaces looking for one that has objects. |
| 4080 | while (space_iterator_->has_next()) { |
| 4081 | object_iterator_ = space_iterator_->next(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4082 | if (HeapObject* obj = object_iterator_->next_object()) { |
| 4083 | return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4084 | } |
| 4085 | } |
| 4086 | } |
| 4087 | // Done with the last space. |
| 4088 | object_iterator_ = NULL; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4089 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | |
| 4093 | void HeapIterator::reset() { |
| 4094 | // Restart the iterator. |
| 4095 | Shutdown(); |
| 4096 | Init(); |
| 4097 | } |
| 4098 | |
| 4099 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4100 | #ifdef DEBUG |
| 4101 | |
| 4102 | static bool search_for_any_global; |
| 4103 | static Object* search_target; |
| 4104 | static bool found_target; |
| 4105 | static List<Object*> object_stack(20); |
| 4106 | |
| 4107 | |
| 4108 | // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject. |
| 4109 | static const int kMarkTag = 2; |
| 4110 | |
| 4111 | static void MarkObjectRecursively(Object** p); |
| 4112 | class MarkObjectVisitor : public ObjectVisitor { |
| 4113 | public: |
| 4114 | void VisitPointers(Object** start, Object** end) { |
| 4115 | // Copy all HeapObject pointers in [start, end) |
| 4116 | for (Object** p = start; p < end; p++) { |
| 4117 | if ((*p)->IsHeapObject()) |
| 4118 | MarkObjectRecursively(p); |
| 4119 | } |
| 4120 | } |
| 4121 | }; |
| 4122 | |
| 4123 | static MarkObjectVisitor mark_visitor; |
| 4124 | |
| 4125 | static void MarkObjectRecursively(Object** p) { |
| 4126 | if (!(*p)->IsHeapObject()) return; |
| 4127 | |
| 4128 | HeapObject* obj = HeapObject::cast(*p); |
| 4129 | |
| 4130 | Object* map = obj->map(); |
| 4131 | |
| 4132 | if (!map->IsHeapObject()) return; // visited before |
| 4133 | |
| 4134 | if (found_target) return; // stop if target found |
| 4135 | object_stack.Add(obj); |
| 4136 | if ((search_for_any_global && obj->IsJSGlobalObject()) || |
| 4137 | (!search_for_any_global && (obj == search_target))) { |
| 4138 | found_target = true; |
| 4139 | return; |
| 4140 | } |
| 4141 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4142 | // not visited yet |
| 4143 | Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map)); |
| 4144 | |
| 4145 | Address map_addr = map_p->address(); |
| 4146 | |
| 4147 | obj->set_map(reinterpret_cast<Map*>(map_addr + kMarkTag)); |
| 4148 | |
| 4149 | MarkObjectRecursively(&map); |
| 4150 | |
| 4151 | obj->IterateBody(map_p->instance_type(), obj->SizeFromMap(map_p), |
| 4152 | &mark_visitor); |
| 4153 | |
| 4154 | if (!found_target) // don't pop if found the target |
| 4155 | object_stack.RemoveLast(); |
| 4156 | } |
| 4157 | |
| 4158 | |
| 4159 | static void UnmarkObjectRecursively(Object** p); |
| 4160 | class UnmarkObjectVisitor : public ObjectVisitor { |
| 4161 | public: |
| 4162 | void VisitPointers(Object** start, Object** end) { |
| 4163 | // Copy all HeapObject pointers in [start, end) |
| 4164 | for (Object** p = start; p < end; p++) { |
| 4165 | if ((*p)->IsHeapObject()) |
| 4166 | UnmarkObjectRecursively(p); |
| 4167 | } |
| 4168 | } |
| 4169 | }; |
| 4170 | |
| 4171 | static UnmarkObjectVisitor unmark_visitor; |
| 4172 | |
| 4173 | static void UnmarkObjectRecursively(Object** p) { |
| 4174 | if (!(*p)->IsHeapObject()) return; |
| 4175 | |
| 4176 | HeapObject* obj = HeapObject::cast(*p); |
| 4177 | |
| 4178 | Object* map = obj->map(); |
| 4179 | |
| 4180 | if (map->IsHeapObject()) return; // unmarked already |
| 4181 | |
| 4182 | Address map_addr = reinterpret_cast<Address>(map); |
| 4183 | |
| 4184 | map_addr -= kMarkTag; |
| 4185 | |
| 4186 | ASSERT_TAG_ALIGNED(map_addr); |
| 4187 | |
| 4188 | HeapObject* map_p = HeapObject::FromAddress(map_addr); |
| 4189 | |
| 4190 | obj->set_map(reinterpret_cast<Map*>(map_p)); |
| 4191 | |
| 4192 | UnmarkObjectRecursively(reinterpret_cast<Object**>(&map_p)); |
| 4193 | |
| 4194 | obj->IterateBody(Map::cast(map_p)->instance_type(), |
| 4195 | obj->SizeFromMap(Map::cast(map_p)), |
| 4196 | &unmark_visitor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | } |
| 4198 | |
| 4199 | |
| 4200 | static void MarkRootObjectRecursively(Object** root) { |
| 4201 | if (search_for_any_global) { |
| 4202 | ASSERT(search_target == NULL); |
| 4203 | } else { |
| 4204 | ASSERT(search_target->IsHeapObject()); |
| 4205 | } |
| 4206 | found_target = false; |
| 4207 | object_stack.Clear(); |
| 4208 | |
| 4209 | MarkObjectRecursively(root); |
| 4210 | UnmarkObjectRecursively(root); |
| 4211 | |
| 4212 | if (found_target) { |
| 4213 | PrintF("=====================================\n"); |
| 4214 | PrintF("==== Path to object ====\n"); |
| 4215 | PrintF("=====================================\n\n"); |
| 4216 | |
| 4217 | ASSERT(!object_stack.is_empty()); |
| 4218 | for (int i = 0; i < object_stack.length(); i++) { |
| 4219 | if (i > 0) PrintF("\n |\n |\n V\n\n"); |
| 4220 | Object* obj = object_stack[i]; |
| 4221 | obj->Print(); |
| 4222 | } |
| 4223 | PrintF("=====================================\n"); |
| 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | // Helper class for visiting HeapObjects recursively. |
| 4229 | class MarkRootVisitor: public ObjectVisitor { |
| 4230 | public: |
| 4231 | void VisitPointers(Object** start, Object** end) { |
| 4232 | // Visit all HeapObject pointers in [start, end) |
| 4233 | for (Object** p = start; p < end; p++) { |
| 4234 | if ((*p)->IsHeapObject()) |
| 4235 | MarkRootObjectRecursively(p); |
| 4236 | } |
| 4237 | } |
| 4238 | }; |
| 4239 | |
| 4240 | |
| 4241 | // Triggers a depth-first traversal of reachable objects from roots |
| 4242 | // and finds a path to a specific heap object and prints it. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4243 | void Heap::TracePathToObject(Object* target) { |
| 4244 | search_target = target; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4245 | search_for_any_global = false; |
| 4246 | |
| 4247 | MarkRootVisitor root_visitor; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4248 | IterateRoots(&root_visitor, VISIT_ONLY_STRONG); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
| 4251 | |
| 4252 | // Triggers a depth-first traversal of reachable objects from roots |
| 4253 | // and finds a path to any global object and prints it. Useful for |
| 4254 | // determining the source for leaks of global objects. |
| 4255 | void Heap::TracePathToGlobal() { |
| 4256 | search_target = NULL; |
| 4257 | search_for_any_global = true; |
| 4258 | |
| 4259 | MarkRootVisitor root_visitor; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4260 | IterateRoots(&root_visitor, VISIT_ONLY_STRONG); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4261 | } |
| 4262 | #endif |
| 4263 | |
| 4264 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4265 | static int CountTotalHolesSize() { |
| 4266 | int holes_size = 0; |
| 4267 | OldSpaces spaces; |
| 4268 | for (OldSpace* space = spaces.next(); |
| 4269 | space != NULL; |
| 4270 | space = spaces.next()) { |
| 4271 | holes_size += space->Waste() + space->AvailableFree(); |
| 4272 | } |
| 4273 | return holes_size; |
| 4274 | } |
| 4275 | |
| 4276 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4277 | GCTracer::GCTracer() |
| 4278 | : start_time_(0.0), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4279 | start_size_(0), |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4280 | gc_count_(0), |
| 4281 | full_gc_count_(0), |
| 4282 | is_compacting_(false), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4283 | marked_count_(0), |
| 4284 | allocated_since_last_gc_(0), |
| 4285 | spent_in_mutator_(0), |
| 4286 | promoted_objects_size_(0) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4287 | // These two fields reflect the state of the previous full collection. |
| 4288 | // Set them before they are changed by the collector. |
| 4289 | previous_has_compacted_ = MarkCompactCollector::HasCompacted(); |
| 4290 | previous_marked_count_ = MarkCompactCollector::previous_marked_count(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4291 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4292 | start_time_ = OS::TimeCurrentMillis(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4293 | start_size_ = Heap::SizeOfObjects(); |
| 4294 | |
| 4295 | for (int i = 0; i < Scope::kNumberOfScopes; i++) { |
| 4296 | scopes_[i] = 0; |
| 4297 | } |
| 4298 | |
| 4299 | in_free_list_or_wasted_before_gc_ = CountTotalHolesSize(); |
| 4300 | |
| 4301 | allocated_since_last_gc_ = Heap::SizeOfObjects() - alive_after_last_gc_; |
| 4302 | |
| 4303 | if (last_gc_end_timestamp_ > 0) { |
| 4304 | spent_in_mutator_ = Max(start_time_ - last_gc_end_timestamp_, 0.0); |
| 4305 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | |
| 4309 | GCTracer::~GCTracer() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4310 | // Printf ONE line iff flag is set. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4311 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
| 4312 | |
| 4313 | bool first_gc = (last_gc_end_timestamp_ == 0); |
| 4314 | |
| 4315 | alive_after_last_gc_ = Heap::SizeOfObjects(); |
| 4316 | last_gc_end_timestamp_ = OS::TimeCurrentMillis(); |
| 4317 | |
| 4318 | int time = static_cast<int>(last_gc_end_timestamp_ - start_time_); |
| 4319 | |
| 4320 | // Update cumulative GC statistics if required. |
| 4321 | if (FLAG_print_cumulative_gc_stat) { |
| 4322 | max_gc_pause_ = Max(max_gc_pause_, time); |
| 4323 | max_alive_after_gc_ = Max(max_alive_after_gc_, alive_after_last_gc_); |
| 4324 | if (!first_gc) { |
| 4325 | min_in_mutator_ = Min(min_in_mutator_, |
| 4326 | static_cast<int>(spent_in_mutator_)); |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | if (!FLAG_trace_gc_nvp) { |
| 4331 | int external_time = static_cast<int>(scopes_[Scope::EXTERNAL]); |
| 4332 | |
| 4333 | PrintF("%s %.1f -> %.1f MB, ", |
| 4334 | CollectorString(), |
| 4335 | static_cast<double>(start_size_) / MB, |
| 4336 | SizeOfHeapObjects()); |
| 4337 | |
| 4338 | if (external_time > 0) PrintF("%d / ", external_time); |
| 4339 | PrintF("%d ms.\n", time); |
| 4340 | } else { |
| 4341 | PrintF("pause=%d ", time); |
| 4342 | PrintF("mutator=%d ", |
| 4343 | static_cast<int>(spent_in_mutator_)); |
| 4344 | |
| 4345 | PrintF("gc="); |
| 4346 | switch (collector_) { |
| 4347 | case SCAVENGER: |
| 4348 | PrintF("s"); |
| 4349 | break; |
| 4350 | case MARK_COMPACTOR: |
| 4351 | PrintF(MarkCompactCollector::HasCompacted() ? "mc" : "ms"); |
| 4352 | break; |
| 4353 | default: |
| 4354 | UNREACHABLE(); |
| 4355 | } |
| 4356 | PrintF(" "); |
| 4357 | |
| 4358 | PrintF("external=%d ", static_cast<int>(scopes_[Scope::EXTERNAL])); |
| 4359 | PrintF("mark=%d ", static_cast<int>(scopes_[Scope::MC_MARK])); |
| 4360 | PrintF("sweep=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP])); |
| 4361 | PrintF("compact=%d ", static_cast<int>(scopes_[Scope::MC_COMPACT])); |
| 4362 | |
| 4363 | PrintF("total_size_before=%d ", start_size_); |
| 4364 | PrintF("total_size_after=%d ", Heap::SizeOfObjects()); |
| 4365 | PrintF("holes_size_before=%d ", in_free_list_or_wasted_before_gc_); |
| 4366 | PrintF("holes_size_after=%d ", CountTotalHolesSize()); |
| 4367 | |
| 4368 | PrintF("allocated=%d ", allocated_since_last_gc_); |
| 4369 | PrintF("promoted=%d ", promoted_objects_size_); |
| 4370 | |
| 4371 | PrintF("\n"); |
| 4372 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 4373 | |
| 4374 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
| 4375 | Heap::PrintShortHeapStatistics(); |
| 4376 | #endif |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | |
| 4380 | const char* GCTracer::CollectorString() { |
| 4381 | switch (collector_) { |
| 4382 | case SCAVENGER: |
| 4383 | return "Scavenge"; |
| 4384 | case MARK_COMPACTOR: |
| 4385 | return MarkCompactCollector::HasCompacted() ? "Mark-compact" |
| 4386 | : "Mark-sweep"; |
| 4387 | } |
| 4388 | return "Unknown GC"; |
| 4389 | } |
| 4390 | |
| 4391 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 4392 | int KeyedLookupCache::Hash(Map* map, String* name) { |
| 4393 | // Uses only lower 32 bits if pointers are larger. |
| 4394 | uintptr_t addr_hash = |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4395 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)) >> kMapHashShift; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 4396 | return static_cast<uint32_t>((addr_hash ^ name->Hash()) & kCapacityMask); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | |
| 4400 | int KeyedLookupCache::Lookup(Map* map, String* name) { |
| 4401 | int index = Hash(map, name); |
| 4402 | Key& key = keys_[index]; |
| 4403 | if ((key.map == map) && key.name->Equals(name)) { |
| 4404 | return field_offsets_[index]; |
| 4405 | } |
| 4406 | return -1; |
| 4407 | } |
| 4408 | |
| 4409 | |
| 4410 | void KeyedLookupCache::Update(Map* map, String* name, int field_offset) { |
| 4411 | String* symbol; |
| 4412 | if (Heap::LookupSymbolIfExists(name, &symbol)) { |
| 4413 | int index = Hash(map, symbol); |
| 4414 | Key& key = keys_[index]; |
| 4415 | key.map = map; |
| 4416 | key.name = symbol; |
| 4417 | field_offsets_[index] = field_offset; |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | |
| 4422 | void KeyedLookupCache::Clear() { |
| 4423 | for (int index = 0; index < kLength; index++) keys_[index].map = NULL; |
| 4424 | } |
| 4425 | |
| 4426 | |
| 4427 | KeyedLookupCache::Key KeyedLookupCache::keys_[KeyedLookupCache::kLength]; |
| 4428 | |
| 4429 | |
| 4430 | int KeyedLookupCache::field_offsets_[KeyedLookupCache::kLength]; |
| 4431 | |
| 4432 | |
| 4433 | void DescriptorLookupCache::Clear() { |
| 4434 | for (int index = 0; index < kLength; index++) keys_[index].array = NULL; |
| 4435 | } |
| 4436 | |
| 4437 | |
| 4438 | DescriptorLookupCache::Key |
| 4439 | DescriptorLookupCache::keys_[DescriptorLookupCache::kLength]; |
| 4440 | |
| 4441 | int DescriptorLookupCache::results_[DescriptorLookupCache::kLength]; |
| 4442 | |
| 4443 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4444 | #ifdef DEBUG |
| 4445 | bool Heap::GarbageCollectionGreedyCheck() { |
| 4446 | ASSERT(FLAG_gc_greedy); |
| 4447 | if (Bootstrapper::IsActive()) return true; |
| 4448 | if (disallow_allocation_failure()) return true; |
| 4449 | return CollectGarbage(0, NEW_SPACE); |
| 4450 | } |
| 4451 | #endif |
| 4452 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4453 | |
| 4454 | TranscendentalCache::TranscendentalCache(TranscendentalCache::Type t) |
| 4455 | : type_(t) { |
| 4456 | uint32_t in0 = 0xffffffffu; // Bit-pattern for a NaN that isn't |
| 4457 | uint32_t in1 = 0xffffffffu; // generated by the FPU. |
| 4458 | for (int i = 0; i < kCacheSize; i++) { |
| 4459 | elements_[i].in[0] = in0; |
| 4460 | elements_[i].in[1] = in1; |
| 4461 | elements_[i].output = NULL; |
| 4462 | } |
| 4463 | } |
| 4464 | |
| 4465 | |
| 4466 | TranscendentalCache* TranscendentalCache::caches_[kNumberOfCaches]; |
| 4467 | |
| 4468 | |
| 4469 | void TranscendentalCache::Clear() { |
| 4470 | for (int i = 0; i < kNumberOfCaches; i++) { |
| 4471 | if (caches_[i] != NULL) { |
| 4472 | delete caches_[i]; |
| 4473 | caches_[i] = NULL; |
| 4474 | } |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4479 | void ExternalStringTable::CleanUp() { |
| 4480 | int last = 0; |
| 4481 | for (int i = 0; i < new_space_strings_.length(); ++i) { |
| 4482 | if (new_space_strings_[i] == Heap::raw_unchecked_null_value()) continue; |
| 4483 | if (Heap::InNewSpace(new_space_strings_[i])) { |
| 4484 | new_space_strings_[last++] = new_space_strings_[i]; |
| 4485 | } else { |
| 4486 | old_space_strings_.Add(new_space_strings_[i]); |
| 4487 | } |
| 4488 | } |
| 4489 | new_space_strings_.Rewind(last); |
| 4490 | last = 0; |
| 4491 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
| 4492 | if (old_space_strings_[i] == Heap::raw_unchecked_null_value()) continue; |
| 4493 | ASSERT(!Heap::InNewSpace(old_space_strings_[i])); |
| 4494 | old_space_strings_[last++] = old_space_strings_[i]; |
| 4495 | } |
| 4496 | old_space_strings_.Rewind(last); |
| 4497 | Verify(); |
| 4498 | } |
| 4499 | |
| 4500 | |
| 4501 | void ExternalStringTable::TearDown() { |
| 4502 | new_space_strings_.Free(); |
| 4503 | old_space_strings_.Free(); |
| 4504 | } |
| 4505 | |
| 4506 | |
| 4507 | List<Object*> ExternalStringTable::new_space_strings_; |
| 4508 | List<Object*> ExternalStringTable::old_space_strings_; |
| 4509 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4510 | } } // namespace v8::internal |