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