Mercurial > hg > octave-nkf
changeset 2477:3d905d3820a4
[project @ 1996-11-07 16:46:11 by jwe]
author | jwe |
---|---|
date | Thu, 07 Nov 1996 16:48:16 +0000 |
parents | 4de4cebed088 |
children | 1fa325961eea |
files | PROJECTS liboctave/Array2.cc liboctave/Array3.cc src/ChangeLog src/ov-base.h src/ov-ch-mat.cc src/ov-ch-mat.h src/ov-colon.h src/ov-complex.cc src/ov-complex.h src/ov-cx-mat.cc src/ov-cx-mat.h src/ov-range.cc src/ov-range.h src/ov-re-mat.cc src/ov-re-mat.h src/ov-scalar.cc src/ov-scalar.h src/ov-str-mat.h src/ov-struct.cc src/ov-struct.h src/ov-va-args.h src/ov.cc src/ov.h src/pt-const.cc |
diffstat | 25 files changed, 268 insertions(+), 80 deletions(-) [+] |
line wrap: on
line diff
--- a/PROJECTS +++ b/PROJECTS @@ -45,6 +45,9 @@ * Move rand, eye, xpow, xdiv, etc., functions to the matrix classes. + * Use octave_allocator for memory management in Array classes once + g++ supports static member templates. + * Implement the following functions: -- ppval -- cross -- dot
--- a/liboctave/Array2.cc +++ b/liboctave/Array2.cc @@ -119,6 +119,7 @@ ArrayRep *old_rep = rep; const T *old_data = data (); + int old_d1 = dim1 (); int old_d2 = dim2 (); int old_len = length ();
--- a/liboctave/Array3.cc +++ b/liboctave/Array3.cc @@ -46,16 +46,109 @@ template <class T> void -Array3<T>::resize (int n, int m, int k) +Array3<T>::resize (int r, int c, int p) { - assert (0); // XXX FIXME XXX + if (r < 0 || c < 0 || p < 0) + { + (*current_liboctave_error_handler) + ("can't resize to negative dimension"); + return; + } + + if (r == dim1 () && c == dim2 () && p == dim3 ()) + return; + + ArrayRep *old_rep = rep; + const T *old_data = data (); + + int old_d1 = dim1 (); + int old_d2 = dim2 (); + int old_d3 = dim3 (); + int old_len = length (); + + rep = new ArrayRep (r*c*p); + + d1 = r; + d2 = c; + d3 = p; + + if (old_data && old_len > 0) + { + int min_r = old_d1 < r ? old_d1 : r; + int min_c = old_d2 < c ? old_d2 : c; + int min_p = old_d3 < p ? old_d3 : p; + + for (int k = 0; k < min_p; k++) + for (int j = 0; j < min_c; j++) + for (int i = 0; i < min_r; i++) + xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; + } + + if (--old_rep->count <= 0) + delete old_rep; } template <class T> void Array3<T>::resize (int n, int m, int k, const T& val) { - assert (0); // XXX FIXME XXX + if (r < 0 || c < 0 || p < 0) + { + (*current_liboctave_error_handler) + ("can't resize to negative dimension"); + return; + } + + if (r == dim1 () && c == dim2 () && p == dim3 ()) + return; + + ArrayRep *old_rep = rep; + const T *old_data = data (); + + int old_d1 = dim1 (); + int old_d2 = dim2 (); + int old_d3 = dim3 (); + int old_len = length (); + + rep = new ArrayRep (r*c*p); + + d1 = r; + d2 = c; + d3 = p; + + if (old_data && old_len > 0) + { + int min_r = old_d1 < r ? old_d1 : r; + int min_c = old_d2 < c ? old_d2 : c; + int min_p = old_d3 < p ? old_d3 : p; + + for (int k = 0; k < min_p; k++) + for (int j = 0; j < min_c; j++) + for (int i = 0; i < min_r; i++) + xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; + } + + // If the copy constructor is expensive, this may win. Otherwise, + // it may make more sense to just copy the value everywhere when + // making the new ArrayRep. + + for (int k = 0; k < min_p; k++) + for (int j = min_c; j < c; j++) + for (int i = 0; i < min_r; i++) + xelem (i, j, k) = val; + + for (int k = 0; k < min_p; k++) + for (int j = 0; j < c; j++) + for (int i = min_r; i < r; i++) + xelem (i, j, k) = val; + + for (int k = min_p; k < p; k++) + for (int j = 0; j < c; j++) + for (int i = 0; i < r; i++) + xelem (i, j, k) = val; + + if (--old_rep->count <= 0) + delete old_rep; } /*
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +Thu Nov 7 07:59:07 1996 John W. Eaton <jwe@bevo.che.wisc.edu> + + * ov-struct.cc, ov-struct.h: Add hooks for custom memory management. + * ov-scalar.cc, ov-scalar.h: Likewise. + * ov-re-mat.cc, ov-re-mat.h: Likewise. + * ov-range.cc, ov-range.h: Likewise. + * ov-cx-mat.cc, ov-cx-mat.h: Likewise. + * ov-complex.cc, ov-complex.h: Likewise. + * ov-ch-mat.cc, ov-ch-mat.h: Likewise. + Wed Nov 6 12:32:48 1996 John W. Eaton <jwe@bevo.che.wisc.edu> * pager.cc (do_sync): Don't call clear_external_pager here.
--- a/src/ov-base.h +++ b/src/ov-base.h @@ -45,7 +45,9 @@ class tree_walker; -// Real scalar values. +// A base value type, so that derived types only have to redefine what +// they need (if they are derived from octave_base_value instead of +// octave_value). class octave_base_value : public octave_value @@ -62,11 +64,6 @@ octave_value *clone (void) { return new octave_base_value (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif - type_conv_fcn numeric_conversion_function (void) const { return (type_conv_fcn) 0; } @@ -182,8 +179,10 @@ private: + // Type id of base value objects, set by register_type(). static int t_id; + // Type name of base value objects, defined in ov-base.cc. static const string t_name; };
--- a/src/ov-ch-mat.cc +++ b/src/ov-ch-mat.cc @@ -35,9 +35,14 @@ #include "gripes.h" #include "pr-output.h" -int octave_char_matrix::t_id = -1; +octave_allocator +octave_char_matrix::allocator (sizeof (octave_char_matrix)); -const string octave_char_matrix::t_name ("char matrix"); +int +octave_char_matrix::t_id (-1); + +const string +octave_char_matrix::t_name ("char matrix"); bool octave_char_matrix::valid_as_scalar_index (void) const
--- a/src/ov-ch-mat.h +++ b/src/ov-ch-mat.h @@ -34,6 +34,7 @@ class ostream; #include "mx-base.h" +#include "oct-alloc.h" #include "str-vec.h" #include "error.h" @@ -45,7 +46,7 @@ class tree_walker; -// Real scalar values. +// Character matrix values. class octave_char_matrix : public octave_base_value @@ -74,10 +75,11 @@ octave_value *clone (void) { return new octave_char_matrix (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } int rows (void) const { return matrix.rows (); } int columns (void) const { return matrix.columns (); } @@ -129,8 +131,12 @@ charMatrix matrix; + static octave_allocator allocator; + + // Type id of character matrix objects, set by register_type(). static int t_id; + // Type name of character matrix objects, defined in ov-ch-mat.cc. static const string t_name; };
--- a/src/ov-colon.h +++ b/src/ov-colon.h @@ -41,7 +41,7 @@ class tree_walker; -// Real scalar values. +// A type to represent `:' as used for indexing. class octave_magic_colon : public octave_base_value @@ -58,11 +58,6 @@ octave_value *clone (void) { return new octave_magic_colon (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif - idx_vector index_vector (void) const { return idx_vector (':'); } bool is_defined (void) const { return true; } @@ -86,8 +81,10 @@ private: + // Type id of magic colon objects, set by register_type(). static int t_id; + // Type name of magic colon objects, defined in ov-colon.cc. static const string t_name; };
--- a/src/ov-complex.cc +++ b/src/ov-complex.cc @@ -38,9 +38,14 @@ #include "gripes.h" #include "pr-output.h" -int octave_complex::t_id = -1; +octave_allocator +octave_complex::allocator (sizeof (octave_complex)); -const string octave_complex::t_name ("complex scalar"); +int +octave_complex::t_id (-1); + +const string +octave_complex::t_name ("complex scalar"); octave_value * octave_complex::try_narrowing_conversion (void)
--- a/src/ov-complex.h +++ b/src/ov-complex.h @@ -34,6 +34,7 @@ class ostream; #include "mx-base.h" +#include "oct-alloc.h" #include "str-vec.h" #include "error.h" @@ -45,7 +46,7 @@ class tree_walker; -// Real scalar values. +// Complex scalar values. class octave_complex : public octave_base_value @@ -65,10 +66,11 @@ octave_value *clone (void) { return new octave_complex (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } octave_value *try_narrowing_conversion (void); @@ -133,8 +135,12 @@ Complex scalar; + static octave_allocator allocator; + + // Type id of complex scalar objects, set in register_type(). static int t_id; + // Type name of complex scalar objects, defined in ov-complex.cc. static const string t_name; };
--- a/src/ov-cx-mat.cc +++ b/src/ov-cx-mat.cc @@ -40,9 +40,14 @@ #include "ov-scalar.h" #include "pr-output.h" -int octave_complex_matrix::t_id = -1; +octave_allocator +octave_complex_matrix::allocator (sizeof (octave_complex_matrix)); -const string octave_complex_matrix::t_name ("complex matrix"); +int +octave_complex_matrix::t_id (-1); + +const string +octave_complex_matrix::t_name ("complex matrix"); octave_complex_matrix::octave_complex_matrix (const ComplexRowVector& v, int pcv)
--- a/src/ov-cx-mat.h +++ b/src/ov-cx-mat.h @@ -34,6 +34,7 @@ class ostream; #include "mx-base.h" +#include "oct-alloc.h" #include "str-vec.h" #include "error.h" @@ -45,7 +46,7 @@ class tree_walker; -// Real scalar values. +// Complex matrix values. class octave_complex_matrix : public octave_base_value @@ -72,10 +73,11 @@ octave_value *clone (void) { return new octave_complex_matrix (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } octave_value *try_narrowing_conversion (void); @@ -145,8 +147,12 @@ ComplexMatrix matrix; + static octave_allocator allocator; + + // Type id of complex matrix objects, set by register_type(). static int t_id; + // Type name of complex matrix objects, defined in ov-cx-mat.cc. static const string t_name; };
--- a/src/ov-range.cc +++ b/src/ov-range.cc @@ -38,9 +38,14 @@ #include "ov-scalar.h" #include "pr-output.h" -int octave_range::t_id = -1; +octave_allocator +octave_range::allocator (sizeof (octave_range)); -const string octave_range::t_name ("range"); +int +octave_range::t_id (-1); + +const string +octave_range::t_name ("range"); static octave_value * default_numeric_conversion_function (const octave_value& a)
--- a/src/ov-range.h +++ b/src/ov-range.h @@ -37,6 +37,7 @@ #include "lo-utils.h" #include "mx-base.h" +#include "oct-alloc.h" #include "str-vec.h" #include "error.h" @@ -80,10 +81,11 @@ octave_value *clone (void) { return new octave_range (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } type_conv_fcn numeric_conversion_function (void) const; @@ -156,8 +158,12 @@ Range range; + static octave_allocator allocator; + + // Type id of range objects, set by register_type (). static int t_id; + // Type name of scalar objects, defined in ov-range.cc. static const string t_name; };
--- a/src/ov-re-mat.cc +++ b/src/ov-re-mat.cc @@ -40,9 +40,14 @@ #include "ov-re-mat.h" #include "pr-output.h" -int octave_matrix::t_id = -1; +octave_allocator +octave_matrix::allocator (sizeof (octave_matrix)); -const string octave_matrix::t_name ("matrix"); +int +octave_matrix::t_id (-1); + +const string +octave_matrix::t_name ("matrix"); octave_matrix::octave_matrix (const RowVector& v, int pcv) : octave_base_value (),
--- a/src/ov-re-mat.h +++ b/src/ov-re-mat.h @@ -34,6 +34,7 @@ class ostream; #include "mx-base.h" +#include "oct-alloc.h" #include "str-vec.h" #include "error.h" @@ -72,10 +73,11 @@ octave_value *clone (void) { return new octave_matrix (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } octave_value *try_narrowing_conversion (void); @@ -146,8 +148,12 @@ Matrix matrix; + static octave_allocator allocator; + + // Type id of matrix objects, set by register_type (). static int t_id; + // Type name of matrix objects, defined in ov-re-mat.cc. static const string t_name; };
--- a/src/ov-scalar.cc +++ b/src/ov-scalar.cc @@ -38,9 +38,14 @@ #include "xdiv.h" #include "xpow.h" -int octave_scalar::t_id = -1; +octave_allocator +octave_scalar::allocator (sizeof (octave_scalar)); -const string octave_scalar::t_name ("scalar"); +int +octave_scalar::t_id (-1); + +const string +octave_scalar::t_name ("scalar"); static inline bool valid_scalar_indices (const octave_value_list& args)
--- a/src/ov-scalar.h +++ b/src/ov-scalar.h @@ -35,6 +35,7 @@ #include "lo-utils.h" #include "mx-base.h" +#include "oct-alloc.h" #include "str-vec.h" #include "mappers.h" @@ -66,10 +67,11 @@ octave_value *clone (void) { return new octave_scalar (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } octave_value index (const octave_value_list& idx) const; @@ -132,10 +134,16 @@ private: + // The value of this scalar. double scalar; + // For custom memory management. + static octave_allocator allocator; + + // Type id of scalar objects, set by register_type(). static int t_id; + // Type name of scalar objects, defined in ov-scalar.cc. static const string t_name; };
--- a/src/ov-str-mat.h +++ b/src/ov-str-mat.h @@ -45,7 +45,8 @@ class tree_walker; -// Real scalar values. +// Character matrix values with special properties for use as +// strings. class octave_char_matrix_str : public octave_char_matrix @@ -77,11 +78,6 @@ octave_value *clone (void) { return new octave_char_matrix_str (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif - type_conv_fcn numeric_conversion_function (void) const; octave_value index (const octave_value_list& idx) const; @@ -119,8 +115,10 @@ private: + // Type id of char_matrix_str objects, set by register_type(). static int t_id; + // Type name of char_matrix_strXX objects, defined in ov-str-mat.cc. static const string t_name; };
--- a/src/ov-struct.cc +++ b/src/ov-struct.cc @@ -34,10 +34,14 @@ #include "ov-struct.h" #include "unwind-prot.h" -int octave_struct::t_id = -1; +octave_allocator +octave_struct::allocator (sizeof (octave_struct)); -const string octave_struct::t_name ("struct"); +int +octave_struct::t_id (-1); +const string +octave_struct::t_name ("struct"); octave_value octave_struct::struct_elt_val (const string& nm, bool silent) const
--- a/src/ov-struct.h +++ b/src/ov-struct.h @@ -37,6 +37,7 @@ #include "str-vec.h" #include "error.h" +#include "oct-alloc.h" #include "oct-map.h" #include "ov-base.h" #include "ov-typeinfo.h" @@ -46,7 +47,7 @@ class tree_walker; -// Real scalar values. +// Data structures. class octave_struct : public octave_base_value @@ -66,10 +67,11 @@ octave_value *clone (void) { return new octave_struct (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } octave_value struct_elt_val (const string& nm, bool silent) const; @@ -94,10 +96,16 @@ private: + // The associative array used to manage the structure data. Octave_map map; + // For custom memory management. + static octave_allocator allocator; + + // Type id of struct objects, set by register_type(). static int t_id; + // Type name of struct objects, defined in ov-struct.cc. static const string t_name; };
--- a/src/ov-va-args.h +++ b/src/ov-va-args.h @@ -41,7 +41,7 @@ class tree_walker; -// Real scalar values. +// A type to represent `all_va_args' as used in function calls. class octave_all_va_args : public octave_base_value @@ -58,11 +58,6 @@ octave_value *clone (void) { return new octave_all_va_args (*this); } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif - bool is_defined (void) const { return true; } bool is_all_va_args (void) const { return true; } @@ -80,8 +75,10 @@ private: + // Type id of all_va_arg objects, set by register_type(). static int t_id; + // Type name of all_va_arg objects, defined in ov-va-args.cc. static const string t_name; };
--- a/src/ov.cc +++ b/src/ov.cc @@ -51,6 +51,11 @@ #include "utils.h" #include "variables.h" +// We are likely to have a lot of octave_value objects to allocate, so +// make the grow_size large. +octave_allocator +octave_value::allocator (sizeof (octave_value), 1024); + // If TRUE, allow assignments like // // octave> A(1) = 3; A(2) = 5
--- a/src/ov.h +++ b/src/ov.h @@ -38,6 +38,7 @@ #include "Range.h" #include "idx-vector.h" #include "mx-base.h" +#include "oct-alloc.h" #include "str-vec.h" #include "error.h" @@ -158,10 +159,11 @@ } } -#if 0 - void *operator new (size_t size); - void operator delete (void *p, size_t size); -#endif + void *operator new (size_t size) + { return allocator.alloc (size); } + + void operator delete (void *p, size_t size) + { allocator.free (p, size); } // Simple assignment. @@ -392,6 +394,8 @@ private: + static octave_allocator allocator; + union { octave_value *freeptr; // For custom memory management.
--- a/src/pt-const.cc +++ b/src/pt-const.cc @@ -60,9 +60,10 @@ #include "utils.h" #include "variables.h" -// We are likely to have a lot of tree_constants to allocate, so make -// the grow_size large. -octave_allocator tree_constant::allocator (sizeof (tree_constant), 1024); +// We are likely to have a lot of tree_constant objects to allocate, +// so make the grow_size large. +octave_allocator +tree_constant::allocator (sizeof (tree_constant), 1024); Octave_map tree_constant::map_value (void) const