new beta-0.90.0
[luatex.git] / source / libs / zziplib / zziplib-src / zzip / __mmap.h
blob5dfc45dcda2ef6979b792cf9c7dc98447ef02979
1 #ifndef __ZZIP_INTERNAL_MMAP_H
2 #define __ZZIP_INTERNAL_MMAP_H
3 #include <zzip/types.h>
5 /*
6 * DO NOT USE THIS CODE.
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.
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
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
32 /* -------------- specify MMAP function imports --------------------------- */
34 #if defined USE_POSIX_MMAP
35 #define USE_MMAP 1
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()
43 #ifndef MAP_FAILED /* hpux10.20 does not have it */
44 #define MAP_FAILED ((void*)(-1))
45 #endif
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 /* and make sure (sizeof(*user) == sizeof(HANDLE)) */
55 static size_t win32_getpagesize (void)
57 SYSTEM_INFO si; GetSystemInfo (&si);
58 return si.dwAllocationGranularity;
60 #ifdef _WIN64
61 static void* win32_mmap (__int64* user, int fd, zzip_off_t offs, size_t len)
62 #else
63 static void* win32_mmap (long* user, int fd, zzip_off_t offs, size_t len)
64 #endif
66 if (! user || *user != 1) /* || offs % getpagesize() */
67 return 0;
69 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
70 if (hFile)
71 #ifdef _WIN64
72 *user = (__int64) CreateFileMapping (hFile, 0, PAGE_READONLY, 0, 0, NULL);
73 #else
74 *user = (long) CreateFileMapping (hFile, 0, PAGE_READONLY, 0, 0, NULL);
75 #endif
76 if (*user)
78 char* p = 0;
79 p = MapViewOfFile(*(HANDLE*)user, FILE_MAP_READ, 0, offs, len);
80 if (p) return p;
81 CloseHandle (*(HANDLE*)user); *user = 1;
83 return MAP_FAILED;
86 #ifdef _WIN64
87 static void win32_munmap (__int64* user, char* fd_map, size_t len)
88 #else
89 static void win32_munmap (long* user, char* fd_map, size_t len)
90 #endif
92 UnmapViewOfFile (fd_map);
93 CloseHandle (*(HANDLE*)user); *user = 1;
95 #ifdef _WIN64
96 #define _zzip_mmap(user, fd, offs, len) \
97 win32_mmap ((__int64*) &(user), fd, offs, len)
98 #define _zzip_munmap(user, ptr, len) \
99 win32_munmap ((__int64*) &(user), ptr, len)
100 #else
101 #define _zzip_mmap(user, fd, offs, len) \
102 win32_mmap ((long*) &(user), fd, offs, len)
103 #define _zzip_munmap(user, ptr, len) \
104 win32_munmap ((long*) &(user), ptr, len)
105 #endif
106 #define _zzip_getpagesize(user) win32_getpagesize()
107 #else /* disable */
108 #define USE_MMAP 0
109 /* USE_MAP is intentional: we expect the compiler to do some "code removal"
110 * on any source code enclosed in if (USE_MMAP) {...} i.e. the unreachable
111 * branch of an if (0) {....} is not emitted to the final object binary. */
113 #ifndef MAP_FAILED
114 #define MAP_FAILED 0
115 #endif
117 #define _zzip_mmap(user, fd, offs, len) (MAP_FAILED)
118 #define _zzip_munmap(user, ptr, len) {}
119 #define _zzip_getpagesize(user) 1
121 #endif /* USE_MMAP defines */
124 #endif