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