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 /* and make sure (sizeof(*user) == sizeof(HANDLE)) */
55 static size_t win32_getpagesize (void)
57 SYSTEM_INFO si
; GetSystemInfo (&si
);
58 return si
.dwAllocationGranularity
;
61 static void* win32_mmap (__int64
* user
, int fd
, zzip_off_t offs
, size_t len
)
63 static void* win32_mmap (long* user
, int fd
, zzip_off_t offs
, size_t len
)
66 if (! user
|| *user
!= 1) /* || offs % getpagesize() */
69 HANDLE hFile
= (HANDLE
)_get_osfhandle(fd
);
72 *user
= (__int64
) CreateFileMapping (hFile
, 0, PAGE_READONLY
, 0, 0, NULL
);
74 *user
= (long) CreateFileMapping (hFile
, 0, PAGE_READONLY
, 0, 0, NULL
);
79 p
= MapViewOfFile(*(HANDLE
*)user
, FILE_MAP_READ
, 0, offs
, len
);
81 CloseHandle (*(HANDLE
*)user
); *user
= 1;
87 static void win32_munmap (__int64
* user
, char* fd_map
, size_t len
)
89 static void win32_munmap (long* user
, char* fd_map
, size_t len
)
92 UnmapViewOfFile (fd_map
);
93 CloseHandle (*(HANDLE
*)user
); *user
= 1;
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)
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)
106 #define _zzip_getpagesize(user) win32_getpagesize()
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. */
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 */