1
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
|
4 2002, 2004, 2005, 2006, 2007 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
1
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
4219
|
28 #include <list> |
|
29 |
6323
|
30 #include "oct-env.h" |
3325
|
31 #include "oct-time.h" |
|
32 #include "file-stat.h" |
707
|
33 |
2492
|
34 #include <defaults.h> |
3325
|
35 |
|
36 #include "defun.h" |
1352
|
37 #include "dynamic-ld.h" |
7336
|
38 #include "ov-fcn.h" |
|
39 #include "ov-dld-fcn.h" |
|
40 #include "ov-mex-fcn.h" |
3325
|
41 #include "parse.h" |
|
42 #include "unwind-prot.h" |
1352
|
43 #include "utils.h" |
707
|
44 #include "variables.h" |
1
|
45 |
5864
|
46 #define STRINGIFY(s) STRINGIFY1(s) |
|
47 #define STRINGIFY1(s) #s |
|
48 |
2894
|
49 class |
3325
|
50 octave_shlib_list |
1664
|
51 { |
2894
|
52 public: |
|
53 |
3325
|
54 static void append (const octave_shlib& shl); |
2894
|
55 |
6063
|
56 static void remove (octave_shlib& shl, octave_shlib::close_hook cl_hook = 0); |
3325
|
57 |
3523
|
58 static void *search (const std::string& fcn_name, octave_shlib& shl, |
3325
|
59 octave_shlib::name_mangler mangler = 0); |
2894
|
60 |
|
61 private: |
|
62 |
3325
|
63 octave_shlib_list (void) { } |
|
64 |
|
65 ~octave_shlib_list (void) { } |
|
66 |
|
67 void do_append (const octave_shlib& shl); |
|
68 |
6063
|
69 void do_remove (octave_shlib& shl, octave_shlib::close_hook cl_hook = 0); |
3325
|
70 |
3523
|
71 void *do_search (const std::string& fcn_name, octave_shlib& shl, |
3325
|
72 octave_shlib::name_mangler mangler = 0); |
|
73 |
|
74 static octave_shlib_list *instance; |
|
75 |
|
76 static bool instance_ok (void); |
|
77 |
|
78 // List of libraries we have loaded. |
4219
|
79 std::list<octave_shlib> lib_list; |
2894
|
80 |
|
81 // No copying! |
|
82 |
3325
|
83 octave_shlib_list (const octave_shlib_list&); |
2894
|
84 |
3325
|
85 octave_shlib_list& operator = (const octave_shlib_list&); |
2894
|
86 }; |
|
87 |
3325
|
88 octave_shlib_list *octave_shlib_list::instance = 0; |
3321
|
89 |
3325
|
90 void |
|
91 octave_shlib_list::do_append (const octave_shlib& shl) |
|
92 { |
4219
|
93 lib_list.push_back (shl); |
1664
|
94 } |
|
95 |
3325
|
96 void |
6063
|
97 octave_shlib_list::do_remove (octave_shlib& shl, |
|
98 octave_shlib::close_hook cl_hook) |
1664
|
99 { |
4219
|
100 for (std::list<octave_shlib>::iterator p = lib_list.begin (); |
|
101 p != lib_list.end (); |
|
102 p++) |
3325
|
103 { |
4219
|
104 if (*p == shl) |
3325
|
105 { |
6063
|
106 shl.close (cl_hook); |
1664
|
107 |
4219
|
108 lib_list.erase (p); |
3033
|
109 |
3325
|
110 break; |
1664
|
111 } |
|
112 } |
|
113 } |
|
114 |
3325
|
115 void * |
3523
|
116 octave_shlib_list::do_search (const std::string& fcn_name, octave_shlib& shl, |
3325
|
117 octave_shlib::name_mangler mangler) |
|
118 { |
|
119 void *function = 0; |
|
120 |
|
121 shl = octave_shlib (); |
1664
|
122 |
4219
|
123 for (std::list<octave_shlib>::iterator p = lib_list.begin (); |
|
124 p != lib_list.end (); |
|
125 p++) |
3325
|
126 { |
4219
|
127 function = p->search (fcn_name, mangler); |
3325
|
128 |
|
129 if (function) |
|
130 { |
4219
|
131 shl = *p; |
3325
|
132 |
|
133 break; |
|
134 } |
|
135 } |
|
136 |
|
137 return function; |
|
138 } |
2926
|
139 |
|
140 bool |
3325
|
141 octave_shlib_list::instance_ok (void) |
2926
|
142 { |
|
143 bool retval = true; |
|
144 |
|
145 if (! instance) |
3325
|
146 instance = new octave_shlib_list (); |
2926
|
147 |
|
148 if (! instance) |
|
149 { |
3325
|
150 ::error ("unable to create shared library list object!"); |
2926
|
151 |
|
152 retval = false; |
|
153 } |
|
154 |
|
155 return retval; |
|
156 } |
|
157 |
|
158 void |
3325
|
159 octave_shlib_list::append (const octave_shlib& shl) |
|
160 { |
|
161 if (instance_ok ()) |
|
162 instance->do_append (shl); |
|
163 } |
|
164 |
|
165 void |
6063
|
166 octave_shlib_list::remove (octave_shlib& shl, |
|
167 octave_shlib::close_hook cl_hook) |
2894
|
168 { |
3325
|
169 if (instance_ok ()) |
6063
|
170 instance->do_remove (shl, cl_hook); |
2894
|
171 } |
|
172 |
3325
|
173 void * |
3523
|
174 octave_shlib_list::search (const std::string& fcn_name, octave_shlib& shl, |
3325
|
175 octave_shlib::name_mangler mangler) |
|
176 { |
|
177 return (instance_ok ()) ? instance->do_search (fcn_name, shl, mangler) : 0; |
|
178 } |
|
179 |
5864
|
180 class |
|
181 octave_mex_file_list |
|
182 { |
|
183 public: |
|
184 |
|
185 static void append (const octave_shlib& shl); |
|
186 |
6063
|
187 static void remove (octave_shlib& shl, octave_shlib::close_hook cl_hook = 0); |
5864
|
188 |
|
189 private: |
|
190 |
|
191 octave_mex_file_list (void) { } |
|
192 |
|
193 ~octave_mex_file_list (void) { } |
|
194 |
|
195 void do_append (const octave_shlib& shl); |
|
196 |
6063
|
197 void do_remove (octave_shlib& shl, octave_shlib::close_hook cl_hook = 0); |
5864
|
198 |
|
199 static octave_mex_file_list *instance; |
|
200 |
|
201 static bool instance_ok (void); |
|
202 |
|
203 // List of libraries we have loaded. |
|
204 std::list<octave_shlib> file_list; |
|
205 |
|
206 // No copying! |
|
207 |
|
208 octave_mex_file_list (const octave_mex_file_list&); |
|
209 |
|
210 octave_mex_file_list& operator = (const octave_mex_file_list&); |
|
211 }; |
|
212 |
|
213 octave_mex_file_list *octave_mex_file_list::instance = 0; |
|
214 |
|
215 void |
|
216 octave_mex_file_list::do_append (const octave_shlib& shl) |
|
217 { |
|
218 file_list.push_back (shl); |
|
219 } |
|
220 |
|
221 void |
6063
|
222 octave_mex_file_list::do_remove (octave_shlib& shl, |
|
223 octave_shlib::close_hook cl_hook) |
5864
|
224 { |
|
225 |
|
226 for (std::list<octave_shlib>::iterator p = file_list.begin (); |
|
227 p != file_list.end (); |
|
228 p++) |
|
229 { |
|
230 if (*p == shl) |
|
231 { |
6063
|
232 shl.close (cl_hook); |
5864
|
233 |
|
234 file_list.erase (p); |
|
235 |
|
236 break; |
|
237 } |
|
238 } |
|
239 } |
|
240 |
|
241 bool |
|
242 octave_mex_file_list::instance_ok (void) |
|
243 { |
|
244 bool retval = true; |
|
245 |
|
246 if (! instance) |
|
247 instance = new octave_mex_file_list (); |
|
248 |
|
249 if (! instance) |
|
250 { |
|
251 ::error ("unable to create shared library list object!"); |
|
252 |
|
253 retval = false; |
|
254 } |
|
255 |
|
256 return retval; |
|
257 } |
|
258 |
|
259 void |
|
260 octave_mex_file_list::append (const octave_shlib& shl) |
|
261 { |
|
262 if (instance_ok ()) |
|
263 instance->do_append (shl); |
|
264 } |
|
265 |
|
266 void |
6063
|
267 octave_mex_file_list::remove (octave_shlib& shl, |
|
268 octave_shlib::close_hook cl_hook) |
5864
|
269 { |
|
270 if (instance_ok ()) |
6063
|
271 instance->do_remove (shl, cl_hook); |
5864
|
272 } |
|
273 |
3325
|
274 octave_dynamic_loader *octave_dynamic_loader::instance = 0; |
|
275 |
|
276 bool octave_dynamic_loader::doing_load = false; |
|
277 |
2969
|
278 bool |
3325
|
279 octave_dynamic_loader::instance_ok (void) |
707
|
280 { |
3325
|
281 bool retval = true; |
1664
|
282 |
3325
|
283 if (! instance) |
|
284 instance = new octave_dynamic_loader (); |
707
|
285 |
3325
|
286 if (! instance) |
707
|
287 { |
3655
|
288 ::error ("unable to create dynamic loader object!"); |
707
|
289 |
3325
|
290 retval = false; |
707
|
291 } |
|
292 |
2893
|
293 return retval; |
|
294 } |
|
295 |
3325
|
296 static |
4954
|
297 void do_clear_function (const std::string& fcn_name) |
3325
|
298 { |
5781
|
299 warning_with_id ("Octave:reload-forces-clear", " %s", fcn_name.c_str ()); |
3325
|
300 |
7336
|
301 symbol_table::clear_user_function (fcn_name); |
3325
|
302 } |
|
303 |
6323
|
304 static void |
|
305 clear (octave_shlib& oct_file) |
|
306 { |
|
307 if (oct_file.number_of_functions_loaded () > 1) |
|
308 warning_with_id ("Octave:reload-forces-clear", |
|
309 "reloading %s clears the following functions:", |
|
310 oct_file.file_name().c_str ()); |
|
311 |
|
312 octave_shlib_list::remove (oct_file, do_clear_function); |
|
313 } |
|
314 |
7336
|
315 octave_function * |
5864
|
316 octave_dynamic_loader::do_load_oct (const std::string& fcn_name, |
6323
|
317 const std::string& file_name, |
|
318 bool relative) |
2893
|
319 { |
7336
|
320 octave_function *retval = 0; |
3325
|
321 |
|
322 octave_shlib oct_file; |
|
323 |
|
324 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
325 |
|
326 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
327 |
|
328 doing_load = true; |
|
329 |
|
330 void *function |
7336
|
331 = octave_shlib_list::search (fcn_name, oct_file, xmangle_name); |
3325
|
332 |
|
333 if (! error_state) |
|
334 { |
6323
|
335 if (function |
|
336 && (! same_file (file_name, oct_file.file_name ()) |
|
337 || oct_file.is_out_of_date ())) |
3325
|
338 { |
6323
|
339 clear (oct_file); |
3325
|
340 function = 0; |
|
341 } |
|
342 |
|
343 if (! function) |
|
344 { |
6323
|
345 std::string oct_file_name = file_name; |
|
346 |
|
347 if (oct_file_name.empty ()) |
|
348 { |
|
349 oct_file_name = oct_file_in_path (fcn_name); |
|
350 |
|
351 if (! oct_file_name.empty ()) |
|
352 relative = ! octave_env::absolute_pathname (oct_file_name); |
|
353 } |
3325
|
354 |
6323
|
355 if (oct_file_name.empty ()) |
|
356 { |
|
357 if (oct_file.is_relative ()) |
|
358 { |
|
359 // Can't see this function from current |
|
360 // directory, so we should clear it. |
|
361 clear (oct_file); |
|
362 function = 0; |
|
363 } |
|
364 } |
|
365 else |
3325
|
366 { |
5781
|
367 oct_file.open (oct_file_name); |
3325
|
368 |
|
369 if (! error_state) |
|
370 { |
|
371 if (oct_file) |
|
372 { |
6323
|
373 if (relative) |
|
374 oct_file.mark_relative (); |
|
375 |
3325
|
376 octave_shlib_list::append (oct_file); |
|
377 |
7336
|
378 function = oct_file.search (fcn_name, xmangle_name); |
3325
|
379 } |
|
380 else |
3655
|
381 ::error ("%s is not a valid shared library", |
|
382 oct_file_name.c_str ()); |
3325
|
383 } |
|
384 } |
|
385 } |
|
386 } |
|
387 |
|
388 if (function) |
|
389 { |
7336
|
390 octave_dld_fcn_getter f |
|
391 = FCN_PTR_CAST (octave_dld_fcn_getter, function); |
3325
|
392 |
6323
|
393 retval = f (oct_file, relative); |
3656
|
394 |
|
395 if (! retval) |
5864
|
396 ::error ("failed to install .oct file function `%s'", |
|
397 fcn_name.c_str ()); |
3325
|
398 } |
3655
|
399 |
3325
|
400 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
401 |
|
402 return retval; |
|
403 } |
|
404 |
7336
|
405 octave_function * |
5864
|
406 octave_dynamic_loader::do_load_mex (const std::string& fcn_name, |
6323
|
407 const std::string& file_name, |
|
408 bool relative) |
5864
|
409 { |
7336
|
410 octave_function *retval = 0; |
5864
|
411 |
|
412 octave_shlib mex_file; |
|
413 |
|
414 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
415 |
|
416 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
417 |
|
418 doing_load = true; |
|
419 |
6323
|
420 std::string mex_file_name = file_name; |
|
421 |
|
422 if (mex_file_name.empty ()) |
|
423 { |
|
424 mex_file_name = mex_file_in_path (fcn_name); |
|
425 |
|
426 if (! mex_file_name.empty ()) |
|
427 relative = ! octave_env::absolute_pathname (mex_file_name); |
|
428 } |
5864
|
429 |
|
430 void *function = 0; |
|
431 |
|
432 bool have_fmex = false; |
|
433 |
|
434 if (! mex_file_name.empty ()) |
|
435 { |
|
436 mex_file.open (mex_file_name); |
|
437 |
|
438 if (! error_state) |
|
439 { |
|
440 if (mex_file) |
|
441 { |
|
442 octave_mex_file_list::append (mex_file); |
|
443 |
|
444 function = mex_file.search ("mexFunction"); |
|
445 |
|
446 if (! function) |
|
447 { |
6221
|
448 // FIXME -- can we determine this C mangling scheme |
|
449 // automatically at run time or configure time? |
|
450 |
|
451 function = mex_file.search ("_mexFunction"); |
|
452 |
|
453 if (! function) |
|
454 { |
|
455 function = mex_file.search (STRINGIFY (F77_FUNC (mexfunction, MEXFUNCTION))); |
|
456 if (function) |
|
457 have_fmex = true; |
|
458 } |
5864
|
459 } |
|
460 } |
|
461 else |
|
462 ::error ("%s is not a valid shared library", |
|
463 mex_file_name.c_str ()); |
|
464 } |
|
465 } |
|
466 |
|
467 if (function) |
7336
|
468 retval = new octave_mex_function (function, have_fmex, mex_file, fcn_name); |
5864
|
469 else |
|
470 ::error ("failed to install .mex file function `%s'", fcn_name.c_str ()); |
|
471 |
|
472 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
473 |
|
474 return retval; |
|
475 } |
|
476 |
|
477 bool |
3523
|
478 octave_dynamic_loader::do_remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
479 { |
|
480 bool retval = false; |
|
481 |
|
482 // We don't need to do anything if this is called because we are in |
|
483 // the process of reloading a .oct file that has changed. |
|
484 |
|
485 if (! doing_load) |
|
486 { |
|
487 retval = shl.remove (fcn_name); |
|
488 |
|
489 if (shl.number_of_functions_loaded () == 0) |
|
490 octave_shlib_list::remove (shl); |
|
491 } |
|
492 |
|
493 return retval; |
|
494 } |
|
495 |
7336
|
496 octave_function * |
5864
|
497 octave_dynamic_loader::load_oct (const std::string& fcn_name, |
7336
|
498 const std::string& file_name, |
|
499 bool relative) |
3325
|
500 { |
6323
|
501 return (instance_ok ()) |
7336
|
502 ? instance->do_load_oct (fcn_name, file_name, relative) : 0; |
5864
|
503 } |
|
504 |
7336
|
505 octave_function * |
5864
|
506 octave_dynamic_loader::load_mex (const std::string& fcn_name, |
7336
|
507 const std::string& file_name, |
|
508 bool relative) |
5864
|
509 { |
6323
|
510 return (instance_ok ()) |
7336
|
511 ? instance->do_load_mex (fcn_name, file_name, relative) : 0; |
3325
|
512 } |
|
513 |
|
514 bool |
3523
|
515 octave_dynamic_loader::remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
516 { |
|
517 return (instance_ok ()) ? instance->do_remove (fcn_name, shl) : false; |
2893
|
518 } |
707
|
519 |
3536
|
520 std::string |
3523
|
521 octave_dynamic_loader::mangle_name (const std::string& name) |
2894
|
522 { |
3222
|
523 #if defined (CXX_PREPENDS_UNDERSCORE) |
3523
|
524 std::string retval ("_FS"); |
3222
|
525 #else |
3523
|
526 std::string retval ("FS"); |
3222
|
527 #endif |
2894
|
528 retval.append (name); |
3842
|
529 retval.append ("_"); |
|
530 retval.append (STRINGIFY (CXX_ABI)); |
2894
|
531 return retval; |
|
532 } |
707
|
533 |
7336
|
534 std::string |
|
535 octave_dynamic_loader::xmangle_name (const std::string& name) |
|
536 { |
|
537 #if defined (CXX_PREPENDS_UNDERSCORE) |
|
538 std::string retval ("_G"); |
|
539 #else |
|
540 std::string retval ("G"); |
|
541 #endif |
|
542 retval.append (name); |
|
543 retval.append ("_"); |
|
544 retval.append (STRINGIFY (CXX_ABI)); |
|
545 return retval; |
|
546 } |
|
547 |
1
|
548 /* |
|
549 ;;; Local Variables: *** |
|
550 ;;; mode: C++ *** |
|
551 ;;; End: *** |
|
552 */ |