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