diff liboctave/array/Array.h @ 30488:abb4823df535 stable

with C++17, match malloc/free for MEX memory (bug #61472) * Array-fwd.h, Array.h: If std::pmr::polymorphic_allocator<T> is available, use it as the default allocator for the Array<T> class. * mex.cc (fp_type_traits): New traits class and specializations. (mx_memory_resource): If std::pmr::polymorphic_allocator<T> is available, define class to manage MEX memory allocations when they are transferred to octave_value objects. (the_mx_memory_resource): New static object. (fp_to_ov): Use resource-aquiring Array constructor with a pointer to the_mx_memory_resource so that the Array object will own the allocated array and use std::free to release the storage storage allocated with mxMalloc or mxRealloc after it is returned to Octave in an octave_value object. If the polymorphic allocator is not availble (for example, compiling with a pre-c++17 compiler) then don't mix malloc with delete[] for arrays of Complex or FloatComplex objects. (int_to_ov): Set up to use the same kind of resource management as for fp_to_ov, except disable for now (we need to extend the intNDArray classes to have resource-acquiring constructors). * acinclude.m4 (OCTAVE_CHECK_STD_PMR_POLYMORPHIC_ALLOCATOR): New macro. * configure.ac: Use it. * mk-octave-config-h.sh: Insert definition for OCTAVE_CHECK_STD_PMR_POLYMORPHIC_ALLOCATOR into octave-config.h.
author John W. Eaton <jwe@octave.org>
date Sat, 04 Dec 2021 09:13:12 -0500
parents 298995435071
children e9a6d3822244
line wrap: on
line diff
--- a/liboctave/array/Array.h	Wed Dec 15 16:22:35 2021 -0500
+++ b/liboctave/array/Array.h	Sat Dec 04 09:13:12 2021 -0500
@@ -789,7 +789,12 @@
 
   //! Apply function fcn to each element of the Array<T, Alloc>.  This function
   //! is optimized with a manually unrolled loop.
+#if defined (HAVE_STD_PMR_POLYMORPHIC_ALLOCATOR)
+  template <typename U, typename F,
+            typename A = std::pmr::polymorphic_allocator<U>>
+#else
   template <typename U, typename F, typename A = std::allocator<U>>
+#endif
   Array<U, A>
   map (F fcn) const
   {
@@ -821,12 +826,20 @@
 
   //@{
   //! Overloads for function references.
+#if defined (HAVE_STD_PMR_POLYMORPHIC_ALLOCATOR)
+  template <typename U, typename A = std::pmr::polymorphic_allocator<U>>
+#else
   template <typename U, typename A = std::allocator<U>>
+#endif
   Array<U, A>
   map (U (&fcn) (T)) const
   { return map<U, U (&) (T), A> (fcn); }
 
+#if defined (HAVE_STD_PMR_POLYMORPHIC_ALLOCATOR)
+  template <typename U, typename A = std::pmr::polymorphic_allocator<U>>
+#else
   template <typename U, typename A = std::allocator<U>>
+#endif
   Array<U, A>
   map (U (&fcn) (const T&)) const
   { return map<U, U (&) (const T&), A> (fcn); }