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