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