Mercurial > octave
changeset 28824:a98fd0657e35
Replace unwind_protect with more efficient constructs in liboctave (bug #59192).
* lo-sysdep.cc, oct-glob.cc, oct-string.cc, url-transfer.cc:
Replace frame.add_fcn () instances with unwind_action<> object.
* unwind-prot.h (unwind_action): Add note to documentation that function
pointer or lambda expression are possible inputs.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 30 Sep 2020 04:16:37 -0700 |
parents | 26cfccfee9a0 |
children | 473ee93cf1ea |
files | liboctave/system/lo-sysdep.cc liboctave/util/oct-glob.cc liboctave/util/oct-string.cc liboctave/util/unwind-prot.h liboctave/util/url-transfer.cc |
diffstat | 5 files changed, 59 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/system/lo-sysdep.cc Wed Sep 30 03:17:52 2020 -0700 +++ b/liboctave/system/lo-sysdep.cc Wed Sep 30 04:16:37 2020 -0700 @@ -430,8 +430,12 @@ #if defined (OCTAVE_USE_WINDOWS_API) wchar_t *wnew_item = u8_to_wchar (new_item); - unwind_protect frame; - frame.add_fcn (std::free, static_cast<void *> (new_item)); + octave::unwind_action free_new_item + ([] (const auto new_item_ptr) + { + std::free (new_item_ptr); + }, static_cast<void *> (new_item)); + if (_wputenv (wnew_item) < 0) (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); #else
--- a/liboctave/util/oct-glob.cc Wed Sep 30 03:17:52 2020 -0700 +++ b/liboctave/util/oct-glob.cc Wed Sep 30 04:16:37 2020 -0700 @@ -78,11 +78,13 @@ int k = 0; - unwind_protect frame; - void *glob_info = octave_create_glob_info_struct (); - frame.add_fcn (octave_destroy_glob_info_struct, glob_info); + octave::unwind_action cleanup_glob_info_struct + ([] (const auto glob_info_ptr) + { + octave_destroy_glob_info_struct (glob_info_ptr); + }, glob_info); for (int i = 0; i < npat; i++) { @@ -154,11 +156,13 @@ int k = 0; - unwind_protect frame; - void *glob_info = octave_create_glob_info_struct (); - frame.add_fcn (octave_destroy_glob_info_struct, glob_info); + octave::unwind_action cleanup_glob_info_struct + ([] (const auto glob_info_ptr) + { + octave_destroy_glob_info_struct (glob_info_ptr); + }, glob_info); for (int i = 0; i < npat; i++) {
--- a/liboctave/util/oct-string.cc Wed Sep 30 03:17:52 2020 -0700 +++ b/liboctave/util/oct-string.cc Wed Sep 30 04:16:37 2020 -0700 @@ -516,8 +516,11 @@ who.c_str (), encoding.c_str (), std::strerror (errno)); } - octave::unwind_protect frame; - frame.add_fcn (::free, static_cast<void *> (native_str)); + octave::unwind_action free_native_str + ([] (const auto native_str_ptr) + { + ::free (native_str_ptr); + }, static_cast<void *> (native_str)); std::string retval = std::string (native_str, length); @@ -547,8 +550,11 @@ who.c_str (), encoding.c_str (), std::strerror (errno)); } - octave::unwind_protect frame; - 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)); std::string retval = std::string (reinterpret_cast<char *> (utf8_str), length); @@ -594,8 +600,11 @@ ("%s: converting from codepage '%s' to UTF-8 failed: %s", who.c_str (), fallback.c_str (), std::strerror (errno)); - octave::unwind_protect frame; - frame.add_fcn (::free, static_cast<void *> (val_utf8)); + octave::unwind_action free_val_utf8 + ([] (const auto val_utf8_ptr) + { + ::free (val_utf8_ptr); + }, static_cast<void *> (val_utf8)); out_str.append (reinterpret_cast<const char *> (val_utf8), lengthp);
--- a/liboctave/util/unwind-prot.h Wed Sep 30 03:17:52 2020 -0700 +++ b/liboctave/util/unwind-prot.h Wed Sep 30 04:16:37 2020 -0700 @@ -147,7 +147,7 @@ // int val = 42; // // // template parameters, std::bind and std::function provide - // // flexibility in calling forms: + // // flexibility in calling forms (function pointer or lambda): // // unwind_action act1 (fcn, val); // unwind_action act2 ([val] (void) { fcn (val); });
--- a/liboctave/util/url-transfer.cc Wed Sep 30 03:17:52 2020 -0700 +++ b/liboctave/util/url-transfer.cc Wed Sep 30 04:16:37 2020 -0700 @@ -448,8 +448,11 @@ { struct curl_slist *slist = nullptr; - unwind_protect frame; - frame.add_fcn (curl_slist_free_all, slist); + octave::unwind_action cleanup_slist + ([] (const auto slist_ptr) + { + curl_slist_free_all (slist_ptr); + }, slist); std::string cmd = "rnfr " + oldname; slist = curl_slist_append (slist, cmd.c_str ()); @@ -616,8 +619,11 @@ struct curl_slist *slist = nullptr; - unwind_protect frame; - frame.add_fcn (curl_slist_free_all, slist); + octave::unwind_action cleanup_slist + ([] (const auto slist_ptr) + { + curl_slist_free_all (slist_ptr); + }, slist); slist = curl_slist_append (slist, "pwd"); SETOPTR (CURLOPT_POSTQUOTE, slist); @@ -708,9 +714,11 @@ { struct curl_slist *slist = nullptr; - unwind_protect frame; - - frame.add_fcn (curl_slist_free_all, slist); + octave::unwind_action cleanup_slist + ([] (const auto slist_ptr) + { + curl_slist_free_all (slist_ptr); + }, slist); if (param.numel () >= 2) { @@ -732,13 +740,16 @@ // path of the file as its value. void form_data_post (const Array<std::string>& param) { - struct curl_httppost *post = nullptr, *last = nullptr; + struct curl_httppost *post = nullptr; + struct curl_httppost *last = nullptr; SETOPT (CURLOPT_URL, m_host_or_url.c_str ()); - unwind_protect frame; - - frame.add_fcn (curl_formfree, post); + octave::unwind_action cleanup_httppost + ([] (const auto httppost_ptr) + { + curl_formfree (httppost_ptr); + }, post); if (param.numel () >= 2) { @@ -912,9 +923,11 @@ { struct curl_slist *slist = nullptr; - unwind_protect frame; - - frame.add_fcn (curl_slist_free_all, slist); + octave::unwind_action cleanup_slist + ([] (const auto slist_ptr) + { + curl_slist_free_all (slist_ptr); + }, slist); std::string cmd = action + ' ' + file_or_dir;