gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r337 - in Extractor/src: include main


From: durner
Subject: [GNUnet-SVN] r337 - in Extractor/src: include main
Date: Sun, 27 Feb 2005 12:19:27 -0800 (PST)

Author: durner
Date: 2005-02-27 12:19:26 -0800 (Sun, 27 Feb 2005)
New Revision: 337

Modified:
   Extractor/src/include/platform.h
   Extractor/src/include/winproc.h
   Extractor/src/main/extractor.c
   Extractor/src/main/winproc.c
Log:
mmap() for Windows

Modified: Extractor/src/include/platform.h
===================================================================
--- Extractor/src/include/platform.h    2005-02-27 18:52:43 UTC (rev 336)
+++ Extractor/src/include/platform.h    2005-02-27 20:19:26 UTC (rev 337)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2001 - 2004 Christian Grothoff (and other contributing authors)
+     (C) 2001 - 2005 Christian Grothoff (and other contributing authors)
 
      libextractor is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -77,6 +77,8 @@
  #define READ(f, b, n) read(f, b, n)
  #define GN_FREAD(b, s, c, f) fread(b, s, c, f)
  #define GN_FWRITE(b, s, c, f) fwrite(b, s, c, f)
+ #define MMAP(s, l, p, f, d, o) mmap(s, l, p, f, d, o)
+ #define MUNMAP(s, l) munmap(s, l);
  #define STRERROR(i) strerror(i)
 #else
 
@@ -105,6 +107,8 @@
  #define READ(f, b, n) _win_read(f, b, n)
  #define GN_FREAD(b, s, c, f) _win_fread(b, s, c, f)
  #define GN_FWRITE(b, s, c, f) _win_fwrite(b, s, c, f)
+ #define MMAP(s, l, p, f, d, o) _win_mmap(s, l, p, f, d, o)
+ #define MUNMAP(s, l) _win_munmap(s, l)
  #define STRERROR(i) _win_strerror(i)
 #endif
 

Modified: Extractor/src/include/winproc.h
===================================================================
--- Extractor/src/include/winproc.h     2005-02-27 18:52:43 UTC (rev 336)
+++ Extractor/src/include/winproc.h     2005-02-27 20:19:26 UTC (rev 337)
@@ -1,6 +1,6 @@
 /*
      This file is part of libextractor.
-     (C) 2001, 2002, 2003 Christian Grothoff (and other contributing authors)
+     (C) 2001 - 2005 Christian Grothoff (and other contributing authors)
 
      libextractor is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -195,6 +195,12 @@
 #undef NO_ADDRESS
 #define NO_ADDRESS 4
  
+#define PROT_READ   0x1
+#define PROT_WRITE  0x2
+#define MAP_SHARED  0x1
+#define MAP_PRIVATE  0x2
+#define MAP_FIXED   0x10
+
 struct statfs
 {
   long f_type;                  /* type of filesystem (see below) */
@@ -328,6 +334,9 @@
 int _win_read(int fildes, void *buf, size_t nbyte);
 size_t _win_fwrite(const void *buffer, size_t size, size_t count, FILE 
*stream);
 size_t _win_fread( void *buffer, size_t size, size_t count, FILE *stream );
+void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
+                unsigned long long offset);
+int _win_munmap(void *start, size_t length);
 char *_win_strerror(int errnum);
 
 #if !HAVE_STRNDUP

Modified: Extractor/src/main/extractor.c
===================================================================
--- Extractor/src/main/extractor.c      2005-02-27 18:52:43 UTC (rev 336)
+++ Extractor/src/main/extractor.c      2005-02-27 20:19:26 UTC (rev 337)
@@ -1,6 +1,6 @@
 /*
      This file is part of libextractor.
-     (C) 2002, 2003, 2004 Vidyut Samanta and Christian Grothoff
+     (C) 2002 - 2005 Vidyut Samanta and Christian Grothoff
 
      libextractor is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -574,22 +574,9 @@
     return NULL;
   }
 
-#ifndef MINGW
   if (size > 1* 1024 * 1024 * 1024)
     size = 1 * 1024 * 1024 * 1024; /* do not mmap/read more than 1 GB! */
-  buffer = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, file, 0);
-#else
-  if (size > 250 * 1024 * 1024)
-    size = 250 * 1024 * 1024; /* do not malloc/read more than 250 MB! */
-  buffer = malloc(size + 1);
-  if (buffer == NULL) { /* out of memory? */
-    close(file);
-    return NULL;
-  }
-  buffer[size] = 0;
-  if (size > 0)
-    read(file, buffer, size);
-#endif
+  buffer = MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, file, 0);
   close(file);
   if ( (buffer == NULL) || (buffer == (void *) -1) )
     return NULL;
@@ -598,14 +585,10 @@
     result = extractor->extractMethod (filename, buffer, size, result);
     extractor = extractor->next;
   }
-#ifndef MINGW
   if (size > 0)
-    munmap (buffer, size);
+    MUNMAP (buffer, size);
   else
     free(buffer);
-#else
-  free(buffer);
-#endif
   return result;
 }
 

Modified: Extractor/src/main/winproc.c
===================================================================
--- Extractor/src/main/winproc.c        2005-02-27 18:52:43 UTC (rev 336)
+++ Extractor/src/main/winproc.c        2005-02-27 20:19:26 UTC (rev 337)
@@ -1078,6 +1078,88 @@
 }
 
 /**
+ * map files into memory
+ * @author Cygwin team
+ * @author Nils Durner
+ */
+void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
+                unsigned long long off) {
+  DWORD protect, high, low, access_param;
+  HANDLE h, hFile;
+  SECURITY_ATTRIBUTES sec_none;
+  void *base;
+
+  errno = 0;
+
+  switch(access)
+  {
+    case PROT_WRITE:
+      protect = PAGE_READWRITE;
+      access_param = FILE_MAP_WRITE;
+      break;
+    case PROT_READ:
+      protect = PAGE_READONLY;
+      access_param = FILE_MAP_READ;
+      break;
+    default:
+      protect = PAGE_WRITECOPY;
+      access_param = FILE_MAP_COPY;
+      break;
+  }
+  
+  sec_none.nLength = sizeof(SECURITY_ATTRIBUTES);
+  sec_none.bInheritHandle = TRUE;
+  sec_none.lpSecurityDescriptor = NULL;
+  
+  hFile = (HANDLE) _get_osfhandle(fd);
+  
+  h = CreateFileMapping(hFile, &sec_none, protect, 0, 0, NULL);
+  
+  if (! h)
+  {
+    SetErrnoFromWinError(GetLastError());
+    return (void *) -1;
+  }
+  
+  high = off >> 32;
+  low = off & ULONG_MAX;
+  base = NULL;
+  
+  /* If a non-zero start is given, try mapping using the given address first.
+     If it fails and flags is not MAP_FIXED, try again with NULL address. */
+  if (start)
+    base = MapViewOfFileEx(h, access_param, high, low, len, start);
+  if (!base && !(flags & MAP_FIXED))
+    base = MapViewOfFileEx(h, access_param, high, low, len, NULL);
+  
+  if (!base || ((flags & MAP_FIXED) && base != start))
+  {
+    if (!base)
+      SetErrnoFromWinError(GetLastError());
+    else
+      errno = EINVAL;
+    
+    CloseHandle(h);
+    return (void *) -1;
+  }
+  
+  return base;
+}
+
+/**
+ * Unmap files from memory
+ * @author Cygwin team
+ * @author Nils Durner
+ */
+int _win_munmap(void *start, size_t length)
+{
+  BOOL success = UnmapViewOfFile(start);
+  SetErrnoFromWinError(GetLastError());
+  
+  return success ? 0 : -1;
+}
+
+/**
  * Determine file-access permission.
  */
 int _win_access( const char *path, int mode )





reply via email to

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