/
dynload.cpp
402 lines (316 loc) · 9.75 KB
/
dynload.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/////////////////////////////////////////////////////////////////////////////
// Name: src/common/dynload.cpp
// Purpose: Dynamic loading framework
// Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's
// (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux)
// Modified by:
// Created: 03/12/01
// RCS-ID: $Id$
// Copyright: (c) 2001 Ron Lee <ron@debian.org>
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_DYNAMIC_LOADER
#ifdef __WINDOWS__
#include "wx/msw/private.h"
#endif
#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/intl.h"
#include "wx/hash.h"
#include "wx/utils.h"
#include "wx/module.h"
#endif
#include "wx/strconv.h"
#include "wx/dynload.h"
// ---------------------------------------------------------------------------
// wxPluginLibrary
// ---------------------------------------------------------------------------
wxDLImports* wxPluginLibrary::ms_classes = NULL;
class wxPluginLibraryModule : public wxModule
{
public:
wxPluginLibraryModule() { }
// TODO: create ms_classes on demand, why always preallocate it?
virtual bool OnInit()
{
wxPluginLibrary::ms_classes = new wxDLImports;
wxPluginManager::CreateManifest();
return true;
}
virtual void OnExit()
{
wxDELETE(wxPluginLibrary::ms_classes);
wxPluginManager::ClearManifest();
}
private:
DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
};
IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
: m_linkcount(1)
, m_objcount(0)
{
const wxClassInfo* const oldFirst = wxClassInfo::GetFirst();
Load( libname, flags );
// It is simple to know what is the last object we registered, it's just
// the new head of the wxClassInfo list:
m_ourLast = wxClassInfo::GetFirst();
// But to find the first wxClassInfo created by this library we need to
// iterate until we get to the previous head as we don't have the links in
// the backwards direction:
if ( m_ourLast != oldFirst )
{
for ( const wxClassInfo* info = m_ourLast; ; info = info->GetNext() )
{
if ( info->GetNext() == oldFirst )
{
m_ourFirst = info;
break;
}
}
}
else // We didn't register any classes at all.
{
m_ourFirst =
m_ourLast = NULL;
}
if( m_handle != 0 )
{
UpdateClasses();
RegisterModules();
}
else
{
// Flag us for deletion
--m_linkcount;
}
}
wxPluginLibrary::~wxPluginLibrary()
{
if( m_handle != 0 )
{
UnregisterModules();
RestoreClasses();
}
}
wxPluginLibrary *wxPluginLibrary::RefLib()
{
wxCHECK_MSG( m_linkcount > 0, NULL,
wxT("Library had been already deleted!") );
++m_linkcount;
return this;
}
bool wxPluginLibrary::UnrefLib()
{
wxASSERT_MSG( m_objcount == 0,
wxT("Library unloaded before all objects were destroyed") );
if ( m_linkcount == 0 || --m_linkcount == 0 )
{
delete this;
return true;
}
return false;
}
// ------------------------
// Private methods
// ------------------------
void wxPluginLibrary::UpdateClasses()
{
if ( !m_ourFirst )
return;
for ( const wxClassInfo *info = m_ourFirst; ; info = info->GetNext() )
{
if( info->GetClassName() )
{
// Hash all the class names into a local table too so
// we can quickly find the entry they correspond to.
(*ms_classes)[info->GetClassName()] = this;
}
if ( info == m_ourLast )
break;
}
}
void wxPluginLibrary::RestoreClasses()
{
// Check if there is a need to restore classes.
if (!ms_classes)
return;
if ( !m_ourFirst )
return;
for ( const wxClassInfo *info = m_ourFirst; ; info = info->GetNext() )
{
ms_classes->erase(ms_classes->find(info->GetClassName()));
if ( info == m_ourLast )
break;
}
}
void wxPluginLibrary::RegisterModules()
{
// Plugin libraries might have wxModules, Register and initialise them if
// they do.
//
// Note that these classes are NOT included in the reference counting since
// it's implicit that they will be unloaded if and when the last handle to
// the library is. We do have to keep a copy of the module's pointer
// though, as there is currently no way to Unregister it without it.
wxASSERT_MSG( m_linkcount == 1,
wxT("RegisterModules should only be called for the first load") );
if ( m_ourFirst )
{
for ( const wxClassInfo *info = m_ourFirst; ; info = info->GetNext() )
{
if( info->IsKindOf(wxCLASSINFO(wxModule)) )
{
wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
wxASSERT_MSG( m, wxT("wxDynamicCast of wxModule failed") );
m_wxmodules.push_back(m);
wxModule::RegisterModule(m);
}
if ( info == m_ourLast )
break;
}
}
// FIXME: Likewise this is (well was) very similar to InitializeModules()
for ( wxModuleList::iterator it = m_wxmodules.begin();
it != m_wxmodules.end();
++it)
{
if( !(*it)->Init() )
{
wxLogDebug(wxT("wxModule::Init() failed for wxPluginLibrary"));
// XXX: Watch this, a different hash implementation might break it,
// a good hash implementation would let us fix it though.
// The name of the game is to remove any uninitialised modules and
// let the dtor Exit the rest on shutdown, (which we'll initiate
// shortly).
wxModuleList::iterator oldNode = m_wxmodules.end();
do {
++it;
if( oldNode != m_wxmodules.end() )
m_wxmodules.erase(oldNode);
wxModule::UnregisterModule( *it );
oldNode = it;
} while( it != m_wxmodules.end() );
--m_linkcount; // Flag us for deletion
break;
}
}
}
void wxPluginLibrary::UnregisterModules()
{
wxModuleList::iterator it;
for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
(*it)->Exit();
for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
wxModule::UnregisterModule( *it );
// NB: content of the list was deleted by UnregisterModule calls above:
m_wxmodules.clear();
}
// ---------------------------------------------------------------------------
// wxPluginManager
// ---------------------------------------------------------------------------
wxDLManifest* wxPluginManager::ms_manifest = NULL;
// ------------------------
// Static accessors
// ------------------------
wxPluginLibrary *
wxPluginManager::LoadLibrary(const wxString &libname, int flags)
{
wxString realname(libname);
if( !(flags & wxDL_VERBATIM) )
realname += wxDynamicLibrary::GetDllExt();
wxPluginLibrary *entry;
if ( flags & wxDL_NOSHARE )
{
entry = NULL;
}
else
{
entry = FindByName(realname);
}
if ( entry )
{
wxLogTrace(wxT("dll"),
wxT("LoadLibrary(%s): already loaded."), realname.c_str());
entry->RefLib();
}
else
{
entry = new wxPluginLibrary( libname, flags );
if ( entry->IsLoaded() )
{
(*ms_manifest)[realname] = entry;
wxLogTrace(wxT("dll"),
wxT("LoadLibrary(%s): loaded ok."), realname.c_str());
}
else
{
wxLogTrace(wxT("dll"),
wxT("LoadLibrary(%s): failed to load."), realname.c_str());
// we have created entry just above
if ( !entry->UnrefLib() )
{
// ... so UnrefLib() is supposed to delete it
wxFAIL_MSG( wxT("Currently linked library is not loaded?") );
}
entry = NULL;
}
}
return entry;
}
bool wxPluginManager::UnloadLibrary(const wxString& libname)
{
wxString realname = libname;
wxPluginLibrary *entry = FindByName(realname);
if ( !entry )
{
realname += wxDynamicLibrary::GetDllExt();
entry = FindByName(realname);
}
if ( !entry )
{
wxLogDebug(wxT("Attempt to unload library '%s' which is not loaded."),
libname.c_str());
return false;
}
wxLogTrace(wxT("dll"), wxT("UnloadLibrary(%s)"), realname.c_str());
if ( !entry->UnrefLib() )
{
// not really unloaded yet
return false;
}
ms_manifest->erase(ms_manifest->find(realname));
return true;
}
// ------------------------
// Class implementation
// ------------------------
bool wxPluginManager::Load(const wxString &libname, int flags)
{
m_entry = wxPluginManager::LoadLibrary(libname, flags);
return IsLoaded();
}
void wxPluginManager::Unload()
{
wxCHECK_RET( m_entry, wxT("unloading an invalid wxPluginManager?") );
for ( wxDLManifest::iterator i = ms_manifest->begin();
i != ms_manifest->end();
++i )
{
if ( i->second == m_entry )
{
ms_manifest->erase(i);
break;
}
}
m_entry->UnrefLib();
m_entry = NULL;
}
#endif // wxUSE_DYNAMIC_LOADER