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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
4219
|
27 #include <list> |
|
28 |
3325
|
29 #include "oct-time.h" |
|
30 #include "file-stat.h" |
707
|
31 |
2492
|
32 #include <defaults.h> |
3325
|
33 |
|
34 #include "defun.h" |
1352
|
35 #include "dynamic-ld.h" |
3325
|
36 #include "parse.h" |
|
37 #include "unwind-prot.h" |
1352
|
38 #include "utils.h" |
707
|
39 #include "variables.h" |
1
|
40 |
3325
|
41 // TRUE means we print a warning if reloading a .oct file forces other |
|
42 // functions to be cleared. |
|
43 static bool Vwarn_reload_forces_clear; |
|
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 |
|
175 octave_dynamic_loader *octave_dynamic_loader::instance = 0; |
|
176 |
|
177 bool octave_dynamic_loader::doing_load = false; |
|
178 |
2969
|
179 bool |
3325
|
180 octave_dynamic_loader::instance_ok (void) |
707
|
181 { |
3325
|
182 bool retval = true; |
1664
|
183 |
3325
|
184 if (! instance) |
|
185 instance = new octave_dynamic_loader (); |
707
|
186 |
3325
|
187 if (! instance) |
707
|
188 { |
3655
|
189 ::error ("unable to create dynamic loader object!"); |
707
|
190 |
3325
|
191 retval = false; |
707
|
192 } |
|
193 |
2893
|
194 return retval; |
|
195 } |
|
196 |
3325
|
197 static |
3523
|
198 void clear_function (const std::string& fcn_name) |
3325
|
199 { |
|
200 if (Vwarn_reload_forces_clear) |
|
201 warning (" %s", fcn_name.c_str ()); |
|
202 |
|
203 curr_sym_tab->clear (fcn_name); |
|
204 |
|
205 if (curr_sym_tab != top_level_sym_tab) |
|
206 top_level_sym_tab->clear (fcn_name); |
|
207 |
4009
|
208 fbi_sym_tab->clear (fcn_name); |
3325
|
209 } |
|
210 |
|
211 bool |
4243
|
212 octave_dynamic_loader::do_load (const std::string& fcn_name, |
|
213 const std::string& file_name) |
2893
|
214 { |
3325
|
215 bool retval = false; |
|
216 |
|
217 octave_shlib oct_file; |
|
218 |
|
219 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
220 |
|
221 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
222 |
|
223 doing_load = true; |
|
224 |
|
225 void *function |
|
226 = octave_shlib_list::search (fcn_name, oct_file, mangle_name); |
|
227 |
|
228 if (! error_state) |
|
229 { |
|
230 if (function && oct_file.is_out_of_date ()) |
|
231 { |
|
232 int n = oct_file.number_of_functions_loaded (); |
|
233 |
|
234 if (n > 0 && Vwarn_reload_forces_clear) |
|
235 warning ("reloading %s clears the following functions:", |
|
236 oct_file.file_name().c_str ()); |
|
237 |
|
238 oct_file.close (clear_function); |
|
239 |
|
240 function = 0; |
|
241 } |
|
242 |
|
243 if (! function) |
|
244 { |
4243
|
245 std::string oct_file_name |
|
246 = file_name.empty () ? oct_file_in_path (fcn_name) : file_name; |
3325
|
247 |
|
248 if (! oct_file_name.empty ()) |
|
249 { |
|
250 oct_file.open (oct_file_name, Vwarn_future_time_stamp); |
|
251 |
|
252 if (! error_state) |
|
253 { |
|
254 if (oct_file) |
|
255 { |
|
256 octave_shlib_list::append (oct_file); |
|
257 |
|
258 function = oct_file.search (fcn_name, mangle_name); |
|
259 } |
|
260 else |
3655
|
261 ::error ("%s is not a valid shared library", |
|
262 oct_file_name.c_str ()); |
3325
|
263 } |
|
264 } |
|
265 } |
|
266 } |
|
267 |
|
268 if (function) |
|
269 { |
|
270 octave_dld_fcn_installer f |
|
271 = X_CAST (octave_dld_fcn_installer, function); |
|
272 |
|
273 retval = f (oct_file); |
3656
|
274 |
|
275 if (! retval) |
|
276 ::error ("failed to install dld function `%s'", fcn_name.c_str ()); |
3325
|
277 } |
3655
|
278 |
3325
|
279 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
280 |
|
281 return retval; |
|
282 } |
|
283 |
|
284 bool |
3523
|
285 octave_dynamic_loader::do_remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
286 { |
|
287 bool retval = false; |
|
288 |
|
289 // We don't need to do anything if this is called because we are in |
|
290 // the process of reloading a .oct file that has changed. |
|
291 |
|
292 if (! doing_load) |
|
293 { |
|
294 retval = shl.remove (fcn_name); |
|
295 |
|
296 if (shl.number_of_functions_loaded () == 0) |
|
297 octave_shlib_list::remove (shl); |
|
298 } |
|
299 |
|
300 return retval; |
|
301 } |
|
302 |
|
303 bool |
4243
|
304 octave_dynamic_loader::load (const std::string& fcn_name, |
|
305 const std::string& file_name) |
3325
|
306 { |
4243
|
307 return (instance_ok ()) ? instance->do_load (fcn_name, file_name) : false; |
3325
|
308 } |
|
309 |
|
310 bool |
3523
|
311 octave_dynamic_loader::remove (const std::string& fcn_name, octave_shlib& shl) |
3325
|
312 { |
|
313 return (instance_ok ()) ? instance->do_remove (fcn_name, shl) : false; |
2893
|
314 } |
707
|
315 |
3842
|
316 #define STRINGIFY(s) STRINGIFY1(s) |
|
317 #define STRINGIFY1(s) #s |
|
318 |
3536
|
319 std::string |
3523
|
320 octave_dynamic_loader::mangle_name (const std::string& name) |
2894
|
321 { |
3222
|
322 #if defined (CXX_PREPENDS_UNDERSCORE) |
3523
|
323 std::string retval ("_FS"); |
3222
|
324 #else |
3523
|
325 std::string retval ("FS"); |
3222
|
326 #endif |
2894
|
327 retval.append (name); |
3842
|
328 retval.append ("_"); |
|
329 retval.append (STRINGIFY (CXX_ABI)); |
2894
|
330 return retval; |
|
331 } |
707
|
332 |
3325
|
333 static int |
|
334 warn_reload_forces_clear (void) |
|
335 { |
|
336 Vwarn_reload_forces_clear = check_preference ("warn_reload_forces_clear"); |
|
337 |
|
338 return 0; |
|
339 } |
|
340 |
|
341 void |
|
342 symbols_of_dynamic_ld (void) |
|
343 { |
4233
|
344 DEFVAR (warn_reload_forces_clear, true, warn_reload_forces_clear, |
3371
|
345 "-*- texinfo -*-\n\ |
|
346 @defvr {Built-in Variable} warn_reload_forces_clear\n\ |
|
347 If several functions have been loaded from the same file, Octave must\n\ |
|
348 clear all the functions before any one of them can be reloaded. If\n\ |
|
349 @code{warn_reload_forces_clear}, Octave will warn you when this happens,\n\ |
|
350 and print a list of the additional functions that it is forced to clear.\n\ |
|
351 @end defvr"); |
3325
|
352 } |
|
353 |
1
|
354 /* |
|
355 ;;; Local Variables: *** |
|
356 ;;; mode: C++ *** |
|
357 ;;; End: *** |
|
358 */ |