Debian Codegraph @

libs/zziplib/zziplib-src/zzip/__mmap.h

raw
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) Guido Draheim, use under copyleft (LGPL,MPL)
13 */
14
15 #ifdef _USE_MMAP
16 #if defined ZZIP_HAVE_SYS_MMAN_H
17 #include <sys/mman.h>
18 #define USE_POSIX_MMAP 1
19 #elif defined ZZIP_HAVE_WINBASE_H || defined WIN32
20 #include <windows.h>
21 #define USE_WIN32_MMAP 1
22 #else
23 #undef _USE_MMAP
24 #endif
25 #endif
26
27 /* -------------- specify MMAP function imports --------------------------- */
28
29 #if defined USE_POSIX_MMAP
30 #define USE_MMAP 1
31
32 #define _zzip_mmap(user, fd, offs, len) \
33 mmap (0, len, PROT_READ, MAP_SHARED, fd, offs)
34 #define _zzip_munmap(user, ptr, len) \
35 munmap (ptr, len)
36 #define _zzip_getpagesize(user) getpagesize()
37
38 #ifndef MAP_FAILED /* hpux10.20 does not have it */
39 #define MAP_FAILED ((void*)(-1))
40 #endif
41
42 #elif defined USE_WIN32_MMAP
43 #define USE_MMAP 1
44 #ifndef MAP_FAILED
45 #define MAP_FAILED 0
46 #endif
47 /* we had used the plugin->sys variable for (user) but not anymore */
48
49 static size_t win32_getpagesize (void)
50 {
51 SYSTEM_INFO si; GetSystemInfo (&si);
52 return si.dwAllocationGranularity;
53 }
54 static void* win32_mmap (long* user, int fd, zzip_off_t offs, size_t len)
55 {
56 if (! user || *user != 1) /* || offs % getpagesize() */
57 return 0;
58 {
59 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
60 HANDLE fileMapping = NULL;
61 if (hFile)
62 fileMapping = CreateFileMapping (hFile, 0, PAGE_READONLY, 0, 0, NULL);
63 if (fileMapping != NULL)
64 {
65 char* p = MapViewOfFile(fileMapping, FILE_MAP_READ, 0, offs, len);
66 CloseHandle (fileMapping); *user = 1;
67 if (p) return p;
68 }
69 return MAP_FAILED;
70 }
71 }
72 static void win32_munmap (long* user, char* fd_map, size_t len)
73 {
74 UnmapViewOfFile (fd_map);
75 }
76
77 #define _zzip_mmap(user, fd, offs, len) \
78 win32_mmap ((long*) &(user), fd, offs, len)
79 #define _zzip_munmap(user, ptr, len) \
80 win32_munmap ((long*) &(user), ptr, len)
81 #define _zzip_getpagesize(user) win32_getpagesize()
82
83 #else /* disable */
84 #define USE_MMAP 0
85 /* USE_MAP is intentional: we expect the compiler to do some "code removal"
86 * on any source code enclosed in if (USE_MMAP) {...} i.e. the unreachable
87 * branch of an if (0) {....} is not emitted to the final object binary. */
88
89 #ifndef MAP_FAILED
90 #define MAP_FAILED 0
91 #endif
92
93 #define _zzip_mmap(user, fd, offs, len) (MAP_FAILED)
94 #define _zzip_munmap(user, ptr, len) {}
95 #define _zzip_getpagesize(user) 1
96
97 #endif /* USE_MMAP defines */
98
99
100 #endif