changeset 27542:b8aa62c1deb5

eliminate global tmp_files variable * interpreter.h, interpreter.cc (temporary_file_list): New class. (interpreter::m_tmp_files): New member variable. (interpreter::mark_for_deletion, interpreter::cleanup_tmp_files): New member functions. (interpreter::cleanup): Don't explicitly cleanup tmp files now. That is handled by the m_tmp_files destructor. * file-io.h, file-io.cc (tmp_files): Delete global variable. (mark_for_deletion, cleanup_tmp_files): Mark as depreacated.
author John W. Eaton <jwe@octave.org>
date Tue, 22 Oct 2019 13:38:02 -0400
parents 5e1f2f1a7fcf
children d7b18d44ae4e
files libinterp/corefcn/file-io.cc libinterp/corefcn/file-io.h libinterp/corefcn/interpreter.cc libinterp/corefcn/interpreter.h
diffstat 4 files changed, 92 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/file-io.cc	Tue Oct 22 08:13:26 2019 -0700
+++ b/libinterp/corefcn/file-io.cc	Tue Oct 22 13:38:02 2019 -0400
@@ -42,9 +42,7 @@
 #include <cstdio>
 
 #include <iomanip>
-#include <stack>
 #include <string>
-#include <vector>
 
 #if defined (HAVE_ZLIB_H)
 #  include <zlib.h>
@@ -66,6 +64,7 @@
 #include "error.h"
 #include "errwarn.h"
 #include "file-io.h"
+#include "interpreter-private.h"
 #include "interpreter.h"
 #include "load-path.h"
 #include "oct-fstrm.h"
@@ -81,29 +80,6 @@
 #include "utils.h"
 #include "variables.h"
 
-// List of files to delete when we exit or crash.
-//
-// FIXME: this should really be static,
-//        but that causes problems on some systems.
-std::stack <std::string> tmp_files;
-
-void
-mark_for_deletion (const std::string& file)
-{
-  tmp_files.push (file);
-}
-
-void
-cleanup_tmp_files (void)
-{
-  while (! tmp_files.empty ())
-    {
-      std::string filename = tmp_files.top ();
-      tmp_files.pop ();
-      octave_unlink_wrapper (filename.c_str ());
-    }
-}
-
 static void
 normalize_fopen_mode (std::string& mode, bool& use_zlib)
 {
@@ -3034,7 +3010,7 @@
           retval(1) = nm;
 
           if (nargin == 2 && args(1).is_true ())
-            mark_for_deletion (nm);
+            interp.mark_for_deletion (nm);
         }
     }
 
@@ -3222,3 +3198,25 @@
 
   return const_value ("stderr", args, streams.stderr_file ());
 }
+
+// Deprecated variables and functions.
+
+// Deprecated in Octave 6.
+void
+mark_for_deletion (const std::string& file)
+{
+  octave::interpreter& interp
+    = octave::__get_interpreter__ ("mark_for_deletion");
+
+  interp.mark_for_deletion (file);
+}
+
+// Deprecated in Octave 6.
+void
+cleanup_tmp_files (void)
+{
+  octave::interpreter& interp
+    = octave::__get_interpreter__ ("cleanup_tmp_files");
+
+  interp.cleanup_tmp_files ();
+}
--- a/libinterp/corefcn/file-io.h	Tue Oct 22 08:13:26 2019 -0700
+++ b/libinterp/corefcn/file-io.h	Tue Oct 22 13:38:02 2019 -0400
@@ -29,8 +29,10 @@
 
 #include <string>
 
+OCTAVE_DEPRECATED (6, "use 'interpreter::mark_for_deletion' instead")
 extern OCTINTERP_API void mark_for_deletion (const std::string&);
 
+OCTAVE_DEPRECATED (6, "use 'interpreter::cleanup_tmp_files' instead")
 extern OCTINTERP_API void cleanup_tmp_files (void);
 
 #endif
--- a/libinterp/corefcn/interpreter.cc	Tue Oct 22 08:13:26 2019 -0700
+++ b/libinterp/corefcn/interpreter.cc	Tue Oct 22 13:38:02 2019 -0400
@@ -26,6 +26,7 @@
 
 #include <cstdio>
 
+#include <set>
 #include <string>
 #include <iostream>
 
@@ -245,6 +246,28 @@
 
 namespace octave
 {
+  temporary_file_list::~temporary_file_list (void)
+  {
+    cleanup ();
+  }
+
+  void temporary_file_list::insert (const std::string& file)
+  {
+    m_files.insert (file);
+  }
+
+  void temporary_file_list::cleanup (void)
+  {
+    while (! m_files.empty ())
+      {
+        auto it = m_files.begin ();
+
+        octave_unlink_wrapper (it->c_str ());
+
+        m_files.erase (it);
+      }
+  }
+
   // The time we last time we changed directories.
   sys::time Vlast_chdir_time = 0.0;
 
@@ -362,6 +385,7 @@
 
   interpreter::interpreter (application *app_context)
     : m_app_context (app_context),
+      m_tmp_files (),
       m_display_info (),
       m_environment (),
       m_settings (),
@@ -1167,8 +1191,6 @@
 
     m_gtk_manager.unload_all_toolkits ();
 
-    OCTAVE_SAFE_CALL (cleanup_tmp_files, ());
-
     // FIXME:  May still need something like this to ensure that
     // destructors for class objects will run properly.  Should that be
     // done earlier?  Before or after atexit functions are executed?
@@ -1655,6 +1677,16 @@
     catch_interrupts ();
   }
 
+  void interpreter::mark_for_deletion (const std::string& file)
+  {
+    m_tmp_files.insert (file);
+  }
+
+  void interpreter::cleanup_tmp_files (void)
+  {
+    m_tmp_files.cleanup ();
+  }
+
   // Functions to call when the interpreter exits.
 
   std::list<std::string> interpreter::atexit_functions;
--- a/libinterp/corefcn/interpreter.h	Tue Oct 22 08:13:26 2019 -0700
+++ b/libinterp/corefcn/interpreter.h	Tue Oct 22 13:38:02 2019 -0400
@@ -26,6 +26,7 @@
 #include "octave-config.h"
 
 #include <map>
+#include <stack>
 #include <string>
 
 #include "child-list.h"
@@ -80,6 +81,31 @@
 
   class application;
 
+  class temporary_file_list
+  {
+  public:
+
+    temporary_file_list (void) : m_files () { }
+
+    // No copying!
+
+    temporary_file_list (const temporary_file_list&) = delete;
+
+    temporary_file_list& operator = (const temporary_file_list&) = delete;
+
+    ~temporary_file_list (void);
+
+    void insert (const std::string& file);
+
+    void cleanup (void);
+
+  private:
+
+    // List of temporary files to delete when we exit.
+    std::set<std::string> m_files;
+
+  };
+
   class OCTINTERP_API interpreter
   {
   public:
@@ -407,6 +433,10 @@
 
     void recover_from_exception (void);
 
+    void mark_for_deletion (const std::string& file);
+
+    void cleanup_tmp_files (void);
+
     static void add_atexit_function (const std::string& fname);
 
     static bool remove_atexit_function (const std::string& fname);
@@ -441,6 +471,8 @@
 
     application *m_app_context;
 
+    temporary_file_list m_tmp_files;
+
     display_info m_display_info;
 
     environment m_environment;