Line data Source code
1 :
2 : /* Copyright (c) 2009, Cedric Stalder <cedric.stalder@gmail.com>
3 : * 2009-2014, Stefan Eilemann <eile@equalizergraphics.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify it under
6 : * the terms of the GNU Lesser General Public License version 2.1 as published
7 : * by the Free Software Foundation.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 : * details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public License
15 : * along with this library; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #include "dso.h"
20 :
21 : #include "debug.h"
22 : #include "log.h"
23 : #include "os.h"
24 :
25 : #ifdef _WIN32 //_MSC_VER
26 : # define LB_DL_ERROR sysError
27 : #else
28 : # include <dlfcn.h>
29 : # define LB_DL_ERROR dlerror()
30 : #endif
31 :
32 : namespace lunchbox
33 : {
34 : namespace detail
35 : {
36 : class DSO
37 : {
38 : public:
39 4 : DSO() : dso( 0 ) {}
40 :
41 : #ifdef _WIN32 //_MSC_VER
42 : HMODULE dso;
43 : #else
44 : void* dso;
45 : #endif
46 : };
47 : }
48 :
49 1 : DSO::DSO()
50 1 : : _impl( new detail::DSO )
51 1 : {}
52 :
53 3 : DSO::DSO( const std::string& name )
54 3 : : _impl( new detail::DSO )
55 : {
56 3 : open( name );
57 3 : }
58 :
59 4 : DSO::~DSO()
60 : {
61 4 : close();
62 4 : delete _impl;
63 4 : }
64 :
65 5 : bool DSO::open( const std::string& fileName )
66 : {
67 5 : if( _impl->dso )
68 : {
69 1 : LBWARN << "DSO already open, close it first" << std::endl;
70 1 : return false;
71 : }
72 :
73 4 : if( fileName.empty( ))
74 : {
75 : #ifdef _WIN32 //_MSC_VER
76 : _impl->dso = GetModuleHandle( 0 );
77 : LBASSERT( _impl->dso );
78 : #else
79 0 : _impl->dso = RTLD_DEFAULT;
80 : #endif
81 : }
82 : else
83 : {
84 : #ifdef _WIN32 //_MSC_VER
85 : _impl->dso = LoadLibrary( fileName.c_str() );
86 : #elif defined( RTLD_LOCAL )
87 4 : _impl->dso = dlopen( fileName.c_str(), RTLD_LAZY | RTLD_LOCAL );
88 : #else
89 : _impl->dso = dlopen( fileName.c_str(), RTLD_LAZY );
90 : #endif
91 4 : if( !_impl->dso )
92 : {
93 0 : LBINFO << "Can't open library " << fileName << ": " << LB_DL_ERROR
94 0 : << std::endl;
95 0 : return false;
96 : }
97 : }
98 :
99 4 : return true;
100 : }
101 :
102 6 : void DSO::close()
103 : {
104 6 : if( !_impl->dso )
105 8 : return;
106 :
107 : #ifdef _WIN32 //_MSC_VER
108 : if( _impl->dso != GetModuleHandle( 0 ))
109 : FreeLibrary( _impl->dso ) ;
110 : #else
111 4 : if( _impl->dso != RTLD_DEFAULT )
112 4 : dlclose ( _impl->dso );
113 : #endif
114 :
115 4 : _impl->dso = 0;
116 : }
117 :
118 19 : void* DSO::getFunctionPointer( const std::string& name ) const
119 : {
120 19 : if( !_impl->dso )
121 1 : return 0;
122 :
123 : #ifdef _WIN32 //_MSC_VER
124 : return (void*)GetProcAddress( _impl->dso, name.c_str( ));
125 : #else
126 18 : return dlsym( _impl->dso, name.c_str( ));
127 : #endif
128 : }
129 :
130 4 : bool DSO::isOpen() const
131 : {
132 4 : return _impl->dso != 0;
133 : }
134 :
135 8 : bool DSO::operator == ( const DSO& rhs ) const
136 : {
137 8 : return _impl->dso == rhs._impl->dso;
138 : }
139 :
140 87 : }
|