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