| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 Heap::~Heap() { | 50 Heap::~Heap() { |
| 51 delete barrier_; | 51 delete barrier_; |
| 52 delete barrier_done_; | 52 delete barrier_done_; |
| 53 | 53 |
| 54 for (int sel = 0; sel < kNumWeakSelectors; sel++) { | 54 for (int sel = 0; sel < kNumWeakSelectors; sel++) { |
| 55 delete new_weak_tables_[sel]; | 55 delete new_weak_tables_[sel]; |
| 56 delete old_weak_tables_[sel]; | 56 delete old_weak_tables_[sel]; |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 void Heap::AbandonRemainingTLAB(Thread* thread) { | 60 void Heap::MakeTLABIterable(Thread* thread) { |
| 61 uword start = thread->top(); | 61 uword start = thread->top(); |
| 62 uword end = thread->end(); | 62 uword end = thread->end(); |
| 63 ASSERT(end >= start); | 63 ASSERT(end >= start); |
| 64 intptr_t size = end - start; | 64 intptr_t size = end - start; |
| 65 if (end == new_space_.end()) { | 65 if (end == new_space_.end()) { |
| 66 size = 0; | 66 size = 0; |
| 67 } | 67 } |
| 68 ASSERT(Utils::IsAligned(size, kObjectAlignment)); | 68 ASSERT(Utils::IsAligned(size, kObjectAlignment)); |
| 69 if (size >= kObjectAlignment) { | 69 if (size >= kObjectAlignment) { |
| 70 FreeListElement::AsElement(start, size); | 70 FreeListElement::AsElement(start, size); |
| 71 ASSERT(RawObject::FromAddr(start)->Size() == size); | 71 ASSERT(RawObject::FromAddr(start)->Size() == size); |
| 72 ASSERT((start + size) == new_space_.top()); | |
| 73 } | 72 } |
| 73 } |
| 74 |
| 75 void Heap::AbandonRemainingTLAB(Thread* thread) { |
| 76 MakeTLABIterable(thread); |
| 74 thread->set_top(0); | 77 thread->set_top(0); |
| 75 thread->set_end(0); | 78 thread->set_end(0); |
| 76 } | 79 } |
| 77 | 80 |
| 78 intptr_t Heap::CalculateTLABSize() { | 81 intptr_t Heap::CalculateTLABSize() { |
| 79 intptr_t size = new_space_.end() - new_space_.top(); | 82 intptr_t size = new_space_.end() - new_space_.top(); |
| 80 return Utils::RoundDown(size, kObjectAlignment); | 83 return Utils::RoundDown(size, kObjectAlignment); |
| 81 } | 84 } |
| 82 | 85 |
| 83 uword Heap::AllocateNew(intptr_t size) { | 86 uword Heap::AllocateNew(intptr_t size) { |
| 84 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); | 87 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
| 85 // Currently, only the Dart thread may allocate in new space. | 88 // Currently, only the Dart thread may allocate in new space. |
| 86 isolate()->AssertCurrentThreadIsMutator(); | 89 isolate()->AssertCurrentThreadIsMutator(); |
| 87 Thread* thread = Thread::Current(); | 90 Thread* thread = Thread::Current(); |
| 88 uword addr = new_space_.TryAllocateInTLAB(thread, size); | 91 uword addr = new_space_.TryAllocateInTLAB(thread, size); |
| 89 if (addr != 0) { | 92 if (addr != 0) { |
| 90 return addr; | 93 return addr; |
| 91 } | 94 } |
| 92 | 95 |
| 93 intptr_t tlab_size = CalculateTLABSize(); | 96 intptr_t tlab_size = CalculateTLABSize(); |
| 94 if ((tlab_size > 0) && (size > tlab_size)) { | 97 if ((tlab_size > 0) && (size > tlab_size)) { |
| 95 return AllocateOld(size, HeapPage::kData); | 98 return AllocateOld(size, HeapPage::kData); |
| 96 } | 99 } |
| 97 | 100 |
| 98 AbandonRemainingTLAB(thread); | 101 AbandonRemainingTLAB(thread); |
| 102 |
| 103 // TODO(danunez): Lock for new space here |
| 99 if (tlab_size > 0) { | 104 if (tlab_size > 0) { |
| 100 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); | 105 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); |
| 101 if (tlab_top != 0) { | 106 if (tlab_top != 0) { |
| 102 addr = new_space_.TryAllocateInTLAB(thread, size); | 107 addr = new_space_.TryAllocateInTLAB(thread, size); |
| 103 ASSERT(addr != 0); | 108 ASSERT(addr != 0); |
| 109 // TODO(danunez): Unlock for new space here |
| 104 return addr; | 110 return addr; |
| 105 } | 111 } |
| 106 } | 112 } |
| 107 | 113 |
| 108 ASSERT(!thread->HasActiveTLAB()); | 114 ASSERT(!thread->HasActiveTLAB()); |
| 109 | 115 |
| 110 // This call to CollectGarbage might end up "reusing" a collection spawned | 116 // This call to CollectGarbage might end up "reusing" a collection spawned |
| 111 // from a different thread and will be racing to allocate the requested | 117 // from a different thread and will be racing to allocate the requested |
| 112 // memory with other threads being released after the collection. | 118 // memory with other threads being released after the collection. |
| 119 |
| 120 // TODO(danunez): Ensure that once we start collecting garbage, everything |
| 121 // stops allocating. |
| 122 // Safepoint should be dealing with this, but we need to make sure somehow. |
| 113 CollectGarbage(kNew); | 123 CollectGarbage(kNew); |
| 114 tlab_size = CalculateTLABSize(); | 124 tlab_size = CalculateTLABSize(); |
| 115 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); | 125 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); |
| 126 |
| 127 // TODO(danunez): Unlock for new space here. Regardless of the outcome, we |
| 128 // don't need the new space. |
| 116 if (tlab_top != 0) { | 129 if (tlab_top != 0) { |
| 117 addr = new_space_.TryAllocateInTLAB(thread, size); | 130 addr = new_space_.TryAllocateInTLAB(thread, size); |
| 118 // It is possible a GC doesn't clear enough space. | 131 // It is possible a GC doesn't clear enough space. |
| 119 // In that case, we must fall through and allocate into old space. | 132 // In that case, we must fall through and allocate into old space. |
| 120 if (addr != 0) { | 133 if (addr != 0) { |
| 121 return addr; | 134 return addr; |
| 122 } | 135 } |
| 123 } | 136 } |
| 124 return AllocateOld(size, HeapPage::kData); | 137 return AllocateOld(size, HeapPage::kData); |
| 125 } | 138 } |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 | 609 |
| 597 bool Heap::Verify(MarkExpectation mark_expectation) const { | 610 bool Heap::Verify(MarkExpectation mark_expectation) const { |
| 598 HeapIterationScope heap_iteration_scope; | 611 HeapIterationScope heap_iteration_scope; |
| 599 return VerifyGC(mark_expectation); | 612 return VerifyGC(mark_expectation); |
| 600 } | 613 } |
| 601 | 614 |
| 602 bool Heap::VerifyGC(MarkExpectation mark_expectation) const { | 615 bool Heap::VerifyGC(MarkExpectation mark_expectation) const { |
| 603 StackZone stack_zone(Thread::Current()); | 616 StackZone stack_zone(Thread::Current()); |
| 604 | 617 |
| 605 // Change the new space's top_ with the more up-to-date thread's view of top_ | 618 // Change the new space's top_ with the more up-to-date thread's view of top_ |
| 606 uword saved_top = new_space_.FlushTLS(); | 619 new_space_.FlushTLS(); |
| 607 | 620 |
| 608 ObjectSet* allocated_set = | 621 ObjectSet* allocated_set = |
| 609 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation); | 622 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation); |
| 610 VerifyPointersVisitor visitor(isolate(), allocated_set); | 623 VerifyPointersVisitor visitor(isolate(), allocated_set); |
| 624 // TODO(danunez): Need to lock and unlock for new space here. |
| 611 VisitObjectPointers(&visitor); | 625 VisitObjectPointers(&visitor); |
| 612 | 626 |
| 613 new_space_.UnflushTLS(saved_top); | |
| 614 // Only returning a value so that Heap::Validate can be called from an ASSERT. | 627 // Only returning a value so that Heap::Validate can be called from an ASSERT. |
| 615 return true; | 628 return true; |
| 616 } | 629 } |
| 617 | 630 |
| 618 void Heap::PrintSizes() const { | 631 void Heap::PrintSizes() const { |
| 619 OS::PrintErr( | 632 OS::PrintErr( |
| 620 "New space (%" Pd64 "k of %" Pd64 | 633 "New space (%" Pd64 "k of %" Pd64 |
| 621 "k) " | 634 "k) " |
| 622 "Old space (%" Pd64 "k of %" Pd64 "k)\n", | 635 "Old space (%" Pd64 "k of %" Pd64 "k)\n", |
| 623 (UsedInWords(kNew) / KBInWords), (CapacityInWords(kNew) / KBInWords), | 636 (UsedInWords(kNew) / KBInWords), (CapacityInWords(kNew) / KBInWords), |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 : StackResource(thread) { | 891 : StackResource(thread) { |
| 879 Dart::vm_isolate()->heap()->WriteProtect(false); | 892 Dart::vm_isolate()->heap()->WriteProtect(false); |
| 880 } | 893 } |
| 881 | 894 |
| 882 WritableVMIsolateScope::~WritableVMIsolateScope() { | 895 WritableVMIsolateScope::~WritableVMIsolateScope() { |
| 883 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0); | 896 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0); |
| 884 Dart::vm_isolate()->heap()->WriteProtect(true); | 897 Dart::vm_isolate()->heap()->WriteProtect(true); |
| 885 } | 898 } |
| 886 | 899 |
| 887 } // namespace dart | 900 } // namespace dart |
| OLD | NEW |