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