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