| # Gaudi Framework, version v21r7 |

Home |  | Generated: 22 Jan 2010

# DsoUtils.h

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005
    00006 //  note: This, I believe, should be part of Reflex::SharedLibrary
    00007
    00008 #ifndef GAUDIKERNEL_DSOUTILS_H
    00009 #define GAUDIKERNEL_DSOUTILS_H
    00010
    00011 // STL includes
    00012 #include <string>
    00013
    00014 #include "GaudiKernel/System.h"
    00015
    00016 // Reflex includes
    00017 #include "Reflex/Reflex.h"
    00018
    00019 namespace DsoUtils {
    00020
    00021 inline std::string libNativeName( const std::string& libName )
    00022 {
    00023 #if defined(_WIN32)
    00024   return libName+".dll";
    00025 #elif defined(__linux) || defined(__APPLE__)
    00026   return "lib"+libName+".so";
    00027 #else
    00028   // variant of the GIGO design pattern
    00029   return libName;
    00030 #endif
    00031 }
    00032
    00033 #ifdef _GNU_SOURCE
    00034 #include <dlfcn.h>
    00035 static std::string dsoName( const ROOT::Reflex::Member& mem )
    00036 {
    00037   Dl_info info;
    00038   if (dladdr (
    00039 #if __GNUC__ < 4
    00040       (void*)mem.Stubfunction()
    00041 #else
    00042       System::FuncPtrCast<void*>(mem.Stubfunction())
    00043 #endif
    00044       , &info) == 0)
    00045     return "";
    00046
    00047   const char* pos = strrchr (info.dli_fname, '/');
    00048   if (pos)
    00049     ++pos;
    00050   else
    00051     pos = info.dli_fname;
    00052   return pos;
    00053 }
    00054 #elif defined(_WIN32)
    00055 #include <windows.h>
    00056
    00057 static std::string dsoName( const ROOT::Reflex::Member& mem )
    00058 {
    00059   void* addr = (void*)(mem.Stubfunction());
    00060   if (addr) {
    00061     MEMORY_BASIC_INFORMATION mbi;
    00062     if ( VirtualQuery(addr, &mbi, sizeof(mbi)) )    {
    00063       HMODULE h_module = (HMODULE)mbi.AllocationBase;
    00064       char mod[1024];
    00065       if( GetModuleFileName(h_module, mod, sizeof(mod)) ) {
    00066         const char* pos = strrchr (mod, '\\');
    00067         if (pos)
    00068           ++pos;
    00069         else
    00070           pos = mod;
    00071         return pos;
    00072       }
    00073     }
    00074   }
    00075   return "";
    00076 }
    00077
    00078 #else // dummy implementation for unknown platforms
    00079 static std::string dsoName( const ROOT::Reflex::Member& )
    00080 {
    00081   return "";
    00082 }
    00083
    00084 #endif
    00085
    00086 static bool inDso( const ROOT::Reflex::Member& mem,
    00087                     const std::string& dsoname )
    00088 {
    00089 #ifdef _WIN32
    00090   char sep = '\\';
    00091 #else
    00092   char sep = '/';
    00093 #endif
    00094
    00095   std::string srcname = dsoName(mem);
    00096   if (srcname.empty()) {
    00097     // we do not know the name of the library, let's guess it's OK
    00098     return true;
    00099   }
    00100
    00101   std::string::size_type pos = dsoname.find_last_of(sep);
    00102   std::string curname;
    00103   if (std::string::npos == pos) {
    00104     curname = dsoname;
    00105   } else {
    00106     curname = dsoname.substr(pos+1);
    00107   }
    00108
    00109   return srcname == curname;
    00110 }
    00111
    00112 } // end namespace DsoUtils
    00113
    00114 #endif // not GAUDIKERNEL_DSOUTILS_H
    00115