1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 Copyright (c) 2011-2021 The plumed team
3 (see the PEOPLE file at the root of the distribution for a list of names)
4
5 See http://www.plumed.org for more information.
6
7 This file is part of plumed, version 2.
8
9 plumed is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 plumed is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #include "DLLoader.h"
23 #include <cstdlib>
24
25 #ifdef __PLUMED_HAS_DLOPEN
26 #include <dlfcn.h>
27 #endif
28
29 namespace PLMD {
30
installed()31 bool DLLoader::installed() {
32 #ifdef __PLUMED_HAS_DLOPEN
33 return true;
34 #else
35 return false;
36 #endif
37 }
38
39
load(const std::string & s)40 void* DLLoader::load(const std::string&s) {
41 #ifdef __PLUMED_HAS_DLOPEN
42 void* p=dlopen(s.c_str(),RTLD_NOW|RTLD_LOCAL);
43 if(!p) {
44 lastError=dlerror();
45 } else {
46 lastError="";
47 handles.push(p);
48 }
49 return p;
50 #else
51 return NULL;
52 #endif
53 }
54
error()55 const std::string & DLLoader::error() {
56 return lastError;
57 }
58
~DLLoader()59 DLLoader::~DLLoader() {
60 auto debug=std::getenv("PLUMED_LOAD_DEBUG");
61 if(debug) std::fprintf(stderr,"delete dlloader\n");
62 #ifdef __PLUMED_HAS_DLOPEN
63 while(!handles.empty()) {
64 int ret=dlclose(handles.top());
65 if(ret) {
66 std::fprintf(stderr,"+++ error reported by dlclose: %s\n",dlerror());
67 }
68 handles.pop();
69 }
70 #endif
71 if(debug) std::fprintf(stderr,"end delete dlloader\n");
72 }
73
DLLoader()74 DLLoader::DLLoader() {
75 // do nothing
76 }
77
78
79 }
80