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