1 #ifndef __ZZIP_INTERNAL_MMAP_H
2 #define __ZZIP_INTERNAL_MMAP_H
3 #include <zzip/types.h>
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
21 #if defined ZZIP_HAVE_SYS_MMAN_H
23 #define USE_POSIX_MMAP 1
24 #elif defined ZZIP_HAVE_WINBASE_H || defined WIN32
26 #define USE_WIN32_MMAP 1
32 /* -------------- specify MMAP function imports --------------------------- */
34 #if defined USE_POSIX_MMAP
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) \
41 #define _zzip_getpagesize(user) getpagesize()
43 #ifndef MAP_FAILED /* hpux10.20 does not have it */
44 #define MAP_FAILED ((void*)(-1))
47 #elif defined USE_WIN32_MMAP
52 /* we (ab)use the "*user" variable to store the FileMapping handle */
53 /* which assumes (sizeof(long) == sizeof(HANDLE)) */
55 static size_t win32_getpagesize ()
57 SYSTEM_INFO si; GetSystemInfo (&si);
58 return si.dwAllocationGranularity;
60 static void* win32_mmap (long* user, int fd, zzip_off_t offs, size_t len)
62 if (! user || *user != 1) /* || offs % getpagesize() */
65 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
67 *user = (int) CreateFileMapping (hFile, 0, PAGE_READONLY, 0, 0, NULL);
71 p = MapViewOfFile(*(HANDLE*)user, FILE_MAP_READ, 0, offs, len);
72 if (p) return p + offs;
73 CloseHandle (*(HANDLE*)user); *user = 1;
78 static void win32_munmap (long* user, char* fd_map, size_t len)
80 UnmapViewOfFile (fd_map);
81 CloseHandle (*(HANDLE*)user); *user = 1;
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()
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. */
100 #define _zzip_mmap(user, fd, offs, len) (MAP_FAILED)
101 #define _zzip_munmap(user, ptr, len) {}
102 #define _zzip_getpagesize(user) 1
104 #endif /* USE_MMAP defines */