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