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