erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "accessors.h" |
| 31 | #include "api.h" |
| 32 | #include "bootstrapper.h" |
| 33 | #include "codegen-inl.h" |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 34 | #include "compilation-cache.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | #include "debug.h" |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 36 | #include "heap-profiler.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | #include "global-handles.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | #include "mark-compact.h" |
| 39 | #include "natives.h" |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 40 | #include "objects-visiting.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | #include "scanner.h" |
| 42 | #include "scopeinfo.h" |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 43 | #include "snapshot.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | #include "v8threads.h" |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 45 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 46 | #include "regexp-macro-assembler.h" |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 47 | #include "arm/regexp-macro-assembler-arm.h" |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 48 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 50 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 51 | namespace v8 { |
| 52 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 55 | String* Heap::hidden_symbol_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 56 | Object* Heap::roots_[Heap::kRootListLength]; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 57 | Object* Heap::global_contexts_list_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 58 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 59 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 60 | NewSpace Heap::new_space_; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 61 | OldSpace* Heap::old_pointer_space_ = NULL; |
| 62 | OldSpace* Heap::old_data_space_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | OldSpace* Heap::code_space_ = NULL; |
| 64 | MapSpace* Heap::map_space_ = NULL; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 65 | CellSpace* Heap::cell_space_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 66 | LargeObjectSpace* Heap::lo_space_ = NULL; |
| 67 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 68 | static const intptr_t kMinimumPromotionLimit = 2 * MB; |
| 69 | static const intptr_t kMinimumAllocationLimit = 8 * MB; |
| 70 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 71 | intptr_t Heap::old_gen_promotion_limit_ = kMinimumPromotionLimit; |
| 72 | intptr_t Heap::old_gen_allocation_limit_ = kMinimumAllocationLimit; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 73 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | int Heap::old_gen_exhausted_ = false; |
| 75 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 76 | int Heap::amount_of_external_allocated_memory_ = 0; |
| 77 | int Heap::amount_of_external_allocated_memory_at_last_global_gc_ = 0; |
| 78 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 79 | // semispace_size_ should be a power of 2 and old_generation_size_ should be |
| 80 | // a multiple of Page::kPageSize. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 81 | #if defined(ANDROID) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 82 | int Heap::max_semispace_size_ = 2*MB; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 83 | intptr_t Heap::max_old_generation_size_ = 192*MB; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 84 | int Heap::initial_semispace_size_ = 128*KB; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 85 | intptr_t Heap::code_range_size_ = 0; |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 86 | #elif defined(V8_TARGET_ARCH_X64) |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 87 | int Heap::max_semispace_size_ = 16*MB; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 88 | intptr_t Heap::max_old_generation_size_ = 1*GB; |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 89 | int Heap::initial_semispace_size_ = 1*MB; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 90 | intptr_t Heap::code_range_size_ = 512*MB; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 91 | #else |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 92 | int Heap::max_semispace_size_ = 8*MB; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 93 | intptr_t Heap::max_old_generation_size_ = 512*MB; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 94 | int Heap::initial_semispace_size_ = 512*KB; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 95 | intptr_t Heap::code_range_size_ = 0; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 96 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 97 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 98 | // The snapshot semispace size will be the default semispace size if |
| 99 | // snapshotting is used and will be the requested semispace size as |
| 100 | // set up by ConfigureHeap otherwise. |
| 101 | int Heap::reserved_semispace_size_ = Heap::max_semispace_size_; |
| 102 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 103 | List<Heap::GCPrologueCallbackPair> Heap::gc_prologue_callbacks_; |
| 104 | List<Heap::GCEpilogueCallbackPair> Heap::gc_epilogue_callbacks_; |
| 105 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | GCCallback Heap::global_gc_prologue_callback_ = NULL; |
| 107 | GCCallback Heap::global_gc_epilogue_callback_ = NULL; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 108 | HeapObjectCallback Heap::gc_safe_size_of_old_object_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | |
| 110 | // Variables set based on semispace_size_ and old_generation_size_ in |
| 111 | // ConfigureHeap. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 112 | |
| 113 | // Will be 4 * reserved_semispace_size_ to ensure that young |
| 114 | // generation can be aligned to its size. |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 115 | int Heap::survived_since_last_expansion_ = 0; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 116 | intptr_t Heap::external_allocation_limit_ = 0; |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 117 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 118 | Heap::HeapState Heap::gc_state_ = NOT_IN_GC; |
| 119 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | int Heap::mc_count_ = 0; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 121 | int Heap::ms_count_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | int Heap::gc_count_ = 0; |
| 123 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 124 | GCTracer* Heap::tracer_ = NULL; |
| 125 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 126 | int Heap::unflattened_strings_length_ = 0; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 127 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 128 | int Heap::always_allocate_scope_depth_ = 0; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 129 | int Heap::linear_allocation_scope_depth_ = 0; |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 130 | int Heap::contexts_disposed_ = 0; |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 131 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 132 | int Heap::young_survivors_after_last_gc_ = 0; |
| 133 | int Heap::high_survival_rate_period_length_ = 0; |
| 134 | double Heap::survival_rate_ = 0; |
| 135 | Heap::SurvivalRateTrend Heap::previous_survival_rate_trend_ = Heap::STABLE; |
| 136 | Heap::SurvivalRateTrend Heap::survival_rate_trend_ = Heap::STABLE; |
| 137 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 138 | #ifdef DEBUG |
| 139 | bool Heap::allocation_allowed_ = true; |
| 140 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | int Heap::allocation_timeout_ = 0; |
| 142 | bool Heap::disallow_allocation_failure_ = false; |
| 143 | #endif // DEBUG |
| 144 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 145 | intptr_t GCTracer::alive_after_last_gc_ = 0; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 146 | double GCTracer::last_gc_end_timestamp_ = 0.0; |
| 147 | int GCTracer::max_gc_pause_ = 0; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 148 | intptr_t GCTracer::max_alive_after_gc_ = 0; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 149 | int GCTracer::min_in_mutator_ = kMaxInt; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 151 | intptr_t Heap::Capacity() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | if (!HasBeenSetup()) return 0; |
| 153 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 154 | return new_space_.Capacity() + |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 155 | old_pointer_space_->Capacity() + |
| 156 | old_data_space_->Capacity() + |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | code_space_->Capacity() + |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 158 | map_space_->Capacity() + |
| 159 | cell_space_->Capacity(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 163 | intptr_t Heap::CommittedMemory() { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 164 | if (!HasBeenSetup()) return 0; |
| 165 | |
| 166 | return new_space_.CommittedMemory() + |
| 167 | old_pointer_space_->CommittedMemory() + |
| 168 | old_data_space_->CommittedMemory() + |
| 169 | code_space_->CommittedMemory() + |
| 170 | map_space_->CommittedMemory() + |
| 171 | cell_space_->CommittedMemory() + |
| 172 | lo_space_->Size(); |
| 173 | } |
| 174 | |
| 175 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 176 | intptr_t Heap::Available() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 177 | if (!HasBeenSetup()) return 0; |
| 178 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 179 | return new_space_.Available() + |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 180 | old_pointer_space_->Available() + |
| 181 | old_data_space_->Available() + |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | code_space_->Available() + |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 183 | map_space_->Available() + |
| 184 | cell_space_->Available(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | bool Heap::HasBeenSetup() { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 189 | return old_pointer_space_ != NULL && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 190 | old_data_space_ != NULL && |
| 191 | code_space_ != NULL && |
| 192 | map_space_ != NULL && |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 193 | cell_space_ != NULL && |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 194 | lo_space_ != NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 198 | int Heap::GcSafeSizeOfOldObject(HeapObject* object) { |
| 199 | ASSERT(!Heap::InNewSpace(object)); // Code only works for old objects. |
| 200 | ASSERT(!MarkCompactCollector::are_map_pointers_encoded()); |
| 201 | MapWord map_word = object->map_word(); |
| 202 | map_word.ClearMark(); |
| 203 | map_word.ClearOverflow(); |
| 204 | return object->SizeFromMap(map_word.ToMap()); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | int Heap::GcSafeSizeOfOldObjectWithEncodedMap(HeapObject* object) { |
| 209 | ASSERT(!Heap::InNewSpace(object)); // Code only works for old objects. |
| 210 | ASSERT(MarkCompactCollector::are_map_pointers_encoded()); |
| 211 | uint32_t marker = Memory::uint32_at(object->address()); |
| 212 | if (marker == MarkCompactCollector::kSingleFreeEncoding) { |
| 213 | return kIntSize; |
| 214 | } else if (marker == MarkCompactCollector::kMultiFreeEncoding) { |
| 215 | return Memory::int_at(object->address() + kIntSize); |
| 216 | } else { |
| 217 | MapWord map_word = object->map_word(); |
| 218 | Address map_address = map_word.DecodeMapAddress(Heap::map_space()); |
| 219 | Map* map = reinterpret_cast<Map*>(HeapObject::FromAddress(map_address)); |
| 220 | return object->SizeFromMap(map); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | GarbageCollector Heap::SelectGarbageCollector(AllocationSpace space) { |
| 226 | // Is global GC requested? |
| 227 | if (space != NEW_SPACE || FLAG_gc_global) { |
| 228 | Counters::gc_compactor_caused_by_request.Increment(); |
| 229 | return MARK_COMPACTOR; |
| 230 | } |
| 231 | |
| 232 | // Is enough data promoted to justify a global GC? |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 233 | if (OldGenerationPromotionLimitReached()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 234 | Counters::gc_compactor_caused_by_promoted_data.Increment(); |
| 235 | return MARK_COMPACTOR; |
| 236 | } |
| 237 | |
| 238 | // Have allocation in OLD and LO failed? |
| 239 | if (old_gen_exhausted_) { |
| 240 | Counters::gc_compactor_caused_by_oldspace_exhaustion.Increment(); |
| 241 | return MARK_COMPACTOR; |
| 242 | } |
| 243 | |
| 244 | // Is there enough space left in OLD to guarantee that a scavenge can |
| 245 | // succeed? |
| 246 | // |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 247 | // Note that MemoryAllocator->MaxAvailable() undercounts the memory available |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 248 | // for object promotion. It counts only the bytes that the memory |
| 249 | // allocator has not yet allocated from the OS and assigned to any space, |
| 250 | // and does not count available bytes already in the old space or code |
| 251 | // space. Undercounting is safe---we may get an unrequested full GC when |
| 252 | // a scavenge would have succeeded. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 253 | if (MemoryAllocator::MaxAvailable() <= new_space_.Size()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 254 | Counters::gc_compactor_caused_by_oldspace_exhaustion.Increment(); |
| 255 | return MARK_COMPACTOR; |
| 256 | } |
| 257 | |
| 258 | // Default |
| 259 | return SCAVENGER; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 264 | // --log-gc to avoid the complicated preprocessor and flag testing. |
| 265 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 266 | void Heap::ReportStatisticsBeforeGC() { |
| 267 | // Heap::ReportHeapStatistics will also log NewSpace statistics when |
| 268 | // compiled with ENABLE_LOGGING_AND_PROFILING and --log-gc is set. The |
| 269 | // following logic is used to avoid double logging. |
| 270 | #if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 271 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.CollectStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | if (FLAG_heap_stats) { |
| 273 | ReportHeapStatistics("Before GC"); |
| 274 | } else if (FLAG_log_gc) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 275 | new_space_.ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 277 | if (FLAG_heap_stats || FLAG_log_gc) new_space_.ClearHistograms(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | #elif defined(DEBUG) |
| 279 | if (FLAG_heap_stats) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 280 | new_space_.CollectStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | ReportHeapStatistics("Before GC"); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 282 | new_space_.ClearHistograms(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 283 | } |
| 284 | #elif defined(ENABLE_LOGGING_AND_PROFILING) |
| 285 | if (FLAG_log_gc) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 286 | new_space_.CollectStatistics(); |
| 287 | new_space_.ReportStatistics(); |
| 288 | new_space_.ClearHistograms(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 289 | } |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 294 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
| 295 | void Heap::PrintShortHeapStatistics() { |
| 296 | if (!FLAG_trace_gc_verbose) return; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 297 | PrintF("Memory allocator, used: %8" V8_PTR_PREFIX "d" |
| 298 | ", available: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 299 | MemoryAllocator::Size(), |
| 300 | MemoryAllocator::Available()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 301 | PrintF("New space, used: %8" V8_PTR_PREFIX "d" |
| 302 | ", available: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 303 | Heap::new_space_.Size(), |
| 304 | new_space_.Available()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 305 | PrintF("Old pointers, used: %8" V8_PTR_PREFIX "d" |
| 306 | ", available: %8" V8_PTR_PREFIX "d" |
| 307 | ", waste: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 308 | old_pointer_space_->Size(), |
| 309 | old_pointer_space_->Available(), |
| 310 | old_pointer_space_->Waste()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 311 | PrintF("Old data space, used: %8" V8_PTR_PREFIX "d" |
| 312 | ", available: %8" V8_PTR_PREFIX "d" |
| 313 | ", waste: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 314 | old_data_space_->Size(), |
| 315 | old_data_space_->Available(), |
| 316 | old_data_space_->Waste()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 317 | PrintF("Code space, used: %8" V8_PTR_PREFIX "d" |
| 318 | ", available: %8" V8_PTR_PREFIX "d" |
| 319 | ", waste: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 320 | code_space_->Size(), |
| 321 | code_space_->Available(), |
| 322 | code_space_->Waste()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 323 | PrintF("Map space, used: %8" V8_PTR_PREFIX "d" |
| 324 | ", available: %8" V8_PTR_PREFIX "d" |
| 325 | ", waste: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 326 | map_space_->Size(), |
| 327 | map_space_->Available(), |
| 328 | map_space_->Waste()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 329 | PrintF("Cell space, used: %8" V8_PTR_PREFIX "d" |
| 330 | ", available: %8" V8_PTR_PREFIX "d" |
| 331 | ", waste: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 332 | cell_space_->Size(), |
| 333 | cell_space_->Available(), |
| 334 | cell_space_->Waste()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 335 | PrintF("Large object space, used: %8" V8_PTR_PREFIX "d" |
| 336 | ", available: %8" V8_PTR_PREFIX "d\n", |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 337 | lo_space_->Size(), |
| 338 | lo_space_->Available()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 339 | } |
| 340 | #endif |
| 341 | |
| 342 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 343 | // TODO(1238405): Combine the infrastructure for --heap-stats and |
| 344 | // --log-gc to avoid the complicated preprocessor and flag testing. |
| 345 | void Heap::ReportStatisticsAfterGC() { |
| 346 | // Similar to the before GC, we use some complicated logic to ensure that |
| 347 | // NewSpace statistics are logged exactly once when --log-gc is turned on. |
| 348 | #if defined(DEBUG) && defined(ENABLE_LOGGING_AND_PROFILING) |
| 349 | if (FLAG_heap_stats) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 350 | new_space_.CollectStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 351 | ReportHeapStatistics("After GC"); |
| 352 | } else if (FLAG_log_gc) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 353 | new_space_.ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | } |
| 355 | #elif defined(DEBUG) |
| 356 | if (FLAG_heap_stats) ReportHeapStatistics("After GC"); |
| 357 | #elif defined(ENABLE_LOGGING_AND_PROFILING) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 358 | if (FLAG_log_gc) new_space_.ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 359 | #endif |
| 360 | } |
| 361 | #endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 362 | |
| 363 | |
| 364 | void Heap::GarbageCollectionPrologue() { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 365 | TranscendentalCache::Clear(); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 366 | ClearJSFunctionResultCaches(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 367 | gc_count_++; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 368 | unflattened_strings_length_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | #ifdef DEBUG |
| 370 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 371 | allow_allocation(false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 372 | |
| 373 | if (FLAG_verify_heap) { |
| 374 | Verify(); |
| 375 | } |
| 376 | |
| 377 | if (FLAG_gc_verbose) Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 378 | #endif |
| 379 | |
| 380 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 381 | ReportStatisticsBeforeGC(); |
| 382 | #endif |
| 383 | } |
| 384 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 385 | intptr_t Heap::SizeOfObjects() { |
| 386 | intptr_t total = 0; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 387 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 388 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 389 | total += space->Size(); |
| 390 | } |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 391 | return total; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void Heap::GarbageCollectionEpilogue() { |
| 395 | #ifdef DEBUG |
| 396 | allow_allocation(true); |
| 397 | ZapFromSpace(); |
| 398 | |
| 399 | if (FLAG_verify_heap) { |
| 400 | Verify(); |
| 401 | } |
| 402 | |
| 403 | if (FLAG_print_global_handles) GlobalHandles::Print(); |
| 404 | if (FLAG_print_handles) PrintHandles(); |
| 405 | if (FLAG_gc_verbose) Print(); |
| 406 | if (FLAG_code_stats) ReportCodeStatistics("After GC"); |
| 407 | #endif |
| 408 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 409 | Counters::alive_after_last_gc.Set(static_cast<int>(SizeOfObjects())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 410 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 411 | Counters::symbol_table_capacity.Set(symbol_table()->Capacity()); |
| 412 | Counters::number_of_symbols.Set(symbol_table()->NumberOfElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 414 | ReportStatisticsAfterGC(); |
| 415 | #endif |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 416 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 417 | Debug::AfterGarbageCollection(); |
| 418 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 422 | void Heap::CollectAllGarbage(bool force_compaction) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 423 | // Since we are ignoring the return value, the exact choice of space does |
| 424 | // not matter, so long as we do not specify NEW_SPACE, which would not |
| 425 | // cause a full GC. |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 426 | MarkCompactCollector::SetForceCompaction(force_compaction); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 427 | CollectGarbage(OLD_POINTER_SPACE); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 428 | MarkCompactCollector::SetForceCompaction(false); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 432 | void Heap::CollectGarbage(AllocationSpace space) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 433 | // The VM is in the GC state until exiting this function. |
| 434 | VMState state(GC); |
| 435 | |
| 436 | #ifdef DEBUG |
| 437 | // Reset the allocation timeout to the GC interval, but make sure to |
| 438 | // allow at least a few allocations after a collection. The reason |
| 439 | // for this is that we have a lot of allocation sequences and we |
| 440 | // assume that a garbage collection will allow the subsequent |
| 441 | // allocation attempts to go through. |
| 442 | allocation_timeout_ = Max(6, FLAG_gc_interval); |
| 443 | #endif |
| 444 | |
| 445 | { GCTracer tracer; |
| 446 | GarbageCollectionPrologue(); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 447 | // The GC count was incremented in the prologue. Tell the tracer about |
| 448 | // it. |
| 449 | tracer.set_gc_count(gc_count_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 450 | |
| 451 | GarbageCollector collector = SelectGarbageCollector(space); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 452 | // Tell the tracer which collector we've selected. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 453 | tracer.set_collector(collector); |
| 454 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 455 | HistogramTimer* rate = (collector == SCAVENGER) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 456 | ? &Counters::gc_scavenger |
| 457 | : &Counters::gc_compactor; |
| 458 | rate->Start(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 459 | PerformGarbageCollection(collector, &tracer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 460 | rate->Stop(); |
| 461 | |
| 462 | GarbageCollectionEpilogue(); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 467 | if (FLAG_log_gc) HeapProfiler::WriteSample(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 468 | if (CpuProfiler::is_profiling()) CpuProfiler::ProcessMovedFunctions(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 469 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 473 | void Heap::PerformScavenge() { |
| 474 | GCTracer tracer; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 475 | PerformGarbageCollection(SCAVENGER, &tracer); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 479 | #ifdef DEBUG |
kasperl@chromium.org | 416c5b0 | 2009-04-14 14:03:52 +0000 | [diff] [blame] | 480 | // Helper class for verifying the symbol table. |
| 481 | class SymbolTableVerifier : public ObjectVisitor { |
| 482 | public: |
| 483 | SymbolTableVerifier() { } |
| 484 | void VisitPointers(Object** start, Object** end) { |
| 485 | // Visit all HeapObject pointers in [start, end). |
| 486 | for (Object** p = start; p < end; p++) { |
| 487 | if ((*p)->IsHeapObject()) { |
| 488 | // Check that the symbol is actually a symbol. |
| 489 | ASSERT((*p)->IsNull() || (*p)->IsUndefined() || (*p)->IsSymbol()); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
kasperl@chromium.org | 416c5b0 | 2009-04-14 14:03:52 +0000 | [diff] [blame] | 492 | } |
| 493 | }; |
| 494 | #endif // DEBUG |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 495 | |
kasperl@chromium.org | 416c5b0 | 2009-04-14 14:03:52 +0000 | [diff] [blame] | 496 | |
| 497 | static void VerifySymbolTable() { |
| 498 | #ifdef DEBUG |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 499 | SymbolTableVerifier verifier; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 500 | Heap::symbol_table()->IterateElements(&verifier); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 501 | #endif // DEBUG |
| 502 | } |
| 503 | |
| 504 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 505 | void Heap::ReserveSpace( |
| 506 | int new_space_size, |
| 507 | int pointer_space_size, |
| 508 | int data_space_size, |
| 509 | int code_space_size, |
| 510 | int map_space_size, |
| 511 | int cell_space_size, |
| 512 | int large_object_size) { |
| 513 | NewSpace* new_space = Heap::new_space(); |
| 514 | PagedSpace* old_pointer_space = Heap::old_pointer_space(); |
| 515 | PagedSpace* old_data_space = Heap::old_data_space(); |
| 516 | PagedSpace* code_space = Heap::code_space(); |
| 517 | PagedSpace* map_space = Heap::map_space(); |
| 518 | PagedSpace* cell_space = Heap::cell_space(); |
| 519 | LargeObjectSpace* lo_space = Heap::lo_space(); |
| 520 | bool gc_performed = true; |
| 521 | while (gc_performed) { |
| 522 | gc_performed = false; |
| 523 | if (!new_space->ReserveSpace(new_space_size)) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 524 | Heap::CollectGarbage(NEW_SPACE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 525 | gc_performed = true; |
| 526 | } |
| 527 | if (!old_pointer_space->ReserveSpace(pointer_space_size)) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 528 | Heap::CollectGarbage(OLD_POINTER_SPACE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 529 | gc_performed = true; |
| 530 | } |
| 531 | if (!(old_data_space->ReserveSpace(data_space_size))) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 532 | Heap::CollectGarbage(OLD_DATA_SPACE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 533 | gc_performed = true; |
| 534 | } |
| 535 | if (!(code_space->ReserveSpace(code_space_size))) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 536 | Heap::CollectGarbage(CODE_SPACE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 537 | gc_performed = true; |
| 538 | } |
| 539 | if (!(map_space->ReserveSpace(map_space_size))) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 540 | Heap::CollectGarbage(MAP_SPACE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 541 | gc_performed = true; |
| 542 | } |
| 543 | if (!(cell_space->ReserveSpace(cell_space_size))) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 544 | Heap::CollectGarbage(CELL_SPACE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 545 | gc_performed = true; |
| 546 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 547 | // We add a slack-factor of 2 in order to have space for a series of |
| 548 | // large-object allocations that are only just larger than the page size. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 549 | large_object_size *= 2; |
| 550 | // The ReserveSpace method on the large object space checks how much |
| 551 | // we can expand the old generation. This includes expansion caused by |
| 552 | // allocation in the other spaces. |
| 553 | large_object_size += cell_space_size + map_space_size + code_space_size + |
| 554 | data_space_size + pointer_space_size; |
| 555 | if (!(lo_space->ReserveSpace(large_object_size))) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 556 | Heap::CollectGarbage(LO_SPACE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 557 | gc_performed = true; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 563 | void Heap::EnsureFromSpaceIsCommitted() { |
| 564 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 565 | |
| 566 | // Committing memory to from space failed. |
| 567 | // Try shrinking and try again. |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 568 | PagedSpaces spaces; |
| 569 | for (PagedSpace* space = spaces.next(); |
| 570 | space != NULL; |
| 571 | space = spaces.next()) { |
| 572 | space->RelinkPageListInChunkOrder(true); |
| 573 | } |
| 574 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 575 | Shrink(); |
| 576 | if (new_space_.CommitFromSpaceIfNeeded()) return; |
| 577 | |
| 578 | // Committing memory to from space failed again. |
| 579 | // Memory is exhausted and we will die. |
| 580 | V8::FatalProcessOutOfMemory("Committing semi space failed."); |
| 581 | } |
| 582 | |
| 583 | |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 584 | class ClearThreadJSFunctionResultCachesVisitor: public ThreadVisitor { |
| 585 | virtual void VisitThread(ThreadLocalTop* top) { |
| 586 | Context* context = top->context_; |
| 587 | if (context == NULL) return; |
| 588 | |
| 589 | FixedArray* caches = |
| 590 | context->global()->global_context()->jsfunction_result_caches(); |
| 591 | int length = caches->length(); |
| 592 | for (int i = 0; i < length; i++) { |
| 593 | JSFunctionResultCache::cast(caches->get(i))->Clear(); |
| 594 | } |
| 595 | } |
| 596 | }; |
| 597 | |
| 598 | |
| 599 | void Heap::ClearJSFunctionResultCaches() { |
| 600 | if (Bootstrapper::IsActive()) return; |
| 601 | ClearThreadJSFunctionResultCachesVisitor visitor; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 602 | ThreadManager::IterateArchivedThreads(&visitor); |
ager@chromium.org | ac091b7 | 2010-05-05 07:34:42 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 606 | void Heap::ClearNormalizedMapCaches() { |
| 607 | if (Bootstrapper::IsActive()) return; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 608 | |
| 609 | Object* context = global_contexts_list_; |
| 610 | while (!context->IsUndefined()) { |
| 611 | Context::cast(context)->normalized_map_cache()->Clear(); |
| 612 | context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK); |
| 613 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 617 | #ifdef DEBUG |
| 618 | |
| 619 | enum PageWatermarkValidity { |
| 620 | ALL_VALID, |
| 621 | ALL_INVALID |
| 622 | }; |
| 623 | |
| 624 | static void VerifyPageWatermarkValidity(PagedSpace* space, |
| 625 | PageWatermarkValidity validity) { |
| 626 | PageIterator it(space, PageIterator::PAGES_IN_USE); |
| 627 | bool expected_value = (validity == ALL_VALID); |
| 628 | while (it.has_next()) { |
| 629 | Page* page = it.next(); |
| 630 | ASSERT(page->IsWatermarkValid() == expected_value); |
| 631 | } |
| 632 | } |
| 633 | #endif |
| 634 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 635 | void Heap::UpdateSurvivalRateTrend(int start_new_space_size) { |
| 636 | double survival_rate = |
| 637 | (static_cast<double>(young_survivors_after_last_gc_) * 100) / |
| 638 | start_new_space_size; |
| 639 | |
| 640 | if (survival_rate > kYoungSurvivalRateThreshold) { |
| 641 | high_survival_rate_period_length_++; |
| 642 | } else { |
| 643 | high_survival_rate_period_length_ = 0; |
| 644 | } |
| 645 | |
| 646 | double survival_rate_diff = survival_rate_ - survival_rate; |
| 647 | |
| 648 | if (survival_rate_diff > kYoungSurvivalRateAllowedDeviation) { |
| 649 | set_survival_rate_trend(DECREASING); |
| 650 | } else if (survival_rate_diff < -kYoungSurvivalRateAllowedDeviation) { |
| 651 | set_survival_rate_trend(INCREASING); |
| 652 | } else { |
| 653 | set_survival_rate_trend(STABLE); |
| 654 | } |
| 655 | |
| 656 | survival_rate_ = survival_rate; |
| 657 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 658 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 659 | void Heap::PerformGarbageCollection(GarbageCollector collector, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 660 | GCTracer* tracer) { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 661 | if (collector != SCAVENGER) { |
| 662 | PROFILE(CodeMovingGCEvent()); |
| 663 | } |
| 664 | |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 665 | VerifySymbolTable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 666 | if (collector == MARK_COMPACTOR && global_gc_prologue_callback_) { |
| 667 | ASSERT(!allocation_allowed_); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 668 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 669 | global_gc_prologue_callback_(); |
| 670 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 671 | |
| 672 | GCType gc_type = |
| 673 | collector == MARK_COMPACTOR ? kGCTypeMarkSweepCompact : kGCTypeScavenge; |
| 674 | |
| 675 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 676 | if (gc_type & gc_prologue_callbacks_[i].gc_type) { |
| 677 | gc_prologue_callbacks_[i].callback(gc_type, kNoGCCallbackFlags); |
| 678 | } |
| 679 | } |
| 680 | |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame] | 681 | EnsureFromSpaceIsCommitted(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 682 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 683 | int start_new_space_size = Heap::new_space()->SizeAsInt(); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 684 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 685 | if (collector == MARK_COMPACTOR) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 686 | // Perform mark-sweep with optional compaction. |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 687 | MarkCompact(tracer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 688 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 689 | bool high_survival_rate_during_scavenges = IsHighSurvivalRate() && |
| 690 | IsStableOrIncreasingSurvivalTrend(); |
| 691 | |
| 692 | UpdateSurvivalRateTrend(start_new_space_size); |
| 693 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 694 | intptr_t old_gen_size = PromotedSpaceSize(); |
| 695 | old_gen_promotion_limit_ = |
| 696 | old_gen_size + Max(kMinimumPromotionLimit, old_gen_size / 3); |
| 697 | old_gen_allocation_limit_ = |
| 698 | old_gen_size + Max(kMinimumAllocationLimit, old_gen_size / 2); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 699 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 700 | if (high_survival_rate_during_scavenges && |
| 701 | IsStableOrIncreasingSurvivalTrend()) { |
| 702 | // Stable high survival rates of young objects both during partial and |
| 703 | // full collection indicate that mutator is either building or modifying |
| 704 | // a structure with a long lifetime. |
| 705 | // In this case we aggressively raise old generation memory limits to |
| 706 | // postpone subsequent mark-sweep collection and thus trade memory |
| 707 | // space for the mutation speed. |
| 708 | old_gen_promotion_limit_ *= 2; |
| 709 | old_gen_allocation_limit_ *= 2; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 710 | } |
| 711 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 712 | old_gen_exhausted_ = false; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 713 | } else { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 714 | tracer_ = tracer; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 715 | Scavenge(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 716 | tracer_ = NULL; |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 717 | |
| 718 | UpdateSurvivalRateTrend(start_new_space_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 719 | } |
ager@chromium.org | 439e85a | 2009-08-26 13:15:29 +0000 | [diff] [blame] | 720 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 721 | Counters::objs_since_last_young.Set(0); |
| 722 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 723 | if (collector == MARK_COMPACTOR) { |
| 724 | DisableAssertNoAllocation allow_allocation; |
| 725 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
| 726 | GlobalHandles::PostGarbageCollectionProcessing(); |
| 727 | } |
| 728 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 729 | // Update relocatables. |
| 730 | Relocatable::PostGarbageCollectionProcessing(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 731 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 732 | if (collector == MARK_COMPACTOR) { |
| 733 | // Register the amount of external allocated memory. |
| 734 | amount_of_external_allocated_memory_at_last_global_gc_ = |
| 735 | amount_of_external_allocated_memory_; |
| 736 | } |
| 737 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 738 | GCCallbackFlags callback_flags = tracer->is_compacting() |
| 739 | ? kGCCallbackFlagCompacted |
| 740 | : kNoGCCallbackFlags; |
| 741 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 742 | if (gc_type & gc_epilogue_callbacks_[i].gc_type) { |
| 743 | gc_epilogue_callbacks_[i].callback(gc_type, callback_flags); |
| 744 | } |
| 745 | } |
| 746 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 747 | if (collector == MARK_COMPACTOR && global_gc_epilogue_callback_) { |
| 748 | ASSERT(!allocation_allowed_); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 749 | GCTracer::Scope scope(tracer, GCTracer::Scope::EXTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 750 | global_gc_epilogue_callback_(); |
| 751 | } |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 752 | VerifySymbolTable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 756 | void Heap::MarkCompact(GCTracer* tracer) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 757 | gc_state_ = MARK_COMPACT; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 758 | LOG(ResourceEvent("markcompact", "begin")); |
| 759 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 760 | MarkCompactCollector::Prepare(tracer); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 761 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 762 | bool is_compacting = MarkCompactCollector::IsCompacting(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 763 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 764 | if (is_compacting) { |
| 765 | mc_count_++; |
| 766 | } else { |
| 767 | ms_count_++; |
| 768 | } |
| 769 | tracer->set_full_gc_count(mc_count_ + ms_count_); |
| 770 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 771 | MarkCompactPrologue(is_compacting); |
| 772 | |
| 773 | MarkCompactCollector::CollectGarbage(); |
| 774 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 775 | LOG(ResourceEvent("markcompact", "end")); |
| 776 | |
| 777 | gc_state_ = NOT_IN_GC; |
| 778 | |
| 779 | Shrink(); |
| 780 | |
| 781 | Counters::objs_since_last_full.Set(0); |
kasperl@chromium.org | 8b2bb26 | 2010-03-01 09:46:28 +0000 | [diff] [blame] | 782 | |
| 783 | contexts_disposed_ = 0; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 787 | void Heap::MarkCompactPrologue(bool is_compacting) { |
| 788 | // At any old GC clear the keyed lookup cache to enable collection of unused |
| 789 | // maps. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 790 | KeyedLookupCache::Clear(); |
| 791 | ContextSlotCache::Clear(); |
| 792 | DescriptorLookupCache::Clear(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 793 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 794 | CompilationCache::MarkCompactPrologue(); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 795 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 796 | CompletelyClearInstanceofCache(); |
| 797 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 798 | if (is_compacting) FlushNumberStringCache(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 799 | |
| 800 | ClearNormalizedMapCaches(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 804 | Object* Heap::FindCodeObject(Address a) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 805 | Object* obj = NULL; // Initialization to please compiler. |
| 806 | { MaybeObject* maybe_obj = code_space_->FindObject(a); |
| 807 | if (!maybe_obj->ToObject(&obj)) { |
| 808 | obj = lo_space_->FindObject(a)->ToObjectUnchecked(); |
| 809 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 810 | } |
| 811 | return obj; |
| 812 | } |
| 813 | |
| 814 | |
| 815 | // Helper class for copying HeapObjects |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 816 | class ScavengeVisitor: public ObjectVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 817 | public: |
| 818 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 819 | void VisitPointer(Object** p) { ScavengePointer(p); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 820 | |
| 821 | void VisitPointers(Object** start, Object** end) { |
| 822 | // Copy all HeapObject pointers in [start, end) |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 823 | for (Object** p = start; p < end; p++) ScavengePointer(p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | private: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 827 | void ScavengePointer(Object** p) { |
| 828 | Object* object = *p; |
| 829 | if (!Heap::InNewSpace(object)) return; |
| 830 | Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p), |
| 831 | reinterpret_cast<HeapObject*>(object)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 832 | } |
| 833 | }; |
| 834 | |
| 835 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 836 | // A queue of objects promoted during scavenge. Each object is accompanied |
| 837 | // by it's size to avoid dereferencing a map pointer for scanning. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 838 | class PromotionQueue { |
| 839 | public: |
| 840 | void Initialize(Address start_address) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 841 | front_ = rear_ = reinterpret_cast<intptr_t*>(start_address); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | bool is_empty() { return front_ <= rear_; } |
| 845 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 846 | void insert(HeapObject* target, int size) { |
| 847 | *(--rear_) = reinterpret_cast<intptr_t>(target); |
| 848 | *(--rear_) = size; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 849 | // Assert no overflow into live objects. |
| 850 | ASSERT(reinterpret_cast<Address>(rear_) >= Heap::new_space()->top()); |
| 851 | } |
| 852 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 853 | void remove(HeapObject** target, int* size) { |
| 854 | *target = reinterpret_cast<HeapObject*>(*(--front_)); |
| 855 | *size = static_cast<int>(*(--front_)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 856 | // Assert no underflow. |
| 857 | ASSERT(front_ >= rear_); |
| 858 | } |
| 859 | |
| 860 | private: |
| 861 | // The front of the queue is higher in memory than the rear. |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 862 | intptr_t* front_; |
| 863 | intptr_t* rear_; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 864 | }; |
| 865 | |
| 866 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 867 | // Shared state read by the scavenge collector and set by ScavengeObject. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 868 | static PromotionQueue promotion_queue; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 869 | |
| 870 | |
| 871 | #ifdef DEBUG |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 872 | // 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] | 873 | // new space. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 874 | class VerifyNonPointerSpacePointersVisitor: public ObjectVisitor { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 875 | public: |
| 876 | void VisitPointers(Object** start, Object**end) { |
| 877 | for (Object** current = start; current < end; current++) { |
| 878 | if ((*current)->IsHeapObject()) { |
| 879 | ASSERT(!Heap::InNewSpace(HeapObject::cast(*current))); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | }; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 884 | |
| 885 | |
| 886 | static void VerifyNonPointerSpacePointers() { |
| 887 | // Verify that there are no pointers to new space in spaces where we |
| 888 | // do not expect them. |
| 889 | VerifyNonPointerSpacePointersVisitor v; |
| 890 | HeapObjectIterator code_it(Heap::code_space()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 891 | for (HeapObject* object = code_it.next(); |
| 892 | object != NULL; object = code_it.next()) |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 893 | object->Iterate(&v); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 894 | |
| 895 | HeapObjectIterator data_it(Heap::old_data_space()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 896 | for (HeapObject* object = data_it.next(); |
| 897 | object != NULL; object = data_it.next()) |
| 898 | object->Iterate(&v); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 899 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 900 | #endif |
| 901 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 902 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 903 | void Heap::CheckNewSpaceExpansionCriteria() { |
| 904 | if (new_space_.Capacity() < new_space_.MaximumCapacity() && |
| 905 | survived_since_last_expansion_ > new_space_.Capacity()) { |
| 906 | // Grow the size of new space if there is room to grow and enough |
| 907 | // data has survived scavenge since the last expansion. |
| 908 | new_space_.Grow(); |
| 909 | survived_since_last_expansion_ = 0; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | void Heap::Scavenge() { |
| 915 | #ifdef DEBUG |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 916 | if (FLAG_enable_slow_asserts) VerifyNonPointerSpacePointers(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 917 | #endif |
| 918 | |
| 919 | gc_state_ = SCAVENGE; |
| 920 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 921 | Page::FlipMeaningOfInvalidatedWatermarkFlag(); |
| 922 | #ifdef DEBUG |
| 923 | VerifyPageWatermarkValidity(old_pointer_space_, ALL_VALID); |
| 924 | VerifyPageWatermarkValidity(map_space_, ALL_VALID); |
| 925 | #endif |
| 926 | |
| 927 | // We do not update an allocation watermark of the top page during linear |
| 928 | // allocation to avoid overhead. So to maintain the watermark invariant |
| 929 | // we have to manually cache the watermark and mark the top page as having an |
| 930 | // invalid watermark. This guarantees that dirty regions iteration will use a |
| 931 | // correct watermark even if a linear allocation happens. |
| 932 | old_pointer_space_->FlushTopPageWatermark(); |
| 933 | map_space_->FlushTopPageWatermark(); |
| 934 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 935 | // Implements Cheney's copying algorithm |
| 936 | LOG(ResourceEvent("scavenge", "begin")); |
| 937 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 938 | // Clear descriptor cache. |
| 939 | DescriptorLookupCache::Clear(); |
| 940 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 941 | // Used for updating survived_since_last_expansion_ at function end. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 942 | intptr_t survived_watermark = PromotedSpaceSize(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 943 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 944 | CheckNewSpaceExpansionCriteria(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 945 | |
| 946 | // Flip the semispaces. After flipping, to space is empty, from space has |
| 947 | // live objects. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 948 | new_space_.Flip(); |
| 949 | new_space_.ResetAllocationInfo(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 950 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 951 | // We need to sweep newly copied objects which can be either in the |
| 952 | // to space or promoted to the old generation. For to-space |
| 953 | // objects, we treat the bottom of the to space as a queue. Newly |
| 954 | // copied and unswept objects lie between a 'front' mark and the |
| 955 | // allocation pointer. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 956 | // |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 957 | // Promoted objects can go into various old-generation spaces, and |
| 958 | // can be allocated internally in the spaces (from the free list). |
| 959 | // We treat the top of the to space as a queue of addresses of |
| 960 | // promoted objects. The addresses of newly promoted and unswept |
| 961 | // objects lie between a 'front' mark and a 'rear' mark that is |
| 962 | // updated as a side effect of promoting an object. |
| 963 | // |
| 964 | // There is guaranteed to be enough room at the top of the to space |
| 965 | // for the addresses of promoted objects: every object promoted |
| 966 | // frees up its size in bytes from the top of the new space, and |
| 967 | // objects are at least one pointer in size. |
| 968 | Address new_space_front = new_space_.ToSpaceLow(); |
| 969 | promotion_queue.Initialize(new_space_.ToSpaceHigh()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 971 | ScavengeVisitor scavenge_visitor; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 972 | // Copy roots. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 973 | IterateRoots(&scavenge_visitor, VISIT_ALL_IN_SCAVENGE); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 974 | |
| 975 | // Copy objects reachable from the old generation. By definition, |
| 976 | // there are no intergenerational pointers in code or data spaces. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 977 | IterateDirtyRegions(old_pointer_space_, |
| 978 | &IteratePointersInDirtyRegion, |
| 979 | &ScavengePointer, |
| 980 | WATERMARK_CAN_BE_INVALID); |
| 981 | |
| 982 | IterateDirtyRegions(map_space_, |
| 983 | &IteratePointersInDirtyMapsRegion, |
| 984 | &ScavengePointer, |
| 985 | WATERMARK_CAN_BE_INVALID); |
| 986 | |
| 987 | lo_space_->IterateDirtyRegions(&ScavengePointer); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 988 | |
| 989 | // Copy objects reachable from cells by scavenging cell values directly. |
| 990 | HeapObjectIterator cell_iterator(cell_space_); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 991 | for (HeapObject* cell = cell_iterator.next(); |
| 992 | cell != NULL; cell = cell_iterator.next()) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 993 | if (cell->IsJSGlobalPropertyCell()) { |
| 994 | Address value_address = |
| 995 | reinterpret_cast<Address>(cell) + |
| 996 | (JSGlobalPropertyCell::kValueOffset - kHeapObjectTag); |
| 997 | scavenge_visitor.VisitPointer(reinterpret_cast<Object**>(value_address)); |
| 998 | } |
| 999 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1000 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1001 | // Scavenge object reachable from the global contexts list directly. |
| 1002 | scavenge_visitor.VisitPointer(BitCast<Object**>(&global_contexts_list_)); |
| 1003 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1004 | new_space_front = DoScavenge(&scavenge_visitor, new_space_front); |
| 1005 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1006 | UpdateNewSpaceReferencesInExternalStringTable( |
| 1007 | &UpdateNewSpaceReferenceInExternalStringTableEntry); |
| 1008 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1009 | ASSERT(new_space_front == new_space_.top()); |
| 1010 | |
| 1011 | // Set age mark. |
| 1012 | new_space_.set_age_mark(new_space_.top()); |
| 1013 | |
| 1014 | // Update how much has survived scavenge. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 1015 | IncrementYoungSurvivorsCounter(static_cast<int>( |
| 1016 | (PromotedSpaceSize() - survived_watermark) + new_space_.Size())); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1017 | |
| 1018 | LOG(ResourceEvent("scavenge", "end")); |
| 1019 | |
| 1020 | gc_state_ = NOT_IN_GC; |
| 1021 | } |
| 1022 | |
| 1023 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1024 | String* Heap::UpdateNewSpaceReferenceInExternalStringTableEntry(Object** p) { |
| 1025 | MapWord first_word = HeapObject::cast(*p)->map_word(); |
| 1026 | |
| 1027 | if (!first_word.IsForwardingAddress()) { |
| 1028 | // Unreachable external string can be finalized. |
| 1029 | FinalizeExternalString(String::cast(*p)); |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | // String is still reachable. |
| 1034 | return String::cast(first_word.ToForwardingAddress()); |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | void Heap::UpdateNewSpaceReferencesInExternalStringTable( |
| 1039 | ExternalStringTableUpdaterCallback updater_func) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1040 | ExternalStringTable::Verify(); |
| 1041 | |
| 1042 | if (ExternalStringTable::new_space_strings_.is_empty()) return; |
| 1043 | |
| 1044 | Object** start = &ExternalStringTable::new_space_strings_[0]; |
| 1045 | Object** end = start + ExternalStringTable::new_space_strings_.length(); |
| 1046 | Object** last = start; |
| 1047 | |
| 1048 | for (Object** p = start; p < end; ++p) { |
| 1049 | ASSERT(Heap::InFromSpace(*p)); |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1050 | String* target = updater_func(p); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1051 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 1052 | if (target == NULL) continue; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1053 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1054 | ASSERT(target->IsExternalString()); |
| 1055 | |
| 1056 | if (Heap::InNewSpace(target)) { |
| 1057 | // String is still in new space. Update the table entry. |
| 1058 | *last = target; |
| 1059 | ++last; |
| 1060 | } else { |
| 1061 | // String got promoted. Move it to the old string list. |
| 1062 | ExternalStringTable::AddOldString(target); |
| 1063 | } |
| 1064 | } |
| 1065 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1066 | ASSERT(last <= end); |
| 1067 | ExternalStringTable::ShrinkNewStrings(static_cast<int>(last - start)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1071 | void Heap::ProcessWeakReferences(WeakObjectRetainer* retainer) { |
| 1072 | Object* head = undefined_value(); |
| 1073 | Context* tail = NULL; |
| 1074 | Object* candidate = global_contexts_list_; |
| 1075 | while (!candidate->IsUndefined()) { |
| 1076 | // Check whether to keep the candidate in the list. |
| 1077 | Context* candidate_context = reinterpret_cast<Context*>(candidate); |
| 1078 | Object* retain = retainer->RetainAs(candidate); |
| 1079 | if (retain != NULL) { |
| 1080 | if (head->IsUndefined()) { |
| 1081 | // First element in the list. |
| 1082 | head = candidate_context; |
| 1083 | } else { |
| 1084 | // Subsequent elements in the list. |
| 1085 | ASSERT(tail != NULL); |
| 1086 | tail->set_unchecked(Context::NEXT_CONTEXT_LINK, |
| 1087 | candidate_context, |
| 1088 | UPDATE_WRITE_BARRIER); |
| 1089 | } |
| 1090 | // Retained context is new tail. |
| 1091 | tail = candidate_context; |
| 1092 | } |
| 1093 | // Move to next element in the list. |
| 1094 | candidate = candidate_context->get(Context::NEXT_CONTEXT_LINK); |
| 1095 | } |
| 1096 | |
| 1097 | // Terminate the list if there is one or more elements. |
| 1098 | if (tail != NULL) { |
| 1099 | tail->set_unchecked(Context::NEXT_CONTEXT_LINK, |
| 1100 | Heap::undefined_value(), |
| 1101 | UPDATE_WRITE_BARRIER); |
| 1102 | } |
| 1103 | |
| 1104 | // Update the head of the list of contexts. |
| 1105 | Heap::global_contexts_list_ = head; |
| 1106 | } |
| 1107 | |
| 1108 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1109 | class NewSpaceScavenger : public StaticNewSpaceVisitor<NewSpaceScavenger> { |
| 1110 | public: |
| 1111 | static inline void VisitPointer(Object** p) { |
| 1112 | Object* object = *p; |
| 1113 | if (!Heap::InNewSpace(object)) return; |
| 1114 | Heap::ScavengeObject(reinterpret_cast<HeapObject**>(p), |
| 1115 | reinterpret_cast<HeapObject*>(object)); |
| 1116 | } |
| 1117 | }; |
| 1118 | |
| 1119 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1120 | Address Heap::DoScavenge(ObjectVisitor* scavenge_visitor, |
| 1121 | Address new_space_front) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1122 | do { |
| 1123 | ASSERT(new_space_front <= new_space_.top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1124 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1125 | // The addresses new_space_front and new_space_.top() define a |
| 1126 | // queue of unprocessed copied objects. Process them until the |
| 1127 | // queue is empty. |
| 1128 | while (new_space_front < new_space_.top()) { |
| 1129 | HeapObject* object = HeapObject::FromAddress(new_space_front); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1130 | new_space_front += NewSpaceScavenger::IterateBody(object->map(), object); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1133 | // Promote and process all the to-be-promoted objects. |
| 1134 | while (!promotion_queue.is_empty()) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1135 | HeapObject* target; |
| 1136 | int size; |
| 1137 | promotion_queue.remove(&target, &size); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1138 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1139 | // Promoted object might be already partially visited |
| 1140 | // during dirty regions iteration. Thus we search specificly |
| 1141 | // for pointers to from semispace instead of looking for pointers |
| 1142 | // to new space. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 1143 | ASSERT(!target->IsMap()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1144 | IterateAndMarkPointersToFromSpace(target->address(), |
| 1145 | target->address() + size, |
| 1146 | &ScavengePointer); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | // Take another spin if there are now unswept objects in new space |
| 1150 | // (there are currently no more unswept promoted objects). |
| 1151 | } while (new_space_front < new_space_.top()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1152 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 1153 | return new_space_front; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1157 | class ScavengingVisitor : public StaticVisitorBase { |
| 1158 | public: |
| 1159 | static void Initialize() { |
| 1160 | table_.Register(kVisitSeqAsciiString, &EvacuateSeqAsciiString); |
| 1161 | table_.Register(kVisitSeqTwoByteString, &EvacuateSeqTwoByteString); |
| 1162 | table_.Register(kVisitShortcutCandidate, &EvacuateShortcutCandidate); |
| 1163 | table_.Register(kVisitByteArray, &EvacuateByteArray); |
| 1164 | table_.Register(kVisitFixedArray, &EvacuateFixedArray); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1165 | table_.Register(kVisitGlobalContext, |
| 1166 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1167 | VisitSpecialized<Context::kSize>); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1168 | |
| 1169 | typedef ObjectEvacuationStrategy<POINTER_OBJECT> PointerObject; |
| 1170 | |
| 1171 | table_.Register(kVisitConsString, |
| 1172 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1173 | VisitSpecialized<ConsString::kSize>); |
| 1174 | |
| 1175 | table_.Register(kVisitSharedFunctionInfo, |
| 1176 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1177 | VisitSpecialized<SharedFunctionInfo::kSize>); |
| 1178 | |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1179 | table_.Register(kVisitJSFunction, |
| 1180 | &ObjectEvacuationStrategy<POINTER_OBJECT>:: |
| 1181 | VisitSpecialized<JSFunction::kSize>); |
| 1182 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1183 | table_.RegisterSpecializations<ObjectEvacuationStrategy<DATA_OBJECT>, |
| 1184 | kVisitDataObject, |
| 1185 | kVisitDataObjectGeneric>(); |
| 1186 | |
| 1187 | table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>, |
| 1188 | kVisitJSObject, |
| 1189 | kVisitJSObjectGeneric>(); |
| 1190 | |
| 1191 | table_.RegisterSpecializations<ObjectEvacuationStrategy<POINTER_OBJECT>, |
| 1192 | kVisitStruct, |
| 1193 | kVisitStructGeneric>(); |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | static inline void Scavenge(Map* map, HeapObject** slot, HeapObject* obj) { |
| 1198 | table_.GetVisitor(map)(map, slot, obj); |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | private: |
| 1203 | enum ObjectContents { DATA_OBJECT, POINTER_OBJECT }; |
| 1204 | enum SizeRestriction { SMALL, UNKNOWN_SIZE }; |
| 1205 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1206 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1207 | static void RecordCopiedObject(HeapObject* obj) { |
| 1208 | bool should_record = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1209 | #ifdef DEBUG |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1210 | should_record = FLAG_heap_stats; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1211 | #endif |
| 1212 | #ifdef ENABLE_LOGGING_AND_PROFILING |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1213 | should_record = should_record || FLAG_log_gc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1214 | #endif |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1215 | if (should_record) { |
| 1216 | if (Heap::new_space()->Contains(obj)) { |
| 1217 | Heap::new_space()->RecordAllocation(obj); |
| 1218 | } else { |
| 1219 | Heap::new_space()->RecordPromotion(obj); |
| 1220 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | #endif // defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
| 1224 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1225 | // Helper function used by CopyObject to copy a source object to an |
| 1226 | // allocated target object and update the forwarding pointer in the source |
| 1227 | // object. Returns the target object. |
| 1228 | INLINE(static HeapObject* MigrateObject(HeapObject* source, |
| 1229 | HeapObject* target, |
| 1230 | int size)) { |
| 1231 | // Copy the content of source to target. |
| 1232 | Heap::CopyBlock(target->address(), source->address(), size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1233 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1234 | // Set the forwarding address. |
| 1235 | source->set_map_word(MapWord::FromForwardingAddress(target)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1236 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1237 | #if defined(DEBUG) || defined(ENABLE_LOGGING_AND_PROFILING) |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1238 | // Update NewSpace stats if necessary. |
| 1239 | RecordCopiedObject(target); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1240 | #endif |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1241 | HEAP_PROFILE(ObjectMoveEvent(source->address(), target->address())); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1242 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
| 1243 | if (Logger::is_logging() || CpuProfiler::is_profiling()) { |
| 1244 | if (target->IsJSFunction()) { |
| 1245 | PROFILE(FunctionMoveEvent(source->address(), target->address())); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1246 | PROFILE(FunctionCreateEventFromMove(JSFunction::cast(target))); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | #endif |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1250 | return target; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1251 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1252 | |
| 1253 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1254 | template<ObjectContents object_contents, SizeRestriction size_restriction> |
| 1255 | static inline void EvacuateObject(Map* map, |
| 1256 | HeapObject** slot, |
| 1257 | HeapObject* object, |
| 1258 | int object_size) { |
| 1259 | ASSERT((size_restriction != SMALL) || |
| 1260 | (object_size <= Page::kMaxHeapObjectSize)); |
| 1261 | ASSERT(object->Size() == object_size); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1262 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1263 | if (Heap::ShouldBePromoted(object->address(), object_size)) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1264 | MaybeObject* maybe_result; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1265 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1266 | if ((size_restriction != SMALL) && |
| 1267 | (object_size > Page::kMaxHeapObjectSize)) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1268 | maybe_result = Heap::lo_space()->AllocateRawFixedArray(object_size); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1269 | } else { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1270 | if (object_contents == DATA_OBJECT) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1271 | maybe_result = Heap::old_data_space()->AllocateRaw(object_size); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1272 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1273 | maybe_result = Heap::old_pointer_space()->AllocateRaw(object_size); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1274 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1277 | Object* result = NULL; // Initialization to please compiler. |
| 1278 | if (maybe_result->ToObject(&result)) { |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1279 | HeapObject* target = HeapObject::cast(result); |
| 1280 | *slot = MigrateObject(object, target, object_size); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1281 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1282 | if (object_contents == POINTER_OBJECT) { |
| 1283 | promotion_queue.insert(target, object_size); |
| 1284 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1285 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1286 | Heap::tracer()->increment_promoted_objects_size(object_size); |
| 1287 | return; |
| 1288 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1289 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1290 | Object* result = |
| 1291 | Heap::new_space()->AllocateRaw(object_size)->ToObjectUnchecked(); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1292 | *slot = MigrateObject(object, HeapObject::cast(result), object_size); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1293 | return; |
| 1294 | } |
| 1295 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1296 | |
| 1297 | static inline void EvacuateFixedArray(Map* map, |
| 1298 | HeapObject** slot, |
| 1299 | HeapObject* object) { |
| 1300 | int object_size = FixedArray::BodyDescriptor::SizeOf(map, object); |
| 1301 | EvacuateObject<POINTER_OBJECT, UNKNOWN_SIZE>(map, |
| 1302 | slot, |
| 1303 | object, |
| 1304 | object_size); |
| 1305 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1306 | |
| 1307 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1308 | static inline void EvacuateByteArray(Map* map, |
| 1309 | HeapObject** slot, |
| 1310 | HeapObject* object) { |
| 1311 | int object_size = reinterpret_cast<ByteArray*>(object)->ByteArraySize(); |
| 1312 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1313 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1314 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1315 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1316 | static inline void EvacuateSeqAsciiString(Map* map, |
| 1317 | HeapObject** slot, |
| 1318 | HeapObject* object) { |
| 1319 | int object_size = SeqAsciiString::cast(object)-> |
| 1320 | SeqAsciiStringSize(map->instance_type()); |
| 1321 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | static inline void EvacuateSeqTwoByteString(Map* map, |
| 1326 | HeapObject** slot, |
| 1327 | HeapObject* object) { |
| 1328 | int object_size = SeqTwoByteString::cast(object)-> |
| 1329 | SeqTwoByteStringSize(map->instance_type()); |
| 1330 | EvacuateObject<DATA_OBJECT, UNKNOWN_SIZE>(map, slot, object, object_size); |
| 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | static inline bool IsShortcutCandidate(int type) { |
| 1335 | return ((type & kShortcutTypeMask) == kShortcutTypeTag); |
| 1336 | } |
| 1337 | |
| 1338 | static inline void EvacuateShortcutCandidate(Map* map, |
| 1339 | HeapObject** slot, |
| 1340 | HeapObject* object) { |
| 1341 | ASSERT(IsShortcutCandidate(map->instance_type())); |
| 1342 | |
| 1343 | if (ConsString::cast(object)->unchecked_second() == Heap::empty_string()) { |
| 1344 | HeapObject* first = |
| 1345 | HeapObject::cast(ConsString::cast(object)->unchecked_first()); |
| 1346 | |
| 1347 | *slot = first; |
| 1348 | |
| 1349 | if (!Heap::InNewSpace(first)) { |
| 1350 | object->set_map_word(MapWord::FromForwardingAddress(first)); |
| 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | MapWord first_word = first->map_word(); |
| 1355 | if (first_word.IsForwardingAddress()) { |
| 1356 | HeapObject* target = first_word.ToForwardingAddress(); |
| 1357 | |
| 1358 | *slot = target; |
| 1359 | object->set_map_word(MapWord::FromForwardingAddress(target)); |
| 1360 | return; |
| 1361 | } |
| 1362 | |
| 1363 | Scavenge(first->map(), slot, first); |
| 1364 | object->set_map_word(MapWord::FromForwardingAddress(*slot)); |
| 1365 | return; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1366 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1367 | |
| 1368 | int object_size = ConsString::kSize; |
| 1369 | EvacuateObject<POINTER_OBJECT, SMALL>(map, slot, object, object_size); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1372 | template<ObjectContents object_contents> |
| 1373 | class ObjectEvacuationStrategy { |
| 1374 | public: |
| 1375 | template<int object_size> |
| 1376 | static inline void VisitSpecialized(Map* map, |
| 1377 | HeapObject** slot, |
| 1378 | HeapObject* object) { |
| 1379 | EvacuateObject<object_contents, SMALL>(map, slot, object, object_size); |
| 1380 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1381 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1382 | static inline void Visit(Map* map, |
| 1383 | HeapObject** slot, |
| 1384 | HeapObject* object) { |
| 1385 | int object_size = map->instance_size(); |
| 1386 | EvacuateObject<object_contents, SMALL>(map, slot, object, object_size); |
| 1387 | } |
| 1388 | }; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1389 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1390 | typedef void (*Callback)(Map* map, HeapObject** slot, HeapObject* object); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1391 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1392 | static VisitorDispatchTable<Callback> table_; |
| 1393 | }; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1394 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1395 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1396 | VisitorDispatchTable<ScavengingVisitor::Callback> ScavengingVisitor::table_; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1397 | |
| 1398 | |
| 1399 | void Heap::ScavengeObjectSlow(HeapObject** p, HeapObject* object) { |
| 1400 | ASSERT(InFromSpace(object)); |
| 1401 | MapWord first_word = object->map_word(); |
| 1402 | ASSERT(!first_word.IsForwardingAddress()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1403 | Map* map = first_word.ToMap(); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1404 | ScavengingVisitor::Scavenge(map, p, object); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | |
| 1408 | void Heap::ScavengePointer(HeapObject** p) { |
| 1409 | ScavengeObject(p, *p); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1413 | MaybeObject* Heap::AllocatePartialMap(InstanceType instance_type, |
| 1414 | int instance_size) { |
| 1415 | Object* result; |
| 1416 | { MaybeObject* maybe_result = AllocateRawMap(); |
| 1417 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1418 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1419 | |
| 1420 | // Map::cast cannot be used due to uninitialized map field. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1421 | reinterpret_cast<Map*>(result)->set_map(raw_unchecked_meta_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1422 | reinterpret_cast<Map*>(result)->set_instance_type(instance_type); |
| 1423 | reinterpret_cast<Map*>(result)->set_instance_size(instance_size); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1424 | reinterpret_cast<Map*>(result)-> |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1425 | set_visitor_id( |
| 1426 | StaticVisitorBase::GetVisitorId(instance_type, instance_size)); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1427 | reinterpret_cast<Map*>(result)->set_inobject_properties(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1428 | reinterpret_cast<Map*>(result)->set_pre_allocated_property_fields(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1429 | reinterpret_cast<Map*>(result)->set_unused_property_fields(0); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1430 | reinterpret_cast<Map*>(result)->set_bit_field(0); |
| 1431 | reinterpret_cast<Map*>(result)->set_bit_field2(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1432 | return result; |
| 1433 | } |
| 1434 | |
| 1435 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1436 | MaybeObject* Heap::AllocateMap(InstanceType instance_type, int instance_size) { |
| 1437 | Object* result; |
| 1438 | { MaybeObject* maybe_result = AllocateRawMap(); |
| 1439 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1440 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1441 | |
| 1442 | Map* map = reinterpret_cast<Map*>(result); |
| 1443 | map->set_map(meta_map()); |
| 1444 | map->set_instance_type(instance_type); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 1445 | map->set_visitor_id( |
| 1446 | StaticVisitorBase::GetVisitorId(instance_type, instance_size)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1447 | map->set_prototype(null_value()); |
| 1448 | map->set_constructor(null_value()); |
| 1449 | map->set_instance_size(instance_size); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1450 | map->set_inobject_properties(0); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 1451 | map->set_pre_allocated_property_fields(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1452 | map->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1453 | map->set_code_cache(empty_fixed_array()); |
| 1454 | map->set_unused_property_fields(0); |
| 1455 | map->set_bit_field(0); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 1456 | map->set_bit_field2((1 << Map::kIsExtensible) | (1 << Map::kHasFastElements)); |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 1457 | |
| 1458 | // If the map object is aligned fill the padding area with Smi 0 objects. |
| 1459 | if (Map::kPadStart < Map::kSize) { |
| 1460 | memset(reinterpret_cast<byte*>(map) + Map::kPadStart - kHeapObjectTag, |
| 1461 | 0, |
| 1462 | Map::kSize - Map::kPadStart); |
| 1463 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1464 | return map; |
| 1465 | } |
| 1466 | |
| 1467 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1468 | MaybeObject* Heap::AllocateCodeCache() { |
| 1469 | Object* result; |
| 1470 | { MaybeObject* maybe_result = AllocateStruct(CODE_CACHE_TYPE); |
| 1471 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1472 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1473 | CodeCache* code_cache = CodeCache::cast(result); |
| 1474 | code_cache->set_default_cache(empty_fixed_array()); |
| 1475 | code_cache->set_normal_type_cache(undefined_value()); |
| 1476 | return code_cache; |
| 1477 | } |
| 1478 | |
| 1479 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1480 | const Heap::StringTypeTable Heap::string_type_table[] = { |
| 1481 | #define STRING_TYPE_ELEMENT(type, size, name, camel_name) \ |
| 1482 | {type, size, k##camel_name##MapRootIndex}, |
| 1483 | STRING_TYPE_LIST(STRING_TYPE_ELEMENT) |
| 1484 | #undef STRING_TYPE_ELEMENT |
| 1485 | }; |
| 1486 | |
| 1487 | |
| 1488 | const Heap::ConstantSymbolTable Heap::constant_symbol_table[] = { |
| 1489 | #define CONSTANT_SYMBOL_ELEMENT(name, contents) \ |
| 1490 | {contents, k##name##RootIndex}, |
| 1491 | SYMBOL_LIST(CONSTANT_SYMBOL_ELEMENT) |
| 1492 | #undef CONSTANT_SYMBOL_ELEMENT |
| 1493 | }; |
| 1494 | |
| 1495 | |
| 1496 | const Heap::StructTable Heap::struct_table[] = { |
| 1497 | #define STRUCT_TABLE_ELEMENT(NAME, Name, name) \ |
| 1498 | { NAME##_TYPE, Name::kSize, k##Name##MapRootIndex }, |
| 1499 | STRUCT_LIST(STRUCT_TABLE_ELEMENT) |
| 1500 | #undef STRUCT_TABLE_ELEMENT |
| 1501 | }; |
| 1502 | |
| 1503 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1504 | bool Heap::CreateInitialMaps() { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1505 | Object* obj; |
| 1506 | { MaybeObject* maybe_obj = AllocatePartialMap(MAP_TYPE, Map::kSize); |
| 1507 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1508 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1509 | // Map::cast cannot be used due to uninitialized map field. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1510 | Map* new_meta_map = reinterpret_cast<Map*>(obj); |
| 1511 | set_meta_map(new_meta_map); |
| 1512 | new_meta_map->set_map(new_meta_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1513 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1514 | { MaybeObject* maybe_obj = |
| 1515 | AllocatePartialMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1516 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1517 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1518 | set_fixed_array_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1519 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1520 | { MaybeObject* maybe_obj = AllocatePartialMap(ODDBALL_TYPE, Oddball::kSize); |
| 1521 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1522 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1523 | set_oddball_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1524 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1525 | // Allocate the empty array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1526 | { MaybeObject* maybe_obj = AllocateEmptyFixedArray(); |
| 1527 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1528 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1529 | set_empty_fixed_array(FixedArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1530 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1531 | { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_DATA_SPACE); |
| 1532 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1533 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1534 | set_null_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1535 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1536 | // Allocate the empty descriptor array. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1537 | { MaybeObject* maybe_obj = AllocateEmptyFixedArray(); |
| 1538 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1539 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1540 | set_empty_descriptor_array(DescriptorArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1541 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1542 | // Fix the instance_descriptors for the existing maps. |
| 1543 | meta_map()->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1544 | meta_map()->set_code_cache(empty_fixed_array()); |
| 1545 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1546 | fixed_array_map()->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1547 | fixed_array_map()->set_code_cache(empty_fixed_array()); |
| 1548 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1549 | oddball_map()->set_instance_descriptors(empty_descriptor_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1550 | oddball_map()->set_code_cache(empty_fixed_array()); |
| 1551 | |
| 1552 | // Fix prototype object for existing maps. |
| 1553 | meta_map()->set_prototype(null_value()); |
| 1554 | meta_map()->set_constructor(null_value()); |
| 1555 | |
| 1556 | fixed_array_map()->set_prototype(null_value()); |
| 1557 | fixed_array_map()->set_constructor(null_value()); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1558 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1559 | oddball_map()->set_prototype(null_value()); |
| 1560 | oddball_map()->set_constructor(null_value()); |
| 1561 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1562 | { MaybeObject* maybe_obj = |
| 1563 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1564 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1565 | } |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1566 | set_fixed_cow_array_map(Map::cast(obj)); |
| 1567 | ASSERT(fixed_array_map() != fixed_cow_array_map()); |
| 1568 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1569 | { MaybeObject* maybe_obj = AllocateMap(HEAP_NUMBER_TYPE, HeapNumber::kSize); |
| 1570 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1571 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1572 | set_heap_number_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1573 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1574 | { MaybeObject* maybe_obj = AllocateMap(PROXY_TYPE, Proxy::kSize); |
| 1575 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1576 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1577 | set_proxy_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1578 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1579 | for (unsigned i = 0; i < ARRAY_SIZE(string_type_table); i++) { |
| 1580 | const StringTypeTable& entry = string_type_table[i]; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1581 | { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size); |
| 1582 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1583 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1584 | roots_[entry.index] = Map::cast(obj); |
| 1585 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1586 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1587 | { MaybeObject* maybe_obj = AllocateMap(STRING_TYPE, kVariableSizeSentinel); |
| 1588 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1589 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1590 | set_undetectable_string_map(Map::cast(obj)); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1591 | Map::cast(obj)->set_is_undetectable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1592 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1593 | { MaybeObject* maybe_obj = |
| 1594 | AllocateMap(ASCII_STRING_TYPE, kVariableSizeSentinel); |
| 1595 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1596 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 1597 | set_undetectable_ascii_string_map(Map::cast(obj)); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1598 | Map::cast(obj)->set_is_undetectable(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1599 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1600 | { MaybeObject* maybe_obj = |
| 1601 | AllocateMap(BYTE_ARRAY_TYPE, kVariableSizeSentinel); |
| 1602 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1603 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1604 | set_byte_array_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1605 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1606 | { MaybeObject* maybe_obj = |
| 1607 | AllocateMap(PIXEL_ARRAY_TYPE, PixelArray::kAlignedSize); |
| 1608 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1609 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1610 | set_pixel_array_map(Map::cast(obj)); |
| 1611 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1612 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_BYTE_ARRAY_TYPE, |
| 1613 | ExternalArray::kAlignedSize); |
| 1614 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1615 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1616 | set_external_byte_array_map(Map::cast(obj)); |
| 1617 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1618 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE, |
| 1619 | ExternalArray::kAlignedSize); |
| 1620 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1621 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1622 | set_external_unsigned_byte_array_map(Map::cast(obj)); |
| 1623 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1624 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_SHORT_ARRAY_TYPE, |
| 1625 | ExternalArray::kAlignedSize); |
| 1626 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1627 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1628 | set_external_short_array_map(Map::cast(obj)); |
| 1629 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1630 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE, |
| 1631 | ExternalArray::kAlignedSize); |
| 1632 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1633 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1634 | set_external_unsigned_short_array_map(Map::cast(obj)); |
| 1635 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1636 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_INT_ARRAY_TYPE, |
| 1637 | ExternalArray::kAlignedSize); |
| 1638 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1639 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1640 | set_external_int_array_map(Map::cast(obj)); |
| 1641 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1642 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE, |
| 1643 | ExternalArray::kAlignedSize); |
| 1644 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1645 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1646 | set_external_unsigned_int_array_map(Map::cast(obj)); |
| 1647 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1648 | { MaybeObject* maybe_obj = AllocateMap(EXTERNAL_FLOAT_ARRAY_TYPE, |
| 1649 | ExternalArray::kAlignedSize); |
| 1650 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1651 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 1652 | set_external_float_array_map(Map::cast(obj)); |
| 1653 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1654 | { MaybeObject* maybe_obj = AllocateMap(CODE_TYPE, kVariableSizeSentinel); |
| 1655 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1656 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1657 | set_code_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1658 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1659 | { MaybeObject* maybe_obj = AllocateMap(JS_GLOBAL_PROPERTY_CELL_TYPE, |
| 1660 | JSGlobalPropertyCell::kSize); |
| 1661 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1662 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1663 | set_global_property_cell_map(Map::cast(obj)); |
| 1664 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1665 | { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, kPointerSize); |
| 1666 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1667 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1668 | set_one_pointer_filler_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1669 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1670 | { MaybeObject* maybe_obj = AllocateMap(FILLER_TYPE, 2 * kPointerSize); |
| 1671 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1672 | } |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 1673 | set_two_pointer_filler_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1674 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1675 | for (unsigned i = 0; i < ARRAY_SIZE(struct_table); i++) { |
| 1676 | const StructTable& entry = struct_table[i]; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1677 | { MaybeObject* maybe_obj = AllocateMap(entry.type, entry.size); |
| 1678 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1679 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1680 | roots_[entry.index] = Map::cast(obj); |
| 1681 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1682 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1683 | { MaybeObject* maybe_obj = |
| 1684 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1685 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1686 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1687 | set_hash_table_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1688 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1689 | { MaybeObject* maybe_obj = |
| 1690 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1691 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1692 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1693 | set_context_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1694 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1695 | { MaybeObject* maybe_obj = |
| 1696 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1697 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1698 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1699 | set_catch_context_map(Map::cast(obj)); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 1700 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1701 | { MaybeObject* maybe_obj = |
| 1702 | AllocateMap(FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 1703 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1704 | } |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1705 | Map* global_context_map = Map::cast(obj); |
| 1706 | global_context_map->set_visitor_id(StaticVisitorBase::kVisitGlobalContext); |
| 1707 | set_global_context_map(global_context_map); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1708 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1709 | { MaybeObject* maybe_obj = AllocateMap(SHARED_FUNCTION_INFO_TYPE, |
| 1710 | SharedFunctionInfo::kAlignedSize); |
| 1711 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1712 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1713 | set_shared_function_info_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1714 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1715 | ASSERT(!Heap::InNewSpace(Heap::empty_fixed_array())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1716 | return true; |
| 1717 | } |
| 1718 | |
| 1719 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1720 | MaybeObject* Heap::AllocateHeapNumber(double value, PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1721 | // Statically ensure that it is safe to allocate heap numbers in paged |
| 1722 | // spaces. |
| 1723 | STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1724 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1725 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1726 | Object* result; |
| 1727 | { MaybeObject* maybe_result = |
| 1728 | AllocateRaw(HeapNumber::kSize, space, OLD_DATA_SPACE); |
| 1729 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1730 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1731 | |
| 1732 | HeapObject::cast(result)->set_map(heap_number_map()); |
| 1733 | HeapNumber::cast(result)->set_value(value); |
| 1734 | return result; |
| 1735 | } |
| 1736 | |
| 1737 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1738 | MaybeObject* Heap::AllocateHeapNumber(double value) { |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 1739 | // Use general version, if we're forced to always allocate. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 1740 | if (always_allocate()) return AllocateHeapNumber(value, TENURED); |
| 1741 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1742 | // This version of AllocateHeapNumber is optimized for |
| 1743 | // allocation in new space. |
| 1744 | STATIC_ASSERT(HeapNumber::kSize <= Page::kMaxHeapObjectSize); |
| 1745 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1746 | Object* result; |
| 1747 | { MaybeObject* maybe_result = new_space_.AllocateRaw(HeapNumber::kSize); |
| 1748 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1749 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1750 | HeapObject::cast(result)->set_map(heap_number_map()); |
| 1751 | HeapNumber::cast(result)->set_value(value); |
| 1752 | return result; |
| 1753 | } |
| 1754 | |
| 1755 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1756 | MaybeObject* Heap::AllocateJSGlobalPropertyCell(Object* value) { |
| 1757 | Object* result; |
| 1758 | { MaybeObject* maybe_result = AllocateRawCell(); |
| 1759 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1760 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1761 | HeapObject::cast(result)->set_map(global_property_cell_map()); |
| 1762 | JSGlobalPropertyCell::cast(result)->set_value(value); |
| 1763 | return result; |
| 1764 | } |
| 1765 | |
| 1766 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1767 | MaybeObject* Heap::CreateOddball(const char* to_string, |
| 1768 | Object* to_number) { |
| 1769 | Object* result; |
| 1770 | { MaybeObject* maybe_result = Allocate(oddball_map(), OLD_DATA_SPACE); |
| 1771 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 1772 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1773 | return Oddball::cast(result)->Initialize(to_string, to_number); |
| 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | bool Heap::CreateApiObjects() { |
| 1778 | Object* obj; |
| 1779 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1780 | { MaybeObject* maybe_obj = AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 1781 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1782 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1783 | set_neander_map(Map::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1784 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1785 | { MaybeObject* maybe_obj = Heap::AllocateJSObjectFromMap(neander_map()); |
| 1786 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1787 | } |
| 1788 | Object* elements; |
| 1789 | { MaybeObject* maybe_elements = AllocateFixedArray(2); |
| 1790 | if (!maybe_elements->ToObject(&elements)) return false; |
| 1791 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1792 | FixedArray::cast(elements)->set(0, Smi::FromInt(0)); |
| 1793 | JSObject::cast(obj)->set_elements(FixedArray::cast(elements)); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1794 | set_message_listeners(JSObject::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1795 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1796 | return true; |
| 1797 | } |
| 1798 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1799 | |
| 1800 | void Heap::CreateCEntryStub() { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 1801 | CEntryStub stub(1); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1802 | set_c_entry_code(*stub.GetCode()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1806 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1807 | void Heap::CreateRegExpCEntryStub() { |
| 1808 | RegExpCEntryStub stub; |
| 1809 | set_re_c_entry_code(*stub.GetCode()); |
| 1810 | } |
| 1811 | #endif |
| 1812 | |
| 1813 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1814 | void Heap::CreateJSEntryStub() { |
| 1815 | JSEntryStub stub; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1816 | set_js_entry_code(*stub.GetCode()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | |
| 1820 | void Heap::CreateJSConstructEntryStub() { |
| 1821 | JSConstructEntryStub stub; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1822 | set_js_construct_entry_code(*stub.GetCode()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1826 | void Heap::CreateFixedStubs() { |
| 1827 | // Here we create roots for fixed stubs. They are needed at GC |
| 1828 | // for cooking and uncooking (check out frames.cc). |
| 1829 | // The eliminates the need for doing dictionary lookup in the |
| 1830 | // stub cache for these stubs. |
| 1831 | HandleScope scope; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1832 | // gcc-4.4 has problem generating correct code of following snippet: |
| 1833 | // { CEntryStub stub; |
| 1834 | // c_entry_code_ = *stub.GetCode(); |
| 1835 | // } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1836 | // { DebuggerStatementStub stub; |
| 1837 | // debugger_statement_code_ = *stub.GetCode(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1838 | // } |
| 1839 | // To workaround the problem, make separate functions without inlining. |
| 1840 | Heap::CreateCEntryStub(); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 1841 | Heap::CreateJSEntryStub(); |
| 1842 | Heap::CreateJSConstructEntryStub(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1843 | #if V8_TARGET_ARCH_ARM && !V8_INTERPRETED_REGEXP |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1844 | Heap::CreateRegExpCEntryStub(); |
| 1845 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | |
| 1849 | bool Heap::CreateInitialObjects() { |
| 1850 | Object* obj; |
| 1851 | |
| 1852 | // The -0 value must be set before NumberFromDouble works. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1853 | { MaybeObject* maybe_obj = AllocateHeapNumber(-0.0, TENURED); |
| 1854 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1855 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1856 | set_minus_zero_value(obj); |
| 1857 | ASSERT(signbit(minus_zero_value()->Number()) != 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1858 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1859 | { MaybeObject* maybe_obj = AllocateHeapNumber(OS::nan_value(), TENURED); |
| 1860 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1861 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1862 | set_nan_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1863 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1864 | { MaybeObject* maybe_obj = Allocate(oddball_map(), OLD_DATA_SPACE); |
| 1865 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1866 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1867 | set_undefined_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1868 | ASSERT(!InNewSpace(undefined_value())); |
| 1869 | |
| 1870 | // Allocate initial symbol table. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1871 | { MaybeObject* maybe_obj = SymbolTable::Allocate(kInitialSymbolTableSize); |
| 1872 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1873 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1874 | // Don't use set_symbol_table() due to asserts. |
| 1875 | roots_[kSymbolTableRootIndex] = obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Assign the print strings for oddballs after creating symboltable. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1878 | Object* symbol; |
| 1879 | { MaybeObject* maybe_symbol = LookupAsciiSymbol("undefined"); |
| 1880 | if (!maybe_symbol->ToObject(&symbol)) return false; |
| 1881 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1882 | Oddball::cast(undefined_value())->set_to_string(String::cast(symbol)); |
| 1883 | Oddball::cast(undefined_value())->set_to_number(nan_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1884 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1885 | // Allocate the null_value |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1886 | { MaybeObject* maybe_obj = |
| 1887 | Oddball::cast(null_value())->Initialize("null", Smi::FromInt(0)); |
| 1888 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1889 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1890 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1891 | { MaybeObject* maybe_obj = CreateOddball("true", Smi::FromInt(1)); |
| 1892 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1893 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1894 | set_true_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1895 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1896 | { MaybeObject* maybe_obj = CreateOddball("false", Smi::FromInt(0)); |
| 1897 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1898 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1899 | set_false_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1900 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1901 | { MaybeObject* maybe_obj = CreateOddball("hole", Smi::FromInt(-1)); |
| 1902 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1903 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1904 | set_the_hole_value(obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1905 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1906 | { MaybeObject* maybe_obj = |
| 1907 | CreateOddball("no_interceptor_result_sentinel", Smi::FromInt(-2)); |
| 1908 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1909 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1910 | set_no_interceptor_result_sentinel(obj); |
| 1911 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1912 | { MaybeObject* maybe_obj = |
| 1913 | CreateOddball("termination_exception", Smi::FromInt(-3)); |
| 1914 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1915 | } |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 1916 | set_termination_exception(obj); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1917 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1918 | // Allocate the empty string. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1919 | { MaybeObject* maybe_obj = AllocateRawAsciiString(0, TENURED); |
| 1920 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1921 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1922 | set_empty_string(String::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1923 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1924 | for (unsigned i = 0; i < ARRAY_SIZE(constant_symbol_table); i++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1925 | { MaybeObject* maybe_obj = |
| 1926 | LookupAsciiSymbol(constant_symbol_table[i].contents); |
| 1927 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1928 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1929 | roots_[constant_symbol_table[i].index] = String::cast(obj); |
| 1930 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1931 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1932 | // Allocate the hidden symbol which is used to identify the hidden properties |
| 1933 | // in JSObjects. The hash code has a special value so that it will not match |
| 1934 | // the empty string when searching for the property. It cannot be part of the |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1935 | // loop above because it needs to be allocated manually with the special |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1936 | // hash code in place. The hash code for the hidden_symbol is zero to ensure |
| 1937 | // that it will always be at the first entry in property descriptors. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1938 | { MaybeObject* maybe_obj = |
| 1939 | AllocateSymbol(CStrVector(""), 0, String::kZeroHash); |
| 1940 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1941 | } |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 1942 | hidden_symbol_ = String::cast(obj); |
| 1943 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1944 | // Allocate the proxy for __proto__. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1945 | { MaybeObject* maybe_obj = |
| 1946 | AllocateProxy((Address) &Accessors::ObjectPrototype); |
| 1947 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1948 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1949 | set_prototype_accessors(Proxy::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1950 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1951 | // Allocate the code_stubs dictionary. The initial size is set to avoid |
| 1952 | // expanding the dictionary during bootstrapping. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1953 | { MaybeObject* maybe_obj = NumberDictionary::Allocate(128); |
| 1954 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1955 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1956 | set_code_stubs(NumberDictionary::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1957 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 1958 | // Allocate the non_monomorphic_cache used in stub-cache.cc. The initial size |
| 1959 | // is set to avoid expanding the dictionary during bootstrapping. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1960 | { MaybeObject* maybe_obj = NumberDictionary::Allocate(64); |
| 1961 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1962 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1963 | set_non_monomorphic_cache(NumberDictionary::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1964 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 1965 | set_instanceof_cache_function(Smi::FromInt(0)); |
| 1966 | set_instanceof_cache_map(Smi::FromInt(0)); |
| 1967 | set_instanceof_cache_answer(Smi::FromInt(0)); |
| 1968 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1969 | CreateFixedStubs(); |
| 1970 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 1971 | // Allocate the dictionary of intrinsic function names. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1972 | { MaybeObject* maybe_obj = StringDictionary::Allocate(Runtime::kNumFunctions); |
| 1973 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1974 | } |
| 1975 | { MaybeObject* maybe_obj = Runtime::InitializeIntrinsicFunctionNames(obj); |
| 1976 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1977 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 1978 | set_intrinsic_function_names(StringDictionary::cast(obj)); |
| 1979 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 1980 | if (InitializeNumberStringCache()->IsFailure()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1981 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 1982 | // Allocate cache for single character ASCII strings. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1983 | { MaybeObject* maybe_obj = |
| 1984 | AllocateFixedArray(String::kMaxAsciiCharCode + 1, TENURED); |
| 1985 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1986 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1987 | set_single_character_string_cache(FixedArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1988 | |
| 1989 | // Allocate cache for external strings pointing to native source code. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 1990 | { MaybeObject* maybe_obj = AllocateFixedArray(Natives::GetBuiltinsCount()); |
| 1991 | if (!maybe_obj->ToObject(&obj)) return false; |
| 1992 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1993 | set_natives_source_cache(FixedArray::cast(obj)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1994 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1995 | // Handling of script id generation is in Factory::NewScript. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1996 | set_last_script_id(undefined_value()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 1997 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 1998 | // Initialize keyed lookup cache. |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 1999 | KeyedLookupCache::Clear(); |
| 2000 | |
| 2001 | // Initialize context slot cache. |
| 2002 | ContextSlotCache::Clear(); |
| 2003 | |
| 2004 | // Initialize descriptor cache. |
| 2005 | DescriptorLookupCache::Clear(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2006 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 2007 | // Initialize compilation cache. |
| 2008 | CompilationCache::Clear(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2009 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2010 | return true; |
| 2011 | } |
| 2012 | |
| 2013 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2014 | MaybeObject* Heap::InitializeNumberStringCache() { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2015 | // Compute the size of the number string cache based on the max heap size. |
| 2016 | // max_semispace_size_ == 512 KB => number_string_cache_size = 32. |
| 2017 | // max_semispace_size_ == 8 MB => number_string_cache_size = 16KB. |
| 2018 | int number_string_cache_size = max_semispace_size_ / 512; |
| 2019 | number_string_cache_size = Max(32, Min(16*KB, number_string_cache_size)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2020 | Object* obj; |
| 2021 | MaybeObject* maybe_obj = |
| 2022 | AllocateFixedArray(number_string_cache_size * 2, TENURED); |
| 2023 | if (maybe_obj->ToObject(&obj)) set_number_string_cache(FixedArray::cast(obj)); |
| 2024 | return maybe_obj; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | |
| 2028 | void Heap::FlushNumberStringCache() { |
| 2029 | // Flush the number to string cache. |
| 2030 | int len = number_string_cache()->length(); |
| 2031 | for (int i = 0; i < len; i++) { |
| 2032 | number_string_cache()->set_undefined(i); |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2037 | static inline int double_get_hash(double d) { |
| 2038 | DoubleRepresentation rep(d); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2039 | return static_cast<int>(rep.bits) ^ static_cast<int>(rep.bits >> 32); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | |
| 2043 | static inline int smi_get_hash(Smi* smi) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2044 | return smi->value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2048 | Object* Heap::GetNumberStringCache(Object* number) { |
| 2049 | int hash; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2050 | int mask = (number_string_cache()->length() >> 1) - 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2051 | if (number->IsSmi()) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2052 | hash = smi_get_hash(Smi::cast(number)) & mask; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2053 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2054 | hash = double_get_hash(number->Number()) & mask; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2055 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2056 | Object* key = number_string_cache()->get(hash * 2); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2057 | if (key == number) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2058 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2059 | } else if (key->IsHeapNumber() && |
| 2060 | number->IsHeapNumber() && |
| 2061 | key->Number() == number->Number()) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2062 | return String::cast(number_string_cache()->get(hash * 2 + 1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2063 | } |
| 2064 | return undefined_value(); |
| 2065 | } |
| 2066 | |
| 2067 | |
| 2068 | void Heap::SetNumberStringCache(Object* number, String* string) { |
| 2069 | int hash; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2070 | int mask = (number_string_cache()->length() >> 1) - 1; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2071 | if (number->IsSmi()) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2072 | hash = smi_get_hash(Smi::cast(number)) & mask; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2073 | number_string_cache()->set(hash * 2, Smi::cast(number)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2074 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2075 | hash = double_get_hash(number->Number()) & mask; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2076 | number_string_cache()->set(hash * 2, number); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2077 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 2078 | number_string_cache()->set(hash * 2 + 1, string); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2082 | MaybeObject* Heap::NumberToString(Object* number, |
| 2083 | bool check_number_string_cache) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2084 | Counters::number_to_string_runtime.Increment(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 2085 | if (check_number_string_cache) { |
| 2086 | Object* cached = GetNumberStringCache(number); |
| 2087 | if (cached != undefined_value()) { |
| 2088 | return cached; |
| 2089 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | char arr[100]; |
| 2093 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 2094 | const char* str; |
| 2095 | if (number->IsSmi()) { |
| 2096 | int num = Smi::cast(number)->value(); |
| 2097 | str = IntToCString(num, buffer); |
| 2098 | } else { |
| 2099 | double num = HeapNumber::cast(number)->value(); |
| 2100 | str = DoubleToCString(num, buffer); |
| 2101 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2102 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2103 | Object* js_string; |
| 2104 | MaybeObject* maybe_js_string = AllocateStringFromAscii(CStrVector(str)); |
| 2105 | if (maybe_js_string->ToObject(&js_string)) { |
| 2106 | SetNumberStringCache(number, String::cast(js_string)); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2107 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2108 | return maybe_js_string; |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2112 | Map* Heap::MapForExternalArrayType(ExternalArrayType array_type) { |
| 2113 | return Map::cast(roots_[RootIndexForExternalArrayType(array_type)]); |
| 2114 | } |
| 2115 | |
| 2116 | |
| 2117 | Heap::RootListIndex Heap::RootIndexForExternalArrayType( |
| 2118 | ExternalArrayType array_type) { |
| 2119 | switch (array_type) { |
| 2120 | case kExternalByteArray: |
| 2121 | return kExternalByteArrayMapRootIndex; |
| 2122 | case kExternalUnsignedByteArray: |
| 2123 | return kExternalUnsignedByteArrayMapRootIndex; |
| 2124 | case kExternalShortArray: |
| 2125 | return kExternalShortArrayMapRootIndex; |
| 2126 | case kExternalUnsignedShortArray: |
| 2127 | return kExternalUnsignedShortArrayMapRootIndex; |
| 2128 | case kExternalIntArray: |
| 2129 | return kExternalIntArrayMapRootIndex; |
| 2130 | case kExternalUnsignedIntArray: |
| 2131 | return kExternalUnsignedIntArrayMapRootIndex; |
| 2132 | case kExternalFloatArray: |
| 2133 | return kExternalFloatArrayMapRootIndex; |
| 2134 | default: |
| 2135 | UNREACHABLE(); |
| 2136 | return kUndefinedValueRootIndex; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2141 | MaybeObject* Heap::NumberFromDouble(double value, PretenureFlag pretenure) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 2142 | // We need to distinguish the minus zero value and this cannot be |
| 2143 | // done after conversion to int. Doing this by comparing bit |
| 2144 | // patterns is faster than using fpclassify() et al. |
| 2145 | static const DoubleRepresentation minus_zero(-0.0); |
| 2146 | |
| 2147 | DoubleRepresentation rep(value); |
| 2148 | if (rep.bits == minus_zero.bits) { |
| 2149 | return AllocateHeapNumber(-0.0, pretenure); |
| 2150 | } |
| 2151 | |
| 2152 | int int_value = FastD2I(value); |
| 2153 | if (value == int_value && Smi::IsValid(int_value)) { |
| 2154 | return Smi::FromInt(int_value); |
| 2155 | } |
| 2156 | |
| 2157 | // Materialize the value in the heap. |
| 2158 | return AllocateHeapNumber(value, pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2162 | MaybeObject* Heap::AllocateProxy(Address proxy, PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2163 | // Statically ensure that it is safe to allocate proxies in paged spaces. |
| 2164 | STATIC_ASSERT(Proxy::kSize <= Page::kMaxHeapObjectSize); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2165 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2166 | Object* result; |
| 2167 | { MaybeObject* maybe_result = Allocate(proxy_map(), space); |
| 2168 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2169 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2170 | |
| 2171 | Proxy::cast(result)->set_proxy(proxy); |
| 2172 | return result; |
| 2173 | } |
| 2174 | |
| 2175 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2176 | MaybeObject* Heap::AllocateSharedFunctionInfo(Object* name) { |
| 2177 | Object* result; |
| 2178 | { MaybeObject* maybe_result = |
| 2179 | Allocate(shared_function_info_map(), OLD_POINTER_SPACE); |
| 2180 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2181 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2182 | |
| 2183 | SharedFunctionInfo* share = SharedFunctionInfo::cast(result); |
| 2184 | share->set_name(name); |
| 2185 | Code* illegal = Builtins::builtin(Builtins::Illegal); |
| 2186 | share->set_code(illegal); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 2187 | share->set_scope_info(SerializedScopeInfo::Empty()); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2188 | Code* construct_stub = Builtins::builtin(Builtins::JSConstructStubGeneric); |
| 2189 | share->set_construct_stub(construct_stub); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2190 | share->set_expected_nof_properties(0); |
| 2191 | share->set_length(0); |
| 2192 | share->set_formal_parameter_count(0); |
| 2193 | share->set_instance_class_name(Object_symbol()); |
| 2194 | share->set_function_data(undefined_value()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2195 | share->set_script(undefined_value()); |
| 2196 | share->set_start_position_and_type(0); |
| 2197 | share->set_debug_info(undefined_value()); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 2198 | share->set_inferred_name(empty_string()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2199 | share->set_compiler_hints(0); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2200 | share->set_initial_map(undefined_value()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2201 | share->set_this_property_assignments_count(0); |
| 2202 | share->set_this_property_assignments(undefined_value()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2203 | share->set_num_literals(0); |
| 2204 | share->set_end_position(0); |
| 2205 | share->set_function_token_position(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2206 | return result; |
| 2207 | } |
| 2208 | |
| 2209 | |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2210 | // Returns true for a character in a range. Both limits are inclusive. |
| 2211 | static inline bool Between(uint32_t character, uint32_t from, uint32_t to) { |
| 2212 | // This makes uses of the the unsigned wraparound. |
| 2213 | return character - from <= to - from; |
| 2214 | } |
| 2215 | |
| 2216 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2217 | MUST_USE_RESULT static inline MaybeObject* MakeOrFindTwoCharacterString( |
| 2218 | uint32_t c1, |
| 2219 | uint32_t c2) { |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2220 | String* symbol; |
| 2221 | // Numeric strings have a different hash algorithm not known by |
| 2222 | // LookupTwoCharsSymbolIfExists, so we skip this step for such strings. |
| 2223 | if ((!Between(c1, '0', '9') || !Between(c2, '0', '9')) && |
| 2224 | Heap::symbol_table()->LookupTwoCharsSymbolIfExists(c1, c2, &symbol)) { |
| 2225 | return symbol; |
| 2226 | // Now we know the length is 2, we might as well make use of that fact |
| 2227 | // when building the new string. |
| 2228 | } else if ((c1 | c2) <= String::kMaxAsciiCharCodeU) { // We can do this |
| 2229 | ASSERT(IsPowerOf2(String::kMaxAsciiCharCodeU + 1)); // because of this. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2230 | Object* result; |
| 2231 | { MaybeObject* maybe_result = Heap::AllocateRawAsciiString(2); |
| 2232 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2233 | } |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2234 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 2235 | dest[0] = c1; |
| 2236 | dest[1] = c2; |
| 2237 | return result; |
| 2238 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2239 | Object* result; |
| 2240 | { MaybeObject* maybe_result = Heap::AllocateRawTwoByteString(2); |
| 2241 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2242 | } |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2243 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
| 2244 | dest[0] = c1; |
| 2245 | dest[1] = c2; |
| 2246 | return result; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2251 | MaybeObject* Heap::AllocateConsString(String* first, String* second) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2252 | int first_length = first->length(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2253 | if (first_length == 0) { |
| 2254 | return second; |
| 2255 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2256 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2257 | int second_length = second->length(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2258 | if (second_length == 0) { |
| 2259 | return first; |
| 2260 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2261 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2262 | int length = first_length + second_length; |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2263 | |
| 2264 | // Optimization for 2-byte strings often used as keys in a decompression |
| 2265 | // dictionary. Check whether we already have the string in the symbol |
| 2266 | // table to prevent creation of many unneccesary strings. |
| 2267 | if (length == 2) { |
| 2268 | unsigned c1 = first->Get(0); |
| 2269 | unsigned c2 = second->Get(0); |
| 2270 | return MakeOrFindTwoCharacterString(c1, c2); |
| 2271 | } |
| 2272 | |
ricow@chromium.org | aa1b616 | 2010-03-29 07:44:58 +0000 | [diff] [blame] | 2273 | bool first_is_ascii = first->IsAsciiRepresentation(); |
| 2274 | bool second_is_ascii = second->IsAsciiRepresentation(); |
| 2275 | bool is_ascii = first_is_ascii && second_is_ascii; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2276 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2277 | // Make sure that an out of memory exception is thrown if the length |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2278 | // of the new cons string is too large. |
| 2279 | if (length > String::kMaxLength || length < 0) { |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2280 | Top::context()->mark_out_of_memory(); |
| 2281 | return Failure::OutOfMemoryException(); |
| 2282 | } |
| 2283 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2284 | bool is_ascii_data_in_two_byte_string = false; |
| 2285 | if (!is_ascii) { |
| 2286 | // At least one of the strings uses two-byte representation so we |
| 2287 | // can't use the fast case code for short ascii strings below, but |
| 2288 | // we can try to save memory if all chars actually fit in ascii. |
| 2289 | is_ascii_data_in_two_byte_string = |
| 2290 | first->HasOnlyAsciiChars() && second->HasOnlyAsciiChars(); |
| 2291 | if (is_ascii_data_in_two_byte_string) { |
| 2292 | Counters::string_add_runtime_ext_to_ascii.Increment(); |
| 2293 | } |
| 2294 | } |
| 2295 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2296 | // If the resulting string is small make a flat string. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2297 | if (length < String::kMinNonFlatLength) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2298 | ASSERT(first->IsFlat()); |
| 2299 | ASSERT(second->IsFlat()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2300 | if (is_ascii) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2301 | Object* result; |
| 2302 | { MaybeObject* maybe_result = AllocateRawAsciiString(length); |
| 2303 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2304 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2305 | // Copy the characters into the new object. |
| 2306 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2307 | // Copy first part. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2308 | const char* src; |
| 2309 | if (first->IsExternalString()) { |
| 2310 | src = ExternalAsciiString::cast(first)->resource()->data(); |
| 2311 | } else { |
| 2312 | src = SeqAsciiString::cast(first)->GetChars(); |
| 2313 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2314 | for (int i = 0; i < first_length; i++) *dest++ = src[i]; |
| 2315 | // Copy second part. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2316 | if (second->IsExternalString()) { |
| 2317 | src = ExternalAsciiString::cast(second)->resource()->data(); |
| 2318 | } else { |
| 2319 | src = SeqAsciiString::cast(second)->GetChars(); |
| 2320 | } |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 2321 | for (int i = 0; i < second_length; i++) *dest++ = src[i]; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2322 | return result; |
| 2323 | } else { |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2324 | if (is_ascii_data_in_two_byte_string) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2325 | Object* result; |
| 2326 | { MaybeObject* maybe_result = AllocateRawAsciiString(length); |
| 2327 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2328 | } |
ricow@chromium.org | aa1b616 | 2010-03-29 07:44:58 +0000 | [diff] [blame] | 2329 | // Copy the characters into the new object. |
| 2330 | char* dest = SeqAsciiString::cast(result)->GetChars(); |
| 2331 | String::WriteToFlat(first, dest, 0, first_length); |
| 2332 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
ricow@chromium.org | aa1b616 | 2010-03-29 07:44:58 +0000 | [diff] [blame] | 2333 | return result; |
| 2334 | } |
| 2335 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2336 | Object* result; |
| 2337 | { MaybeObject* maybe_result = AllocateRawTwoByteString(length); |
| 2338 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2339 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2340 | // Copy the characters into the new object. |
| 2341 | uc16* dest = SeqTwoByteString::cast(result)->GetChars(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2342 | String::WriteToFlat(first, dest, 0, first_length); |
| 2343 | String::WriteToFlat(second, dest + first_length, 0, second_length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2344 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2345 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2348 | Map* map = (is_ascii || is_ascii_data_in_two_byte_string) ? |
| 2349 | cons_ascii_string_map() : cons_string_map(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2350 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2351 | Object* result; |
| 2352 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 2353 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2354 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2355 | |
| 2356 | AssertNoAllocation no_gc; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2357 | ConsString* cons_string = ConsString::cast(result); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2358 | WriteBarrierMode mode = cons_string->GetWriteBarrierMode(no_gc); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2359 | cons_string->set_length(length); |
| 2360 | cons_string->set_hash_field(String::kEmptyHashField); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2361 | cons_string->set_first(first, mode); |
| 2362 | cons_string->set_second(second, mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2363 | return result; |
| 2364 | } |
| 2365 | |
| 2366 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2367 | MaybeObject* Heap::AllocateSubString(String* buffer, |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2368 | int start, |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2369 | int end, |
| 2370 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2371 | int length = end - start; |
| 2372 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2373 | if (length == 1) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2374 | return Heap::LookupSingleCharacterStringFromCode( |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2375 | buffer->Get(start)); |
ager@chromium.org | 6141cbe | 2009-11-20 12:14:52 +0000 | [diff] [blame] | 2376 | } else if (length == 2) { |
| 2377 | // Optimization for 2-byte strings often used as keys in a decompression |
| 2378 | // dictionary. Check whether we already have the string in the symbol |
| 2379 | // table to prevent creation of many unneccesary strings. |
| 2380 | unsigned c1 = buffer->Get(start); |
| 2381 | unsigned c2 = buffer->Get(start + 1); |
| 2382 | return MakeOrFindTwoCharacterString(c1, c2); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2385 | // Make an attempt to flatten the buffer to reduce access time. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 2386 | buffer = buffer->TryFlattenGetString(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2387 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2388 | Object* result; |
| 2389 | { MaybeObject* maybe_result = buffer->IsAsciiRepresentation() |
| 2390 | ? AllocateRawAsciiString(length, pretenure ) |
| 2391 | : AllocateRawTwoByteString(length, pretenure); |
| 2392 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2393 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2394 | String* string_result = String::cast(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2395 | // Copy the characters into the new object. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2396 | if (buffer->IsAsciiRepresentation()) { |
| 2397 | ASSERT(string_result->IsAsciiRepresentation()); |
| 2398 | char* dest = SeqAsciiString::cast(string_result)->GetChars(); |
| 2399 | String::WriteToFlat(buffer, dest, start, end); |
| 2400 | } else { |
| 2401 | ASSERT(string_result->IsTwoByteRepresentation()); |
| 2402 | uc16* dest = SeqTwoByteString::cast(string_result)->GetChars(); |
| 2403 | String::WriteToFlat(buffer, dest, start, end); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2404 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2405 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2406 | return result; |
| 2407 | } |
| 2408 | |
| 2409 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2410 | MaybeObject* Heap::AllocateExternalStringFromAscii( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2411 | ExternalAsciiString::Resource* resource) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2412 | size_t length = resource->length(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2413 | if (length > static_cast<size_t>(String::kMaxLength)) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2414 | Top::context()->mark_out_of_memory(); |
| 2415 | return Failure::OutOfMemoryException(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2418 | Map* map = external_ascii_string_map(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2419 | Object* result; |
| 2420 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 2421 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2422 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2423 | |
| 2424 | ExternalAsciiString* external_string = ExternalAsciiString::cast(result); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2425 | external_string->set_length(static_cast<int>(length)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2426 | external_string->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2427 | external_string->set_resource(resource); |
| 2428 | |
| 2429 | return result; |
| 2430 | } |
| 2431 | |
| 2432 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2433 | MaybeObject* Heap::AllocateExternalStringFromTwoByte( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2434 | ExternalTwoByteString::Resource* resource) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2435 | size_t length = resource->length(); |
| 2436 | if (length > static_cast<size_t>(String::kMaxLength)) { |
| 2437 | Top::context()->mark_out_of_memory(); |
| 2438 | return Failure::OutOfMemoryException(); |
| 2439 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2440 | |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 2441 | // For small strings we check whether the resource contains only |
| 2442 | // ascii characters. If yes, we use a different string map. |
| 2443 | bool is_ascii = true; |
| 2444 | if (length >= static_cast<size_t>(String::kMinNonFlatLength)) { |
| 2445 | is_ascii = false; |
| 2446 | } else { |
| 2447 | const uc16* data = resource->data(); |
| 2448 | for (size_t i = 0; i < length; i++) { |
| 2449 | if (data[i] > String::kMaxAsciiCharCode) { |
| 2450 | is_ascii = false; |
| 2451 | break; |
| 2452 | } |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | Map* map = is_ascii ? |
| 2457 | Heap::external_string_with_ascii_data_map() : Heap::external_string_map(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2458 | Object* result; |
| 2459 | { MaybeObject* maybe_result = Allocate(map, NEW_SPACE); |
| 2460 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2461 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2462 | |
| 2463 | ExternalTwoByteString* external_string = ExternalTwoByteString::cast(result); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 2464 | external_string->set_length(static_cast<int>(length)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 2465 | external_string->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2466 | external_string->set_resource(resource); |
| 2467 | |
| 2468 | return result; |
| 2469 | } |
| 2470 | |
| 2471 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2472 | MaybeObject* Heap::LookupSingleCharacterStringFromCode(uint16_t code) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2473 | if (code <= String::kMaxAsciiCharCode) { |
| 2474 | Object* value = Heap::single_character_string_cache()->get(code); |
| 2475 | if (value != Heap::undefined_value()) return value; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2476 | |
| 2477 | char buffer[1]; |
| 2478 | buffer[0] = static_cast<char>(code); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2479 | Object* result; |
| 2480 | MaybeObject* maybe_result = LookupSymbol(Vector<const char>(buffer, 1)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2481 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2482 | if (!maybe_result->ToObject(&result)) return maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2483 | Heap::single_character_string_cache()->set(code, result); |
| 2484 | return result; |
| 2485 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2486 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2487 | Object* result; |
| 2488 | { MaybeObject* maybe_result = Heap::AllocateRawTwoByteString(1); |
| 2489 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2490 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2491 | String* answer = String::cast(result); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 2492 | answer->Set(0, code); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2493 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2497 | MaybeObject* Heap::AllocateByteArray(int length, PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2498 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 2499 | return Failure::OutOfMemoryException(); |
| 2500 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2501 | if (pretenure == NOT_TENURED) { |
| 2502 | return AllocateByteArray(length); |
| 2503 | } |
| 2504 | int size = ByteArray::SizeFor(length); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2505 | Object* result; |
| 2506 | { MaybeObject* maybe_result = (size <= MaxObjectSizeInPagedSpace()) |
| 2507 | ? old_data_space_->AllocateRaw(size) |
| 2508 | : lo_space_->AllocateRaw(size); |
| 2509 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2510 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2511 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2512 | reinterpret_cast<ByteArray*>(result)->set_map(byte_array_map()); |
| 2513 | reinterpret_cast<ByteArray*>(result)->set_length(length); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2514 | return result; |
| 2515 | } |
| 2516 | |
| 2517 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2518 | MaybeObject* Heap::AllocateByteArray(int length) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2519 | if (length < 0 || length > ByteArray::kMaxLength) { |
| 2520 | return Failure::OutOfMemoryException(); |
| 2521 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2522 | int size = ByteArray::SizeFor(length); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2523 | AllocationSpace space = |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2524 | (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : NEW_SPACE; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2525 | Object* result; |
| 2526 | { MaybeObject* maybe_result = AllocateRaw(size, space, OLD_DATA_SPACE); |
| 2527 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2528 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2529 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2530 | reinterpret_cast<ByteArray*>(result)->set_map(byte_array_map()); |
| 2531 | reinterpret_cast<ByteArray*>(result)->set_length(length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2532 | return result; |
| 2533 | } |
| 2534 | |
| 2535 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 2536 | void Heap::CreateFillerObjectAt(Address addr, int size) { |
| 2537 | if (size == 0) return; |
| 2538 | HeapObject* filler = HeapObject::FromAddress(addr); |
| 2539 | if (size == kPointerSize) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2540 | filler->set_map(one_pointer_filler_map()); |
| 2541 | } else if (size == 2 * kPointerSize) { |
| 2542 | filler->set_map(two_pointer_filler_map()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 2543 | } else { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 2544 | filler->set_map(byte_array_map()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 2545 | ByteArray::cast(filler)->set_length(ByteArray::LengthFor(size)); |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2550 | MaybeObject* Heap::AllocatePixelArray(int length, |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2551 | uint8_t* external_pointer, |
| 2552 | PretenureFlag pretenure) { |
| 2553 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2554 | Object* result; |
| 2555 | { MaybeObject* maybe_result = |
| 2556 | AllocateRaw(PixelArray::kAlignedSize, space, OLD_DATA_SPACE); |
| 2557 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2558 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2559 | |
| 2560 | reinterpret_cast<PixelArray*>(result)->set_map(pixel_array_map()); |
| 2561 | reinterpret_cast<PixelArray*>(result)->set_length(length); |
| 2562 | reinterpret_cast<PixelArray*>(result)->set_external_pointer(external_pointer); |
| 2563 | |
| 2564 | return result; |
| 2565 | } |
| 2566 | |
| 2567 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2568 | MaybeObject* Heap::AllocateExternalArray(int length, |
| 2569 | ExternalArrayType array_type, |
| 2570 | void* external_pointer, |
| 2571 | PretenureFlag pretenure) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2572 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2573 | Object* result; |
| 2574 | { MaybeObject* maybe_result = AllocateRaw(ExternalArray::kAlignedSize, |
| 2575 | space, |
| 2576 | OLD_DATA_SPACE); |
| 2577 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2578 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2579 | |
| 2580 | reinterpret_cast<ExternalArray*>(result)->set_map( |
| 2581 | MapForExternalArrayType(array_type)); |
| 2582 | reinterpret_cast<ExternalArray*>(result)->set_length(length); |
| 2583 | reinterpret_cast<ExternalArray*>(result)->set_external_pointer( |
| 2584 | external_pointer); |
| 2585 | |
| 2586 | return result; |
| 2587 | } |
| 2588 | |
| 2589 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2590 | MaybeObject* Heap::CreateCode(const CodeDesc& desc, |
| 2591 | Code::Flags flags, |
| 2592 | Handle<Object> self_reference) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2593 | // Allocate ByteArray before the Code object, so that we do not risk |
| 2594 | // leaving uninitialized Code object (and breaking the heap). |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2595 | Object* reloc_info; |
| 2596 | { MaybeObject* maybe_reloc_info = AllocateByteArray(desc.reloc_size, TENURED); |
| 2597 | if (!maybe_reloc_info->ToObject(&reloc_info)) return maybe_reloc_info; |
| 2598 | } |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2599 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2600 | // Compute size |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2601 | int body_size = RoundUp(desc.instr_size, kObjectAlignment); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2602 | int obj_size = Code::SizeFor(body_size); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2603 | ASSERT(IsAligned(static_cast<intptr_t>(obj_size), kCodeAlignment)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2604 | MaybeObject* maybe_result; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2605 | if (obj_size > MaxObjectSizeInPagedSpace()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2606 | maybe_result = lo_space_->AllocateRawCode(obj_size); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2607 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2608 | maybe_result = code_space_->AllocateRaw(obj_size); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2609 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2610 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2611 | Object* result; |
| 2612 | if (!maybe_result->ToObject(&result)) return maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2613 | |
| 2614 | // Initialize the object |
| 2615 | HeapObject::cast(result)->set_map(code_map()); |
| 2616 | Code* code = Code::cast(result); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2617 | ASSERT(!CodeRange::exists() || CodeRange::contains(code->address())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2618 | code->set_instruction_size(desc.instr_size); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2619 | code->set_relocation_info(ByteArray::cast(reloc_info)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2620 | code->set_flags(flags); |
kasperl@chromium.org | 061ef74 | 2009-02-27 12:16:20 +0000 | [diff] [blame] | 2621 | // Allow self references to created code object by patching the handle to |
| 2622 | // point to the newly allocated Code object. |
| 2623 | if (!self_reference.is_null()) { |
| 2624 | *(self_reference.location()) = code; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 2625 | } |
| 2626 | // Migrate generated code. |
| 2627 | // The generated code can contain Object** values (typically from handles) |
| 2628 | // that are dereferenced during the copy to point directly to the actual heap |
| 2629 | // objects. These pointers can include references to the code object itself, |
| 2630 | // through the self_reference parameter. |
| 2631 | code->CopyFrom(desc); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2632 | |
| 2633 | #ifdef DEBUG |
| 2634 | code->Verify(); |
| 2635 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2636 | return code; |
| 2637 | } |
| 2638 | |
| 2639 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2640 | MaybeObject* Heap::CopyCode(Code* code) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2641 | // Allocate an object the same size as the code object. |
| 2642 | int obj_size = code->Size(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2643 | MaybeObject* maybe_result; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2644 | if (obj_size > MaxObjectSizeInPagedSpace()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2645 | maybe_result = lo_space_->AllocateRawCode(obj_size); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2646 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2647 | maybe_result = code_space_->AllocateRaw(obj_size); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2650 | Object* result; |
| 2651 | if (!maybe_result->ToObject(&result)) return maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2652 | |
| 2653 | // Copy code object. |
| 2654 | Address old_addr = code->address(); |
| 2655 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2656 | CopyBlock(new_addr, old_addr, obj_size); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2657 | // Relocate the copy. |
| 2658 | Code* new_code = Code::cast(result); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 2659 | ASSERT(!CodeRange::exists() || CodeRange::contains(code->address())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2660 | new_code->Relocate(new_addr - old_addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2661 | return new_code; |
| 2662 | } |
| 2663 | |
| 2664 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2665 | MaybeObject* Heap::CopyCode(Code* code, Vector<byte> reloc_info) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2666 | // Allocate ByteArray before the Code object, so that we do not risk |
| 2667 | // leaving uninitialized Code object (and breaking the heap). |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2668 | Object* reloc_info_array; |
| 2669 | { MaybeObject* maybe_reloc_info_array = |
| 2670 | AllocateByteArray(reloc_info.length(), TENURED); |
| 2671 | if (!maybe_reloc_info_array->ToObject(&reloc_info_array)) { |
| 2672 | return maybe_reloc_info_array; |
| 2673 | } |
| 2674 | } |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2675 | |
| 2676 | int new_body_size = RoundUp(code->instruction_size(), kObjectAlignment); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2677 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 2678 | int new_obj_size = Code::SizeFor(new_body_size); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2679 | |
| 2680 | Address old_addr = code->address(); |
| 2681 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 2682 | size_t relocation_offset = |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2683 | static_cast<size_t>(code->instruction_end() - old_addr); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2684 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2685 | MaybeObject* maybe_result; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2686 | if (new_obj_size > MaxObjectSizeInPagedSpace()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2687 | maybe_result = lo_space_->AllocateRawCode(new_obj_size); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2688 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2689 | maybe_result = code_space_->AllocateRaw(new_obj_size); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2692 | Object* result; |
| 2693 | if (!maybe_result->ToObject(&result)) return maybe_result; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2694 | |
| 2695 | // Copy code object. |
| 2696 | Address new_addr = reinterpret_cast<HeapObject*>(result)->address(); |
| 2697 | |
| 2698 | // Copy header and instructions. |
| 2699 | memcpy(new_addr, old_addr, relocation_offset); |
| 2700 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2701 | Code* new_code = Code::cast(result); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2702 | new_code->set_relocation_info(ByteArray::cast(reloc_info_array)); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2703 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 2704 | // Copy patched rinfo. |
| 2705 | memcpy(new_code->relocation_start(), reloc_info.start(), reloc_info.length()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2706 | |
| 2707 | // Relocate the copy. |
| 2708 | ASSERT(!CodeRange::exists() || CodeRange::contains(code->address())); |
| 2709 | new_code->Relocate(new_addr - old_addr); |
| 2710 | |
| 2711 | #ifdef DEBUG |
| 2712 | code->Verify(); |
| 2713 | #endif |
| 2714 | return new_code; |
| 2715 | } |
| 2716 | |
| 2717 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2718 | MaybeObject* Heap::Allocate(Map* map, AllocationSpace space) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2719 | ASSERT(gc_state_ == NOT_IN_GC); |
| 2720 | ASSERT(map->instance_type() != MAP_TYPE); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 2721 | // If allocation failures are disallowed, we may allocate in a different |
| 2722 | // space when new space is full and the object is not a large object. |
| 2723 | AllocationSpace retry_space = |
| 2724 | (space != NEW_SPACE) ? space : TargetSpaceId(map->instance_type()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2725 | Object* result; |
| 2726 | { MaybeObject* maybe_result = |
| 2727 | AllocateRaw(map->instance_size(), space, retry_space); |
| 2728 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2729 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2730 | HeapObject::cast(result)->set_map(map); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2731 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 2732 | ProducerHeapProfile::RecordJSObjectAllocation(result); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 2733 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2734 | return result; |
| 2735 | } |
| 2736 | |
| 2737 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2738 | MaybeObject* Heap::InitializeFunction(JSFunction* function, |
| 2739 | SharedFunctionInfo* shared, |
| 2740 | Object* prototype) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2741 | ASSERT(!prototype->IsMap()); |
| 2742 | function->initialize_properties(); |
| 2743 | function->initialize_elements(); |
| 2744 | function->set_shared(shared); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2745 | function->set_code(shared->code()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2746 | function->set_prototype_or_initial_map(prototype); |
| 2747 | function->set_context(undefined_value()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2748 | function->set_literals(empty_fixed_array()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2749 | return function; |
| 2750 | } |
| 2751 | |
| 2752 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2753 | MaybeObject* Heap::AllocateFunctionPrototype(JSFunction* function) { |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 2754 | // Allocate the prototype. Make sure to use the object function |
| 2755 | // from the function's context, since the function can be from a |
| 2756 | // different context. |
| 2757 | JSFunction* object_function = |
| 2758 | function->context()->global_context()->object_function(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2759 | Object* prototype; |
| 2760 | { MaybeObject* maybe_prototype = AllocateJSObject(object_function); |
| 2761 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 2762 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2763 | // When creating the prototype for the function we must set its |
| 2764 | // constructor to the function. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2765 | Object* result; |
| 2766 | { MaybeObject* maybe_result = |
| 2767 | JSObject::cast(prototype)->SetProperty(constructor_symbol(), |
| 2768 | function, |
| 2769 | DONT_ENUM); |
| 2770 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2771 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2772 | return prototype; |
| 2773 | } |
| 2774 | |
| 2775 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2776 | MaybeObject* Heap::AllocateFunction(Map* function_map, |
| 2777 | SharedFunctionInfo* shared, |
| 2778 | Object* prototype, |
| 2779 | PretenureFlag pretenure) { |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 2780 | AllocationSpace space = |
| 2781 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2782 | Object* result; |
| 2783 | { MaybeObject* maybe_result = Allocate(function_map, space); |
| 2784 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2785 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2786 | return InitializeFunction(JSFunction::cast(result), shared, prototype); |
| 2787 | } |
| 2788 | |
| 2789 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2790 | MaybeObject* Heap::AllocateArgumentsObject(Object* callee, int length) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2791 | // To get fast allocation and map sharing for arguments objects we |
| 2792 | // allocate them based on an arguments boilerplate. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2793 | |
| 2794 | // This calls Copy directly rather than using Heap::AllocateRaw so we |
| 2795 | // duplicate the check here. |
| 2796 | ASSERT(allocation_allowed_ && gc_state_ == NOT_IN_GC); |
| 2797 | |
| 2798 | JSObject* boilerplate = |
| 2799 | Top::context()->global_context()->arguments_boilerplate(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2800 | |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 2801 | // Check that the size of the boilerplate matches our |
| 2802 | // expectations. The ArgumentsAccessStub::GenerateNewObject relies |
| 2803 | // on the size being a known constant. |
| 2804 | ASSERT(kArgumentsObjectSize == boilerplate->map()->instance_size()); |
| 2805 | |
| 2806 | // Do the allocation. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2807 | Object* result; |
| 2808 | { MaybeObject* maybe_result = |
| 2809 | AllocateRaw(kArgumentsObjectSize, NEW_SPACE, OLD_POINTER_SPACE); |
| 2810 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 2811 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2812 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2813 | // Copy the content. The arguments boilerplate doesn't have any |
| 2814 | // fields that point to new space so it's safe to skip the write |
| 2815 | // barrier here. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 2816 | CopyBlock(HeapObject::cast(result)->address(), |
| 2817 | boilerplate->address(), |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 2818 | kArgumentsObjectSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2819 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2820 | // Set the two properties. |
| 2821 | JSObject::cast(result)->InObjectPropertyAtPut(arguments_callee_index, |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 2822 | callee); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2823 | JSObject::cast(result)->InObjectPropertyAtPut(arguments_length_index, |
| 2824 | Smi::FromInt(length), |
| 2825 | SKIP_WRITE_BARRIER); |
| 2826 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2827 | // Check the state of the object |
| 2828 | ASSERT(JSObject::cast(result)->HasFastProperties()); |
| 2829 | ASSERT(JSObject::cast(result)->HasFastElements()); |
| 2830 | |
| 2831 | return result; |
| 2832 | } |
| 2833 | |
| 2834 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 2835 | static bool HasDuplicates(DescriptorArray* descriptors) { |
| 2836 | int count = descriptors->number_of_descriptors(); |
| 2837 | if (count > 1) { |
| 2838 | String* prev_key = descriptors->GetKey(0); |
| 2839 | for (int i = 1; i != count; i++) { |
| 2840 | String* current_key = descriptors->GetKey(i); |
| 2841 | if (prev_key == current_key) return true; |
| 2842 | prev_key = current_key; |
| 2843 | } |
| 2844 | } |
| 2845 | return false; |
| 2846 | } |
| 2847 | |
| 2848 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2849 | MaybeObject* Heap::AllocateInitialMap(JSFunction* fun) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2850 | ASSERT(!fun->has_initial_map()); |
| 2851 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2852 | // First create a new map with the size and number of in-object properties |
| 2853 | // suggested by the function. |
| 2854 | int instance_size = fun->shared()->CalculateInstanceSize(); |
| 2855 | int in_object_properties = fun->shared()->CalculateInObjectProperties(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2856 | Object* map_obj; |
| 2857 | { MaybeObject* maybe_map_obj = |
| 2858 | Heap::AllocateMap(JS_OBJECT_TYPE, instance_size); |
| 2859 | if (!maybe_map_obj->ToObject(&map_obj)) return maybe_map_obj; |
| 2860 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2861 | |
| 2862 | // Fetch or allocate prototype. |
| 2863 | Object* prototype; |
| 2864 | if (fun->has_instance_prototype()) { |
| 2865 | prototype = fun->instance_prototype(); |
| 2866 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2867 | { MaybeObject* maybe_prototype = AllocateFunctionPrototype(fun); |
| 2868 | if (!maybe_prototype->ToObject(&prototype)) return maybe_prototype; |
| 2869 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2870 | } |
| 2871 | Map* map = Map::cast(map_obj); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2872 | map->set_inobject_properties(in_object_properties); |
| 2873 | map->set_unused_property_fields(in_object_properties); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2874 | map->set_prototype(prototype); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2875 | ASSERT(map->has_fast_elements()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2876 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2877 | // If the function has only simple this property assignments add |
| 2878 | // field descriptors for these to the initial map as the object |
| 2879 | // cannot be constructed without having these properties. Guard by |
| 2880 | // the inline_new flag so we only change the map if we generate a |
| 2881 | // specialized construct stub. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2882 | ASSERT(in_object_properties <= Map::kMaxPreAllocatedPropertyFields); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 2883 | if (fun->shared()->CanGenerateInlineConstructor(prototype)) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2884 | int count = fun->shared()->this_property_assignments_count(); |
| 2885 | if (count > in_object_properties) { |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 2886 | // Inline constructor can only handle inobject properties. |
| 2887 | fun->shared()->ForbidInlineConstructor(); |
| 2888 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2889 | Object* descriptors_obj; |
| 2890 | { MaybeObject* maybe_descriptors_obj = DescriptorArray::Allocate(count); |
| 2891 | if (!maybe_descriptors_obj->ToObject(&descriptors_obj)) { |
| 2892 | return maybe_descriptors_obj; |
| 2893 | } |
| 2894 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 2895 | DescriptorArray* descriptors = DescriptorArray::cast(descriptors_obj); |
| 2896 | for (int i = 0; i < count; i++) { |
| 2897 | String* name = fun->shared()->GetThisPropertyAssignmentName(i); |
| 2898 | ASSERT(name->IsSymbol()); |
| 2899 | FieldDescriptor field(name, i, NONE); |
| 2900 | field.SetEnumerationIndex(i); |
| 2901 | descriptors->Set(i, &field); |
| 2902 | } |
| 2903 | descriptors->SetNextEnumerationIndex(count); |
| 2904 | descriptors->SortUnchecked(); |
| 2905 | |
| 2906 | // The descriptors may contain duplicates because the compiler does not |
| 2907 | // guarantee the uniqueness of property names (it would have required |
| 2908 | // quadratic time). Once the descriptors are sorted we can check for |
| 2909 | // duplicates in linear time. |
| 2910 | if (HasDuplicates(descriptors)) { |
| 2911 | fun->shared()->ForbidInlineConstructor(); |
| 2912 | } else { |
| 2913 | map->set_instance_descriptors(descriptors); |
| 2914 | map->set_pre_allocated_property_fields(count); |
| 2915 | map->set_unused_property_fields(in_object_properties - count); |
| 2916 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2917 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2918 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2919 | |
| 2920 | fun->shared()->StartInobjectSlackTracking(map); |
| 2921 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2922 | return map; |
| 2923 | } |
| 2924 | |
| 2925 | |
| 2926 | void Heap::InitializeJSObjectFromMap(JSObject* obj, |
| 2927 | FixedArray* properties, |
| 2928 | Map* map) { |
| 2929 | obj->set_properties(properties); |
| 2930 | obj->initialize_elements(); |
| 2931 | // TODO(1240798): Initialize the object's body using valid initial values |
| 2932 | // according to the object's initial map. For example, if the map's |
| 2933 | // instance type is JS_ARRAY_TYPE, the length field should be initialized |
| 2934 | // to a number (eg, Smi::FromInt(0)) and the elements initialized to a |
| 2935 | // fixed array (eg, Heap::empty_fixed_array()). Currently, the object |
| 2936 | // verification code has to cope with (temporarily) invalid objects. See |
| 2937 | // for example, JSArray::JSArrayVerify). |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 2938 | Object* filler; |
| 2939 | // We cannot always fill with one_pointer_filler_map because objects |
| 2940 | // created from API functions expect their internal fields to be initialized |
| 2941 | // with undefined_value. |
| 2942 | if (map->constructor()->IsJSFunction() && |
| 2943 | JSFunction::cast(map->constructor())->shared()-> |
| 2944 | IsInobjectSlackTrackingInProgress()) { |
| 2945 | // We might want to shrink the object later. |
| 2946 | ASSERT(obj->GetInternalFieldCount() == 0); |
| 2947 | filler = Heap::one_pointer_filler_map(); |
| 2948 | } else { |
| 2949 | filler = Heap::undefined_value(); |
| 2950 | } |
| 2951 | obj->InitializeBody(map->instance_size(), filler); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2955 | MaybeObject* Heap::AllocateJSObjectFromMap(Map* map, PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2956 | // JSFunctions should be allocated using AllocateFunction to be |
| 2957 | // properly initialized. |
| 2958 | ASSERT(map->instance_type() != JS_FUNCTION_TYPE); |
| 2959 | |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2960 | // Both types of global objects should be allocated using |
| 2961 | // AllocateGlobalObject to be properly initialized. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 2962 | ASSERT(map->instance_type() != JS_GLOBAL_OBJECT_TYPE); |
| 2963 | ASSERT(map->instance_type() != JS_BUILTINS_OBJECT_TYPE); |
| 2964 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2965 | // Allocate the backing storage for the properties. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 2966 | int prop_size = |
| 2967 | map->pre_allocated_property_fields() + |
| 2968 | map->unused_property_fields() - |
| 2969 | map->inobject_properties(); |
| 2970 | ASSERT(prop_size >= 0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2971 | Object* properties; |
| 2972 | { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, pretenure); |
| 2973 | if (!maybe_properties->ToObject(&properties)) return maybe_properties; |
| 2974 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2975 | |
| 2976 | // Allocate the JSObject. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 2977 | AllocationSpace space = |
| 2978 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 2979 | if (map->instance_size() > MaxObjectSizeInPagedSpace()) space = LO_SPACE; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2980 | Object* obj; |
| 2981 | { MaybeObject* maybe_obj = Allocate(map, space); |
| 2982 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 2983 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2984 | |
| 2985 | // Initialize the JSObject. |
| 2986 | InitializeJSObjectFromMap(JSObject::cast(obj), |
| 2987 | FixedArray::cast(properties), |
| 2988 | map); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 2989 | ASSERT(JSObject::cast(obj)->HasFastElements()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2990 | return obj; |
| 2991 | } |
| 2992 | |
| 2993 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2994 | MaybeObject* Heap::AllocateJSObject(JSFunction* constructor, |
| 2995 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2996 | // Allocate the initial map if absent. |
| 2997 | if (!constructor->has_initial_map()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 2998 | Object* initial_map; |
| 2999 | { MaybeObject* maybe_initial_map = AllocateInitialMap(constructor); |
| 3000 | if (!maybe_initial_map->ToObject(&initial_map)) return maybe_initial_map; |
| 3001 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3002 | constructor->set_initial_map(Map::cast(initial_map)); |
| 3003 | Map::cast(initial_map)->set_constructor(constructor); |
| 3004 | } |
| 3005 | // Allocate the object based on the constructors initial map. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3006 | MaybeObject* result = |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3007 | AllocateJSObjectFromMap(constructor->initial_map(), pretenure); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3008 | #ifdef DEBUG |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3009 | // Make sure result is NOT a global object if valid. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3010 | Object* non_failure; |
| 3011 | ASSERT(!result->ToObject(&non_failure) || !non_failure->IsGlobalObject()); |
| 3012 | #endif |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3013 | return result; |
| 3014 | } |
| 3015 | |
| 3016 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3017 | MaybeObject* Heap::AllocateGlobalObject(JSFunction* constructor) { |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3018 | ASSERT(constructor->has_initial_map()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3019 | Map* map = constructor->initial_map(); |
| 3020 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3021 | // Make sure no field properties are described in the initial map. |
| 3022 | // This guarantees us that normalizing the properties does not |
| 3023 | // require us to change property values to JSGlobalPropertyCells. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3024 | ASSERT(map->NextFreePropertyIndex() == 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3025 | |
| 3026 | // Make sure we don't have a ton of pre-allocated slots in the |
| 3027 | // global objects. They will be unused once we normalize the object. |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3028 | ASSERT(map->unused_property_fields() == 0); |
| 3029 | ASSERT(map->inobject_properties() == 0); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3030 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3031 | // Initial size of the backing store to avoid resize of the storage during |
| 3032 | // bootstrapping. The size differs between the JS global object ad the |
| 3033 | // builtins object. |
| 3034 | int initial_size = map->instance_type() == JS_GLOBAL_OBJECT_TYPE ? 64 : 512; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3035 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3036 | // Allocate a dictionary object for backing storage. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3037 | Object* obj; |
| 3038 | { MaybeObject* maybe_obj = |
| 3039 | StringDictionary::Allocate( |
| 3040 | map->NumberOfDescribedProperties() * 2 + initial_size); |
| 3041 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3042 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3043 | StringDictionary* dictionary = StringDictionary::cast(obj); |
| 3044 | |
| 3045 | // The global object might be created from an object template with accessors. |
| 3046 | // Fill these accessors into the dictionary. |
| 3047 | DescriptorArray* descs = map->instance_descriptors(); |
| 3048 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 3049 | PropertyDetails details = descs->GetDetails(i); |
| 3050 | ASSERT(details.type() == CALLBACKS); // Only accessors are expected. |
| 3051 | PropertyDetails d = |
| 3052 | PropertyDetails(details.attributes(), CALLBACKS, details.index()); |
| 3053 | Object* value = descs->GetCallbacksObject(i); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3054 | { MaybeObject* maybe_value = Heap::AllocateJSGlobalPropertyCell(value); |
| 3055 | if (!maybe_value->ToObject(&value)) return maybe_value; |
| 3056 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3057 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3058 | Object* result; |
| 3059 | { MaybeObject* maybe_result = dictionary->Add(descs->GetKey(i), value, d); |
| 3060 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3061 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3062 | dictionary = StringDictionary::cast(result); |
| 3063 | } |
| 3064 | |
| 3065 | // Allocate the global object and initialize it with the backing store. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3066 | { MaybeObject* maybe_obj = Allocate(map, OLD_POINTER_SPACE); |
| 3067 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3068 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3069 | JSObject* global = JSObject::cast(obj); |
| 3070 | InitializeJSObjectFromMap(global, dictionary, map); |
| 3071 | |
| 3072 | // Create a new map for the global object. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3073 | { MaybeObject* maybe_obj = map->CopyDropDescriptors(); |
| 3074 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3075 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 3076 | Map* new_map = Map::cast(obj); |
| 3077 | |
| 3078 | // Setup the global object as a normalized object. |
| 3079 | global->set_map(new_map); |
| 3080 | global->map()->set_instance_descriptors(Heap::empty_descriptor_array()); |
| 3081 | global->set_properties(dictionary); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 3082 | |
| 3083 | // Make sure result is a global object with properties in dictionary. |
| 3084 | ASSERT(global->IsGlobalObject()); |
| 3085 | ASSERT(!global->HasFastProperties()); |
| 3086 | return global; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3090 | MaybeObject* Heap::CopyJSObject(JSObject* source) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3091 | // Never used to copy functions. If functions need to be copied we |
| 3092 | // have to be careful to clear the literals array. |
| 3093 | ASSERT(!source->IsJSFunction()); |
| 3094 | |
| 3095 | // Make the clone. |
| 3096 | Map* map = source->map(); |
| 3097 | int object_size = map->instance_size(); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3098 | Object* clone; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3099 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3100 | // If we're forced to always allocate, we use the general allocation |
| 3101 | // functions which may leave us with an object in old space. |
| 3102 | if (always_allocate()) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3103 | { MaybeObject* maybe_clone = |
| 3104 | AllocateRaw(object_size, NEW_SPACE, OLD_POINTER_SPACE); |
| 3105 | if (!maybe_clone->ToObject(&clone)) return maybe_clone; |
| 3106 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3107 | Address clone_address = HeapObject::cast(clone)->address(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3108 | CopyBlock(clone_address, |
| 3109 | source->address(), |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3110 | object_size); |
| 3111 | // Update write barrier for all fields that lie beyond the header. |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 3112 | RecordWrites(clone_address, |
| 3113 | JSObject::kHeaderSize, |
antonm@chromium.org | 8e5e338 | 2010-03-24 09:56:30 +0000 | [diff] [blame] | 3114 | (object_size - JSObject::kHeaderSize) / kPointerSize); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3115 | } else { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3116 | { MaybeObject* maybe_clone = new_space_.AllocateRaw(object_size); |
| 3117 | if (!maybe_clone->ToObject(&clone)) return maybe_clone; |
| 3118 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3119 | ASSERT(Heap::InNewSpace(clone)); |
| 3120 | // 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] | 3121 | // the contents without worrying about updating the write barrier. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3122 | CopyBlock(HeapObject::cast(clone)->address(), |
| 3123 | source->address(), |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3124 | object_size); |
| 3125 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3126 | |
| 3127 | FixedArray* elements = FixedArray::cast(source->elements()); |
| 3128 | FixedArray* properties = FixedArray::cast(source->properties()); |
| 3129 | // Update elements if necessary. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 3130 | if (elements->length() > 0) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3131 | Object* elem; |
| 3132 | { MaybeObject* maybe_elem = |
| 3133 | (elements->map() == fixed_cow_array_map()) ? |
| 3134 | elements : CopyFixedArray(elements); |
| 3135 | if (!maybe_elem->ToObject(&elem)) return maybe_elem; |
| 3136 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3137 | JSObject::cast(clone)->set_elements(FixedArray::cast(elem)); |
| 3138 | } |
| 3139 | // Update properties if necessary. |
| 3140 | if (properties->length() > 0) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3141 | Object* prop; |
| 3142 | { MaybeObject* maybe_prop = CopyFixedArray(properties); |
| 3143 | if (!maybe_prop->ToObject(&prop)) return maybe_prop; |
| 3144 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3145 | JSObject::cast(clone)->set_properties(FixedArray::cast(prop)); |
| 3146 | } |
| 3147 | // Return the new clone. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3148 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 3149 | ProducerHeapProfile::RecordJSObjectAllocation(clone); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 3150 | #endif |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3151 | return clone; |
| 3152 | } |
| 3153 | |
| 3154 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3155 | MaybeObject* Heap::ReinitializeJSGlobalProxy(JSFunction* constructor, |
| 3156 | JSGlobalProxy* object) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3157 | ASSERT(constructor->has_initial_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3158 | Map* map = constructor->initial_map(); |
| 3159 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3160 | // Check that the already allocated object has the same size and type as |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3161 | // objects allocated using the constructor. |
| 3162 | ASSERT(map->instance_size() == object->map()->instance_size()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3163 | ASSERT(map->instance_type() == object->map()->instance_type()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3164 | |
| 3165 | // Allocate the backing storage for the properties. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3166 | int prop_size = map->unused_property_fields() - map->inobject_properties(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3167 | Object* properties; |
| 3168 | { MaybeObject* maybe_properties = AllocateFixedArray(prop_size, TENURED); |
| 3169 | if (!maybe_properties->ToObject(&properties)) return maybe_properties; |
| 3170 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3171 | |
| 3172 | // Reset the map for the object. |
| 3173 | object->set_map(constructor->initial_map()); |
| 3174 | |
| 3175 | // Reinitialize the object from the constructor map. |
| 3176 | InitializeJSObjectFromMap(object, FixedArray::cast(properties), map); |
| 3177 | return object; |
| 3178 | } |
| 3179 | |
| 3180 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3181 | MaybeObject* Heap::AllocateStringFromAscii(Vector<const char> string, |
| 3182 | PretenureFlag pretenure) { |
| 3183 | Object* result; |
| 3184 | { MaybeObject* maybe_result = |
| 3185 | AllocateRawAsciiString(string.length(), pretenure); |
| 3186 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3187 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3188 | |
| 3189 | // Copy the characters into the new object. |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3190 | SeqAsciiString* string_result = SeqAsciiString::cast(result); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3191 | for (int i = 0; i < string.length(); i++) { |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3192 | string_result->SeqAsciiStringSet(i, string[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3193 | } |
| 3194 | return result; |
| 3195 | } |
| 3196 | |
| 3197 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3198 | MaybeObject* Heap::AllocateStringFromUtf8(Vector<const char> string, |
| 3199 | PretenureFlag pretenure) { |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3200 | // V8 only supports characters in the Basic Multilingual Plane. |
| 3201 | const uc32 kMaxSupportedChar = 0xFFFF; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3202 | // Count the number of characters in the UTF-8 string and check if |
| 3203 | // it is an ASCII string. |
| 3204 | Access<Scanner::Utf8Decoder> decoder(Scanner::utf8_decoder()); |
| 3205 | decoder->Reset(string.start(), string.length()); |
| 3206 | int chars = 0; |
| 3207 | bool is_ascii = true; |
| 3208 | while (decoder->has_more()) { |
| 3209 | uc32 r = decoder->GetNext(); |
| 3210 | if (r > String::kMaxAsciiCharCode) is_ascii = false; |
| 3211 | chars++; |
| 3212 | } |
| 3213 | |
| 3214 | // If the string is ascii, we do not need to convert the characters |
| 3215 | // since UTF8 is backwards compatible with ascii. |
| 3216 | if (is_ascii) return AllocateStringFromAscii(string, pretenure); |
| 3217 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3218 | Object* result; |
| 3219 | { MaybeObject* maybe_result = AllocateRawTwoByteString(chars, pretenure); |
| 3220 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3221 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3222 | |
| 3223 | // Convert and copy the characters into the new object. |
| 3224 | String* string_result = String::cast(result); |
| 3225 | decoder->Reset(string.start(), string.length()); |
| 3226 | for (int i = 0; i < chars; i++) { |
| 3227 | uc32 r = decoder->GetNext(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 3228 | if (r > kMaxSupportedChar) { r = unibrow::Utf8::kBadChar; } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3229 | string_result->Set(i, r); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3230 | } |
| 3231 | return result; |
| 3232 | } |
| 3233 | |
| 3234 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3235 | MaybeObject* Heap::AllocateStringFromTwoByte(Vector<const uc16> string, |
| 3236 | PretenureFlag pretenure) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3237 | // Check if the string is an ASCII string. |
| 3238 | int i = 0; |
| 3239 | while (i < string.length() && string[i] <= String::kMaxAsciiCharCode) i++; |
| 3240 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3241 | MaybeObject* maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3242 | if (i == string.length()) { // It's an ASCII string. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3243 | maybe_result = AllocateRawAsciiString(string.length(), pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3244 | } else { // It's not an ASCII string. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3245 | maybe_result = AllocateRawTwoByteString(string.length(), pretenure); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3246 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3247 | Object* result; |
| 3248 | if (!maybe_result->ToObject(&result)) return maybe_result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Copy the characters into the new object, which may be either ASCII or |
| 3251 | // UTF-16. |
| 3252 | String* string_result = String::cast(result); |
| 3253 | for (int i = 0; i < string.length(); i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3254 | string_result->Set(i, string[i]); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3255 | } |
| 3256 | return result; |
| 3257 | } |
| 3258 | |
| 3259 | |
| 3260 | Map* Heap::SymbolMapForString(String* string) { |
| 3261 | // If the string is in new space it cannot be used as a symbol. |
| 3262 | if (InNewSpace(string)) return NULL; |
| 3263 | |
| 3264 | // Find the corresponding symbol map for strings. |
| 3265 | Map* map = string->map(); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3266 | if (map == ascii_string_map()) return ascii_symbol_map(); |
| 3267 | if (map == string_map()) return symbol_map(); |
| 3268 | if (map == cons_string_map()) return cons_symbol_map(); |
| 3269 | if (map == cons_ascii_string_map()) return cons_ascii_symbol_map(); |
| 3270 | if (map == external_string_map()) return external_symbol_map(); |
| 3271 | if (map == external_ascii_string_map()) return external_ascii_symbol_map(); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 3272 | if (map == external_string_with_ascii_data_map()) { |
| 3273 | return external_symbol_with_ascii_data_map(); |
| 3274 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3275 | |
| 3276 | // No match found. |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | |
| 3280 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3281 | MaybeObject* Heap::AllocateInternalSymbol(unibrow::CharacterStream* buffer, |
| 3282 | int chars, |
| 3283 | uint32_t hash_field) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3284 | ASSERT(chars >= 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3285 | // Ensure the chars matches the number of characters in the buffer. |
| 3286 | ASSERT(static_cast<unsigned>(chars) == buffer->Length()); |
| 3287 | // Determine whether the string is ascii. |
| 3288 | bool is_ascii = true; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3289 | while (buffer->has_more()) { |
| 3290 | if (buffer->GetNext() > unibrow::Utf8::kMaxOneByteChar) { |
| 3291 | is_ascii = false; |
| 3292 | break; |
| 3293 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3294 | } |
| 3295 | buffer->Rewind(); |
| 3296 | |
| 3297 | // Compute map and object size. |
| 3298 | int size; |
| 3299 | Map* map; |
| 3300 | |
| 3301 | if (is_ascii) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3302 | if (chars > SeqAsciiString::kMaxLength) { |
| 3303 | return Failure::OutOfMemoryException(); |
| 3304 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3305 | map = ascii_symbol_map(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3306 | size = SeqAsciiString::SizeFor(chars); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3307 | } else { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3308 | if (chars > SeqTwoByteString::kMaxLength) { |
| 3309 | return Failure::OutOfMemoryException(); |
| 3310 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3311 | map = symbol_map(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3312 | size = SeqTwoByteString::SizeFor(chars); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | // Allocate string. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3316 | Object* result; |
| 3317 | { MaybeObject* maybe_result = (size > MaxObjectSizeInPagedSpace()) |
| 3318 | ? lo_space_->AllocateRaw(size) |
| 3319 | : old_data_space_->AllocateRaw(size); |
| 3320 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3321 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3322 | |
| 3323 | reinterpret_cast<HeapObject*>(result)->set_map(map); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3324 | // Set length and hash fields of the allocated string. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3325 | String* answer = String::cast(result); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3326 | answer->set_length(chars); |
| 3327 | answer->set_hash_field(hash_field); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3328 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3329 | ASSERT_EQ(size, answer->Size()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3330 | |
| 3331 | // Fill in the characters. |
| 3332 | for (int i = 0; i < chars; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3333 | answer->Set(i, buffer->GetNext()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3334 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 3335 | return answer; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3339 | MaybeObject* Heap::AllocateRawAsciiString(int length, PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3340 | if (length < 0 || length > SeqAsciiString::kMaxLength) { |
| 3341 | return Failure::OutOfMemoryException(); |
| 3342 | } |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3343 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3344 | int size = SeqAsciiString::SizeFor(length); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3345 | ASSERT(size <= SeqAsciiString::kMaxSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3346 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3347 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 3348 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 3349 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3350 | if (space == NEW_SPACE) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3351 | if (size > kMaxObjectSizeInNewSpace) { |
| 3352 | // Allocate in large object space, retry space will be ignored. |
| 3353 | space = LO_SPACE; |
| 3354 | } else if (size > MaxObjectSizeInPagedSpace()) { |
| 3355 | // Allocate in new space, retry in large object space. |
| 3356 | retry_space = LO_SPACE; |
| 3357 | } |
| 3358 | } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) { |
| 3359 | space = LO_SPACE; |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3360 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3361 | Object* result; |
| 3362 | { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space); |
| 3363 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3364 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3365 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3366 | // Partially initialize the object. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3367 | HeapObject::cast(result)->set_map(ascii_string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3368 | String::cast(result)->set_length(length); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3369 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3370 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 3371 | return result; |
| 3372 | } |
| 3373 | |
| 3374 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3375 | MaybeObject* Heap::AllocateRawTwoByteString(int length, |
| 3376 | PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3377 | if (length < 0 || length > SeqTwoByteString::kMaxLength) { |
| 3378 | return Failure::OutOfMemoryException(); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3379 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3380 | int size = SeqTwoByteString::SizeFor(length); |
| 3381 | ASSERT(size <= SeqTwoByteString::kMaxSize); |
| 3382 | AllocationSpace space = (pretenure == TENURED) ? OLD_DATA_SPACE : NEW_SPACE; |
| 3383 | AllocationSpace retry_space = OLD_DATA_SPACE; |
| 3384 | |
| 3385 | if (space == NEW_SPACE) { |
| 3386 | if (size > kMaxObjectSizeInNewSpace) { |
| 3387 | // Allocate in large object space, retry space will be ignored. |
| 3388 | space = LO_SPACE; |
| 3389 | } else if (size > MaxObjectSizeInPagedSpace()) { |
| 3390 | // Allocate in new space, retry in large object space. |
| 3391 | retry_space = LO_SPACE; |
| 3392 | } |
| 3393 | } else if (space == OLD_DATA_SPACE && size > MaxObjectSizeInPagedSpace()) { |
| 3394 | space = LO_SPACE; |
| 3395 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3396 | Object* result; |
| 3397 | { MaybeObject* maybe_result = AllocateRaw(size, space, retry_space); |
| 3398 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3399 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3400 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3401 | // Partially initialize the object. |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3402 | HeapObject::cast(result)->set_map(string_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3403 | String::cast(result)->set_length(length); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3404 | String::cast(result)->set_hash_field(String::kEmptyHashField); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3405 | ASSERT_EQ(size, HeapObject::cast(result)->Size()); |
| 3406 | return result; |
| 3407 | } |
| 3408 | |
| 3409 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3410 | MaybeObject* Heap::AllocateEmptyFixedArray() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3411 | int size = FixedArray::SizeFor(0); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3412 | Object* result; |
| 3413 | { MaybeObject* maybe_result = |
| 3414 | AllocateRaw(size, OLD_DATA_SPACE, OLD_DATA_SPACE); |
| 3415 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3416 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3417 | // Initialize the object. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3418 | reinterpret_cast<FixedArray*>(result)->set_map(fixed_array_map()); |
| 3419 | reinterpret_cast<FixedArray*>(result)->set_length(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3420 | return result; |
| 3421 | } |
| 3422 | |
| 3423 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3424 | MaybeObject* Heap::AllocateRawFixedArray(int length) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3425 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 3426 | return Failure::OutOfMemoryException(); |
| 3427 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3428 | ASSERT(length > 0); |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 3429 | // Use the general function if we're forced to always allocate. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 3430 | if (always_allocate()) return AllocateFixedArray(length, TENURED); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3431 | // Allocate the raw data for a fixed array. |
| 3432 | int size = FixedArray::SizeFor(length); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3433 | return size <= kMaxObjectSizeInNewSpace |
| 3434 | ? new_space_.AllocateRaw(size) |
| 3435 | : lo_space_->AllocateRawFixedArray(size); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3439 | MaybeObject* Heap::CopyFixedArrayWithMap(FixedArray* src, Map* map) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3440 | int len = src->length(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3441 | Object* obj; |
| 3442 | { MaybeObject* maybe_obj = AllocateRawFixedArray(len); |
| 3443 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3444 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3445 | if (Heap::InNewSpace(obj)) { |
| 3446 | HeapObject* dst = HeapObject::cast(obj); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3447 | dst->set_map(map); |
| 3448 | CopyBlock(dst->address() + kPointerSize, |
| 3449 | src->address() + kPointerSize, |
| 3450 | FixedArray::SizeFor(len) - kPointerSize); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3451 | return obj; |
| 3452 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 3453 | HeapObject::cast(obj)->set_map(map); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3454 | FixedArray* result = FixedArray::cast(obj); |
| 3455 | result->set_length(len); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3456 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3457 | // Copy the content |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3458 | AssertNoAllocation no_gc; |
| 3459 | WriteBarrierMode mode = result->GetWriteBarrierMode(no_gc); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3460 | for (int i = 0; i < len; i++) result->set(i, src->get(i), mode); |
| 3461 | return result; |
| 3462 | } |
| 3463 | |
| 3464 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3465 | MaybeObject* Heap::AllocateFixedArray(int length) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 3466 | ASSERT(length >= 0); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 3467 | if (length == 0) return empty_fixed_array(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3468 | Object* result; |
| 3469 | { MaybeObject* maybe_result = AllocateRawFixedArray(length); |
| 3470 | if (!maybe_result->ToObject(&result)) return maybe_result; |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3471 | } |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3472 | // Initialize header. |
| 3473 | FixedArray* array = reinterpret_cast<FixedArray*>(result); |
| 3474 | array->set_map(fixed_array_map()); |
| 3475 | array->set_length(length); |
| 3476 | // Initialize body. |
| 3477 | ASSERT(!Heap::InNewSpace(undefined_value())); |
| 3478 | MemsetPointer(array->data_start(), undefined_value(), length); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3479 | return result; |
| 3480 | } |
| 3481 | |
| 3482 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3483 | MaybeObject* Heap::AllocateRawFixedArray(int length, PretenureFlag pretenure) { |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3484 | if (length < 0 || length > FixedArray::kMaxLength) { |
| 3485 | return Failure::OutOfMemoryException(); |
| 3486 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3487 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3488 | AllocationSpace space = |
| 3489 | (pretenure == TENURED) ? OLD_POINTER_SPACE : NEW_SPACE; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3490 | int size = FixedArray::SizeFor(length); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3491 | if (space == NEW_SPACE && size > kMaxObjectSizeInNewSpace) { |
| 3492 | // Too big for new space. |
| 3493 | space = LO_SPACE; |
| 3494 | } else if (space == OLD_POINTER_SPACE && |
| 3495 | size > MaxObjectSizeInPagedSpace()) { |
| 3496 | // Too big for old pointer space. |
| 3497 | space = LO_SPACE; |
| 3498 | } |
| 3499 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3500 | AllocationSpace retry_space = |
| 3501 | (size <= MaxObjectSizeInPagedSpace()) ? OLD_POINTER_SPACE : LO_SPACE; |
| 3502 | |
| 3503 | return AllocateRaw(size, space, retry_space); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3507 | MUST_USE_RESULT static MaybeObject* AllocateFixedArrayWithFiller( |
| 3508 | int length, |
| 3509 | PretenureFlag pretenure, |
| 3510 | Object* filler) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3511 | ASSERT(length >= 0); |
| 3512 | ASSERT(Heap::empty_fixed_array()->IsFixedArray()); |
| 3513 | if (length == 0) return Heap::empty_fixed_array(); |
| 3514 | |
| 3515 | ASSERT(!Heap::InNewSpace(filler)); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3516 | Object* result; |
| 3517 | { MaybeObject* maybe_result = Heap::AllocateRawFixedArray(length, pretenure); |
| 3518 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3519 | } |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 3520 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3521 | HeapObject::cast(result)->set_map(Heap::fixed_array_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3522 | FixedArray* array = FixedArray::cast(result); |
| 3523 | array->set_length(length); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3524 | MemsetPointer(array->data_start(), filler, length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3525 | return array; |
| 3526 | } |
| 3527 | |
| 3528 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3529 | MaybeObject* Heap::AllocateFixedArray(int length, PretenureFlag pretenure) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3530 | return AllocateFixedArrayWithFiller(length, pretenure, undefined_value()); |
| 3531 | } |
| 3532 | |
| 3533 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3534 | MaybeObject* Heap::AllocateFixedArrayWithHoles(int length, |
| 3535 | PretenureFlag pretenure) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 3536 | return AllocateFixedArrayWithFiller(length, pretenure, the_hole_value()); |
| 3537 | } |
| 3538 | |
| 3539 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3540 | MaybeObject* Heap::AllocateUninitializedFixedArray(int length) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3541 | if (length == 0) return empty_fixed_array(); |
| 3542 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3543 | Object* obj; |
| 3544 | { MaybeObject* maybe_obj = AllocateRawFixedArray(length); |
| 3545 | if (!maybe_obj->ToObject(&obj)) return maybe_obj; |
| 3546 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3547 | |
| 3548 | reinterpret_cast<FixedArray*>(obj)->set_map(fixed_array_map()); |
| 3549 | FixedArray::cast(obj)->set_length(length); |
| 3550 | return obj; |
| 3551 | } |
| 3552 | |
| 3553 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3554 | MaybeObject* Heap::AllocateHashTable(int length, PretenureFlag pretenure) { |
| 3555 | Object* result; |
| 3556 | { MaybeObject* maybe_result = Heap::AllocateFixedArray(length, pretenure); |
| 3557 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3558 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3559 | reinterpret_cast<HeapObject*>(result)->set_map(hash_table_map()); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 3560 | ASSERT(result->IsHashTable()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3561 | return result; |
| 3562 | } |
| 3563 | |
| 3564 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3565 | MaybeObject* Heap::AllocateGlobalContext() { |
| 3566 | Object* result; |
| 3567 | { MaybeObject* maybe_result = |
| 3568 | Heap::AllocateFixedArray(Context::GLOBAL_CONTEXT_SLOTS); |
| 3569 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3570 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3571 | Context* context = reinterpret_cast<Context*>(result); |
| 3572 | context->set_map(global_context_map()); |
| 3573 | ASSERT(context->IsGlobalContext()); |
| 3574 | ASSERT(result->IsContext()); |
| 3575 | return result; |
| 3576 | } |
| 3577 | |
| 3578 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3579 | MaybeObject* Heap::AllocateFunctionContext(int length, JSFunction* function) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3580 | ASSERT(length >= Context::MIN_CONTEXT_SLOTS); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3581 | Object* result; |
| 3582 | { MaybeObject* maybe_result = Heap::AllocateFixedArray(length); |
| 3583 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3584 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3585 | Context* context = reinterpret_cast<Context*>(result); |
| 3586 | context->set_map(context_map()); |
| 3587 | context->set_closure(function); |
| 3588 | context->set_fcontext(context); |
| 3589 | context->set_previous(NULL); |
| 3590 | context->set_extension(NULL); |
| 3591 | context->set_global(function->context()->global()); |
| 3592 | ASSERT(!context->IsGlobalContext()); |
| 3593 | ASSERT(context->is_function_context()); |
| 3594 | ASSERT(result->IsContext()); |
| 3595 | return result; |
| 3596 | } |
| 3597 | |
| 3598 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3599 | MaybeObject* Heap::AllocateWithContext(Context* previous, |
| 3600 | JSObject* extension, |
| 3601 | bool is_catch_context) { |
| 3602 | Object* result; |
| 3603 | { MaybeObject* maybe_result = |
| 3604 | Heap::AllocateFixedArray(Context::MIN_CONTEXT_SLOTS); |
| 3605 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3606 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3607 | Context* context = reinterpret_cast<Context*>(result); |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 3608 | context->set_map(is_catch_context ? catch_context_map() : context_map()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3609 | context->set_closure(previous->closure()); |
| 3610 | context->set_fcontext(previous->fcontext()); |
| 3611 | context->set_previous(previous); |
| 3612 | context->set_extension(extension); |
| 3613 | context->set_global(previous->global()); |
| 3614 | ASSERT(!context->IsGlobalContext()); |
| 3615 | ASSERT(!context->is_function_context()); |
| 3616 | ASSERT(result->IsContext()); |
| 3617 | return result; |
| 3618 | } |
| 3619 | |
| 3620 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3621 | MaybeObject* Heap::AllocateStruct(InstanceType type) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3622 | Map* map; |
| 3623 | switch (type) { |
| 3624 | #define MAKE_CASE(NAME, Name, name) case NAME##_TYPE: map = name##_map(); break; |
| 3625 | STRUCT_LIST(MAKE_CASE) |
| 3626 | #undef MAKE_CASE |
| 3627 | default: |
| 3628 | UNREACHABLE(); |
| 3629 | return Failure::InternalError(); |
| 3630 | } |
| 3631 | int size = map->instance_size(); |
| 3632 | AllocationSpace space = |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 3633 | (size > MaxObjectSizeInPagedSpace()) ? LO_SPACE : OLD_POINTER_SPACE; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3634 | Object* result; |
| 3635 | { MaybeObject* maybe_result = Heap::Allocate(map, space); |
| 3636 | if (!maybe_result->ToObject(&result)) return maybe_result; |
| 3637 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3638 | Struct::cast(result)->InitializeBody(size); |
| 3639 | return result; |
| 3640 | } |
| 3641 | |
| 3642 | |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3643 | bool Heap::IdleNotification() { |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3644 | static const int kIdlesBeforeScavenge = 4; |
| 3645 | static const int kIdlesBeforeMarkSweep = 7; |
| 3646 | static const int kIdlesBeforeMarkCompact = 8; |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3647 | static int number_idle_notifications = 0; |
| 3648 | static int last_gc_count = gc_count_; |
| 3649 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3650 | bool uncommit = true; |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3651 | bool finished = false; |
| 3652 | |
| 3653 | if (last_gc_count == gc_count_) { |
| 3654 | number_idle_notifications++; |
| 3655 | } else { |
| 3656 | number_idle_notifications = 0; |
| 3657 | last_gc_count = gc_count_; |
| 3658 | } |
| 3659 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3660 | if (number_idle_notifications == kIdlesBeforeScavenge) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3661 | if (contexts_disposed_ > 0) { |
| 3662 | HistogramTimerScope scope(&Counters::gc_context); |
| 3663 | CollectAllGarbage(false); |
| 3664 | } else { |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 3665 | CollectGarbage(NEW_SPACE); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3666 | } |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3667 | new_space_.Shrink(); |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3668 | last_gc_count = gc_count_; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3669 | |
| 3670 | } else if (number_idle_notifications == kIdlesBeforeMarkSweep) { |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 3671 | // Before doing the mark-sweep collections we clear the |
| 3672 | // compilation cache to avoid hanging on to source code and |
| 3673 | // generated code for cached functions. |
| 3674 | CompilationCache::Clear(); |
| 3675 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 3676 | CollectAllGarbage(false); |
| 3677 | new_space_.Shrink(); |
| 3678 | last_gc_count = gc_count_; |
| 3679 | |
| 3680 | } else if (number_idle_notifications == kIdlesBeforeMarkCompact) { |
| 3681 | CollectAllGarbage(true); |
| 3682 | new_space_.Shrink(); |
| 3683 | last_gc_count = gc_count_; |
| 3684 | number_idle_notifications = 0; |
| 3685 | finished = true; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3686 | |
| 3687 | } else if (contexts_disposed_ > 0) { |
| 3688 | if (FLAG_expose_gc) { |
| 3689 | contexts_disposed_ = 0; |
| 3690 | } else { |
| 3691 | HistogramTimerScope scope(&Counters::gc_context); |
| 3692 | CollectAllGarbage(false); |
| 3693 | last_gc_count = gc_count_; |
| 3694 | } |
| 3695 | // If this is the first idle notification, we reset the |
| 3696 | // notification count to avoid letting idle notifications for |
| 3697 | // context disposal garbage collections start a potentially too |
| 3698 | // aggressive idle GC cycle. |
| 3699 | if (number_idle_notifications <= 1) { |
| 3700 | number_idle_notifications = 0; |
| 3701 | uncommit = false; |
| 3702 | } |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3705 | // Make sure that we have no pending context disposals and |
| 3706 | // conditionally uncommit from space. |
| 3707 | ASSERT(contexts_disposed_ == 0); |
| 3708 | if (uncommit) Heap::UncommitFromSpace(); |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 3709 | return finished; |
| 3710 | } |
| 3711 | |
| 3712 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3713 | #ifdef DEBUG |
| 3714 | |
| 3715 | void Heap::Print() { |
| 3716 | if (!HasBeenSetup()) return; |
| 3717 | Top::PrintStack(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3718 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 3719 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 3720 | space->Print(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | |
| 3724 | void Heap::ReportCodeStatistics(const char* title) { |
| 3725 | PrintF(">>>>>> Code Stats (%s) >>>>>>\n", title); |
| 3726 | PagedSpace::ResetCodeStatistics(); |
| 3727 | // We do not look for code in new space, map space, or old space. If code |
| 3728 | // somehow ends up in those spaces, we would miss it here. |
| 3729 | code_space_->CollectCodeStatistics(); |
| 3730 | lo_space_->CollectCodeStatistics(); |
| 3731 | PagedSpace::ReportCodeStatistics(); |
| 3732 | } |
| 3733 | |
| 3734 | |
| 3735 | // This function expects that NewSpace's allocated objects histogram is |
| 3736 | // populated (via a call to CollectStatistics or else as a side effect of a |
| 3737 | // just-completed scavenge collection). |
| 3738 | void Heap::ReportHeapStatistics(const char* title) { |
| 3739 | USE(title); |
| 3740 | PrintF(">>>>>> =============== %s (%d) =============== >>>>>>\n", |
| 3741 | title, gc_count_); |
| 3742 | PrintF("mark-compact GC : %d\n", mc_count_); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 3743 | PrintF("old_gen_promotion_limit_ %" V8_PTR_PREFIX "d\n", |
| 3744 | old_gen_promotion_limit_); |
| 3745 | PrintF("old_gen_allocation_limit_ %" V8_PTR_PREFIX "d\n", |
| 3746 | old_gen_allocation_limit_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3747 | |
| 3748 | PrintF("\n"); |
| 3749 | PrintF("Number of handles : %d\n", HandleScope::NumberOfHandles()); |
| 3750 | GlobalHandles::PrintStats(); |
| 3751 | PrintF("\n"); |
| 3752 | |
| 3753 | PrintF("Heap statistics : "); |
| 3754 | MemoryAllocator::ReportStatistics(); |
| 3755 | PrintF("To space : "); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3756 | new_space_.ReportStatistics(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3757 | PrintF("Old pointer space : "); |
| 3758 | old_pointer_space_->ReportStatistics(); |
| 3759 | PrintF("Old data space : "); |
| 3760 | old_data_space_->ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3761 | PrintF("Code space : "); |
| 3762 | code_space_->ReportStatistics(); |
| 3763 | PrintF("Map space : "); |
| 3764 | map_space_->ReportStatistics(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3765 | PrintF("Cell space : "); |
| 3766 | cell_space_->ReportStatistics(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3767 | PrintF("Large object space : "); |
| 3768 | lo_space_->ReportStatistics(); |
| 3769 | PrintF(">>>>>> ========================================= >>>>>>\n"); |
| 3770 | } |
| 3771 | |
| 3772 | #endif // DEBUG |
| 3773 | |
| 3774 | bool Heap::Contains(HeapObject* value) { |
| 3775 | return Contains(value->address()); |
| 3776 | } |
| 3777 | |
| 3778 | |
| 3779 | bool Heap::Contains(Address addr) { |
| 3780 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
| 3781 | return HasBeenSetup() && |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3782 | (new_space_.ToSpaceContains(addr) || |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3783 | old_pointer_space_->Contains(addr) || |
| 3784 | old_data_space_->Contains(addr) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3785 | code_space_->Contains(addr) || |
| 3786 | map_space_->Contains(addr) || |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3787 | cell_space_->Contains(addr) || |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3788 | lo_space_->SlowContains(addr)); |
| 3789 | } |
| 3790 | |
| 3791 | |
| 3792 | bool Heap::InSpace(HeapObject* value, AllocationSpace space) { |
| 3793 | return InSpace(value->address(), space); |
| 3794 | } |
| 3795 | |
| 3796 | |
| 3797 | bool Heap::InSpace(Address addr, AllocationSpace space) { |
| 3798 | if (OS::IsOutsideAllocatedSpace(addr)) return false; |
| 3799 | if (!HasBeenSetup()) return false; |
| 3800 | |
| 3801 | switch (space) { |
| 3802 | case NEW_SPACE: |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3803 | return new_space_.ToSpaceContains(addr); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 3804 | case OLD_POINTER_SPACE: |
| 3805 | return old_pointer_space_->Contains(addr); |
| 3806 | case OLD_DATA_SPACE: |
| 3807 | return old_data_space_->Contains(addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3808 | case CODE_SPACE: |
| 3809 | return code_space_->Contains(addr); |
| 3810 | case MAP_SPACE: |
| 3811 | return map_space_->Contains(addr); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3812 | case CELL_SPACE: |
| 3813 | return cell_space_->Contains(addr); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3814 | case LO_SPACE: |
| 3815 | return lo_space_->SlowContains(addr); |
| 3816 | } |
| 3817 | |
| 3818 | return false; |
| 3819 | } |
| 3820 | |
| 3821 | |
| 3822 | #ifdef DEBUG |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3823 | static void DummyScavengePointer(HeapObject** p) { |
| 3824 | } |
| 3825 | |
| 3826 | |
| 3827 | static void VerifyPointersUnderWatermark( |
| 3828 | PagedSpace* space, |
| 3829 | DirtyRegionCallback visit_dirty_region) { |
| 3830 | PageIterator it(space, PageIterator::PAGES_IN_USE); |
| 3831 | |
| 3832 | while (it.has_next()) { |
| 3833 | Page* page = it.next(); |
| 3834 | Address start = page->ObjectAreaStart(); |
| 3835 | Address end = page->AllocationWatermark(); |
| 3836 | |
| 3837 | Heap::IterateDirtyRegions(Page::kAllRegionsDirtyMarks, |
| 3838 | start, |
| 3839 | end, |
| 3840 | visit_dirty_region, |
| 3841 | &DummyScavengePointer); |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | |
| 3846 | static void VerifyPointersUnderWatermark(LargeObjectSpace* space) { |
| 3847 | LargeObjectIterator it(space); |
| 3848 | for (HeapObject* object = it.next(); object != NULL; object = it.next()) { |
| 3849 | if (object->IsFixedArray()) { |
| 3850 | Address slot_address = object->address(); |
| 3851 | Address end = object->address() + object->Size(); |
| 3852 | |
| 3853 | while (slot_address < end) { |
| 3854 | HeapObject** slot = reinterpret_cast<HeapObject**>(slot_address); |
| 3855 | // When we are not in GC the Heap::InNewSpace() predicate |
| 3856 | // checks that pointers which satisfy predicate point into |
| 3857 | // the active semispace. |
| 3858 | Heap::InNewSpace(*slot); |
| 3859 | slot_address += kPointerSize; |
| 3860 | } |
| 3861 | } |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3866 | void Heap::Verify() { |
| 3867 | ASSERT(HasBeenSetup()); |
| 3868 | |
| 3869 | VerifyPointersVisitor visitor; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3870 | IterateRoots(&visitor, VISIT_ONLY_STRONG); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3871 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3872 | new_space_.Verify(); |
| 3873 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3874 | VerifyPointersAndDirtyRegionsVisitor dirty_regions_visitor; |
| 3875 | old_pointer_space_->Verify(&dirty_regions_visitor); |
| 3876 | map_space_->Verify(&dirty_regions_visitor); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3877 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3878 | VerifyPointersUnderWatermark(old_pointer_space_, |
| 3879 | &IteratePointersInDirtyRegion); |
| 3880 | VerifyPointersUnderWatermark(map_space_, |
| 3881 | &IteratePointersInDirtyMapsRegion); |
| 3882 | VerifyPointersUnderWatermark(lo_space_); |
| 3883 | |
| 3884 | VerifyPageWatermarkValidity(old_pointer_space_, ALL_INVALID); |
| 3885 | VerifyPageWatermarkValidity(map_space_, ALL_INVALID); |
| 3886 | |
| 3887 | VerifyPointersVisitor no_dirty_regions_visitor; |
| 3888 | old_data_space_->Verify(&no_dirty_regions_visitor); |
| 3889 | code_space_->Verify(&no_dirty_regions_visitor); |
| 3890 | cell_space_->Verify(&no_dirty_regions_visitor); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 3891 | |
| 3892 | lo_space_->Verify(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3893 | } |
| 3894 | #endif // DEBUG |
| 3895 | |
| 3896 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3897 | MaybeObject* Heap::LookupSymbol(Vector<const char> string) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3898 | Object* symbol = NULL; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3899 | Object* new_table; |
| 3900 | { MaybeObject* maybe_new_table = |
| 3901 | symbol_table()->LookupSymbol(string, &symbol); |
| 3902 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 3903 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3904 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 3905 | // SymbolTable is a singleton and checks for identity. |
| 3906 | roots_[kSymbolTableRootIndex] = new_table; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3907 | ASSERT(symbol != NULL); |
| 3908 | return symbol; |
| 3909 | } |
| 3910 | |
| 3911 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3912 | MaybeObject* Heap::LookupSymbol(String* string) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3913 | if (string->IsSymbol()) return string; |
| 3914 | Object* symbol = NULL; |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame^] | 3915 | Object* new_table; |
| 3916 | { MaybeObject* maybe_new_table = |
| 3917 | symbol_table()->LookupString(string, &symbol); |
| 3918 | if (!maybe_new_table->ToObject(&new_table)) return maybe_new_table; |
| 3919 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3920 | // Can't use set_symbol_table because SymbolTable::cast knows that |
| 3921 | // SymbolTable is a singleton and checks for identity. |
| 3922 | roots_[kSymbolTableRootIndex] = new_table; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3923 | ASSERT(symbol != NULL); |
| 3924 | return symbol; |
| 3925 | } |
| 3926 | |
| 3927 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3928 | bool Heap::LookupSymbolIfExists(String* string, String** symbol) { |
| 3929 | if (string->IsSymbol()) { |
| 3930 | *symbol = string; |
| 3931 | return true; |
| 3932 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 3933 | return symbol_table()->LookupSymbolIfExists(string, symbol); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3937 | #ifdef DEBUG |
| 3938 | void Heap::ZapFromSpace() { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 3939 | ASSERT(reinterpret_cast<Object*>(kFromSpaceZapValue)->IsHeapObject()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3940 | for (Address a = new_space_.FromSpaceLow(); |
| 3941 | a < new_space_.FromSpaceHigh(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3942 | a += kPointerSize) { |
| 3943 | Memory::Address_at(a) = kFromSpaceZapValue; |
| 3944 | } |
| 3945 | } |
| 3946 | #endif // DEBUG |
| 3947 | |
| 3948 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3949 | bool Heap::IteratePointersInDirtyRegion(Address start, |
| 3950 | Address end, |
| 3951 | ObjectSlotCallback copy_object_func) { |
| 3952 | Address slot_address = start; |
| 3953 | bool pointers_to_new_space_found = false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3954 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3955 | while (slot_address < end) { |
| 3956 | Object** slot = reinterpret_cast<Object**>(slot_address); |
| 3957 | if (Heap::InNewSpace(*slot)) { |
| 3958 | ASSERT((*slot)->IsHeapObject()); |
| 3959 | copy_object_func(reinterpret_cast<HeapObject**>(slot)); |
| 3960 | if (Heap::InNewSpace(*slot)) { |
| 3961 | ASSERT((*slot)->IsHeapObject()); |
| 3962 | pointers_to_new_space_found = true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3963 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3964 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3965 | slot_address += kPointerSize; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3966 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3967 | return pointers_to_new_space_found; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3968 | } |
| 3969 | |
| 3970 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3971 | // Compute start address of the first map following given addr. |
| 3972 | static inline Address MapStartAlign(Address addr) { |
| 3973 | Address page = Page::FromAddress(addr)->ObjectAreaStart(); |
| 3974 | return page + (((addr - page) + (Map::kSize - 1)) / Map::kSize * Map::kSize); |
| 3975 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 3976 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 3977 | |
| 3978 | // Compute end address of the first map preceding given addr. |
| 3979 | static inline Address MapEndAlign(Address addr) { |
| 3980 | Address page = Page::FromAllocationTop(addr)->ObjectAreaStart(); |
| 3981 | return page + ((addr - page) / Map::kSize * Map::kSize); |
| 3982 | } |
| 3983 | |
| 3984 | |
| 3985 | static bool IteratePointersInDirtyMaps(Address start, |
| 3986 | Address end, |
| 3987 | ObjectSlotCallback copy_object_func) { |
| 3988 | ASSERT(MapStartAlign(start) == start); |
| 3989 | ASSERT(MapEndAlign(end) == end); |
| 3990 | |
| 3991 | Address map_address = start; |
| 3992 | bool pointers_to_new_space_found = false; |
| 3993 | |
| 3994 | while (map_address < end) { |
| 3995 | ASSERT(!Heap::InNewSpace(Memory::Object_at(map_address))); |
| 3996 | ASSERT(Memory::Object_at(map_address)->IsMap()); |
| 3997 | |
| 3998 | Address pointer_fields_start = map_address + Map::kPointerFieldsBeginOffset; |
| 3999 | Address pointer_fields_end = map_address + Map::kPointerFieldsEndOffset; |
| 4000 | |
| 4001 | if (Heap::IteratePointersInDirtyRegion(pointer_fields_start, |
| 4002 | pointer_fields_end, |
| 4003 | copy_object_func)) { |
| 4004 | pointers_to_new_space_found = true; |
| 4005 | } |
| 4006 | |
| 4007 | map_address += Map::kSize; |
| 4008 | } |
| 4009 | |
| 4010 | return pointers_to_new_space_found; |
| 4011 | } |
| 4012 | |
| 4013 | |
| 4014 | bool Heap::IteratePointersInDirtyMapsRegion( |
| 4015 | Address start, |
| 4016 | Address end, |
| 4017 | ObjectSlotCallback copy_object_func) { |
| 4018 | Address map_aligned_start = MapStartAlign(start); |
| 4019 | Address map_aligned_end = MapEndAlign(end); |
| 4020 | |
| 4021 | bool contains_pointers_to_new_space = false; |
| 4022 | |
| 4023 | if (map_aligned_start != start) { |
| 4024 | Address prev_map = map_aligned_start - Map::kSize; |
| 4025 | ASSERT(Memory::Object_at(prev_map)->IsMap()); |
| 4026 | |
| 4027 | Address pointer_fields_start = |
| 4028 | Max(start, prev_map + Map::kPointerFieldsBeginOffset); |
| 4029 | |
| 4030 | Address pointer_fields_end = |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 4031 | Min(prev_map + Map::kPointerFieldsEndOffset, end); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4032 | |
| 4033 | contains_pointers_to_new_space = |
| 4034 | IteratePointersInDirtyRegion(pointer_fields_start, |
| 4035 | pointer_fields_end, |
| 4036 | copy_object_func) |
| 4037 | || contains_pointers_to_new_space; |
| 4038 | } |
| 4039 | |
| 4040 | contains_pointers_to_new_space = |
| 4041 | IteratePointersInDirtyMaps(map_aligned_start, |
| 4042 | map_aligned_end, |
| 4043 | copy_object_func) |
| 4044 | || contains_pointers_to_new_space; |
| 4045 | |
| 4046 | if (map_aligned_end != end) { |
| 4047 | ASSERT(Memory::Object_at(map_aligned_end)->IsMap()); |
| 4048 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 4049 | Address pointer_fields_start = |
| 4050 | map_aligned_end + Map::kPointerFieldsBeginOffset; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4051 | |
| 4052 | Address pointer_fields_end = |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 4053 | Min(end, map_aligned_end + Map::kPointerFieldsEndOffset); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4054 | |
| 4055 | contains_pointers_to_new_space = |
| 4056 | IteratePointersInDirtyRegion(pointer_fields_start, |
| 4057 | pointer_fields_end, |
| 4058 | copy_object_func) |
| 4059 | || contains_pointers_to_new_space; |
| 4060 | } |
| 4061 | |
| 4062 | return contains_pointers_to_new_space; |
| 4063 | } |
| 4064 | |
| 4065 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 4066 | void Heap::IterateAndMarkPointersToFromSpace(Address start, |
| 4067 | Address end, |
| 4068 | ObjectSlotCallback callback) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4069 | Address slot_address = start; |
| 4070 | Page* page = Page::FromAddress(start); |
| 4071 | |
| 4072 | uint32_t marks = page->GetRegionMarks(); |
| 4073 | |
| 4074 | while (slot_address < end) { |
| 4075 | Object** slot = reinterpret_cast<Object**>(slot_address); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 4076 | if (Heap::InFromSpace(*slot)) { |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4077 | ASSERT((*slot)->IsHeapObject()); |
| 4078 | callback(reinterpret_cast<HeapObject**>(slot)); |
| 4079 | if (Heap::InNewSpace(*slot)) { |
| 4080 | ASSERT((*slot)->IsHeapObject()); |
| 4081 | marks |= page->GetRegionMaskForAddress(slot_address); |
| 4082 | } |
| 4083 | } |
| 4084 | slot_address += kPointerSize; |
| 4085 | } |
| 4086 | |
| 4087 | page->SetRegionMarks(marks); |
| 4088 | } |
| 4089 | |
| 4090 | |
| 4091 | uint32_t Heap::IterateDirtyRegions( |
| 4092 | uint32_t marks, |
| 4093 | Address area_start, |
| 4094 | Address area_end, |
| 4095 | DirtyRegionCallback visit_dirty_region, |
| 4096 | ObjectSlotCallback copy_object_func) { |
| 4097 | uint32_t newmarks = 0; |
| 4098 | uint32_t mask = 1; |
| 4099 | |
| 4100 | if (area_start >= area_end) { |
| 4101 | return newmarks; |
| 4102 | } |
| 4103 | |
| 4104 | Address region_start = area_start; |
| 4105 | |
| 4106 | // area_start does not necessarily coincide with start of the first region. |
| 4107 | // Thus to calculate the beginning of the next region we have to align |
| 4108 | // area_start by Page::kRegionSize. |
| 4109 | Address second_region = |
| 4110 | reinterpret_cast<Address>( |
| 4111 | reinterpret_cast<intptr_t>(area_start + Page::kRegionSize) & |
| 4112 | ~Page::kRegionAlignmentMask); |
| 4113 | |
| 4114 | // Next region might be beyond area_end. |
| 4115 | Address region_end = Min(second_region, area_end); |
| 4116 | |
| 4117 | if (marks & mask) { |
| 4118 | if (visit_dirty_region(region_start, region_end, copy_object_func)) { |
| 4119 | newmarks |= mask; |
| 4120 | } |
| 4121 | } |
| 4122 | mask <<= 1; |
| 4123 | |
| 4124 | // Iterate subsequent regions which fully lay inside [area_start, area_end[. |
| 4125 | region_start = region_end; |
| 4126 | region_end = region_start + Page::kRegionSize; |
| 4127 | |
| 4128 | while (region_end <= area_end) { |
| 4129 | if (marks & mask) { |
| 4130 | if (visit_dirty_region(region_start, region_end, copy_object_func)) { |
| 4131 | newmarks |= mask; |
| 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | region_start = region_end; |
| 4136 | region_end = region_start + Page::kRegionSize; |
| 4137 | |
| 4138 | mask <<= 1; |
| 4139 | } |
| 4140 | |
| 4141 | if (region_start != area_end) { |
| 4142 | // A small piece of area left uniterated because area_end does not coincide |
| 4143 | // with region end. Check whether region covering last part of area is |
| 4144 | // dirty. |
| 4145 | if (marks & mask) { |
| 4146 | if (visit_dirty_region(region_start, area_end, copy_object_func)) { |
| 4147 | newmarks |= mask; |
| 4148 | } |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | return newmarks; |
| 4153 | } |
| 4154 | |
| 4155 | |
| 4156 | |
| 4157 | void Heap::IterateDirtyRegions( |
| 4158 | PagedSpace* space, |
| 4159 | DirtyRegionCallback visit_dirty_region, |
| 4160 | ObjectSlotCallback copy_object_func, |
| 4161 | ExpectedPageWatermarkState expected_page_watermark_state) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4162 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4163 | PageIterator it(space, PageIterator::PAGES_IN_USE); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4164 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4165 | while (it.has_next()) { |
| 4166 | Page* page = it.next(); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4167 | uint32_t marks = page->GetRegionMarks(); |
| 4168 | |
| 4169 | if (marks != Page::kAllRegionsCleanMarks) { |
| 4170 | Address start = page->ObjectAreaStart(); |
| 4171 | |
| 4172 | // Do not try to visit pointers beyond page allocation watermark. |
| 4173 | // Page can contain garbage pointers there. |
| 4174 | Address end; |
| 4175 | |
| 4176 | if ((expected_page_watermark_state == WATERMARK_SHOULD_BE_VALID) || |
| 4177 | page->IsWatermarkValid()) { |
| 4178 | end = page->AllocationWatermark(); |
| 4179 | } else { |
| 4180 | end = page->CachedAllocationWatermark(); |
| 4181 | } |
| 4182 | |
| 4183 | ASSERT(space == old_pointer_space_ || |
| 4184 | (space == map_space_ && |
| 4185 | ((page->ObjectAreaStart() - end) % Map::kSize == 0))); |
| 4186 | |
| 4187 | page->SetRegionMarks(IterateDirtyRegions(marks, |
| 4188 | start, |
| 4189 | end, |
| 4190 | visit_dirty_region, |
| 4191 | copy_object_func)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 4192 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 4193 | |
| 4194 | // Mark page watermark as invalid to maintain watermark validity invariant. |
| 4195 | // See Page::FlipMeaningOfInvalidatedWatermarkFlag() for details. |
| 4196 | page->InvalidateWatermark(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4197 | } |
| 4198 | } |
| 4199 | |
| 4200 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4201 | void Heap::IterateRoots(ObjectVisitor* v, VisitMode mode) { |
| 4202 | IterateStrongRoots(v, mode); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4203 | IterateWeakRoots(v, mode); |
| 4204 | } |
| 4205 | |
| 4206 | |
| 4207 | void Heap::IterateWeakRoots(ObjectVisitor* v, VisitMode mode) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4208 | v->VisitPointer(reinterpret_cast<Object**>(&roots_[kSymbolTableRootIndex])); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4209 | v->Synchronize("symbol_table"); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4210 | if (mode != VISIT_ALL_IN_SCAVENGE) { |
| 4211 | // Scavenge collections have special processing for this. |
| 4212 | ExternalStringTable::Iterate(v); |
| 4213 | } |
| 4214 | v->Synchronize("external_string_table"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4218 | void Heap::IterateStrongRoots(ObjectVisitor* v, VisitMode mode) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 4219 | v->VisitPointers(&roots_[0], &roots_[kStrongRootListLength]); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4220 | v->Synchronize("strong_root_list"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4221 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 4222 | v->VisitPointer(BitCast<Object**>(&hidden_symbol_)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4223 | v->Synchronize("symbol"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4224 | |
| 4225 | Bootstrapper::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4226 | v->Synchronize("bootstrapper"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4227 | Top::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4228 | v->Synchronize("top"); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4229 | Relocatable::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4230 | v->Synchronize("relocatable"); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4231 | |
| 4232 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4233 | Debug::Iterate(v); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 4234 | #endif |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4235 | v->Synchronize("debug"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 4236 | CompilationCache::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4237 | v->Synchronize("compilationcache"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4238 | |
| 4239 | // Iterate over local handles in handle scopes. |
| 4240 | HandleScopeImplementer::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4241 | v->Synchronize("handlescope"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4242 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4243 | // Iterate over the builtin code objects and code stubs in the |
| 4244 | // heap. Note that it is not necessary to iterate over code objects |
| 4245 | // on scavenge collections. |
| 4246 | if (mode != VISIT_ALL_IN_SCAVENGE) { |
| 4247 | Builtins::IterateBuiltins(v); |
| 4248 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4249 | v->Synchronize("builtins"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4250 | |
| 4251 | // Iterate over global handles. |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4252 | if (mode == VISIT_ONLY_STRONG) { |
| 4253 | GlobalHandles::IterateStrongRoots(v); |
| 4254 | } else { |
| 4255 | GlobalHandles::IterateAllRoots(v); |
| 4256 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4257 | v->Synchronize("globalhandles"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4258 | |
| 4259 | // Iterate over pointers being held by inactive threads. |
| 4260 | ThreadManager::Iterate(v); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4261 | v->Synchronize("threadmanager"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4262 | |
| 4263 | // Iterate over the pointers the Serialization/Deserialization code is |
| 4264 | // holding. |
| 4265 | // During garbage collection this keeps the partial snapshot cache alive. |
| 4266 | // During deserialization of the startup snapshot this creates the partial |
| 4267 | // snapshot cache and deserializes the objects it refers to. During |
| 4268 | // serialization this does nothing, since the partial snapshot cache is |
| 4269 | // empty. However the next thing we do is create the partial snapshot, |
| 4270 | // filling up the partial snapshot cache with objects it needs as we go. |
| 4271 | SerializerDeserializer::Iterate(v); |
| 4272 | // We don't do a v->Synchronize call here, because in debug mode that will |
| 4273 | // output a flag to the snapshot. However at this point the serializer and |
| 4274 | // deserializer are deliberately a little unsynchronized (see above) so the |
| 4275 | // checking of the sync flag in the snapshot would fail. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4276 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4277 | |
| 4278 | |
| 4279 | // Flag is set when the heap has been configured. The heap can be repeatedly |
| 4280 | // configured through the API until it is setup. |
| 4281 | static bool heap_configured = false; |
| 4282 | |
| 4283 | // TODO(1236194): Since the heap size is configurable on the command line |
| 4284 | // and through the API, we should gracefully handle the case that the heap |
| 4285 | // size is not big enough to fit all the initial objects. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4286 | bool Heap::ConfigureHeap(int max_semispace_size, int max_old_gen_size) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4287 | if (HasBeenSetup()) return false; |
| 4288 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4289 | if (max_semispace_size > 0) max_semispace_size_ = max_semispace_size; |
| 4290 | |
| 4291 | if (Snapshot::IsEnabled()) { |
| 4292 | // If we are using a snapshot we always reserve the default amount |
| 4293 | // of memory for each semispace because code in the snapshot has |
| 4294 | // write-barrier code that relies on the size and alignment of new |
| 4295 | // space. We therefore cannot use a larger max semispace size |
| 4296 | // than the default reserved semispace size. |
| 4297 | if (max_semispace_size_ > reserved_semispace_size_) { |
| 4298 | max_semispace_size_ = reserved_semispace_size_; |
| 4299 | } |
| 4300 | } else { |
| 4301 | // If we are not using snapshots we reserve space for the actual |
| 4302 | // max semispace size. |
| 4303 | reserved_semispace_size_ = max_semispace_size_; |
| 4304 | } |
| 4305 | |
| 4306 | if (max_old_gen_size > 0) max_old_generation_size_ = max_old_gen_size; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4307 | |
| 4308 | // The new space size must be a power of two to support single-bit testing |
| 4309 | // for containment. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4310 | max_semispace_size_ = RoundUpToPowerOf2(max_semispace_size_); |
| 4311 | reserved_semispace_size_ = RoundUpToPowerOf2(reserved_semispace_size_); |
| 4312 | initial_semispace_size_ = Min(initial_semispace_size_, max_semispace_size_); |
| 4313 | external_allocation_limit_ = 10 * max_semispace_size_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4314 | |
| 4315 | // The old generation is paged. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4316 | max_old_generation_size_ = RoundUp(max_old_generation_size_, Page::kPageSize); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4317 | |
| 4318 | heap_configured = true; |
| 4319 | return true; |
| 4320 | } |
| 4321 | |
| 4322 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4323 | bool Heap::ConfigureHeapDefault() { |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 4324 | return ConfigureHeap( |
| 4325 | FLAG_max_new_space_size * (KB / 2), FLAG_max_old_space_size * MB); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
| 4328 | |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4329 | void Heap::RecordStats(HeapStats* stats, bool take_snapshot) { |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 4330 | *stats->start_marker = HeapStats::kStartMarker; |
| 4331 | *stats->end_marker = HeapStats::kEndMarker; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 4332 | *stats->new_space_size = new_space_.SizeAsInt(); |
| 4333 | *stats->new_space_capacity = static_cast<int>(new_space_.Capacity()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 4334 | *stats->old_pointer_space_size = old_pointer_space_->Size(); |
| 4335 | *stats->old_pointer_space_capacity = old_pointer_space_->Capacity(); |
| 4336 | *stats->old_data_space_size = old_data_space_->Size(); |
| 4337 | *stats->old_data_space_capacity = old_data_space_->Capacity(); |
| 4338 | *stats->code_space_size = code_space_->Size(); |
| 4339 | *stats->code_space_capacity = code_space_->Capacity(); |
| 4340 | *stats->map_space_size = map_space_->Size(); |
| 4341 | *stats->map_space_capacity = map_space_->Capacity(); |
| 4342 | *stats->cell_space_size = cell_space_->Size(); |
| 4343 | *stats->cell_space_capacity = cell_space_->Capacity(); |
| 4344 | *stats->lo_space_size = lo_space_->Size(); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 4345 | GlobalHandles::RecordStats(stats); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4346 | *stats->memory_allocator_size = MemoryAllocator::Size(); |
| 4347 | *stats->memory_allocator_capacity = |
| 4348 | MemoryAllocator::Size() + MemoryAllocator::Available(); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 4349 | *stats->os_error = OS::GetLastError(); |
lrn@chromium.org | c4e51ac | 2010-08-09 09:47:21 +0000 | [diff] [blame] | 4350 | if (take_snapshot) { |
| 4351 | HeapIterator iterator; |
| 4352 | for (HeapObject* obj = iterator.next(); |
| 4353 | obj != NULL; |
| 4354 | obj = iterator.next()) { |
| 4355 | // Note: snapshot won't be precise because IsFreeListNode returns true |
| 4356 | // for any bytearray. |
| 4357 | if (FreeListNode::IsFreeListNode(obj)) continue; |
| 4358 | InstanceType type = obj->map()->instance_type(); |
| 4359 | ASSERT(0 <= type && type <= LAST_TYPE); |
| 4360 | stats->objects_per_type[type]++; |
| 4361 | stats->size_per_type[type] += obj->Size(); |
| 4362 | } |
| 4363 | } |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 4367 | intptr_t Heap::PromotedSpaceSize() { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4368 | return old_pointer_space_->Size() |
| 4369 | + old_data_space_->Size() |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4370 | + code_space_->Size() |
| 4371 | + map_space_->Size() |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4372 | + cell_space_->Size() |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4373 | + lo_space_->Size(); |
| 4374 | } |
| 4375 | |
| 4376 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4377 | int Heap::PromotedExternalMemorySize() { |
| 4378 | if (amount_of_external_allocated_memory_ |
| 4379 | <= amount_of_external_allocated_memory_at_last_global_gc_) return 0; |
| 4380 | return amount_of_external_allocated_memory_ |
| 4381 | - amount_of_external_allocated_memory_at_last_global_gc_; |
| 4382 | } |
| 4383 | |
| 4384 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4385 | bool Heap::Setup(bool create_heap_objects) { |
| 4386 | // Initialize heap spaces and initial maps and objects. Whenever something |
| 4387 | // goes wrong, just return false. The caller should check the results and |
| 4388 | // call Heap::TearDown() to release allocated memory. |
| 4389 | // |
| 4390 | // If the heap is not yet configured (eg, through the API), configure it. |
| 4391 | // Configuration is based on the flags new-space-size (really the semispace |
| 4392 | // size) and old-space-size if set or the initial values of semispace_size_ |
| 4393 | // and old_generation_size_ otherwise. |
| 4394 | if (!heap_configured) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4395 | if (!ConfigureHeapDefault()) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 4398 | ScavengingVisitor::Initialize(); |
| 4399 | NewSpaceScavenger::Initialize(); |
| 4400 | MarkCompactCollector::Initialize(); |
| 4401 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 4402 | MarkMapPointersAsEncoded(false); |
| 4403 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4404 | // Setup memory allocator and reserve a chunk of memory for new |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4405 | // space. The chunk is double the size of the requested reserved |
| 4406 | // new space size to ensure that we can find a pair of semispaces that |
| 4407 | // are contiguous and aligned to their size. |
| 4408 | if (!MemoryAllocator::Setup(MaxReserved())) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4409 | void* chunk = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4410 | MemoryAllocator::ReserveInitialChunk(4 * reserved_semispace_size_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4411 | if (chunk == NULL) return false; |
| 4412 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4413 | // Align the pair of semispaces to their size, which must be a power |
| 4414 | // of 2. |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4415 | Address new_space_start = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4416 | RoundUp(reinterpret_cast<byte*>(chunk), 2 * reserved_semispace_size_); |
| 4417 | if (!new_space_.Setup(new_space_start, 2 * reserved_semispace_size_)) { |
| 4418 | return false; |
| 4419 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4420 | |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4421 | // Initialize old pointer space. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4422 | old_pointer_space_ = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4423 | new OldSpace(max_old_generation_size_, OLD_POINTER_SPACE, NOT_EXECUTABLE); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4424 | if (old_pointer_space_ == NULL) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4425 | if (!old_pointer_space_->Setup(NULL, 0)) return false; |
| 4426 | |
| 4427 | // Initialize old data space. |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4428 | old_data_space_ = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4429 | new OldSpace(max_old_generation_size_, OLD_DATA_SPACE, NOT_EXECUTABLE); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4430 | if (old_data_space_ == NULL) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4431 | if (!old_data_space_->Setup(NULL, 0)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4432 | |
| 4433 | // Initialize the code space, set its maximum capacity to the old |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4434 | // generation size. It needs executable memory. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 4435 | // On 64-bit platform(s), we put all code objects in a 2 GB range of |
| 4436 | // virtual address space, so that they can call each other with near calls. |
| 4437 | if (code_range_size_ > 0) { |
| 4438 | if (!CodeRange::Setup(code_range_size_)) { |
| 4439 | return false; |
| 4440 | } |
| 4441 | } |
| 4442 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4443 | code_space_ = |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4444 | new OldSpace(max_old_generation_size_, CODE_SPACE, EXECUTABLE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4445 | if (code_space_ == NULL) return false; |
ager@chromium.org | a1645e2 | 2009-09-09 19:27:10 +0000 | [diff] [blame] | 4446 | if (!code_space_->Setup(NULL, 0)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4447 | |
| 4448 | // Initialize map space. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4449 | map_space_ = new MapSpace(FLAG_use_big_map_space |
| 4450 | ? max_old_generation_size_ |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 4451 | : MapSpace::kMaxMapPageIndex * Page::kPageSize, |
| 4452 | FLAG_max_map_space_pages, |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 4453 | MAP_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4454 | if (map_space_ == NULL) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4455 | if (!map_space_->Setup(NULL, 0)) return false; |
| 4456 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4457 | // Initialize global property cell space. |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4458 | cell_space_ = new CellSpace(max_old_generation_size_, CELL_SPACE); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4459 | if (cell_space_ == NULL) return false; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4460 | if (!cell_space_->Setup(NULL, 0)) return false; |
| 4461 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4462 | // The large object code space may contain code or data. We set the memory |
| 4463 | // to be non-executable here for safety, but this means we need to enable it |
| 4464 | // explicitly when allocating large code objects. |
| 4465 | lo_space_ = new LargeObjectSpace(LO_SPACE); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4466 | if (lo_space_ == NULL) return false; |
| 4467 | if (!lo_space_->Setup()) return false; |
| 4468 | |
| 4469 | if (create_heap_objects) { |
| 4470 | // Create initial maps. |
| 4471 | if (!CreateInitialMaps()) return false; |
| 4472 | if (!CreateApiObjects()) return false; |
| 4473 | |
| 4474 | // Create initial objects |
| 4475 | if (!CreateInitialObjects()) return false; |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 4476 | |
| 4477 | global_contexts_list_ = undefined_value(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 4480 | LOG(IntPtrTEvent("heap-capacity", Capacity())); |
| 4481 | LOG(IntPtrTEvent("heap-available", Available())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4482 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4483 | #ifdef ENABLE_LOGGING_AND_PROFILING |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4484 | // This should be called only after initial objects have been created. |
| 4485 | ProducerHeapProfile::Setup(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 4486 | #endif |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4487 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4488 | return true; |
| 4489 | } |
| 4490 | |
| 4491 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4492 | void Heap::SetStackLimits() { |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4493 | // On 64 bit machines, pointers are generally out of range of Smis. We write |
| 4494 | // something that looks like an out of range Smi to the GC. |
| 4495 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4496 | // Set up the special root array entries containing the stack limits. |
| 4497 | // These are actually addresses, but the tag makes the GC ignore it. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4498 | roots_[kStackLimitRootIndex] = |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4499 | reinterpret_cast<Object*>( |
| 4500 | (StackGuard::jslimit() & ~kSmiTagMask) | kSmiTag); |
| 4501 | roots_[kRealStackLimitRootIndex] = |
| 4502 | reinterpret_cast<Object*>( |
| 4503 | (StackGuard::real_jslimit() & ~kSmiTagMask) | kSmiTag); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4507 | void Heap::TearDown() { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4508 | if (FLAG_print_cumulative_gc_stat) { |
| 4509 | PrintF("\n\n"); |
| 4510 | PrintF("gc_count=%d ", gc_count_); |
| 4511 | PrintF("mark_sweep_count=%d ", ms_count_); |
| 4512 | PrintF("mark_compact_count=%d ", mc_count_); |
| 4513 | PrintF("max_gc_pause=%d ", GCTracer::get_max_gc_pause()); |
| 4514 | PrintF("min_in_mutator=%d ", GCTracer::get_min_in_mutator()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 4515 | PrintF("max_alive_after_gc=%" V8_PTR_PREFIX "d ", |
| 4516 | GCTracer::get_max_alive_after_gc()); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4517 | PrintF("\n\n"); |
| 4518 | } |
| 4519 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4520 | GlobalHandles::TearDown(); |
| 4521 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4522 | ExternalStringTable::TearDown(); |
| 4523 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 4524 | new_space_.TearDown(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4525 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4526 | if (old_pointer_space_ != NULL) { |
| 4527 | old_pointer_space_->TearDown(); |
| 4528 | delete old_pointer_space_; |
| 4529 | old_pointer_space_ = NULL; |
| 4530 | } |
| 4531 | |
| 4532 | if (old_data_space_ != NULL) { |
| 4533 | old_data_space_->TearDown(); |
| 4534 | delete old_data_space_; |
| 4535 | old_data_space_ = NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | if (code_space_ != NULL) { |
| 4539 | code_space_->TearDown(); |
| 4540 | delete code_space_; |
| 4541 | code_space_ = NULL; |
| 4542 | } |
| 4543 | |
| 4544 | if (map_space_ != NULL) { |
| 4545 | map_space_->TearDown(); |
| 4546 | delete map_space_; |
| 4547 | map_space_ = NULL; |
| 4548 | } |
| 4549 | |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4550 | if (cell_space_ != NULL) { |
| 4551 | cell_space_->TearDown(); |
| 4552 | delete cell_space_; |
| 4553 | cell_space_ = NULL; |
| 4554 | } |
| 4555 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4556 | if (lo_space_ != NULL) { |
| 4557 | lo_space_->TearDown(); |
| 4558 | delete lo_space_; |
| 4559 | lo_space_ = NULL; |
| 4560 | } |
| 4561 | |
| 4562 | MemoryAllocator::TearDown(); |
| 4563 | } |
| 4564 | |
| 4565 | |
| 4566 | void Heap::Shrink() { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4567 | // Try to shrink all paged spaces. |
| 4568 | PagedSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4569 | for (PagedSpace* space = spaces.next(); space != NULL; space = spaces.next()) |
| 4570 | space->Shrink(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 4574 | #ifdef ENABLE_HEAP_PROTECTION |
| 4575 | |
| 4576 | void Heap::Protect() { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4577 | if (HasBeenSetup()) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4578 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4579 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 4580 | space->Protect(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4581 | } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 4582 | } |
| 4583 | |
| 4584 | |
| 4585 | void Heap::Unprotect() { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4586 | if (HasBeenSetup()) { |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4587 | AllSpaces spaces; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4588 | for (Space* space = spaces.next(); space != NULL; space = spaces.next()) |
| 4589 | space->Unprotect(); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4590 | } |
kasperl@chromium.org | f5aa837 | 2009-03-24 14:47:14 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | #endif |
| 4594 | |
| 4595 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 4596 | void Heap::AddGCPrologueCallback(GCPrologueCallback callback, GCType gc_type) { |
| 4597 | ASSERT(callback != NULL); |
| 4598 | GCPrologueCallbackPair pair(callback, gc_type); |
| 4599 | ASSERT(!gc_prologue_callbacks_.Contains(pair)); |
| 4600 | return gc_prologue_callbacks_.Add(pair); |
| 4601 | } |
| 4602 | |
| 4603 | |
| 4604 | void Heap::RemoveGCPrologueCallback(GCPrologueCallback callback) { |
| 4605 | ASSERT(callback != NULL); |
| 4606 | for (int i = 0; i < gc_prologue_callbacks_.length(); ++i) { |
| 4607 | if (gc_prologue_callbacks_[i].callback == callback) { |
| 4608 | gc_prologue_callbacks_.Remove(i); |
| 4609 | return; |
| 4610 | } |
| 4611 | } |
| 4612 | UNREACHABLE(); |
| 4613 | } |
| 4614 | |
| 4615 | |
| 4616 | void Heap::AddGCEpilogueCallback(GCEpilogueCallback callback, GCType gc_type) { |
| 4617 | ASSERT(callback != NULL); |
| 4618 | GCEpilogueCallbackPair pair(callback, gc_type); |
| 4619 | ASSERT(!gc_epilogue_callbacks_.Contains(pair)); |
| 4620 | return gc_epilogue_callbacks_.Add(pair); |
| 4621 | } |
| 4622 | |
| 4623 | |
| 4624 | void Heap::RemoveGCEpilogueCallback(GCEpilogueCallback callback) { |
| 4625 | ASSERT(callback != NULL); |
| 4626 | for (int i = 0; i < gc_epilogue_callbacks_.length(); ++i) { |
| 4627 | if (gc_epilogue_callbacks_[i].callback == callback) { |
| 4628 | gc_epilogue_callbacks_.Remove(i); |
| 4629 | return; |
| 4630 | } |
| 4631 | } |
| 4632 | UNREACHABLE(); |
| 4633 | } |
| 4634 | |
| 4635 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4636 | #ifdef DEBUG |
| 4637 | |
| 4638 | class PrintHandleVisitor: public ObjectVisitor { |
| 4639 | public: |
| 4640 | void VisitPointers(Object** start, Object** end) { |
| 4641 | for (Object** p = start; p < end; p++) |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 4642 | PrintF(" handle %p to %p\n", |
| 4643 | reinterpret_cast<void*>(p), |
| 4644 | reinterpret_cast<void*>(*p)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4645 | } |
| 4646 | }; |
| 4647 | |
| 4648 | void Heap::PrintHandles() { |
| 4649 | PrintF("Handles:\n"); |
| 4650 | PrintHandleVisitor v; |
| 4651 | HandleScopeImplementer::Iterate(&v); |
| 4652 | } |
| 4653 | |
| 4654 | #endif |
| 4655 | |
| 4656 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4657 | Space* AllSpaces::next() { |
| 4658 | switch (counter_++) { |
| 4659 | case NEW_SPACE: |
| 4660 | return Heap::new_space(); |
| 4661 | case OLD_POINTER_SPACE: |
| 4662 | return Heap::old_pointer_space(); |
| 4663 | case OLD_DATA_SPACE: |
| 4664 | return Heap::old_data_space(); |
| 4665 | case CODE_SPACE: |
| 4666 | return Heap::code_space(); |
| 4667 | case MAP_SPACE: |
| 4668 | return Heap::map_space(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4669 | case CELL_SPACE: |
| 4670 | return Heap::cell_space(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4671 | case LO_SPACE: |
| 4672 | return Heap::lo_space(); |
| 4673 | default: |
| 4674 | return NULL; |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | |
| 4679 | PagedSpace* PagedSpaces::next() { |
| 4680 | switch (counter_++) { |
| 4681 | case OLD_POINTER_SPACE: |
| 4682 | return Heap::old_pointer_space(); |
| 4683 | case OLD_DATA_SPACE: |
| 4684 | return Heap::old_data_space(); |
| 4685 | case CODE_SPACE: |
| 4686 | return Heap::code_space(); |
| 4687 | case MAP_SPACE: |
| 4688 | return Heap::map_space(); |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4689 | case CELL_SPACE: |
| 4690 | return Heap::cell_space(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4691 | default: |
| 4692 | return NULL; |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | |
| 4697 | |
| 4698 | OldSpace* OldSpaces::next() { |
| 4699 | switch (counter_++) { |
| 4700 | case OLD_POINTER_SPACE: |
| 4701 | return Heap::old_pointer_space(); |
| 4702 | case OLD_DATA_SPACE: |
| 4703 | return Heap::old_data_space(); |
| 4704 | case CODE_SPACE: |
| 4705 | return Heap::code_space(); |
| 4706 | default: |
| 4707 | return NULL; |
| 4708 | } |
| 4709 | } |
| 4710 | |
| 4711 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4712 | SpaceIterator::SpaceIterator() : current_space_(FIRST_SPACE), iterator_(NULL) { |
| 4713 | } |
| 4714 | |
| 4715 | |
| 4716 | SpaceIterator::~SpaceIterator() { |
| 4717 | // Delete active iterator if any. |
| 4718 | delete iterator_; |
| 4719 | } |
| 4720 | |
| 4721 | |
| 4722 | bool SpaceIterator::has_next() { |
| 4723 | // Iterate until no more spaces. |
| 4724 | return current_space_ != LAST_SPACE; |
| 4725 | } |
| 4726 | |
| 4727 | |
| 4728 | ObjectIterator* SpaceIterator::next() { |
| 4729 | if (iterator_ != NULL) { |
| 4730 | delete iterator_; |
| 4731 | iterator_ = NULL; |
| 4732 | // Move to the next space |
| 4733 | current_space_++; |
| 4734 | if (current_space_ > LAST_SPACE) { |
| 4735 | return NULL; |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | // Return iterator for the new current space. |
| 4740 | return CreateIterator(); |
| 4741 | } |
| 4742 | |
| 4743 | |
| 4744 | // Create an iterator for the space to iterate. |
| 4745 | ObjectIterator* SpaceIterator::CreateIterator() { |
| 4746 | ASSERT(iterator_ == NULL); |
| 4747 | |
| 4748 | switch (current_space_) { |
| 4749 | case NEW_SPACE: |
| 4750 | iterator_ = new SemiSpaceIterator(Heap::new_space()); |
| 4751 | break; |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 4752 | case OLD_POINTER_SPACE: |
| 4753 | iterator_ = new HeapObjectIterator(Heap::old_pointer_space()); |
| 4754 | break; |
| 4755 | case OLD_DATA_SPACE: |
| 4756 | iterator_ = new HeapObjectIterator(Heap::old_data_space()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4757 | break; |
| 4758 | case CODE_SPACE: |
| 4759 | iterator_ = new HeapObjectIterator(Heap::code_space()); |
| 4760 | break; |
| 4761 | case MAP_SPACE: |
| 4762 | iterator_ = new HeapObjectIterator(Heap::map_space()); |
| 4763 | break; |
kasperl@chromium.org | defbd10 | 2009-07-13 14:04:26 +0000 | [diff] [blame] | 4764 | case CELL_SPACE: |
| 4765 | iterator_ = new HeapObjectIterator(Heap::cell_space()); |
| 4766 | break; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 4767 | case LO_SPACE: |
| 4768 | iterator_ = new LargeObjectIterator(Heap::lo_space()); |
| 4769 | break; |
| 4770 | } |
| 4771 | |
| 4772 | // Return the newly allocated iterator; |
| 4773 | ASSERT(iterator_ != NULL); |
| 4774 | return iterator_; |
| 4775 | } |
| 4776 | |
| 4777 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4778 | HeapIterator::HeapIterator() { |
| 4779 | Init(); |
| 4780 | } |
| 4781 | |
| 4782 | |
| 4783 | HeapIterator::~HeapIterator() { |
| 4784 | Shutdown(); |
| 4785 | } |
| 4786 | |
| 4787 | |
| 4788 | void HeapIterator::Init() { |
| 4789 | // Start the iteration. |
| 4790 | space_iterator_ = new SpaceIterator(); |
| 4791 | object_iterator_ = space_iterator_->next(); |
| 4792 | } |
| 4793 | |
| 4794 | |
| 4795 | void HeapIterator::Shutdown() { |
| 4796 | // Make sure the last iterator is deallocated. |
| 4797 | delete space_iterator_; |
| 4798 | space_iterator_ = NULL; |
| 4799 | object_iterator_ = NULL; |
| 4800 | } |
| 4801 | |
| 4802 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4803 | HeapObject* HeapIterator::next() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4804 | // No iterator means we are done. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4805 | if (object_iterator_ == NULL) return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4806 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4807 | if (HeapObject* obj = object_iterator_->next_object()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4808 | // If the current iterator has more objects we are fine. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4809 | return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4810 | } else { |
| 4811 | // Go though the spaces looking for one that has objects. |
| 4812 | while (space_iterator_->has_next()) { |
| 4813 | object_iterator_ = space_iterator_->next(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4814 | if (HeapObject* obj = object_iterator_->next_object()) { |
| 4815 | return obj; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4816 | } |
| 4817 | } |
| 4818 | } |
| 4819 | // Done with the last space. |
| 4820 | object_iterator_ = NULL; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 4821 | return NULL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | |
| 4825 | void HeapIterator::reset() { |
| 4826 | // Restart the iterator. |
| 4827 | Shutdown(); |
| 4828 | Init(); |
| 4829 | } |
| 4830 | |
| 4831 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4832 | #ifdef DEBUG |
| 4833 | |
| 4834 | static bool search_for_any_global; |
| 4835 | static Object* search_target; |
| 4836 | static bool found_target; |
| 4837 | static List<Object*> object_stack(20); |
| 4838 | |
| 4839 | |
| 4840 | // Tags 0, 1, and 3 are used. Use 2 for marking visited HeapObject. |
| 4841 | static const int kMarkTag = 2; |
| 4842 | |
| 4843 | static void MarkObjectRecursively(Object** p); |
| 4844 | class MarkObjectVisitor : public ObjectVisitor { |
| 4845 | public: |
| 4846 | void VisitPointers(Object** start, Object** end) { |
| 4847 | // Copy all HeapObject pointers in [start, end) |
| 4848 | for (Object** p = start; p < end; p++) { |
| 4849 | if ((*p)->IsHeapObject()) |
| 4850 | MarkObjectRecursively(p); |
| 4851 | } |
| 4852 | } |
| 4853 | }; |
| 4854 | |
| 4855 | static MarkObjectVisitor mark_visitor; |
| 4856 | |
| 4857 | static void MarkObjectRecursively(Object** p) { |
| 4858 | if (!(*p)->IsHeapObject()) return; |
| 4859 | |
| 4860 | HeapObject* obj = HeapObject::cast(*p); |
| 4861 | |
| 4862 | Object* map = obj->map(); |
| 4863 | |
| 4864 | if (!map->IsHeapObject()) return; // visited before |
| 4865 | |
| 4866 | if (found_target) return; // stop if target found |
| 4867 | object_stack.Add(obj); |
| 4868 | if ((search_for_any_global && obj->IsJSGlobalObject()) || |
| 4869 | (!search_for_any_global && (obj == search_target))) { |
| 4870 | found_target = true; |
| 4871 | return; |
| 4872 | } |
| 4873 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4874 | // not visited yet |
| 4875 | Map* map_p = reinterpret_cast<Map*>(HeapObject::cast(map)); |
| 4876 | |
| 4877 | Address map_addr = map_p->address(); |
| 4878 | |
| 4879 | obj->set_map(reinterpret_cast<Map*>(map_addr + kMarkTag)); |
| 4880 | |
| 4881 | MarkObjectRecursively(&map); |
| 4882 | |
| 4883 | obj->IterateBody(map_p->instance_type(), obj->SizeFromMap(map_p), |
| 4884 | &mark_visitor); |
| 4885 | |
| 4886 | if (!found_target) // don't pop if found the target |
| 4887 | object_stack.RemoveLast(); |
| 4888 | } |
| 4889 | |
| 4890 | |
| 4891 | static void UnmarkObjectRecursively(Object** p); |
| 4892 | class UnmarkObjectVisitor : public ObjectVisitor { |
| 4893 | public: |
| 4894 | void VisitPointers(Object** start, Object** end) { |
| 4895 | // Copy all HeapObject pointers in [start, end) |
| 4896 | for (Object** p = start; p < end; p++) { |
| 4897 | if ((*p)->IsHeapObject()) |
| 4898 | UnmarkObjectRecursively(p); |
| 4899 | } |
| 4900 | } |
| 4901 | }; |
| 4902 | |
| 4903 | static UnmarkObjectVisitor unmark_visitor; |
| 4904 | |
| 4905 | static void UnmarkObjectRecursively(Object** p) { |
| 4906 | if (!(*p)->IsHeapObject()) return; |
| 4907 | |
| 4908 | HeapObject* obj = HeapObject::cast(*p); |
| 4909 | |
| 4910 | Object* map = obj->map(); |
| 4911 | |
| 4912 | if (map->IsHeapObject()) return; // unmarked already |
| 4913 | |
| 4914 | Address map_addr = reinterpret_cast<Address>(map); |
| 4915 | |
| 4916 | map_addr -= kMarkTag; |
| 4917 | |
| 4918 | ASSERT_TAG_ALIGNED(map_addr); |
| 4919 | |
| 4920 | HeapObject* map_p = HeapObject::FromAddress(map_addr); |
| 4921 | |
| 4922 | obj->set_map(reinterpret_cast<Map*>(map_p)); |
| 4923 | |
| 4924 | UnmarkObjectRecursively(reinterpret_cast<Object**>(&map_p)); |
| 4925 | |
| 4926 | obj->IterateBody(Map::cast(map_p)->instance_type(), |
| 4927 | obj->SizeFromMap(Map::cast(map_p)), |
| 4928 | &unmark_visitor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | |
| 4932 | static void MarkRootObjectRecursively(Object** root) { |
| 4933 | if (search_for_any_global) { |
| 4934 | ASSERT(search_target == NULL); |
| 4935 | } else { |
| 4936 | ASSERT(search_target->IsHeapObject()); |
| 4937 | } |
| 4938 | found_target = false; |
| 4939 | object_stack.Clear(); |
| 4940 | |
| 4941 | MarkObjectRecursively(root); |
| 4942 | UnmarkObjectRecursively(root); |
| 4943 | |
| 4944 | if (found_target) { |
| 4945 | PrintF("=====================================\n"); |
| 4946 | PrintF("==== Path to object ====\n"); |
| 4947 | PrintF("=====================================\n\n"); |
| 4948 | |
| 4949 | ASSERT(!object_stack.is_empty()); |
| 4950 | for (int i = 0; i < object_stack.length(); i++) { |
| 4951 | if (i > 0) PrintF("\n |\n |\n V\n\n"); |
| 4952 | Object* obj = object_stack[i]; |
| 4953 | obj->Print(); |
| 4954 | } |
| 4955 | PrintF("=====================================\n"); |
| 4956 | } |
| 4957 | } |
| 4958 | |
| 4959 | |
| 4960 | // Helper class for visiting HeapObjects recursively. |
| 4961 | class MarkRootVisitor: public ObjectVisitor { |
| 4962 | public: |
| 4963 | void VisitPointers(Object** start, Object** end) { |
| 4964 | // Visit all HeapObject pointers in [start, end) |
| 4965 | for (Object** p = start; p < end; p++) { |
| 4966 | if ((*p)->IsHeapObject()) |
| 4967 | MarkRootObjectRecursively(p); |
| 4968 | } |
| 4969 | } |
| 4970 | }; |
| 4971 | |
| 4972 | |
| 4973 | // Triggers a depth-first traversal of reachable objects from roots |
| 4974 | // and finds a path to a specific heap object and prints it. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 4975 | void Heap::TracePathToObject(Object* target) { |
| 4976 | search_target = target; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4977 | search_for_any_global = false; |
| 4978 | |
| 4979 | MarkRootVisitor root_visitor; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4980 | IterateRoots(&root_visitor, VISIT_ONLY_STRONG); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | |
| 4984 | // Triggers a depth-first traversal of reachable objects from roots |
| 4985 | // and finds a path to any global object and prints it. Useful for |
| 4986 | // determining the source for leaks of global objects. |
| 4987 | void Heap::TracePathToGlobal() { |
| 4988 | search_target = NULL; |
| 4989 | search_for_any_global = true; |
| 4990 | |
| 4991 | MarkRootVisitor root_visitor; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 4992 | IterateRoots(&root_visitor, VISIT_ONLY_STRONG); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 4993 | } |
| 4994 | #endif |
| 4995 | |
| 4996 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 4997 | static intptr_t CountTotalHolesSize() { |
| 4998 | intptr_t holes_size = 0; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 4999 | OldSpaces spaces; |
| 5000 | for (OldSpace* space = spaces.next(); |
| 5001 | space != NULL; |
| 5002 | space = spaces.next()) { |
| 5003 | holes_size += space->Waste() + space->AvailableFree(); |
| 5004 | } |
| 5005 | return holes_size; |
| 5006 | } |
| 5007 | |
| 5008 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5009 | GCTracer::GCTracer() |
| 5010 | : start_time_(0.0), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5011 | start_size_(0), |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5012 | gc_count_(0), |
| 5013 | full_gc_count_(0), |
| 5014 | is_compacting_(false), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5015 | marked_count_(0), |
| 5016 | allocated_since_last_gc_(0), |
| 5017 | spent_in_mutator_(0), |
| 5018 | promoted_objects_size_(0) { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5019 | // These two fields reflect the state of the previous full collection. |
| 5020 | // Set them before they are changed by the collector. |
| 5021 | previous_has_compacted_ = MarkCompactCollector::HasCompacted(); |
| 5022 | previous_marked_count_ = MarkCompactCollector::previous_marked_count(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5023 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5024 | start_time_ = OS::TimeCurrentMillis(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5025 | start_size_ = Heap::SizeOfObjects(); |
| 5026 | |
| 5027 | for (int i = 0; i < Scope::kNumberOfScopes; i++) { |
| 5028 | scopes_[i] = 0; |
| 5029 | } |
| 5030 | |
| 5031 | in_free_list_or_wasted_before_gc_ = CountTotalHolesSize(); |
| 5032 | |
| 5033 | allocated_since_last_gc_ = Heap::SizeOfObjects() - alive_after_last_gc_; |
| 5034 | |
| 5035 | if (last_gc_end_timestamp_ > 0) { |
| 5036 | spent_in_mutator_ = Max(start_time_ - last_gc_end_timestamp_, 0.0); |
| 5037 | } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | |
| 5041 | GCTracer::~GCTracer() { |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5042 | // Printf ONE line iff flag is set. |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5043 | if (!FLAG_trace_gc && !FLAG_print_cumulative_gc_stat) return; |
| 5044 | |
| 5045 | bool first_gc = (last_gc_end_timestamp_ == 0); |
| 5046 | |
| 5047 | alive_after_last_gc_ = Heap::SizeOfObjects(); |
| 5048 | last_gc_end_timestamp_ = OS::TimeCurrentMillis(); |
| 5049 | |
| 5050 | int time = static_cast<int>(last_gc_end_timestamp_ - start_time_); |
| 5051 | |
| 5052 | // Update cumulative GC statistics if required. |
| 5053 | if (FLAG_print_cumulative_gc_stat) { |
| 5054 | max_gc_pause_ = Max(max_gc_pause_, time); |
| 5055 | max_alive_after_gc_ = Max(max_alive_after_gc_, alive_after_last_gc_); |
| 5056 | if (!first_gc) { |
| 5057 | min_in_mutator_ = Min(min_in_mutator_, |
| 5058 | static_cast<int>(spent_in_mutator_)); |
| 5059 | } |
| 5060 | } |
| 5061 | |
| 5062 | if (!FLAG_trace_gc_nvp) { |
| 5063 | int external_time = static_cast<int>(scopes_[Scope::EXTERNAL]); |
| 5064 | |
| 5065 | PrintF("%s %.1f -> %.1f MB, ", |
| 5066 | CollectorString(), |
| 5067 | static_cast<double>(start_size_) / MB, |
| 5068 | SizeOfHeapObjects()); |
| 5069 | |
| 5070 | if (external_time > 0) PrintF("%d / ", external_time); |
| 5071 | PrintF("%d ms.\n", time); |
| 5072 | } else { |
| 5073 | PrintF("pause=%d ", time); |
| 5074 | PrintF("mutator=%d ", |
| 5075 | static_cast<int>(spent_in_mutator_)); |
| 5076 | |
| 5077 | PrintF("gc="); |
| 5078 | switch (collector_) { |
| 5079 | case SCAVENGER: |
| 5080 | PrintF("s"); |
| 5081 | break; |
| 5082 | case MARK_COMPACTOR: |
| 5083 | PrintF(MarkCompactCollector::HasCompacted() ? "mc" : "ms"); |
| 5084 | break; |
| 5085 | default: |
| 5086 | UNREACHABLE(); |
| 5087 | } |
| 5088 | PrintF(" "); |
| 5089 | |
| 5090 | PrintF("external=%d ", static_cast<int>(scopes_[Scope::EXTERNAL])); |
| 5091 | PrintF("mark=%d ", static_cast<int>(scopes_[Scope::MC_MARK])); |
| 5092 | PrintF("sweep=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP])); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 5093 | PrintF("sweepns=%d ", static_cast<int>(scopes_[Scope::MC_SWEEP_NEWSPACE])); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5094 | PrintF("compact=%d ", static_cast<int>(scopes_[Scope::MC_COMPACT])); |
| 5095 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5096 | PrintF("total_size_before=%" V8_PTR_PREFIX "d ", start_size_); |
| 5097 | PrintF("total_size_after=%" V8_PTR_PREFIX "d ", Heap::SizeOfObjects()); |
| 5098 | PrintF("holes_size_before=%" V8_PTR_PREFIX "d ", |
| 5099 | in_free_list_or_wasted_before_gc_); |
| 5100 | PrintF("holes_size_after=%" V8_PTR_PREFIX "d ", CountTotalHolesSize()); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5101 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5102 | PrintF("allocated=%" V8_PTR_PREFIX "d ", allocated_since_last_gc_); |
| 5103 | PrintF("promoted=%" V8_PTR_PREFIX "d ", promoted_objects_size_); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 5104 | |
| 5105 | PrintF("\n"); |
| 5106 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 5107 | |
| 5108 | #if defined(ENABLE_LOGGING_AND_PROFILING) |
| 5109 | Heap::PrintShortHeapStatistics(); |
| 5110 | #endif |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
| 5113 | |
| 5114 | const char* GCTracer::CollectorString() { |
| 5115 | switch (collector_) { |
| 5116 | case SCAVENGER: |
| 5117 | return "Scavenge"; |
| 5118 | case MARK_COMPACTOR: |
| 5119 | return MarkCompactCollector::HasCompacted() ? "Mark-compact" |
| 5120 | : "Mark-sweep"; |
| 5121 | } |
| 5122 | return "Unknown GC"; |
| 5123 | } |
| 5124 | |
| 5125 | |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5126 | int KeyedLookupCache::Hash(Map* map, String* name) { |
| 5127 | // Uses only lower 32 bits if pointers are larger. |
| 5128 | uintptr_t addr_hash = |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5129 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(map)) >> kMapHashShift; |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 5130 | return static_cast<uint32_t>((addr_hash ^ name->Hash()) & kCapacityMask); |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
| 5133 | |
| 5134 | int KeyedLookupCache::Lookup(Map* map, String* name) { |
| 5135 | int index = Hash(map, name); |
| 5136 | Key& key = keys_[index]; |
| 5137 | if ((key.map == map) && key.name->Equals(name)) { |
| 5138 | return field_offsets_[index]; |
| 5139 | } |
| 5140 | return -1; |
| 5141 | } |
| 5142 | |
| 5143 | |
| 5144 | void KeyedLookupCache::Update(Map* map, String* name, int field_offset) { |
| 5145 | String* symbol; |
| 5146 | if (Heap::LookupSymbolIfExists(name, &symbol)) { |
| 5147 | int index = Hash(map, symbol); |
| 5148 | Key& key = keys_[index]; |
| 5149 | key.map = map; |
| 5150 | key.name = symbol; |
| 5151 | field_offsets_[index] = field_offset; |
| 5152 | } |
| 5153 | } |
| 5154 | |
| 5155 | |
| 5156 | void KeyedLookupCache::Clear() { |
| 5157 | for (int index = 0; index < kLength; index++) keys_[index].map = NULL; |
| 5158 | } |
| 5159 | |
| 5160 | |
| 5161 | KeyedLookupCache::Key KeyedLookupCache::keys_[KeyedLookupCache::kLength]; |
| 5162 | |
| 5163 | |
| 5164 | int KeyedLookupCache::field_offsets_[KeyedLookupCache::kLength]; |
| 5165 | |
| 5166 | |
| 5167 | void DescriptorLookupCache::Clear() { |
| 5168 | for (int index = 0; index < kLength; index++) keys_[index].array = NULL; |
| 5169 | } |
| 5170 | |
| 5171 | |
| 5172 | DescriptorLookupCache::Key |
| 5173 | DescriptorLookupCache::keys_[DescriptorLookupCache::kLength]; |
| 5174 | |
| 5175 | int DescriptorLookupCache::results_[DescriptorLookupCache::kLength]; |
| 5176 | |
| 5177 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5178 | #ifdef DEBUG |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 5179 | void Heap::GarbageCollectionGreedyCheck() { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5180 | ASSERT(FLAG_gc_greedy); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 5181 | if (Bootstrapper::IsActive()) return; |
| 5182 | if (disallow_allocation_failure()) return; |
| 5183 | CollectGarbage(NEW_SPACE); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 5184 | } |
| 5185 | #endif |
| 5186 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 5187 | |
| 5188 | TranscendentalCache::TranscendentalCache(TranscendentalCache::Type t) |
| 5189 | : type_(t) { |
| 5190 | uint32_t in0 = 0xffffffffu; // Bit-pattern for a NaN that isn't |
| 5191 | uint32_t in1 = 0xffffffffu; // generated by the FPU. |
| 5192 | for (int i = 0; i < kCacheSize; i++) { |
| 5193 | elements_[i].in[0] = in0; |
| 5194 | elements_[i].in[1] = in1; |
| 5195 | elements_[i].output = NULL; |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | |
| 5200 | TranscendentalCache* TranscendentalCache::caches_[kNumberOfCaches]; |
| 5201 | |
| 5202 | |
| 5203 | void TranscendentalCache::Clear() { |
| 5204 | for (int i = 0; i < kNumberOfCaches; i++) { |
| 5205 | if (caches_[i] != NULL) { |
| 5206 | delete caches_[i]; |
| 5207 | caches_[i] = NULL; |
| 5208 | } |
| 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 5213 | void ExternalStringTable::CleanUp() { |
| 5214 | int last = 0; |
| 5215 | for (int i = 0; i < new_space_strings_.length(); ++i) { |
| 5216 | if (new_space_strings_[i] == Heap::raw_unchecked_null_value()) continue; |
| 5217 | if (Heap::InNewSpace(new_space_strings_[i])) { |
| 5218 | new_space_strings_[last++] = new_space_strings_[i]; |
| 5219 | } else { |
| 5220 | old_space_strings_.Add(new_space_strings_[i]); |
| 5221 | } |
| 5222 | } |
| 5223 | new_space_strings_.Rewind(last); |
| 5224 | last = 0; |
| 5225 | for (int i = 0; i < old_space_strings_.length(); ++i) { |
| 5226 | if (old_space_strings_[i] == Heap::raw_unchecked_null_value()) continue; |
| 5227 | ASSERT(!Heap::InNewSpace(old_space_strings_[i])); |
| 5228 | old_space_strings_[last++] = old_space_strings_[i]; |
| 5229 | } |
| 5230 | old_space_strings_.Rewind(last); |
| 5231 | Verify(); |
| 5232 | } |
| 5233 | |
| 5234 | |
| 5235 | void ExternalStringTable::TearDown() { |
| 5236 | new_space_strings_.Free(); |
| 5237 | old_space_strings_.Free(); |
| 5238 | } |
| 5239 | |
| 5240 | |
| 5241 | List<Object*> ExternalStringTable::new_space_strings_; |
| 5242 | List<Object*> ExternalStringTable::old_space_strings_; |
| 5243 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 5244 | } } // namespace v8::internal |