Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(623)

Unified Diff: runtime/bin/vmservice/observatory/lib/object_graph.dart

Issue 773573005: vmservice: basic object graph serialization. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/bin/vmservice/observatory/lib/object_graph.dart
===================================================================
--- runtime/bin/vmservice/observatory/lib/object_graph.dart (revision 0)
+++ runtime/bin/vmservice/observatory/lib/object_graph.dart (working copy)
@@ -0,0 +1,71 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library object_graph;
+
+import 'dart:collection';
+import 'dart:typed_data';
+
+// Port of dart::ReadStream from vm/datastream.h.
+class ReadStream {
+ int _cur = 0;
+ ByteData _data;
Cutch 2014/12/02 16:46:26 could this be final?
koda 2014/12/02 18:47:12 Done.
+
+ ReadStream(this._data);
+
+ int get pendingBytes => _data.lengthInBytes - _cur;
+
+ int readUnsigned() {
+ int result = 0;
+ int shift = 0;
+ while (_data.getUint8(_cur) <= maxUnsignedDataPerByte) {
+ result |= _data.getUint8(_cur) << shift;
+ shift += dataBitsPerByte;
+ ++_cur;
+ }
+ result |= (_data.getUint8(_cur) & byteMask) << shift;
+ ++_cur;
+ return result;
+ }
+
+ static const int dataBitsPerByte = 7;
+ static const int byteMask = (1 << dataBitsPerByte) - 1;
+ static const int maxUnsignedDataPerByte = byteMask;
+}
+
+class ObjectVertex {
+ int _id;
Cutch 2014/12/02 16:46:26 could this be final?
koda 2014/12/02 18:47:12 Done.
+ int size;
+ int classId;
Cutch 2014/12/02 16:46:26 could these be made final? or at least have non-pu
koda 2014/12/02 18:47:12 Hid the setters.
+ List<ObjectVertex> succ = new List<ObjectVertex>();
Cutch 2014/12/02 16:46:26 could this be final?
koda 2014/12/02 18:47:12 Done.
+ ObjectVertex(this._id);
+}
+
+// See implementation of ObjectGraph::Serialize for format.
+class ObjectGraph {
+ Map<int, ObjectVertex> _idToVertex = new Map<int, ObjectVertex>();
Cutch 2014/12/02 16:46:26 could this be final?
koda 2014/12/02 18:47:12 Done.
+
+ ObjectVertex _asVertex(int id) {
+ return _idToVertex.putIfAbsent(id, () => new ObjectVertex(id));
+ }
+
+ void _addFrom(ReadStream stream) {
+ ObjectVertex obj = _asVertex(stream.readUnsigned());
+ obj.size = stream.readUnsigned();
+ obj.classId = stream.readUnsigned();
Cutch 2014/12/02 16:46:26 if you read the id, size, and classId and then cal
koda 2014/12/02 18:47:12 No.
+ int last = stream.readUnsigned();
+ while (last != 0) {
+ obj.succ.add(_asVertex(last));
Cutch 2014/12/02 16:46:26 Oh, I see why you can't make _asVertex take all th
koda 2014/12/02 18:47:12 Right. I added a comment explaining the two-stage
+ last = stream.readUnsigned();
+ }
+ }
+
+ ObjectGraph(ReadStream reader) {
+ while (reader.pendingBytes > 0) {
+ _addFrom(reader);
+ }
+ }
+
+ Iterable<ObjectVertex> get vertices => _idToVertex.values;
+}

Powered by Google App Engine
This is Rietveld 408576698