changeset 1168:e2036dce97ea

[project @ 1995-03-10 18:49:55 by jwe]
author jwe
date Fri, 10 Mar 1995 18:50:02 +0000
parents 364906259d4a
children 95a92c971e1e
files src/pt-cmd.cc src/pt-cmd.h src/pt-const.cc src/pt-const.h src/tc-rep.cc src/tc-rep.h
diffstat 6 files changed, 227 insertions(+), 92 deletions(-) [+]
line wrap: on
line diff
--- a/src/pt-cmd.cc	Fri Mar 10 16:39:45 1995 +0000
+++ b/src/pt-cmd.cc	Fri Mar 10 18:50:02 1995 +0000
@@ -50,7 +50,7 @@
 #include "unwind-prot.h"
 
 // Decide if it's time to quit a for or while loop.
-static int
+static inline int
 quit_loop_now (void)
 {
 // Maybe handle `continue N' someday...
@@ -240,6 +240,87 @@
   delete list;
 }
 
+inline void
+tree_for_command::do_for_loop_once (tree_constant& rhs, int& quit)
+{
+  quit = 0;
+
+  tree_constant *tmp = new tree_constant (rhs);
+  tree_simple_assignment_expression tmp_ass (id, tmp, 1);
+  tmp_ass.eval (0);
+
+  if (error_state)
+    {
+      eval_error ();
+      return;
+    }
+
+  if (list)
+    {
+      list->eval (1);
+      if (error_state)
+	{
+	  eval_error ();
+	  quit = 1;
+	  return;
+	}
+    }
+
+  quit = quit_loop_now ();
+}
+
+inline void
+tree_for_command::do_for_loop_once (tree_identifier *ident,
+				    tree_constant& rhs, int& quit)
+{
+  quit = 0;
+
+  ident->assign (rhs);
+
+  if (error_state)
+    {
+      eval_error ();
+      return;
+    }
+
+  if (list)
+    {
+      list->eval (1);
+      if (error_state)
+	{
+	  eval_error ();
+	  quit = 1;
+	  return;
+	}
+    }
+
+  quit = quit_loop_now ();
+}
+
+#define DO_LOOP(val) \
+  do \
+    { \
+      if (ident) \
+	for (int i = 0; i < steps; i++) \
+	  { \
+	    tree_constant rhs (val); \
+	    int quit = 0; \
+	    do_for_loop_once (ident, rhs, quit); \
+	    if (quit) \
+	      break; \
+	  } \
+      else \
+	for (int i = 0; i < steps; i++) \
+	  { \
+	    tree_constant rhs (val); \
+	    int quit = 0; \
+	    do_for_loop_once (rhs, quit); \
+	    if (quit) \
+	      break; \
+	  } \
+    } \
+  while (0)
+
 void
 tree_for_command::eval (void)
 {
@@ -254,11 +335,21 @@
       return;
     }
 
+  tree_identifier *ident = 0;
+  if (! id->arg_list ())
+    {
+      tree_indirect_ref *idr = id->ident ();
+      if (idr->is_identifier_only ())
+	ident = idr->ident ();
+    }
+
   if (tmp_expr.is_scalar_type ())
     {
-      tree_constant *rhs = new tree_constant (tmp_expr);
       int quit = 0;
-      do_for_loop_once (rhs, quit);
+      if (ident)
+	do_for_loop_once (ident, tmp_expr, quit);
+      else
+	do_for_loop_once (tmp_expr, quit);
     }
   else if (tmp_expr.is_matrix_type ())
     {
@@ -279,29 +370,19 @@
 	  steps = cm_tmp.columns ();
 	}
 
-      for (int i = 0; i < steps; i++)
+      if (tmp_expr.is_real_matrix ())
 	{
-	  tree_constant *rhs = 0;
-
 	  if (nr == 1)
-	    {
-	      if (tmp_expr.is_real_matrix ())
-		rhs = new tree_constant (m_tmp (0, i));
-	      else
-		rhs = new tree_constant (cm_tmp (0, i));
-	    }
+	    DO_LOOP(m_tmp (0, i));
 	  else
-	    {
-	      if (tmp_expr.is_real_matrix ())
-		rhs = new tree_constant (m_tmp.extract (0, i, nr-1, i));
-	      else
-		rhs = new tree_constant (cm_tmp.extract (0, i, nr-1, i));
-	    }
-
-	  int quit = 0;
-	  do_for_loop_once (rhs, quit);
-	  if (quit)
-	    break;
+	    DO_LOOP(m_tmp.extract (0, i, nr-1, i));
+	}
+      else
+	{
+	  if (nr == 1)
+	    DO_LOOP(cm_tmp (0, i));
+	  else
+	    DO_LOOP(cm_tmp.extract (0, i, nr-1, i));
 	}
     }
   else if (tmp_expr.is_string ())
@@ -316,16 +397,35 @@
       double b = rng.base ();
       double increment = rng.inc ();
 
-      for (int i = 0; i < steps; i++)
+      if (ident)
 	{
-	  double tmp_val = b + i * increment;
+	  for (int i = 0; i < steps; i++)
+	    {
+	      double tmp_val = b + i * increment;
+
+	      tree_constant rhs (tmp_val);
+
+	      int quit = 0;
+	      do_for_loop_once (ident, rhs, quit);
 
-	  tree_constant *rhs = new tree_constant (tmp_val);
+	      if (quit)
+		break;
+	    }
+	}
+      else
+	{
+	  for (int i = 0; i < steps; i++)
+	    {
+	      double tmp_val = b + i * increment;
 
-	  int quit = 0;
-	  do_for_loop_once (rhs, quit);
-	  if (quit)
-	    break;
+	      tree_constant rhs (tmp_val);
+
+	      int quit = 0;
+	      do_for_loop_once (rhs, quit);
+
+	      if (quit)
+		break;
+	    }
 	}
     }
   else
@@ -344,35 +444,6 @@
 }
 
 void
-tree_for_command::do_for_loop_once (tree_constant *rhs, int& quit)
-{
-  quit = 0;
-
-  tree_simple_assignment_expression tmp_ass (id, rhs, 1);
-
-  tmp_ass.eval (0);
-
-  if (error_state)
-    {
-      eval_error ();
-      return;
-    }
-
-  if (list)
-    {
-      list->eval (1);
-      if (error_state)
-	{
-	  eval_error ();
-	  quit = 1;
-	  return;
-	}
-    }
-
-  quit = quit_loop_now ();
-}
-
-void
 tree_for_command::print_code (ostream& os)
 {
   print_code_indent (os);
--- a/src/pt-cmd.h	Fri Mar 10 16:39:45 1995 +0000
+++ b/src/pt-cmd.h	Fri Mar 10 18:50:02 1995 +0000
@@ -31,6 +31,7 @@
 class tree_if_command_list;
 class tree_expression;
 class tree_index_expression;
+class tree_identifier;
 class tree_constant;
 class symbol_record;
 
@@ -149,7 +150,10 @@
   void print_code (ostream& os);
 
 private:
-  void do_for_loop_once (tree_constant *rhs, int& quit);
+  void do_for_loop_once (tree_identifier *ident,
+			 tree_constant& rhs, int& quit);
+
+  void do_for_loop_once (tree_constant& rhs, int& quit);
 
   tree_index_expression *id;	// Identifier to modify.
   tree_expression *expr;	// Expression to evaluate.
--- a/src/pt-const.cc	Fri Mar 10 16:39:45 1995 +0000
+++ b/src/pt-const.cc	Fri Mar 10 18:50:02 1995 +0000
@@ -33,6 +33,18 @@
 #include "user-prefs.h"
 #include "oct-map.h"
 
+// The following three variables could be made static members of the
+// tree_constant class.
+
+// Pointer to the blocks of memory we manage.
+static tree_constant *newlist;
+
+// Multiplier for allocating new blocks.
+static const int newlist_grow_size = 128;
+
+// Pointer to the last element of the last block allocated.
+static tree_constant *newlist_tail = 0;
+
 Octave_map
 tree_constant::map_value (void) const
 {
@@ -53,22 +65,39 @@
     }
 }
 
-#if defined (MDEBUG)
 void *
 tree_constant::operator new (size_t size)
 {
-  tree_constant *p = ::new tree_constant;
-  cerr << "tree_constant::new(): " << p << "\n";
-  return p;
+  assert (size == sizeof (tree_constant));
+
+  if (! newlist)
+    {
+      int block_size = newlist_grow_size * sizeof (tree_constant);
+      newlist = (tree_constant *) new char [block_size];
+
+      for (int i = 0; i < newlist_grow_size - 1; i++)
+	newlist[i].freeptr = &newlist[i+1];
+
+      newlist[i].freeptr = 0;
+
+      if (newlist_tail)
+	newlist_tail->freeptr = newlist;
+
+      newlist_tail = &newlist[i];
+    }
+
+  tree_constant *tmp = newlist;
+  newlist = newlist->freeptr;
+  return tmp;
 }
 
 void
 tree_constant::operator delete (void *p, size_t size)
 {
-  cerr << "tree_constant::delete(): " << p << "\n";
-  ::delete p;
+  tree_constant *tmp = (tree_constant *) p;
+  tmp->freeptr = newlist;
+  newlist = tmp;
 }
-#endif
 
 // Simple assignment.
 
--- a/src/pt-const.h	Fri Mar 10 16:39:45 1995 +0000
+++ b/src/pt-const.h	Fri Mar 10 18:50:02 1995 +0000
@@ -47,11 +47,15 @@
 {
 private:
 
+// The real representation of a constant, declared in tc-rep.h
+
 #include "tc-rep.h"
 
-// The real representation of a constant, declared in tc-rep.h
-
-  tree_constant_rep *rep;
+  union
+    {
+      tree_constant *freeptr;  // For custom memory management.
+      tree_constant_rep *rep;  // The real representation.
+    };
 
 public:
 
@@ -151,10 +155,8 @@
 
   ~tree_constant (void);
 
-#if defined (MDEBUG)
   void *operator new (size_t size);
   void operator delete (void *p, size_t size);
-#endif
 
 // Simple assignment.
 
--- a/src/tc-rep.cc	Fri Mar 10 16:39:45 1995 +0000
+++ b/src/tc-rep.cc	Fri Mar 10 18:50:02 1995 +0000
@@ -49,6 +49,18 @@
 
 #include "tc-inlines.h"
 
+// The following three variables could be made static members of the
+// TC_REP class.
+
+// Pointer to the blocks of memory we manage.
+static TC_REP *newlist;
+
+// Multiplier for allocating new blocks.
+static const int newlist_grow_size = 128;
+
+// Pointer to the last element of the last block allocated.
+static TC_REP *newlist_tail = 0;
+
 static int
 any_element_is_complex (const ComplexMatrix& a)
 {
@@ -446,22 +458,39 @@
   delete [] orig_text;
 }
 
-#if defined (MDEBUG)
 void *
 TC_REP::operator new (size_t size)
 {
-  tree_constant_rep *p = ::new tree_constant_rep;
-  cerr << "TC_REP::new(): " << p << "\n";
-  return p;
+  assert (size == sizeof (TC_REP));
+
+  if (! newlist)
+    {
+      int block_size = newlist_grow_size * sizeof (TC_REP);
+      newlist = (TC_REP *) new char [block_size];
+
+      for (int i = 0; i < newlist_grow_size - 1; i++)
+	newlist[i].freeptr = &newlist[i+1];
+
+      newlist[i].freeptr = 0;
+
+      if (newlist_tail)
+	newlist_tail->freeptr = newlist;
+
+      newlist_tail = &newlist[i];
+    }
+
+  TC_REP *tmp = newlist;
+  newlist = newlist->freeptr;
+  return tmp;
 }
 
 void
 TC_REP::operator delete (void *p, size_t size)
 {
-  cerr << "TC_REP::delete(): " << p << "\n";
-  ::delete p;
+  TC_REP *tmp = (TC_REP *) p;
+  tmp->freeptr = newlist;
+  newlist = tmp;
 }
-#endif
 
 int
 TC_REP::rows (void) const
--- a/src/tc-rep.h	Fri Mar 10 16:39:45 1995 +0000
+++ b/src/tc-rep.h	Fri Mar 10 18:50:02 1995 +0000
@@ -81,10 +81,8 @@
 
   ~tree_constant_rep (void);
 
-#if defined (MDEBUG)
   void *operator new (size_t size);
   void operator delete (void *p, size_t size);
-#endif
 
   int rows (void) const;
   int columns (void) const;
@@ -373,20 +371,22 @@
 
 // Data.
 
-  int count;
+  union
+    {
+      double scalar;		      // A real scalar constant.
+      Matrix *matrix;		      // A real matrix constant.
+      Complex *complex_scalar;	      // A real scalar constant.
+      ComplexMatrix *complex_matrix;  // A real matrix constant.
+      char *string;		      // A character string constant.
+      Range *range;		      // A set of evenly spaced values.
+      Octave_map *a_map;	      // An associative array.
+
+      tree_constant_rep *freeptr;     // For custom memory management.
+    };
 
   constant_type type_tag;
 
-  union
-    {
-      double scalar;			// A real scalar constant.
-      Matrix *matrix;			// A real matrix constant.
-      Complex *complex_scalar;		// A real scalar constant.
-      ComplexMatrix *complex_matrix;	// A real matrix constant.
-      char *string;			// A character string constant.
-      Range *range;			// A set of evenly spaced values.
-      Octave_map *a_map;		// An associative array.
-    };
+  int count;
 
   char *orig_text;
 };