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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 for (int sel = 0; | 67 for (int sel = 0; |
68 sel < kNumWeakSelectors; | 68 sel < kNumWeakSelectors; |
69 sel++) { | 69 sel++) { |
70 delete new_weak_tables_[sel]; | 70 delete new_weak_tables_[sel]; |
71 delete old_weak_tables_[sel]; | 71 delete old_weak_tables_[sel]; |
72 } | 72 } |
73 } | 73 } |
74 | 74 |
75 | 75 |
76 uword Heap::AllocateNew(intptr_t size) { | 76 uword Heap::AllocateNew(intptr_t size) { |
77 ASSERT(isolate()->no_safepoint_scope_depth() == 0); | 77 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
| 78 // Currently, only the Dart thread may allocate in new space. |
| 79 isolate()->AssertCurrentThreadIsMutator(); |
78 uword addr = new_space_.TryAllocate(size); | 80 uword addr = new_space_.TryAllocate(size); |
79 if (addr == 0) { | 81 if (addr == 0) { |
80 CollectGarbage(kNew); | 82 CollectGarbage(kNew); |
81 addr = new_space_.TryAllocate(size); | 83 addr = new_space_.TryAllocate(size); |
82 if (addr == 0) { | 84 if (addr == 0) { |
83 return AllocateOld(size, HeapPage::kData); | 85 return AllocateOld(size, HeapPage::kData); |
84 } | 86 } |
85 } | 87 } |
86 return addr; | 88 return addr; |
87 } | 89 } |
88 | 90 |
89 | 91 |
90 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { | 92 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { |
91 ASSERT(isolate()->no_safepoint_scope_depth() == 0); | 93 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
| 94 #if defined(DEBUG) |
| 95 // Currently, allocation from non-Dart threads must not trigger GC. |
| 96 if (GrowthControlState()) { |
| 97 isolate()->AssertCurrentThreadIsMutator(); |
| 98 } |
| 99 #endif |
92 uword addr = old_space_.TryAllocate(size, type); | 100 uword addr = old_space_.TryAllocate(size, type); |
93 if (addr != 0) { | 101 if (addr != 0) { |
94 return addr; | 102 return addr; |
95 } | 103 } |
96 // If we are in the process of running a sweep wait for the sweeper to free | 104 // If we are in the process of running a sweep wait for the sweeper to free |
97 // memory. | 105 // memory. |
98 { | 106 { |
99 MonitorLocker ml(old_space_.tasks_lock()); | 107 MonitorLocker ml(old_space_.tasks_lock()); |
100 addr = old_space_.TryAllocate(size, type); | 108 addr = old_space_.TryAllocate(size, type); |
101 while ((addr == 0) && (old_space_.tasks() > 0)) { | 109 while ((addr == 0) && (old_space_.tasks() > 0)) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 return addr; | 150 return addr; |
143 } | 151 } |
144 // Give up allocating this object. | 152 // Give up allocating this object. |
145 OS::PrintErr( | 153 OS::PrintErr( |
146 "Exhausted heap space, trying to allocate %" Pd " bytes.\n", size); | 154 "Exhausted heap space, trying to allocate %" Pd " bytes.\n", size); |
147 return 0; | 155 return 0; |
148 } | 156 } |
149 | 157 |
150 | 158 |
151 uword Heap::AllocatePretenured(intptr_t size) { | 159 uword Heap::AllocatePretenured(intptr_t size) { |
152 ASSERT(isolate()->no_safepoint_scope_depth() == 0); | 160 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
153 uword addr = old_space_.TryAllocateDataBump(size, PageSpace::kControlGrowth); | 161 uword addr = old_space_.TryAllocateDataBump(size, PageSpace::kControlGrowth); |
154 if (addr != 0) return addr; | 162 if (addr != 0) return addr; |
155 return AllocateOld(size, HeapPage::kData); | 163 return AllocateOld(size, HeapPage::kData); |
156 } | 164 } |
157 | 165 |
158 | 166 |
159 void Heap::AllocateExternal(intptr_t size, Space space) { | 167 void Heap::AllocateExternal(intptr_t size, Space space) { |
160 ASSERT(isolate()->no_safepoint_scope_depth() == 0); | 168 ASSERT(Thread::Current()->no_safepoint_scope_depth() == 0); |
161 if (space == kNew) { | 169 if (space == kNew) { |
162 new_space_.AllocateExternal(size); | 170 new_space_.AllocateExternal(size); |
163 if (new_space_.ExternalInWords() > (FLAG_new_gen_ext_limit * MBInWords)) { | 171 if (new_space_.ExternalInWords() > (FLAG_new_gen_ext_limit * MBInWords)) { |
164 // Attempt to free some external allocation by a scavenge. (If the total | 172 // Attempt to free some external allocation by a scavenge. (If the total |
165 // remains above the limit, next external alloc will trigger another.) | 173 // remains above the limit, next external alloc will trigger another.) |
166 CollectGarbage(kNew); | 174 CollectGarbage(kNew); |
167 } | 175 } |
168 } else { | 176 } else { |
169 ASSERT(space == kOld); | 177 ASSERT(space == kOld); |
170 old_space_.AllocateExternal(size); | 178 old_space_.AllocateExternal(size); |
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 MicrosecondsToMilliseconds(stats_.times_[2]), | 760 MicrosecondsToMilliseconds(stats_.times_[2]), |
753 MicrosecondsToMilliseconds(stats_.times_[3]), | 761 MicrosecondsToMilliseconds(stats_.times_[3]), |
754 stats_.data_[0], | 762 stats_.data_[0], |
755 stats_.data_[1], | 763 stats_.data_[1], |
756 stats_.data_[2], | 764 stats_.data_[2], |
757 stats_.data_[3]); | 765 stats_.data_[3]); |
758 } | 766 } |
759 | 767 |
760 | 768 |
761 #if defined(DEBUG) | 769 #if defined(DEBUG) |
762 NoSafepointScope::NoSafepointScope() : StackResource(Isolate::Current()) { | 770 NoSafepointScope::NoSafepointScope() : StackResource(Thread::Current()) { |
763 isolate()->IncrementNoSafepointScopeDepth(); | 771 thread()->IncrementNoSafepointScopeDepth(); |
764 } | 772 } |
765 | 773 |
766 | 774 |
767 NoSafepointScope::~NoSafepointScope() { | 775 NoSafepointScope::~NoSafepointScope() { |
768 isolate()->DecrementNoSafepointScopeDepth(); | 776 thread()->DecrementNoSafepointScopeDepth(); |
769 } | 777 } |
770 #endif // defined(DEBUG) | 778 #endif // defined(DEBUG) |
771 | 779 |
772 | 780 |
773 NoHeapGrowthControlScope::NoHeapGrowthControlScope() | 781 NoHeapGrowthControlScope::NoHeapGrowthControlScope() |
774 : StackResource(Isolate::Current()) { | 782 : StackResource(Isolate::Current()) { |
775 Heap* heap = reinterpret_cast<Isolate*>(isolate())->heap(); | 783 Heap* heap = reinterpret_cast<Isolate*>(isolate())->heap(); |
776 current_growth_controller_state_ = heap->GrowthControlState(); | 784 current_growth_controller_state_ = heap->GrowthControlState(); |
777 heap->DisableGrowthControl(); | 785 heap->DisableGrowthControl(); |
778 } | 786 } |
779 | 787 |
780 | 788 |
781 NoHeapGrowthControlScope::~NoHeapGrowthControlScope() { | 789 NoHeapGrowthControlScope::~NoHeapGrowthControlScope() { |
782 Heap* heap = reinterpret_cast<Isolate*>(isolate())->heap(); | 790 Heap* heap = reinterpret_cast<Isolate*>(isolate())->heap(); |
783 heap->SetGrowthControlState(current_growth_controller_state_); | 791 heap->SetGrowthControlState(current_growth_controller_state_); |
784 } | 792 } |
785 | 793 |
786 } // namespace dart | 794 } // namespace dart |
OLD | NEW |