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