Chromium Code Reviews| 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) { | |
| 61 uword start = thread->top(); | |
| 62 uword end = thread->end(); | |
| 63 ASSERT(end >= start); | |
| 64 intptr_t size = end - start; | |
| 65 if (end == new_space_.end()) { | |
| 66 size = 0; | |
| 67 } | |
| 68 ASSERT(Utils::IsAligned(size, kObjectAlignment)); | |
| 69 if (size >= kObjectAlignment) { | |
| 70 FreeListElement::AsElement(start, size); | |
| 71 ASSERT(RawObject::FromAddr(start)->Size() == size); | |
| 72 ASSERT(start + size == new_space_.top()); | |
|
rmacnak
2017/07/26 21:30:29
(start + size) == new_space_.top()
danunez
2017/07/26 22:07:11
Done.
| |
| 73 } | |
| 74 thread->set_top(start + size); | |
| 75 } | |
| 76 | |
| 77 intptr_t Heap::CalculateTLABSize() { | |
| 78 intptr_t size = new_space_.end() - new_space_.top(); | |
| 79 return Utils::RoundDown(size, kObjectAlignment); | |
| 80 } | |
| 81 | |
| 60 uword Heap::AllocateNew(intptr_t size) { | 82 uword Heap::AllocateNew(intptr_t size) { |
| 61 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); | 83 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
| 62 // Currently, only the Dart thread may allocate in new space. | 84 // Currently, only the Dart thread may allocate in new space. |
| 63 isolate()->AssertCurrentThreadIsMutator(); | 85 isolate()->AssertCurrentThreadIsMutator(); |
| 64 Thread* thread = Thread::Current(); | 86 Thread* thread = Thread::Current(); |
| 65 uword addr = new_space_.TryAllocateInTLAB(thread, size); | 87 uword addr = new_space_.TryAllocateInTLAB(thread, size); |
| 66 if (addr == 0) { | 88 if (addr != 0) { |
| 67 // This call to CollectGarbage might end up "reusing" a collection spawned | 89 return addr; |
| 68 // from a different thread and will be racing to allocate the requested | 90 } |
| 69 // memory with other threads being released after the collection. | 91 |
| 70 CollectGarbage(kNew); | 92 intptr_t tlab_size = CalculateTLABSize(); |
| 71 addr = new_space_.TryAllocateInTLAB(thread, size); | 93 if (tlab_size > 0 && size > tlab_size) { |
|
rmacnak
2017/07/26 21:30:29
if ((tlab_size > 0) && (size > tlab_size)) {
danunez
2017/07/26 22:07:11
Done.
| |
| 72 if (addr == 0) { | 94 return AllocateOld(size, HeapPage::kData); |
| 73 return AllocateOld(size, HeapPage::kData); | 95 } |
| 96 | |
| 97 AbandonRemainingTLAB(thread); | |
| 98 if (tlab_size > 0) { | |
| 99 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); | |
| 100 if (tlab_top != 0) { | |
| 101 addr = new_space_.TryAllocateInTLAB(thread, size); | |
| 102 ASSERT(addr != 0); | |
| 103 return addr; | |
| 74 } | 104 } |
| 75 } | 105 } |
| 76 return addr; | 106 |
|
rmacnak
2017/07/26 21:30:29
ASSERT(thread has no active tlab)
danunez
2017/07/26 22:07:11
Done.
| |
| 107 // This call to CollectGarbage might end up "reusing" a collection spawned | |
| 108 // from a different thread and will be racing to allocate the requested | |
| 109 // memory with other threads being released after the collection. | |
| 110 CollectGarbage(kNew); | |
| 111 tlab_size = CalculateTLABSize(); | |
| 112 uword tlab_top = new_space_.TryAllocateNewTLAB(thread, tlab_size); | |
| 113 if (tlab_top != 0) { | |
| 114 addr = new_space_.TryAllocateInTLAB(thread, size); | |
| 115 // It is possible a GC doesn't clear enough space. | |
| 116 // In that case, we must fall through and allocate into old space. | |
| 117 if (addr != 0) { | |
| 118 return addr; | |
| 119 } | |
| 120 } | |
| 121 return AllocateOld(size, HeapPage::kData); | |
| 77 } | 122 } |
| 78 | 123 |
| 79 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { | 124 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { |
| 80 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); | 125 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
| 81 uword addr = old_space_.TryAllocate(size, type); | 126 uword addr = old_space_.TryAllocate(size, type); |
| 82 if (addr != 0) { | 127 if (addr != 0) { |
| 83 return addr; | 128 return addr; |
| 84 } | 129 } |
| 85 // If we are in the process of running a sweep, wait for the sweeper to free | 130 // If we are in the process of running a sweep, wait for the sweeper to free |
| 86 // memory. | 131 // memory. |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 548 | 593 |
| 549 bool Heap::Verify(MarkExpectation mark_expectation) const { | 594 bool Heap::Verify(MarkExpectation mark_expectation) const { |
| 550 HeapIterationScope heap_iteration_scope; | 595 HeapIterationScope heap_iteration_scope; |
| 551 return VerifyGC(mark_expectation); | 596 return VerifyGC(mark_expectation); |
| 552 } | 597 } |
| 553 | 598 |
| 554 bool Heap::VerifyGC(MarkExpectation mark_expectation) const { | 599 bool Heap::VerifyGC(MarkExpectation mark_expectation) const { |
| 555 StackZone stack_zone(Thread::Current()); | 600 StackZone stack_zone(Thread::Current()); |
| 556 | 601 |
| 557 // Change the new space's top_ with the more up-to-date thread's view of top_ | 602 // Change the new space's top_ with the more up-to-date thread's view of top_ |
| 558 new_space_.FlushTLS(); | 603 uword saved_top = new_space_.FlushTLS(); |
| 559 | 604 |
| 560 ObjectSet* allocated_set = | 605 ObjectSet* allocated_set = |
| 561 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation); | 606 CreateAllocatedObjectSet(stack_zone.GetZone(), mark_expectation); |
| 562 VerifyPointersVisitor visitor(isolate(), allocated_set); | 607 VerifyPointersVisitor visitor(isolate(), allocated_set); |
| 563 VisitObjectPointers(&visitor); | 608 VisitObjectPointers(&visitor); |
| 564 | 609 |
| 610 new_space_.UnflushTLS(saved_top); | |
| 565 // Only returning a value so that Heap::Validate can be called from an ASSERT. | 611 // Only returning a value so that Heap::Validate can be called from an ASSERT. |
| 566 return true; | 612 return true; |
| 567 } | 613 } |
| 568 | 614 |
| 569 void Heap::PrintSizes() const { | 615 void Heap::PrintSizes() const { |
| 570 OS::PrintErr( | 616 OS::PrintErr( |
| 571 "New space (%" Pd64 "k of %" Pd64 | 617 "New space (%" Pd64 "k of %" Pd64 |
| 572 "k) " | 618 "k) " |
| 573 "Old space (%" Pd64 "k of %" Pd64 "k)\n", | 619 "Old space (%" Pd64 "k of %" Pd64 "k)\n", |
| 574 (UsedInWords(kNew) / KBInWords), (CapacityInWords(kNew) / KBInWords), | 620 (UsedInWords(kNew) / KBInWords), (CapacityInWords(kNew) / KBInWords), |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 829 : StackResource(thread) { | 875 : StackResource(thread) { |
| 830 Dart::vm_isolate()->heap()->WriteProtect(false); | 876 Dart::vm_isolate()->heap()->WriteProtect(false); |
| 831 } | 877 } |
| 832 | 878 |
| 833 WritableVMIsolateScope::~WritableVMIsolateScope() { | 879 WritableVMIsolateScope::~WritableVMIsolateScope() { |
| 834 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0); | 880 ASSERT(Dart::vm_isolate()->heap()->UsedInWords(Heap::kNew) == 0); |
| 835 Dart::vm_isolate()->heap()->WriteProtect(true); | 881 Dart::vm_isolate()->heap()->WriteProtect(true); |
| 836 } | 882 } |
| 837 | 883 |
| 838 } // namespace dart | 884 } // namespace dart |
| OLD | NEW |