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