/
library.cpp
186 lines (157 loc) · 4.85 KB
/
library.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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "de/libdeng2.h"
#include "de/Library"
#include "de/Log"
#include "de/LogBuffer"
#if defined(UNIX) && defined(DENG2_QT_4_7_OR_NEWER) && !defined(DENG2_QT_4_8_OR_NEWER)
# define DENG2_USE_DLOPEN
# include <dlfcn.h>
typedef void *Handle;
#else
typedef QLibrary *Handle;
#endif
using namespace de;
char const *Library::DEFAULT_TYPE = "library/generic";
struct Library::Instance
{
/// Handle to the shared library.
Handle library;
typedef QMap<String, void *> Symbols;
Symbols symbols;
/// Type identifier for the library (e.g., "deng-plugin/generic").
/// Queried by calling deng_LibraryType(), if one is exported in the library.
String type;
Instance() : library(0), type(DEFAULT_TYPE)
{}
#ifdef DENG2_USE_DLOPEN
NativePath fileName;
NativePath nativePath() { return fileName; }
bool isLoaded() const { return library != 0; }
#else
NativePath nativePath() {
DENG2_ASSERT(library);
return library->fileName();
}
bool isLoaded() const { return library->isLoaded(); }
#endif
};
Library::Library(NativePath const &nativePath) : d(0)
{
d = new Instance;
LOG_AS("Library");
LOG_TRACE("Loading \"%s\"") << nativePath.pretty();
#ifndef DENG2_USE_DLOPEN
d->library = new QLibrary(nativePath);
d->library->setLoadHints(QLibrary::ResolveAllSymbolsHint);
d->library->load();
#else
d->fileName = nativePath;
d->library = dlopen(nativePath.toUtf8().constData(), RTLD_NOW);
#endif
if(!d->isLoaded())
{
#ifndef DENG2_USE_DLOPEN
QString msg = d->library->errorString();
delete d->library;
#else
QString msg = dlerror();
#endif
d->library = 0;
/// @throw LoadError Opening of the dynamic library failed.
throw LoadError("Library::Library", msg);
}
if(hasSymbol("deng_LibraryType"))
{
// Query the type identifier.
d->type = DENG2_SYMBOL(deng_LibraryType)();
}
// Automatically call the initialization function, if one exists.
if(d->type.beginsWith("deng-plugin/") && hasSymbol("deng_InitializePlugin"))
{
DENG2_SYMBOL(deng_InitializePlugin)();
}
}
Library::~Library()
{
if(d->library)
{
LOG_AS("~Library");
LOG_TRACE("Unloading \"%s\"") << d->nativePath().pretty();
// Automatically call the shutdown function, if one exists.
if(d->type.beginsWith("deng-plugin/") && hasSymbol("deng_ShutdownPlugin"))
{
DENG2_SYMBOL(deng_ShutdownPlugin)();
}
// The log buffer may contain log entries built by the library; those
// entries contain pointers to functions that are about to disappear.
LogBuffer::appBuffer().clear();
#ifndef DENG2_USE_DLOPEN
d->library->unload();
delete d->library;
#else
dlclose(d->library);
#endif
}
delete d;
}
String const &Library::type() const
{
return d->type;
}
void *Library::address(String const &name, SymbolLookupMode lookup)
{
if(!d->library)
{
/// @throw SymbolMissingError There is no library loaded at the moment.
throw SymbolMissingError("Library::symbol", "Library not loaded");
}
// Already looked up?
Instance::Symbols::iterator found = d->symbols.find(name);
if(found != d->symbols.end())
{
return found.value();
}
#ifndef DENG2_USE_DLOPEN
void *ptr = d->library->resolve(name.toAscii().constData());
#else
void *ptr = dlsym(d->library, name.toAscii().constData());
#endif
if(!ptr)
{
if(lookup == RequiredSymbol)
{
/// @throw SymbolMissingError There is no symbol @a name in the library.
throw SymbolMissingError("Library::symbol", "Symbol '" + name + "' was not found");
}
return 0;
}
d->symbols[name] = ptr;
return ptr;
}
bool Library::hasSymbol(String const &name) const
{
// First check the symbols cache.
if(d->symbols.find(name) != d->symbols.end()) return true;
#ifndef DENG2_USE_DLOPEN
return d->library->resolve(name.toAscii().constData()) != 0;
#else
return dlsym(d->library, name.toAscii().constData()) != 0;
#endif
}