OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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/object_graph.h" | 5 #include "vm/object_graph.h" |
6 | 6 |
7 #include "vm/dart.h" | 7 #include "vm/dart.h" |
8 #include "vm/growable_array.h" | 8 #include "vm/growable_array.h" |
9 #include "vm/isolate.h" | 9 #include "vm/isolate.h" |
10 #include "vm/object.h" | 10 #include "vm/object.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 ObjectGraph::Visitor* visitor) { | 139 ObjectGraph::Visitor* visitor) { |
140 NoGCScope no_gc_scope_; | 140 NoGCScope no_gc_scope_; |
141 Stack stack(isolate_); | 141 Stack stack(isolate_); |
142 RawObject* root_raw = root.raw(); | 142 RawObject* root_raw = root.raw(); |
143 stack.VisitPointer(&root_raw); | 143 stack.VisitPointer(&root_raw); |
144 stack.TraverseGraph(visitor); | 144 stack.TraverseGraph(visitor); |
145 // TODO(koda): Optimize if we only visited a small subgraph. | 145 // TODO(koda): Optimize if we only visited a small subgraph. |
146 Unmarker::UnmarkAll(isolate_); | 146 Unmarker::UnmarkAll(isolate_); |
147 } | 147 } |
148 | 148 |
| 149 |
| 150 class SizeVisitor : public ObjectGraph::Visitor { |
| 151 public: |
| 152 SizeVisitor() : size_(0) { } |
| 153 intptr_t size() const { return size_; } |
| 154 virtual bool ShouldSkip(RawObject* obj) const { return false; } |
| 155 virtual Direction VisitObject(ObjectGraph::StackIterator* it) { |
| 156 RawObject* obj = it->Get(); |
| 157 if (ShouldSkip(obj)) { |
| 158 return kBacktrack; |
| 159 } |
| 160 size_ += obj->Size(); |
| 161 return kProceed; |
| 162 } |
| 163 private: |
| 164 intptr_t size_; |
| 165 }; |
| 166 |
| 167 |
| 168 class SizeExcludingObjectVisitor : public SizeVisitor { |
| 169 public: |
| 170 explicit SizeExcludingObjectVisitor(const Object& skip) : skip_(skip) { } |
| 171 virtual bool ShouldSkip(RawObject* obj) const { return obj == skip_.raw(); } |
| 172 private: |
| 173 const Object& skip_; |
| 174 }; |
| 175 |
| 176 |
| 177 class SizeExcludingClassVisitor : public SizeVisitor { |
| 178 public: |
| 179 explicit SizeExcludingClassVisitor(intptr_t skip) : skip_(skip) { } |
| 180 virtual bool ShouldSkip(RawObject* obj) const { |
| 181 return obj->GetClassId() == skip_; |
| 182 } |
| 183 private: |
| 184 const intptr_t skip_; |
| 185 }; |
| 186 |
| 187 |
| 188 intptr_t ObjectGraph::SizeRetainedByInstance(const Object& obj) { |
| 189 SizeVisitor total; |
| 190 IterateObjects(&total); |
| 191 intptr_t size_total = total.size(); |
| 192 SizeExcludingObjectVisitor excluding_obj(obj); |
| 193 IterateObjects(&excluding_obj); |
| 194 intptr_t size_excluding_obj = excluding_obj.size(); |
| 195 return size_total - size_excluding_obj; |
| 196 } |
| 197 |
| 198 |
| 199 intptr_t ObjectGraph::SizeRetainedByClass(intptr_t class_id) { |
| 200 SizeVisitor total; |
| 201 IterateObjects(&total); |
| 202 intptr_t size_total = total.size(); |
| 203 SizeExcludingClassVisitor excluding_class(class_id); |
| 204 IterateObjects(&excluding_class); |
| 205 intptr_t size_excluding_class = excluding_class.size(); |
| 206 return size_total - size_excluding_class; |
| 207 } |
| 208 |
149 } // namespace dart | 209 } // namespace dart |
OLD | NEW |