clSPARSE  v0.10.0.0
a software library containing Sparse functions written in OpenCL
loadDynamicLibrary.hpp
Go to the documentation of this file.
1 /* ************************************************************************
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  * ************************************************************************ */
16 
23 #pragma once
24 #ifndef _SHAREDLIBRARY_H_
25 #define _SHAREDLIBRARY_H_
26 #include <string>
27 
28 // _WIN32 is defined for both 32 & 64 bit environments
29 #if defined( _WIN32 )
30 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
31 // Windows Header Files:
32 #include <windows.h>
33 #else
34 #include <dlfcn.h>
35 #endif
36 
47 inline void* LoadSharedLibrary( const std::string& libPrefix, std::string libraryName, bool quiet )
48 {
49 #if defined( _WIN32 )
50  libraryName += ".dll";
51 
52  // HMODULE is actually the load address; function returns NULL if it cannot find the shared library
53  HMODULE fileHandle = ::LoadLibraryExA(libraryName.c_str(), NULL, NULL);
54 #elif defined(__linux__)
55  std::string linuxName = libPrefix;
56  linuxName += libraryName += ".so";
57  void* fileHandle = ::dlopen( linuxName.c_str( ), RTLD_NOW );
58  if( !quiet && !fileHandle )
59  {
60  std::cerr << ::dlerror( ) << std::endl;
61  }
62 #elif defined(__APPLE__)
63  std::string appleName = libPrefix;
64  appleName += libraryName += ".dylib";
65  void* fileHandle = ::dlopen( appleName.c_str( ), RTLD_NOW );
66  if( !quiet && !fileHandle )
67  {
68  std::cerr << ::dlerror( ) << std::endl;
69  }
70 #else
71 #error "unsupported platform"
72 #endif
73 
74  return fileHandle;
75 }
76 
85 inline int FreeSharedLibrary(void*& libHandle)
86 {
87  int result = 0;
88 
89 #if defined( _WIN32 )
90  if (libHandle != 0)
91  result = ::FreeLibrary(reinterpret_cast<HMODULE>(libHandle));
92 #else
93  if( libHandle != 0 )
94  result = ( ::dlclose( libHandle ) == 0 );
95 #endif
96 
97  libHandle = NULL;
98 
99  return result;
100 }
101 
113 inline void* LoadFunctionAddr(void* libHandle, std::string funcName)
114 {
115  if (libHandle == NULL)
116  return NULL;
117 
118 #if defined( _WIN32 )
119  HMODULE fileHandle = reinterpret_cast<HMODULE>(libHandle);
120 
121  void* pFunc = reinterpret_cast<void*>(::GetProcAddress(fileHandle, funcName.c_str()));
122 #else
123  void* pFunc = ::dlsym( libHandle, funcName.c_str( ) );
124 #endif
125 
126  return pFunc;
127 }
128 
129 #endif // _SHAREDLIBRARY_H_
int FreeSharedLibrary(void *&libHandle)
Release the handle to the dynamic library.
Definition: loadDynamicLibrary.hpp:85
void * LoadFunctionAddr(void *libHandle, std::string funcName)
Query for function pointer in library.
Definition: loadDynamicLibrary.hpp:113
void * LoadSharedLibrary(const std::string &libPrefix, std::string libraryName, bool quiet)
Create a platform specific handle to a loaded dynamic library.
Definition: loadDynamicLibrary.hpp:47