]> gitweb.factorcode.org Git - factor.git/commitdiff
VM: new iteration method each_object_each_slot()
authorBjörn Lindqvist <bjourne@gmail.com>
Tue, 18 Aug 2015 05:55:36 +0000 (07:55 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 24 Aug 2015 01:36:31 +0000 (18:36 -0700)
used by find_data_references() and dump_edges() instead of the functor structs

vm/debug.cpp
vm/vm.hpp

index 43ec2795ad62b78ec451d49595a303debb31e941..da00e76f876236b2de330bb7e07f1e3c052dddf1 100644 (file)
@@ -298,64 +298,26 @@ void factor_vm::dump_objects(ostream& out, cell type) {
   each_object(object_dumper);
 }
 
-struct find_data_reference_slot_visitor {
-  cell look_for;
-  object* obj;
-  factor_vm* parent;
-  ostream& out;
-
-  find_data_reference_slot_visitor(cell look_for,
-                                   object* obj,
-                                   factor_vm* parent,
-                                   ostream& out)
-    : look_for(look_for), obj(obj), parent(parent), out(out) {}
-
-  void operator()(cell* scan) {
-    if (look_for == *scan) {
+void factor_vm::find_data_references(ostream& out, cell look_for) {
+  auto find_data_ref_func = [&](object* obj, cell* slot) {
+    if (look_for == *slot) {
       out << padded_address((cell)obj) << " ";
-      parent->print_nested_obj(out, tag_dynamic(obj), 2);
+      print_nested_obj(out, tag_dynamic(obj), 2);
       out << endl;
     }
-  }
-};
-
-struct dump_edges_slot_visitor {
-  object* obj;
-  factor_vm* parent;
-  ostream& out;
-
-  dump_edges_slot_visitor(cell, object* obj, factor_vm* parent, ostream& out)
-      : obj(obj), parent(parent), out(out) {}
-
-  void operator()(cell* scan) {
-    if (TAG(*scan) > F_TYPE)
-      out << (void*)tag_dynamic(obj) << " ==> " << (void*)*scan << endl;
-  }
-};
-
-template <typename SlotVisitor> struct data_reference_object_visitor {
-  cell look_for;
-  factor_vm* parent;
-  ostream& out;
-
-  data_reference_object_visitor(cell look_for, factor_vm* parent, ostream& out)
-      : look_for(look_for), parent(parent), out(out) {}
-
-  void operator()(object* obj) {
-    SlotVisitor visitor(look_for, obj, parent, out);
-    obj->each_slot(visitor);
-  }
-};
-
-void factor_vm::find_data_references(ostream& out, cell look_for) {
-  data_reference_object_visitor<find_data_reference_slot_visitor>
-      visitor(look_for, this, out);
-  each_object(visitor);
+  };
+  each_object_each_slot(find_data_ref_func);
 }
 
 void factor_vm::dump_edges(ostream& out) {
-  data_reference_object_visitor<dump_edges_slot_visitor> visitor(0, this, out);
-  each_object(visitor);
+  auto dump_edges_func = [&](object* obj, cell* scan) {
+    if (TAG(*scan) > F_TYPE) {
+      out << (void*)tag_dynamic(obj);
+      out << " ==> ";
+      out << (void*)*scan << endl;
+    }
+  };
+  each_object_each_slot(dump_edges_func);
 }
 
 struct code_block_printer {
index 3d47484f3a3f28dab4221d2771e49c6c93c4207d..545cab10c1928db21260f753c45e2b95ef0ed0f5 100644 (file)
--- a/vm/vm.hpp
+++ b/vm/vm.hpp
@@ -330,6 +330,17 @@ struct factor_vm {
     gc_off = false;
   }
 
+  template <typename Iterator>
+  inline void each_object_each_slot(Iterator& iterator) {
+    auto each_object_func = [&](object* obj) {
+      auto each_slot_func = [&](cell* slot) {
+        iterator(obj, slot);
+      };
+      obj->each_slot(each_slot_func);
+    };
+    each_object(each_object_func);
+  }
+
   /* the write barrier must be called any time we are potentially storing a
      pointer from an older generation to a younger one */
   inline void write_barrier(cell* slot_ptr) {