cmDynamicLoader.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDynamicLoader.h"
  14. // This file is actually several different implementations.
  15. // 1. HP machines which uses shl_load
  16. // 2. Apple OSX which uses NSLinkModule
  17. // 3. Windows which uses LoadLibrary
  18. // 4. Most unix systems which use dlopen (default )
  19. // Each part of the ifdef contains a complete implementation for
  20. // the static methods of cmDynamicLoader.
  21. class cmDynamicLoaderCache
  22. {
  23. public:
  24. ~cmDynamicLoaderCache();
  25. void CacheFile(const char* path, const cmLibHandle&);
  26. bool GetCacheFile(const char* path, cmLibHandle&);
  27. bool FlushCache(const char* path);
  28. void FlushCache();
  29. static cmDynamicLoaderCache* GetInstance();
  30. private:
  31. std::map<std::string, cmLibHandle> m_CacheMap;
  32. static cmDynamicLoaderCache* Instance;
  33. };
  34. cmDynamicLoaderCache* cmDynamicLoaderCache::Instance = 0;
  35. cmDynamicLoaderCache::~cmDynamicLoaderCache()
  36. {
  37. }
  38. void cmDynamicLoaderCache::CacheFile(const char* path, const cmLibHandle& p)
  39. {
  40. cmLibHandle h;
  41. if ( this->GetCacheFile(path, h) )
  42. {
  43. this->FlushCache(path);
  44. }
  45. this->m_CacheMap[path] = p;
  46. }
  47. bool cmDynamicLoaderCache::GetCacheFile(const char* path, cmLibHandle& p)
  48. {
  49. std::map<std::string, cmLibHandle>::iterator it = m_CacheMap.find(path);
  50. if ( it != m_CacheMap.end() )
  51. {
  52. p = it->second;
  53. return true;
  54. }
  55. return false;
  56. }
  57. bool cmDynamicLoaderCache::FlushCache(const char* path)
  58. {
  59. std::map<std::string, cmLibHandle>::iterator it = m_CacheMap.find(path);
  60. bool ret = false;
  61. if ( it != m_CacheMap.end() )
  62. {
  63. cmDynamicLoader::CloseLibrary(it->second);
  64. m_CacheMap.erase(it);
  65. ret = true;
  66. }
  67. delete cmDynamicLoaderCache::Instance;
  68. cmDynamicLoaderCache::Instance = 0;
  69. return ret;
  70. }
  71. void cmDynamicLoaderCache::FlushCache()
  72. {
  73. for ( std::map<std::string, cmLibHandle>::iterator it = m_CacheMap.begin();
  74. it != m_CacheMap.end(); it++ )
  75. {
  76. cmDynamicLoader::CloseLibrary(it->second);
  77. }
  78. m_CacheMap.erase(m_CacheMap.begin(), m_CacheMap.end());
  79. }
  80. cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
  81. {
  82. if ( !cmDynamicLoaderCache::Instance )
  83. {
  84. cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
  85. }
  86. return cmDynamicLoaderCache::Instance;
  87. }
  88. // ---------------------------------------------------------------
  89. // 1. Implementation for HPUX machines
  90. #ifdef __hpux
  91. #define CMDYNAMICLOADER_DEFINED 1
  92. #include <dl.h>
  93. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  94. {
  95. cmLibHandle lh;
  96. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  97. {
  98. return lh;
  99. }
  100. lh = shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L);
  101. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  102. return lh;
  103. }
  104. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  105. {
  106. return 0;
  107. }
  108. cmDynamicLoaderFunction
  109. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  110. {
  111. void* addr;
  112. int status;
  113. status = shl_findsym (&lib, sym, TYPE_PROCEDURE, &addr);
  114. void* result = (status < 0) ? (void*)0 : addr;
  115. // Hack to cast pointer-to-data to pointer-to-function.
  116. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  117. }
  118. const char* cmDynamicLoader::LibPrefix()
  119. {
  120. return "lib";
  121. }
  122. const char* cmDynamicLoader::LibExtension()
  123. {
  124. return ".sl";
  125. }
  126. const char* cmDynamicLoader::LastError()
  127. {
  128. return 0;
  129. }
  130. #endif
  131. // ---------------------------------------------------------------
  132. // 2. Implementation for Darwin (including OSX) Machines
  133. #ifdef __APPLE__
  134. #define CMDYNAMICLOADER_DEFINED
  135. #include <mach-o/dyld.h>
  136. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  137. {
  138. cmLibHandle lh;
  139. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  140. {
  141. return lh;
  142. }
  143. NSObjectFileImageReturnCode rc;
  144. NSObjectFileImage image;
  145. rc = NSCreateObjectFileImageFromFile(libname, &image);
  146. lh = NSLinkModule(image, libname, TRUE);
  147. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  148. return lh;
  149. }
  150. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  151. {
  152. return 0;
  153. }
  154. cmDynamicLoaderFunction
  155. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  156. {
  157. void *result=0;
  158. if(NSIsSymbolNameDefined(sym))
  159. {
  160. NSSymbol symbol= NSLookupAndBindSymbol(sym);
  161. if(symbol)
  162. {
  163. result = NSAddressOfSymbol(symbol);
  164. }
  165. }
  166. // Hack to cast pointer-to-data to pointer-to-function.
  167. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  168. }
  169. const char* cmDynamicLoader::LibPrefix()
  170. {
  171. return "lib";
  172. }
  173. const char* cmDynamicLoader::LibExtension()
  174. {
  175. return ".so";
  176. }
  177. const char* cmDynamicLoader::LastError()
  178. {
  179. return 0;
  180. }
  181. #endif
  182. // ---------------------------------------------------------------
  183. // 3. Implementation for Windows win32 code
  184. #ifdef _WIN32
  185. #include <windows.h>
  186. #define CMDYNAMICLOADER_DEFINED 1
  187. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  188. {
  189. cmLibHandle lh;
  190. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  191. {
  192. return lh;
  193. }
  194. #ifdef UNICODE
  195. wchar_t *libn = new wchar_t [mbstowcs(NULL, libname, 32000)];
  196. mbstowcs(libn, libname, 32000);
  197. cmLibHandle ret = LoadLibrary(libn);
  198. delete [] libn;
  199. lh = ret;
  200. #else
  201. lh = LoadLibrary(libname);
  202. #endif
  203. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  204. return lh;
  205. }
  206. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  207. {
  208. return (int)FreeLibrary(lib);
  209. }
  210. cmDynamicLoaderFunction
  211. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  212. {
  213. void* result = 0;
  214. #ifdef UNICODE
  215. wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
  216. mbstowcs(wsym, sym, 32000);
  217. void *ret = GetProcAddress(lib, wsym);
  218. delete [] wsym;
  219. result = ret;
  220. #else
  221. result = GetProcAddress(lib, sym);
  222. #endif
  223. // Hack to cast pointer-to-data to pointer-to-function.
  224. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  225. }
  226. const char* cmDynamicLoader::LibPrefix()
  227. {
  228. return "";
  229. }
  230. const char* cmDynamicLoader::LibExtension()
  231. {
  232. return ".dll";
  233. }
  234. const char* cmDynamicLoader::LastError()
  235. {
  236. LPVOID lpMsgBuf;
  237. FormatMessage(
  238. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  239. NULL,
  240. GetLastError(),
  241. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  242. (LPTSTR) &lpMsgBuf,
  243. 0,
  244. NULL
  245. );
  246. // Free the buffer.
  247. LocalFree( lpMsgBuf );
  248. static char* str = 0;
  249. delete [] str;
  250. str = strcpy(new char[strlen((char*)lpMsgBuf)+1], (char*)lpMsgBuf);
  251. return str;
  252. }
  253. #endif
  254. // ---------------------------------------------------------------
  255. // 4. Implementation for default UNIX machines.
  256. // if nothing has been defined then use this
  257. #ifndef CMDYNAMICLOADER_DEFINED
  258. #define CMDYNAMICLOADER_DEFINED
  259. // Setup for most unix machines
  260. #include <dlfcn.h>
  261. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  262. {
  263. cmLibHandle lh;
  264. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  265. {
  266. return lh;
  267. }
  268. lh = dlopen(libname, RTLD_LAZY);
  269. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  270. return lh;
  271. }
  272. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  273. {
  274. return (int)dlclose(lib);
  275. }
  276. cmDynamicLoaderFunction
  277. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  278. {
  279. void* result = dlsym(lib, sym);
  280. // Hack to cast pointer-to-data to pointer-to-function.
  281. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  282. }
  283. const char* cmDynamicLoader::LibPrefix()
  284. {
  285. return "lib";
  286. }
  287. const char* cmDynamicLoader::LibExtension()
  288. {
  289. #ifdef __CYGWIN__
  290. return ".dll";
  291. #else
  292. return ".so";
  293. #endif
  294. }
  295. const char* cmDynamicLoader::LastError()
  296. {
  297. return dlerror();
  298. }
  299. #endif
  300. void cmDynamicLoader::FlushCache()
  301. {
  302. cmDynamicLoaderCache::GetInstance()->FlushCache();
  303. }