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