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 RUNTIME_VM_OBJECT_SET_H_ | 5 #ifndef RUNTIME_VM_OBJECT_SET_H_ |
6 #define RUNTIME_VM_OBJECT_SET_H_ | 6 #define RUNTIME_VM_OBJECT_SET_H_ |
7 | 7 |
8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
9 #include "vm/bit_vector.h" | 9 #include "vm/bit_vector.h" |
10 #include "vm/globals.h" | 10 #include "vm/globals.h" |
11 #include "vm/raw_object.h" | 11 #include "vm/raw_object.h" |
12 #include "vm/zone.h" | 12 #include "vm/zone.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 | 15 |
16 class ObjectSetRegion : public ZoneAllocated { | 16 class ObjectSetRegion : public ZoneAllocated { |
17 public: | 17 public: |
18 ObjectSetRegion(Zone* zone, uword start, uword end) | 18 ObjectSetRegion(Zone* zone, uword start, uword end) |
19 : start_(start), | 19 : start_(start), |
20 end_(end), | 20 end_(end), |
21 bit_vector_(zone, (end - start) >> kWordSizeLog2), | 21 bit_vector_(zone, (end - start) >> kWordSizeLog2), |
22 next_(NULL) { | 22 next_(NULL) {} |
23 } | |
24 | 23 |
25 bool ContainsAddress(uword address) { | 24 bool ContainsAddress(uword address) { |
26 return address >= start_ && address < end_; | 25 return address >= start_ && address < end_; |
27 } | 26 } |
28 | 27 |
29 intptr_t IndexForAddress(uword address) { | 28 intptr_t IndexForAddress(uword address) { |
30 ASSERT(Utils::IsAligned(address, kWordSize)); | 29 ASSERT(Utils::IsAligned(address, kWordSize)); |
31 return (address - start_) >> kWordSizeLog2; | 30 return (address - start_) >> kWordSizeLog2; |
32 } | 31 } |
33 | 32 |
34 void AddObject(uword address) { | 33 void AddObject(uword address) { bit_vector_.Add(IndexForAddress(address)); } |
35 bit_vector_.Add(IndexForAddress(address)); | |
36 } | |
37 | 34 |
38 bool ContainsObject(uword address) { | 35 bool ContainsObject(uword address) { |
39 return bit_vector_.Contains(IndexForAddress(address)); | 36 return bit_vector_.Contains(IndexForAddress(address)); |
40 } | 37 } |
41 | 38 |
42 ObjectSetRegion* next() { return next_; } | 39 ObjectSetRegion* next() { return next_; } |
43 void set_next(ObjectSetRegion* region) { next_ = region; } | 40 void set_next(ObjectSetRegion* region) { next_ = region; } |
44 | 41 |
45 private: | 42 private: |
46 uword start_; | 43 uword start_; |
47 uword end_; | 44 uword end_; |
48 BitVector bit_vector_; | 45 BitVector bit_vector_; |
49 ObjectSetRegion* next_; | 46 ObjectSetRegion* next_; |
50 }; | 47 }; |
51 | 48 |
52 class ObjectSet : public ZoneAllocated { | 49 class ObjectSet : public ZoneAllocated { |
53 public: | 50 public: |
54 explicit ObjectSet(Zone* zone) : zone_(zone), head_(NULL) { } | 51 explicit ObjectSet(Zone* zone) : zone_(zone), head_(NULL) {} |
55 | 52 |
56 void AddRegion(uword start, uword end) { | 53 void AddRegion(uword start, uword end) { |
57 ObjectSetRegion* region = new(zone_) ObjectSetRegion(zone_, start, end); | 54 ObjectSetRegion* region = new (zone_) ObjectSetRegion(zone_, start, end); |
58 region->set_next(head_); | 55 region->set_next(head_); |
59 head_ = region; | 56 head_ = region; |
60 } | 57 } |
61 | 58 |
62 bool Contains(RawObject* raw_obj) const { | 59 bool Contains(RawObject* raw_obj) const { |
63 uword raw_addr = RawObject::ToAddr(raw_obj); | 60 uword raw_addr = RawObject::ToAddr(raw_obj); |
64 for (ObjectSetRegion* region = head_; | 61 for (ObjectSetRegion* region = head_; region != NULL; |
65 region != NULL; | |
66 region = region->next()) { | 62 region = region->next()) { |
67 if (region->ContainsAddress(raw_addr)) { | 63 if (region->ContainsAddress(raw_addr)) { |
68 return region->ContainsObject(raw_addr); | 64 return region->ContainsObject(raw_addr); |
69 } | 65 } |
70 } | 66 } |
71 return false; | 67 return false; |
72 } | 68 } |
73 | 69 |
74 void Add(RawObject* raw_obj) { | 70 void Add(RawObject* raw_obj) { |
75 uword raw_addr = RawObject::ToAddr(raw_obj); | 71 uword raw_addr = RawObject::ToAddr(raw_obj); |
76 for (ObjectSetRegion* region = head_; | 72 for (ObjectSetRegion* region = head_; region != NULL; |
77 region != NULL; | |
78 region = region->next()) { | 73 region = region->next()) { |
79 if (region->ContainsAddress(raw_addr)) { | 74 if (region->ContainsAddress(raw_addr)) { |
80 return region->AddObject(raw_addr); | 75 return region->AddObject(raw_addr); |
81 } | 76 } |
82 } | 77 } |
83 FATAL("Address not in any heap region"); | 78 FATAL("Address not in any heap region"); |
84 } | 79 } |
85 | 80 |
86 private: | 81 private: |
87 Zone* zone_; | 82 Zone* zone_; |
88 ObjectSetRegion* head_; | 83 ObjectSetRegion* head_; |
89 }; | 84 }; |
90 | 85 |
91 } // namespace dart | 86 } // namespace dart |
92 | 87 |
93 #endif // RUNTIME_VM_OBJECT_SET_H_ | 88 #endif // RUNTIME_VM_OBJECT_SET_H_ |
OLD | NEW |