| 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/flags.h" | 9 #include "vm/flags.h" |
| 10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 Heap::~Heap() { | 51 Heap::~Heap() { |
| 52 delete barrier_; | 52 delete barrier_; |
| 53 delete barrier_done_; | 53 delete barrier_done_; |
| 54 | 54 |
| 55 for (int sel = 0; sel < kNumWeakSelectors; sel++) { | 55 for (int sel = 0; sel < kNumWeakSelectors; sel++) { |
| 56 delete new_weak_tables_[sel]; | 56 delete new_weak_tables_[sel]; |
| 57 delete old_weak_tables_[sel]; | 57 delete old_weak_tables_[sel]; |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 void Heap::MakeTLABIterable(Thread* thread) { | |
| 62 uword start = thread->top(); | |
| 63 uword end = thread->end(); | |
| 64 ASSERT(end >= start); | |
| 65 intptr_t size = end - start; | |
| 66 ASSERT(Utils::IsAligned(size, kObjectAlignment)); | |
| 67 if (size >= kObjectAlignment) { | |
| 68 FreeListElement::AsElement(start, size); | |
| 69 ASSERT(RawObject::FromAddr(start)->Size() == size); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void Heap::AbandonRemainingTLAB(Thread* thread) { | |
| 74 MakeTLABIterable(thread); | |
| 75 thread->set_top(0); | |
| 76 thread->set_end(0); | |
| 77 } | |
| 78 | |
| 79 intptr_t Heap::CalculateTLABSize() { | |
| 80 intptr_t size = new_space_.end() - new_space_.top(); | |
| 81 return Utils::RoundDown(size, kObjectAlignment); | |
| 82 } | |
| 83 | |
| 84 uword Heap::AllocateNew(intptr_t size) { | 61 uword Heap::AllocateNew(intptr_t size) { |
| 85 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); | 62 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
| 86 // Currently, only the Dart thread may allocate in new space. | 63 // Currently, only the Dart thread may allocate in new space. |
| 87 isolate()->AssertCurrentThreadIsMutator(); | 64 isolate()->AssertCurrentThreadIsMutator(); |
| 88 Thread* thread = Thread::Current(); | 65 Thread* thread = Thread::Current(); |
| 89 uword addr = new_space_.TryAllocateInTLAB(thread, size); | 66 uword addr = new_space_.TryAllocateInTLAB(thread, size); |
| 90 if (addr != 0) { | 67 if (addr == 0) { |
| 91 return addr; | 68 // This call to CollectGarbage might end up "reusing" a collection spawned |
| 92 } | 69 // from a different thread and will be racing to allocate the requested |
| 93 | 70 // memory with other threads being released after the collection. |
| 94 intptr_t tlab_size = CalculateTLABSize(); | 71 CollectGarbage(kNew); |
| 95 if ((tlab_size > 0) && (size > tlab_size)) { | 72 addr = new_space_.TryAllocateInTLAB(thread, size); |
| 96 return AllocateOld(size, HeapPage::kData); | 73 if (addr == 0) { |
| 97 } | 74 return AllocateOld(size, HeapPage::kData); |
| 98 | |
| 99 AbandonRemainingTLAB(thread); | |
| 100 if (tlab_size > 0) { | |
| 101 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); | |
| 102 if (tlab_top != 0) { | |
| 103 addr = new_space_.TryAllocateInTLAB(thread, size); | |
| 104 ASSERT(addr != 0); | |
| 105 return addr; | |
| 106 } | 75 } |
| 107 } | 76 } |
| 108 | 77 return addr; |
| 109 ASSERT(!thread->HasActiveTLAB()); | |
| 110 | |
| 111 // This call to CollectGarbage might end up "reusing" a collection spawned | |
| 112 // from a different thread and will be racing to allocate the requested | |
| 113 // memory with other threads being released after the collection. | |
| 114 CollectGarbage(kNew); | |
| 115 tlab_size = CalculateTLABSize(); | |
| 116 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); | |
| 117 if (tlab_top != 0) { | |
| 118 addr = new_space_.TryAllocateInTLAB(thread, size); | |
| 119 // It is possible a GC doesn't clear enough space. | |
| 120 // In that case, we must fall through and allocate into old space. | |
| 121 if (addr != 0) { | |
| 122 return addr; | |
| 123 } | |
| 124 } | |
| 125 return AllocateOld(size, HeapPage::kData); | |
| 126 } | 78 } |
| 127 | 79 |
| 128 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { | 80 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { |
| 129 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); | 81 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
| 130 uword addr = old_space_.TryAllocate(size, type); | 82 uword addr = old_space_.TryAllocate(size, type); |
| 131 if (addr != 0) { | 83 if (addr != 0) { |
| 132 return addr; | 84 return addr; |
| 133 } | 85 } |
| 134 // If we are in the process of running a sweep, wait for the sweeper to free | 86 // If we are in the process of running a sweep, wait for the sweeper to free |
| 135 // memory. | 87 // memory. |
| (...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 | 566 |
| 615 bool Heap::Verify(MarkExpectation mark_expectation) const { | 567 bool Heap::Verify(MarkExpectation mark_expectation) const { |
| 616 HeapIterationScope heap_iteration_scope(Thread::Current()); | 568 HeapIterationScope heap_iteration_scope(Thread::Current()); |
| 617 return VerifyGC(mark_expectation); | 569 return VerifyGC(mark_expectation); |
| 618 } | 570 } |
| 619 | 571 |
| 620 bool Heap::VerifyGC(MarkExpectation mark_expectation) const { | 572 bool Heap::VerifyGC(MarkExpectation mark_expectation) const { |
| 621 StackZone stack_zone(Thread::Current()); | 573 StackZone stack_zone(Thread::Current()); |
| 622 | 574 |
| 623 // Change the new space's top_ with the more up-to-date thread's view of top_ | 575 // Change the new space's top_ with the more up-to-date thread's view of top_ |
| 624 new_space_.MakeNewSpaceIterable(); | 576 new_space_.FlushTLS(); |
| 625 | 577 |
| 626 ObjectSet* allocated_set = | 578 ObjectSet* allocated_set = |
| 627 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation); | 579 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation); |
| 628 VerifyPointersVisitor visitor(isolate(), allocated_set); | 580 VerifyPointersVisitor visitor(isolate(), allocated_set); |
| 629 | |
| 630 VisitObjectPointers(&visitor); | 581 VisitObjectPointers(&visitor); |
| 631 | 582 |
| 632 // Only returning a value so that Heap::Validate can be called from an ASSERT. | 583 // Only returning a value so that Heap::Validate can be called from an ASSERT. |
| 633 return true; | 584 return true; |
| 634 } | 585 } |
| 635 | 586 |
| 636 void Heap::PrintSizes() const { | 587 void Heap::PrintSizes() const { |
| 637 OS::PrintErr( | 588 OS::PrintErr( |
| 638 "New space (%" Pd64 "k of %" Pd64 | 589 "New space (%" Pd64 "k of %" Pd64 |
| 639 "k) " | 590 "k) " |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 : StackResource(thread) { | 847 : StackResource(thread) { |
| 897 Dart::vm_isolate()->heap()->WriteProtect(false); | 848 Dart::vm_isolate()->heap()->WriteProtect(false); |
| 898 } | 849 } |
| 899 | 850 |
| 900 WritableVMIsolateScope::~WritableVMIsolateScope() { | 851 WritableVMIsolateScope::~WritableVMIsolateScope() { |
| 901 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0); | 852 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0); |
| 902 Dart::vm_isolate()->heap()->WriteProtect(true); | 853 Dart::vm_isolate()->heap()->WriteProtect(true); |
| 903 } | 854 } |
| 904 | 855 |
| 905 } // namespace dart | 856 } // namespace dart |
| OLD | NEW |