]>
Commit | Line | Data |
---|---|---|
0b9ab0bd RL |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dynload.cpp | |
3 | // Purpose: Dynamic loading framework | |
4 | // Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's | |
5 | // (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux) | |
6 | // Modified by: | |
7 | // Created: 03/12/01 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2001 Ron Lee <ron@debian.org> | |
55d99c7a | 10 | // Licence: wxWindows licence |
0b9ab0bd RL |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
14f355c2 | 13 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
0b9ab0bd RL |
14 | #pragma implementation "dynload.h" |
15 | #endif | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
409bd320 | 24 | #pragma hdrstop |
0b9ab0bd RL |
25 | #endif |
26 | ||
27 | #if wxUSE_DYNAMIC_LOADER | |
28 | ||
29 | #ifdef __WINDOWS__ | |
409bd320 | 30 | #include "wx/msw/private.h" |
0b9ab0bd RL |
31 | #endif |
32 | ||
33 | #ifndef WX_PRECOMP | |
409bd320 VZ |
34 | #include "wx/log.h" |
35 | #include "wx/intl.h" | |
2b5f62a0 | 36 | #include "wx/hash.h" |
076fdb21 | 37 | #include "wx/utils.h" |
0b9ab0bd RL |
38 | #endif |
39 | ||
401eb3de | 40 | #include "wx/strconv.h" |
0b9ab0bd | 41 | |
409bd320 | 42 | #include "wx/dynload.h" |
dabd1377 | 43 | #include "wx/module.h" |
0b9ab0bd | 44 | |
0b9ab0bd RL |
45 | |
46 | // --------------------------------------------------------------------------- | |
4f89dbc4 | 47 | // wxPluginLibrary |
0b9ab0bd RL |
48 | // --------------------------------------------------------------------------- |
49 | ||
50 | ||
dabd1377 JS |
51 | wxDLImports* wxPluginLibrary::ms_classes = NULL; |
52 | ||
53 | class wxPluginLibraryModule : public wxModule | |
54 | { | |
55 | public: | |
d0fcccc4 VZ |
56 | wxPluginLibraryModule() { } |
57 | ||
58 | // TODO: create ms_classes on demand, why always preallocate it? | |
59 | virtual bool OnInit() | |
60 | { | |
b2bb920f | 61 | wxPluginLibrary::ms_classes = new wxDLImports; |
d0fcccc4 VZ |
62 | wxPluginManager::CreateManifest(); |
63 | return TRUE; | |
64 | } | |
65 | ||
66 | virtual void OnExit() | |
67 | { | |
68 | delete wxPluginLibrary::ms_classes; | |
69 | wxPluginLibrary::ms_classes = NULL; | |
70 | wxPluginManager::ClearManifest(); | |
71 | } | |
dabd1377 JS |
72 | |
73 | private: | |
74 | DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule ) | |
75 | }; | |
76 | ||
77 | IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule) | |
78 | ||
0b9ab0bd | 79 | |
23213f18 | 80 | wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags) |
4f89dbc4 | 81 | : m_linkcount(1) |
7c1e2b44 | 82 | , m_objcount(0) |
0b9ab0bd | 83 | { |
4f89dbc4 RL |
84 | m_before = wxClassInfo::sm_first; |
85 | Load( libname, flags ); | |
86 | m_after = wxClassInfo::sm_first; | |
87 | ||
0b9ab0bd RL |
88 | if( m_handle != 0 ) |
89 | { | |
0b9ab0bd RL |
90 | RegisterModules(); |
91 | } | |
92 | else | |
d0fcccc4 VZ |
93 | { |
94 | // Flag us for deletion | |
95 | --m_linkcount; | |
96 | } | |
0b9ab0bd RL |
97 | } |
98 | ||
4f89dbc4 | 99 | wxPluginLibrary::~wxPluginLibrary() |
0b9ab0bd | 100 | { |
01610529 RL |
101 | if( m_handle != 0 ) |
102 | { | |
103 | UnregisterModules(); | |
01610529 | 104 | } |
0b9ab0bd RL |
105 | } |
106 | ||
d0fcccc4 VZ |
107 | wxPluginLibrary *wxPluginLibrary::RefLib() |
108 | { | |
109 | wxCHECK_MSG( m_linkcount > 0, NULL, | |
110 | _T("Library had been already deleted!") ); | |
111 | ||
112 | ++m_linkcount; | |
113 | return this; | |
114 | } | |
115 | ||
4f89dbc4 | 116 | bool wxPluginLibrary::UnrefLib() |
7c1e2b44 | 117 | { |
d0fcccc4 VZ |
118 | wxASSERT_MSG( m_objcount == 0, |
119 | _T("Library unloaded before all objects were destroyed") ); | |
120 | ||
121 | if ( --m_linkcount == 0 ) | |
7c1e2b44 RL |
122 | { |
123 | delete this; | |
124 | return TRUE; | |
125 | } | |
d0fcccc4 | 126 | |
7c1e2b44 RL |
127 | return FALSE; |
128 | } | |
0b9ab0bd RL |
129 | |
130 | // ------------------------ | |
131 | // Private methods | |
132 | // ------------------------ | |
133 | ||
4f89dbc4 | 134 | void wxPluginLibrary::RegisterModules() |
0b9ab0bd RL |
135 | { |
136 | // Plugin libraries might have wxModules, Register and initialise them if | |
137 | // they do. | |
138 | // | |
139 | // Note that these classes are NOT included in the reference counting since | |
140 | // it's implicit that they will be unloaded if and when the last handle to | |
141 | // the library is. We do have to keep a copy of the module's pointer | |
142 | // though, as there is currently no way to Unregister it without it. | |
143 | ||
7c1e2b44 | 144 | wxASSERT_MSG( m_linkcount == 1, |
0b9ab0bd RL |
145 | _T("RegisterModules should only be called for the first load") ); |
146 | ||
6eccc0c4 | 147 | for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next) |
0b9ab0bd RL |
148 | { |
149 | if( info->IsKindOf(CLASSINFO(wxModule)) ) | |
150 | { | |
151 | wxModule *m = wxDynamicCast(info->CreateObject(), wxModule); | |
152 | ||
153 | wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") ); | |
154 | ||
df5168c4 | 155 | m_wxmodules.push_back(m); |
0b9ab0bd RL |
156 | wxModule::RegisterModule(m); |
157 | } | |
158 | } | |
159 | ||
160 | // FIXME: Likewise this is (well was) very similar to InitializeModules() | |
161 | ||
df5168c4 MB |
162 | for ( wxModuleList::iterator it = m_wxmodules.begin(); |
163 | it != m_wxmodules.end(); | |
164 | ++it) | |
0b9ab0bd | 165 | { |
df5168c4 | 166 | if( !(*it)->Init() ) |
0b9ab0bd | 167 | { |
4f89dbc4 | 168 | wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary")); |
0b9ab0bd RL |
169 | |
170 | // XXX: Watch this, a different hash implementation might break it, | |
171 | // a good hash implementation would let us fix it though. | |
172 | ||
173 | // The name of the game is to remove any uninitialised modules and | |
174 | // let the dtor Exit the rest on shutdown, (which we'll initiate | |
175 | // shortly). | |
176 | ||
df5168c4 | 177 | wxModuleList::iterator oldNode = m_wxmodules.end(); |
0b9ab0bd | 178 | do { |
df5168c4 MB |
179 | ++it; |
180 | if( oldNode != m_wxmodules.end() ) | |
181 | m_wxmodules.erase(oldNode); | |
182 | wxModule::UnregisterModule( *it ); | |
183 | oldNode = it; | |
184 | } while( it != m_wxmodules.end() ); | |
0b9ab0bd | 185 | |
7c1e2b44 | 186 | --m_linkcount; // Flag us for deletion |
0b9ab0bd RL |
187 | break; |
188 | } | |
189 | } | |
190 | } | |
191 | ||
4f89dbc4 | 192 | void wxPluginLibrary::UnregisterModules() |
0b9ab0bd | 193 | { |
df5168c4 | 194 | wxModuleList::iterator it; |
0b9ab0bd | 195 | |
df5168c4 MB |
196 | for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it ) |
197 | (*it)->Exit(); | |
0b9ab0bd | 198 | |
df5168c4 MB |
199 | for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it ) |
200 | wxModule::UnregisterModule( *it ); | |
0b9ab0bd | 201 | |
df5168c4 | 202 | WX_CLEAR_LIST(wxModuleList, m_wxmodules); |
0b9ab0bd RL |
203 | } |
204 | ||
205 | ||
206 | // --------------------------------------------------------------------------- | |
d0fcccc4 | 207 | // wxPluginManager |
0b9ab0bd RL |
208 | // --------------------------------------------------------------------------- |
209 | ||
dabd1377 | 210 | wxDLManifest* wxPluginManager::ms_manifest = NULL; |
0b9ab0bd RL |
211 | |
212 | // ------------------------ | |
213 | // Static accessors | |
214 | // ------------------------ | |
215 | ||
d0fcccc4 VZ |
216 | wxPluginLibrary * |
217 | wxPluginManager::LoadLibrary(const wxString &libname, int flags) | |
0b9ab0bd | 218 | { |
4f89dbc4 | 219 | wxString realname(libname); |
0b9ab0bd | 220 | |
4f89dbc4 RL |
221 | if( !(flags & wxDL_VERBATIM) ) |
222 | realname += wxDynamicLibrary::GetDllExt(); | |
223 | ||
d0fcccc4 VZ |
224 | wxPluginLibrary *entry; |
225 | ||
226 | if ( flags & wxDL_NOSHARE ) | |
227 | { | |
228 | entry = NULL; | |
229 | } | |
230 | else | |
231 | { | |
2b5f62a0 | 232 | entry = FindByName(realname); |
d0fcccc4 | 233 | } |
4f89dbc4 | 234 | |
d0fcccc4 | 235 | if ( entry ) |
0b9ab0bd | 236 | { |
d0fcccc4 VZ |
237 | wxLogTrace(_T("dll"), |
238 | _T("LoadLibrary(%s): already loaded."), realname.c_str()); | |
239 | ||
7c1e2b44 | 240 | entry->RefLib(); |
0b9ab0bd RL |
241 | } |
242 | else | |
243 | { | |
4f89dbc4 | 244 | entry = new wxPluginLibrary( libname, flags ); |
0b9ab0bd | 245 | |
d0fcccc4 | 246 | if ( entry->IsLoaded() ) |
0b9ab0bd | 247 | { |
2b5f62a0 | 248 | (*ms_manifest)[realname] = entry; |
d0fcccc4 VZ |
249 | |
250 | wxLogTrace(_T("dll"), | |
251 | _T("LoadLibrary(%s): loaded ok."), realname.c_str()); | |
252 | ||
0b9ab0bd RL |
253 | } |
254 | else | |
255 | { | |
d0fcccc4 VZ |
256 | wxLogTrace(_T("dll"), |
257 | _T("LoadLibrary(%s): failed to load."), realname.c_str()); | |
258 | ||
259 | // we have created entry just above | |
260 | if ( !entry->UnrefLib() ) | |
261 | { | |
262 | // ... so UnrefLib() is supposed to delete it | |
263 | wxFAIL_MSG( _T("Currently linked library is not loaded?") ); | |
264 | } | |
265 | ||
266 | entry = NULL; | |
0b9ab0bd RL |
267 | } |
268 | } | |
d0fcccc4 | 269 | |
0b9ab0bd RL |
270 | return entry; |
271 | } | |
272 | ||
d0fcccc4 | 273 | bool wxPluginManager::UnloadLibrary(const wxString& libname) |
0b9ab0bd | 274 | { |
d0fcccc4 | 275 | wxString realname = libname; |
4f89dbc4 | 276 | |
2b5f62a0 | 277 | wxPluginLibrary *entry = FindByName(realname); |
0b9ab0bd | 278 | |
d0fcccc4 VZ |
279 | if ( !entry ) |
280 | { | |
281 | realname += wxDynamicLibrary::GetDllExt(); | |
0b9ab0bd | 282 | |
2b5f62a0 | 283 | entry = FindByName(realname); |
d0fcccc4 VZ |
284 | } |
285 | ||
286 | if ( !entry ) | |
287 | { | |
288 | wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."), | |
289 | libname.c_str()); | |
290 | ||
291 | return FALSE; | |
292 | } | |
293 | ||
294 | wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str()); | |
295 | ||
296 | if ( !entry->UnrefLib() ) | |
297 | { | |
298 | // not really unloaded yet | |
299 | return FALSE; | |
300 | } | |
301 | ||
2b5f62a0 | 302 | ms_manifest->erase(ms_manifest->find(realname)); |
d0fcccc4 VZ |
303 | |
304 | return TRUE; | |
4f89dbc4 RL |
305 | } |
306 | ||
0b9ab0bd RL |
307 | // ------------------------ |
308 | // Class implementation | |
309 | // ------------------------ | |
310 | ||
23213f18 | 311 | bool wxPluginManager::Load(const wxString &libname, int flags) |
0b9ab0bd | 312 | { |
4f89dbc4 | 313 | m_entry = wxPluginManager::LoadLibrary(libname, flags); |
2b5f62a0 | 314 | |
4f89dbc4 | 315 | return IsLoaded(); |
0b9ab0bd RL |
316 | } |
317 | ||
4f89dbc4 | 318 | void wxPluginManager::Unload() |
0b9ab0bd | 319 | { |
2b5f62a0 | 320 | wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") ); |
0b9ab0bd | 321 | |
2b5f62a0 VZ |
322 | for ( wxDLManifest::iterator i = ms_manifest->begin(); |
323 | i != ms_manifest->end(); | |
324 | ++i ) | |
4f89dbc4 | 325 | { |
2b5f62a0 VZ |
326 | if ( i->second == m_entry ) |
327 | { | |
328 | ms_manifest->erase(i); | |
46113053 | 329 | break; |
2b5f62a0 | 330 | } |
4f89dbc4 | 331 | } |
2b5f62a0 VZ |
332 | |
333 | m_entry->UnrefLib(); | |
334 | ||
335 | m_entry = NULL; | |
0b9ab0bd RL |
336 | } |
337 | ||
1948bb32 VS |
338 | |
339 | ||
340 | #if WXWIN_COMPATIBILITY_2_2 | |
341 | ||
342 | wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle) | |
343 | { | |
344 | for ( wxDLManifest::iterator i = ms_manifest->begin(); | |
345 | i != ms_manifest->end(); | |
346 | ++i ) | |
347 | { | |
348 | wxPluginLibrary * const lib = i->second; | |
349 | ||
350 | if ( lib->GetLibHandle() == handle ) | |
351 | return lib; | |
352 | } | |
353 | ||
354 | return NULL; | |
355 | } | |
356 | ||
4f89dbc4 RL |
357 | // --------------------------------------------------------------------------- |
358 | // wxDllLoader (all these methods are static) | |
359 | // --------------------------------------------------------------------------- | |
360 | ||
4f89dbc4 | 361 | |
3d7fa4e6 | 362 | wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success) |
4f89dbc4 | 363 | { |
d0fcccc4 VZ |
364 | wxPluginLibrary *p = wxPluginManager::LoadLibrary |
365 | ( | |
366 | name, | |
367 | wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE | |
368 | ); | |
369 | ||
3d7fa4e6 VZ |
370 | if ( success ) |
371 | *success = p != NULL; | |
372 | ||
d0fcccc4 | 373 | return p ? p->GetLibHandle() : 0; |
0b9ab0bd RL |
374 | } |
375 | ||
4f89dbc4 | 376 | void wxDllLoader::UnloadLibrary(wxDllType handle) |
0b9ab0bd | 377 | { |
4f89dbc4 | 378 | wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle); |
d0fcccc4 VZ |
379 | |
380 | wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") ); | |
381 | ||
4f89dbc4 | 382 | p->UnrefLib(); |
0b9ab0bd RL |
383 | } |
384 | ||
d0fcccc4 VZ |
385 | void * |
386 | wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success) | |
0b9ab0bd | 387 | { |
4f89dbc4 | 388 | wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle); |
d0fcccc4 VZ |
389 | |
390 | if ( !p ) | |
391 | { | |
392 | wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") ); | |
393 | ||
394 | if ( success ) | |
395 | *success = FALSE; | |
396 | ||
397 | return NULL; | |
398 | } | |
399 | ||
4f89dbc4 | 400 | return p->GetSymbol(name, success); |
0b9ab0bd RL |
401 | } |
402 | ||
1948bb32 VS |
403 | |
404 | // --------------------------------------------------------------------------- | |
405 | // Global variables | |
406 | // --------------------------------------------------------------------------- | |
407 | ||
408 | wxLibraries wxTheLibraries; | |
409 | ||
410 | // ============================================================================ | |
411 | // implementation | |
412 | // ============================================================================ | |
413 | ||
414 | // construct the full name from the base shared object name: adds a .dll | |
415 | // suffix under Windows or .so under Unix | |
416 | static wxString ConstructLibraryName(const wxString& basename) | |
417 | { | |
418 | wxString fullname; | |
419 | fullname << basename << wxDllLoader::GetDllExt(); | |
420 | ||
421 | return fullname; | |
422 | } | |
423 | ||
424 | // --------------------------------------------------------------------------- | |
425 | // wxLibrary (one instance per dynamic library) | |
426 | // --------------------------------------------------------------------------- | |
427 | ||
428 | wxLibrary::wxLibrary(wxDllType handle) | |
429 | { | |
430 | typedef wxClassInfo *(*t_get_first)(void); | |
431 | t_get_first get_first; | |
432 | ||
433 | m_handle = handle; | |
434 | ||
435 | // Some system may use a local heap for library. | |
436 | get_first = (t_get_first)GetSymbol(_T("wxGetClassFirst")); | |
437 | // It is a wxWindows DLL. | |
438 | if (get_first) | |
439 | PrepareClasses(get_first()); | |
440 | } | |
441 | ||
442 | wxLibrary::~wxLibrary() | |
443 | { | |
444 | if ( m_handle ) | |
445 | { | |
446 | wxDllLoader::UnloadLibrary(m_handle); | |
447 | } | |
448 | } | |
449 | ||
450 | wxObject *wxLibrary::CreateObject(const wxString& name) | |
451 | { | |
452 | wxClassInfo *info = (wxClassInfo *)classTable.Get(name); | |
453 | ||
454 | if (!info) | |
455 | return NULL; | |
456 | ||
457 | return info->CreateObject(); | |
458 | } | |
459 | ||
460 | void wxLibrary::PrepareClasses(wxClassInfo *first) | |
461 | { | |
462 | // Index all class infos by their class name | |
463 | wxClassInfo *info = first; | |
464 | while (info) | |
465 | { | |
466 | if (info->m_className) | |
467 | classTable.Put(info->m_className, (wxObject *)info); | |
468 | info = info->m_next; | |
469 | } | |
470 | ||
d1d738f1 | 471 | #if !wxUSE_EXTENDED_RTTI |
1948bb32 VS |
472 | // Set base pointers for each wxClassInfo |
473 | info = first; | |
474 | while (info) | |
475 | { | |
476 | if (info->GetBaseClassName1()) | |
477 | info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1()); | |
478 | if (info->GetBaseClassName2()) | |
479 | info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2()); | |
480 | info = info->m_next; | |
481 | } | |
1bc7510a | 482 | #endif |
1948bb32 VS |
483 | } |
484 | ||
485 | void *wxLibrary::GetSymbol(const wxString& symbname) | |
486 | { | |
487 | return wxDllLoader::GetSymbol(m_handle, symbname); | |
488 | } | |
489 | ||
490 | ||
491 | // --------------------------------------------------------------------------- | |
492 | // wxLibraries (only one instance should normally exist) | |
493 | // --------------------------------------------------------------------------- | |
494 | ||
495 | wxLibraries::wxLibraries():m_loaded(wxKEY_STRING) | |
496 | { | |
497 | } | |
498 | ||
499 | wxLibraries::~wxLibraries() | |
500 | { | |
501 | wxNode *node = m_loaded.First(); | |
502 | ||
503 | while (node) { | |
504 | wxLibrary *lib = (wxLibrary *)node->Data(); | |
505 | delete lib; | |
506 | ||
507 | node = node->Next(); | |
508 | } | |
509 | } | |
510 | ||
511 | wxLibrary *wxLibraries::LoadLibrary(const wxString& name) | |
512 | { | |
513 | wxLibrary *lib; | |
514 | wxClassInfo *old_sm_first; | |
515 | wxNode *node = m_loaded.Find(name.GetData()); | |
516 | ||
517 | if (node != NULL) | |
518 | return ((wxLibrary *)node->Data()); | |
519 | ||
520 | // If DLL shares data, this is necessary. | |
521 | old_sm_first = wxClassInfo::sm_first; | |
522 | wxClassInfo::sm_first = NULL; | |
523 | ||
524 | wxString libname = ConstructLibraryName(name); | |
525 | ||
526 | bool success = FALSE; | |
527 | wxDllType handle = wxDllLoader::LoadLibrary(libname, &success); | |
528 | if(success) | |
529 | { | |
530 | lib = new wxLibrary(handle); | |
531 | wxClassInfo::sm_first = old_sm_first; | |
532 | ||
533 | m_loaded.Append(name.GetData(), lib); | |
534 | } | |
535 | else | |
536 | lib = NULL; | |
537 | return lib; | |
538 | } | |
539 | ||
540 | wxObject *wxLibraries::CreateObject(const wxString& path) | |
541 | { | |
542 | wxNode *node = m_loaded.First(); | |
543 | wxObject *obj; | |
544 | ||
545 | while (node) { | |
546 | obj = ((wxLibrary *)node->Data())->CreateObject(path); | |
547 | if (obj) | |
548 | return obj; | |
549 | ||
550 | node = node->Next(); | |
551 | } | |
552 | return NULL; | |
553 | } | |
554 | ||
4f89dbc4 RL |
555 | #endif // WXWIN_COMPATIBILITY_2_2 |
556 | ||
1948bb32 | 557 | |
0b9ab0bd RL |
558 | #endif // wxUSE_DYNAMIC_LOADER |
559 |