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 |
3325
|
52 static void remove (octave_shlib& shl); |
|
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 |
|
65 void do_remove (octave_shlib& shl); |
|
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 |
|
93 octave_shlib_list::do_remove (octave_shlib& shl) |
1664
|
94 { |
4219
|
95 |
|
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 { |
|
102 shl.close (); |
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 |
|
162 octave_shlib_list::remove (octave_shlib& shl) |
2894
|
163 { |
3325
|
164 if (instance_ok ()) |
|
165 instance->do_remove (shl); |
2894
|
166 } |
|
167 |
3325
|
168 void * |
3523
|
169 octave_shlib_list::search (const std::string& fcn_name, octave_shlib& shl, |
3325
|
170 octave_shlib::name_mangler mangler) |
|
171 { |
|
172 return (instance_ok ()) ? instance->do_search (fcn_name, shl, mangler) : 0; |
|
173 } |
|
174 |
5864
|
175 class |
|
176 octave_mex_file_list |
|
177 { |
|
178 public: |
|
179 |
|
180 static void append (const octave_shlib& shl); |
|
181 |
|
182 static void remove (octave_shlib& shl); |
|
183 |
|
184 private: |
|
185 |
|
186 octave_mex_file_list (void) { } |
|
187 |
|
188 ~octave_mex_file_list (void) { } |
|
189 |
|
190 void do_append (const octave_shlib& shl); |
|
191 |
|
192 void do_remove (octave_shlib& shl); |
|
193 |
|
194 static octave_mex_file_list *instance; |
|
195 |
|
196 static bool instance_ok (void); |
|
197 |
|
198 // List of libraries we have loaded. |
|
199 std::list<octave_shlib> file_list; |
|
200 |
|
201 // No copying! |
|
202 |
|
203 octave_mex_file_list (const octave_mex_file_list&); |
|
204 |
|
205 octave_mex_file_list& operator = (const octave_mex_file_list&); |
|
206 }; |
|
207 |
|
208 octave_mex_file_list *octave_mex_file_list::instance = 0; |
|
209 |
|
210 void |
|
211 octave_mex_file_list::do_append (const octave_shlib& shl) |
|
212 { |
|
213 file_list.push_back (shl); |
|
214 } |
|
215 |
|
216 void |
|
217 octave_mex_file_list::do_remove (octave_shlib& shl) |
|
218 { |
|
219 |
|
220 for (std::list<octave_shlib>::iterator p = file_list.begin (); |
|
221 p != file_list.end (); |
|
222 p++) |
|
223 { |
|
224 if (*p == shl) |
|
225 { |
|
226 shl.close (); |
|
227 |
|
228 file_list.erase (p); |
|
229 |
|
230 break; |
|
231 } |
|
232 } |
|
233 } |
|
234 |
|
235 bool |
|
236 octave_mex_file_list::instance_ok (void) |
|
237 { |
|
238 bool retval = true; |
|
239 |
|
240 if (! instance) |
|
241 instance = new octave_mex_file_list (); |
|
242 |
|
243 if (! instance) |
|
244 { |
|
245 ::error ("unable to create shared library list object!"); |
|
246 |
|
247 retval = false; |
|
248 } |
|
249 |
|
250 return retval; |
|
251 } |
|
252 |
|
253 void |
|
254 octave_mex_file_list::append (const octave_shlib& shl) |
|
255 { |
|
256 if (instance_ok ()) |
|
257 instance->do_append (shl); |
|
258 } |
|
259 |
|
260 void |
|
261 octave_mex_file_list::remove (octave_shlib& shl) |
|
262 { |
|
263 if (instance_ok ()) |
|
264 instance->do_remove (shl); |
|
265 } |
|
266 |
3325
|
267 octave_dynamic_loader *octave_dynamic_loader::instance = 0; |
|
268 |
|
269 bool octave_dynamic_loader::doing_load = false; |
|
270 |
2969
|
271 bool |
3325
|
272 octave_dynamic_loader::instance_ok (void) |
707
|
273 { |
3325
|
274 bool retval = true; |
1664
|
275 |
3325
|
276 if (! instance) |
|
277 instance = new octave_dynamic_loader (); |
707
|
278 |
3325
|
279 if (! instance) |
707
|
280 { |
3655
|
281 ::error ("unable to create dynamic loader object!"); |
707
|
282 |
3325
|
283 retval = false; |
707
|
284 } |
|
285 |
2893
|
286 return retval; |
|
287 } |
|
288 |
3325
|
289 static |
4954
|
290 void do_clear_function (const std::string& fcn_name) |
3325
|
291 { |
5781
|
292 warning_with_id ("Octave:reload-forces-clear", " %s", fcn_name.c_str ()); |
3325
|
293 |
|
294 curr_sym_tab->clear (fcn_name); |
|
295 |
|
296 if (curr_sym_tab != top_level_sym_tab) |
|
297 top_level_sym_tab->clear (fcn_name); |
|
298 |
4009
|
299 fbi_sym_tab->clear (fcn_name); |
3325
|
300 } |
|
301 |
|
302 bool |
5864
|
303 octave_dynamic_loader::do_load_oct (const std::string& fcn_name, |
|
304 const std::string& file_name) |
2893
|
305 { |
3325
|
306 bool retval = false; |
|
307 |
|
308 octave_shlib oct_file; |
|
309 |
|
310 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
311 |
|
312 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
313 |
|
314 doing_load = true; |
|
315 |
|
316 void *function |
|
317 = octave_shlib_list::search (fcn_name, oct_file, mangle_name); |
|
318 |
|
319 if (! error_state) |
|
320 { |
|
321 if (function && oct_file.is_out_of_date ()) |
|
322 { |
|
323 int n = oct_file.number_of_functions_loaded (); |
|
324 |
5781
|
325 if (n > 0) |
|
326 warning_with_id ("Octave:reload-forces-clear", |
|
327 "reloading %s clears the following functions:", |
|
328 oct_file.file_name().c_str ()); |
3325
|
329 |
4954
|
330 oct_file.close (do_clear_function); |
3325
|
331 |
|
332 function = 0; |
|
333 } |
|
334 |
|
335 if (! function) |
|
336 { |
4243
|
337 std::string oct_file_name |
|
338 = file_name.empty () ? oct_file_in_path (fcn_name) : file_name; |
3325
|
339 |
|
340 if (! oct_file_name.empty ()) |
|
341 { |
5781
|
342 oct_file.open (oct_file_name); |
3325
|
343 |
|
344 if (! error_state) |
|
345 { |
|
346 if (oct_file) |
|
347 { |
|
348 octave_shlib_list::append (oct_file); |
|
349 |
|
350 function = oct_file.search (fcn_name, mangle_name); |
|
351 } |
|
352 else |
3655
|
353 ::error ("%s is not a valid shared library", |
|
354 oct_file_name.c_str ()); |
3325
|
355 } |
|
356 } |
5864
|
357 |
3325
|
358 } |
|
359 } |
|
360 |
|
361 if (function) |
|
362 { |
|
363 octave_dld_fcn_installer f |
5854
|
364 = FCN_PTR_CAST (octave_dld_fcn_installer, function); |
3325
|
365 |
|
366 retval = f (oct_file); |
3656
|
367 |
|
368 if (! retval) |
5864
|
369 ::error ("failed to install .oct file function `%s'", |
|
370 fcn_name.c_str ()); |
3325
|
371 } |
3655
|
372 |
3325
|
373 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
374 |
|
375 return retval; |
|
376 } |
|
377 |
|
378 bool |
5864
|
379 octave_dynamic_loader::do_load_mex (const std::string& fcn_name, |
|
380 const std::string& file_name) |
|
381 { |
|
382 bool retval = false; |
|
383 |
|
384 octave_shlib mex_file; |
|
385 |
|
386 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
387 |
|
388 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
389 |
|
390 doing_load = true; |
|
391 |
|
392 std::string mex_file_name |
|
393 = file_name.empty () ? mex_file_in_path (fcn_name) : file_name; |
|
394 |
|
395 void *function = 0; |
|
396 |
|
397 bool have_fmex = false; |
|
398 |
|
399 if (! mex_file_name.empty ()) |
|
400 { |
|
401 mex_file.open (mex_file_name); |
|
402 |
|
403 if (! error_state) |
|
404 { |
|
405 if (mex_file) |
|
406 { |
|
407 octave_mex_file_list::append (mex_file); |
|
408 |
|
409 function = mex_file.search ("mexFunction"); |
|
410 |
|
411 if (! function) |
|
412 { |
|
413 function = mex_file.search (STRINGIFY (F77_FUNC (mexfunction, MEXFUNCTION))); |
|
414 if (function) |
|
415 have_fmex = true; |
|
416 } |
|
417 } |
|
418 else |
|
419 ::error ("%s is not a valid shared library", |
|
420 mex_file_name.c_str ()); |
|
421 } |
|
422 } |
|
423 |
|
424 if (function) |
|
425 { |
|
426 install_mex_function (function, have_fmex, fcn_name, mex_file); |
|
427 |
|
428 retval = true; |
|
429 } |
|
430 else |
|
431 ::error ("failed to install .mex file function `%s'", fcn_name.c_str ()); |
|
432 |
|
433 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
434 |
|
435 return retval; |
|
436 } |
|
437 |
|
438 bool |
3523
|
439 octave_dynamic_loader::do_remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
440 { |
|
441 bool retval = false; |
|
442 |
|
443 // We don't need to do anything if this is called because we are in |
|
444 // the process of reloading a .oct file that has changed. |
|
445 |
|
446 if (! doing_load) |
|
447 { |
|
448 retval = shl.remove (fcn_name); |
|
449 |
|
450 if (shl.number_of_functions_loaded () == 0) |
|
451 octave_shlib_list::remove (shl); |
|
452 } |
|
453 |
|
454 return retval; |
|
455 } |
|
456 |
|
457 bool |
5864
|
458 octave_dynamic_loader::load_oct (const std::string& fcn_name, |
|
459 const std::string& file_name) |
3325
|
460 { |
5864
|
461 return (instance_ok ()) ? instance->do_load_oct (fcn_name, file_name) : false; |
|
462 } |
|
463 |
|
464 bool |
|
465 octave_dynamic_loader::load_mex (const std::string& fcn_name, |
|
466 const std::string& file_name) |
|
467 { |
|
468 return (instance_ok ()) ? instance->do_load_mex (fcn_name, file_name) : false; |
3325
|
469 } |
|
470 |
|
471 bool |
3523
|
472 octave_dynamic_loader::remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
473 { |
|
474 return (instance_ok ()) ? instance->do_remove (fcn_name, shl) : false; |
2893
|
475 } |
707
|
476 |
3536
|
477 std::string |
3523
|
478 octave_dynamic_loader::mangle_name (const std::string& name) |
2894
|
479 { |
3222
|
480 #if defined (CXX_PREPENDS_UNDERSCORE) |
3523
|
481 std::string retval ("_FS"); |
3222
|
482 #else |
3523
|
483 std::string retval ("FS"); |
3222
|
484 #endif |
2894
|
485 retval.append (name); |
3842
|
486 retval.append ("_"); |
|
487 retval.append (STRINGIFY (CXX_ABI)); |
2894
|
488 return retval; |
|
489 } |
707
|
490 |
1
|
491 /* |
|
492 ;;; Local Variables: *** |
|
493 ;;; mode: C++ *** |
|
494 ;;; End: *** |
|
495 */ |