[Quipu-dev] quipu/quipu: Refactor init operations

Back to archive index

scmno****@osdn***** scmno****@osdn*****
Mon Jun 18 06:16:12 JST 2018


changeset 6f9cbcb4479e in quipu/quipu
details: http://hg.osdn.jp/view/quipu/quipu?cmd=changeset;node=6f9cbcb4479e
user: Agustina Arzille <avarz****@riseu*****>
date: Sun Jun 17 21:15:58 2018 +0000
description: Refactor init operations

diffstat:

 array.cpp    |   4 +-
 builtins.cpp |  14 +++++++++---
 bvector.cpp  |   4 +-
 cons.cpp     |   4 +-
 event.cpp    |  13 ++++++++---
 floatp.cpp   |   4 +-
 initop.cpp   |  50 +++++++++---------------------------------------
 initop.h     |  61 +++++++++++++++++++++++++++++++++++++++++++----------------
 io.cpp       |  13 ++++++++---
 memory.cpp   |   4 +-
 quipu.cpp    |   7 +++++-
 stream.cpp   |   4 +-
 symbol.cpp   |  10 ++++++--
 thread.cpp   |   8 +++---
 xtime.cpp    |  10 ++++----
 15 files changed, 116 insertions(+), 94 deletions(-)

diffs (truncated from 494 to 300 lines):

diff -r f896a868038c -r 6f9cbcb4479e array.cpp
--- a/array.cpp	Thu Jun 14 18:24:56 2018 -0300
+++ b/array.cpp	Sun Jun 17 21:15:58 2018 +0000
@@ -319,12 +319,12 @@
   return (ret);
 }
 
-static bool
+static int
 do_init_array (interpreter *)
 {
   empty_array.type = typecode::ARRAY;
   empty_array.size = sizeof (empty_array);
-  return (true);
+  return (init_op::result_ok);
 }
 
 init_op init_array (do_init_array, "array");
diff -r f896a868038c -r 6f9cbcb4479e builtins.cpp
--- a/builtins.cpp	Thu Jun 14 18:24:56 2018 -0300
+++ b/builtins.cpp	Sun Jun 17 21:15:58 2018 +0000
@@ -1430,9 +1430,16 @@
     }
 }
 
-static bool
+
+QP_EXPORT init_op init_symbols;
+
+static int
 do_init_builtins (interpreter *interp)
 {
+  int ret = init_op::call_deps (interp, &init_symbols);
+  if (ret != init_op::result_ok)
+    return (ret);
+
   const char *names = BUILTIN_NAMES;
   const native_function::fn_type *fcts = BUILTINS;
   native_function *outp = global_builtins;
@@ -1443,11 +1450,10 @@
       names += strlen (names) + 1;
     }
 
-  return (true);
+  return (ret);
 }
 
-QP_EXPORT init_op init_symbols;
-init_op init_builtins (do_init_builtins, "builtins", &init_symbols);
+init_op init_builtins (do_init_builtins, "builtins");
 
 QP_DECLS_END
 
diff -r f896a868038c -r 6f9cbcb4479e bvector.cpp
--- a/bvector.cpp	Thu Jun 14 18:24:56 2018 -0300
+++ b/bvector.cpp	Sun Jun 17 21:15:58 2018 +0000
@@ -587,7 +587,7 @@
   return (sp->hval);
 }
 
-static bool
+static int
 do_init_bvector (interpreter *)
 {
   static const unsigned char empty_data[] = { 0 };
@@ -596,7 +596,7 @@
   empty_string.full = FLAGS_CONST;
   empty_string.type = typecode::STR;
   empty_bvector.data = empty_string.data = (unsigned char *)empty_data;
-  return (true);
+  return (init_op::result_ok);
 }
 
 init_op init_bvector (do_init_bvector, "bvector");
diff -r f896a868038c -r 6f9cbcb4479e cons.cpp
--- a/cons.cpp	Thu Jun 14 18:24:56 2018 -0300
+++ b/cons.cpp	Sun Jun 17 21:15:58 2018 +0000
@@ -433,13 +433,13 @@
 // External definitions.
 object NIL;
 
-static bool
+static int
 do_init_cons (interpreter *)
 {
   static cons nil;
   NIL = nil.car = nil.cdr =
     ((cons *)ensure_mask (&nil, TYPE_SHIFT))->as_obj ();
-  return (true);
+  return (init_op::result_ok);
 }
 
 init_op init_cons (do_init_cons, "cons");
diff -r f896a868038c -r 6f9cbcb4479e event.cpp
--- a/event.cpp	Thu Jun 14 18:24:56 2018 -0300
+++ b/event.cpp	Sun Jun 17 21:15:58 2018 +0000
@@ -46,9 +46,15 @@
   qp_return (ev_handlers[ev]);
 }
 
-static bool
+QP_EXPORT init_op init_symbols;
+
+static int
 do_init_events (interpreter *interp)
 {
+  int ret = init_op::call_deps (interp, &init_symbols);
+  if (ret != init_op::result_ok)
+    return (ret);
+
   lwlock_init (&evh_lock);
   for (unsigned int i = 0; i < NPENDEV; ++i)
     ev_handlers[i] = UNBOUND;
@@ -77,10 +83,9 @@
   gcreq_fct.name = "gc-request";
   ev_handlers[GCREQ_EV - 1] = gcreq_fct.as_obj ();
 
-  return (true);
+  return (init_op::result_ok);
 }
 
-QP_EXPORT init_op init_symbols;
-init_op init_event (do_init_events, "events", &init_symbols);
+init_op init_event (do_init_events, "events");
 
 QP_DECLS_END
diff -r f896a868038c -r 6f9cbcb4479e floatp.cpp
--- a/floatp.cpp	Thu Jun 14 18:24:56 2018 -0300
+++ b/floatp.cpp	Sun Jun 17 21:15:58 2018 +0000
@@ -25,7 +25,7 @@
 object FLT_QNAN;
 object FLT_ZERO;
 
-static bool
+static int
 do_init_float (interpreter *)
 {
   static fltval SF_PINF;
@@ -64,7 +64,7 @@
   FLT_ZERO = SF_ZERO.as_obj ();
 #endif
 
-  return (true);
+  return (init_op::result_ok);
 }
 
 init_op init_float (do_init_float, "float");
diff -r f896a868038c -r 6f9cbcb4479e initop.cpp
--- a/initop.cpp	Thu Jun 14 18:24:56 2018 -0300
+++ b/initop.cpp	Sun Jun 17 21:15:58 2018 +0000
@@ -25,13 +25,6 @@
 
 void init_op_list::add (init_op *op)
 {
-  if (op->deps != nullptr)
-    {
-      init_op **tmp = make_initops (op->ndeps);
-      memcpy (tmp, op->deps, op->ndeps * sizeof (*tmp));
-      op->deps = tmp;
-    }
-
   if (this->size <= this->nops)
     {
       init_op **tmp = make_initops (this->size *= 2);
@@ -45,46 +38,23 @@
 
 bool init_op_list::call (interpreter *interp)
 {
-  while (true)
+  int ret = init_op::result_ok;
+
+  for (int i = 0; i < this->nops; ++i)
     {
-      init_op *op = nullptr;
-
-      for (int i = 0; i < this->nops; ++i)
+      init_op *op = this->ops[i];
+      ret = op->call (interp);
+      if (ret == init_op::result_circular)
         {
-          init_op *tmp = this->ops[i];
-          if (tmp->ndeps == 0)
-            {
-              op = tmp;
-              xfree (op->deps);
-              this->ops[i] = this->ops[--this->nops];
-              break;
-            }
-        }
-
-      if (!op)
-        {
-          fputs ("Failed to satisfy dependencies", stderr);
+          fprintf (stderr, "Circular dependency detected "
+            "in call to %s\n", op->name);
           return (false);
         }
-      else if (!op->cb (interp))
+      else if (ret == init_op::result_failed)
         {
-          fprintf (stderr, "Failed to initialize: %s\n", op->name);
+          fprintf (stderr, "Failed to initialize %s\n", op->name);
           return (false);
         }
-
-      if (!this->nops)
-        break;
-
-      for (int i = 0; i < this->nops; ++i)
-        {
-          init_op *tmp = this->ops[i];
-          for (int j = 0; j < tmp->ndeps; ++j)
-            if (op == tmp->deps[j])
-              {
-                tmp->deps[j] = tmp->deps[--tmp->ndeps];
-                break;
-              }
-        }
     }
 
   xfree (this->ops);
diff -r f896a868038c -r 6f9cbcb4479e initop.h
--- a/initop.h	Thu Jun 14 18:24:56 2018 -0300
+++ b/initop.h	Sun Jun 17 21:15:58 2018 +0000
@@ -5,8 +5,8 @@
 
 QP_DECLS_BEGIN
 
+class interpreter;
 class init_op;
-class interpreter;
 
 class init_op_list
 {
@@ -25,29 +25,56 @@
 class init_op
 {
 public:
-  init_op **deps;
-  int ndeps;
-  bool (*cb) (interpreter *);
+  enum
+    {
+      st_init,
+      st_wip,
+      st_done
+    };
+
+  enum
+    {
+      result_ok,
+      result_circular,
+      result_failed
+    };
+
+  int state = st_init;
+  int (*cb) (interpreter *);
   const char *name;
 
-  init_op (bool (*__cb) (interpreter *), const char *__name)
+  template <class ...Args>
+  static int call_deps (interpreter *__interp, Args... __args)
     {
-      this->deps = nullptr;
-      this->ndeps = 0;
-      this->cb = __cb;
-      this->name = __name;
+      init_op *__deps[] = { __args... };
+      int __ret = result_ok;
+
+      for (auto __p : __deps)
+        if ((__ret = __p->call (__interp)) != result_ok)
+          break;
+
+      return (__ret);
+    }
+
+  init_op (int (*__cb) (interpreter *), const char *__nm) :
+      cb (__cb), name (__nm)
+    {
       init_op_list::get().add (this);
     }
 
-  template <class ...Args>
-  init_op (bool (*__cb) (interpreter *), const char *__name, Args... __args)
+  int call (interpreter *__interp)
     {
-      init_op *__ops[] = { __args... };
-      this->deps = __ops;
-      this->ndeps = (int)QP_NELEM (__ops);
-      this->cb = __cb;
-      this->name = __name;
-      init_op_list::get().add (this);
+      if (this->state == st_wip)
+        return (result_circular);
+      else if (this->state == st_done)
+        return (result_ok);
+
+      this->state = st_wip;
+      int __ret = this->cb (__interp);




More information about the Quipu-dev mailing list
Back to archive index