changeset 30223:b8841fcd28c8

combine calls to mxArray::malloc and mxArray::calloc in wrapper function * mex.cc, mxarray.h (mxArray::alloc): New function that calls either mxArray::malloc or mxArray::calloc depending on value of INIT parameter. Use it to simplify allocation and initialization of arrays in mxArray class constructors.
author John W. Eaton <jwe@octave.org>
date Wed, 29 Sep 2021 15:41:21 -0400
parents 50ab0178b6fc
children 8670f5165430
files libinterp/corefcn/mex.cc libinterp/corefcn/mxarray.h
diffstat 2 files changed, 15 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/mex.cc	Sun Oct 03 13:14:22 2021 +0200
+++ b/libinterp/corefcn/mex.cc	Wed Sep 29 15:41:21 2021 -0400
@@ -1570,16 +1570,12 @@
                   bool init = true)
     : mxArray_matlab (interleaved, id, ndims, dims),
       m_complex (flag == mxCOMPLEX),
-      m_pr (init
-            ? mxArray::calloc (get_number_of_elements (), get_element_size ())
-            : mxArray::malloc (get_number_of_elements () * get_element_size ())),
+      m_pr (mxArray::alloc (init, get_number_of_elements (), get_element_size ())),
       m_pi (m_interleaved
-          ? nullptr
-          : (m_complex
-             ? (init
-                ? mxArray::calloc (get_number_of_elements (), get_element_size ())
-                : mxArray::malloc (get_number_of_elements () * get_element_size ()))
-             : nullptr))
+            ? nullptr
+            : (m_complex
+               ? mxArray::alloc (init, get_number_of_elements (), get_element_size ())
+               : nullptr))
   { }
 
   mxArray_number (bool interleaved, mxClassID id, const dim_vector& dv,
@@ -1596,15 +1592,11 @@
   mxArray_number (bool interleaved, mxClassID id, mwSize m, mwSize n,
                   mxComplexity flag = mxREAL, bool init = true)
     : mxArray_matlab (interleaved, id, m, n), m_complex (flag == mxCOMPLEX),
-      m_pr (init
-            ? mxArray::calloc (get_number_of_elements (), get_element_size ())
-            : mxArray::malloc (get_number_of_elements () * get_element_size ())),
+      m_pr (mxArray::alloc (init, get_number_of_elements (), get_element_size ())),
       m_pi (m_interleaved
             ? nullptr
             : (m_complex
-               ? (init
-                  ? mxArray::calloc (get_number_of_elements (), get_element_size ())
-                  : mxArray::malloc (get_number_of_elements () * get_element_size ()))
+               ? (mxArray::alloc (init, get_number_of_elements (), get_element_size ()))
                : nullptr))
   { }
 
@@ -3222,6 +3214,12 @@
   return mex_context ? mex_context->calloc_unmarked (n, t) : ::calloc (n, t);
 }
 
+void *
+mxArray::alloc (bool init, std::size_t n, std::size_t t)
+{
+  return init ? mxArray::calloc (n, t) : mxArray::malloc (n * t);
+}
+
 static inline void *
 maybe_mark_foreign (void *ptr)
 {
--- a/libinterp/corefcn/mxarray.h	Sun Oct 03 13:14:22 2021 +0200
+++ b/libinterp/corefcn/mxarray.h	Wed Sep 29 15:41:21 2021 -0400
@@ -678,6 +678,8 @@
 
   static OCTINTERP_API void * calloc (std::size_t n, std::size_t t);
 
+  static OCTINTERP_API void * alloc (bool init, std::size_t n, std::size_t t);
+
   static char * strsave (const char *str)
   {
     char *retval = nullptr;