cmDynamicLoader.cxx 8.0 KB

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