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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
4219
|
28 #include <list> |
|
29 |
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 |
|
305 bool |
5864
|
306 octave_dynamic_loader::do_load_oct (const std::string& fcn_name, |
|
307 const std::string& file_name) |
2893
|
308 { |
3325
|
309 bool retval = false; |
|
310 |
|
311 octave_shlib oct_file; |
|
312 |
|
313 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
314 |
|
315 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
316 |
|
317 doing_load = true; |
|
318 |
|
319 void *function |
|
320 = octave_shlib_list::search (fcn_name, oct_file, mangle_name); |
|
321 |
|
322 if (! error_state) |
|
323 { |
|
324 if (function && oct_file.is_out_of_date ()) |
|
325 { |
|
326 int n = oct_file.number_of_functions_loaded (); |
|
327 |
5781
|
328 if (n > 0) |
|
329 warning_with_id ("Octave:reload-forces-clear", |
|
330 "reloading %s clears the following functions:", |
|
331 oct_file.file_name().c_str ()); |
3325
|
332 |
6063
|
333 octave_shlib_list::remove (oct_file, do_clear_function); |
3325
|
334 |
|
335 function = 0; |
|
336 } |
|
337 |
|
338 if (! function) |
|
339 { |
4243
|
340 std::string oct_file_name |
|
341 = file_name.empty () ? oct_file_in_path (fcn_name) : file_name; |
3325
|
342 |
|
343 if (! oct_file_name.empty ()) |
|
344 { |
5781
|
345 oct_file.open (oct_file_name); |
3325
|
346 |
|
347 if (! error_state) |
|
348 { |
|
349 if (oct_file) |
|
350 { |
|
351 octave_shlib_list::append (oct_file); |
|
352 |
|
353 function = oct_file.search (fcn_name, mangle_name); |
|
354 } |
|
355 else |
3655
|
356 ::error ("%s is not a valid shared library", |
|
357 oct_file_name.c_str ()); |
3325
|
358 } |
|
359 } |
5864
|
360 |
3325
|
361 } |
|
362 } |
|
363 |
|
364 if (function) |
|
365 { |
|
366 octave_dld_fcn_installer f |
5854
|
367 = FCN_PTR_CAST (octave_dld_fcn_installer, function); |
3325
|
368 |
|
369 retval = f (oct_file); |
3656
|
370 |
|
371 if (! retval) |
5864
|
372 ::error ("failed to install .oct file function `%s'", |
|
373 fcn_name.c_str ()); |
3325
|
374 } |
3655
|
375 |
3325
|
376 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
377 |
|
378 return retval; |
|
379 } |
|
380 |
|
381 bool |
5864
|
382 octave_dynamic_loader::do_load_mex (const std::string& fcn_name, |
|
383 const std::string& file_name) |
|
384 { |
|
385 bool retval = false; |
|
386 |
|
387 octave_shlib mex_file; |
|
388 |
|
389 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
390 |
|
391 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
392 |
|
393 doing_load = true; |
|
394 |
|
395 std::string mex_file_name |
|
396 = file_name.empty () ? mex_file_in_path (fcn_name) : file_name; |
|
397 |
|
398 void *function = 0; |
|
399 |
|
400 bool have_fmex = false; |
|
401 |
|
402 if (! mex_file_name.empty ()) |
|
403 { |
|
404 mex_file.open (mex_file_name); |
|
405 |
|
406 if (! error_state) |
|
407 { |
|
408 if (mex_file) |
|
409 { |
|
410 octave_mex_file_list::append (mex_file); |
|
411 |
|
412 function = mex_file.search ("mexFunction"); |
|
413 |
|
414 if (! function) |
|
415 { |
6221
|
416 // FIXME -- can we determine this C mangling scheme |
|
417 // automatically at run time or configure time? |
|
418 |
|
419 function = mex_file.search ("_mexFunction"); |
|
420 |
|
421 if (! function) |
|
422 { |
|
423 function = mex_file.search (STRINGIFY (F77_FUNC (mexfunction, MEXFUNCTION))); |
|
424 if (function) |
|
425 have_fmex = true; |
|
426 } |
5864
|
427 } |
|
428 } |
|
429 else |
|
430 ::error ("%s is not a valid shared library", |
|
431 mex_file_name.c_str ()); |
|
432 } |
|
433 } |
|
434 |
|
435 if (function) |
|
436 { |
|
437 install_mex_function (function, have_fmex, fcn_name, mex_file); |
|
438 |
|
439 retval = true; |
|
440 } |
|
441 else |
|
442 ::error ("failed to install .mex file function `%s'", fcn_name.c_str ()); |
|
443 |
|
444 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
445 |
|
446 return retval; |
|
447 } |
|
448 |
|
449 bool |
3523
|
450 octave_dynamic_loader::do_remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
451 { |
|
452 bool retval = false; |
|
453 |
|
454 // We don't need to do anything if this is called because we are in |
|
455 // the process of reloading a .oct file that has changed. |
|
456 |
|
457 if (! doing_load) |
|
458 { |
|
459 retval = shl.remove (fcn_name); |
|
460 |
|
461 if (shl.number_of_functions_loaded () == 0) |
|
462 octave_shlib_list::remove (shl); |
|
463 } |
|
464 |
|
465 return retval; |
|
466 } |
|
467 |
|
468 bool |
5864
|
469 octave_dynamic_loader::load_oct (const std::string& fcn_name, |
|
470 const std::string& file_name) |
3325
|
471 { |
5864
|
472 return (instance_ok ()) ? instance->do_load_oct (fcn_name, file_name) : false; |
|
473 } |
|
474 |
|
475 bool |
|
476 octave_dynamic_loader::load_mex (const std::string& fcn_name, |
|
477 const std::string& file_name) |
|
478 { |
|
479 return (instance_ok ()) ? instance->do_load_mex (fcn_name, file_name) : false; |
3325
|
480 } |
|
481 |
|
482 bool |
3523
|
483 octave_dynamic_loader::remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
484 { |
|
485 return (instance_ok ()) ? instance->do_remove (fcn_name, shl) : false; |
2893
|
486 } |
707
|
487 |
3536
|
488 std::string |
3523
|
489 octave_dynamic_loader::mangle_name (const std::string& name) |
2894
|
490 { |
3222
|
491 #if defined (CXX_PREPENDS_UNDERSCORE) |
3523
|
492 std::string retval ("_FS"); |
3222
|
493 #else |
3523
|
494 std::string retval ("FS"); |
3222
|
495 #endif |
2894
|
496 retval.append (name); |
3842
|
497 retval.append ("_"); |
|
498 retval.append (STRINGIFY (CXX_ABI)); |
2894
|
499 return retval; |
|
500 } |
707
|
501 |
1
|
502 /* |
|
503 ;;; Local Variables: *** |
|
504 ;;; mode: C++ *** |
|
505 ;;; End: *** |
|
506 */ |