| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/heap.h" | 5 #include "vm/heap.h" |
| 6 | 6 |
| 7 #include "platform/assert.h" | 7 #include "platform/assert.h" |
| 8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
| 9 #include "vm/compiler_stats.h" | 9 #include "vm/compiler_stats.h" |
| 10 #include "vm/flags.h" | 10 #include "vm/flags.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 "Enables heap verification after GC."); | 25 "Enables heap verification after GC."); |
| 26 DEFINE_FLAG(bool, gc_at_alloc, false, "GC at every allocation."); | 26 DEFINE_FLAG(bool, gc_at_alloc, false, "GC at every allocation."); |
| 27 DEFINE_FLAG(int, new_gen_heap_size, 32, "new gen heap size in MB," | 27 DEFINE_FLAG(int, new_gen_heap_size, 32, "new gen heap size in MB," |
| 28 "e.g: --new_gen_heap_size=64 allocates a 64MB new gen heap"); | 28 "e.g: --new_gen_heap_size=64 allocates a 64MB new gen heap"); |
| 29 DEFINE_FLAG(int, old_gen_heap_size, Heap::kHeapSizeInMB, | 29 DEFINE_FLAG(int, old_gen_heap_size, Heap::kHeapSizeInMB, |
| 30 "old gen heap size in MB," | 30 "old gen heap size in MB," |
| 31 "e.g: --old_gen_heap_size=1024 allocates a 1024MB old gen heap"); | 31 "e.g: --old_gen_heap_size=1024 allocates a 1024MB old gen heap"); |
| 32 DEFINE_FLAG(int, code_heap_size, Heap::kCodeHeapSizeInMB, | 32 DEFINE_FLAG(int, code_heap_size, Heap::kCodeHeapSizeInMB, |
| 33 "code heap size in MB," | 33 "code heap size in MB," |
| 34 "e.g: --code_heap_size=8 allocates a 8MB code heap"); | 34 "e.g: --code_heap_size=8 allocates a 8MB code heap"); |
| 35 DEFINE_FLAG(int, stub_code_heap_size, Heap::kStubCodeHeapSizeInKB, | |
| 36 "stub code heap size in KB," | |
| 37 "e.g: --stub_code_heap_size=256 allocates a 256KB stub code heap"); | |
| 38 | 35 |
| 39 Heap::Heap() { | 36 Heap::Heap() { |
| 40 new_space_ = new Scavenger(this, | 37 new_space_ = new Scavenger(this, |
| 41 (FLAG_new_gen_heap_size * MB), | 38 (FLAG_new_gen_heap_size * MB), |
| 42 kNewObjectAlignmentOffset); | 39 kNewObjectAlignmentOffset); |
| 43 old_space_ = new PageSpace(this, (FLAG_old_gen_heap_size * MB)); | 40 old_space_ = new PageSpace(this, (FLAG_old_gen_heap_size * MB)); |
| 44 code_space_ = new PageSpace(this, (FLAG_code_heap_size * MB), true); | 41 code_space_ = new PageSpace(this, (FLAG_code_heap_size * MB), true); |
| 45 stub_code_space_ = new PageSpace(this, (FLAG_stub_code_heap_size * KB), true); | |
| 46 } | 42 } |
| 47 | 43 |
| 48 | 44 |
| 49 Heap::~Heap() { | 45 Heap::~Heap() { |
| 50 delete new_space_; | 46 delete new_space_; |
| 51 delete old_space_; | 47 delete old_space_; |
| 52 delete code_space_; | 48 delete code_space_; |
| 53 delete stub_code_space_; | |
| 54 } | 49 } |
| 55 | 50 |
| 56 | 51 |
| 57 uword Heap::AllocateNew(intptr_t size) { | 52 uword Heap::AllocateNew(intptr_t size) { |
| 58 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 53 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
| 59 uword addr = new_space_->TryAllocate(size); | 54 uword addr = new_space_->TryAllocate(size); |
| 60 if (addr != 0) { | 55 if (addr != 0) { |
| 61 return addr; | 56 return addr; |
| 62 } | 57 } |
| 63 CollectGarbage(kNew); | 58 CollectGarbage(kNew); |
| 64 if (FLAG_verbose_gc) { | 59 if (FLAG_verbose_gc) { |
| 65 OS::PrintErr("New space (%dk) Old space (%dk) " | 60 OS::PrintErr("New space (%dk) Old space (%dk) Code space (%dk)\n", |
| 66 "Code space (%dk) Stub Code space(%dk)\n", | |
| 67 (new_space_->in_use() / KB), | 61 (new_space_->in_use() / KB), |
| 68 (old_space_->in_use() / KB), | 62 (old_space_->in_use() / KB), |
| 69 (code_space_->in_use() / KB), | 63 (code_space_->in_use() / KB)); |
| 70 (stub_code_space_->in_use() / KB)); | |
| 71 } | 64 } |
| 72 addr = new_space_->TryAllocate(size); | 65 addr = new_space_->TryAllocate(size); |
| 73 if (addr != 0) { | 66 if (addr != 0) { |
| 74 return addr; | 67 return addr; |
| 75 } | 68 } |
| 76 return AllocateOld(size); | 69 return AllocateOld(size); |
| 77 } | 70 } |
| 78 | 71 |
| 79 | 72 |
| 80 uword Heap::AllocateOld(intptr_t size) { | 73 uword Heap::AllocateOld(intptr_t size) { |
| 81 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 74 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
| 82 uword addr = old_space_->TryAllocate(size); | 75 uword addr = old_space_->TryAllocate(size); |
| 83 if (addr == 0) { | 76 if (addr == 0) { |
| 84 CollectAllGarbage(); | 77 CollectAllGarbage(); |
| 85 if (FLAG_verbose_gc) { | 78 if (FLAG_verbose_gc) { |
| 86 OS::PrintErr("New space (%dk) Old space (%dk) " | 79 OS::PrintErr("New space (%dk) Old space (%dk) Code space (%dk)\n", |
| 87 "Code space (%dk) Stub Code space(%dk)\n", | |
| 88 (new_space_->in_use() / KB), | 80 (new_space_->in_use() / KB), |
| 89 (old_space_->in_use() / KB), | 81 (old_space_->in_use() / KB), |
| 90 (code_space_->in_use() / KB), | 82 (code_space_->in_use() / KB)); |
| 91 (stub_code_space_->in_use() / KB)); | |
| 92 } | 83 } |
| 93 addr = old_space_->TryAllocate(size); | 84 addr = old_space_->TryAllocate(size); |
| 94 if (addr == 0) { | 85 if (addr == 0) { |
| 95 // TODO(cshapiro): Support possible heap growth and OOM exception. | 86 // TODO(cshapiro): Support possible heap growth and OOM exception. |
| 96 FATAL1("Exhausted heap space, trying to allocate %d bytes.", size); | 87 FATAL1("Exhausted heap space, trying to allocate %d bytes.", size); |
| 97 } | 88 } |
| 98 } | 89 } |
| 99 return addr; | 90 return addr; |
| 100 } | 91 } |
| 101 | 92 |
| 102 | 93 |
| 103 uword Heap::AllocateCode(PageSpace* space, intptr_t size) { | 94 uword Heap::AllocateCode(PageSpace* space, intptr_t size) { |
| 104 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 95 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
| 105 ASSERT(Utils::IsAligned(size, OS::PreferredCodeAlignment())); | 96 ASSERT(Utils::IsAligned(size, OS::PreferredCodeAlignment())); |
| 106 uword addr = space->TryAllocate(size); | 97 uword addr = space->TryAllocate(size); |
| 107 if (addr == 0) { | 98 if (addr == 0) { |
| 108 // TODO(iposva): Support GC. | 99 // TODO(iposva): Support GC. |
| 109 FATAL("Exhausted code heap space."); | 100 FATAL("Exhausted code heap space."); |
| 110 } | 101 } |
| 111 if (FLAG_compiler_stats) { | 102 if (FLAG_compiler_stats) { |
| 112 CompilerStats::code_allocated += size; | 103 CompilerStats::code_allocated += size; |
| 113 } | 104 } |
| 114 return addr; | 105 return addr; |
| 115 } | 106 } |
| 116 | 107 |
| 117 | 108 |
| 118 bool Heap::Contains(uword addr) const { | 109 bool Heap::Contains(uword addr) const { |
| 119 return new_space_->Contains(addr) || | 110 return new_space_->Contains(addr) || |
| 120 old_space_->Contains(addr) || | 111 old_space_->Contains(addr) || |
| 121 code_space_->Contains(addr) || | 112 code_space_->Contains(addr); |
| 122 stub_code_space_->Contains(addr); | |
| 123 } | 113 } |
| 124 | 114 |
| 125 | 115 |
| 126 bool Heap::CodeContains(uword addr) const { | 116 bool Heap::CodeContains(uword addr) const { |
| 127 return code_space_->Contains(addr); | 117 return code_space_->Contains(addr); |
| 128 } | 118 } |
| 129 | 119 |
| 130 | 120 |
| 131 bool Heap::StubCodeContains(uword addr) const { | |
| 132 return stub_code_space_->Contains(addr); | |
| 133 } | |
| 134 | |
| 135 | |
| 136 void Heap::IterateNewPointers(ObjectPointerVisitor* visitor) { | 121 void Heap::IterateNewPointers(ObjectPointerVisitor* visitor) { |
| 137 new_space_->VisitObjectPointers(visitor); | 122 new_space_->VisitObjectPointers(visitor); |
| 138 } | 123 } |
| 139 | 124 |
| 140 | 125 |
| 141 void Heap::IterateOldPointers(ObjectPointerVisitor* visitor) { | 126 void Heap::IterateOldPointers(ObjectPointerVisitor* visitor) { |
| 142 old_space_->VisitObjectPointers(visitor); | 127 old_space_->VisitObjectPointers(visitor); |
| 143 code_space_->VisitObjectPointers(visitor); | 128 code_space_->VisitObjectPointers(visitor); |
| 144 stub_code_space_->VisitObjectPointers(visitor); | |
| 145 } | 129 } |
| 146 | 130 |
| 147 | 131 |
| 148 void Heap::IterateCodePointers(ObjectPointerVisitor* visitor) { | 132 void Heap::IterateCodePointers(ObjectPointerVisitor* visitor) { |
| 149 code_space_->VisitObjectPointers(visitor); | 133 code_space_->VisitObjectPointers(visitor); |
| 150 } | 134 } |
| 151 | 135 |
| 152 | 136 |
| 153 void Heap::IterateStubCodePointers(ObjectPointerVisitor* visitor) { | |
| 154 stub_code_space_->VisitObjectPointers(visitor); | |
| 155 } | |
| 156 | |
| 157 | |
| 158 RawInstructions* Heap::FindObjectInCodeSpace(FindObjectVisitor* visitor) { | 137 RawInstructions* Heap::FindObjectInCodeSpace(FindObjectVisitor* visitor) { |
| 159 // The code heap can only have RawInstructions objects. | 138 // The code heap can only have RawInstructions objects. |
| 160 RawObject* raw_obj = code_space_->FindObject(visitor); | 139 RawObject* raw_obj = code_space_->FindObject(visitor); |
| 161 ASSERT((raw_obj == Object::null()) || | 140 ASSERT((raw_obj == Object::null()) || |
| 162 (raw_obj->ptr()->class_->ptr()->instance_kind_ == kInstructions)); | 141 (raw_obj->ptr()->class_->ptr()->instance_kind_ == kInstructions)); |
| 163 return reinterpret_cast<RawInstructions*>(raw_obj); | 142 return reinterpret_cast<RawInstructions*>(raw_obj); |
| 164 } | 143 } |
| 165 | 144 |
| 166 | 145 |
| 167 RawInstructions* Heap::FindObjectInStubCodeSpace(FindObjectVisitor* visitor) { | |
| 168 // The stub code heap can only have RawInstructions objects. | |
| 169 RawObject* raw_obj = stub_code_space_->FindObject(visitor); | |
| 170 ASSERT((raw_obj == Object::null()) || | |
| 171 (raw_obj->ptr()->class_->ptr()->instance_kind_ == kInstructions)); | |
| 172 return reinterpret_cast<RawInstructions*>(raw_obj); | |
| 173 } | |
| 174 | |
| 175 | |
| 176 void Heap::CollectGarbage(Space space, ApiCallbacks api_callbacks) { | 146 void Heap::CollectGarbage(Space space, ApiCallbacks api_callbacks) { |
| 177 bool invoke_api_callbacks = (api_callbacks == kInvokeApiCallbacks); | 147 bool invoke_api_callbacks = (api_callbacks == kInvokeApiCallbacks); |
| 178 switch (space) { | 148 switch (space) { |
| 179 case kNew: | 149 case kNew: |
| 180 new_space_->Scavenge(invoke_api_callbacks); | 150 new_space_->Scavenge(invoke_api_callbacks); |
| 181 break; | 151 break; |
| 182 case kOld: | 152 case kOld: |
| 183 old_space_->MarkSweep(invoke_api_callbacks); | 153 old_space_->MarkSweep(invoke_api_callbacks); |
| 184 break; | 154 break; |
| 185 case kDartCode: | 155 case kCode: |
| 186 UNIMPLEMENTED(); | 156 UNIMPLEMENTED(); |
| 187 code_space_->MarkSweep(invoke_api_callbacks); | 157 code_space_->MarkSweep(invoke_api_callbacks); |
| 188 break; | 158 break; |
| 189 case kStubCode: | |
| 190 UNIMPLEMENTED(); | |
| 191 stub_code_space_->MarkSweep(invoke_api_callbacks); | |
| 192 break; | |
| 193 default: | 159 default: |
| 194 UNREACHABLE(); | 160 UNREACHABLE(); |
| 195 } | 161 } |
| 196 } | 162 } |
| 197 | 163 |
| 198 | 164 |
| 199 void Heap::CollectGarbage(Space space) { | 165 void Heap::CollectGarbage(Space space) { |
| 200 ApiCallbacks api_callbacks; | 166 ApiCallbacks api_callbacks; |
| 201 if (space == kOld) { | 167 if (space == kOld) { |
| 202 api_callbacks = kInvokeApiCallbacks; | 168 api_callbacks = kInvokeApiCallbacks; |
| 203 } else { | 169 } else { |
| 204 api_callbacks = kIgnoreApiCallbacks; | 170 api_callbacks = kIgnoreApiCallbacks; |
| 205 } | 171 } |
| 206 CollectGarbage(space, api_callbacks); | 172 CollectGarbage(space, api_callbacks); |
| 207 } | 173 } |
| 208 | 174 |
| 209 | 175 |
| 210 void Heap::CollectAllGarbage() { | 176 void Heap::CollectAllGarbage() { |
| 211 new_space_->Scavenge(kInvokeApiCallbacks); | 177 new_space_->Scavenge(kInvokeApiCallbacks); |
| 212 old_space_->MarkSweep(kInvokeApiCallbacks); | 178 old_space_->MarkSweep(kInvokeApiCallbacks); |
| 213 // TODO(iposva): Merge old and code space. | 179 // TODO(iposva): Merge old and code space. |
| 214 // code_space_->MarkSweep(kInvokeApiCallbacks); | 180 // code_space_->MarkSweep(kInvokeApiCallbacks); |
| 215 // stub_code_space_->MarkSweep(kInvokeApiCallbacks); | |
| 216 } | 181 } |
| 217 | 182 |
| 218 | 183 |
| 219 uword Heap::TopAddress() { | 184 uword Heap::TopAddress() { |
| 220 return reinterpret_cast<uword>(new_space_->TopAddress()); | 185 return reinterpret_cast<uword>(new_space_->TopAddress()); |
| 221 } | 186 } |
| 222 | 187 |
| 223 | 188 |
| 224 uword Heap::EndAddress() { | 189 uword Heap::EndAddress() { |
| 225 return reinterpret_cast<uword>(new_space_->EndAddress()); | 190 return reinterpret_cast<uword>(new_space_->EndAddress()); |
| 226 } | 191 } |
| 227 | 192 |
| 228 | 193 |
| 229 void Heap::Init(Isolate* isolate) { | 194 void Heap::Init(Isolate* isolate) { |
| 230 ASSERT(isolate->heap() == NULL); | 195 ASSERT(isolate->heap() == NULL); |
| 231 Heap* heap = new Heap(); | 196 Heap* heap = new Heap(); |
| 232 isolate->set_heap(heap); | 197 isolate->set_heap(heap); |
| 233 } | 198 } |
| 234 | 199 |
| 235 | 200 |
| 236 bool Heap::Verify() const { | 201 bool Heap::Verify() const { |
| 237 VerifyPointersVisitor visitor(Isolate::Current()); | 202 VerifyPointersVisitor visitor(Isolate::Current()); |
| 238 new_space_->VisitObjectPointers(&visitor); | 203 new_space_->VisitObjectPointers(&visitor); |
| 239 old_space_->VisitObjectPointers(&visitor); | 204 old_space_->VisitObjectPointers(&visitor); |
| 240 code_space_->VisitObjectPointers(&visitor); | 205 code_space_->VisitObjectPointers(&visitor); |
| 241 stub_code_space_->VisitObjectPointers(&visitor); | |
| 242 // Only returning a value so that Heap::Validate can be called from an ASSERT. | 206 // Only returning a value so that Heap::Validate can be called from an ASSERT. |
| 243 return true; | 207 return true; |
| 244 } | 208 } |
| 245 | 209 |
| 246 | 210 |
| 247 #if defined(DEBUG) | 211 #if defined(DEBUG) |
| 248 NoGCScope::NoGCScope() : StackResource(Isolate::Current()) { | 212 NoGCScope::NoGCScope() : StackResource(Isolate::Current()) { |
| 249 isolate()->IncrementNoGCScopeDepth(); | 213 isolate()->IncrementNoGCScopeDepth(); |
| 250 } | 214 } |
| 251 | 215 |
| 252 | 216 |
| 253 NoGCScope::~NoGCScope() { | 217 NoGCScope::~NoGCScope() { |
| 254 isolate()->DecrementNoGCScopeDepth(); | 218 isolate()->DecrementNoGCScopeDepth(); |
| 255 } | 219 } |
| 256 #endif // defined(DEBUG) | 220 #endif // defined(DEBUG) |
| 257 | 221 |
| 258 } // namespace dart | 222 } // namespace dart |
| OLD | NEW |