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