1 | #ifndef __ZZIP_INTERNAL_MMAP_H |
---|
2 | #define __ZZIP_INTERNAL_MMAP_H |
---|
3 | #include <zzip/types.h> |
---|
4 | |
---|
5 | /* |
---|
6 | * DO NOT USE THIS CODE. |
---|
7 | * |
---|
8 | * It is an internal header file for zziplib that carries some inline |
---|
9 | * functions (or just static members) and a few defines, simply to be |
---|
10 | * able to reuse these across - and have everything in a specific place. |
---|
11 | * |
---|
12 | * Copyright (c) 2002,2003 Guido Draheim |
---|
13 | * All rights reserved, |
---|
14 | * use under the restrictions of the |
---|
15 | * Lesser GNU General Public License |
---|
16 | * or alternatively the restrictions |
---|
17 | * of the Mozilla Public License 1.1 |
---|
18 | */ |
---|
19 | |
---|
20 | #ifdef _USE_MMAP |
---|
21 | #if defined ZZIP_HAVE_SYS_MMAN_H |
---|
22 | #include <sys/mman.h> |
---|
23 | #define USE_POSIX_MMAP 1 |
---|
24 | #elif defined ZZIP_HAVE_WINBASE_H || defined WIN32 |
---|
25 | #include <windows.h> |
---|
26 | #define USE_WIN32_MMAP 1 |
---|
27 | #else |
---|
28 | #undef _USE_MMAP |
---|
29 | #endif |
---|
30 | #endif |
---|
31 | |
---|
32 | /* -------------- specify MMAP function imports --------------------------- */ |
---|
33 | |
---|
34 | #if defined USE_POSIX_MMAP |
---|
35 | #define USE_MMAP 1 |
---|
36 | |
---|
37 | #define _zzip_mmap(user, fd, offs, len) \ |
---|
38 | mmap (0, len, PROT_READ, MAP_SHARED, fd, offs) |
---|
39 | #define _zzip_munmap(user, ptr, len) \ |
---|
40 | munmap (ptr, len) |
---|
41 | #define _zzip_getpagesize(user) getpagesize() |
---|
42 | |
---|
43 | #ifndef MAP_FAILED /* hpux10.20 does not have it */ |
---|
44 | #define MAP_FAILED ((void*)(-1)) |
---|
45 | #endif |
---|
46 | |
---|
47 | #elif defined USE_WIN32_MMAP |
---|
48 | #define USE_MMAP 1 |
---|
49 | #ifndef MAP_FAILED |
---|
50 | #define MAP_FAILED 0 |
---|
51 | #endif |
---|
52 | /* we (ab)use the "*user" variable to store the FileMapping handle */ |
---|
53 | /* which assumes (sizeof(long) == sizeof(HANDLE)) */ |
---|
54 | |
---|
55 | static size_t win32_getpagesize () |
---|
56 | { |
---|
57 | SYSTEM_INFO si; GetSystemInfo (&si); |
---|
58 | return si.dwAllocationGranularity; |
---|
59 | } |
---|
60 | static void* win32_mmap (long* user, int fd, zzip_off_t offs, size_t len) |
---|
61 | { |
---|
62 | if (! user || *user != 1) /* || offs % getpagesize() */ |
---|
63 | return 0; |
---|
64 | { |
---|
65 | HANDLE hFile = (HANDLE)_get_osfhandle(fd); |
---|
66 | if (hFile) |
---|
67 | *user = (int) CreateFileMapping (hFile, 0, PAGE_READONLY, 0, 0, NULL); |
---|
68 | if (*user) |
---|
69 | { |
---|
70 | char* p = 0; |
---|
71 | p = MapViewOfFile(*(HANDLE*)user, FILE_MAP_READ, 0, offs, len); |
---|
72 | if (p) return p + offs; |
---|
73 | CloseHandle (*(HANDLE*)user); *user = 1; |
---|
74 | } |
---|
75 | return MAP_FAILED; |
---|
76 | } |
---|
77 | } |
---|
78 | static void win32_munmap (long* user, char* fd_map, size_t len) |
---|
79 | { |
---|
80 | UnmapViewOfFile (fd_map); |
---|
81 | CloseHandle (*(HANDLE*)user); *user = 1; |
---|
82 | } |
---|
83 | |
---|
84 | #define _zzip_mmap(user, fd, offs, len) \ |
---|
85 | win32_mmap ((long*) &(user), fd, offs, len) |
---|
86 | #define _zzip_munmap(user, ptr, len) \ |
---|
87 | win32_munmap ((long*) &(user), ptr, len) |
---|
88 | #define _zzip_getpagesize(user) win32_getpagesize() |
---|
89 | |
---|
90 | #else /* disable */ |
---|
91 | #define USE_MMAP 0 |
---|
92 | /* USE_MAP is intentional: we expect the compiler to do some "code removal" |
---|
93 | * on any source code enclosed in if (USE_MMAP) {...} i.e. the unreachable |
---|
94 | * branch of an if (0) {....} is not emitted to the final object binary. */ |
---|
95 | |
---|
96 | #ifndef MAP_FAILED |
---|
97 | #define MAP_FAILED 0 |
---|
98 | #endif |
---|
99 | |
---|
100 | #define _zzip_mmap(user, fd, offs, len) (MAP_FAILED) |
---|
101 | #define _zzip_munmap(user, ptr, len) {} |
---|
102 | #define _zzip_getpagesize(user) 1 |
---|
103 | |
---|
104 | #endif /* USE_MMAP defines */ |
---|
105 | |
---|
106 | |
---|
107 | #endif |
---|