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 |
3325
|
27 #include "oct-time.h" |
|
28 #include "file-stat.h" |
707
|
29 |
3325
|
30 #include "DLList.h" |
1
|
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 |
|
45 template class DLNode<octave_shlib>; |
|
46 template class DLList<octave_shlib>; |
1664
|
47 |
2894
|
48 class |
3325
|
49 octave_shlib_list |
1664
|
50 { |
2894
|
51 public: |
|
52 |
3325
|
53 static void append (const octave_shlib& shl); |
2894
|
54 |
3325
|
55 static void remove (octave_shlib& shl); |
|
56 |
|
57 static void *search (const string& fcn_name, octave_shlib& shl, |
|
58 octave_shlib::name_mangler mangler = 0); |
2894
|
59 |
|
60 private: |
|
61 |
3325
|
62 octave_shlib_list (void) { } |
|
63 |
|
64 ~octave_shlib_list (void) { } |
|
65 |
|
66 void do_append (const octave_shlib& shl); |
|
67 |
|
68 void do_remove (octave_shlib& shl); |
|
69 |
|
70 void *do_search (const string& fcn_name, octave_shlib& shl, |
|
71 octave_shlib::name_mangler mangler = 0); |
|
72 |
|
73 static octave_shlib_list *instance; |
|
74 |
|
75 static bool instance_ok (void); |
|
76 |
|
77 // List of libraries we have loaded. |
|
78 DLList<octave_shlib> lib_list; |
2894
|
79 |
|
80 // No copying! |
|
81 |
3325
|
82 octave_shlib_list (const octave_shlib_list&); |
2894
|
83 |
3325
|
84 octave_shlib_list& operator = (const octave_shlib_list&); |
2894
|
85 }; |
|
86 |
3325
|
87 octave_shlib_list *octave_shlib_list::instance = 0; |
3321
|
88 |
3325
|
89 void |
|
90 octave_shlib_list::do_append (const octave_shlib& shl) |
|
91 { |
|
92 lib_list.append (shl); |
1664
|
93 } |
|
94 |
3325
|
95 void |
|
96 octave_shlib_list::do_remove (octave_shlib& shl) |
1664
|
97 { |
3325
|
98 for (Pix p = lib_list.first (); p != 0; lib_list.next (p)) |
|
99 { |
|
100 if (lib_list(p) == shl) |
|
101 { |
|
102 shl.close (); |
1664
|
103 |
3325
|
104 lib_list.del (p); |
3033
|
105 |
3325
|
106 break; |
1664
|
107 } |
|
108 } |
|
109 } |
|
110 |
3325
|
111 void * |
|
112 octave_shlib_list::do_search (const string& fcn_name, octave_shlib& shl, |
|
113 octave_shlib::name_mangler mangler) |
|
114 { |
|
115 void *function = 0; |
|
116 |
|
117 shl = octave_shlib (); |
1664
|
118 |
3325
|
119 for (Pix p = lib_list.first (); p != 0; lib_list.next (p)) |
|
120 { |
|
121 function = lib_list(p).search (fcn_name, mangler); |
|
122 |
|
123 if (function) |
|
124 { |
|
125 shl = lib_list(p); |
|
126 |
|
127 break; |
|
128 } |
|
129 } |
|
130 |
|
131 return function; |
|
132 } |
2926
|
133 |
|
134 bool |
3325
|
135 octave_shlib_list::instance_ok (void) |
2926
|
136 { |
|
137 bool retval = true; |
|
138 |
|
139 if (! instance) |
3325
|
140 instance = new octave_shlib_list (); |
2926
|
141 |
|
142 if (! instance) |
|
143 { |
3325
|
144 ::error ("unable to create shared library list object!"); |
2926
|
145 |
|
146 retval = false; |
|
147 } |
|
148 |
|
149 return retval; |
|
150 } |
|
151 |
|
152 void |
3325
|
153 octave_shlib_list::append (const octave_shlib& shl) |
|
154 { |
|
155 if (instance_ok ()) |
|
156 instance->do_append (shl); |
|
157 } |
|
158 |
|
159 void |
|
160 octave_shlib_list::remove (octave_shlib& shl) |
2894
|
161 { |
3325
|
162 if (instance_ok ()) |
|
163 instance->do_remove (shl); |
2894
|
164 } |
|
165 |
3325
|
166 void * |
|
167 octave_shlib_list::search (const string& fcn_name, octave_shlib& shl, |
|
168 octave_shlib::name_mangler mangler) |
|
169 { |
|
170 return (instance_ok ()) ? instance->do_search (fcn_name, shl, mangler) : 0; |
|
171 } |
|
172 |
|
173 octave_dynamic_loader *octave_dynamic_loader::instance = 0; |
|
174 |
|
175 bool octave_dynamic_loader::doing_load = false; |
|
176 |
2969
|
177 bool |
3325
|
178 octave_dynamic_loader::instance_ok (void) |
707
|
179 { |
3325
|
180 bool retval = true; |
1664
|
181 |
3325
|
182 if (! instance) |
|
183 instance = new octave_dynamic_loader (); |
707
|
184 |
3325
|
185 if (! instance) |
707
|
186 { |
3325
|
187 error ("unable to create dynamic loader object!"); |
707
|
188 |
3325
|
189 retval = false; |
707
|
190 } |
|
191 |
2893
|
192 return retval; |
|
193 } |
|
194 |
3325
|
195 static |
|
196 void clear_function (const string& fcn_name) |
|
197 { |
|
198 if (Vwarn_reload_forces_clear) |
|
199 warning (" %s", fcn_name.c_str ()); |
|
200 |
|
201 curr_sym_tab->clear (fcn_name); |
|
202 |
|
203 if (curr_sym_tab != top_level_sym_tab) |
|
204 top_level_sym_tab->clear (fcn_name); |
|
205 |
|
206 global_sym_tab->clear (fcn_name); |
|
207 } |
|
208 |
|
209 bool |
|
210 octave_dynamic_loader::do_load (const string& fcn_name) |
2893
|
211 { |
3325
|
212 bool retval = false; |
|
213 |
|
214 octave_shlib oct_file; |
|
215 |
|
216 unwind_protect::begin_frame ("octave_dynamic_loader::do_load"); |
|
217 |
|
218 unwind_protect_bool (octave_dynamic_loader::doing_load); |
|
219 |
|
220 doing_load = true; |
|
221 |
|
222 void *function |
|
223 = octave_shlib_list::search (fcn_name, oct_file, mangle_name); |
|
224 |
|
225 if (! error_state) |
|
226 { |
|
227 if (function && oct_file.is_out_of_date ()) |
|
228 { |
|
229 int n = oct_file.number_of_functions_loaded (); |
|
230 |
|
231 if (n > 0 && Vwarn_reload_forces_clear) |
|
232 warning ("reloading %s clears the following functions:", |
|
233 oct_file.file_name().c_str ()); |
|
234 |
|
235 oct_file.close (clear_function); |
|
236 |
|
237 function = 0; |
|
238 } |
|
239 |
|
240 if (! function) |
|
241 { |
|
242 string oct_file_name = oct_file_in_path (fcn_name); |
|
243 |
|
244 if (! oct_file_name.empty ()) |
|
245 { |
|
246 oct_file.open (oct_file_name, Vwarn_future_time_stamp); |
|
247 |
|
248 if (! error_state) |
|
249 { |
|
250 if (oct_file) |
|
251 { |
|
252 octave_shlib_list::append (oct_file); |
|
253 |
|
254 function = oct_file.search (fcn_name, mangle_name); |
|
255 } |
|
256 else |
|
257 error ("%s is not a valid shared library", |
|
258 oct_file_name.c_str ()); |
|
259 } |
|
260 } |
|
261 } |
|
262 } |
|
263 |
|
264 if (function) |
|
265 { |
|
266 octave_dld_fcn_installer f |
|
267 = X_CAST (octave_dld_fcn_installer, function); |
|
268 |
|
269 retval = f (oct_file); |
|
270 |
|
271 if (! retval) |
|
272 error ("failed to install dld function `%s'", fcn_name.c_str ()); |
|
273 } |
|
274 |
|
275 unwind_protect::run_frame ("octave_dynamic_loader::do_load"); |
|
276 |
|
277 return retval; |
|
278 } |
|
279 |
|
280 bool |
|
281 octave_dynamic_loader::do_remove (const string& fcn_name, octave_shlib& shl) |
|
282 { |
|
283 bool retval = false; |
|
284 |
|
285 // We don't need to do anything if this is called because we are in |
|
286 // the process of reloading a .oct file that has changed. |
|
287 |
|
288 if (! doing_load) |
|
289 { |
|
290 retval = shl.remove (fcn_name); |
|
291 |
|
292 if (shl.number_of_functions_loaded () == 0) |
|
293 octave_shlib_list::remove (shl); |
|
294 } |
|
295 |
|
296 return retval; |
|
297 } |
|
298 |
|
299 bool |
|
300 octave_dynamic_loader::load (const string& fcn_name) |
|
301 { |
|
302 return (instance_ok ()) ? instance->do_load (fcn_name) : false; |
|
303 } |
|
304 |
|
305 bool |
|
306 octave_dynamic_loader::remove (const string& fcn_name, octave_shlib& shl) |
|
307 { |
|
308 return (instance_ok ()) ? instance->do_remove (fcn_name, shl) : false; |
2893
|
309 } |
707
|
310 |
2894
|
311 string |
|
312 octave_dynamic_loader::mangle_name (const string& name) |
|
313 { |
3222
|
314 #if defined (CXX_PREPENDS_UNDERSCORE) |
|
315 string retval ("_FS"); |
|
316 #else |
2894
|
317 string retval ("FS"); |
3222
|
318 #endif |
2894
|
319 retval.append (name); |
3325
|
320 retval.append ("__FRC12octave_shlib"); |
2894
|
321 return retval; |
|
322 } |
707
|
323 |
3325
|
324 static int |
|
325 warn_reload_forces_clear (void) |
|
326 { |
|
327 Vwarn_reload_forces_clear = check_preference ("warn_reload_forces_clear"); |
|
328 |
|
329 return 0; |
|
330 } |
|
331 |
|
332 void |
|
333 symbols_of_dynamic_ld (void) |
|
334 { |
|
335 DEFVAR (warn_reload_forces_clear, 1.0, warn_reload_forces_clear, |
|
336 "warn if reloading a .oct file forces other functions to be cleared"); |
|
337 } |
|
338 |
1
|
339 /* |
|
340 ;;; Local Variables: *** |
|
341 ;;; mode: C++ *** |
|
342 ;;; End: *** |
|
343 */ |