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