changeset 13266:c053740eb2aa

improve memory use for the pager and diary streams (bug #34431) * pager.h, pager.cc (octave_pager_stream::reset, octave_pager_stream::do_reset): New functions. (octave_diary_stream::reset, octave_diary_stream::do_reset): New functions. * input.cc (octave_gets, get_user_input): Call octave_pager_stream::reset and octave_diary_stream::reset prior to printing prompt and getting input.
author John W. Eaton <jwe@octave.org>
date Mon, 03 Oct 2011 11:03:40 -0400
parents 89789bc755a1
children f26ea04b5356
files src/input.cc src/pager.cc src/pager.h
diffstat 3 files changed, 60 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/input.cc	Mon Oct 03 03:11:58 2011 -0500
+++ b/src/input.cc	Mon Oct 03 11:03:40 2011 -0400
@@ -275,6 +275,9 @@
 
       flush_octave_stdout ();
 
+      octave_pager_stream::reset ();
+      octave_diary_stream::reset ();
+
       octave_diary << prompt;
 
       retval = interactive_input (prompt);
@@ -794,6 +797,9 @@
 
   flush_octave_stdout ();
 
+  octave_pager_stream::reset ();
+  octave_diary_stream::reset ();
+
   octave_diary << prompt;
 
   std::string input_buf = interactive_input (prompt.c_str (), true);
--- a/src/pager.cc	Mon Oct 03 03:11:58 2011 -0500
+++ b/src/pager.cc	Mon Oct 03 11:03:40 2011 -0400
@@ -334,6 +334,29 @@
     pb->set_diary_skip ();
 }
 
+// Reinitialize the pager buffer to avoid hanging on to large internal
+// buffers when they might not be needed.  This function should only be
+// called when the pager is not in use.  For example, just before
+// getting command-line input.
+
+void
+octave_pager_stream::reset (void)
+{
+  if (! instance)
+    instance = new octave_pager_stream ();
+
+  instance->do_reset ();
+}
+
+void
+octave_pager_stream::do_reset (void)
+{
+  delete pb;
+  pb = new octave_pager_buf ();
+  rdbuf (pb);
+  setf (unitbuf);
+}
+
 octave_diary_stream *octave_diary_stream::instance = 0;
 
 octave_diary_stream::octave_diary_stream (void) : std::ostream (0), db (0)
@@ -358,6 +381,29 @@
   return *instance;
 }
 
+// Reinitialize the diary buffer to avoid hanging on to large internal
+// buffers when they might not be needed.  This function should only be
+// called when the pager is not in use.  For example, just before
+// getting command-line input.
+
+void
+octave_diary_stream::reset (void)
+{
+  if (! instance)
+    instance = new octave_diary_stream ();
+
+  instance->do_reset ();
+}
+
+void
+octave_diary_stream::do_reset (void)
+{
+  delete db;
+  db = new octave_diary_buf ();
+  rdbuf (db);
+  setf (unitbuf);
+}
+
 void
 flush_octave_stdout (void)
 {
--- a/src/pager.h	Mon Oct 03 03:11:58 2011 -0500
+++ b/src/pager.h	Mon Oct 03 11:03:40 2011 -0400
@@ -68,8 +68,12 @@
 
   static octave_pager_stream& stream (void);
 
+  static void reset (void);
+
 private:
 
+  void do_reset (void);
+
   static octave_pager_stream *instance;
 
   octave_pager_buf *pb;
@@ -108,8 +112,12 @@
 
   static octave_diary_stream& stream (void);
 
+  static void reset (void);
+
 private:
 
+  void do_reset (void);
+
   static octave_diary_stream *instance;
 
   octave_diary_buf *db;