5900
|
1 #include <config.h> |
5864
|
2 |
|
3 #include <cfloat> |
|
4 #include <csetjmp> |
5900
|
5 #include <cstdarg> |
5864
|
6 #include <cstdlib> |
5900
|
7 #include <cstring> |
|
8 #include <cctype> |
|
9 |
5864
|
10 #include <set> |
5900
|
11 |
|
12 #include "f77-fcn.h" |
|
13 #include "lo-ieee.h" |
|
14 |
|
15 // mxArray must be declared as a class before including mexproto.h. |
|
16 class mxArray; |
|
17 #include "Cell.h" |
|
18 #include "mexproto.h" |
|
19 #include "oct-map.h" |
|
20 #include "oct-obj.h" |
|
21 #include "ov.h" |
6068
|
22 #include "ov-mex-fcn.h" |
5900
|
23 #include "ov-usr-fcn.h" |
5864
|
24 #include "pager.h" |
|
25 #include "parse.h" |
|
26 #include "toplev.h" |
5900
|
27 #include "unwind-prot.h" |
|
28 #include "utils.h" |
5864
|
29 #include "variables.h" |
5900
|
30 |
|
31 // #define DEBUG 1 |
|
32 |
5905
|
33 static void |
|
34 xfree (void *ptr) |
|
35 { |
|
36 ::free (ptr); |
|
37 } |
|
38 |
5900
|
39 static int |
|
40 max_str_len (int m, const char **str) |
|
41 { |
|
42 int max_len = 0; |
|
43 |
|
44 for (int i = 0; i < m; i++) |
|
45 { |
|
46 int tmp = strlen (str[i]); |
|
47 |
|
48 if (tmp > max_len) |
|
49 max_len = tmp; |
|
50 } |
|
51 |
|
52 return max_len; |
|
53 } |
|
54 |
|
55 static int |
|
56 valid_key (const char *key) |
|
57 { |
|
58 int retval = 0; |
|
59 |
|
60 int nel = strlen (key); |
|
61 |
|
62 if (nel > 0) |
|
63 { |
|
64 if (isalpha (key[0])) |
|
65 { |
|
66 for (int i = 1; i < nel; i++) |
|
67 { |
|
68 if (! (isalnum (key[i]) || key[i] == '_')) |
|
69 goto done; |
|
70 } |
|
71 |
|
72 retval = 1; |
|
73 } |
|
74 } |
|
75 |
|
76 done: |
|
77 |
|
78 return retval; |
|
79 } |
|
80 |
|
81 // ------------------------------------------------------------------ |
|
82 |
|
83 // A class to provide the default implemenation of some of the virtual |
|
84 // functions declared in the mxArray class. |
|
85 |
|
86 class mxArray_base : public mxArray |
|
87 { |
|
88 protected: |
|
89 |
|
90 mxArray_base (void) : mxArray (xmxArray ()) { } |
|
91 |
|
92 public: |
|
93 |
|
94 mxArray *clone (void) const = 0; |
|
95 |
|
96 ~mxArray_base (void) { } |
|
97 |
|
98 bool is_octave_value (void) const { return false; } |
|
99 |
|
100 int is_cell (void) const = 0; |
|
101 |
|
102 int is_char (void) const = 0; |
|
103 |
|
104 int is_class (const char *name_arg) const |
|
105 { |
|
106 int retval = 0; |
|
107 |
|
108 const char *cname = get_class_name (); |
|
109 |
|
110 if (cname && name_arg) |
|
111 retval = ! strcmp (cname, name_arg); |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 int is_complex (void) const = 0; |
|
117 |
|
118 int is_double (void) const = 0; |
|
119 |
|
120 int is_int16 (void) const = 0; |
|
121 |
|
122 int is_int32 (void) const = 0; |
|
123 |
|
124 int is_int64 (void) const = 0; |
|
125 |
|
126 int is_int8 (void) const = 0; |
|
127 |
|
128 int is_logical (void) const = 0; |
|
129 |
|
130 int is_numeric (void) const = 0; |
|
131 |
|
132 int is_single (void) const = 0; |
|
133 |
|
134 int is_sparse (void) const = 0; |
|
135 |
|
136 int is_struct (void) const = 0; |
|
137 |
|
138 int is_uint16 (void) const = 0; |
|
139 |
|
140 int is_uint32 (void) const = 0; |
|
141 |
|
142 int is_uint64 (void) const = 0; |
|
143 |
|
144 int is_uint8 (void) const = 0; |
|
145 |
|
146 int is_logical_scalar (void) const |
|
147 { |
|
148 return is_logical () && get_number_of_elements () == 1; |
|
149 } |
|
150 |
|
151 int is_logical_scalar_true (void) const = 0; |
|
152 |
|
153 int get_m (void) const = 0; |
|
154 |
|
155 int get_n (void) const = 0; |
|
156 |
|
157 int *get_dimensions (void) const = 0; |
|
158 |
|
159 int get_number_of_dimensions (void) const = 0; |
|
160 |
|
161 void set_m (int m) = 0; |
|
162 |
|
163 void set_n (int n) = 0; |
|
164 |
|
165 void set_dimensions (int *dims_arg, int ndims_arg) = 0; |
|
166 |
|
167 int get_number_of_elements (void) const = 0; |
|
168 |
|
169 int is_empty (void) const = 0; |
|
170 |
|
171 mxClassID get_class_id (void) const = 0; |
|
172 |
|
173 const char *get_class_name (void) const = 0; |
|
174 |
|
175 void set_class_name (const char *name_arg) = 0; |
|
176 |
|
177 mxArray *get_cell (int /*idx*/) const |
|
178 { |
|
179 invalid_type_error (); |
|
180 return 0; |
|
181 } |
|
182 |
|
183 void set_cell (int idx, mxArray *val) = 0; |
|
184 |
|
185 void *get_data (void) const = 0; |
|
186 |
|
187 void *get_imag_data (void) const = 0; |
|
188 |
|
189 void set_data (void *pr) = 0; |
|
190 |
|
191 void set_imag_data (void *pi) = 0; |
|
192 |
|
193 int *get_ir (void) const = 0; |
|
194 |
|
195 int *get_jc (void) const = 0; |
|
196 |
|
197 int get_nzmax (void) const = 0; |
|
198 |
|
199 void set_ir (int *ir) = 0; |
|
200 |
|
201 void set_jc (int *jc) = 0; |
|
202 |
|
203 void set_nzmax (int nzmax) = 0; |
|
204 |
|
205 int add_field (const char *key) = 0; |
|
206 |
|
207 void remove_field (int key_num) = 0; |
|
208 |
|
209 mxArray *get_field_by_number (int index, int key_num) const = 0; |
|
210 |
|
211 void set_field_by_number (int index, int key_num, mxArray *val) = 0; |
|
212 |
|
213 int get_number_of_fields (void) const = 0; |
|
214 |
|
215 const char *get_field_name_by_number (int key_num) const = 0; |
|
216 |
|
217 int get_field_number (const char *key) const = 0; |
|
218 |
|
219 int get_string (char *buf, int buflen) const = 0; |
|
220 |
|
221 char *array_to_string (void) const = 0; |
|
222 |
|
223 int calc_single_subscript (int nsubs, int *subs) const = 0; |
|
224 |
|
225 int get_element_size (void) const = 0; |
|
226 |
|
227 bool mutation_needed (void) const { return false; } |
|
228 |
|
229 mxArray *mutate (void) const { return 0; } |
|
230 |
|
231 protected: |
|
232 |
5907
|
233 octave_value as_octave_value (void) const = 0; |
|
234 |
5900
|
235 mxArray_base (const mxArray_base&) : mxArray (xmxArray ()) { } |
|
236 |
|
237 void invalid_type_error (void) const |
|
238 { |
|
239 error ("invalid type for operation"); |
|
240 } |
|
241 |
|
242 void error (const char *msg) const |
|
243 { |
|
244 // FIXME |
|
245 ::error ("%s", msg); |
|
246 } |
|
247 }; |
|
248 |
|
249 // The object that handles values pass to MEX files from Octave. Some |
|
250 // methods in this class may set mutate_flag to TRUE to tell the |
|
251 // mxArray class to convert to the Matlab-style representation and |
|
252 // then invoke the method on that object instead (for example, getting |
|
253 // a pointer to real or imaginary data from a complex object requires |
|
254 // a mutation but getting a pointer to real data from a real object |
|
255 // does not). Changing the representation causes a copy so we try to |
|
256 // avoid it unless it is really necessary. Once the conversion |
|
257 // happens, we delete this representation, so the conversion can only |
|
258 // happen once per call to a MEX file. |
|
259 |
|
260 class mxArray_octave_value : public mxArray_base |
|
261 { |
|
262 public: |
|
263 |
|
264 mxArray_octave_value (const octave_value& ov) |
|
265 : mxArray_base (), val (ov), mutate_flag (false), |
|
266 id (mxUNKNOWN_CLASS), class_name (0), ndims (-1), dims (0) { } |
|
267 |
|
268 mxArray *clone (void) const { return new mxArray_octave_value (*this); } |
|
269 |
|
270 ~mxArray_octave_value (void) |
|
271 { |
|
272 mxFree (class_name); |
|
273 mxFree (dims); |
|
274 } |
|
275 |
|
276 bool is_octave_value (void) const { return true; } |
|
277 |
|
278 int is_cell (void) const { return val.is_cell (); } |
|
279 |
|
280 int is_char (void) const { return val.is_string (); } |
|
281 |
|
282 int is_complex (void) const { return val.is_complex_type (); } |
|
283 |
|
284 int is_double (void) const { return val.is_double_type (); } |
|
285 |
|
286 int is_int16 (void) const { return val.is_int16_type (); } |
|
287 |
|
288 int is_int32 (void) const { return val.is_int32_type (); } |
|
289 |
|
290 int is_int64 (void) const { return val.is_int64_type (); } |
|
291 |
|
292 int is_int8 (void) const { return val.is_int8_type (); } |
|
293 |
|
294 int is_logical (void) const { return val.is_bool_type (); } |
|
295 |
|
296 int is_numeric (void) const { return val.is_numeric_type (); } |
|
297 |
|
298 int is_single (void) const { return val.is_single_type (); } |
|
299 |
|
300 int is_sparse (void) const { return val.is_sparse_type (); } |
|
301 |
|
302 int is_struct (void) const { return val.is_map (); } |
|
303 |
|
304 int is_uint16 (void) const { return val.is_uint16_type (); } |
|
305 |
6069
|
306 int is_uint32 (void) const { return val.is_uint32_type (); } |
|
307 |
|
308 int is_uint64 (void) const { return val.is_uint64_type (); } |
|
309 |
|
310 int is_uint8 (void) const { return val.is_uint8_type (); } |
5900
|
311 |
|
312 int is_range (void) const { return val.is_range (); } |
|
313 |
|
314 int is_real_type (void) const { return val.is_real_type (); } |
|
315 |
|
316 int is_logical_scalar_true (void) const |
|
317 { |
|
318 return (is_logical_scalar () && val.is_true ()); |
|
319 } |
|
320 |
|
321 int get_m (void) const { return val.rows (); } |
|
322 |
6187
|
323 int get_n (void) const |
|
324 { |
|
325 int n = 1; |
|
326 |
|
327 // Force dims and ndims to be cached. |
|
328 get_dimensions(); |
|
329 |
|
330 for (int i = ndims - 1; i > 0; i--) |
|
331 n *= dims[i]; |
|
332 |
|
333 return n; |
|
334 } |
5900
|
335 |
|
336 int *get_dimensions (void) const |
|
337 { |
|
338 if (! dims) |
|
339 { |
|
340 // Force ndims to be cached. |
|
341 get_number_of_dimensions (); |
|
342 |
|
343 dims = static_cast<int *> (malloc (ndims * sizeof (int))); |
|
344 |
|
345 dim_vector dv = val.dims (); |
|
346 |
|
347 for (int i = 0; i < ndims; i++) |
|
348 dims[i] = dv(i); |
|
349 } |
|
350 |
|
351 return dims; |
|
352 } |
|
353 |
|
354 int get_number_of_dimensions (void) const |
|
355 { |
|
356 if (ndims < 0) |
|
357 ndims = val.ndims (); |
|
358 |
|
359 return ndims; |
|
360 } |
|
361 |
|
362 void set_m (int /*m*/) { panic_impossible (); } |
|
363 |
|
364 void set_n (int /*n*/) { panic_impossible (); } |
|
365 |
|
366 void set_dimensions (int */*dims_arg*/, int /*ndims_arg*/) |
|
367 { |
|
368 panic_impossible (); |
|
369 } |
|
370 |
|
371 int get_number_of_elements (void) const { return val.numel (); } |
|
372 |
|
373 int is_empty (void) const { return val.is_empty (); } |
|
374 |
|
375 mxClassID get_class_id (void) const |
|
376 { |
|
377 id = mxUNKNOWN_CLASS; |
|
378 |
|
379 std::string cn = val.class_name (); |
|
380 |
|
381 if (cn == "cell") |
|
382 id = mxCELL_CLASS; |
|
383 else if (cn == "struct") |
|
384 id = mxSTRUCT_CLASS; |
|
385 else if (cn == "logical") |
|
386 id = mxLOGICAL_CLASS; |
|
387 else if (cn == "char") |
|
388 id = mxCHAR_CLASS; |
|
389 else if (cn == "double") |
|
390 id = mxDOUBLE_CLASS; |
5903
|
391 else if (cn == "sparse") |
|
392 { |
|
393 if (val.is_bool_type ()) |
|
394 id = mxLOGICAL_CLASS; |
|
395 else |
|
396 id = mxDOUBLE_CLASS; |
|
397 } |
5900
|
398 else if (cn == "single") |
|
399 id = mxSINGLE_CLASS; |
|
400 else if (cn == "int8") |
|
401 id = mxINT8_CLASS; |
|
402 else if (cn == "uint8") |
|
403 id = mxUINT8_CLASS; |
|
404 else if (cn == "int16") |
|
405 id = mxINT16_CLASS; |
|
406 else if (cn == "uint16") |
|
407 id = mxUINT16_CLASS; |
|
408 else if (cn == "int32") |
|
409 id = mxINT32_CLASS; |
|
410 else if (cn == "uint32") |
|
411 id = mxUINT32_CLASS; |
|
412 else if (cn == "int64") |
|
413 id = mxINT64_CLASS; |
|
414 else if (cn == "uint64") |
|
415 id = mxUINT64_CLASS; |
|
416 else if (cn == "function handle") |
|
417 id = mxFUNCTION_CLASS; |
|
418 |
|
419 return id; |
|
420 } |
|
421 |
|
422 const char *get_class_name (void) const |
|
423 { |
|
424 if (! class_name) |
|
425 { |
|
426 std::string s = val.class_name (); |
|
427 class_name = strsave (s.c_str ()); |
|
428 } |
|
429 |
|
430 return class_name; |
|
431 } |
|
432 |
|
433 // Not allowed. |
|
434 void set_class_name (const char */*name_arg*/) { panic_impossible (); } |
|
435 |
|
436 mxArray *get_cell (int /*idx*/) const |
|
437 { |
|
438 request_mutation (); |
|
439 return 0; |
|
440 } |
|
441 |
|
442 // Not allowed. |
|
443 void set_cell (int /*idx*/, mxArray */*val*/) { panic_impossible (); } |
|
444 |
|
445 void *get_data (void) const |
|
446 { |
|
447 void *retval = 0; |
|
448 |
|
449 if (is_char () |
|
450 || (is_numeric () && is_real_type () && ! is_range ())) |
|
451 retval = val.mex_get_data (); |
|
452 else |
|
453 request_mutation (); |
|
454 |
|
455 return retval; |
|
456 } |
|
457 |
|
458 void *get_imag_data (void) const |
|
459 { |
|
460 void *retval = 0; |
|
461 |
|
462 if (is_numeric () && is_real_type ()) |
|
463 retval = 0; |
|
464 else |
|
465 request_mutation (); |
|
466 |
|
467 return retval; |
|
468 } |
|
469 |
|
470 // Not allowed. |
|
471 void set_data (void */*pr*/) { panic_impossible (); } |
|
472 |
|
473 // Not allowed. |
|
474 void set_imag_data (void */*pi*/) { panic_impossible (); } |
|
475 |
|
476 int *get_ir (void) const |
|
477 { |
|
478 #if SIZEOF_OCTAVE_IDX_TYPE == SIZEOF_INT |
5902
|
479 return val.mex_get_ir (); |
5900
|
480 #else |
|
481 request_mutation (); |
|
482 return 0; |
|
483 #endif |
|
484 } |
|
485 |
|
486 int *get_jc (void) const |
|
487 { |
|
488 #if SIZEOF_OCTAVE_IDX_TYPE == SIZEOF_INT |
|
489 return val.mex_get_jc (); |
|
490 #else |
|
491 request_mutation (); |
|
492 return 0; |
|
493 #endif |
|
494 } |
|
495 |
|
496 int get_nzmax (void) const { return val.nzmax (); } |
|
497 |
|
498 // Not allowed. |
|
499 void set_ir (int */*ir*/) { panic_impossible (); } |
|
500 |
|
501 // Not allowed. |
|
502 void set_jc (int */*jc*/) { panic_impossible (); } |
|
503 |
|
504 // Not allowed. |
|
505 void set_nzmax (int /*nzmax*/) { panic_impossible (); } |
|
506 |
|
507 // Not allowed. |
|
508 int add_field (const char */*key*/) |
|
509 { |
|
510 panic_impossible (); |
|
511 return -1; |
|
512 } |
|
513 |
|
514 // Not allowed. |
|
515 void remove_field (int /*key_num*/) { panic_impossible (); } |
|
516 |
|
517 mxArray *get_field_by_number (int /*index*/, int /*key_num*/) const |
|
518 { |
|
519 request_mutation (); |
|
520 return 0; |
|
521 } |
|
522 |
|
523 // Not allowed. |
|
524 void set_field_by_number (int /*index*/, int /*key_num*/, mxArray */*val*/) |
|
525 { |
|
526 panic_impossible (); |
|
527 } |
|
528 |
|
529 int get_number_of_fields (void) const { return val.nfields (); } |
|
530 |
|
531 const char *get_field_name_by_number (int /*key_num*/) const |
|
532 { |
|
533 request_mutation (); |
|
534 return 0; |
|
535 } |
|
536 |
|
537 int get_field_number (const char */*key*/) const |
|
538 { |
|
539 request_mutation (); |
|
540 return 0; |
|
541 } |
|
542 |
|
543 int get_string (char *buf, int buflen) const |
|
544 { |
|
545 int retval = 1; |
|
546 |
|
547 int nel = get_number_of_elements (); |
|
548 |
|
549 if (val.is_string () && nel < buflen) |
|
550 { |
|
551 charNDArray tmp = val.char_array_value (); |
|
552 |
|
553 const char *p = tmp.data (); |
|
554 |
|
555 for (int i = 0; i < buflen; i++) |
|
556 buf[i] = p[i]; |
|
557 |
|
558 buf[nel] = 0; |
|
559 |
|
560 retval = 0; |
|
561 } |
|
562 |
|
563 return retval; |
|
564 } |
|
565 |
|
566 char *array_to_string (void) const |
|
567 { |
|
568 // FIXME -- this is suposed to handle multi-byte character |
|
569 // strings. |
|
570 |
|
571 char *buf = 0; |
|
572 |
|
573 if (val.is_string ()) |
|
574 { |
|
575 int nel = get_number_of_elements (); |
|
576 |
|
577 buf = static_cast<char *> (malloc (nel + 1)); |
|
578 |
|
579 if (buf) |
|
580 { |
|
581 charNDArray tmp = val.char_array_value (); |
|
582 |
|
583 const char *p = tmp.data (); |
|
584 |
|
585 for (int i = 0; i < nel; i++) |
|
586 buf[i] = p[i]; |
|
587 |
|
588 buf[nel] = '\0'; |
|
589 } |
|
590 } |
|
591 |
|
592 return buf; |
|
593 } |
|
594 |
|
595 int calc_single_subscript (int nsubs, int *subs) const |
|
596 { |
|
597 int retval = 0; |
|
598 |
|
599 // Force ndims, dims to be cached. |
|
600 get_dimensions (); |
|
601 |
|
602 int n = nsubs <= ndims ? nsubs : ndims; |
|
603 |
|
604 while (--n > 0) |
|
605 retval = retval * dims[n] + subs[n]; |
|
606 |
|
607 return retval; |
|
608 } |
|
609 |
|
610 int get_element_size (void) const |
|
611 { |
|
612 // Force id to be cached. |
|
613 get_class_id (); |
|
614 |
|
615 switch (id) |
|
616 { |
|
617 case mxCELL_CLASS: return sizeof (mxArray *); |
|
618 case mxSTRUCT_CLASS: return sizeof (mxArray *); |
|
619 case mxLOGICAL_CLASS: return sizeof (mxLogical); |
|
620 case mxCHAR_CLASS: return sizeof (mxChar); |
|
621 case mxDOUBLE_CLASS: return sizeof (double); |
|
622 case mxSINGLE_CLASS: return sizeof (float); |
|
623 case mxINT8_CLASS: return 1; |
|
624 case mxUINT8_CLASS: return 1; |
|
625 case mxINT16_CLASS: return 2; |
|
626 case mxUINT16_CLASS: return 2; |
|
627 case mxINT32_CLASS: return 4; |
|
628 case mxUINT32_CLASS: return 4; |
|
629 case mxINT64_CLASS: return 8; |
|
630 case mxUINT64_CLASS: return 8; |
|
631 case mxFUNCTION_CLASS: return 0; |
|
632 default: return 0; |
|
633 } |
|
634 } |
|
635 |
|
636 bool mutation_needed (void) const { return mutate_flag; } |
|
637 |
|
638 void request_mutation (void) const |
|
639 { |
|
640 if (mutate_flag) |
|
641 panic_impossible (); |
|
642 |
|
643 mutate_flag = true; |
|
644 } |
|
645 |
|
646 mxArray *mutate (void) const { return val.as_mxArray (); } |
|
647 |
|
648 protected: |
|
649 |
5907
|
650 octave_value as_octave_value (void) const { return val; } |
|
651 |
5900
|
652 mxArray_octave_value (const mxArray_octave_value& arg) |
|
653 : mxArray_base (arg), val (arg.val), mutate_flag (arg.mutate_flag), |
|
654 id (arg.id), class_name (strsave (arg.class_name)), ndims (arg.ndims), |
|
655 dims (ndims > 0 ? static_cast<int *> (malloc (ndims * sizeof (int))) : 0) |
|
656 { |
|
657 if (dims) |
|
658 { |
|
659 for (int i = 0; i < ndims; i++) |
|
660 dims[i] = arg.dims[i]; |
|
661 } |
|
662 } |
|
663 |
|
664 private: |
|
665 |
|
666 octave_value val; |
|
667 |
|
668 mutable bool mutate_flag; |
|
669 |
|
670 // Caching these does not cost much or lead to much duplicated |
|
671 // code. For other things, we just request mutation to a |
|
672 // Matlab-style mxArray object. |
|
673 |
|
674 mutable mxClassID id; |
|
675 mutable char *class_name; |
|
676 mutable int ndims; |
|
677 mutable int *dims; |
|
678 }; |
|
679 |
|
680 // The base class for the Matlab-style representation, used to handle |
|
681 // things that are common to all Matlab-style objects. |
|
682 |
|
683 class mxArray_matlab : public mxArray_base |
|
684 { |
|
685 protected: |
|
686 |
|
687 mxArray_matlab (mxClassID id_arg = mxUNKNOWN_CLASS) |
|
688 : mxArray_base (), class_name (0), id (id_arg), ndims (0), dims (0) { } |
|
689 |
|
690 mxArray_matlab (mxClassID id_arg, int ndims_arg, const int *dims_arg) |
|
691 : mxArray_base (), class_name (0), id (id_arg), |
|
692 ndims (ndims_arg < 2 ? 2 : ndims_arg), |
|
693 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
694 { |
|
695 if (ndims_arg < 2) |
|
696 { |
|
697 dims[0] = 1; |
|
698 dims[1] = 1; |
|
699 } |
|
700 |
|
701 for (int i = 0; i < ndims_arg; i++) |
|
702 dims[i] = dims_arg[i]; |
|
703 |
|
704 for (int i = ndims - 1; i > 1; i--) |
|
705 { |
|
706 if (dims[i] == 1) |
|
707 ndims--; |
|
708 else |
|
709 break; |
|
710 } |
|
711 } |
|
712 |
|
713 mxArray_matlab (mxClassID id_arg, const dim_vector& dv) |
|
714 : mxArray_base (), class_name (0), id (id_arg), |
|
715 ndims (dv.length ()), |
|
716 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
717 { |
|
718 for (int i = 0; i < ndims; i++) |
|
719 dims[i] = dv(i); |
|
720 |
|
721 for (int i = ndims - 1; i > 1; i--) |
|
722 { |
|
723 if (dims[i] == 1) |
|
724 ndims--; |
|
725 else |
|
726 break; |
|
727 } |
|
728 } |
|
729 |
|
730 mxArray_matlab (mxClassID id_arg, int m, int n) |
|
731 : mxArray_base (), class_name (0), id (id_arg), ndims (2), |
|
732 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
733 { |
|
734 dims[0] = m; |
|
735 dims[1] = n; |
|
736 } |
|
737 |
|
738 public: |
|
739 |
|
740 ~mxArray_matlab (void) |
|
741 { |
|
742 mxFree (class_name); |
|
743 mxFree (dims); |
|
744 } |
|
745 |
|
746 int is_cell (void) const { return id == mxCELL_CLASS; } |
|
747 |
|
748 int is_char (void) const { return id == mxCHAR_CLASS; } |
|
749 |
|
750 int is_complex (void) const { return 0; } |
|
751 |
|
752 int is_double (void) const { return id == mxDOUBLE_CLASS; } |
|
753 |
|
754 int is_int16 (void) const { return id == mxINT16_CLASS; } |
|
755 |
|
756 int is_int32 (void) const { return id == mxINT32_CLASS; } |
|
757 |
|
758 int is_int64 (void) const { return id == mxINT64_CLASS; } |
|
759 |
|
760 int is_int8 (void) const { return id == mxINT8_CLASS; } |
|
761 |
|
762 int is_logical (void) const { return id == mxLOGICAL_CLASS; } |
|
763 |
|
764 int is_numeric (void) const |
|
765 { |
|
766 return (id == mxDOUBLE_CLASS || id == mxSINGLE_CLASS |
|
767 || id == mxINT8_CLASS || id == mxUINT8_CLASS |
|
768 || id == mxINT16_CLASS || id == mxUINT16_CLASS |
|
769 || id == mxINT32_CLASS || id == mxUINT32_CLASS |
|
770 || id == mxINT64_CLASS || id == mxUINT64_CLASS); |
|
771 } |
|
772 |
|
773 int is_single (void) const { return id == mxSINGLE_CLASS; } |
|
774 |
|
775 int is_sparse (void) const { return 0; } |
|
776 |
|
777 int is_struct (void) const { return id == mxSTRUCT_CLASS; } |
|
778 |
|
779 int is_uint16 (void) const { return id == mxUINT16_CLASS; } |
|
780 |
|
781 int is_uint32 (void) const { return id == mxUINT32_CLASS; } |
|
782 |
|
783 int is_uint64 (void) const { return id == mxUINT64_CLASS; } |
|
784 |
|
785 int is_uint8 (void) const { return id == mxUINT8_CLASS; } |
|
786 |
|
787 int is_logical_scalar_true (void) const |
|
788 { |
|
789 return (is_logical_scalar () |
|
790 && static_cast<mxLogical *> (get_data ())[0] != 0); |
|
791 } |
|
792 |
|
793 int get_m (void) const { return dims[0]; } |
|
794 |
6187
|
795 int get_n (void) const |
|
796 { |
|
797 int n = 1; |
|
798 |
|
799 for (int i = ndims - 1 ; i > 0 ; i--) |
|
800 n *= dims[i]; |
|
801 |
|
802 return n; |
|
803 } |
5900
|
804 |
|
805 int *get_dimensions (void) const { return dims; } |
|
806 |
|
807 int get_number_of_dimensions (void) const { return ndims; } |
|
808 |
|
809 void set_m (int m) { dims[0] = m; } |
|
810 |
|
811 void set_n (int n) { dims[1] = n; } |
|
812 |
|
813 void set_dimensions (int *dims_arg, int ndims_arg) |
|
814 { |
|
815 dims = dims_arg; |
|
816 ndims = ndims_arg; |
|
817 } |
|
818 |
|
819 int get_number_of_elements (void) const |
|
820 { |
|
821 int retval = dims[0]; |
|
822 |
|
823 for (int i = 1; i < ndims; i++) |
|
824 retval *= dims[i]; |
|
825 |
|
826 return retval; |
|
827 } |
|
828 |
|
829 int is_empty (void) const { return get_number_of_elements () == 0; } |
|
830 |
|
831 mxClassID get_class_id (void) const { return id; } |
|
832 |
|
833 const char *get_class_name (void) const |
|
834 { |
|
835 switch (id) |
|
836 { |
|
837 case mxCELL_CLASS: return "cell"; |
|
838 case mxSTRUCT_CLASS: return "struct"; |
|
839 case mxLOGICAL_CLASS: return "logical"; |
|
840 case mxCHAR_CLASS: return "char"; |
|
841 case mxDOUBLE_CLASS: return "double"; |
|
842 case mxSINGLE_CLASS: return "single"; |
|
843 case mxINT8_CLASS: return "int8"; |
|
844 case mxUINT8_CLASS: return "uint8"; |
|
845 case mxINT16_CLASS: return "int16"; |
|
846 case mxUINT16_CLASS: return "uint16"; |
|
847 case mxINT32_CLASS: return "int32"; |
|
848 case mxUINT32_CLASS: return "uint32"; |
|
849 case mxINT64_CLASS: return "int64"; |
|
850 case mxUINT64_CLASS: return "uint64"; |
|
851 case mxFUNCTION_CLASS: return "function handle"; |
|
852 default: return "unknown"; |
|
853 } |
|
854 } |
|
855 |
|
856 void set_class_name (const char *name_arg) |
|
857 { |
|
858 mxFree (class_name); |
|
859 class_name = static_cast<char *> (malloc (strlen (name_arg) + 1)); |
|
860 strcpy (class_name, name_arg); |
|
861 } |
|
862 |
|
863 mxArray *get_cell (int /*idx*/) const |
|
864 { |
|
865 invalid_type_error (); |
|
866 return 0; |
|
867 } |
|
868 |
|
869 void set_cell (int /*idx*/, mxArray */*val*/) |
|
870 { |
|
871 invalid_type_error (); |
|
872 } |
|
873 |
|
874 void *get_data (void) const |
|
875 { |
|
876 invalid_type_error (); |
|
877 return 0; |
|
878 } |
|
879 |
|
880 void *get_imag_data (void) const |
|
881 { |
|
882 invalid_type_error (); |
|
883 return 0; |
|
884 } |
|
885 |
|
886 void set_data (void */*pr*/) |
|
887 { |
|
888 invalid_type_error (); |
|
889 } |
|
890 |
|
891 void set_imag_data (void */*pi*/) |
|
892 { |
|
893 invalid_type_error (); |
|
894 } |
|
895 |
|
896 int *get_ir (void) const |
|
897 { |
|
898 invalid_type_error (); |
|
899 return 0; |
|
900 } |
|
901 |
|
902 int *get_jc (void) const |
|
903 { |
|
904 invalid_type_error (); |
|
905 return 0; |
|
906 } |
|
907 |
|
908 int get_nzmax (void) const |
|
909 { |
|
910 invalid_type_error (); |
|
911 return 0; |
|
912 } |
|
913 |
|
914 void set_ir (int */*ir*/) |
|
915 { |
|
916 invalid_type_error (); |
|
917 } |
|
918 |
|
919 void set_jc (int */*jc*/) |
|
920 { |
|
921 invalid_type_error (); |
|
922 } |
|
923 |
|
924 void set_nzmax (int /*nzmax*/) |
|
925 { |
|
926 invalid_type_error (); |
|
927 } |
|
928 |
|
929 int add_field (const char */*key*/) |
|
930 { |
|
931 invalid_type_error (); |
|
932 return -1; |
|
933 } |
|
934 |
|
935 void remove_field (int /*key_num*/) |
|
936 { |
|
937 invalid_type_error (); |
|
938 } |
|
939 |
|
940 mxArray *get_field_by_number (int /*index*/, int /*key_num*/) const |
|
941 { |
|
942 invalid_type_error (); |
|
943 return 0; |
|
944 } |
|
945 |
|
946 void set_field_by_number (int /*index*/, int /*key_num*/, mxArray */*val*/) |
|
947 { |
|
948 invalid_type_error (); |
|
949 } |
|
950 |
|
951 int get_number_of_fields (void) const |
|
952 { |
|
953 invalid_type_error (); |
|
954 return 0; |
|
955 } |
|
956 |
|
957 const char *get_field_name_by_number (int /*key_num*/) const |
|
958 { |
|
959 invalid_type_error (); |
|
960 return 0; |
|
961 } |
|
962 |
|
963 int get_field_number (const char */*key*/) const |
|
964 { |
|
965 return -1; |
|
966 } |
|
967 |
|
968 int get_string (char */*buf*/, int /*buflen*/) const |
|
969 { |
|
970 invalid_type_error (); |
|
971 return 0; |
|
972 } |
|
973 |
|
974 char *array_to_string (void) const |
|
975 { |
|
976 invalid_type_error (); |
|
977 return 0; |
|
978 } |
|
979 |
|
980 int calc_single_subscript (int nsubs, int *subs) const |
|
981 { |
|
982 int retval = 0; |
|
983 |
|
984 int n = nsubs <= ndims ? nsubs : ndims; |
|
985 |
|
986 while (--n > 0) |
|
987 retval = retval * dims[n] + subs[n]; |
|
988 |
|
989 return retval; |
|
990 } |
|
991 |
|
992 int get_element_size (void) const |
|
993 { |
|
994 switch (id) |
|
995 { |
|
996 case mxCELL_CLASS: return sizeof (mxArray *); |
|
997 case mxSTRUCT_CLASS: return sizeof (mxArray *); |
|
998 case mxLOGICAL_CLASS: return sizeof (mxLogical); |
|
999 case mxCHAR_CLASS: return sizeof (mxChar); |
|
1000 case mxDOUBLE_CLASS: return sizeof (double); |
|
1001 case mxSINGLE_CLASS: return sizeof (float); |
|
1002 case mxINT8_CLASS: return 1; |
|
1003 case mxUINT8_CLASS: return 1; |
|
1004 case mxINT16_CLASS: return 2; |
|
1005 case mxUINT16_CLASS: return 2; |
|
1006 case mxINT32_CLASS: return 4; |
|
1007 case mxUINT32_CLASS: return 4; |
|
1008 case mxINT64_CLASS: return 8; |
|
1009 case mxUINT64_CLASS: return 8; |
|
1010 case mxFUNCTION_CLASS: return 0; |
|
1011 default: return 0; |
|
1012 } |
|
1013 } |
|
1014 |
|
1015 protected: |
|
1016 |
|
1017 mxArray_matlab (const mxArray_matlab& val) |
|
1018 : mxArray_base (val), class_name (strsave (val.class_name)), |
|
1019 id (val.id), ndims (val.ndims), |
|
1020 dims (static_cast<int *> (malloc (ndims * sizeof (int)))) |
|
1021 { |
|
1022 for (int i = 0; i < ndims; i++) |
|
1023 dims[i] = val.dims[i]; |
|
1024 } |
|
1025 |
|
1026 dim_vector |
|
1027 dims_to_dim_vector (void) const |
|
1028 { |
|
1029 int nd = get_number_of_dimensions (); |
|
1030 |
|
1031 int *d = get_dimensions (); |
|
1032 |
|
1033 dim_vector dv; |
|
1034 dv.resize (nd); |
|
1035 |
|
1036 for (int i = 0; i < nd; i++) |
|
1037 dv(i) = d[i]; |
|
1038 |
|
1039 return dv; |
|
1040 } |
|
1041 |
|
1042 private: |
|
1043 |
|
1044 char *class_name; |
|
1045 |
|
1046 mxClassID id; |
|
1047 |
|
1048 int ndims; |
|
1049 int *dims; |
|
1050 |
|
1051 void invalid_type_error (void) const |
|
1052 { |
|
1053 error ("invalid type for operation"); |
|
1054 } |
|
1055 }; |
|
1056 |
|
1057 // Matlab-style numeric, character, and logical data. |
|
1058 |
|
1059 class mxArray_number : public mxArray_matlab |
|
1060 { |
|
1061 public: |
|
1062 |
|
1063 mxArray_number (mxClassID id_arg, int ndims_arg, const int *dims_arg, |
|
1064 mxComplexity flag = mxREAL) |
|
1065 : mxArray_matlab (id_arg, ndims_arg, dims_arg), |
|
1066 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1067 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1068 |
|
1069 mxArray_number (mxClassID id_arg, const dim_vector& dv, |
|
1070 mxComplexity flag = mxREAL) |
|
1071 : mxArray_matlab (id_arg, dv), |
|
1072 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1073 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1074 |
|
1075 mxArray_number (mxClassID id_arg, int m, int n, mxComplexity flag = mxREAL) |
|
1076 : mxArray_matlab (id_arg, m, n), |
|
1077 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1078 pi (flag == mxCOMPLEX ? calloc (get_number_of_elements (), get_element_size ()) : 0) { } |
|
1079 |
|
1080 mxArray_number (mxClassID id_arg, double val) |
|
1081 : mxArray_matlab (id_arg, 1, 1), |
|
1082 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1083 pi (0) |
|
1084 { |
|
1085 double *dpr = static_cast<double *> (pr); |
|
1086 dpr[0] = val; |
|
1087 } |
|
1088 |
|
1089 mxArray_number (mxClassID id_arg, mxLogical val) |
|
1090 : mxArray_matlab (id_arg, 1, 1), |
|
1091 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1092 pi (0) |
|
1093 { |
|
1094 mxLogical *lpr = static_cast<mxLogical *> (pr); |
|
1095 lpr[0] = val; |
|
1096 } |
|
1097 |
|
1098 mxArray_number (const char *str) |
|
1099 : mxArray_matlab (mxCHAR_CLASS, 1, strlen (str)), |
|
1100 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1101 pi (0) |
|
1102 { |
|
1103 mxChar *cpr = static_cast<mxChar *> (pr); |
|
1104 int nel = get_number_of_elements (); |
|
1105 for (int i = 0; i < nel; i++) |
|
1106 cpr[i] = str[i]; |
|
1107 } |
|
1108 |
|
1109 mxArray_number (int m, const char **str) |
|
1110 : mxArray_matlab (mxCHAR_CLASS, m, max_str_len (m, str)), |
|
1111 pr (calloc (get_number_of_elements (), get_element_size ())), |
|
1112 pi (0) |
|
1113 { |
|
1114 mxChar *cpr = static_cast<mxChar *> (pr); |
|
1115 |
|
1116 int *dv = get_dimensions (); |
|
1117 |
|
1118 int nc = dv[1]; |
|
1119 |
|
1120 for (int j = 0; j < m; j++) |
|
1121 { |
|
1122 const char *ptr = str[j]; |
|
1123 |
|
1124 int tmp_len = strlen (ptr); |
|
1125 |
|
1126 for (int i = 0; i < tmp_len; i++) |
|
1127 cpr[i] = static_cast<mxChar> (ptr[i]); |
|
1128 |
|
1129 for (int i = tmp_len; i < nc; i++) |
|
1130 cpr[i] = static_cast<mxChar> (' '); |
|
1131 } |
|
1132 } |
|
1133 |
|
1134 mxArray_number *clone (void) const { return new mxArray_number (*this); } |
|
1135 |
|
1136 ~mxArray_number (void) |
|
1137 { |
|
1138 mxFree (pr); |
|
1139 mxFree (pi); |
|
1140 } |
|
1141 |
5907
|
1142 int is_complex (void) const { return pi != 0; } |
|
1143 |
|
1144 void *get_data (void) const { return pr; } |
|
1145 |
|
1146 void *get_imag_data (void) const { return pi; } |
|
1147 |
|
1148 void set_data (void *pr_arg) { pr = pr_arg; } |
|
1149 |
|
1150 void set_imag_data (void *pi_arg) { pi = pi_arg; } |
|
1151 |
|
1152 int get_string (char *buf, int buflen) const |
|
1153 { |
|
1154 int retval = 1; |
|
1155 |
|
1156 int n = get_number_of_elements (); |
|
1157 |
|
1158 if (n < buflen) |
|
1159 { |
|
1160 mxChar *ptr = static_cast<mxChar *> (pr); |
|
1161 |
|
1162 for (int i = 0; i < n; i++) |
|
1163 buf[i] = static_cast<char> (ptr[i]); |
|
1164 |
|
1165 buf[n] = 0; |
|
1166 } |
|
1167 |
|
1168 return retval; |
|
1169 } |
|
1170 |
|
1171 char *array_to_string (void) const |
|
1172 { |
|
1173 // FIXME -- this is suposed to handle multi-byte character |
|
1174 // strings. |
|
1175 |
|
1176 int nel = get_number_of_elements (); |
|
1177 |
|
1178 char *buf = static_cast<char *> (malloc (nel + 1)); |
|
1179 |
|
1180 if (buf) |
|
1181 { |
|
1182 mxChar *ptr = static_cast<mxChar *> (pr); |
|
1183 |
|
1184 for (int i = 0; i < nel; i++) |
|
1185 buf[i] = static_cast<char> (ptr[i]); |
|
1186 |
|
1187 buf[nel] = '\0'; |
|
1188 } |
|
1189 |
|
1190 return buf; |
|
1191 } |
|
1192 |
|
1193 protected: |
|
1194 |
5900
|
1195 template <typename ELT_T, typename ARRAY_T, typename ARRAY_ELT_T> |
|
1196 octave_value |
|
1197 int_to_ov (const dim_vector& dv) const |
|
1198 { |
|
1199 octave_value retval; |
|
1200 |
|
1201 int nel = get_number_of_elements (); |
|
1202 |
|
1203 ELT_T *ppr = static_cast<ELT_T *> (pr); |
|
1204 |
|
1205 if (pi) |
|
1206 error ("complex integer types are not supported"); |
|
1207 else |
|
1208 { |
|
1209 ARRAY_T val (dv); |
|
1210 |
|
1211 ARRAY_ELT_T *ptr = val.fortran_vec (); |
|
1212 |
|
1213 for (int i = 0; i < nel; i++) |
|
1214 ptr[i] = ppr[i]; |
|
1215 |
|
1216 retval = val; |
|
1217 } |
|
1218 |
|
1219 return retval; |
|
1220 } |
|
1221 |
|
1222 octave_value as_octave_value (void) const |
|
1223 { |
|
1224 octave_value retval; |
|
1225 |
|
1226 dim_vector dv = dims_to_dim_vector (); |
|
1227 |
|
1228 switch (get_class_id ()) |
|
1229 { |
|
1230 case mxLOGICAL_CLASS: |
|
1231 retval = int_to_ov<bool, boolNDArray, bool> (dv); |
|
1232 break; |
|
1233 |
|
1234 case mxCHAR_CLASS: |
|
1235 { |
|
1236 int nel = get_number_of_elements (); |
|
1237 |
|
1238 mxChar *ppr = static_cast<mxChar *> (pr); |
|
1239 |
|
1240 charNDArray val (dv); |
|
1241 |
|
1242 char *ptr = val.fortran_vec (); |
|
1243 |
|
1244 for (int i = 0; i < nel; i++) |
|
1245 ptr[i] = static_cast<char> (ppr[i]); |
|
1246 |
|
1247 retval = octave_value (val, true, '\''); |
|
1248 } |
|
1249 break; |
|
1250 |
|
1251 case mxSINGLE_CLASS: |
|
1252 error ("single precision data type not supported"); |
|
1253 break; |
|
1254 |
|
1255 case mxDOUBLE_CLASS: |
|
1256 { |
|
1257 int nel = get_number_of_elements (); |
|
1258 |
|
1259 double *ppr = static_cast<double *> (pr); |
|
1260 |
|
1261 if (pi) |
|
1262 { |
|
1263 ComplexNDArray val (dv); |
|
1264 |
|
1265 Complex *ptr = val.fortran_vec (); |
|
1266 |
|
1267 double *ppi = static_cast<double *> (pi); |
|
1268 |
|
1269 for (int i = 0; i < nel; i++) |
|
1270 ptr[i] = Complex (ppr[i], ppi[i]); |
|
1271 |
|
1272 retval = val; |
|
1273 } |
|
1274 else |
|
1275 { |
|
1276 NDArray val (dv); |
|
1277 |
|
1278 double *ptr = val.fortran_vec (); |
|
1279 |
|
1280 for (int i = 0; i < nel; i++) |
|
1281 ptr[i] = ppr[i]; |
|
1282 |
|
1283 retval = val; |
|
1284 } |
|
1285 } |
|
1286 break; |
|
1287 |
|
1288 case mxINT8_CLASS: |
|
1289 retval = int_to_ov<int8_t, int8NDArray, octave_int8> (dv); |
|
1290 break; |
|
1291 |
|
1292 case mxUINT8_CLASS: |
|
1293 retval = int_to_ov<uint8_t, uint8NDArray, octave_uint8> (dv); |
|
1294 break; |
|
1295 |
|
1296 case mxINT16_CLASS: |
|
1297 retval = int_to_ov<int16_t, int16NDArray, octave_int16> (dv); |
|
1298 break; |
|
1299 |
|
1300 case mxUINT16_CLASS: |
|
1301 retval = int_to_ov<uint16_t, uint16NDArray, octave_uint16> (dv); |
|
1302 break; |
|
1303 |
|
1304 case mxINT32_CLASS: |
|
1305 retval = int_to_ov<int32_t, int32NDArray, octave_int32> (dv); |
|
1306 break; |
|
1307 |
|
1308 case mxUINT32_CLASS: |
|
1309 retval = int_to_ov<uint32_t, uint32NDArray, octave_uint32> (dv); |
|
1310 break; |
|
1311 |
|
1312 case mxINT64_CLASS: |
|
1313 retval = int_to_ov<int64_t, int64NDArray, octave_int64> (dv); |
|
1314 break; |
|
1315 |
|
1316 case mxUINT64_CLASS: |
|
1317 retval = int_to_ov<uint64_t, uint64NDArray, octave_uint64> (dv); |
|
1318 break; |
|
1319 |
|
1320 default: |
|
1321 panic_impossible (); |
|
1322 } |
|
1323 |
|
1324 return retval; |
|
1325 } |
|
1326 |
|
1327 mxArray_number (const mxArray_number& val) |
|
1328 : mxArray_matlab (val), |
|
1329 pr (malloc (get_number_of_elements () * get_element_size ())), |
|
1330 pi (val.pi ? malloc (get_number_of_elements () * get_element_size ()) : 0) |
|
1331 { |
5907
|
1332 size_t nbytes = get_number_of_elements () * get_element_size (); |
|
1333 |
|
1334 if (pr) |
|
1335 memcpy (pr, val.pr, nbytes); |
5900
|
1336 |
|
1337 if (pi) |
5907
|
1338 memcpy (pi, val.pi, nbytes); |
5900
|
1339 } |
|
1340 |
|
1341 private: |
|
1342 |
|
1343 void *pr; |
|
1344 void *pi; |
|
1345 }; |
|
1346 |
|
1347 // Matlab-style sparse arrays. |
|
1348 |
5903
|
1349 class mxArray_sparse : public mxArray_matlab |
5900
|
1350 { |
|
1351 public: |
|
1352 |
|
1353 mxArray_sparse (mxClassID id_arg, int m, int n, int nzmax_arg, |
|
1354 mxComplexity flag = mxREAL) |
5903
|
1355 : mxArray_matlab (id_arg, m, n), nzmax (nzmax_arg) |
5900
|
1356 { |
5903
|
1357 pr = (calloc (nzmax, get_element_size ())); |
|
1358 pi = (flag == mxCOMPLEX ? calloc (nzmax, get_element_size ()) : 0); |
5900
|
1359 ir = static_cast<int *> (calloc (nzmax, sizeof (int))); |
5903
|
1360 jc = static_cast<int *> (calloc (n + 1, sizeof (int))); |
5900
|
1361 } |
|
1362 |
|
1363 mxArray_sparse *clone (void) const { return new mxArray_sparse (*this); } |
|
1364 |
|
1365 ~mxArray_sparse (void) |
|
1366 { |
5903
|
1367 mxFree (pr); |
|
1368 mxFree (pi); |
5900
|
1369 mxFree (ir); |
|
1370 mxFree (jc); |
|
1371 } |
|
1372 |
5907
|
1373 int is_complex (void) const { return pi != 0; } |
|
1374 |
|
1375 int is_sparse (void) const { return 1; } |
|
1376 |
|
1377 void *get_data (void) const { return pr; } |
|
1378 |
|
1379 void *get_imag_data (void) const { return pi; } |
|
1380 |
|
1381 void set_data (void *pr_arg) { pr = pr_arg; } |
|
1382 |
|
1383 void set_imag_data (void *pi_arg) { pi = pi_arg; } |
|
1384 |
|
1385 int *get_ir (void) const { return ir; } |
|
1386 |
|
1387 int *get_jc (void) const { return jc; } |
|
1388 |
|
1389 int get_nzmax (void) const { return nzmax; } |
|
1390 |
|
1391 void set_ir (int *ir_arg) { ir = ir_arg; } |
|
1392 |
|
1393 void set_jc (int *jc_arg) { jc = jc_arg; } |
|
1394 |
|
1395 void set_nzmax (int nzmax_arg) { nzmax = nzmax_arg; } |
|
1396 |
|
1397 protected: |
|
1398 |
5900
|
1399 octave_value as_octave_value (void) const |
|
1400 { |
5903
|
1401 octave_value retval; |
|
1402 |
|
1403 dim_vector dv = dims_to_dim_vector (); |
|
1404 |
|
1405 switch (get_class_id ()) |
|
1406 { |
|
1407 case mxLOGICAL_CLASS: |
|
1408 { |
|
1409 bool *ppr = static_cast<bool *> (pr); |
|
1410 |
5982
|
1411 SparseBoolMatrix val (get_m (), get_n (), |
|
1412 static_cast<octave_idx_type> (nzmax)); |
5903
|
1413 |
|
1414 for (int i = 0; i < nzmax; i++) |
|
1415 { |
|
1416 val.xdata(i) = ppr[i]; |
|
1417 val.xridx(i) = ir[i]; |
|
1418 } |
|
1419 |
|
1420 for (int i = 0; i < get_n () + 1; i++) |
|
1421 val.xcidx(i) = jc[i]; |
|
1422 |
|
1423 retval = val; |
|
1424 } |
|
1425 break; |
|
1426 |
|
1427 case mxSINGLE_CLASS: |
|
1428 error ("single precision data type not supported"); |
|
1429 break; |
|
1430 |
|
1431 case mxDOUBLE_CLASS: |
|
1432 { |
|
1433 if (pi) |
|
1434 { |
|
1435 double *ppr = static_cast<double *> (pr); |
|
1436 double *ppi = static_cast<double *> (pi); |
|
1437 |
5982
|
1438 SparseComplexMatrix val (get_m (), get_n (), |
|
1439 static_cast<octave_idx_type> (nzmax)); |
5903
|
1440 |
|
1441 for (int i = 0; i < nzmax; i++) |
|
1442 { |
|
1443 val.xdata(i) = Complex (ppr[i], ppi[i]); |
|
1444 val.xridx(i) = ir[i]; |
|
1445 } |
|
1446 |
|
1447 for (int i = 0; i < get_n () + 1; i++) |
|
1448 val.xcidx(i) = jc[i]; |
|
1449 |
|
1450 retval = val; |
|
1451 } |
|
1452 else |
|
1453 { |
|
1454 double *ppr = static_cast<double *> (pr); |
|
1455 |
5982
|
1456 SparseMatrix val (get_m (), get_n (), |
|
1457 static_cast<octave_idx_type> (nzmax)); |
5903
|
1458 |
|
1459 for (int i = 0; i < nzmax; i++) |
|
1460 { |
|
1461 val.xdata(i) = ppr[i]; |
|
1462 val.xridx(i) = ir[i]; |
|
1463 } |
|
1464 |
|
1465 for (int i = 0; i < get_n () + 1; i++) |
|
1466 val.xcidx(i) = jc[i]; |
|
1467 |
|
1468 retval = val; |
|
1469 } |
|
1470 } |
|
1471 break; |
|
1472 |
|
1473 default: |
|
1474 panic_impossible (); |
|
1475 } |
|
1476 |
|
1477 return retval; |
5900
|
1478 } |
|
1479 |
|
1480 private: |
|
1481 |
|
1482 int nzmax; |
|
1483 |
5903
|
1484 void *pr; |
|
1485 void *pi; |
5900
|
1486 int *ir; |
|
1487 int *jc; |
|
1488 |
|
1489 mxArray_sparse (const mxArray_sparse& val) |
5903
|
1490 : mxArray_matlab (val), nzmax (val.nzmax), |
5900
|
1491 ir (static_cast<int *> (malloc (nzmax * sizeof (int)))), |
|
1492 jc (static_cast<int *> (malloc (nzmax * sizeof (int)))) |
|
1493 { |
5907
|
1494 size_t nbytes = nzmax * get_element_size (); |
|
1495 |
|
1496 if (pr) |
|
1497 memcpy (pr, val.pr, nbytes); |
|
1498 |
5903
|
1499 if (pi) |
5907
|
1500 memcpy (pi, val.pi, nbytes); |
|
1501 |
|
1502 if (ir) |
|
1503 memcpy (ir, val.ir, nzmax * sizeof (int)); |
|
1504 |
|
1505 if (jc) |
|
1506 memcpy (jc, val.jc, (val.get_n () + 1) * sizeof (int)); |
5900
|
1507 } |
|
1508 }; |
|
1509 |
|
1510 // Matlab-style struct arrays. |
|
1511 |
|
1512 class mxArray_struct : public mxArray_matlab |
|
1513 { |
|
1514 public: |
|
1515 |
|
1516 mxArray_struct (int ndims_arg, const int *dims_arg, int num_keys_arg, |
|
1517 const char **keys) |
|
1518 : mxArray_matlab (mxSTRUCT_CLASS, ndims_arg, dims_arg), nfields (num_keys_arg), |
|
1519 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1520 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1521 { |
|
1522 init (keys); |
|
1523 } |
|
1524 |
|
1525 mxArray_struct (const dim_vector& dv, int num_keys_arg, const char **keys) |
|
1526 : mxArray_matlab (mxSTRUCT_CLASS, dv), nfields (num_keys_arg), |
|
1527 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1528 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1529 { |
|
1530 init (keys); |
|
1531 } |
|
1532 |
|
1533 mxArray_struct (int m, int n, int num_keys_arg, const char **keys) |
|
1534 : mxArray_matlab (mxSTRUCT_CLASS, m, n), nfields (num_keys_arg), |
|
1535 fields (static_cast<char **> (calloc (nfields, sizeof (char *)))), |
|
1536 data (static_cast<mxArray **> (calloc (nfields * get_number_of_elements (), sizeof (mxArray *)))) |
|
1537 { |
|
1538 init (keys); |
|
1539 } |
|
1540 |
|
1541 void init (const char **keys) |
|
1542 { |
|
1543 for (int i = 0; i < nfields; i++) |
|
1544 fields[i] = strsave (keys[i]); |
|
1545 } |
|
1546 |
|
1547 mxArray_struct *clone (void) const { return new mxArray_struct (*this); } |
|
1548 |
|
1549 ~mxArray_struct (void) |
|
1550 { |
|
1551 for (int i = 0; i < nfields; i++) |
|
1552 mxFree (fields[i]); |
|
1553 |
|
1554 mxFree (fields); |
|
1555 |
|
1556 int ntot = nfields * get_number_of_elements (); |
|
1557 |
|
1558 for (int i = 0; i < ntot; i++) |
5905
|
1559 delete data[i]; |
5900
|
1560 |
|
1561 mxFree (data); |
|
1562 } |
|
1563 |
|
1564 int add_field (const char *key) |
|
1565 { |
|
1566 int retval = -1; |
|
1567 |
|
1568 if (valid_key (key)) |
|
1569 { |
|
1570 nfields++; |
|
1571 |
|
1572 fields = static_cast<char **> (mxRealloc (fields, nfields * sizeof (char *))); |
|
1573 |
|
1574 if (fields) |
|
1575 { |
|
1576 fields[nfields-1] = strsave (key); |
|
1577 |
|
1578 int nel = get_number_of_elements (); |
|
1579 |
|
1580 int ntot = nfields * nel; |
|
1581 |
|
1582 mxArray **new_data = static_cast<mxArray **> (malloc (ntot * sizeof (mxArray *))); |
|
1583 |
|
1584 if (new_data) |
|
1585 { |
|
1586 int j = 0; |
|
1587 int k = 0; |
|
1588 int n = 0; |
|
1589 |
|
1590 for (int i = 0; i < ntot; i++) |
|
1591 { |
|
1592 if (++n == nfields) |
|
1593 { |
|
1594 new_data[j++] = 0; |
|
1595 n = 0; |
|
1596 } |
|
1597 else |
|
1598 new_data[j++] = data[k++]; |
|
1599 } |
|
1600 |
|
1601 mxFree (data); |
|
1602 |
|
1603 data = new_data; |
|
1604 |
|
1605 retval = nfields - 1; |
|
1606 } |
|
1607 } |
|
1608 } |
|
1609 |
|
1610 return retval; |
|
1611 } |
|
1612 |
|
1613 void remove_field (int key_num) |
|
1614 { |
|
1615 if (key_num >= 0 && key_num < nfields) |
|
1616 { |
|
1617 int nel = get_number_of_elements (); |
|
1618 |
|
1619 int ntot = nfields * nel; |
|
1620 |
|
1621 int new_nfields = nfields - 1; |
|
1622 |
|
1623 char **new_fields = static_cast<char **> (malloc (new_nfields * sizeof (char *))); |
|
1624 |
|
1625 mxArray **new_data = static_cast<mxArray **> (malloc (new_nfields * nel * sizeof (mxArray *))); |
|
1626 |
|
1627 for (int i = 0; i < key_num; i++) |
|
1628 new_fields[i] = fields[i]; |
|
1629 |
|
1630 for (int i = key_num + 1; i < nfields; i++) |
|
1631 new_fields[i-1] = fields[i]; |
|
1632 |
|
1633 if (new_nfields > 0) |
|
1634 { |
|
1635 int j = 0; |
|
1636 int k = 0; |
|
1637 int n = 0; |
|
1638 |
|
1639 for (int i = 0; i < ntot; i++) |
|
1640 { |
|
1641 if (n == key_num) |
|
1642 k++; |
|
1643 else |
|
1644 new_data[j++] = data[k++]; |
|
1645 |
|
1646 if (++n == nfields) |
|
1647 n = 0; |
|
1648 } |
|
1649 } |
|
1650 |
|
1651 nfields = new_nfields; |
|
1652 |
|
1653 mxFree (fields); |
|
1654 mxFree (data); |
|
1655 |
|
1656 fields = new_fields; |
|
1657 data = new_data; |
|
1658 } |
|
1659 } |
|
1660 |
|
1661 mxArray *get_field_by_number (int index, int key_num) const |
|
1662 { |
6187
|
1663 return key_num >= 0 && key_num < nfields |
6188
|
1664 ? data[nfields * index + key_num] : 0; |
5900
|
1665 } |
|
1666 |
6071
|
1667 void set_field_by_number (int index, int key_num, mxArray *val); |
5900
|
1668 |
|
1669 int get_number_of_fields (void) const { return nfields; } |
|
1670 |
|
1671 const char *get_field_name_by_number (int key_num) const |
|
1672 { |
|
1673 return key_num >= 0 && key_num < nfields ? fields[key_num] : 0; |
|
1674 } |
|
1675 |
|
1676 int get_field_number (const char *key) const |
|
1677 { |
|
1678 int retval = -1; |
|
1679 |
|
1680 for (int i = 0; i < nfields; i++) |
|
1681 { |
|
1682 if (! strcmp (key, fields[i])) |
|
1683 { |
|
1684 retval = i; |
|
1685 break; |
|
1686 } |
|
1687 } |
|
1688 |
|
1689 return retval; |
|
1690 } |
|
1691 |
|
1692 void *get_data (void) const { return data; } |
|
1693 |
|
1694 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1695 |
5907
|
1696 protected: |
|
1697 |
|
1698 octave_value as_octave_value (void) const |
|
1699 { |
|
1700 dim_vector dv = dims_to_dim_vector (); |
|
1701 |
|
1702 string_vector keys (fields, nfields); |
|
1703 |
|
1704 Octave_map m; |
|
1705 |
|
1706 int ntot = nfields * get_number_of_elements (); |
|
1707 |
|
1708 for (int i = 0; i < nfields; i++) |
|
1709 { |
|
1710 Cell c (dv); |
|
1711 |
|
1712 octave_value *p = c.fortran_vec (); |
|
1713 |
|
1714 int k = 0; |
|
1715 for (int j = i; j < ntot; j += nfields) |
|
1716 p[k++] = mxArray::as_octave_value (data[j]); |
|
1717 |
|
1718 m.assign (keys[i], c); |
|
1719 } |
|
1720 |
|
1721 return m; |
|
1722 } |
|
1723 |
5900
|
1724 private: |
|
1725 |
|
1726 int nfields; |
|
1727 |
|
1728 char **fields; |
|
1729 |
|
1730 mxArray **data; |
|
1731 |
|
1732 mxArray_struct (const mxArray_struct& val) |
|
1733 : mxArray_matlab (val), nfields (val.nfields), |
|
1734 fields (static_cast<char **> (malloc (nfields * sizeof (char *)))), |
|
1735 data (static_cast<mxArray **> (malloc (nfields * get_number_of_elements () * sizeof (mxArray *)))) |
|
1736 { |
|
1737 for (int i = 0; i < nfields; i++) |
|
1738 fields[i] = strsave (val.fields[i]); |
|
1739 |
|
1740 int nel = get_number_of_elements (); |
|
1741 |
|
1742 for (int i = 0; i < nel * nfields; i++) |
|
1743 data[i] = val.data[i]->clone (); |
|
1744 } |
|
1745 }; |
|
1746 |
|
1747 // Matlab-style cell arrays. |
|
1748 |
|
1749 class mxArray_cell : public mxArray_matlab |
|
1750 { |
|
1751 public: |
|
1752 |
|
1753 mxArray_cell (int ndims_arg, const int *dims_arg) |
|
1754 : mxArray_matlab (mxCELL_CLASS, ndims_arg, dims_arg), |
|
1755 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1756 |
|
1757 mxArray_cell (const dim_vector& dv) |
|
1758 : mxArray_matlab (mxCELL_CLASS, dv), |
|
1759 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1760 |
|
1761 mxArray_cell (int m, int n) |
|
1762 : mxArray_matlab (mxCELL_CLASS, m, n), |
|
1763 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1764 |
|
1765 mxArray_cell *clone (void) const { return new mxArray_cell (*this); } |
|
1766 |
|
1767 ~mxArray_cell (void) |
|
1768 { |
|
1769 int nel = get_number_of_elements (); |
|
1770 |
|
1771 for (int i = 0; i < nel; i++) |
5905
|
1772 delete data[i]; |
5900
|
1773 |
|
1774 mxFree (data); |
|
1775 } |
|
1776 |
6187
|
1777 mxArray *get_cell (int idx) const |
|
1778 { |
|
1779 return idx >= 0 && idx < get_number_of_elements () ? data[idx] : 0; |
|
1780 } |
5907
|
1781 |
6071
|
1782 void set_cell (int idx, mxArray *val); |
5907
|
1783 |
|
1784 void *get_data (void) const { return data; } |
|
1785 |
|
1786 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1787 |
|
1788 protected: |
|
1789 |
5900
|
1790 octave_value as_octave_value (void) const |
|
1791 { |
|
1792 dim_vector dv = dims_to_dim_vector (); |
|
1793 |
|
1794 Cell c (dv); |
|
1795 |
|
1796 int nel = get_number_of_elements (); |
|
1797 |
|
1798 octave_value *p = c.fortran_vec (); |
|
1799 |
|
1800 for (int i = 0; i < nel; i++) |
5907
|
1801 p[i] = mxArray::as_octave_value (data[i]); |
5900
|
1802 |
|
1803 return c; |
|
1804 } |
|
1805 |
|
1806 private: |
|
1807 |
|
1808 mxArray **data; |
|
1809 |
|
1810 mxArray_cell (const mxArray_cell& val) |
|
1811 : mxArray_matlab (val), |
|
1812 data (static_cast<mxArray **> (malloc (get_number_of_elements () * sizeof (mxArray *)))) |
|
1813 { |
|
1814 int nel = get_number_of_elements (); |
|
1815 |
|
1816 for (int i = 0; i < nel; i++) |
|
1817 data[i] = val.data[i]->clone (); |
|
1818 } |
|
1819 }; |
|
1820 |
|
1821 // ------------------------------------------------------------------ |
|
1822 |
|
1823 mxArray::mxArray (const octave_value& ov) |
6065
|
1824 : rep (new mxArray_octave_value (ov)), name (0) { } |
5900
|
1825 |
|
1826 mxArray::mxArray (mxClassID id, int ndims, const int *dims, mxComplexity flag) |
6065
|
1827 : rep (new mxArray_number (id, ndims, dims, flag)), name (0) { } |
5900
|
1828 |
|
1829 mxArray::mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag) |
6065
|
1830 : rep (new mxArray_number (id, dv, flag)), name (0) { } |
5900
|
1831 |
|
1832 mxArray::mxArray (mxClassID id, int m, int n, mxComplexity flag) |
6065
|
1833 : rep (new mxArray_number (id, m, n, flag)), name (0) { } |
5900
|
1834 |
|
1835 mxArray::mxArray (mxClassID id, double val) |
6065
|
1836 : rep (new mxArray_number (id, val)), name (0) { } |
5900
|
1837 |
|
1838 mxArray::mxArray (mxClassID id, mxLogical val) |
6065
|
1839 : rep (new mxArray_number (id, val)), name (0) { } |
5900
|
1840 |
|
1841 mxArray::mxArray (const char *str) |
6065
|
1842 : rep (new mxArray_number (str)), name (0) { } |
5900
|
1843 |
|
1844 mxArray::mxArray (int m, const char **str) |
6065
|
1845 : rep (new mxArray_number (m, str)), name (0) { } |
5900
|
1846 |
|
1847 mxArray::mxArray (mxClassID id, int m, int n, int nzmax, mxComplexity flag) |
6065
|
1848 : rep (new mxArray_sparse (id, m, n, nzmax, flag)), name (0) { } |
5900
|
1849 |
|
1850 mxArray::mxArray (int ndims, const int *dims, int num_keys, const char **keys) |
6065
|
1851 : rep (new mxArray_struct (ndims, dims, num_keys, keys)), name (0) { } |
5900
|
1852 |
|
1853 mxArray::mxArray (const dim_vector& dv, int num_keys, const char **keys) |
6065
|
1854 : rep (new mxArray_struct (dv, num_keys, keys)), name (0) { } |
5900
|
1855 |
|
1856 mxArray::mxArray (int m, int n, int num_keys, const char **keys) |
6065
|
1857 : rep (new mxArray_struct (m, n, num_keys, keys)), name (0) { } |
5900
|
1858 |
|
1859 mxArray::mxArray (int ndims, const int *dims) |
6065
|
1860 : rep (new mxArray_cell (ndims, dims)), name (0) { } |
5900
|
1861 |
|
1862 mxArray::mxArray (const dim_vector& dv) |
6065
|
1863 : rep (new mxArray_cell (dv)), name (0) { } |
5900
|
1864 |
|
1865 mxArray::mxArray (int m, int n) |
6065
|
1866 : rep (new mxArray_cell (m, n)), name (0) { } |
5900
|
1867 |
|
1868 mxArray::~mxArray (void) |
|
1869 { |
|
1870 mxFree (name); |
|
1871 |
|
1872 delete rep; |
|
1873 } |
|
1874 |
|
1875 void |
|
1876 mxArray::set_name (const char *name_arg) |
|
1877 { |
|
1878 mxFree (name); |
|
1879 name = strsave (name_arg); |
|
1880 } |
|
1881 |
5907
|
1882 octave_value |
|
1883 mxArray::as_octave_value (mxArray *ptr) |
|
1884 { |
|
1885 return ptr ? ptr->as_octave_value () : octave_value (Matrix ()); |
|
1886 } |
|
1887 |
|
1888 octave_value |
|
1889 mxArray::as_octave_value (void) const |
|
1890 { |
|
1891 return rep->as_octave_value (); |
|
1892 } |
|
1893 |
5900
|
1894 void |
|
1895 mxArray::maybe_mutate (void) const |
|
1896 { |
|
1897 if (rep->is_octave_value ()) |
|
1898 { |
|
1899 // The mutate function returns a pointer to a complete new |
|
1900 // mxArray object (or 0, if no mutation happened). We just want |
|
1901 // to replace the existing rep with the rep from the new object. |
|
1902 |
|
1903 mxArray *new_val = rep->mutate (); |
|
1904 |
|
1905 if (new_val) |
|
1906 { |
|
1907 delete rep; |
|
1908 rep = new_val->rep; |
|
1909 new_val->rep = 0; |
|
1910 delete new_val; |
|
1911 } |
|
1912 } |
|
1913 } |
|
1914 |
|
1915 // ------------------------------------------------------------------ |
|
1916 |
|
1917 // A clas to manage calls to MEX functions. Mostly deals with memory |
|
1918 // management. |
5864
|
1919 |
|
1920 class mex |
|
1921 { |
|
1922 public: |
|
1923 |
6068
|
1924 mex (octave_mex_function *f) |
|
1925 : curr_mex_fcn (f), memlist (), arraylist (), fname (0) { } |
5864
|
1926 |
|
1927 ~mex (void) |
|
1928 { |
|
1929 if (! memlist.empty ()) |
5905
|
1930 error ("mex: %s: cleanup failed", function_name ()); |
5900
|
1931 |
|
1932 mxFree (fname); |
5864
|
1933 } |
|
1934 |
5900
|
1935 const char *function_name (void) const |
|
1936 { |
|
1937 if (! fname) |
|
1938 { |
|
1939 octave_function *fcn = octave_call_stack::current (); |
|
1940 |
|
1941 if (fcn) |
|
1942 { |
|
1943 std::string nm = fcn->name (); |
6065
|
1944 fname = mxArray::strsave (nm.c_str ()); |
5900
|
1945 } |
|
1946 else |
6065
|
1947 fname = mxArray::strsave ("unknown"); |
5900
|
1948 } |
|
1949 |
|
1950 return fname; |
|
1951 } |
|
1952 |
|
1953 // Free all unmarked pointers obtained from malloc and calloc. |
|
1954 static void cleanup (void *ptr) |
|
1955 { |
|
1956 mex *context = static_cast<mex *> (ptr); |
|
1957 |
5905
|
1958 // We can't use mex::free here because it modifies memlist. |
5900
|
1959 for (std::set<void *>::iterator p = context->memlist.begin (); |
|
1960 p != context->memlist.end (); p++) |
5905
|
1961 { |
|
1962 if (*p) |
|
1963 { |
|
1964 context->unmark (*p); |
|
1965 |
|
1966 xfree (*p); |
|
1967 } |
|
1968 } |
|
1969 |
|
1970 context->memlist.clear (); |
|
1971 |
|
1972 // We can't use mex::free_value here because it modifies arraylist. |
5900
|
1973 for (std::set<mxArray *>::iterator p = context->arraylist.begin (); |
|
1974 p != context->arraylist.end (); p++) |
5905
|
1975 delete *p; |
|
1976 |
|
1977 context->arraylist.clear (); |
5900
|
1978 } |
5864
|
1979 |
6071
|
1980 // Allocate memory. |
5900
|
1981 void *malloc_unmarked (size_t n) |
|
1982 { |
|
1983 void *ptr = ::malloc (n); |
|
1984 |
|
1985 if (! ptr) |
|
1986 { |
|
1987 // FIXME -- could use "octave_new_handler();" instead |
|
1988 |
|
1989 error ("%s: failed to allocate %d bytes of memory", |
5905
|
1990 function_name (), n); |
5900
|
1991 |
|
1992 abort (); |
|
1993 } |
|
1994 |
|
1995 global_mark (ptr); |
|
1996 |
|
1997 return ptr; |
|
1998 } |
|
1999 |
6071
|
2000 // Allocate memory to be freed on exit. |
5900
|
2001 void *malloc (size_t n) |
|
2002 { |
|
2003 void *ptr = malloc_unmarked (n); |
|
2004 |
|
2005 mark (ptr); |
|
2006 |
|
2007 return ptr; |
|
2008 } |
|
2009 |
6071
|
2010 // Allocate memory and initialize to 0. |
5900
|
2011 void *calloc_unmarked (size_t n, size_t t) |
|
2012 { |
|
2013 void *ptr = malloc_unmarked (n*t); |
|
2014 |
|
2015 memset (ptr, 0, n*t); |
|
2016 |
|
2017 return ptr; |
|
2018 } |
|
2019 |
6071
|
2020 // Allocate memory to be freed on exit and initialize to 0. |
5900
|
2021 void *calloc (size_t n, size_t t) |
|
2022 { |
|
2023 void *ptr = calloc_unmarked (n, t); |
|
2024 |
|
2025 mark (ptr); |
|
2026 |
|
2027 return ptr; |
|
2028 } |
|
2029 |
|
2030 // Reallocate a pointer obtained from malloc or calloc. We don't |
|
2031 // need an "unmarked" version of this. |
|
2032 void *realloc (void *ptr, size_t n) |
|
2033 { |
|
2034 void *v = ::realloc (ptr, n); |
|
2035 |
|
2036 std::set<void *>::iterator p = memlist.find (ptr); |
|
2037 |
|
2038 if (v && p != memlist.end ()) |
|
2039 { |
|
2040 memlist.erase (p); |
|
2041 memlist.insert (v); |
|
2042 } |
|
2043 |
|
2044 p = global_memlist.find (ptr); |
|
2045 |
|
2046 if (v && p != global_memlist.end ()) |
|
2047 { |
|
2048 global_memlist.erase (p); |
|
2049 global_memlist.insert (v); |
|
2050 } |
|
2051 |
|
2052 return v; |
|
2053 } |
|
2054 |
|
2055 // Free a pointer obtained from malloc or calloc. |
|
2056 void free (void *ptr) |
|
2057 { |
|
2058 if (ptr) |
|
2059 { |
|
2060 unmark (ptr); |
|
2061 |
|
2062 std::set<void *>::iterator p = global_memlist.find (ptr); |
|
2063 |
|
2064 if (p != global_memlist.end ()) |
|
2065 { |
|
2066 global_memlist.erase (p); |
|
2067 |
5905
|
2068 xfree (ptr); |
5900
|
2069 } |
|
2070 else |
|
2071 warning ("mxFree: skipping memory not allocated by mxMalloc, mxCalloc, or mxRealloc"); |
|
2072 } |
|
2073 } |
|
2074 |
|
2075 // Mark a pointer so that it will not be freed on exit. |
|
2076 void persistent (void *ptr) { unmark (ptr); } |
|
2077 |
6065
|
2078 mxArray *mark_array (mxArray *ptr) |
|
2079 { |
|
2080 arraylist.insert (ptr); |
|
2081 return ptr; |
|
2082 } |
|
2083 |
6071
|
2084 void unmark_array (mxArray *ptr) |
|
2085 { |
|
2086 std::set<mxArray *>::iterator p = arraylist.find (ptr); |
|
2087 |
|
2088 if (p != arraylist.end ()) |
|
2089 arraylist.erase (p); |
|
2090 } |
|
2091 |
5900
|
2092 // Make a new array value and initialize from an octave value; it will be |
|
2093 // freed on exit unless marked as persistent. |
|
2094 mxArray *make_value (const octave_value& ov) |
|
2095 { |
6065
|
2096 return mark_array (new mxArray (ov)); |
5900
|
2097 } |
|
2098 |
|
2099 // Free an array and its contents. |
6065
|
2100 bool free_value (mxArray *ptr) |
5900
|
2101 { |
6065
|
2102 bool inlist = false; |
|
2103 |
5905
|
2104 std::set<mxArray *>::iterator p = arraylist.find (ptr); |
|
2105 |
|
2106 if (p != arraylist.end ()) |
|
2107 { |
6065
|
2108 inlist = true; |
5905
|
2109 arraylist.erase (p); |
|
2110 delete ptr; |
|
2111 } |
|
2112 #ifdef DEBUG |
|
2113 else |
|
2114 warning ("mex::free_value: skipping memory not allocated by mex::make_value"); |
|
2115 #endif |
6065
|
2116 |
|
2117 return inlist; |
5900
|
2118 } |
|
2119 |
6065
|
2120 // Remove PTR from the list of arrays to be free on exit. |
6071
|
2121 void persistent (mxArray *ptr) { unmark_array (ptr); } |
5900
|
2122 |
6068
|
2123 octave_mex_function *current_mex_function (void) const |
|
2124 { |
|
2125 return curr_mex_fcn; |
|
2126 } |
|
2127 |
5900
|
2128 // 1 if error should be returned to MEX file, 0 if abort. |
5864
|
2129 int trap_feval_error; |
|
2130 |
5900
|
2131 // longjmp return point if mexErrMsgTxt or error. |
5864
|
2132 jmp_buf jump; |
|
2133 |
5900
|
2134 // Trigger a long jump back to the mex calling function. |
5864
|
2135 void abort (void) { longjmp (jump, 1); } |
|
2136 |
|
2137 private: |
|
2138 |
6068
|
2139 // Pointer to the mex function that corresponds to this mex context. |
|
2140 octave_mex_function *curr_mex_fcn; |
|
2141 |
5900
|
2142 // List of memory resources that need to be freed upon exit. |
|
2143 std::set<void *> memlist; |
|
2144 |
|
2145 std::set<mxArray *> arraylist; |
|
2146 |
|
2147 // The name of the currently executing function. |
|
2148 mutable char *fname; |
|
2149 |
|
2150 // Mark a pointer to be freed on exit. |
6071
|
2151 void mark (void *ptr) |
5900
|
2152 { |
5864
|
2153 #ifdef DEBUG |
6071
|
2154 if (memlist.find (ptr) != memlist.end ()) |
5905
|
2155 warning ("%s: double registration ignored", function_name ()); |
5864
|
2156 #endif |
|
2157 |
6071
|
2158 memlist.insert (ptr); |
5900
|
2159 } |
|
2160 |
|
2161 // Unmark a pointer to be freed on exit, either because it was |
|
2162 // made persistent, or because it was already freed. |
5905
|
2163 void unmark (void *ptr) |
5900
|
2164 { |
5905
|
2165 std::set<void *>::iterator p = memlist.find (ptr); |
|
2166 |
|
2167 if (p != memlist.end ()) |
|
2168 memlist.erase (p); |
5864
|
2169 #ifdef DEBUG |
5905
|
2170 else |
|
2171 warning ("%s: value not marked", function_name ()); |
5864
|
2172 #endif |
5900
|
2173 } |
|
2174 |
|
2175 // List of memory resources we allocated. |
|
2176 static std::set<void *> global_memlist; |
|
2177 |
|
2178 // Mark a pointer as one we allocated. |
5905
|
2179 void global_mark (void *ptr) |
5900
|
2180 { |
|
2181 #ifdef DEBUG |
5905
|
2182 if (global_memlist.find (ptr) != global_memlist.end ()) |
|
2183 warning ("%s: double registration ignored", function_name ()); |
5864
|
2184 #endif |
5900
|
2185 |
5905
|
2186 global_memlist.insert (ptr); |
5864
|
2187 } |
|
2188 |
5900
|
2189 // Unmark a pointer as one we allocated. |
5905
|
2190 void global_unmark (void *ptr) |
5864
|
2191 { |
5905
|
2192 std::set<void *>::iterator p = global_memlist.find (ptr); |
|
2193 |
|
2194 if (p != global_memlist.end ()) |
|
2195 global_memlist.erase (p); |
5900
|
2196 #ifdef DEBUG |
5905
|
2197 else |
|
2198 warning ("%s: value not marked", function_name ()); |
5900
|
2199 #endif |
|
2200 |
5864
|
2201 } |
|
2202 }; |
|
2203 |
5900
|
2204 // List of memory resources we allocated. |
|
2205 std::set<void *> mex::global_memlist; |
|
2206 |
|
2207 // Current context. |
|
2208 mex *mex_context = 0; |
|
2209 |
|
2210 void * |
|
2211 mxArray::malloc (size_t n) |
|
2212 { |
6065
|
2213 return mex_context ? mex_context->malloc_unmarked (n) : ::malloc (n); |
5900
|
2214 } |
|
2215 |
|
2216 void * |
|
2217 mxArray::calloc (size_t n, size_t t) |
|
2218 { |
6065
|
2219 return mex_context ? mex_context->calloc_unmarked (n, t) : ::calloc (n, t); |
5900
|
2220 } |
|
2221 |
6071
|
2222 static inline mxArray * |
|
2223 maybe_unmark_array (mxArray *ptr) |
|
2224 { |
|
2225 if (mex_context) |
|
2226 mex_context->unmark_array (ptr); |
|
2227 |
|
2228 return ptr; |
|
2229 } |
|
2230 |
|
2231 void |
|
2232 mxArray_struct::set_field_by_number (int index, int key_num, mxArray *val) |
|
2233 { |
6187
|
2234 if (key_num >= 0 && key_num < nfields) |
|
2235 data[nfields * index + key_num] = maybe_unmark_array (val); |
6071
|
2236 } |
|
2237 |
|
2238 void |
|
2239 mxArray_cell::set_cell (int idx, mxArray *val) |
|
2240 { |
6187
|
2241 if (idx >= 0 && idx < get_number_of_elements ()) |
|
2242 data[idx] = maybe_unmark_array (val); |
6071
|
2243 } |
|
2244 |
5900
|
2245 // ------------------------------------------------------------------ |
|
2246 |
|
2247 // C interface to mxArray objects: |
|
2248 |
|
2249 // Floating point predicates. |
|
2250 |
|
2251 int |
|
2252 mxIsFinite (const double v) |
|
2253 { |
|
2254 return lo_ieee_finite (v) != 0; |
|
2255 } |
|
2256 |
|
2257 int |
|
2258 mxIsInf (const double v) |
|
2259 { |
|
2260 return lo_ieee_isinf (v) != 0; |
|
2261 } |
|
2262 |
|
2263 int |
|
2264 mxIsNaN (const double v) |
|
2265 { |
|
2266 return lo_ieee_isnan (v) != 0; |
|
2267 } |
|
2268 |
|
2269 double |
|
2270 mxGetEps (void) |
|
2271 { |
|
2272 return DBL_EPSILON; |
|
2273 } |
|
2274 |
|
2275 double |
|
2276 mxGetInf (void) |
|
2277 { |
|
2278 return lo_ieee_inf_value (); |
|
2279 } |
|
2280 |
|
2281 double |
|
2282 mxGetNaN (void) |
|
2283 { |
|
2284 return lo_ieee_nan_value (); |
|
2285 } |
|
2286 |
|
2287 // Memory management. |
|
2288 void * |
|
2289 mxCalloc (size_t n, size_t size) |
|
2290 { |
|
2291 return mex_context ? mex_context->calloc (n, size) : calloc (n, size); |
|
2292 } |
|
2293 |
|
2294 void * |
|
2295 mxMalloc (size_t n) |
|
2296 { |
|
2297 return mex_context ? mex_context->malloc (n) : malloc (n); |
|
2298 } |
|
2299 |
|
2300 void * |
|
2301 mxRealloc (void *ptr, size_t size) |
|
2302 { |
|
2303 return mex_context ? mex_context->realloc (ptr, size) : realloc (ptr, size); |
|
2304 } |
|
2305 |
|
2306 void |
|
2307 mxFree (void *ptr) |
5864
|
2308 { |
5900
|
2309 if (mex_context) |
|
2310 mex_context->free (ptr); |
5864
|
2311 else |
6071
|
2312 xfree (ptr); |
5900
|
2313 } |
6065
|
2314 |
|
2315 static inline mxArray * |
|
2316 maybe_mark_array (mxArray *ptr) |
|
2317 { |
|
2318 return mex_context ? mex_context->mark_array (ptr) : ptr; |
|
2319 } |
5900
|
2320 |
|
2321 // Constructors. |
|
2322 mxArray * |
|
2323 mxCreateCellArray (int ndims, const int *dims) |
|
2324 { |
6065
|
2325 return maybe_mark_array (new mxArray (ndims, dims)); |
5900
|
2326 } |
|
2327 |
|
2328 mxArray * |
|
2329 mxCreateCellMatrix (int m, int n) |
|
2330 { |
6065
|
2331 return maybe_mark_array (new mxArray (m, n)); |
5900
|
2332 } |
|
2333 |
|
2334 mxArray * |
|
2335 mxCreateCharArray (int ndims, const int *dims) |
|
2336 { |
6065
|
2337 return maybe_mark_array (new mxArray (mxCHAR_CLASS, ndims, dims)); |
5864
|
2338 } |
|
2339 |
5900
|
2340 mxArray * |
|
2341 mxCreateCharMatrixFromStrings (int m, const char **str) |
|
2342 { |
6065
|
2343 return maybe_mark_array (new mxArray (m, str)); |
5900
|
2344 } |
|
2345 |
|
2346 mxArray * |
|
2347 mxCreateDoubleMatrix (int m, int n, mxComplexity flag) |
|
2348 { |
6065
|
2349 return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, m, n, flag)); |
5900
|
2350 } |
|
2351 |
|
2352 mxArray * |
|
2353 mxCreateDoubleScalar (double val) |
|
2354 { |
6065
|
2355 return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, val)); |
5900
|
2356 } |
|
2357 |
|
2358 mxArray * |
|
2359 mxCreateLogicalArray (int ndims, const int *dims) |
5864
|
2360 { |
6065
|
2361 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, ndims, dims)); |
5900
|
2362 } |
|
2363 |
|
2364 mxArray * |
|
2365 mxCreateLogicalMatrix (int m, int n) |
|
2366 { |
6065
|
2367 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, m, n)); |
5900
|
2368 } |
|
2369 |
|
2370 mxArray * |
|
2371 mxCreateLogicalScalar (int val) |
|
2372 { |
6065
|
2373 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, val)); |
5900
|
2374 } |
|
2375 |
|
2376 mxArray * |
|
2377 mxCreateNumericArray (int ndims, const int *dims, mxClassID class_id, |
|
2378 mxComplexity flag) |
|
2379 { |
6065
|
2380 return maybe_mark_array (new mxArray (class_id, ndims, dims, flag)); |
5864
|
2381 } |
|
2382 |
5900
|
2383 mxArray * |
|
2384 mxCreateNumericMatrix (int m, int n, mxClassID class_id, mxComplexity flag) |
|
2385 { |
6065
|
2386 return maybe_mark_array (new mxArray (class_id, m, n, flag)); |
5900
|
2387 } |
|
2388 |
|
2389 mxArray * |
|
2390 mxCreateSparse (int m, int n, int nzmax, mxComplexity flag) |
|
2391 { |
6065
|
2392 return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, m, n, nzmax, flag)); |
5900
|
2393 } |
|
2394 |
|
2395 mxArray * |
|
2396 mxCreateSparseLogicalMatrix (int m, int n, int nzmax) |
|
2397 { |
6065
|
2398 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, m, n, nzmax)); |
5900
|
2399 } |
|
2400 |
|
2401 mxArray * |
|
2402 mxCreateString (const char *str) |
|
2403 { |
6065
|
2404 return maybe_mark_array (new mxArray (str)); |
5900
|
2405 } |
|
2406 |
|
2407 mxArray * |
|
2408 mxCreateStructArray (int ndims, int *dims, int num_keys, const char **keys) |
|
2409 { |
6065
|
2410 return maybe_mark_array (new mxArray (ndims, dims, num_keys, keys)); |
5900
|
2411 } |
5864
|
2412 |
|
2413 mxArray * |
5900
|
2414 mxCreateStructMatrix (int m, int n, int num_keys, const char **keys) |
|
2415 { |
6065
|
2416 return maybe_mark_array (new mxArray (m, n, num_keys, keys)); |
5900
|
2417 } |
|
2418 |
|
2419 // Copy constructor. |
|
2420 mxArray * |
|
2421 mxDuplicateArray (const mxArray *ptr) |
|
2422 { |
6065
|
2423 return maybe_mark_array (ptr->clone ()); |
5900
|
2424 } |
|
2425 |
|
2426 // Destructor. |
|
2427 void |
|
2428 mxDestroyArray (mxArray *ptr) |
|
2429 { |
6065
|
2430 if (! (mex_context && mex_context->free_value (ptr))) |
|
2431 delete ptr; |
5900
|
2432 } |
|
2433 |
|
2434 // Type Predicates. |
|
2435 int |
|
2436 mxIsCell (const mxArray *ptr) |
|
2437 { |
|
2438 return ptr->is_cell (); |
|
2439 } |
|
2440 |
|
2441 int |
|
2442 mxIsChar (const mxArray *ptr) |
|
2443 { |
|
2444 return ptr->is_char (); |
|
2445 } |
|
2446 |
|
2447 int |
|
2448 mxIsClass (const mxArray *ptr, const char *name) |
|
2449 { |
|
2450 return ptr->is_class (name); |
|
2451 } |
|
2452 |
|
2453 int |
|
2454 mxIsComplex (const mxArray *ptr) |
|
2455 { |
|
2456 return ptr->is_complex (); |
|
2457 } |
|
2458 |
|
2459 int |
|
2460 mxIsDouble (const mxArray *ptr) |
|
2461 { |
|
2462 return ptr->is_double (); |
|
2463 } |
|
2464 |
|
2465 int |
|
2466 mxIsInt16 (const mxArray *ptr) |
|
2467 { |
|
2468 return ptr->is_int16 (); |
|
2469 } |
|
2470 |
|
2471 int |
|
2472 mxIsInt32 (const mxArray *ptr) |
|
2473 { |
|
2474 return ptr->is_int32 (); |
|
2475 } |
|
2476 |
|
2477 int |
|
2478 mxIsInt64 (const mxArray *ptr) |
|
2479 { |
|
2480 return ptr->is_int64 (); |
|
2481 } |
|
2482 |
|
2483 int |
|
2484 mxIsInt8 (const mxArray *ptr) |
|
2485 { |
|
2486 return ptr->is_int8 (); |
|
2487 } |
|
2488 |
|
2489 int |
|
2490 mxIsLogical (const mxArray *ptr) |
|
2491 { |
|
2492 return ptr->is_logical (); |
|
2493 } |
|
2494 |
|
2495 int |
|
2496 mxIsNumeric (const mxArray *ptr) |
|
2497 { |
|
2498 return ptr->is_numeric (); |
|
2499 } |
|
2500 |
|
2501 int |
|
2502 mxIsSingle (const mxArray *ptr) |
|
2503 { |
|
2504 return ptr->is_single (); |
|
2505 } |
|
2506 |
|
2507 int |
|
2508 mxIsSparse (const mxArray *ptr) |
|
2509 { |
|
2510 return ptr->is_sparse (); |
|
2511 } |
|
2512 |
|
2513 int |
|
2514 mxIsStruct (const mxArray *ptr) |
|
2515 { |
|
2516 return ptr->is_struct (); |
|
2517 } |
|
2518 |
|
2519 int |
|
2520 mxIsUint16 (const mxArray *ptr) |
|
2521 { |
|
2522 return ptr->is_uint16 (); |
|
2523 } |
|
2524 |
|
2525 int |
|
2526 mxIsUint32 (const mxArray *ptr) |
|
2527 { |
|
2528 return ptr->is_uint32 (); |
|
2529 } |
|
2530 |
|
2531 int |
|
2532 mxIsUint64 (const mxArray *ptr) |
|
2533 { |
|
2534 return ptr->is_uint64 (); |
|
2535 } |
|
2536 |
|
2537 int |
|
2538 mxIsUint8 (const mxArray *ptr) |
|
2539 { |
|
2540 return ptr->is_uint8 (); |
|
2541 } |
|
2542 |
|
2543 // Odd type+size predicate. |
|
2544 int |
|
2545 mxIsLogicalScalar (const mxArray *ptr) |
|
2546 { |
|
2547 return ptr->is_logical_scalar (); |
|
2548 } |
|
2549 |
|
2550 // Odd type+size+value predicate. |
|
2551 int |
|
2552 mxIsLogicalScalarTrue (const mxArray *ptr) |
|
2553 { |
|
2554 return ptr->is_logical_scalar_true (); |
|
2555 } |
|
2556 |
|
2557 // Size predicate. |
|
2558 int |
|
2559 mxIsEmpty (const mxArray *ptr) |
|
2560 { |
|
2561 return ptr->is_empty (); |
|
2562 } |
|
2563 |
|
2564 // Just plain odd thing to ask of a value. |
|
2565 int |
|
2566 mxIsFromGlobalWS (const mxArray */*ptr*/) |
|
2567 { |
|
2568 // FIXME |
|
2569 abort (); |
|
2570 return 0; |
|
2571 } |
|
2572 |
|
2573 // Dimension extractors. |
|
2574 int |
|
2575 mxGetM (const mxArray *ptr) |
|
2576 { |
|
2577 return ptr->get_m (); |
|
2578 } |
|
2579 |
|
2580 int |
|
2581 mxGetN (const mxArray *ptr) |
|
2582 { |
|
2583 return ptr->get_n (); |
|
2584 } |
|
2585 |
|
2586 int * |
|
2587 mxGetDimensions (const mxArray *ptr) |
5864
|
2588 { |
5900
|
2589 return ptr->get_dimensions (); |
|
2590 } |
|
2591 |
|
2592 int |
|
2593 mxGetNumberOfDimensions (const mxArray *ptr) |
|
2594 { |
|
2595 return ptr->get_number_of_dimensions (); |
|
2596 } |
|
2597 |
|
2598 int |
|
2599 mxGetNumberOfElements (const mxArray *ptr) |
|
2600 { |
|
2601 return ptr->get_number_of_elements (); |
|
2602 } |
|
2603 |
|
2604 // Dimension setters. |
|
2605 void |
|
2606 mxSetM (mxArray *ptr, int m) |
|
2607 { |
|
2608 ptr->set_m (m); |
|
2609 } |
|
2610 |
|
2611 void |
|
2612 mxSetN (mxArray *ptr, int n) |
|
2613 { |
|
2614 ptr->set_n (n); |
|
2615 } |
|
2616 |
|
2617 void |
|
2618 mxSetDimensions (mxArray *ptr, int *dims, int ndims) |
|
2619 { |
|
2620 ptr->set_dimensions (dims, ndims); |
|
2621 } |
|
2622 |
|
2623 // Data extractors. |
|
2624 double * |
|
2625 mxGetPr (const mxArray *ptr) |
|
2626 { |
|
2627 return static_cast<double *> (ptr->get_data ()); |
|
2628 } |
|
2629 |
|
2630 double * |
|
2631 mxGetPi (const mxArray *ptr) |
|
2632 { |
|
2633 return static_cast<double *> (ptr->get_imag_data ()); |
|
2634 } |
|
2635 |
|
2636 double |
|
2637 mxGetScalar (const mxArray *ptr) |
|
2638 { |
|
2639 double *d = mxGetPr (ptr); |
|
2640 return d[0]; |
|
2641 } |
|
2642 |
|
2643 mxChar * |
|
2644 mxGetChars (const mxArray *ptr) |
|
2645 { |
|
2646 return static_cast<mxChar *> (ptr->get_data ()); |
|
2647 } |
|
2648 |
|
2649 mxLogical * |
|
2650 mxGetLogicals (const mxArray *ptr) |
|
2651 { |
|
2652 return static_cast<mxLogical *> (ptr->get_data ()); |
|
2653 } |
|
2654 |
|
2655 void * |
|
2656 mxGetData (const mxArray *ptr) |
|
2657 { |
|
2658 return ptr->get_data (); |
|
2659 } |
|
2660 |
|
2661 void * |
|
2662 mxGetImagData (const mxArray *ptr) |
|
2663 { |
|
2664 return ptr->get_imag_data (); |
|
2665 } |
|
2666 |
|
2667 // Data setters. |
|
2668 void |
|
2669 mxSetPr (mxArray *ptr, double *pr) |
|
2670 { |
|
2671 ptr->set_data (pr); |
|
2672 } |
|
2673 |
|
2674 void |
|
2675 mxSetPi (mxArray *ptr, double *pi) |
|
2676 { |
|
2677 ptr->set_imag_data (pi); |
5864
|
2678 } |
|
2679 |
5900
|
2680 void |
|
2681 mxSetData (mxArray *ptr, void *pr) |
|
2682 { |
|
2683 ptr->set_data (pr); |
|
2684 } |
|
2685 |
|
2686 void |
|
2687 mxSetImagData (mxArray *ptr, void *pi) |
|
2688 { |
|
2689 ptr->set_imag_data (pi); |
|
2690 } |
|
2691 |
|
2692 // Classes. |
|
2693 mxClassID |
|
2694 mxGetClassID (const mxArray *ptr) |
|
2695 { |
|
2696 return ptr->get_class_id (); |
|
2697 } |
|
2698 |
|
2699 const char * |
|
2700 mxGetClassName (const mxArray *ptr) |
|
2701 { |
|
2702 return ptr->get_class_name (); |
|
2703 } |
|
2704 |
|
2705 void |
|
2706 mxSetClassName (mxArray *ptr, const char *name) |
|
2707 { |
|
2708 ptr->set_class_name (name); |
|
2709 } |
|
2710 |
|
2711 // Cell support. |
|
2712 mxArray * |
|
2713 mxGetCell (const mxArray *ptr, int idx) |
|
2714 { |
|
2715 return ptr->get_cell (idx); |
|
2716 } |
|
2717 |
|
2718 void |
|
2719 mxSetCell (mxArray *ptr, int idx, mxArray *val) |
|
2720 { |
|
2721 ptr->set_cell (idx, val); |
|
2722 } |
|
2723 |
|
2724 // Sparse support. |
|
2725 int * |
|
2726 mxGetIr (const mxArray *ptr) |
|
2727 { |
|
2728 return ptr->get_ir (); |
|
2729 } |
|
2730 |
|
2731 int * |
|
2732 mxGetJc (const mxArray *ptr) |
|
2733 { |
|
2734 return ptr->get_jc (); |
|
2735 } |
|
2736 |
|
2737 int |
|
2738 mxGetNzmax (const mxArray *ptr) |
|
2739 { |
|
2740 return ptr->get_nzmax (); |
|
2741 } |
|
2742 |
|
2743 void |
|
2744 mxSetIr (mxArray *ptr, int *ir) |
|
2745 { |
|
2746 ptr->set_ir (ir); |
|
2747 } |
|
2748 |
|
2749 void |
|
2750 mxSetJc (mxArray *ptr, int *jc) |
|
2751 { |
|
2752 ptr->set_jc (jc); |
|
2753 } |
|
2754 |
|
2755 void |
|
2756 mxSetNzmax (mxArray *ptr, int nzmax) |
|
2757 { |
|
2758 ptr->set_nzmax (nzmax); |
|
2759 } |
|
2760 |
|
2761 // Structure support. |
|
2762 int |
|
2763 mxAddField (mxArray *ptr, const char *key) |
|
2764 { |
|
2765 return ptr->add_field (key); |
|
2766 } |
|
2767 |
|
2768 void |
|
2769 mxRemoveField (mxArray *ptr, int key_num) |
|
2770 { |
|
2771 ptr->remove_field (key_num); |
|
2772 } |
5864
|
2773 |
|
2774 mxArray * |
5900
|
2775 mxGetField (const mxArray *ptr, int index, const char *key) |
|
2776 { |
|
2777 int key_num = mxGetFieldNumber (ptr, key); |
|
2778 return mxGetFieldByNumber (ptr, index, key_num); |
|
2779 } |
|
2780 |
|
2781 mxArray * |
|
2782 mxGetFieldByNumber (const mxArray *ptr, int index, int key_num) |
5864
|
2783 { |
5900
|
2784 return ptr->get_field_by_number (index, key_num); |
5864
|
2785 } |
|
2786 |
5900
|
2787 void |
|
2788 mxSetField (mxArray *ptr, int index, const char *key, mxArray *val) |
|
2789 { |
|
2790 int key_num = mxGetFieldNumber (ptr, key); |
|
2791 mxSetFieldByNumber (ptr, index, key_num, val); |
|
2792 } |
5864
|
2793 |
|
2794 void |
5900
|
2795 mxSetFieldByNumber (mxArray *ptr, int index, int key_num, mxArray *val) |
5864
|
2796 { |
5900
|
2797 ptr->set_field_by_number (index, key_num, val); |
|
2798 } |
|
2799 |
|
2800 int |
|
2801 mxGetNumberOfFields (const mxArray *ptr) |
|
2802 { |
|
2803 return ptr->get_number_of_fields (); |
5864
|
2804 } |
|
2805 |
5900
|
2806 const char * |
|
2807 mxGetFieldNameByNumber (const mxArray *ptr, int key_num) |
5864
|
2808 { |
5900
|
2809 return ptr->get_field_name_by_number (key_num); |
|
2810 } |
|
2811 |
|
2812 int |
|
2813 mxGetFieldNumber (const mxArray *ptr, const char *key) |
|
2814 { |
|
2815 return ptr->get_field_number (key); |
5864
|
2816 } |
|
2817 |
5900
|
2818 int |
|
2819 mxGetString (const mxArray *ptr, char *buf, int buflen) |
|
2820 { |
|
2821 return ptr->get_string (buf, buflen); |
|
2822 } |
|
2823 |
|
2824 char * |
|
2825 mxArrayToString (const mxArray *ptr) |
5864
|
2826 { |
5900
|
2827 return ptr->array_to_string (); |
|
2828 } |
|
2829 |
|
2830 int |
|
2831 mxCalcSingleSubscript (const mxArray *ptr, int nsubs, int *subs) |
|
2832 { |
|
2833 return ptr->calc_single_subscript (nsubs, subs); |
5864
|
2834 } |
5900
|
2835 |
|
2836 int |
|
2837 mxGetElementSize (const mxArray *ptr) |
|
2838 { |
|
2839 return ptr->get_element_size (); |
|
2840 } |
|
2841 |
|
2842 // ------------------------------------------------------------------ |
5864
|
2843 |
|
2844 typedef void (*cmex_fptr) (int nlhs, mxArray **plhs, int nrhs, mxArray **prhs); |
|
2845 typedef F77_RET_T (*fmex_fptr) (int& nlhs, mxArray **plhs, int& nrhs, mxArray **prhs); |
|
2846 |
|
2847 octave_value_list |
6068
|
2848 call_mex (bool have_fmex, void *f, const octave_value_list& args, |
|
2849 int nargout, octave_mex_function *curr_mex_fcn) |
5864
|
2850 { |
5900
|
2851 // Use at least 1 for nargout since even for zero specified args, |
|
2852 // still want to be able to return an ans. |
5864
|
2853 |
|
2854 int nargin = args.length (); |
5900
|
2855 OCTAVE_LOCAL_BUFFER (mxArray *, argin, nargin); |
5864
|
2856 for (int i = 0; i < nargin; i++) |
|
2857 argin[i] = 0; |
|
2858 |
|
2859 int nout = nargout == 0 ? 1 : nargout; |
5900
|
2860 OCTAVE_LOCAL_BUFFER (mxArray *, argout, nout); |
5864
|
2861 for (int i = 0; i < nout; i++) |
|
2862 argout[i] = 0; |
|
2863 |
5905
|
2864 unwind_protect::begin_frame ("call_mex"); |
|
2865 |
|
2866 // Save old mex pointer. |
|
2867 unwind_protect_ptr (mex_context); |
|
2868 |
6068
|
2869 mex context (curr_mex_fcn); |
5900
|
2870 |
|
2871 unwind_protect::add (mex::cleanup, static_cast<void *> (&context)); |
5864
|
2872 |
|
2873 for (int i = 0; i < nargin; i++) |
|
2874 argin[i] = context.make_value (args(i)); |
|
2875 |
|
2876 if (setjmp (context.jump) == 0) |
|
2877 { |
5900
|
2878 mex_context = &context; |
5864
|
2879 |
6068
|
2880 if (have_fmex) |
5864
|
2881 { |
|
2882 fmex_fptr fcn = FCN_PTR_CAST (fmex_fptr, f); |
|
2883 |
|
2884 int tmp_nargout = nargout; |
|
2885 int tmp_nargin = nargin; |
|
2886 |
|
2887 fcn (tmp_nargout, argout, tmp_nargin, argin); |
|
2888 } |
|
2889 else |
|
2890 { |
|
2891 cmex_fptr fcn = FCN_PTR_CAST (cmex_fptr, f); |
|
2892 |
|
2893 fcn (nargout, argout, nargin, argin); |
|
2894 } |
|
2895 } |
|
2896 |
|
2897 // Convert returned array entries back into octave values. |
|
2898 |
|
2899 octave_value_list retval; |
|
2900 |
|
2901 if (! error_state) |
|
2902 { |
|
2903 if (nargout == 0 && argout[0]) |
|
2904 { |
5900
|
2905 // We have something for ans. |
|
2906 nargout = 1; |
|
2907 } |
|
2908 |
|
2909 retval.resize (nargout); |
|
2910 |
|
2911 for (int i = 0; i < nargout; i++) |
5907
|
2912 retval(i) = mxArray::as_octave_value (argout[i]); |
5864
|
2913 } |
|
2914 |
|
2915 // Clean up mex resources. |
5905
|
2916 unwind_protect::run_frame ("call_mex"); |
5864
|
2917 |
|
2918 return retval; |
|
2919 } |
|
2920 |
|
2921 // C interface to mex functions: |
|
2922 |
|
2923 const char * |
|
2924 mexFunctionName (void) |
|
2925 { |
5900
|
2926 return mex_context ? mex_context->function_name () : "unknown"; |
|
2927 } |
|
2928 |
|
2929 int |
|
2930 mexCallMATLAB (int nargout, mxArray *argout[], int nargin, mxArray *argin[], |
|
2931 const char *fname) |
|
2932 { |
|
2933 octave_value_list args; |
|
2934 |
|
2935 // FIXME -- do we need unwind protect to clean up args? Off hand, I |
|
2936 // would say that this problem is endemic to Octave and we will |
|
2937 // continue to have memory leaks after Ctrl-C until proper exception |
|
2938 // handling is implemented. longjmp() only clears the stack, so any |
|
2939 // class which allocates data on the heap is going to leak. |
|
2940 |
|
2941 args.resize (nargin); |
|
2942 |
|
2943 for (int i = 0; i < nargin; i++) |
5907
|
2944 args(i) = mxArray::as_octave_value (argin[i]); |
5900
|
2945 |
|
2946 octave_value_list retval = feval (fname, args, nargout); |
|
2947 |
|
2948 if (error_state && mex_context->trap_feval_error == 0) |
5864
|
2949 { |
5900
|
2950 // FIXME -- is this the correct way to clean up? abort() is |
|
2951 // going to trigger a long jump, so the normal class destructors |
|
2952 // will not be called. Hopefully this will reduce things to a |
|
2953 // tiny leak. Maybe create a new octave memory tracer type |
|
2954 // which prints a friendly message every time it is |
|
2955 // created/copied/deleted to check this. |
|
2956 |
|
2957 args.resize (0); |
|
2958 retval.resize (0); |
|
2959 mex_context->abort (); |
|
2960 } |
|
2961 |
|
2962 int num_to_copy = retval.length (); |
|
2963 |
|
2964 if (nargout < retval.length ()) |
|
2965 num_to_copy = nargout; |
|
2966 |
|
2967 for (int i = 0; i < num_to_copy; i++) |
|
2968 { |
|
2969 // FIXME -- it would be nice to avoid copying the value here, |
|
2970 // but there is no way to steal memory from a matrix, never mind |
|
2971 // that matrix memory is allocated by new[] and mxArray memory |
|
2972 // is allocated by malloc(). |
|
2973 argout[i] = mex_context->make_value (retval (i)); |
|
2974 } |
|
2975 |
|
2976 while (num_to_copy < nargout) |
|
2977 argout[num_to_copy++] = 0; |
|
2978 |
|
2979 if (error_state) |
|
2980 { |
|
2981 error_state = 0; |
|
2982 return 1; |
5864
|
2983 } |
|
2984 else |
5900
|
2985 return 0; |
|
2986 } |
|
2987 |
|
2988 void |
|
2989 mexSetTrapFlag (int flag) |
|
2990 { |
|
2991 if (mex_context) |
|
2992 mex_context->trap_feval_error = flag; |
|
2993 } |
|
2994 |
|
2995 int |
|
2996 mexEvalString (const char *s) |
|
2997 { |
|
2998 int retval = 0; |
|
2999 |
|
3000 int parse_status; |
|
3001 |
|
3002 octave_value_list ret; |
|
3003 |
|
3004 ret = eval_string (s, false, parse_status, 0); |
|
3005 |
|
3006 if (parse_status || error_state) |
|
3007 { |
|
3008 error_state = 0; |
|
3009 |
|
3010 retval = 1; |
|
3011 } |
5864
|
3012 |
|
3013 return retval; |
|
3014 } |
|
3015 |
|
3016 void |
|
3017 mexErrMsgTxt (const char *s) |
|
3018 { |
|
3019 if (s && strlen (s) > 0) |
5879
|
3020 error ("%s: %s", mexFunctionName (), s); |
5864
|
3021 else |
|
3022 // Just set the error state; don't print msg. |
|
3023 error (""); |
|
3024 |
5900
|
3025 mex_context->abort (); |
5864
|
3026 } |
|
3027 |
5879
|
3028 void |
|
3029 mexErrMsgIdAndTxt (const char *id, const char *s) |
|
3030 { |
|
3031 if (s && strlen (s) > 0) |
|
3032 error_with_id (id, "%s: %s", mexFunctionName (), s); |
|
3033 else |
|
3034 // Just set the error state; don't print msg. |
|
3035 error (""); |
|
3036 |
5900
|
3037 mex_context->abort (); |
5879
|
3038 } |
|
3039 |
|
3040 void |
|
3041 mexWarnMsgTxt (const char *s) |
|
3042 { |
|
3043 warning ("%s", s); |
|
3044 } |
|
3045 |
|
3046 void |
|
3047 mexWarnMsgIdAndTxt (const char *id, const char *s) |
|
3048 { |
|
3049 warning_with_id (id, "%s", s); |
|
3050 } |
5864
|
3051 |
|
3052 void |
|
3053 mexPrintf (const char *fmt, ...) |
|
3054 { |
|
3055 va_list args; |
|
3056 va_start (args, fmt); |
|
3057 octave_vformat (octave_stdout, fmt, args); |
|
3058 va_end (args); |
|
3059 } |
|
3060 |
|
3061 mxArray * |
5879
|
3062 mexGetVariable (const char *space, const char *name) |
5864
|
3063 { |
|
3064 mxArray *retval = 0; |
|
3065 |
|
3066 // FIXME -- this should be in variable.cc, but the correct |
|
3067 // functionality is not exported. Particularly, get_global_value() |
|
3068 // generates an error if the symbol is undefined. |
|
3069 |
|
3070 symbol_record *sr = 0; |
|
3071 |
|
3072 if (! strcmp (space, "global")) |
|
3073 sr = global_sym_tab->lookup (name); |
|
3074 else if (! strcmp (space, "caller")) |
|
3075 sr = curr_sym_tab->lookup (name); |
|
3076 else if (! strcmp (space, "base")) |
5900
|
3077 sr = top_level_sym_tab->lookup (name); |
5864
|
3078 else |
5879
|
3079 mexErrMsgTxt ("mexGetVariable: symbol table does not exist"); |
5864
|
3080 |
|
3081 if (sr) |
|
3082 { |
|
3083 octave_value sr_def = sr->def (); |
|
3084 |
|
3085 if (sr_def.is_defined ()) |
|
3086 { |
5900
|
3087 retval = mex_context->make_value (sr_def); |
|
3088 |
|
3089 retval->set_name (name); |
5864
|
3090 } |
|
3091 } |
|
3092 |
|
3093 return retval; |
|
3094 } |
|
3095 |
5879
|
3096 const mxArray * |
|
3097 mexGetVariablePtr (const char *space, const char *name) |
5864
|
3098 { |
5879
|
3099 return mexGetVariable (space, name); |
5864
|
3100 } |
|
3101 |
5900
|
3102 int |
|
3103 mexPutVariable (const char *space, const char *name, mxArray *ptr) |
5864
|
3104 { |
5900
|
3105 if (! ptr) |
|
3106 return 1; |
|
3107 |
|
3108 if (! name) |
|
3109 return 1; |
|
3110 |
|
3111 if (name[0] == '\0') |
|
3112 name = ptr->get_name (); |
|
3113 |
|
3114 if (! name || name[0] == '\0') |
|
3115 return 1; |
|
3116 |
|
3117 if (! strcmp (space, "global")) |
5907
|
3118 set_global_value (name, mxArray::as_octave_value (ptr)); |
5900
|
3119 else |
|
3120 { |
|
3121 // FIXME -- this belongs in variables.cc. |
|
3122 |
|
3123 symbol_record *sr = 0; |
|
3124 |
|
3125 if (! strcmp (space, "caller")) |
|
3126 sr = curr_sym_tab->lookup (name, true); |
|
3127 else if (! strcmp (space, "base")) |
|
3128 sr = top_level_sym_tab->lookup (name, true); |
|
3129 else |
|
3130 mexErrMsgTxt ("mexPutVariable: symbol table does not exist"); |
|
3131 |
|
3132 if (sr) |
5907
|
3133 sr->define (mxArray::as_octave_value (ptr)); |
5900
|
3134 else |
|
3135 panic_impossible (); |
|
3136 } |
|
3137 |
|
3138 return 0; |
5864
|
3139 } |
|
3140 |
|
3141 void |
5900
|
3142 mexMakeArrayPersistent (mxArray *ptr) |
5864
|
3143 { |
5900
|
3144 if (mex_context) |
|
3145 mex_context->persistent (ptr); |
5864
|
3146 } |
5879
|
3147 |
5864
|
3148 void |
5900
|
3149 mexMakeMemoryPersistent (void *ptr) |
5864
|
3150 { |
5900
|
3151 if (mex_context) |
|
3152 mex_context->persistent (ptr); |
5864
|
3153 } |
|
3154 |
5900
|
3155 int |
6068
|
3156 mexAtExit (void (*f) (void)) |
5864
|
3157 { |
6068
|
3158 if (mex_context) |
|
3159 { |
|
3160 octave_mex_function *curr_mex_fcn = mex_context->current_mex_function (); |
|
3161 |
|
3162 assert (curr_mex_fcn); |
|
3163 |
|
3164 curr_mex_fcn->atexit (f); |
|
3165 } |
|
3166 |
5900
|
3167 return 0; |
5864
|
3168 } |
|
3169 |
5900
|
3170 const mxArray * |
|
3171 mexGet (double /*handle*/, const char */*property*/) |
5864
|
3172 { |
5900
|
3173 // FIXME |
|
3174 error ("mexGet: not implemented"); |
|
3175 return 0; |
5864
|
3176 } |
|
3177 |
5900
|
3178 int |
|
3179 mexIsGlobal (const mxArray *ptr) |
5864
|
3180 { |
5900
|
3181 return mxIsFromGlobalWS (ptr); |
5864
|
3182 } |
|
3183 |
5900
|
3184 int |
|
3185 mexIsLocked (void) |
5864
|
3186 { |
5900
|
3187 int retval = 0; |
|
3188 |
|
3189 if (mex_context) |
|
3190 { |
|
3191 const char *fname = mexFunctionName (); |
|
3192 |
|
3193 retval = mislocked (fname); |
|
3194 } |
|
3195 |
|
3196 return retval; |
5864
|
3197 } |
|
3198 |
5900
|
3199 std::map<std::string,int> mex_lock_count; |
|
3200 |
|
3201 void |
|
3202 mexLock (void) |
5864
|
3203 { |
5900
|
3204 if (mex_context) |
5864
|
3205 { |
5900
|
3206 const char *fname = mexFunctionName (); |
|
3207 |
|
3208 if (mex_lock_count.find (fname) == mex_lock_count.end ()) |
|
3209 mex_lock_count[fname] = 1; |
|
3210 else |
|
3211 mex_lock_count[fname]++; |
|
3212 |
|
3213 mlock (fname); |
5864
|
3214 } |
|
3215 } |
|
3216 |
5900
|
3217 int |
|
3218 mexSet (double /*handle*/, const char */*property*/, mxArray */*val*/) |
|
3219 { |
|
3220 // FIXME |
|
3221 error ("mexSet: not implemented"); |
|
3222 return 0; |
|
3223 } |
|
3224 |
|
3225 void |
|
3226 mexUnlock (void) |
5864
|
3227 { |
5900
|
3228 if (mex_context) |
5864
|
3229 { |
5900
|
3230 const char *fname = mexFunctionName (); |
|
3231 |
5905
|
3232 std::map<std::string,int>::iterator p = mex_lock_count.find (fname); |
|
3233 |
6062
|
3234 if (p != mex_lock_count.end ()) |
5900
|
3235 { |
|
3236 int count = --mex_lock_count[fname]; |
|
3237 |
|
3238 if (count == 0) |
|
3239 { |
|
3240 munlock (fname); |
|
3241 |
5905
|
3242 mex_lock_count.erase (p); |
5900
|
3243 } |
|
3244 } |
5864
|
3245 } |
|
3246 } |