# HG changeset patch # User John W. Eaton # Date 1632944481 14400 # Node ID b8841fcd28c8a3ac7681b8cc531259e189f7df71 # Parent 50ab0178b6fcfc83f662748c7241736cec453c60 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. diff -r 50ab0178b6fc -r b8841fcd28c8 libinterp/corefcn/mex.cc --- 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) { diff -r 50ab0178b6fc -r b8841fcd28c8 libinterp/corefcn/mxarray.h --- 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;