/src/zziplib/zzip/__mmap.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 107 lines · 72 code · 15 blank · 20 comment · 7 complexity · 58172afad5f2e53c9535fb2037e0b7b6 MD5 · raw file

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