OSDN Git Service

2010-11-27 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libjava / win32.cc
index ec089a5..6a1c2c7 100644 (file)
@@ -1,6 +1,6 @@
 // win32.cc - Helper functions for Microsoft-flavored OSs.
 
-/* Copyright (C) 2002, 2003  Free Software Foundation
+/* Copyright (C) 2002, 2003, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -12,8 +12,11 @@ details.  */
 #include <platform.h>
 #include <sys/timeb.h>
 #include <stdlib.h>
+#include <string.h>
 #include <fcntl.h>
 
+#include <java-stack.h>
+
 #include <java/lang/ArithmeticException.h>
 #include <java/lang/UnsupportedOperationException.h>
 #include <java/io/IOException.h>
@@ -143,11 +146,24 @@ _Jv_Win32TempString::~_Jv_Win32TempString()
 }
 
 // class WSAEventWrapper
+WSAEventWrapper::WSAEventWrapper ():
+  m_hEvent(0),
+  m_fd(0),
+  m_dwSelFlags(0)
+{}
+
 WSAEventWrapper::WSAEventWrapper (int fd, DWORD dwSelFlags):
   m_hEvent(0),
-  m_fd(fd),
-  m_dwSelFlags(dwSelFlags)
+  m_fd(0),
+  m_dwSelFlags(0)
+{
+  init(fd, dwSelFlags);
+}
+
+void WSAEventWrapper::init(int fd, DWORD dwSelFlags)
 {
+  m_fd = fd;
+  m_dwSelFlags = dwSelFlags;
   m_hEvent = WSACreateEvent ();
   if (dwSelFlags)
     WSAEventSelect(fd, m_hEvent, dwSelFlags);
@@ -243,7 +259,7 @@ _Jv_platform_initialize (void)
 {
   // Initialise winsock for networking
   WSADATA data;
-  if (WSAStartup (MAKEWORD (1, 1), &data))
+  if (WSAStartup (MAKEWORD (2, 2), &data))
     MessageBox (NULL, _T("Error initialising winsock library."), _T("Error"),
     MB_OK | MB_ICONEXCLAMATION);
 
@@ -266,19 +282,10 @@ _Jv_platform_gettimeofday ()
   return t.time * 1000LL + t.millitm;
 }
 
-// The following definitions "fake out" mingw to think that -mthreads
-// was enabled and that mingwthr.dll was linked. GCJ-compiled
-// applications don't need this helper library because we can safely
-// detect thread death (return from Thread.run()).
-
-int _CRT_MT = 1;
-
-extern "C" int
-__mingwthr_key_dtor (DWORD, void (*) (void *))
+jlong
+_Jv_platform_nanotime ()
 {
-  // FIXME: for now we do nothing; this causes a memory leak of
-  //        approximately 24 bytes per thread created.
-  return 0;
+  return _Jv_platform_gettimeofday () * 1000LL;
 }
 
 static bool dirExists (LPCTSTR dir)
@@ -423,41 +430,6 @@ _Jv_platform_initProperties (java::util::Properties* newprops)
     }
 }
 
-/* Store up to SIZE return address of the current program state in
-   ARRAY and return the exact number of values stored.  */
-int
-backtrace (void **__array, int __size)
-{
-  register void *_ebp __asm__ ("ebp");
-  register void *_esp __asm__ ("esp");
-  unsigned int *rfp;
-
-  int i=0;
-  for (rfp = *(unsigned int**)_ebp;
-       rfp && i < __size;
-       rfp = *(unsigned int **)rfp)
-    {
-      int diff = *rfp - (unsigned int)rfp;
-      if ((void*)rfp < _esp || diff > 4 * 1024 || diff < 0) break;
-
-    __array[i++] = (void*)(rfp[1]-4);
-  }
-  return i;
-}
-
-int
-_Jv_select (int n, fd_set *readfds, fd_set  *writefds,
-      fd_set *exceptfds, struct timeval *timeout)
-{
-  int r = ::select (n, readfds, writefds, exceptfds, timeout);
-  if (r == SOCKET_ERROR)
-    {
-      DWORD dwErrorCode = WSAGetLastError ();
-      throw new java::io::IOException (_Jv_WinStrError (dwErrorCode));
-    }
-  return r;      
-}
-
 int
 _Jv_pipe (int filedes[2])
 {
@@ -471,3 +443,42 @@ _Jv_platform_close_on_exec (HANDLE h)
   // no effect under Win9X.
   SetHandleInformation (h, HANDLE_FLAG_INHERIT, 0);
 }
+
+// Given an address, find the object that defines it and the nearest
+// defined symbol to that address.  Returns 0 if no object defines this
+// address.
+int
+_Jv_platform_dladdr (void *addr, _Jv_AddrInfo *info)
+{
+  // Since we do not have dladdr() on Windows, we use a trick involving
+  // VirtualQuery() to find the module (EXE or DLL) that contains a given
+  // address.  This was taken from Matt Pietrek's "Under the Hood" column
+  // for the April 1997 issue of Microsoft Systems Journal.
+
+  MEMORY_BASIC_INFORMATION mbi;
+  if (!VirtualQuery (addr, &mbi, sizeof (mbi)))
+  {
+    return 0;
+  }
+  
+  HMODULE hMod = (HMODULE) mbi.AllocationBase;
+
+  char moduleName[MAX_PATH];
+
+  // FIXME: We explicitly use the ANSI variant of the function here.
+  if (!GetModuleFileNameA (hMod, moduleName, sizeof (moduleName)))
+  {
+    return 0;
+  }
+
+  char *file_name = (char *)(malloc (strlen (moduleName) + 1));
+  strcpy (file_name, moduleName);
+  info->file_name = file_name;
+
+  // FIXME.
+  info->base = NULL;
+  info->sym_name = NULL;
+  info->sym_addr = NULL;
+
+  return 1;
+}