# HG changeset patch # User jwe # Date 1161313392 0 # Node ID 814f20da2cdb435d37ebb6083be23285b75da9da # Parent fede829d805ad763796ca5789ad6a99055c26836 [project @ 2006-10-20 03:01:43 by jwe] diff --git a/scripts/miscellaneous/zip.m b/scripts/miscellaneous/zip.m --- a/scripts/miscellaneous/zip.m +++ b/scripts/miscellaneous/zip.m @@ -18,8 +18,8 @@ ## 02110-1301, USA. ## -*- texinfo -*- -## @deftypefn {Function File} {@var{entries} =} zip (@var{zipfile},@var{files}) -## @deftypefnx {Function File} {@var{entries} =} zip (@var{zipfile},@var{files},@var{rootdir}) +## @deftypefn {Function File} {@var{entries} =} zip (@var{zipfile}, @var{files}) +## @deftypefnx {Function File} {@var{entries} =} zip (@var{zipfile}, @var{files}, @var{rootdir}) ## Compress the list of files and/or directories specified in @var{files} ## into the archive @var{zipfiles} in the same directory. If @var{rootdir} ## is defined the @var{files} is located relative to @var{rootdir} rather diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,31 @@ +2006-10-19 John W. Eaton + + * ov-struct.cc (octave_struct::as_mxArrary): + Use OCTAVE_LOCAL_BUFFER to allocate tempoarary array of field names. + + * mxarray.h (mxArray::persistent): Delete var and all uses. + (mxArray::mark_persistent, mxArray::unmark_persistent, + mxArray::is_persistent): Delete functions. + * mex.cc (mex::function_name): Use mxArray::strsave, not strsave. + (mex::mark_array): New function. + (mex::make_value): Use it. + (mex::free_value): Return true if we found ptr in arraylist. + (mex::persistent (mxArray *)): Remove ptr from arraylist instead + of marking it as persistent. + (mxArray::malloc): Call ::malloc instead of malloc. + (mxArray::calloc): Call ::calloc instead of calloc. + (maybe_mark_array): New function. + (mxCreateCellArray, mxCreateCellMatrix, mxCreateCharArray, + mxCreateCharMatrixFromStrings, mxCreateDoubleMatrix, + mxCreateDoubleScalar, mxCreateLogicalArray, mxCreateLogicalMatrix, + mxCreateLogicalScalar, mxCreateNumericArray, + mxCreateNumericMatrix, mxCreateSparse, + mxCreateSparseLogicalMatrix, mxCreateString, mxCreateStructArray, + mxCreateStructMatrix, mxDuplicateArray): Use it. + (mxDestroyArray): No need to check persistence now. + Also delete ptr if mex_context->free_value does not. + (call_mex): No need to delete elements of argout now. + 2006-10-18 John W. Eaton * dynamic-ld.cc (octave_shlib_list::remove, diff --git a/src/mex.cc b/src/mex.cc --- a/src/mex.cc +++ b/src/mex.cc @@ -32,10 +32,6 @@ static void xfree (void *ptr) { -#ifdef DEBUG - std::cerr << "free: " << ptr << std::endl; -#endif - ::free (ptr); } @@ -1808,49 +1804,49 @@ // ------------------------------------------------------------------ mxArray::mxArray (const octave_value& ov) - : rep (new mxArray_octave_value (ov)), name (0), persistent (false) { } + : rep (new mxArray_octave_value (ov)), name (0) { } mxArray::mxArray (mxClassID id, int ndims, const int *dims, mxComplexity flag) - : rep (new mxArray_number (id, ndims, dims, flag)), name (0), persistent (false) { } + : rep (new mxArray_number (id, ndims, dims, flag)), name (0) { } mxArray::mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag) - : rep (new mxArray_number (id, dv, flag)), name (0), persistent (false) { } + : rep (new mxArray_number (id, dv, flag)), name (0) { } mxArray::mxArray (mxClassID id, int m, int n, mxComplexity flag) - : rep (new mxArray_number (id, m, n, flag)), name (0), persistent (false) { } + : rep (new mxArray_number (id, m, n, flag)), name (0) { } mxArray::mxArray (mxClassID id, double val) - : rep (new mxArray_number (id, val)), name (0), persistent (false) { } + : rep (new mxArray_number (id, val)), name (0) { } mxArray::mxArray (mxClassID id, mxLogical val) - : rep (new mxArray_number (id, val)), name (0), persistent (false) { } + : rep (new mxArray_number (id, val)), name (0) { } mxArray::mxArray (const char *str) - : rep (new mxArray_number (str)), name (0), persistent (false) { } + : rep (new mxArray_number (str)), name (0) { } mxArray::mxArray (int m, const char **str) - : rep (new mxArray_number (m, str)), name (0), persistent (false) { } + : rep (new mxArray_number (m, str)), name (0) { } mxArray::mxArray (mxClassID id, int m, int n, int nzmax, mxComplexity flag) - : rep (new mxArray_sparse (id, m, n, nzmax, flag)), name (0), persistent (false) { } + : rep (new mxArray_sparse (id, m, n, nzmax, flag)), name (0) { } mxArray::mxArray (int ndims, const int *dims, int num_keys, const char **keys) - : rep (new mxArray_struct (ndims, dims, num_keys, keys)), name (0), persistent (false) { } + : rep (new mxArray_struct (ndims, dims, num_keys, keys)), name (0) { } mxArray::mxArray (const dim_vector& dv, int num_keys, const char **keys) - : rep (new mxArray_struct (dv, num_keys, keys)), name (0), persistent (false) { } + : rep (new mxArray_struct (dv, num_keys, keys)), name (0) { } mxArray::mxArray (int m, int n, int num_keys, const char **keys) - : rep (new mxArray_struct (m, n, num_keys, keys)), name (0), persistent (false) { } + : rep (new mxArray_struct (m, n, num_keys, keys)), name (0) { } mxArray::mxArray (int ndims, const int *dims) - : rep (new mxArray_cell (ndims, dims)), name (0), persistent (false) { } + : rep (new mxArray_cell (ndims, dims)), name (0) { } mxArray::mxArray (const dim_vector& dv) - : rep (new mxArray_cell (dv)), name (0), persistent (false) { } + : rep (new mxArray_cell (dv)), name (0) { } mxArray::mxArray (int m, int n) - : rep (new mxArray_cell (m, n)), name (0), persistent (false) { } + : rep (new mxArray_cell (m, n)), name (0) { } mxArray::~mxArray (void) { @@ -1927,10 +1923,10 @@ if (fcn) { std::string nm = fcn->name (); - fname = strsave (nm.c_str ()); + fname = mxArray::strsave (nm.c_str ()); } else - fname = strsave ("unknown"); + fname = mxArray::strsave ("unknown"); } return fname; @@ -1968,10 +1964,6 @@ { void *ptr = ::malloc (n); -#ifdef DEBUG - std::cerr << "malloc " << n << " bytes: " << ptr << std::endl; -#endif - if (! ptr) { // FIXME -- could use "octave_new_handler();" instead @@ -2021,10 +2013,6 @@ { void *v = ::realloc (ptr, n); -#ifdef DEBUG - std::cerr << "realloc: " << n << " bytes: " << ptr << std::endl; -#endif - std::set::iterator p = memlist.find (ptr); if (v && p != memlist.end ()) @@ -2067,22 +2055,29 @@ // Mark a pointer so that it will not be freed on exit. void persistent (void *ptr) { unmark (ptr); } + mxArray *mark_array (mxArray *ptr) + { + arraylist.insert (ptr); + return ptr; + } + // Make a new array value and initialize from an octave value; it will be // freed on exit unless marked as persistent. mxArray *make_value (const octave_value& ov) { - mxArray *ptr = new mxArray (ov); - arraylist.insert (ptr); - return ptr; + return mark_array (new mxArray (ov)); } // Free an array and its contents. - void free_value (mxArray *ptr) + bool free_value (mxArray *ptr) { + bool inlist = false; + std::set::iterator p = arraylist.find (ptr); if (p != arraylist.end ()) { + inlist = true; arraylist.erase (p); delete ptr; } @@ -2090,12 +2085,17 @@ else warning ("mex::free_value: skipping memory not allocated by mex::make_value"); #endif + + return inlist; } - // Mark an array and its contents so it will not be freed on exit. + // Remove PTR from the list of arrays to be free on exit. void persistent (mxArray *ptr) { - ptr->mark_persistent (); + std::set::iterator p = arraylist.find (ptr); + + if (p != arraylist.end ()) + arraylist.erase (p); } // 1 if error should be returned to MEX file, 0 if abort. @@ -2180,13 +2180,13 @@ void * mxArray::malloc (size_t n) { - return mex_context ? mex_context->malloc_unmarked (n) : malloc (n); + return mex_context ? mex_context->malloc_unmarked (n) : ::malloc (n); } void * mxArray::calloc (size_t n, size_t t) { - return mex_context ? mex_context->calloc_unmarked (n, t) : calloc (n, t); + return mex_context ? mex_context->calloc_unmarked (n, t) : ::calloc (n, t); } // ------------------------------------------------------------------ @@ -2258,123 +2258,124 @@ else free (ptr); } + +static inline mxArray * +maybe_mark_array (mxArray *ptr) +{ + return mex_context ? mex_context->mark_array (ptr) : ptr; +} // Constructors. mxArray * mxCreateCellArray (int ndims, const int *dims) { - return new mxArray (ndims, dims); + return maybe_mark_array (new mxArray (ndims, dims)); } mxArray * mxCreateCellMatrix (int m, int n) { - return new mxArray (m, n); + return maybe_mark_array (new mxArray (m, n)); } mxArray * mxCreateCharArray (int ndims, const int *dims) { - return new mxArray (mxCHAR_CLASS, ndims, dims); + return maybe_mark_array (new mxArray (mxCHAR_CLASS, ndims, dims)); } mxArray * mxCreateCharMatrixFromStrings (int m, const char **str) { - return new mxArray (m, str); + return maybe_mark_array (new mxArray (m, str)); } mxArray * mxCreateDoubleMatrix (int m, int n, mxComplexity flag) { - return new mxArray (mxDOUBLE_CLASS, m, n, flag); + return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, m, n, flag)); } mxArray * mxCreateDoubleScalar (double val) { - return new mxArray (mxDOUBLE_CLASS, val); + return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, val)); } mxArray * mxCreateLogicalArray (int ndims, const int *dims) { - return new mxArray (mxLOGICAL_CLASS, ndims, dims); + return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, ndims, dims)); } mxArray * mxCreateLogicalMatrix (int m, int n) { - return new mxArray (mxLOGICAL_CLASS, m, n); + return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, m, n)); } mxArray * mxCreateLogicalScalar (int val) { - return new mxArray (mxLOGICAL_CLASS, val); + return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, val)); } mxArray * mxCreateNumericArray (int ndims, const int *dims, mxClassID class_id, mxComplexity flag) { - return new mxArray (class_id, ndims, dims, flag); + return maybe_mark_array (new mxArray (class_id, ndims, dims, flag)); } mxArray * mxCreateNumericMatrix (int m, int n, mxClassID class_id, mxComplexity flag) { - return new mxArray (class_id, m, n, flag); + return maybe_mark_array (new mxArray (class_id, m, n, flag)); } mxArray * mxCreateSparse (int m, int n, int nzmax, mxComplexity flag) { - return new mxArray (mxDOUBLE_CLASS, m, n, nzmax, flag); + return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, m, n, nzmax, flag)); } mxArray * mxCreateSparseLogicalMatrix (int m, int n, int nzmax) { - return new mxArray (mxLOGICAL_CLASS, m, n, nzmax); + return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, m, n, nzmax)); } mxArray * mxCreateString (const char *str) { - return new mxArray (str); + return maybe_mark_array (new mxArray (str)); } mxArray * mxCreateStructArray (int ndims, int *dims, int num_keys, const char **keys) { - return new mxArray (ndims, dims, num_keys, keys); + return maybe_mark_array (new mxArray (ndims, dims, num_keys, keys)); } mxArray * mxCreateStructMatrix (int m, int n, int num_keys, const char **keys) { - return new mxArray (m, n, num_keys, keys); + return maybe_mark_array (new mxArray (m, n, num_keys, keys)); } // Copy constructor. mxArray * mxDuplicateArray (const mxArray *ptr) { - return ptr->clone (); + return maybe_mark_array (ptr->clone ()); } // Destructor. void mxDestroyArray (mxArray *ptr) { - if (! ptr->is_persistent ()) - { - if (mex_context) - mex_context->free_value (ptr); - else - delete ptr; - } + if (! (mex_context && mex_context->free_value (ptr))) + delete ptr; } // Type Predicates. @@ -2874,12 +2875,6 @@ retval(i) = mxArray::as_octave_value (argout[i]); } - // Is it always safe to do this? Are users required to use one of - // the mxCreateXYZ functions to create the values that are put in - // the output array? - for (int i = 0; i < nout; i++) - delete argout[i]; - // Clean up mex resources. unwind_protect::run_frame ("call_mex"); diff --git a/src/mxarray.h b/src/mxarray.h --- a/src/mxarray.h +++ b/src/mxarray.h @@ -157,17 +157,11 @@ { mxArray *new_rep = rep->clone (); - return new mxArray (new_rep, name, persistent); + return new mxArray (new_rep, name); } virtual ~mxArray (void); - void mark_persistent (void) const { persistent = true; } - - void unmark_persistent (void) const { persistent = false; } - - bool is_persistent (void) const { return persistent; } - virtual bool is_octave_value (void) const { return rep->is_octave_value (); } virtual int is_cell (void) const { return rep->is_cell (); } @@ -312,7 +306,7 @@ virtual octave_value as_octave_value (void) const; - mxArray (const xmxArray&) : rep (0), name (0), persistent (false) { } + mxArray (const xmxArray&) : rep (0), name (0) { } private: @@ -320,10 +314,8 @@ char *name; - mutable bool persistent; - - mxArray (mxArray *r, const char *n, bool p) - : rep (r), name (strsave (n)), persistent (p) { } + mxArray (mxArray *r, const char *n) + : rep (r), name (strsave (n)) { } void maybe_mutate (void) const; diff --git a/src/ov-struct.cc b/src/ov-struct.cc --- a/src/ov-struct.cc +++ b/src/ov-struct.cc @@ -1278,9 +1278,11 @@ { int nf = nfields (); string_vector kv = map_keys (); - const char **f = static_cast (mxArray::malloc (nf * sizeof (const char *))); + + OCTAVE_LOCAL_BUFFER (const char *, f, nf); + for (int i = 0; i < nf; i++) - f[i] = mxArray::strsave (kv[i].c_str ()); + f[i] = kv[i].c_str (); mxArray *retval = new mxArray (dims (), nf, f);