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 |
6071
|
1649 void set_field_by_number (int index, int key_num, mxArray *val); |
5900
|
1650 |
|
1651 int get_number_of_fields (void) const { return nfields; } |
|
1652 |
|
1653 const char *get_field_name_by_number (int key_num) const |
|
1654 { |
|
1655 return key_num >= 0 && key_num < nfields ? fields[key_num] : 0; |
|
1656 } |
|
1657 |
|
1658 int get_field_number (const char *key) const |
|
1659 { |
|
1660 int retval = -1; |
|
1661 |
|
1662 for (int i = 0; i < nfields; i++) |
|
1663 { |
|
1664 if (! strcmp (key, fields[i])) |
|
1665 { |
|
1666 retval = i; |
|
1667 break; |
|
1668 } |
|
1669 } |
|
1670 |
|
1671 return retval; |
|
1672 } |
|
1673 |
|
1674 void *get_data (void) const { return data; } |
|
1675 |
|
1676 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1677 |
5907
|
1678 protected: |
|
1679 |
|
1680 octave_value as_octave_value (void) const |
|
1681 { |
|
1682 dim_vector dv = dims_to_dim_vector (); |
|
1683 |
|
1684 string_vector keys (fields, nfields); |
|
1685 |
|
1686 Octave_map m; |
|
1687 |
|
1688 int ntot = nfields * get_number_of_elements (); |
|
1689 |
|
1690 for (int i = 0; i < nfields; i++) |
|
1691 { |
|
1692 Cell c (dv); |
|
1693 |
|
1694 octave_value *p = c.fortran_vec (); |
|
1695 |
|
1696 int k = 0; |
|
1697 for (int j = i; j < ntot; j += nfields) |
|
1698 p[k++] = mxArray::as_octave_value (data[j]); |
|
1699 |
|
1700 m.assign (keys[i], c); |
|
1701 } |
|
1702 |
|
1703 return m; |
|
1704 } |
|
1705 |
5900
|
1706 private: |
|
1707 |
|
1708 int nfields; |
|
1709 |
|
1710 char **fields; |
|
1711 |
|
1712 mxArray **data; |
|
1713 |
|
1714 mxArray_struct (const mxArray_struct& val) |
|
1715 : mxArray_matlab (val), nfields (val.nfields), |
|
1716 fields (static_cast<char **> (malloc (nfields * sizeof (char *)))), |
|
1717 data (static_cast<mxArray **> (malloc (nfields * get_number_of_elements () * sizeof (mxArray *)))) |
|
1718 { |
|
1719 for (int i = 0; i < nfields; i++) |
|
1720 fields[i] = strsave (val.fields[i]); |
|
1721 |
|
1722 int nel = get_number_of_elements (); |
|
1723 |
|
1724 for (int i = 0; i < nel * nfields; i++) |
|
1725 data[i] = val.data[i]->clone (); |
|
1726 } |
|
1727 }; |
|
1728 |
|
1729 // Matlab-style cell arrays. |
|
1730 |
|
1731 class mxArray_cell : public mxArray_matlab |
|
1732 { |
|
1733 public: |
|
1734 |
|
1735 mxArray_cell (int ndims_arg, const int *dims_arg) |
|
1736 : mxArray_matlab (mxCELL_CLASS, ndims_arg, dims_arg), |
|
1737 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1738 |
|
1739 mxArray_cell (const dim_vector& dv) |
|
1740 : mxArray_matlab (mxCELL_CLASS, dv), |
|
1741 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1742 |
|
1743 mxArray_cell (int m, int n) |
|
1744 : mxArray_matlab (mxCELL_CLASS, m, n), |
|
1745 data (static_cast<mxArray **> (calloc (get_number_of_elements (), sizeof (mxArray *)))) { } |
|
1746 |
|
1747 mxArray_cell *clone (void) const { return new mxArray_cell (*this); } |
|
1748 |
|
1749 ~mxArray_cell (void) |
|
1750 { |
|
1751 int nel = get_number_of_elements (); |
|
1752 |
|
1753 for (int i = 0; i < nel; i++) |
5905
|
1754 delete data[i]; |
5900
|
1755 |
|
1756 mxFree (data); |
|
1757 } |
|
1758 |
5907
|
1759 mxArray *get_cell (int idx) const { return data[idx]; } |
|
1760 |
6071
|
1761 void set_cell (int idx, mxArray *val); |
5907
|
1762 |
|
1763 void *get_data (void) const { return data; } |
|
1764 |
|
1765 void set_data (void *data_arg) { data = static_cast<mxArray **> (data_arg); } |
|
1766 |
|
1767 protected: |
|
1768 |
5900
|
1769 octave_value as_octave_value (void) const |
|
1770 { |
|
1771 dim_vector dv = dims_to_dim_vector (); |
|
1772 |
|
1773 Cell c (dv); |
|
1774 |
|
1775 int nel = get_number_of_elements (); |
|
1776 |
|
1777 octave_value *p = c.fortran_vec (); |
|
1778 |
|
1779 for (int i = 0; i < nel; i++) |
5907
|
1780 p[i] = mxArray::as_octave_value (data[i]); |
5900
|
1781 |
|
1782 return c; |
|
1783 } |
|
1784 |
|
1785 private: |
|
1786 |
|
1787 mxArray **data; |
|
1788 |
|
1789 mxArray_cell (const mxArray_cell& val) |
|
1790 : mxArray_matlab (val), |
|
1791 data (static_cast<mxArray **> (malloc (get_number_of_elements () * sizeof (mxArray *)))) |
|
1792 { |
|
1793 int nel = get_number_of_elements (); |
|
1794 |
|
1795 for (int i = 0; i < nel; i++) |
|
1796 data[i] = val.data[i]->clone (); |
|
1797 } |
|
1798 }; |
|
1799 |
|
1800 // ------------------------------------------------------------------ |
|
1801 |
|
1802 mxArray::mxArray (const octave_value& ov) |
6065
|
1803 : rep (new mxArray_octave_value (ov)), name (0) { } |
5900
|
1804 |
|
1805 mxArray::mxArray (mxClassID id, int ndims, const int *dims, mxComplexity flag) |
6065
|
1806 : rep (new mxArray_number (id, ndims, dims, flag)), name (0) { } |
5900
|
1807 |
|
1808 mxArray::mxArray (mxClassID id, const dim_vector& dv, mxComplexity flag) |
6065
|
1809 : rep (new mxArray_number (id, dv, flag)), name (0) { } |
5900
|
1810 |
|
1811 mxArray::mxArray (mxClassID id, int m, int n, mxComplexity flag) |
6065
|
1812 : rep (new mxArray_number (id, m, n, flag)), name (0) { } |
5900
|
1813 |
|
1814 mxArray::mxArray (mxClassID id, double val) |
6065
|
1815 : rep (new mxArray_number (id, val)), name (0) { } |
5900
|
1816 |
|
1817 mxArray::mxArray (mxClassID id, mxLogical val) |
6065
|
1818 : rep (new mxArray_number (id, val)), name (0) { } |
5900
|
1819 |
|
1820 mxArray::mxArray (const char *str) |
6065
|
1821 : rep (new mxArray_number (str)), name (0) { } |
5900
|
1822 |
|
1823 mxArray::mxArray (int m, const char **str) |
6065
|
1824 : rep (new mxArray_number (m, str)), name (0) { } |
5900
|
1825 |
|
1826 mxArray::mxArray (mxClassID id, int m, int n, int nzmax, mxComplexity flag) |
6065
|
1827 : rep (new mxArray_sparse (id, m, n, nzmax, flag)), name (0) { } |
5900
|
1828 |
|
1829 mxArray::mxArray (int ndims, const int *dims, int num_keys, const char **keys) |
6065
|
1830 : rep (new mxArray_struct (ndims, dims, num_keys, keys)), name (0) { } |
5900
|
1831 |
|
1832 mxArray::mxArray (const dim_vector& dv, int num_keys, const char **keys) |
6065
|
1833 : rep (new mxArray_struct (dv, num_keys, keys)), name (0) { } |
5900
|
1834 |
|
1835 mxArray::mxArray (int m, int n, int num_keys, const char **keys) |
6065
|
1836 : rep (new mxArray_struct (m, n, num_keys, keys)), name (0) { } |
5900
|
1837 |
|
1838 mxArray::mxArray (int ndims, const int *dims) |
6065
|
1839 : rep (new mxArray_cell (ndims, dims)), name (0) { } |
5900
|
1840 |
|
1841 mxArray::mxArray (const dim_vector& dv) |
6065
|
1842 : rep (new mxArray_cell (dv)), name (0) { } |
5900
|
1843 |
|
1844 mxArray::mxArray (int m, int n) |
6065
|
1845 : rep (new mxArray_cell (m, n)), name (0) { } |
5900
|
1846 |
|
1847 mxArray::~mxArray (void) |
|
1848 { |
|
1849 mxFree (name); |
|
1850 |
|
1851 delete rep; |
|
1852 } |
|
1853 |
|
1854 void |
|
1855 mxArray::set_name (const char *name_arg) |
|
1856 { |
|
1857 mxFree (name); |
|
1858 name = strsave (name_arg); |
|
1859 } |
|
1860 |
5907
|
1861 octave_value |
|
1862 mxArray::as_octave_value (mxArray *ptr) |
|
1863 { |
|
1864 return ptr ? ptr->as_octave_value () : octave_value (Matrix ()); |
|
1865 } |
|
1866 |
|
1867 octave_value |
|
1868 mxArray::as_octave_value (void) const |
|
1869 { |
|
1870 return rep->as_octave_value (); |
|
1871 } |
|
1872 |
5900
|
1873 void |
|
1874 mxArray::maybe_mutate (void) const |
|
1875 { |
|
1876 if (rep->is_octave_value ()) |
|
1877 { |
|
1878 // The mutate function returns a pointer to a complete new |
|
1879 // mxArray object (or 0, if no mutation happened). We just want |
|
1880 // to replace the existing rep with the rep from the new object. |
|
1881 |
|
1882 mxArray *new_val = rep->mutate (); |
|
1883 |
|
1884 if (new_val) |
|
1885 { |
|
1886 delete rep; |
|
1887 rep = new_val->rep; |
|
1888 new_val->rep = 0; |
|
1889 delete new_val; |
|
1890 } |
|
1891 } |
|
1892 } |
|
1893 |
|
1894 // ------------------------------------------------------------------ |
|
1895 |
|
1896 // A clas to manage calls to MEX functions. Mostly deals with memory |
|
1897 // management. |
5864
|
1898 |
|
1899 class mex |
|
1900 { |
|
1901 public: |
|
1902 |
6068
|
1903 mex (octave_mex_function *f) |
|
1904 : curr_mex_fcn (f), memlist (), arraylist (), fname (0) { } |
5864
|
1905 |
|
1906 ~mex (void) |
|
1907 { |
|
1908 if (! memlist.empty ()) |
5905
|
1909 error ("mex: %s: cleanup failed", function_name ()); |
5900
|
1910 |
|
1911 mxFree (fname); |
5864
|
1912 } |
|
1913 |
5900
|
1914 const char *function_name (void) const |
|
1915 { |
|
1916 if (! fname) |
|
1917 { |
|
1918 octave_function *fcn = octave_call_stack::current (); |
|
1919 |
|
1920 if (fcn) |
|
1921 { |
|
1922 std::string nm = fcn->name (); |
6065
|
1923 fname = mxArray::strsave (nm.c_str ()); |
5900
|
1924 } |
|
1925 else |
6065
|
1926 fname = mxArray::strsave ("unknown"); |
5900
|
1927 } |
|
1928 |
|
1929 return fname; |
|
1930 } |
|
1931 |
|
1932 // Free all unmarked pointers obtained from malloc and calloc. |
|
1933 static void cleanup (void *ptr) |
|
1934 { |
|
1935 mex *context = static_cast<mex *> (ptr); |
|
1936 |
5905
|
1937 // We can't use mex::free here because it modifies memlist. |
5900
|
1938 for (std::set<void *>::iterator p = context->memlist.begin (); |
|
1939 p != context->memlist.end (); p++) |
5905
|
1940 { |
|
1941 if (*p) |
|
1942 { |
|
1943 context->unmark (*p); |
|
1944 |
|
1945 xfree (*p); |
|
1946 } |
|
1947 } |
|
1948 |
|
1949 context->memlist.clear (); |
|
1950 |
|
1951 // We can't use mex::free_value here because it modifies arraylist. |
5900
|
1952 for (std::set<mxArray *>::iterator p = context->arraylist.begin (); |
|
1953 p != context->arraylist.end (); p++) |
5905
|
1954 delete *p; |
|
1955 |
|
1956 context->arraylist.clear (); |
5900
|
1957 } |
5864
|
1958 |
6071
|
1959 // Allocate memory. |
5900
|
1960 void *malloc_unmarked (size_t n) |
|
1961 { |
|
1962 void *ptr = ::malloc (n); |
|
1963 |
|
1964 if (! ptr) |
|
1965 { |
|
1966 // FIXME -- could use "octave_new_handler();" instead |
|
1967 |
|
1968 error ("%s: failed to allocate %d bytes of memory", |
5905
|
1969 function_name (), n); |
5900
|
1970 |
|
1971 abort (); |
|
1972 } |
|
1973 |
|
1974 global_mark (ptr); |
|
1975 |
|
1976 return ptr; |
|
1977 } |
|
1978 |
6071
|
1979 // Allocate memory to be freed on exit. |
5900
|
1980 void *malloc (size_t n) |
|
1981 { |
|
1982 void *ptr = malloc_unmarked (n); |
|
1983 |
|
1984 mark (ptr); |
|
1985 |
|
1986 return ptr; |
|
1987 } |
|
1988 |
6071
|
1989 // Allocate memory and initialize to 0. |
5900
|
1990 void *calloc_unmarked (size_t n, size_t t) |
|
1991 { |
|
1992 void *ptr = malloc_unmarked (n*t); |
|
1993 |
|
1994 memset (ptr, 0, n*t); |
|
1995 |
|
1996 return ptr; |
|
1997 } |
|
1998 |
6071
|
1999 // Allocate memory to be freed on exit and initialize to 0. |
5900
|
2000 void *calloc (size_t n, size_t t) |
|
2001 { |
|
2002 void *ptr = calloc_unmarked (n, t); |
|
2003 |
|
2004 mark (ptr); |
|
2005 |
|
2006 return ptr; |
|
2007 } |
|
2008 |
|
2009 // Reallocate a pointer obtained from malloc or calloc. We don't |
|
2010 // need an "unmarked" version of this. |
|
2011 void *realloc (void *ptr, size_t n) |
|
2012 { |
|
2013 void *v = ::realloc (ptr, n); |
|
2014 |
|
2015 std::set<void *>::iterator p = memlist.find (ptr); |
|
2016 |
|
2017 if (v && p != memlist.end ()) |
|
2018 { |
|
2019 memlist.erase (p); |
|
2020 memlist.insert (v); |
|
2021 } |
|
2022 |
|
2023 p = global_memlist.find (ptr); |
|
2024 |
|
2025 if (v && p != global_memlist.end ()) |
|
2026 { |
|
2027 global_memlist.erase (p); |
|
2028 global_memlist.insert (v); |
|
2029 } |
|
2030 |
|
2031 return v; |
|
2032 } |
|
2033 |
|
2034 // Free a pointer obtained from malloc or calloc. |
|
2035 void free (void *ptr) |
|
2036 { |
|
2037 if (ptr) |
|
2038 { |
|
2039 unmark (ptr); |
|
2040 |
|
2041 std::set<void *>::iterator p = global_memlist.find (ptr); |
|
2042 |
|
2043 if (p != global_memlist.end ()) |
|
2044 { |
|
2045 global_memlist.erase (p); |
|
2046 |
5905
|
2047 xfree (ptr); |
5900
|
2048 } |
|
2049 else |
|
2050 warning ("mxFree: skipping memory not allocated by mxMalloc, mxCalloc, or mxRealloc"); |
|
2051 } |
|
2052 } |
|
2053 |
|
2054 // Mark a pointer so that it will not be freed on exit. |
|
2055 void persistent (void *ptr) { unmark (ptr); } |
|
2056 |
6065
|
2057 mxArray *mark_array (mxArray *ptr) |
|
2058 { |
|
2059 arraylist.insert (ptr); |
|
2060 return ptr; |
|
2061 } |
|
2062 |
6071
|
2063 void unmark_array (mxArray *ptr) |
|
2064 { |
|
2065 std::set<mxArray *>::iterator p = arraylist.find (ptr); |
|
2066 |
|
2067 if (p != arraylist.end ()) |
|
2068 arraylist.erase (p); |
|
2069 } |
|
2070 |
5900
|
2071 // Make a new array value and initialize from an octave value; it will be |
|
2072 // freed on exit unless marked as persistent. |
|
2073 mxArray *make_value (const octave_value& ov) |
|
2074 { |
6065
|
2075 return mark_array (new mxArray (ov)); |
5900
|
2076 } |
|
2077 |
|
2078 // Free an array and its contents. |
6065
|
2079 bool free_value (mxArray *ptr) |
5900
|
2080 { |
6065
|
2081 bool inlist = false; |
|
2082 |
5905
|
2083 std::set<mxArray *>::iterator p = arraylist.find (ptr); |
|
2084 |
|
2085 if (p != arraylist.end ()) |
|
2086 { |
6065
|
2087 inlist = true; |
5905
|
2088 arraylist.erase (p); |
|
2089 delete ptr; |
|
2090 } |
|
2091 #ifdef DEBUG |
|
2092 else |
|
2093 warning ("mex::free_value: skipping memory not allocated by mex::make_value"); |
|
2094 #endif |
6065
|
2095 |
|
2096 return inlist; |
5900
|
2097 } |
|
2098 |
6065
|
2099 // Remove PTR from the list of arrays to be free on exit. |
6071
|
2100 void persistent (mxArray *ptr) { unmark_array (ptr); } |
5900
|
2101 |
6068
|
2102 octave_mex_function *current_mex_function (void) const |
|
2103 { |
|
2104 return curr_mex_fcn; |
|
2105 } |
|
2106 |
5900
|
2107 // 1 if error should be returned to MEX file, 0 if abort. |
5864
|
2108 int trap_feval_error; |
|
2109 |
5900
|
2110 // longjmp return point if mexErrMsgTxt or error. |
5864
|
2111 jmp_buf jump; |
|
2112 |
5900
|
2113 // Trigger a long jump back to the mex calling function. |
5864
|
2114 void abort (void) { longjmp (jump, 1); } |
|
2115 |
|
2116 private: |
|
2117 |
6068
|
2118 // Pointer to the mex function that corresponds to this mex context. |
|
2119 octave_mex_function *curr_mex_fcn; |
|
2120 |
5900
|
2121 // List of memory resources that need to be freed upon exit. |
|
2122 std::set<void *> memlist; |
|
2123 |
|
2124 std::set<mxArray *> arraylist; |
|
2125 |
|
2126 // The name of the currently executing function. |
|
2127 mutable char *fname; |
|
2128 |
|
2129 // Mark a pointer to be freed on exit. |
6071
|
2130 void mark (void *ptr) |
5900
|
2131 { |
5864
|
2132 #ifdef DEBUG |
6071
|
2133 if (memlist.find (ptr) != memlist.end ()) |
5905
|
2134 warning ("%s: double registration ignored", function_name ()); |
5864
|
2135 #endif |
|
2136 |
6071
|
2137 memlist.insert (ptr); |
5900
|
2138 } |
|
2139 |
|
2140 // Unmark a pointer to be freed on exit, either because it was |
|
2141 // made persistent, or because it was already freed. |
5905
|
2142 void unmark (void *ptr) |
5900
|
2143 { |
5905
|
2144 std::set<void *>::iterator p = memlist.find (ptr); |
|
2145 |
|
2146 if (p != memlist.end ()) |
|
2147 memlist.erase (p); |
5864
|
2148 #ifdef DEBUG |
5905
|
2149 else |
|
2150 warning ("%s: value not marked", function_name ()); |
5864
|
2151 #endif |
5900
|
2152 } |
|
2153 |
|
2154 // List of memory resources we allocated. |
|
2155 static std::set<void *> global_memlist; |
|
2156 |
|
2157 // Mark a pointer as one we allocated. |
5905
|
2158 void global_mark (void *ptr) |
5900
|
2159 { |
|
2160 #ifdef DEBUG |
5905
|
2161 if (global_memlist.find (ptr) != global_memlist.end ()) |
|
2162 warning ("%s: double registration ignored", function_name ()); |
5864
|
2163 #endif |
5900
|
2164 |
5905
|
2165 global_memlist.insert (ptr); |
5864
|
2166 } |
|
2167 |
5900
|
2168 // Unmark a pointer as one we allocated. |
5905
|
2169 void global_unmark (void *ptr) |
5864
|
2170 { |
5905
|
2171 std::set<void *>::iterator p = global_memlist.find (ptr); |
|
2172 |
|
2173 if (p != global_memlist.end ()) |
|
2174 global_memlist.erase (p); |
5900
|
2175 #ifdef DEBUG |
5905
|
2176 else |
|
2177 warning ("%s: value not marked", function_name ()); |
5900
|
2178 #endif |
|
2179 |
5864
|
2180 } |
|
2181 }; |
|
2182 |
5900
|
2183 // List of memory resources we allocated. |
|
2184 std::set<void *> mex::global_memlist; |
|
2185 |
|
2186 // Current context. |
|
2187 mex *mex_context = 0; |
|
2188 |
|
2189 void * |
|
2190 mxArray::malloc (size_t n) |
|
2191 { |
6065
|
2192 return mex_context ? mex_context->malloc_unmarked (n) : ::malloc (n); |
5900
|
2193 } |
|
2194 |
|
2195 void * |
|
2196 mxArray::calloc (size_t n, size_t t) |
|
2197 { |
6065
|
2198 return mex_context ? mex_context->calloc_unmarked (n, t) : ::calloc (n, t); |
5900
|
2199 } |
|
2200 |
6071
|
2201 static inline mxArray * |
|
2202 maybe_unmark_array (mxArray *ptr) |
|
2203 { |
|
2204 if (mex_context) |
|
2205 mex_context->unmark_array (ptr); |
|
2206 |
|
2207 return ptr; |
|
2208 } |
|
2209 |
|
2210 void |
|
2211 mxArray_struct::set_field_by_number (int index, int key_num, mxArray *val) |
|
2212 { |
|
2213 int idx = nfields * index + key_num; |
|
2214 |
|
2215 data[idx] = maybe_unmark_array (val); |
|
2216 } |
|
2217 |
|
2218 void |
|
2219 mxArray_cell::set_cell (int idx, mxArray *val) |
|
2220 { |
|
2221 data[idx] = maybe_unmark_array (val); |
|
2222 } |
|
2223 |
5900
|
2224 // ------------------------------------------------------------------ |
|
2225 |
|
2226 // C interface to mxArray objects: |
|
2227 |
|
2228 // Floating point predicates. |
|
2229 |
|
2230 int |
|
2231 mxIsFinite (const double v) |
|
2232 { |
|
2233 return lo_ieee_finite (v) != 0; |
|
2234 } |
|
2235 |
|
2236 int |
|
2237 mxIsInf (const double v) |
|
2238 { |
|
2239 return lo_ieee_isinf (v) != 0; |
|
2240 } |
|
2241 |
|
2242 int |
|
2243 mxIsNaN (const double v) |
|
2244 { |
|
2245 return lo_ieee_isnan (v) != 0; |
|
2246 } |
|
2247 |
|
2248 double |
|
2249 mxGetEps (void) |
|
2250 { |
|
2251 return DBL_EPSILON; |
|
2252 } |
|
2253 |
|
2254 double |
|
2255 mxGetInf (void) |
|
2256 { |
|
2257 return lo_ieee_inf_value (); |
|
2258 } |
|
2259 |
|
2260 double |
|
2261 mxGetNaN (void) |
|
2262 { |
|
2263 return lo_ieee_nan_value (); |
|
2264 } |
|
2265 |
|
2266 // Memory management. |
|
2267 void * |
|
2268 mxCalloc (size_t n, size_t size) |
|
2269 { |
|
2270 return mex_context ? mex_context->calloc (n, size) : calloc (n, size); |
|
2271 } |
|
2272 |
|
2273 void * |
|
2274 mxMalloc (size_t n) |
|
2275 { |
|
2276 return mex_context ? mex_context->malloc (n) : malloc (n); |
|
2277 } |
|
2278 |
|
2279 void * |
|
2280 mxRealloc (void *ptr, size_t size) |
|
2281 { |
|
2282 return mex_context ? mex_context->realloc (ptr, size) : realloc (ptr, size); |
|
2283 } |
|
2284 |
|
2285 void |
|
2286 mxFree (void *ptr) |
5864
|
2287 { |
5900
|
2288 if (mex_context) |
|
2289 mex_context->free (ptr); |
5864
|
2290 else |
6071
|
2291 xfree (ptr); |
5900
|
2292 } |
6065
|
2293 |
|
2294 static inline mxArray * |
|
2295 maybe_mark_array (mxArray *ptr) |
|
2296 { |
|
2297 return mex_context ? mex_context->mark_array (ptr) : ptr; |
|
2298 } |
5900
|
2299 |
|
2300 // Constructors. |
|
2301 mxArray * |
|
2302 mxCreateCellArray (int ndims, const int *dims) |
|
2303 { |
6065
|
2304 return maybe_mark_array (new mxArray (ndims, dims)); |
5900
|
2305 } |
|
2306 |
|
2307 mxArray * |
|
2308 mxCreateCellMatrix (int m, int n) |
|
2309 { |
6065
|
2310 return maybe_mark_array (new mxArray (m, n)); |
5900
|
2311 } |
|
2312 |
|
2313 mxArray * |
|
2314 mxCreateCharArray (int ndims, const int *dims) |
|
2315 { |
6065
|
2316 return maybe_mark_array (new mxArray (mxCHAR_CLASS, ndims, dims)); |
5864
|
2317 } |
|
2318 |
5900
|
2319 mxArray * |
|
2320 mxCreateCharMatrixFromStrings (int m, const char **str) |
|
2321 { |
6065
|
2322 return maybe_mark_array (new mxArray (m, str)); |
5900
|
2323 } |
|
2324 |
|
2325 mxArray * |
|
2326 mxCreateDoubleMatrix (int m, int n, mxComplexity flag) |
|
2327 { |
6065
|
2328 return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, m, n, flag)); |
5900
|
2329 } |
|
2330 |
|
2331 mxArray * |
|
2332 mxCreateDoubleScalar (double val) |
|
2333 { |
6065
|
2334 return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, val)); |
5900
|
2335 } |
|
2336 |
|
2337 mxArray * |
|
2338 mxCreateLogicalArray (int ndims, const int *dims) |
5864
|
2339 { |
6065
|
2340 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, ndims, dims)); |
5900
|
2341 } |
|
2342 |
|
2343 mxArray * |
|
2344 mxCreateLogicalMatrix (int m, int n) |
|
2345 { |
6065
|
2346 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, m, n)); |
5900
|
2347 } |
|
2348 |
|
2349 mxArray * |
|
2350 mxCreateLogicalScalar (int val) |
|
2351 { |
6065
|
2352 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, val)); |
5900
|
2353 } |
|
2354 |
|
2355 mxArray * |
|
2356 mxCreateNumericArray (int ndims, const int *dims, mxClassID class_id, |
|
2357 mxComplexity flag) |
|
2358 { |
6065
|
2359 return maybe_mark_array (new mxArray (class_id, ndims, dims, flag)); |
5864
|
2360 } |
|
2361 |
5900
|
2362 mxArray * |
|
2363 mxCreateNumericMatrix (int m, int n, mxClassID class_id, mxComplexity flag) |
|
2364 { |
6065
|
2365 return maybe_mark_array (new mxArray (class_id, m, n, flag)); |
5900
|
2366 } |
|
2367 |
|
2368 mxArray * |
|
2369 mxCreateSparse (int m, int n, int nzmax, mxComplexity flag) |
|
2370 { |
6065
|
2371 return maybe_mark_array (new mxArray (mxDOUBLE_CLASS, m, n, nzmax, flag)); |
5900
|
2372 } |
|
2373 |
|
2374 mxArray * |
|
2375 mxCreateSparseLogicalMatrix (int m, int n, int nzmax) |
|
2376 { |
6065
|
2377 return maybe_mark_array (new mxArray (mxLOGICAL_CLASS, m, n, nzmax)); |
5900
|
2378 } |
|
2379 |
|
2380 mxArray * |
|
2381 mxCreateString (const char *str) |
|
2382 { |
6065
|
2383 return maybe_mark_array (new mxArray (str)); |
5900
|
2384 } |
|
2385 |
|
2386 mxArray * |
|
2387 mxCreateStructArray (int ndims, int *dims, int num_keys, const char **keys) |
|
2388 { |
6065
|
2389 return maybe_mark_array (new mxArray (ndims, dims, num_keys, keys)); |
5900
|
2390 } |
5864
|
2391 |
|
2392 mxArray * |
5900
|
2393 mxCreateStructMatrix (int m, int n, int num_keys, const char **keys) |
|
2394 { |
6065
|
2395 return maybe_mark_array (new mxArray (m, n, num_keys, keys)); |
5900
|
2396 } |
|
2397 |
|
2398 // Copy constructor. |
|
2399 mxArray * |
|
2400 mxDuplicateArray (const mxArray *ptr) |
|
2401 { |
6065
|
2402 return maybe_mark_array (ptr->clone ()); |
5900
|
2403 } |
|
2404 |
|
2405 // Destructor. |
|
2406 void |
|
2407 mxDestroyArray (mxArray *ptr) |
|
2408 { |
6065
|
2409 if (! (mex_context && mex_context->free_value (ptr))) |
|
2410 delete ptr; |
5900
|
2411 } |
|
2412 |
|
2413 // Type Predicates. |
|
2414 int |
|
2415 mxIsCell (const mxArray *ptr) |
|
2416 { |
|
2417 return ptr->is_cell (); |
|
2418 } |
|
2419 |
|
2420 int |
|
2421 mxIsChar (const mxArray *ptr) |
|
2422 { |
|
2423 return ptr->is_char (); |
|
2424 } |
|
2425 |
|
2426 int |
|
2427 mxIsClass (const mxArray *ptr, const char *name) |
|
2428 { |
|
2429 return ptr->is_class (name); |
|
2430 } |
|
2431 |
|
2432 int |
|
2433 mxIsComplex (const mxArray *ptr) |
|
2434 { |
|
2435 return ptr->is_complex (); |
|
2436 } |
|
2437 |
|
2438 int |
|
2439 mxIsDouble (const mxArray *ptr) |
|
2440 { |
|
2441 return ptr->is_double (); |
|
2442 } |
|
2443 |
|
2444 int |
|
2445 mxIsInt16 (const mxArray *ptr) |
|
2446 { |
|
2447 return ptr->is_int16 (); |
|
2448 } |
|
2449 |
|
2450 int |
|
2451 mxIsInt32 (const mxArray *ptr) |
|
2452 { |
|
2453 return ptr->is_int32 (); |
|
2454 } |
|
2455 |
|
2456 int |
|
2457 mxIsInt64 (const mxArray *ptr) |
|
2458 { |
|
2459 return ptr->is_int64 (); |
|
2460 } |
|
2461 |
|
2462 int |
|
2463 mxIsInt8 (const mxArray *ptr) |
|
2464 { |
|
2465 return ptr->is_int8 (); |
|
2466 } |
|
2467 |
|
2468 int |
|
2469 mxIsLogical (const mxArray *ptr) |
|
2470 { |
|
2471 return ptr->is_logical (); |
|
2472 } |
|
2473 |
|
2474 int |
|
2475 mxIsNumeric (const mxArray *ptr) |
|
2476 { |
|
2477 return ptr->is_numeric (); |
|
2478 } |
|
2479 |
|
2480 int |
|
2481 mxIsSingle (const mxArray *ptr) |
|
2482 { |
|
2483 return ptr->is_single (); |
|
2484 } |
|
2485 |
|
2486 int |
|
2487 mxIsSparse (const mxArray *ptr) |
|
2488 { |
|
2489 return ptr->is_sparse (); |
|
2490 } |
|
2491 |
|
2492 int |
|
2493 mxIsStruct (const mxArray *ptr) |
|
2494 { |
|
2495 return ptr->is_struct (); |
|
2496 } |
|
2497 |
|
2498 int |
|
2499 mxIsUint16 (const mxArray *ptr) |
|
2500 { |
|
2501 return ptr->is_uint16 (); |
|
2502 } |
|
2503 |
|
2504 int |
|
2505 mxIsUint32 (const mxArray *ptr) |
|
2506 { |
|
2507 return ptr->is_uint32 (); |
|
2508 } |
|
2509 |
|
2510 int |
|
2511 mxIsUint64 (const mxArray *ptr) |
|
2512 { |
|
2513 return ptr->is_uint64 (); |
|
2514 } |
|
2515 |
|
2516 int |
|
2517 mxIsUint8 (const mxArray *ptr) |
|
2518 { |
|
2519 return ptr->is_uint8 (); |
|
2520 } |
|
2521 |
|
2522 // Odd type+size predicate. |
|
2523 int |
|
2524 mxIsLogicalScalar (const mxArray *ptr) |
|
2525 { |
|
2526 return ptr->is_logical_scalar (); |
|
2527 } |
|
2528 |
|
2529 // Odd type+size+value predicate. |
|
2530 int |
|
2531 mxIsLogicalScalarTrue (const mxArray *ptr) |
|
2532 { |
|
2533 return ptr->is_logical_scalar_true (); |
|
2534 } |
|
2535 |
|
2536 // Size predicate. |
|
2537 int |
|
2538 mxIsEmpty (const mxArray *ptr) |
|
2539 { |
|
2540 return ptr->is_empty (); |
|
2541 } |
|
2542 |
|
2543 // Just plain odd thing to ask of a value. |
|
2544 int |
|
2545 mxIsFromGlobalWS (const mxArray */*ptr*/) |
|
2546 { |
|
2547 // FIXME |
|
2548 abort (); |
|
2549 return 0; |
|
2550 } |
|
2551 |
|
2552 // Dimension extractors. |
|
2553 int |
|
2554 mxGetM (const mxArray *ptr) |
|
2555 { |
|
2556 return ptr->get_m (); |
|
2557 } |
|
2558 |
|
2559 int |
|
2560 mxGetN (const mxArray *ptr) |
|
2561 { |
|
2562 return ptr->get_n (); |
|
2563 } |
|
2564 |
|
2565 int * |
|
2566 mxGetDimensions (const mxArray *ptr) |
5864
|
2567 { |
5900
|
2568 return ptr->get_dimensions (); |
|
2569 } |
|
2570 |
|
2571 int |
|
2572 mxGetNumberOfDimensions (const mxArray *ptr) |
|
2573 { |
|
2574 return ptr->get_number_of_dimensions (); |
|
2575 } |
|
2576 |
|
2577 int |
|
2578 mxGetNumberOfElements (const mxArray *ptr) |
|
2579 { |
|
2580 return ptr->get_number_of_elements (); |
|
2581 } |
|
2582 |
|
2583 // Dimension setters. |
|
2584 void |
|
2585 mxSetM (mxArray *ptr, int m) |
|
2586 { |
|
2587 ptr->set_m (m); |
|
2588 } |
|
2589 |
|
2590 void |
|
2591 mxSetN (mxArray *ptr, int n) |
|
2592 { |
|
2593 ptr->set_n (n); |
|
2594 } |
|
2595 |
|
2596 void |
|
2597 mxSetDimensions (mxArray *ptr, int *dims, int ndims) |
|
2598 { |
|
2599 ptr->set_dimensions (dims, ndims); |
|
2600 } |
|
2601 |
|
2602 // Data extractors. |
|
2603 double * |
|
2604 mxGetPr (const mxArray *ptr) |
|
2605 { |
|
2606 return static_cast<double *> (ptr->get_data ()); |
|
2607 } |
|
2608 |
|
2609 double * |
|
2610 mxGetPi (const mxArray *ptr) |
|
2611 { |
|
2612 return static_cast<double *> (ptr->get_imag_data ()); |
|
2613 } |
|
2614 |
|
2615 double |
|
2616 mxGetScalar (const mxArray *ptr) |
|
2617 { |
|
2618 double *d = mxGetPr (ptr); |
|
2619 return d[0]; |
|
2620 } |
|
2621 |
|
2622 mxChar * |
|
2623 mxGetChars (const mxArray *ptr) |
|
2624 { |
|
2625 return static_cast<mxChar *> (ptr->get_data ()); |
|
2626 } |
|
2627 |
|
2628 mxLogical * |
|
2629 mxGetLogicals (const mxArray *ptr) |
|
2630 { |
|
2631 return static_cast<mxLogical *> (ptr->get_data ()); |
|
2632 } |
|
2633 |
|
2634 void * |
|
2635 mxGetData (const mxArray *ptr) |
|
2636 { |
|
2637 return ptr->get_data (); |
|
2638 } |
|
2639 |
|
2640 void * |
|
2641 mxGetImagData (const mxArray *ptr) |
|
2642 { |
|
2643 return ptr->get_imag_data (); |
|
2644 } |
|
2645 |
|
2646 // Data setters. |
|
2647 void |
|
2648 mxSetPr (mxArray *ptr, double *pr) |
|
2649 { |
|
2650 ptr->set_data (pr); |
|
2651 } |
|
2652 |
|
2653 void |
|
2654 mxSetPi (mxArray *ptr, double *pi) |
|
2655 { |
|
2656 ptr->set_imag_data (pi); |
5864
|
2657 } |
|
2658 |
5900
|
2659 void |
|
2660 mxSetData (mxArray *ptr, void *pr) |
|
2661 { |
|
2662 ptr->set_data (pr); |
|
2663 } |
|
2664 |
|
2665 void |
|
2666 mxSetImagData (mxArray *ptr, void *pi) |
|
2667 { |
|
2668 ptr->set_imag_data (pi); |
|
2669 } |
|
2670 |
|
2671 // Classes. |
|
2672 mxClassID |
|
2673 mxGetClassID (const mxArray *ptr) |
|
2674 { |
|
2675 return ptr->get_class_id (); |
|
2676 } |
|
2677 |
|
2678 const char * |
|
2679 mxGetClassName (const mxArray *ptr) |
|
2680 { |
|
2681 return ptr->get_class_name (); |
|
2682 } |
|
2683 |
|
2684 void |
|
2685 mxSetClassName (mxArray *ptr, const char *name) |
|
2686 { |
|
2687 ptr->set_class_name (name); |
|
2688 } |
|
2689 |
|
2690 // Cell support. |
|
2691 mxArray * |
|
2692 mxGetCell (const mxArray *ptr, int idx) |
|
2693 { |
|
2694 return ptr->get_cell (idx); |
|
2695 } |
|
2696 |
|
2697 void |
|
2698 mxSetCell (mxArray *ptr, int idx, mxArray *val) |
|
2699 { |
|
2700 ptr->set_cell (idx, val); |
|
2701 } |
|
2702 |
|
2703 // Sparse support. |
|
2704 int * |
|
2705 mxGetIr (const mxArray *ptr) |
|
2706 { |
|
2707 return ptr->get_ir (); |
|
2708 } |
|
2709 |
|
2710 int * |
|
2711 mxGetJc (const mxArray *ptr) |
|
2712 { |
|
2713 return ptr->get_jc (); |
|
2714 } |
|
2715 |
|
2716 int |
|
2717 mxGetNzmax (const mxArray *ptr) |
|
2718 { |
|
2719 return ptr->get_nzmax (); |
|
2720 } |
|
2721 |
|
2722 void |
|
2723 mxSetIr (mxArray *ptr, int *ir) |
|
2724 { |
|
2725 ptr->set_ir (ir); |
|
2726 } |
|
2727 |
|
2728 void |
|
2729 mxSetJc (mxArray *ptr, int *jc) |
|
2730 { |
|
2731 ptr->set_jc (jc); |
|
2732 } |
|
2733 |
|
2734 void |
|
2735 mxSetNzmax (mxArray *ptr, int nzmax) |
|
2736 { |
|
2737 ptr->set_nzmax (nzmax); |
|
2738 } |
|
2739 |
|
2740 // Structure support. |
|
2741 int |
|
2742 mxAddField (mxArray *ptr, const char *key) |
|
2743 { |
|
2744 return ptr->add_field (key); |
|
2745 } |
|
2746 |
|
2747 void |
|
2748 mxRemoveField (mxArray *ptr, int key_num) |
|
2749 { |
|
2750 ptr->remove_field (key_num); |
|
2751 } |
5864
|
2752 |
|
2753 mxArray * |
5900
|
2754 mxGetField (const mxArray *ptr, int index, const char *key) |
|
2755 { |
|
2756 int key_num = mxGetFieldNumber (ptr, key); |
|
2757 return mxGetFieldByNumber (ptr, index, key_num); |
|
2758 } |
|
2759 |
|
2760 mxArray * |
|
2761 mxGetFieldByNumber (const mxArray *ptr, int index, int key_num) |
5864
|
2762 { |
5900
|
2763 return ptr->get_field_by_number (index, key_num); |
5864
|
2764 } |
|
2765 |
5900
|
2766 void |
|
2767 mxSetField (mxArray *ptr, int index, const char *key, mxArray *val) |
|
2768 { |
|
2769 int key_num = mxGetFieldNumber (ptr, key); |
|
2770 mxSetFieldByNumber (ptr, index, key_num, val); |
|
2771 } |
5864
|
2772 |
|
2773 void |
5900
|
2774 mxSetFieldByNumber (mxArray *ptr, int index, int key_num, mxArray *val) |
5864
|
2775 { |
5900
|
2776 ptr->set_field_by_number (index, key_num, val); |
|
2777 } |
|
2778 |
|
2779 int |
|
2780 mxGetNumberOfFields (const mxArray *ptr) |
|
2781 { |
|
2782 return ptr->get_number_of_fields (); |
5864
|
2783 } |
|
2784 |
5900
|
2785 const char * |
|
2786 mxGetFieldNameByNumber (const mxArray *ptr, int key_num) |
5864
|
2787 { |
5900
|
2788 return ptr->get_field_name_by_number (key_num); |
|
2789 } |
|
2790 |
|
2791 int |
|
2792 mxGetFieldNumber (const mxArray *ptr, const char *key) |
|
2793 { |
|
2794 return ptr->get_field_number (key); |
5864
|
2795 } |
|
2796 |
5900
|
2797 int |
|
2798 mxGetString (const mxArray *ptr, char *buf, int buflen) |
|
2799 { |
|
2800 return ptr->get_string (buf, buflen); |
|
2801 } |
|
2802 |
|
2803 char * |
|
2804 mxArrayToString (const mxArray *ptr) |
5864
|
2805 { |
5900
|
2806 return ptr->array_to_string (); |
|
2807 } |
|
2808 |
|
2809 int |
|
2810 mxCalcSingleSubscript (const mxArray *ptr, int nsubs, int *subs) |
|
2811 { |
|
2812 return ptr->calc_single_subscript (nsubs, subs); |
5864
|
2813 } |
5900
|
2814 |
|
2815 int |
|
2816 mxGetElementSize (const mxArray *ptr) |
|
2817 { |
|
2818 return ptr->get_element_size (); |
|
2819 } |
|
2820 |
|
2821 // ------------------------------------------------------------------ |
5864
|
2822 |
|
2823 typedef void (*cmex_fptr) (int nlhs, mxArray **plhs, int nrhs, mxArray **prhs); |
|
2824 typedef F77_RET_T (*fmex_fptr) (int& nlhs, mxArray **plhs, int& nrhs, mxArray **prhs); |
|
2825 |
|
2826 octave_value_list |
6068
|
2827 call_mex (bool have_fmex, void *f, const octave_value_list& args, |
|
2828 int nargout, octave_mex_function *curr_mex_fcn) |
5864
|
2829 { |
5900
|
2830 // Use at least 1 for nargout since even for zero specified args, |
|
2831 // still want to be able to return an ans. |
5864
|
2832 |
|
2833 int nargin = args.length (); |
5900
|
2834 OCTAVE_LOCAL_BUFFER (mxArray *, argin, nargin); |
5864
|
2835 for (int i = 0; i < nargin; i++) |
|
2836 argin[i] = 0; |
|
2837 |
|
2838 int nout = nargout == 0 ? 1 : nargout; |
5900
|
2839 OCTAVE_LOCAL_BUFFER (mxArray *, argout, nout); |
5864
|
2840 for (int i = 0; i < nout; i++) |
|
2841 argout[i] = 0; |
|
2842 |
5905
|
2843 unwind_protect::begin_frame ("call_mex"); |
|
2844 |
|
2845 // Save old mex pointer. |
|
2846 unwind_protect_ptr (mex_context); |
|
2847 |
6068
|
2848 mex context (curr_mex_fcn); |
5900
|
2849 |
|
2850 unwind_protect::add (mex::cleanup, static_cast<void *> (&context)); |
5864
|
2851 |
|
2852 for (int i = 0; i < nargin; i++) |
|
2853 argin[i] = context.make_value (args(i)); |
|
2854 |
|
2855 if (setjmp (context.jump) == 0) |
|
2856 { |
5900
|
2857 mex_context = &context; |
5864
|
2858 |
6068
|
2859 if (have_fmex) |
5864
|
2860 { |
|
2861 fmex_fptr fcn = FCN_PTR_CAST (fmex_fptr, f); |
|
2862 |
|
2863 int tmp_nargout = nargout; |
|
2864 int tmp_nargin = nargin; |
|
2865 |
|
2866 fcn (tmp_nargout, argout, tmp_nargin, argin); |
|
2867 } |
|
2868 else |
|
2869 { |
|
2870 cmex_fptr fcn = FCN_PTR_CAST (cmex_fptr, f); |
|
2871 |
|
2872 fcn (nargout, argout, nargin, argin); |
|
2873 } |
|
2874 } |
|
2875 |
|
2876 // Convert returned array entries back into octave values. |
|
2877 |
|
2878 octave_value_list retval; |
|
2879 |
|
2880 if (! error_state) |
|
2881 { |
|
2882 if (nargout == 0 && argout[0]) |
|
2883 { |
5900
|
2884 // We have something for ans. |
|
2885 nargout = 1; |
|
2886 } |
|
2887 |
|
2888 retval.resize (nargout); |
|
2889 |
|
2890 for (int i = 0; i < nargout; i++) |
5907
|
2891 retval(i) = mxArray::as_octave_value (argout[i]); |
5864
|
2892 } |
|
2893 |
|
2894 // Clean up mex resources. |
5905
|
2895 unwind_protect::run_frame ("call_mex"); |
5864
|
2896 |
|
2897 return retval; |
|
2898 } |
|
2899 |
|
2900 // C interface to mex functions: |
|
2901 |
|
2902 const char * |
|
2903 mexFunctionName (void) |
|
2904 { |
5900
|
2905 return mex_context ? mex_context->function_name () : "unknown"; |
|
2906 } |
|
2907 |
|
2908 int |
|
2909 mexCallMATLAB (int nargout, mxArray *argout[], int nargin, mxArray *argin[], |
|
2910 const char *fname) |
|
2911 { |
|
2912 octave_value_list args; |
|
2913 |
|
2914 // FIXME -- do we need unwind protect to clean up args? Off hand, I |
|
2915 // would say that this problem is endemic to Octave and we will |
|
2916 // continue to have memory leaks after Ctrl-C until proper exception |
|
2917 // handling is implemented. longjmp() only clears the stack, so any |
|
2918 // class which allocates data on the heap is going to leak. |
|
2919 |
|
2920 args.resize (nargin); |
|
2921 |
|
2922 for (int i = 0; i < nargin; i++) |
5907
|
2923 args(i) = mxArray::as_octave_value (argin[i]); |
5900
|
2924 |
|
2925 octave_value_list retval = feval (fname, args, nargout); |
|
2926 |
|
2927 if (error_state && mex_context->trap_feval_error == 0) |
5864
|
2928 { |
5900
|
2929 // FIXME -- is this the correct way to clean up? abort() is |
|
2930 // going to trigger a long jump, so the normal class destructors |
|
2931 // will not be called. Hopefully this will reduce things to a |
|
2932 // tiny leak. Maybe create a new octave memory tracer type |
|
2933 // which prints a friendly message every time it is |
|
2934 // created/copied/deleted to check this. |
|
2935 |
|
2936 args.resize (0); |
|
2937 retval.resize (0); |
|
2938 mex_context->abort (); |
|
2939 } |
|
2940 |
|
2941 int num_to_copy = retval.length (); |
|
2942 |
|
2943 if (nargout < retval.length ()) |
|
2944 num_to_copy = nargout; |
|
2945 |
|
2946 for (int i = 0; i < num_to_copy; i++) |
|
2947 { |
|
2948 // FIXME -- it would be nice to avoid copying the value here, |
|
2949 // but there is no way to steal memory from a matrix, never mind |
|
2950 // that matrix memory is allocated by new[] and mxArray memory |
|
2951 // is allocated by malloc(). |
|
2952 argout[i] = mex_context->make_value (retval (i)); |
|
2953 } |
|
2954 |
|
2955 while (num_to_copy < nargout) |
|
2956 argout[num_to_copy++] = 0; |
|
2957 |
|
2958 if (error_state) |
|
2959 { |
|
2960 error_state = 0; |
|
2961 return 1; |
5864
|
2962 } |
|
2963 else |
5900
|
2964 return 0; |
|
2965 } |
|
2966 |
|
2967 void |
|
2968 mexSetTrapFlag (int flag) |
|
2969 { |
|
2970 if (mex_context) |
|
2971 mex_context->trap_feval_error = flag; |
|
2972 } |
|
2973 |
|
2974 int |
|
2975 mexEvalString (const char *s) |
|
2976 { |
|
2977 int retval = 0; |
|
2978 |
|
2979 int parse_status; |
|
2980 |
|
2981 octave_value_list ret; |
|
2982 |
|
2983 ret = eval_string (s, false, parse_status, 0); |
|
2984 |
|
2985 if (parse_status || error_state) |
|
2986 { |
|
2987 error_state = 0; |
|
2988 |
|
2989 retval = 1; |
|
2990 } |
5864
|
2991 |
|
2992 return retval; |
|
2993 } |
|
2994 |
|
2995 void |
|
2996 mexErrMsgTxt (const char *s) |
|
2997 { |
|
2998 if (s && strlen (s) > 0) |
5879
|
2999 error ("%s: %s", mexFunctionName (), s); |
5864
|
3000 else |
|
3001 // Just set the error state; don't print msg. |
|
3002 error (""); |
|
3003 |
5900
|
3004 mex_context->abort (); |
5864
|
3005 } |
|
3006 |
5879
|
3007 void |
|
3008 mexErrMsgIdAndTxt (const char *id, const char *s) |
|
3009 { |
|
3010 if (s && strlen (s) > 0) |
|
3011 error_with_id (id, "%s: %s", mexFunctionName (), s); |
|
3012 else |
|
3013 // Just set the error state; don't print msg. |
|
3014 error (""); |
|
3015 |
5900
|
3016 mex_context->abort (); |
5879
|
3017 } |
|
3018 |
|
3019 void |
|
3020 mexWarnMsgTxt (const char *s) |
|
3021 { |
|
3022 warning ("%s", s); |
|
3023 } |
|
3024 |
|
3025 void |
|
3026 mexWarnMsgIdAndTxt (const char *id, const char *s) |
|
3027 { |
|
3028 warning_with_id (id, "%s", s); |
|
3029 } |
5864
|
3030 |
|
3031 void |
|
3032 mexPrintf (const char *fmt, ...) |
|
3033 { |
|
3034 va_list args; |
|
3035 va_start (args, fmt); |
|
3036 octave_vformat (octave_stdout, fmt, args); |
|
3037 va_end (args); |
|
3038 } |
|
3039 |
|
3040 mxArray * |
5879
|
3041 mexGetVariable (const char *space, const char *name) |
5864
|
3042 { |
|
3043 mxArray *retval = 0; |
|
3044 |
|
3045 // FIXME -- this should be in variable.cc, but the correct |
|
3046 // functionality is not exported. Particularly, get_global_value() |
|
3047 // generates an error if the symbol is undefined. |
|
3048 |
|
3049 symbol_record *sr = 0; |
|
3050 |
|
3051 if (! strcmp (space, "global")) |
|
3052 sr = global_sym_tab->lookup (name); |
|
3053 else if (! strcmp (space, "caller")) |
|
3054 sr = curr_sym_tab->lookup (name); |
|
3055 else if (! strcmp (space, "base")) |
5900
|
3056 sr = top_level_sym_tab->lookup (name); |
5864
|
3057 else |
5879
|
3058 mexErrMsgTxt ("mexGetVariable: symbol table does not exist"); |
5864
|
3059 |
|
3060 if (sr) |
|
3061 { |
|
3062 octave_value sr_def = sr->def (); |
|
3063 |
|
3064 if (sr_def.is_defined ()) |
|
3065 { |
5900
|
3066 retval = mex_context->make_value (sr_def); |
|
3067 |
|
3068 retval->set_name (name); |
5864
|
3069 } |
|
3070 } |
|
3071 |
|
3072 return retval; |
|
3073 } |
|
3074 |
5879
|
3075 const mxArray * |
|
3076 mexGetVariablePtr (const char *space, const char *name) |
5864
|
3077 { |
5879
|
3078 return mexGetVariable (space, name); |
5864
|
3079 } |
|
3080 |
5900
|
3081 int |
|
3082 mexPutVariable (const char *space, const char *name, mxArray *ptr) |
5864
|
3083 { |
5900
|
3084 if (! ptr) |
|
3085 return 1; |
|
3086 |
|
3087 if (! name) |
|
3088 return 1; |
|
3089 |
|
3090 if (name[0] == '\0') |
|
3091 name = ptr->get_name (); |
|
3092 |
|
3093 if (! name || name[0] == '\0') |
|
3094 return 1; |
|
3095 |
|
3096 if (! strcmp (space, "global")) |
5907
|
3097 set_global_value (name, mxArray::as_octave_value (ptr)); |
5900
|
3098 else |
|
3099 { |
|
3100 // FIXME -- this belongs in variables.cc. |
|
3101 |
|
3102 symbol_record *sr = 0; |
|
3103 |
|
3104 if (! strcmp (space, "caller")) |
|
3105 sr = curr_sym_tab->lookup (name, true); |
|
3106 else if (! strcmp (space, "base")) |
|
3107 sr = top_level_sym_tab->lookup (name, true); |
|
3108 else |
|
3109 mexErrMsgTxt ("mexPutVariable: symbol table does not exist"); |
|
3110 |
|
3111 if (sr) |
5907
|
3112 sr->define (mxArray::as_octave_value (ptr)); |
5900
|
3113 else |
|
3114 panic_impossible (); |
|
3115 } |
|
3116 |
|
3117 return 0; |
5864
|
3118 } |
|
3119 |
|
3120 void |
5900
|
3121 mexMakeArrayPersistent (mxArray *ptr) |
5864
|
3122 { |
5900
|
3123 if (mex_context) |
|
3124 mex_context->persistent (ptr); |
5864
|
3125 } |
5879
|
3126 |
5864
|
3127 void |
5900
|
3128 mexMakeMemoryPersistent (void *ptr) |
5864
|
3129 { |
5900
|
3130 if (mex_context) |
|
3131 mex_context->persistent (ptr); |
5864
|
3132 } |
|
3133 |
5900
|
3134 int |
6068
|
3135 mexAtExit (void (*f) (void)) |
5864
|
3136 { |
6068
|
3137 if (mex_context) |
|
3138 { |
|
3139 octave_mex_function *curr_mex_fcn = mex_context->current_mex_function (); |
|
3140 |
|
3141 assert (curr_mex_fcn); |
|
3142 |
|
3143 curr_mex_fcn->atexit (f); |
|
3144 } |
|
3145 |
5900
|
3146 return 0; |
5864
|
3147 } |
|
3148 |
5900
|
3149 const mxArray * |
|
3150 mexGet (double /*handle*/, const char */*property*/) |
5864
|
3151 { |
5900
|
3152 // FIXME |
|
3153 error ("mexGet: not implemented"); |
|
3154 return 0; |
5864
|
3155 } |
|
3156 |
5900
|
3157 int |
|
3158 mexIsGlobal (const mxArray *ptr) |
5864
|
3159 { |
5900
|
3160 return mxIsFromGlobalWS (ptr); |
5864
|
3161 } |
|
3162 |
5900
|
3163 int |
|
3164 mexIsLocked (void) |
5864
|
3165 { |
5900
|
3166 int retval = 0; |
|
3167 |
|
3168 if (mex_context) |
|
3169 { |
|
3170 const char *fname = mexFunctionName (); |
|
3171 |
|
3172 retval = mislocked (fname); |
|
3173 } |
|
3174 |
|
3175 return retval; |
5864
|
3176 } |
|
3177 |
5900
|
3178 std::map<std::string,int> mex_lock_count; |
|
3179 |
|
3180 void |
|
3181 mexLock (void) |
5864
|
3182 { |
5900
|
3183 if (mex_context) |
5864
|
3184 { |
5900
|
3185 const char *fname = mexFunctionName (); |
|
3186 |
|
3187 if (mex_lock_count.find (fname) == mex_lock_count.end ()) |
|
3188 mex_lock_count[fname] = 1; |
|
3189 else |
|
3190 mex_lock_count[fname]++; |
|
3191 |
|
3192 mlock (fname); |
5864
|
3193 } |
|
3194 } |
|
3195 |
5900
|
3196 int |
|
3197 mexSet (double /*handle*/, const char */*property*/, mxArray */*val*/) |
|
3198 { |
|
3199 // FIXME |
|
3200 error ("mexSet: not implemented"); |
|
3201 return 0; |
|
3202 } |
|
3203 |
|
3204 void |
|
3205 mexUnlock (void) |
5864
|
3206 { |
5900
|
3207 if (mex_context) |
5864
|
3208 { |
5900
|
3209 const char *fname = mexFunctionName (); |
|
3210 |
5905
|
3211 std::map<std::string,int>::iterator p = mex_lock_count.find (fname); |
|
3212 |
6062
|
3213 if (p != mex_lock_count.end ()) |
5900
|
3214 { |
|
3215 int count = --mex_lock_count[fname]; |
|
3216 |
|
3217 if (count == 0) |
|
3218 { |
|
3219 munlock (fname); |
|
3220 |
5905
|
3221 mex_lock_count.erase (p); |
5900
|
3222 } |
|
3223 } |
5864
|
3224 } |
|
3225 } |