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/heap_profiler.h" | 10 #include "vm/heap_profiler.h" |
| 11 #include "vm/heap_trace.h" |
11 #include "vm/isolate.h" | 12 #include "vm/isolate.h" |
12 #include "vm/object.h" | 13 #include "vm/object.h" |
13 #include "vm/object_set.h" | 14 #include "vm/object_set.h" |
14 #include "vm/os.h" | 15 #include "vm/os.h" |
15 #include "vm/pages.h" | 16 #include "vm/pages.h" |
| 17 #include "vm/raw_object.h" |
16 #include "vm/scavenger.h" | 18 #include "vm/scavenger.h" |
17 #include "vm/stack_frame.h" | 19 #include "vm/stack_frame.h" |
18 #include "vm/verifier.h" | 20 #include "vm/verifier.h" |
19 #include "vm/virtual_memory.h" | 21 #include "vm/virtual_memory.h" |
20 | 22 |
21 namespace dart { | 23 namespace dart { |
22 | 24 |
23 DEFINE_FLAG(bool, verbose_gc, false, "Enables verbose GC."); | 25 DEFINE_FLAG(bool, verbose_gc, false, "Enables verbose GC."); |
24 DEFINE_FLAG(bool, verify_before_gc, false, | 26 DEFINE_FLAG(bool, verify_before_gc, false, |
25 "Enables heap verification before GC."); | 27 "Enables heap verification before GC."); |
26 DEFINE_FLAG(bool, verify_after_gc, false, | 28 DEFINE_FLAG(bool, verify_after_gc, false, |
27 "Enables heap verification after GC."); | 29 "Enables heap verification after GC."); |
28 DEFINE_FLAG(bool, gc_at_alloc, false, "GC at every allocation."); | 30 DEFINE_FLAG(bool, gc_at_alloc, false, "GC at every allocation."); |
29 DEFINE_FLAG(int, new_gen_heap_size, 32, "new gen heap size in MB," | 31 DEFINE_FLAG(int, new_gen_heap_size, 32, "new gen heap size in MB," |
30 "e.g: --new_gen_heap_size=64 allocates a 64MB new gen heap"); | 32 "e.g: --new_gen_heap_size=64 allocates a 64MB new gen heap"); |
31 DEFINE_FLAG(int, old_gen_heap_size, Heap::kHeapSizeInMB, | 33 DEFINE_FLAG(int, old_gen_heap_size, Heap::kHeapSizeInMB, |
32 "old gen heap size in MB," | 34 "old gen heap size in MB," |
33 "e.g: --old_gen_heap_size=1024 allocates a 1024MB old gen heap"); | 35 "e.g: --old_gen_heap_size=1024 allocates a 1024MB old gen heap"); |
34 | 36 |
35 Heap::Heap() : read_only_(false) { | 37 Heap::Heap() : read_only_(false) { |
36 new_space_ = new Scavenger(this, | 38 new_space_ = new Scavenger(this, |
37 (FLAG_new_gen_heap_size * MB), | 39 (FLAG_new_gen_heap_size * MB), |
38 kNewObjectAlignmentOffset); | 40 kNewObjectAlignmentOffset); |
39 old_space_ = new PageSpace(this, (FLAG_old_gen_heap_size * MB)); | 41 old_space_ = new PageSpace(this, (FLAG_old_gen_heap_size * MB)); |
| 42 heap_trace_ = new HeapTrace; |
40 } | 43 } |
41 | 44 |
42 | 45 |
43 Heap::~Heap() { | 46 Heap::~Heap() { |
44 delete new_space_; | 47 delete new_space_; |
45 delete old_space_; | 48 delete old_space_; |
46 } | 49 } |
47 | 50 |
48 | 51 |
49 uword Heap::AllocateNew(intptr_t size) { | 52 uword Heap::AllocateNew(intptr_t size) { |
50 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 53 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
51 uword addr = new_space_->TryAllocate(size); | 54 uword addr = new_space_->TryAllocate(size); |
52 if (addr != 0) { | 55 if (addr == 0) { |
53 return addr; | 56 CollectGarbage(kNew); |
| 57 addr = new_space_->TryAllocate(size); |
| 58 if (addr == 0) { |
| 59 return AllocateOld(size, HeapPage::kData); |
| 60 } |
54 } | 61 } |
55 CollectGarbage(kNew); | 62 if (HeapTrace::is_enabled()) { |
56 addr = new_space_->TryAllocate(size); | 63 heap_trace_->TraceAllocation(addr, size); |
57 if (addr != 0) { | |
58 return addr; | |
59 } | 64 } |
60 return AllocateOld(size, HeapPage::kData); | 65 return addr; |
61 } | 66 } |
62 | 67 |
63 | 68 |
64 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { | 69 uword Heap::AllocateOld(intptr_t size, HeapPage::PageType type) { |
65 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); | 70 ASSERT(Isolate::Current()->no_gc_scope_depth() == 0); |
66 uword addr = old_space_->TryAllocate(size, type); | 71 uword addr = old_space_->TryAllocate(size, type); |
67 if (addr == 0) { | 72 if (addr == 0) { |
68 CollectAllGarbage(); | 73 CollectAllGarbage(); |
69 addr = old_space_->TryAllocate(size, type, PageSpace::kForceGrowth); | 74 addr = old_space_->TryAllocate(size, type, PageSpace::kForceGrowth); |
70 if (addr == 0) { | 75 if (addr == 0) { |
71 OS::PrintErr("Exhausted heap space, trying to allocate %"Pd" bytes.\n", | 76 OS::PrintErr("Exhausted heap space, trying to allocate %"Pd" bytes.\n", |
72 size); | 77 size); |
| 78 return 0; |
73 } | 79 } |
74 } | 80 } |
| 81 if (HeapTrace::is_enabled()) { |
| 82 heap_trace_->TraceAllocation(addr, size); |
| 83 } |
75 return addr; | 84 return addr; |
76 } | 85 } |
77 | 86 |
78 | 87 |
79 bool Heap::Contains(uword addr) const { | 88 bool Heap::Contains(uword addr) const { |
80 return new_space_->Contains(addr) || | 89 return new_space_->Contains(addr) || |
81 old_space_->Contains(addr); | 90 old_space_->Contains(addr); |
82 } | 91 } |
83 | 92 |
84 | 93 |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 isolate()->IncrementNoGCScopeDepth(); | 367 isolate()->IncrementNoGCScopeDepth(); |
359 } | 368 } |
360 | 369 |
361 | 370 |
362 NoGCScope::~NoGCScope() { | 371 NoGCScope::~NoGCScope() { |
363 isolate()->DecrementNoGCScopeDepth(); | 372 isolate()->DecrementNoGCScopeDepth(); |
364 } | 373 } |
365 #endif // defined(DEBUG) | 374 #endif // defined(DEBUG) |
366 | 375 |
367 } // namespace dart | 376 } // namespace dart |
OLD | NEW |