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