libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

commit 712b4e5b6e37e92b70becf07577d39236804bc35
parent c9463383b3ddef1f27a6291bd8908f69fd14476a
Author: Nils Durner <durner@gnunet.org>
Date:   Sun, 27 Feb 2005 20:19:26 +0000

mmap() for Windows

Diffstat:
Msrc/include/platform.h | 6+++++-
Msrc/include/winproc.h | 11++++++++++-
Msrc/main/extractor.c | 23+++--------------------
Msrc/main/winproc.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 100 insertions(+), 22 deletions(-)

diff --git a/src/include/platform.h b/src/include/platform.h @@ -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 diff --git a/src/include/winproc.h b/src/include/winproc.h @@ -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 @@ extern "C" { #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_write(int fildes, const void *buf, size_t nbyte); 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 diff --git a/src/main/extractor.c b/src/main/extractor.c @@ -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 @@ EXTRACTOR_getKeywords (EXTRACTOR_ExtractorList * extractor, 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 @@ EXTRACTOR_getKeywords (EXTRACTOR_ExtractorList * extractor, 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; } diff --git a/src/main/winproc.c b/src/main/winproc.c @@ -1078,6 +1078,88 @@ int _win_pipe(int *phandles) } /** + * 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 )