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