[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / OS_NS_dlfcn.inl
blob9b75ac3ad5c94e246130665a037222ce37fa2a53
1 // -*- C++ -*-
2 //
3 // $Id: OS_NS_dlfcn.inl 80826 2008-03-04 14:51:23Z wotte $
5 #include "ace/OS_NS_macros.h"
6 #include "ace/OS_NS_errno.h"
7 #include "ace/OS_NS_fcntl.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_unistd.h"
10 #include "ace/Default_Constants.h"
11 #include "ace/os_include/os_fcntl.h"
12 #include "ace/os_include/os_string.h"
14 #if defined (ACE_WIN32) && defined (ACE_HAS_PHARLAP)
15 # include "ace/OS_NS_stdio.h"
16 #endif
18 #if defined (ACE_USES_ASM_SYMBOL_IN_DLSYM)
19 #  include "ace/OS_Memory.h"
20 #  include "ace/OS_NS_string.h"
21 #endif /* ACE_USES_ASM_SYMBOL_IN_DLSYM */
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
25 ACE_INLINE int
26 ACE_OS::dlclose (ACE_SHLIB_HANDLE handle)
28   ACE_OS_TRACE ("ACE_OS::dlclose");
29 #if defined (ACE_LACKS_DLCLOSE)
30   ACE_UNUSED_ARG (handle);
31   return 0;
32 #elif defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
34 # if !defined (ACE_HAS_AUTOMATIC_INIT_FINI)
35   // SunOS4 does not automatically call _fini()!
36   void *ptr;
38   ACE_OSCALL (::dlsym (handle, ACE_TEXT ("_fini")), void *, 0, ptr);
40   if (ptr != 0)
41     (*((int (*)(void)) ptr)) (); // Call _fini hook explicitly.
42 # endif /* ACE_HAS_AUTOMATIC_INIT_FINI */
43 #if defined (_M_UNIX)
44   ACE_OSCALL_RETURN (::_dlclose (handle), int, -1);
45 #else /* _MUNIX */
46     ACE_OSCALL_RETURN (::dlclose (handle), int, -1);
47 #endif /* _M_UNIX */
48 #elif defined (ACE_WIN32)
49   ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::FreeLibrary (handle), ace_result_), int, -1);
50 #elif defined (__hpux)
51   // HP-UX 10.x and 32-bit 11.00 do not pay attention to the ref count
52   // when unloading a dynamic lib.  So, if the ref count is more than
53   // 1, do not unload the lib.  This will cause a library loaded more
54   // than once to not be unloaded until the process runs down, but
55   // that's life.  It's better than unloading a library that's in use.
56   // So far as I know, there's no way to decrement the refcnt that the
57   // kernel is looking at - the shl_descriptor is a copy of what the
58   // kernel has, not the actual struct.  On 64-bit HP-UX using dlopen,
59   // this problem has been fixed.
60   struct shl_descriptor  desc;
61   if (shl_gethandle_r (handle, &desc) == -1)
62     return -1;
63   if (desc.ref_count > 1)
64     return 0;
65 # if defined(__GNUC__) || __cplusplus >= 199707L
66   ACE_OSCALL_RETURN (::shl_unload (handle), int, -1);
67 # else
68   ACE_OSCALL_RETURN (::cxxshl_unload (handle), int, -1);
69 # endif  /* aC++ vs. Hp C++ */
70 #else
71   ACE_UNUSED_ARG (handle);
72   ACE_NOTSUP_RETURN (-1);
73 #endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
76 ACE_INLINE ACE_TCHAR *
77 ACE_OS::dlerror (void)
79   ACE_OS_TRACE ("ACE_OS::dlerror");
80 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
81   const char *err;
82 #   if defined(_M_UNIX)
83   ACE_OSCALL (::_dlerror (), const char *, 0, err);
84 #   else /* _M_UNIX */
85   ACE_OSCALL (::dlerror (), const char *, 0, err);
86 #   endif /* _M_UNIX */
87   if (err == 0)
88     return 0;
89 #   if defined (ACE_USES_WCHAR)
90   const size_t BufLen = 256;
91   static wchar_t buf[BufLen];
92   ACE_OS::strncpy (buf, ACE_TEXT_CHAR_TO_TCHAR (err), BufLen);
93   return buf;
94 #   else
95   return const_cast <char *> (err);
96 #   endif /* ACE_USES_WCHAR */
97 # elif defined (__hpux) || defined (ACE_VXWORKS)
98   //FUZZ: disable check_for_lack_ACE_OS
99   ACE_OSCALL_RETURN (::strerror(errno), char *, 0);
100   //FUZZ: enable check_for_lack_ACE_OS
101 # elif defined (ACE_WIN32)
102   static ACE_TCHAR buf[128];
103 #   if defined (ACE_HAS_PHARLAP)
104   ACE_OS::sprintf (buf, "error code %d", GetLastError());
105 #   else
106   ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
107                           0,
108                           ::GetLastError (),
109                           0,
110                           buf,
111                           sizeof buf / sizeof buf[0],
112                           0);
113 #   endif /* ACE_HAS_PHARLAP */
114   return buf;
115 # else
116   ACE_NOTSUP_RETURN (0);
117 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
120 ACE_INLINE ACE_SHLIB_HANDLE
121 ACE_OS::dlopen (const ACE_TCHAR *fname,
122                 int mode)
124   ACE_OS_TRACE ("ACE_OS::dlopen");
126 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
127   void *handle;
128 #   if defined (ACE_HAS_SGIDLADD)
129   ACE_OSCALL
130     (::sgidladd (ACE_TEXT_ALWAYS_CHAR (fname), mode), void *, 0, handle);
131 #   elif defined (_M_UNIX)
132   ACE_OSCALL
133     (::_dlopen (ACE_TEXT_ALWAYS_CHAR (fname), mode), void *, 0, handle);
134 #   else
135   ACE_OSCALL
136     (::dlopen (ACE_TEXT_ALWAYS_CHAR (fname), mode), void *, 0, handle);
137 #   endif /* ACE_HAS_SGIDLADD */
138 #   if !defined (ACE_HAS_AUTOMATIC_INIT_FINI)
139   if (handle != 0)
140     {
141       void *ptr;
142       // Some systems (e.g., SunOS4) do not automatically call _init(), so
143       // we'll have to call it manually.
145       ACE_OSCALL (::dlsym (handle, ACE_TEXT ("_init")), void *, 0, ptr);
147       if (ptr != 0 && (*((int (*)(void)) ptr)) () == -1) // Call _init hook explicitly.
148         {
149           // Close down the handle to prevent leaks.
150           ::dlclose (handle);
151           return 0;
152         }
153     }
154 #   endif /* ACE_HAS_AUTOMATIC_INIT_FINI */
155   return handle;
156 # elif defined (ACE_WIN32)
157   ACE_UNUSED_ARG (mode);
159   ACE_WIN32CALL_RETURN (ACE_TEXT_LoadLibrary (fname), ACE_SHLIB_HANDLE, 0);
160 # elif defined (__hpux)
162 #   if defined(__GNUC__) || __cplusplus >= 199707L
163   ACE_OSCALL_RETURN (::shl_load(fname, mode, 0L), ACE_SHLIB_HANDLE, 0);
164 #   else
165   ACE_OSCALL_RETURN (::cxxshl_load(fname, mode, 0L), ACE_SHLIB_HANDLE, 0);
166 #   endif  /* aC++ vs. Hp C++ */
167 # elif defined (ACE_VXWORKS) && !defined (__RTP__)
168   MODULE* handle = 0;
169   // Open readonly
170   ACE_HANDLE filehandle = ACE_OS::open (fname,
171                                         O_RDONLY,
172                                         ACE_DEFAULT_FILE_PERMS);
174   if (filehandle != ACE_INVALID_HANDLE)
175     {
176       ACE_OS::last_error(0);
177       ACE_OSCALL ( ::loadModule (filehandle, LOAD_GLOBAL_SYMBOLS|LOAD_COMMON_MATCH_ALL ), MODULE *, 0, handle);
178       int loaderror = ACE_OS::last_error();
179       ACE_OS::close (filehandle);
181       if ( (loaderror != 0) && (handle != 0) )
182         {
183           // ouch something went wrong most likely unresolved externals
184           if (handle)
185             ::unldByModuleId ( handle, 0 );
186           handle = 0;
187         }
188     }
189   else
190     {
191       // couldn't open file
192       handle = 0;
193     }
194   return handle;
195 # else
196   ACE_UNUSED_ARG (fname);
197   ACE_UNUSED_ARG (mode);
198   ACE_NOTSUP_RETURN (0);
199 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
202 ACE_INLINE void *
203 ACE_OS::dlsym (ACE_SHLIB_HANDLE handle,
204                const ACE_TCHAR *sname)
206   ACE_OS_TRACE ("ACE_OS::dlsym");
208 #if defined (ACE_HAS_DLSYM_SEGFAULT_ON_INVALID_HANDLE)
209   // Check if the handle is valid before making any calls using it.
210   if (handle == ACE_SHLIB_INVALID_HANDLE)
211     return 0;
212 #endif /* ACE_HAS_DLSYM_SEGFAULT_ON_INVALID_HANDLE */
214   // Get the correct OS type.
215 #if defined (ACE_HAS_WINCE)
216   // CE (at least thru Pocket PC 2003) offers GetProcAddressW, not ...A, so
217   // we always need a wide-char string.
218   const wchar_t *symbolname = 0;
219 #  if defined (ACE_USES_WCHAR)
220   symbolname = sname;
221 #  else
222   ACE_Ascii_To_Wide sname_xlate (sname);
223   symbolname = sname_xlate.wchar_rep ();
224 #  endif /* ACE_USES_WCHAR */
225 #elif defined (ACE_USES_WCHAR)
226   // WinCE is WCHAR always; other platforms need a char * symbol name
227   ACE_Wide_To_Ascii w_sname (sname);
228   char *symbolname = w_sname.char_rep ();
229 #elif defined (ACE_VXWORKS)
230   char *symbolname = const_cast<char *> (sname);
231 #else
232   const char *symbolname = sname;
233 #endif /* ACE_HAS_WINCE */
235 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
237 #   if defined (ACE_USES_ASM_SYMBOL_IN_DLSYM)
238   int l = ACE_OS::strlen (symbolname) + 2;
239   char *asm_symbolname = 0;
240   ACE_NEW_RETURN (asm_symbolname, char[l], 0);
241   ACE_OS::strcpy (asm_symbolname, "_") ;
242   ACE_OS::strcpy (asm_symbolname + 1, symbolname) ;
243   void *ace_result;
244   ACE_OSCALL (::dlsym (handle, asm_symbolname), void *, 0, ace_result);
245   delete [] asm_symbolname;
246   return ace_result;
247 #   elif defined (_M_UNIX)
248   ACE_OSCALL_RETURN (::_dlsym (handle, symbolname), void *, 0);
249 #   else
250   ACE_OSCALL_RETURN (::dlsym (handle, symbolname), void *, 0);
251 #   endif /* ACE_USES_ASM_SYMBOL_IN_DLSYM */
253 # elif defined (ACE_WIN32)
255   ACE_WIN32CALL_RETURN (::GetProcAddress (handle, symbolname), void *, 0);
257 # elif defined (__hpux)
259   void *value = 0;
260   int status;
261   shl_t _handle = handle;
262   ACE_OSCALL (::shl_findsym(&_handle, symbolname, TYPE_UNDEFINED, &value), int, -1, status);
263   return status == 0 ? value : 0;
265 # elif defined (ACE_VXWORKS) && !defined (__RTP__)
267   // For now we use the VxWorks global symbol table
268   // which resolves the most recently loaded symbols .. which resolve mostly what we want..
269   ACE_UNUSED_ARG (handle);
270   SYM_TYPE symtype;
271   char *value = 0;
272   STATUS status;
273   ACE_OSCALL (::symFindByName(sysSymTbl, symbolname, &value, &symtype), int, -1, status);
275   return status == OK ? reinterpret_cast <void*>(value) : 0;
277 # else
279   ACE_UNUSED_ARG (handle);
280   ACE_UNUSED_ARG (symbolname);
281   ACE_NOTSUP_RETURN (0);
283 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
286 ACE_END_VERSIONED_NAMESPACE_DECL