diff m4/acinclude.m4 @ 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 8a341cf6773d
children 79a7f3e3cf54
line wrap: on
line diff
--- a/m4/acinclude.m4	Wed Dec 15 16:22:35 2021 -0500
+++ b/m4/acinclude.m4	Sat Dec 04 09:13:12 2021 -0500
@@ -255,6 +255,51 @@
   fi
 ])
 dnl
+dnl Check whether std::pmr::polymorphic_allocator is available.
+dnl
+AC_DEFUN([OCTAVE_CHECK_STD_PMR_POLYMORPHIC_ALLOCATOR], [
+  AC_CACHE_CHECK([whether std::pmr::polymorphic_allocator is avalable],
+    [octave_cv_std_pmr_polymorphic_allocator],
+    [AC_LANG_PUSH(C++)
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+      #include <cstdlib>
+      #include <memory_resource>
+      #include <vector>
+      class mx_memory_resource : public std::pmr::memory_resource
+      {
+      private:
+        void * do_allocate (std::size_t bytes, size_t /*alignment*/)
+        {
+          void *ptr = std::malloc (bytes);
+          if (! ptr)
+            throw std::bad_alloc ();
+            return ptr;
+        }
+        void do_deallocate (void* ptr, std::size_t /*bytes*/,
+                            std::size_t /*alignment*/)
+        {
+          std::free (ptr);
+        }
+        bool do_is_equal (const std::pmr::memory_resource& other) const noexcept
+        {
+          return this == dynamic_cast<const mx_memory_resource *> (&other);
+          return true;
+        }
+      };
+      mx_memory_resource the_mx_memory_resource;
+    ]], [[
+      std::pmr::vector<int> my_int_vec { &the_mx_memory_resource };
+    ]])],
+    octave_cv_std_pmr_polymorphic_allocator=yes,
+    octave_cv_std_pmr_polymorphic_allocator=no)
+    AC_LANG_POP(C++)
+  ])
+  if test $octave_cv_std_pmr_polymorphic_allocator = yes; then
+    AC_DEFINE(HAVE_STD_PMR_POLYMORPHIC_ALLOCATOR, 1,
+      [Define to 1 if std::pmr::polymorphic_allocator is available.])
+  fi
+])
+dnl
 dnl Check whether CXSparse is version 2.2 or later
 dnl FIXME: This test uses a version number.  It potentially could
 dnl        be re-written to actually call a function, but is it worth it?