Mercurial > octave
changeset 28823:26cfccfee9a0
Replace unwind_protect with more efficient constructs (bug #59192).
* debug.cc, error.cc, ft-text-renderer.cc, load-path.cc, mex.cc, oct-hist.cc,
pager.cc, rand.cc, strfns.cc, sysdep.cc, toplev.cc, variables.cc,
__delaunayn__.cc, __voronoi__.cc, audiodevinfo.cc, audioread.cc, convhulln.cc,
ov-class.cc, ov-struct.cc: Remove unwind_protect frames declared but never used.
Replace frame.protect_var () instances with unwind_protect_var<> object.
Replace frame.add_fcn () instances with unwind_action<> object.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 30 Sep 2020 03:17:52 -0700 |
parents | fd05bcb94ab4 |
children | a98fd0657e35 |
files | libinterp/corefcn/debug.cc libinterp/corefcn/error.cc libinterp/corefcn/ft-text-renderer.cc libinterp/corefcn/load-path.cc libinterp/corefcn/mex.cc libinterp/corefcn/oct-hist.cc libinterp/corefcn/pager.cc libinterp/corefcn/rand.cc libinterp/corefcn/strfns.cc libinterp/corefcn/sysdep.cc libinterp/corefcn/toplev.cc libinterp/corefcn/variables.cc libinterp/dldfcn/__delaunayn__.cc libinterp/dldfcn/__voronoi__.cc libinterp/dldfcn/audiodevinfo.cc libinterp/dldfcn/audioread.cc libinterp/dldfcn/convhulln.cc libinterp/octave-value/ov-class.cc libinterp/octave-value/ov-struct.cc |
diffstat | 19 files changed, 69 insertions(+), 104 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/debug.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/debug.cc Wed Sep 30 03:17:52 2020 -0700 @@ -838,8 +838,6 @@ octave_value_list retval; - octave::unwind_protect frame; - octave_idx_type curr_frame = -1; size_t nskip = 0;
--- a/libinterp/corefcn/error.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/error.cc Wed Sep 30 03:17:52 2020 -0700 @@ -571,10 +571,7 @@ || application::forced_interactive ()) && debug_on_warning () && in_user_code && bptab.debug_on_warn (id)) { - unwind_protect frame; - - frame.protect_var (m_debug_on_warning); - m_debug_on_warning = false; + unwind_protect_var<bool> restore_var (m_debug_on_warning, false); tw.enter_debugger (); }
--- a/libinterp/corefcn/ft-text-renderer.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/ft-text-renderer.cc Wed Sep 30 03:17:52 2020 -0700 @@ -980,9 +980,9 @@ m_strlist = std::list<text_renderer::string> (); - unwind_protect frame; - frame.protect_var (m_do_strlist); - frame.protect_var (m_strlist); + octave::unwind_protect_var<bool> restore_var1 (m_do_strlist); + octave::unwind_protect_var<std::list<text_renderer::string>> + restore_var2 (m_strlist); m_do_strlist = true; text_to_pixels (txt, pxls, box, ha, va, rot, interp, false);
--- a/libinterp/corefcn/load-path.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/load-path.cc Wed Sep 30 03:17:52 2020 -0700 @@ -963,8 +963,6 @@ if (! octave_interpreter_ready) return; - unwind_protect frame; - std::string file = sys::file_ops::concat (dir, script_file); sys::file_stat fs (file);
--- a/libinterp/corefcn/mex.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/mex.cc Wed Sep 30 03:17:52 2020 -0700 @@ -4250,10 +4250,8 @@ for (int i = 0; i < nout; i++) argout[i] = nullptr; - octave::unwind_protect_safe frame; - // Save old mex pointer. - frame.protect_var (mex_context); + octave::unwind_protect_var<mex *> restore_var (mex_context); mex context (mex_fcn);
--- a/libinterp/corefcn/oct-hist.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/oct-hist.cc Wed Sep 30 03:17:52 2020 -0700 @@ -327,12 +327,14 @@ { bool numbered_output = nargout == 0; - unwind_protect frame; + octave::unwind_action restore_history_filename + ([] (const auto& old_filename) + { + command_history::set_file (old_filename); + }, command_history::file ()); string_vector hlist; - frame.add_fcn (command_history::set_file, command_history::file ()); - int nargin = args.length (); // Number of history lines to show (-1 = all)
--- a/libinterp/corefcn/pager.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/pager.cc Wed Sep 30 03:17:52 2020 -0700 @@ -324,10 +324,10 @@ { if (! m_flushing_output_to_pager) { - unwind_protect frame; - - frame.protect_var (m_really_flush_to_pager); - frame.protect_var (m_flushing_output_to_pager); + octave::unwind_protect_var<bool> + restore_var1 (m_really_flush_to_pager); + octave::unwind_protect_var<bool> + restore_var2 (m_flushing_output_to_pager); m_really_flush_to_pager = true; m_flushing_output_to_pager = true;
--- a/libinterp/corefcn/rand.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/rand.cc Wed Sep 30 03:17:52 2020 -0700 @@ -117,10 +117,12 @@ octave_value retval; dim_vector dims; - octave::unwind_protect frame; // Restore current distribution on any exit. - frame.add_fcn (octave::rand::distribution, - octave::rand::distribution ()); + octave::unwind_action restore_distribution + ([] (const auto& old_distribution) + { + octave::rand::distribution (old_distribution); + }, octave::rand::distribution ()); octave::rand::distribution (distribution);
--- a/libinterp/corefcn/strfns.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/strfns.cc Wed Sep 30 03:17:52 2020 -0700 @@ -901,8 +901,6 @@ size_t length; uint8_t *utf8_str = nullptr; - octave::unwind_protect frame; - utf8_str = octave_u8_conv_from_encoding (codepage, src, srclen, &length); if (! utf8_str) @@ -915,7 +913,11 @@ codepage, std::strerror (errno)); } - frame.add_fcn (::free, static_cast<void *> (utf8_str)); + octave::unwind_action free_utf8_str + ([] (const auto utf8_str_ptr) + { + ::free (utf8_str_ptr); + }, static_cast<void *> (utf8_str)); octave_idx_type len = length; @@ -953,8 +955,6 @@ size_t length; char *native_bytes = nullptr; - octave::unwind_protect frame; - native_bytes = octave_u8_conv_to_encoding (codepage, src, srclen, &length); if (! native_bytes) @@ -967,7 +967,11 @@ codepage, std::strerror (errno)); } - frame.add_fcn (::free, static_cast<void *> (native_bytes)); + octave::unwind_action free_native_bytes + ([] (const auto native_bytes_ptr) + { + ::free (native_bytes_ptr); + }, static_cast<void *> (native_bytes)); octave_idx_type len = length;
--- a/libinterp/corefcn/sysdep.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/sysdep.cc Wed Sep 30 03:17:52 2020 -0700 @@ -718,9 +718,12 @@ wchar_t *wcommand = u8_to_wchar (command); wchar_t *wmode = u8_to_wchar (mode); - unwind_protect frame; - frame.add_fcn (::free, static_cast<void *> (wcommand)); - frame.add_fcn (::free, static_cast<void *> (wmode)); + octave::unwind_action free_memory + ([] (const auto wcommand_ptr, const auto wmode_ptr) + { + ::free (wcommand_ptr); + ::free (wmode_ptr); + }, static_cast<void *> (wcommand), static_cast<void *> (wmode)); if (wmode && wmode[0] && ! wmode[1]) { @@ -961,9 +964,11 @@ if (result != ERROR_SUCCESS) return result; - unwind_protect frame; - - frame.add_fcn (reg_close_key_wrapper, h_subkey); + octave::unwind_action restore_keys + ([] (const auto& old_subkeys) + { + reg_close_key_wrapper (old_subkeys); + }, h_subkey); std::wstring wname = sys::u8_to_wstring (name); DWORD length = 0;
--- a/libinterp/corefcn/toplev.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/toplev.cc Wed Sep 30 03:17:52 2020 -0700 @@ -281,9 +281,11 @@ cmd_str = '"' + cmd_str + '"'; #endif - octave::unwind_protect frame; - - frame.add_fcn (restore_signal_mask, get_signal_mask ()); + octave::unwind_action restore_mask + ([] (const auto signal_mask_ptr) + { + restore_signal_mask (signal_mask_ptr); + }, get_signal_mask ()); octave_unblock_async_signals (); octave_unblock_signal_by_name ("SIGTSTP");
--- a/libinterp/corefcn/variables.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/corefcn/variables.cc Wed Sep 30 03:17:52 2020 -0700 @@ -1399,8 +1399,8 @@ if (val.is_defined ()) { // Ensure auto-restoration. - octave::unwind_protect frame; - frame.protect_var (Vmissing_function_hook); + octave::unwind_protect_var<std::string> + restore_var (Vmissing_function_hook); // Clear the variable prior to calling the function. const std::string func_name = Vmissing_function_hook;
--- a/libinterp/dldfcn/__delaunayn__.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/dldfcn/__delaunayn__.cc Wed Sep 30 03:17:52 2020 -0700 @@ -166,8 +166,6 @@ sprintf (flags, "qhull d %s", options.c_str ()); - octave::unwind_protect frame; - // Replace the outfile pointer with stdout for debugging information. #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM) FILE *outfile = std::fopen ("NUL", "w"); @@ -179,12 +177,13 @@ if (! outfile) error ("__delaunayn__: unable to create temporary file for output"); - frame.add_fcn (close_fcn, outfile); + octave::unwind_action close_outfile + ([] (const auto file_ptr) { close_fcn (file_ptr); }, outfile); int exitcode = qh_new_qhull (dim, n, pt_array, ismalloc, flags, outfile, errfile); - frame.add_fcn (free_qhull_memory); + octave::unwind_action free_memory ([] () { free_qhull_memory (); }); if (exitcode) error ("__delaunayn__: qhull failed");
--- a/libinterp/dldfcn/__voronoi__.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/dldfcn/__voronoi__.cc Wed Sep 30 03:17:52 2020 -0700 @@ -158,8 +158,6 @@ boolT ismalloc = false; - octave::unwind_protect frame; - // Replace the outfile pointer with stdout for debugging information. #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM) FILE *outfile = std::fopen ("NUL", "w"); @@ -171,7 +169,8 @@ if (! outfile) error ("__voronoi__: unable to create temporary file for output"); - frame.add_fcn (close_fcn, outfile); + octave::unwind_action close_outfile + ([] (const auto file_ptr) { close_fcn (file_ptr); }, outfile); // qh_new_qhull command and points arguments are not const... @@ -184,7 +183,7 @@ int exitcode = qh_new_qhull (dim, num_points, points.fortran_vec (), ismalloc, cmd_str, outfile, errfile); - frame.add_fcn (free_qhull_memory); + octave::unwind_action free_memory ([] () { free_qhull_memory (); }); if (exitcode) error ("%s: qhull failed", caller.c_str ());
--- a/libinterp/dldfcn/audiodevinfo.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/dldfcn/audiodevinfo.cc Wed Sep 30 03:17:52 2020 -0700 @@ -863,12 +863,6 @@ return paContinue; } -static void -safe_audioplayer_stop (audioplayer *player) -{ - player->stop (); -} - audioplayer::audioplayer (void) : octave_callback_function (nullptr), id (-1), fs (0), nbits (16), channels (0), sample_number (0), @@ -1156,9 +1150,8 @@ start = get_sample_number (); end = get_end_sample (); - octave::unwind_protect frame; - - frame.add_fcn (safe_audioplayer_stop, this); + octave::unwind_action stop_audioplayer + ([] (audioplayer * player) { player->stop (); }, this); for (unsigned int i = start; i < end; i += buffer_size) { @@ -1543,12 +1536,6 @@ return paContinue; } -static void -safe_audiorecorder_stop (audiorecorder *recorder) -{ - recorder->stop (); -} - audiorecorder::audiorecorder (void) : octave_callback_function (nullptr), id (-1), fs (8000), nbits (8), channels (1), sample_number (0), @@ -1836,9 +1823,8 @@ unsigned int frames = seconds * get_fs (); - octave::unwind_protect frame; - - frame.add_fcn (safe_audiorecorder_stop, this); + octave::unwind_action stop_audiorecorder + ([] (audiorecorder * recorder) { recorder->stop (); }, this); for (unsigned int i = 0; i < frames; i += buffer_size) {
--- a/libinterp/dldfcn/audioread.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/dldfcn/audioread.cc Wed Sep 30 03:17:52 2020 -0700 @@ -49,14 +49,6 @@ # include <sndfile.h> #endif -#if defined (HAVE_SNDFILE) -static void -safe_close (SNDFILE *file) -{ - sf_close (file); -} -#endif - DEFUN_DLD (audioread, args, , doc: /* -*- texinfo -*- @deftypefn {} {[@var{y}, @var{fs}] =} audioread (@var{filename}) @@ -96,9 +88,8 @@ error ("audioread: failed to open input file '%s': %s", filename.c_str (), sf_strerror (file)); - octave::unwind_protect frame; - - frame.add_fcn (safe_close, file); + octave::unwind_action close_open_file + ([] (auto file_ptr) { sf_close (file_ptr); }, file); OCTAVE_LOCAL_BUFFER (double, data, info.frames * info.channels); @@ -431,9 +422,8 @@ error ("audiowrite: failed to open output file '%s': %s", filename.c_str (), sf_strerror (file)); - octave::unwind_protect frame; - - frame.add_fcn (safe_close, file); + octave::unwind_action close_open_file + ([] (auto file_ptr) { sf_close (file_ptr); }, file); sf_command (file, SFC_SET_NORM_DOUBLE, nullptr, SF_TRUE); sf_command (file, SFC_SET_CLIPPING, nullptr, SF_TRUE) ; @@ -626,9 +616,8 @@ error ("audioinfo: failed to open input file '%s': %s", filename.c_str (), sf_strerror (file)); - octave::unwind_protect frame; - - frame.add_fcn (safe_close, file); + octave::unwind_action close_open_file + ([] (auto file_ptr) { sf_close (file_ptr); }, file); octave_scalar_map result;
--- a/libinterp/dldfcn/convhulln.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/dldfcn/convhulln.cc Wed Sep 30 03:17:52 2020 -0700 @@ -58,12 +58,6 @@ # endif static void -close_fcn (FILE *f) -{ - std::fclose (f); -} - -static void free_qhull_memory () { qh_freeqhull (! qh_ALL); @@ -173,8 +167,6 @@ boolT ismalloc = false; - octave::unwind_protect frame; - // Replace the outfile pointer with stdout for debugging information. #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM) FILE *outfile = std::fopen ("NUL", "w"); @@ -186,7 +178,8 @@ if (! outfile) error ("convhulln: unable to create temporary file for output"); - frame.add_fcn (close_fcn, outfile); + octave::unwind_action close_outfile + ([] (const auto file_ptr) { std::fclose (file_ptr); }, outfile); // qh_new_qhull command and points arguments are not const... @@ -199,7 +192,7 @@ int exitcode = qh_new_qhull (dim, num_points, points.fortran_vec (), ismalloc, cmd_str, outfile, errfile); - frame.add_fcn (free_qhull_memory); + octave::unwind_action free_memory ([] () { free_qhull_memory (); }); if (exitcode) error ("convhulln: qhull failed");
--- a/libinterp/octave-value/ov-class.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/octave-value/ov-class.cc Wed Sep 30 03:17:52 2020 -0700 @@ -579,8 +579,7 @@ if (obsolete_copies == 0 && meth.is_user_function () && meth.user_function_value ()->subsasgn_optimization_ok ()) { - octave::unwind_protect frame; - frame.protect_var (obsolete_copies); + octave::unwind_protect_var<int> restore_var (obsolete_copies); obsolete_copies = 2; tmp = octave::feval (meth.function_value (), args); @@ -1018,8 +1017,6 @@ void octave_class::print_raw (std::ostream& os, bool) const { - octave::unwind_protect frame; - indent (os); os << " <class " << class_name () << '>'; newline (os);
--- a/libinterp/octave-value/ov-struct.cc Wed Sep 30 08:59:11 2020 +0200 +++ b/libinterp/octave-value/ov-struct.cc Wed Sep 30 03:17:52 2020 -0700 @@ -586,9 +586,7 @@ void octave_struct::print_raw (std::ostream& os, bool) const { - octave::unwind_protect frame; - - frame.protect_var (Vstruct_levels_to_print); + octave::unwind_protect_var<int> restore_var (Vstruct_levels_to_print); if (Vstruct_levels_to_print >= 0) { @@ -1299,9 +1297,7 @@ void octave_scalar_struct::print_raw (std::ostream& os, bool) const { - octave::unwind_protect frame; - - frame.protect_var (Vstruct_levels_to_print); + octave::unwind_protect_var<int> restore_var (Vstruct_levels_to_print); if (Vstruct_levels_to_print >= 0) {