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