| 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 #ifndef VM_SCAVENGER_H_ | 5 #ifndef VM_SCAVENGER_H_ |
| 6 #define VM_SCAVENGER_H_ | 6 #define VM_SCAVENGER_H_ |
| 7 | 7 |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "platform/utils.h" | 9 #include "platform/utils.h" |
| 10 #include "vm/dart.h" | 10 #include "vm/dart.h" |
| 11 #include "vm/flags.h" | 11 #include "vm/flags.h" |
| 12 #include "vm/globals.h" | 12 #include "vm/globals.h" |
| 13 #include "vm/raw_object.h" | 13 #include "vm/raw_object.h" |
| 14 #include "vm/spaces.h" | 14 #include "vm/spaces.h" |
| 15 #include "vm/virtual_memory.h" | 15 #include "vm/virtual_memory.h" |
| 16 #include "vm/visitor.h" | 16 #include "vm/visitor.h" |
| 17 | 17 |
| 18 namespace dart { | 18 namespace dart { |
| 19 | 19 |
| 20 // Forward declarations. | 20 // Forward declarations. |
| 21 class Heap; | 21 class Heap; |
| 22 class Isolate; | 22 class Isolate; |
| 23 class JSONObject; | 23 class JSONObject; |
| 24 class ScavengerVisitor; | 24 class ScavengerVisitor; |
| 25 | 25 |
| 26 DECLARE_FLAG(bool, gc_at_alloc); | 26 DECLARE_FLAG(bool, gc_at_alloc); |
| 27 | 27 |
| 28 |
| 29 // Wrapper around VirtualMemory that adds caching and handles the empty case. |
| 30 class SemiSpace { |
| 31 public: |
| 32 static void InitOnce(); |
| 33 |
| 34 // Get a space of the given size. Returns NULL on out of memory. If size is 0, |
| 35 // returns an empty space: pointer(), start() and end() all return NULL/0. |
| 36 static SemiSpace* New(intptr_t size); |
| 37 |
| 38 // Hand back an unused space. |
| 39 void Delete(); |
| 40 |
| 41 void* pointer() const { return region_.pointer(); } |
| 42 uword start() const { return region_.start(); } |
| 43 uword end() const { return region_.end(); } |
| 44 intptr_t size() const { return static_cast<intptr_t>(region_.size()); } |
| 45 bool Contains(uword address) const { return region_.Contains(address); } |
| 46 |
| 47 // Set write protection mode for this space. The space must not be protected |
| 48 // when Delete is called. |
| 49 // TODO(koda): Remember protection mode in VirtualMemory and assert this. |
| 50 void WriteProtect(bool read_only); |
| 51 |
| 52 private: |
| 53 explicit SemiSpace(VirtualMemory* reserved); |
| 54 ~SemiSpace(); |
| 55 |
| 56 VirtualMemory* reserved_; // NULL for an emtpy space. |
| 57 MemoryRegion region_; |
| 58 |
| 59 static SemiSpace* cache_; |
| 60 static Mutex* mutex_; |
| 61 }; |
| 62 |
| 63 |
| 28 class Scavenger { | 64 class Scavenger { |
| 29 public: | 65 public: |
| 30 Scavenger(Heap* heap, intptr_t max_capacity_in_words, uword object_alignment); | 66 Scavenger(Heap* heap, intptr_t max_capacity_in_words, uword object_alignment); |
| 31 ~Scavenger(); | 67 ~Scavenger(); |
| 32 | 68 |
| 33 // Check whether this Scavenger contains this address. | 69 // Check whether this Scavenger contains this address. |
| 34 // During scavenging both the to and from spaces contain "legal" objects. | 70 // During scavenging both the to and from spaces contain "legal" objects. |
| 35 // During a scavenge this function only returns true for addresses that will | 71 // During a scavenge this function only returns true for addresses that will |
| 36 // be part of the surviving objects. | 72 // be part of the surviving objects. |
| 37 bool Contains(uword addr) const { | 73 bool Contains(uword addr) const { |
| 38 // No reasonable algorithm should be checking for objects in from space. At | 74 // No reasonable algorithm should be checking for objects in from space. At |
| 39 // least unless it is debugging code. This might need to be relaxed later, | 75 // least unless it is debugging code. This might need to be relaxed later, |
| 40 // but currently it helps prevent dumb bugs. | 76 // but currently it helps prevent dumb bugs. |
| 41 ASSERT(!from_->Contains(addr)); | 77 ASSERT(from_ == NULL || !from_->Contains(addr)); |
| 42 return to_->Contains(addr); | 78 return to_->Contains(addr); |
| 43 } | 79 } |
| 44 | 80 |
| 45 RawObject* FindObject(FindObjectVisitor* visitor) const; | 81 RawObject* FindObject(FindObjectVisitor* visitor) const; |
| 46 | 82 |
| 47 uword TryAllocate(intptr_t size) { | 83 uword TryAllocate(intptr_t size) { |
| 48 ASSERT(Utils::IsAligned(size, kObjectAlignment)); | 84 ASSERT(Utils::IsAligned(size, kObjectAlignment)); |
| 49 ASSERT(heap_ != Dart::vm_isolate()->heap()); | 85 ASSERT(heap_ != Dart::vm_isolate()->heap()); |
| 50 #if defined(DEBUG) | 86 #if defined(DEBUG) |
| 51 if (FLAG_gc_at_alloc && !scavenging_) { | 87 if (FLAG_gc_at_alloc && !scavenging_) { |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 ASSERT(end_ <= to_->end()); | 212 ASSERT(end_ <= to_->end()); |
| 177 return result; | 213 return result; |
| 178 } | 214 } |
| 179 bool PromotedStackHasMore() const { | 215 bool PromotedStackHasMore() const { |
| 180 ASSERT(scavenging_); | 216 ASSERT(scavenging_); |
| 181 return end_ < to_->end(); | 217 return end_ < to_->end(); |
| 182 } | 218 } |
| 183 | 219 |
| 184 void ProcessWeakTables(); | 220 void ProcessWeakTables(); |
| 185 | 221 |
| 186 VirtualMemory* space_; | 222 SemiSpace* from_; |
| 187 MemoryRegion* to_; | 223 SemiSpace* to_; |
| 188 MemoryRegion* from_; | |
| 189 | 224 |
| 190 Heap* heap_; | 225 Heap* heap_; |
| 191 | 226 |
| 192 // Current allocation top and end. These values are being accessed directly | 227 // Current allocation top and end. These values are being accessed directly |
| 193 // from generated code. | 228 // from generated code. |
| 194 uword top_; | 229 uword top_; |
| 195 uword end_; | 230 uword end_; |
| 196 | 231 |
| 197 // A pointer to the first unscanned object. Scanning completes when | 232 // A pointer to the first unscanned object. Scanning completes when |
| 198 // this value meets the allocation top. | 233 // this value meets the allocation top. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 215 | 250 |
| 216 friend class ScavengerVisitor; | 251 friend class ScavengerVisitor; |
| 217 friend class ScavengerWeakVisitor; | 252 friend class ScavengerWeakVisitor; |
| 218 | 253 |
| 219 DISALLOW_COPY_AND_ASSIGN(Scavenger); | 254 DISALLOW_COPY_AND_ASSIGN(Scavenger); |
| 220 }; | 255 }; |
| 221 | 256 |
| 222 } // namespace dart | 257 } // namespace dart |
| 223 | 258 |
| 224 #endif // VM_SCAVENGER_H_ | 259 #endif // VM_SCAVENGER_H_ |
| OLD | NEW |