win32.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // win32.h -- Helper functions for Microsoft-flavored OSs.
  2. /* Copyright (C) 2002, 2003, 2006 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #ifndef __JV_WIN32_H__
  8. #define __JV_WIN32_H__
  9. // Enable UNICODE support?
  10. #ifdef MINGW_LIBGCJ_UNICODE
  11. #define UNICODE
  12. #define _UNICODE
  13. #endif // MINGW_LIBGCJ_UNICODE
  14. #include <tchar.h>
  15. // Includes
  16. #define WIN32_LEAN_AND_MEAN
  17. // Force Winsock 2 interface.
  18. #include <winsock2.h>
  19. #include <windows.h>
  20. #undef WIN32_LEAN_AND_MEAN
  21. #undef STRICT
  22. #include <ws2tcpip.h>
  23. #include <gcj/cni.h>
  24. #include <jvm.h>
  25. #include <java/util/Properties.h>
  26. #include <io.h>
  27. /* Begin UNICODE Support Classes and Functions */
  28. /* Helper class which creates a temporary, null-terminated,
  29. wide-character C string. */
  30. class _Jv_Win32TempString
  31. {
  32. public:
  33. _Jv_Win32TempString(jstring jstr);
  34. ~_Jv_Win32TempString();
  35. // Accessors
  36. operator LPCTSTR() const
  37. {
  38. return buf_;
  39. }
  40. LPCTSTR buf() const
  41. {
  42. return buf_;
  43. }
  44. LPTSTR buf()
  45. {
  46. return buf_;
  47. }
  48. private:
  49. TCHAR stackbuf_[500];
  50. LPTSTR buf_;
  51. };
  52. // Mimics the JV_TEMP_STRING_UTF macro in jvm.h
  53. #define JV_TEMP_STRING_WIN32(x,y) _Jv_Win32TempString x(y);
  54. // Creates a jstring from a LPCTSTR
  55. extern jstring _Jv_Win32NewString (LPCTSTR pcsz);
  56. /* End UNICODE Helpers */
  57. // Prefix and suffix for shared libraries.
  58. #define _Jv_platform_solib_prefix ""
  59. #define _Jv_platform_solib_suffix ".dll"
  60. // Name of the Process implementation.
  61. #define _Jv_platform_process ::java::lang::Win32Process
  62. // Separator for file name components.
  63. #define _Jv_platform_file_separator ((jchar) '\\')
  64. // Separator for path components.
  65. #define _Jv_platform_path_separator ((jchar) ';')
  66. // List of names for `JNI_OnLoad'. On Win32, JNI_OnLoad is an
  67. // "stdcall" function taking two pointers (8 bytes) as arguments. It
  68. // could also have been exported as "JNI_OnLoad@8" (MinGW) or
  69. // "_JNI_OnLoad@8" (MSVC).
  70. #define _Jv_platform_onload_names \
  71. { "JNI_OnLoad", "JNI_OnLoad@8", "_JNI_OnLoad@8", NULL }
  72. // Type of libffi ABI used by JNICALL methods. NOTE: This must agree
  73. // with the JNICALL definition in jni.h
  74. #define _Jv_platform_ffi_abi FFI_STDCALL
  75. /* Useful helper classes and methods. */
  76. /* A C++ wrapper around a WSAEVENT which closes the event
  77. in its destructor. If dwSelFlags is non-zero, we also
  78. issue an WSAEventSelect on the socket descriptor with
  79. the given flags; this is undone by a corresponding call
  80. to WSAEventSelect(fd, 0, 0) in our destructor. */
  81. class WSAEventWrapper
  82. {
  83. public:
  84. // Default constructor. Call init() after this.
  85. WSAEventWrapper();
  86. WSAEventWrapper(int fd, DWORD dwSelFlags);
  87. ~WSAEventWrapper();
  88. // Used for two-step initialization after calling
  89. // default constructor.
  90. void init(int fd, DWORD dwSelFlags);
  91. int getFD()
  92. {
  93. return m_fd;
  94. }
  95. WSAEVENT getEventHandle()
  96. {
  97. return m_hEvent;
  98. }
  99. private:
  100. WSAEVENT m_hEvent;
  101. int m_fd;
  102. DWORD m_dwSelFlags;
  103. };
  104. // Error string text. The int argument is compatible
  105. // with both int WSAGetLastError() and DWORD GetLastError()
  106. // I tried avoiding having to pass the error explicitly, but
  107. // it didn't work this was invoked with say
  108. // throw new SomeException(_Jv_WinStrError()).
  109. extern jstring
  110. _Jv_WinStrError (LPCTSTR lpszPrologue, int nErrorCode);
  111. extern jstring
  112. _Jv_WinStrError (int nErrorCode);
  113. extern void
  114. _Jv_ThrowIOException (DWORD dwErrorCode);
  115. extern void
  116. _Jv_ThrowIOException ();
  117. extern void
  118. _Jv_ThrowSocketException (DWORD dwErrorCode);
  119. extern void
  120. _Jv_ThrowSocketException ();
  121. // Platform implementation
  122. extern void _Jv_platform_initialize (void);
  123. extern void _Jv_platform_initProperties (java::util::Properties*);
  124. extern jlong _Jv_platform_gettimeofday ();
  125. extern jlong _Jv_platform_nanotime ();
  126. extern int _Jv_pipe (int filedes[2]);
  127. extern void
  128. _Jv_platform_close_on_exec (HANDLE h);
  129. #ifdef JV_HASH_SYNCHRONIZATION
  130. /* Suspends the execution of the current thread for the specified
  131. number of microseconds. Tries to emulate the behaviour of usleep()
  132. on UNIX and provides a granularity of 1 millisecond. */
  133. inline void
  134. _Jv_platform_usleep (unsigned long usecs)
  135. {
  136. if (usecs > 0UL)
  137. {
  138. unsigned long millis = ((usecs + 999UL) / 1000UL);
  139. Sleep (millis);
  140. }
  141. }
  142. #endif /* JV_HASH_SYNCHRONIZATION */
  143. // Forward declaration. See java-stack.h for definition.
  144. struct _Jv_AddrInfo;
  145. // Given an address, determine the executable or shared object that defines
  146. // it and the nearest named symbol.
  147. extern int _Jv_platform_dladdr (void *addr, _Jv_AddrInfo *info);
  148. #endif /* __JV_WIN32_H__ */