OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "platform/assert.h" |
| 6 #include "vm/object_graph.h" |
| 7 #include "vm/unit_test.h" |
| 8 |
| 9 namespace dart { |
| 10 |
| 11 class Counter : public ObjectGraph::Visitor { |
| 12 public: |
| 13 // Records the number of objects and total size visited, excluding 'skip' |
| 14 // and any objects only reachable through 'skip'. |
| 15 Counter(RawObject* skip, RawObject* expected_parent) |
| 16 : count_(0), size_(0), skip_(skip), expected_parent_(expected_parent) { } |
| 17 |
| 18 virtual Direction VisitObject(ObjectGraph::StackIterator* it) { |
| 19 RawObject* obj = it->Get(); |
| 20 if (obj == skip_) { |
| 21 EXPECT(it->MoveToParent()); |
| 22 EXPECT_EQ(expected_parent_, it->Get()); |
| 23 return kBacktrack; |
| 24 } |
| 25 ++count_; |
| 26 size_ += obj->Size(); |
| 27 return kProceed; |
| 28 } |
| 29 |
| 30 int count() const { return count_; } |
| 31 int size() const { return size_; } |
| 32 |
| 33 private: |
| 34 int count_; |
| 35 intptr_t size_; |
| 36 RawObject* skip_; |
| 37 RawObject* expected_parent_; |
| 38 }; |
| 39 |
| 40 |
| 41 TEST_CASE(ObjectGraph) { |
| 42 Isolate* isolate = Isolate::Current(); |
| 43 // Create a simple object graph with objects a, b, c, d: |
| 44 // a+->b+->c |
| 45 // + + |
| 46 // | v |
| 47 // +-->d |
| 48 Array& a = Array::Handle(Array::New(2, Heap::kNew)); |
| 49 Array& b = Array::Handle(Array::New(2, Heap::kOld)); |
| 50 Array& c = Array::Handle(Array::New(0, Heap::kOld)); |
| 51 Array& d = Array::Handle(Array::New(0, Heap::kOld)); |
| 52 a.SetAt(0, b); |
| 53 b.SetAt(0, c); |
| 54 b.SetAt(1, d); |
| 55 a.SetAt(1, d); |
| 56 intptr_t b_size = b.raw()->Size(); |
| 57 intptr_t c_size = c.raw()->Size(); |
| 58 { |
| 59 // No more allocation; raw pointers ahead. |
| 60 NoGCScope no_gc_scope; |
| 61 RawObject* b_raw = b.raw(); |
| 62 // Clear handles to cut unintended retained paths. |
| 63 b = Array::null(); |
| 64 c = Array::null(); |
| 65 d = Array::null(); |
| 66 ObjectGraph graph(isolate); |
| 67 { |
| 68 // Compare count and size when 'b' is/isn't skipped. |
| 69 Counter with(Object::null(), Object::null()); |
| 70 graph.IterateObjectsFrom(a, &with); |
| 71 Counter without(b_raw, a.raw()); |
| 72 graph.IterateObjectsFrom(a, &without); |
| 73 // Only 'b' and 'c' were cut off. |
| 74 EXPECT_EQ(2, with.count() - without.count()); |
| 75 EXPECT_EQ(b_size + c_size, |
| 76 with.size() - without.size()); |
| 77 } |
| 78 { |
| 79 // Like above, but iterate over the entire isolate. The counts and sizes |
| 80 // are thus larger, but the difference should still be just 'b' and 'c'. |
| 81 Counter with(Object::null(), Object::null()); |
| 82 graph.IterateObjects(&with); |
| 83 Counter without(b_raw, a.raw()); |
| 84 graph.IterateObjects(&without); |
| 85 EXPECT_EQ(2, with.count() - without.count()); |
| 86 EXPECT_EQ(b_size + c_size, |
| 87 with.size() - without.size()); |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 } // namespace dart |
OLD | NEW |