cmDynamicLoader.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. return ret;
  68. }
  69. void cmDynamicLoaderCache::FlushCache()
  70. {
  71. for ( std::map<std::string, cmLibHandle>::iterator it = m_CacheMap.begin();
  72. it != m_CacheMap.end(); it++ )
  73. {
  74. cmDynamicLoader::CloseLibrary(it->second);
  75. }
  76. delete cmDynamicLoaderCache::Instance;
  77. cmDynamicLoaderCache::Instance = 0;
  78. }
  79. cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
  80. {
  81. if ( !cmDynamicLoaderCache::Instance )
  82. {
  83. cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
  84. }
  85. return cmDynamicLoaderCache::Instance;
  86. }
  87. // ---------------------------------------------------------------
  88. // 1. Implementation for HPUX machines
  89. #ifdef __hpux
  90. #define CMDYNAMICLOADER_DEFINED 1
  91. #include <dl.h>
  92. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  93. {
  94. cmLibHandle lh;
  95. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  96. {
  97. return lh;
  98. }
  99. lh = shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L);
  100. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  101. return lh;
  102. }
  103. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  104. {
  105. return 0;
  106. }
  107. cmDynamicLoaderFunction
  108. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  109. {
  110. void* addr;
  111. int status;
  112. status = shl_findsym (&lib, sym, TYPE_PROCEDURE, &addr);
  113. void* result = (status < 0) ? (void*)0 : addr;
  114. // Hack to cast pointer-to-data to pointer-to-function.
  115. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  116. }
  117. const char* cmDynamicLoader::LibPrefix()
  118. {
  119. return "lib";
  120. }
  121. const char* cmDynamicLoader::LibExtension()
  122. {
  123. return ".sl";
  124. }
  125. const char* cmDynamicLoader::LastError()
  126. {
  127. return 0;
  128. }
  129. #endif
  130. // ---------------------------------------------------------------
  131. // 2. Implementation for Darwin (including OSX) Machines
  132. #ifdef __APPLE__
  133. #define CMDYNAMICLOADER_DEFINED
  134. #include <mach-o/dyld.h>
  135. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  136. {
  137. cmLibHandle lh;
  138. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  139. {
  140. return lh;
  141. }
  142. NSObjectFileImageReturnCode rc;
  143. NSObjectFileImage image;
  144. rc = NSCreateObjectFileImageFromFile(libname, &image);
  145. lh = NSLinkModule(image, libname, TRUE);
  146. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  147. return lh;
  148. }
  149. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  150. {
  151. return 0;
  152. }
  153. cmDynamicLoaderFunction
  154. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  155. {
  156. void *result=0;
  157. if(NSIsSymbolNameDefined(sym))
  158. {
  159. NSSymbol symbol= NSLookupAndBindSymbol(sym);
  160. if(symbol)
  161. {
  162. result = NSAddressOfSymbol(symbol);
  163. }
  164. }
  165. // Hack to cast pointer-to-data to pointer-to-function.
  166. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  167. }
  168. const char* cmDynamicLoader::LibPrefix()
  169. {
  170. return "lib";
  171. }
  172. const char* cmDynamicLoader::LibExtension()
  173. {
  174. return ".so";
  175. }
  176. const char* cmDynamicLoader::LastError()
  177. {
  178. return 0;
  179. }
  180. #endif
  181. // ---------------------------------------------------------------
  182. // 3. Implementation for Windows win32 code
  183. #ifdef _WIN32
  184. #include <windows.h>
  185. #define CMDYNAMICLOADER_DEFINED 1
  186. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  187. {
  188. cmLibHandle lh;
  189. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  190. {
  191. return lh;
  192. }
  193. #ifdef UNICODE
  194. wchar_t *libn = new wchar_t [mbstowcs(NULL, libname, 32000)];
  195. mbstowcs(libn, libname, 32000);
  196. cmLibHandle ret = LoadLibrary(libn);
  197. delete [] libn;
  198. lh = ret;
  199. #else
  200. lh = LoadLibrary(libname);
  201. #endif
  202. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  203. return lh;
  204. }
  205. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  206. {
  207. return (int)FreeLibrary(lib);
  208. }
  209. cmDynamicLoaderFunction
  210. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  211. {
  212. void* result = 0;
  213. #ifdef UNICODE
  214. wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
  215. mbstowcs(wsym, sym, 32000);
  216. void *ret = GetProcAddress(lib, wsym);
  217. delete [] wsym;
  218. result = ret;
  219. #else
  220. result = GetProcAddress(lib, sym);
  221. #endif
  222. // Hack to cast pointer-to-data to pointer-to-function.
  223. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  224. }
  225. const char* cmDynamicLoader::LibPrefix()
  226. {
  227. return "";
  228. }
  229. const char* cmDynamicLoader::LibExtension()
  230. {
  231. return ".dll";
  232. }
  233. const char* cmDynamicLoader::LastError()
  234. {
  235. LPVOID lpMsgBuf;
  236. FormatMessage(
  237. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  238. NULL,
  239. GetLastError(),
  240. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  241. (LPTSTR) &lpMsgBuf,
  242. 0,
  243. NULL
  244. );
  245. // Free the buffer.
  246. LocalFree( lpMsgBuf );
  247. static char* str = 0;
  248. delete [] str;
  249. str = strcpy(new char[strlen((char*)lpMsgBuf)+1], (char*)lpMsgBuf);
  250. return str;
  251. }
  252. #endif
  253. // ---------------------------------------------------------------
  254. // 4. Implementation for default UNIX machines.
  255. // if nothing has been defined then use this
  256. #ifndef CMDYNAMICLOADER_DEFINED
  257. #define CMDYNAMICLOADER_DEFINED
  258. // Setup for most unix machines
  259. #include <dlfcn.h>
  260. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  261. {
  262. cmLibHandle lh;
  263. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  264. {
  265. return lh;
  266. }
  267. lh = dlopen(libname, RTLD_LAZY);
  268. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  269. return lh;
  270. }
  271. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  272. {
  273. return (int)dlclose(lib);
  274. }
  275. cmDynamicLoaderFunction
  276. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  277. {
  278. void* result = dlsym(lib, sym);
  279. // Hack to cast pointer-to-data to pointer-to-function.
  280. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  281. }
  282. const char* cmDynamicLoader::LibPrefix()
  283. {
  284. return "lib";
  285. }
  286. const char* cmDynamicLoader::LibExtension()
  287. {
  288. #ifdef __CYGWIN__
  289. return ".dll";
  290. #else
  291. return ".so";
  292. #endif
  293. }
  294. const char* cmDynamicLoader::LastError()
  295. {
  296. return dlerror();
  297. }
  298. #endif
  299. void cmDynamicLoader::FlushCache()
  300. {
  301. cmDynamicLoaderCache::GetInstance()->FlushCache();
  302. }