gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r431 - Extractor/src/main


From: durner
Subject: [GNUnet-SVN] r431 - Extractor/src/main
Date: Sun, 13 Mar 2005 12:34:17 -0800 (PST)

Author: durner
Date: 2005-03-13 12:34:12 -0800 (Sun, 13 Mar 2005)
New Revision: 431

Modified:
   Extractor/src/main/extractor.c
   Extractor/src/main/winproc.c
Log:
initialize POSIX emulation properly and close mmaped() file under Windows

Modified: Extractor/src/main/extractor.c
===================================================================
--- Extractor/src/main/extractor.c      2005-03-12 22:09:39 UTC (rev 430)
+++ Extractor/src/main/extractor.c      2005-03-13 20:34:12 UTC (rev 431)
@@ -181,16 +181,6 @@
 
 /* ************library initialization ***************** */
 
-#ifdef MINGW
-void __attribute__ ((constructor)) le_win_init(void) {
-  InitWinEnv();
-}
-
-void __attribute__ ((destructor)) le_win_fini(void) {
-  ShutdownWinEnv();
-}
-#endif
-
 static char * old_dlsearchpath = NULL;
 
 /* using libtool, needs init! */
@@ -221,12 +211,19 @@
   if (strstr (lt_dlgetsearchpath (), PLUGIN_PATH) == NULL)
     lt_dladdsearchdir (PLUGIN_PATH);
 #endif
+#ifdef MINGW
+  InitWinEnv();
+#endif
 }
 
 void __attribute__ ((destructor)) le_ltdl_fini(void) {
   lt_dlsetsearchpath(old_dlsearchpath);
   if (old_dlsearchpath != NULL)
     free(old_dlsearchpath);
+#ifdef MINGW
+  ShutdownWinEnv();
+#endif    
+
   lt_dlexit ();
 }
 

Modified: Extractor/src/main/winproc.c
===================================================================
--- Extractor/src/main/winproc.c        2005-03-12 22:09:39 UTC (rev 430)
+++ Extractor/src/main/winproc.c        2005-03-13 20:34:12 UTC (rev 431)
@@ -42,6 +42,11 @@
   gettext_noop("Unknown resolver error")              /* errno > 4 */
 };
 
+typedef struct {
+  char *pStart;
+  HANDLE hMapping;
+} TMapping;
+
 static char szRootDir[_MAX_PATH + 1];
 static long lRootDirLen;
 static char szHomeDir[_MAX_PATH + 2];
@@ -51,6 +56,9 @@
 unsigned int uiSockCount = 0;
 Winsock *pSocks;
 static char __langinfo[251];
+static unsigned int uiMappingsCount = 0;
+static TMapping *pMappings;
+HANDLE hMappingsLock;
 
 static HINSTANCE hNTDLL, hIphlpapi;
 TNtQuerySystemInformation GNNtQuerySystemInformation;
@@ -428,6 +436,11 @@
     exit(1);
   }
 
+  /* To keep track of mapped files */
+  pMappings = (TMapping *) malloc(sizeof(TMapping));
+  pMappings[0].pStart = NULL;
+  hMappingsLock = CreateMutex(NULL, FALSE, NULL);
+
   /* Open files in binary mode */
   _fmode = _O_BINARY;
   
@@ -475,6 +488,9 @@
  */
 void ShutdownWinEnv()
 {
+  free(pMappings);
+  CloseHandle(hMappingsLock);  
+
   FreeLibrary(hNTDLL);
   FreeLibrary(hIphlpapi);
 }
@@ -1090,6 +1106,8 @@
   HANDLE h, hFile;
   SECURITY_ATTRIBUTES sec_none;
   void *base;
+  BOOL bFound = FALSE;
+  unsigned int uiIndex;
 
   errno = 0;
 
@@ -1144,7 +1162,43 @@
     CloseHandle(h);
     return (void *) -1;
   }
+
+  /* Save mapping handle */
+  WaitForSingleObject(hMappingsLock, INFINITE);
+
+  for(uiIndex = 0; uiIndex <= uiMappingsCount; uiIndex++)
+  {
+    if (pMappings[uiIndex].pStart == base)
+    {
+      bFound = 1;
+      break;
+    }
+  }
   
+  if (! bFound)
+  {
+    uiIndex = 0;
+    
+    while(TRUE)
+    {
+      if (pMappings[uiIndex].pStart == NULL)
+      {
+        pMappings[uiIndex].pStart = base;
+        pMappings[uiIndex].hMapping = h;
+      }
+      if (uiIndex == uiMappingsCount)
+      {
+        uiMappingsCount++;
+        pMappings = (TMapping *) realloc(pMappings, (uiMappingsCount + 1) * 
sizeof(TMapping));
+        pMappings[uiMappingsCount].pStart = NULL;
+        
+        break;
+      }
+      uiIndex++;
+    }
+  }
+  ReleaseMutex(hMappingsLock);
+  
   return base;
 }
 
@@ -1155,9 +1209,31 @@
  */
 int _win_munmap(void *start, size_t length)
 {
+  unsigned uiIndex;
   BOOL success = UnmapViewOfFile(start);
   SetErrnoFromWinError(GetLastError());
+
+  if (success)
+  {
+    /* Release mapping handle */
+    WaitForSingleObject(hMappingsLock, INFINITE);
   
+    for(uiIndex = 0; uiIndex <= uiMappingsCount; uiIndex++)
+    {
+      if (pMappings[uiIndex].pStart == start)
+      {
+        success = CloseHandle(pMappings[uiIndex].hMapping);
+        SetErrnoFromWinError(GetLastError());
+        pMappings[uiIndex].pStart = NULL;
+        pMappings[uiIndex].hMapping = NULL;
+
+        break;
+      }
+    }
+    
+    ReleaseMutex(hMappingsLock);
+  }
+  
   return success ? 0 : -1;
 }
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]