mmap_wrapper.hxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef WINMMAP_H
  2. #define WINMMAP_H
  3. #include <cstddef>
  4. #include <inttypes.h>
  5. //! Portable Memory-Mapped File
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. namespace pmmf {
  9. /**
  10. * @brief FileDescriptor - alias for native handle of file
  11. */
  12. typedef HANDLE FileDescriptor;
  13. const FileDescriptor INVALID_FILE_DESCRIPTOR = INVALID_HANDLE_VALUE;
  14. #ifdef _WIN64
  15. typedef int64_t off_t;
  16. #else
  17. typedef uint32_t off_t;
  18. #endif
  19. #else
  20. #include <aio.h>
  21. namespace pmmf {
  22. /**
  23. * @brief FileDescriptor - alias for native handle of file
  24. */
  25. typedef int FileDescriptor;
  26. constexpr FileDescriptor INVALID_FILE_DESCRIPTOR = -1;
  27. #endif
  28. /**
  29. * @brief The ProtectionMode enum - describes the desired memory protection mode (it must not conflict with the file open mode)
  30. */
  31. enum class ProtectionMode : int {
  32. read = 0x01, //< data can be read
  33. write = 0x02, //< data can be write
  34. //#ifdef FILE_MAP_EXECUTE
  35. exec = 0x04, //< data can be executed
  36. rx = 0x01 | 0x04,
  37. wx = 0x02 | 0x04,
  38. rwx = 0x01 | 0x02 | 0x04,
  39. rxt = 0x01 | 0x04 | 0x08,
  40. wxt = 0x02 | 0x04 | 0x08,
  41. rwxt = 0x01 | 0x02 | 0x04 | 0x08,
  42. //#else
  43. // exec = 0x00, //< data can be executed (FLAG IS DISABLED)
  44. // rx = 0x01 | 0x00,
  45. // wx = 0x02 | 0x00,
  46. // rwx = 0x01 | 0x02 | 0x00,
  47. // rxt = 0x01 | 0x00 | 0x08,
  48. // wxt = 0x02 | 0x00 | 0x08,
  49. // rwxt = 0x01 | 0x02 | 0x00 | 0x08,
  50. //#endif
  51. trunc = 0x08, //< clear file on open
  52. rw = 0x01 | 0x02,
  53. rt = 0x01 | 0x08,
  54. wt = 0x02 | 0x08,
  55. rwt = 0x01 | 0x02 | 0x08
  56. };
  57. /**
  58. * @brief The MapFlag enum - The flags argument provides other information about the handling of the mapped data. The value of flags is the bitwise inclusive OR of these options
  59. */
  60. enum class MapFlag : int {
  61. shared = 0x0001,
  62. priv = 0x0002,
  63. anonymous = 0x0004,
  64. anon = anonymous,
  65. fixed = 0x0008,
  66. #ifndef _WIN32 // Not implemented in windows
  67. denywrite = 0x0010,
  68. executable = 0x0020,
  69. noreserve = 0x0040,
  70. locked = 0x0080,
  71. growsdown = 0x0100,
  72. file = 0x0200
  73. #endif
  74. };
  75. #ifndef MAP_FAILED
  76. #define MAP_FAILED reinterpret_cast<void*>(0xFFFFFFFFFFFFFFFFull)
  77. #endif
  78. /**
  79. * @brief mmap - portable analogue of Unix mmap, see: https://www.opennet.ru/man.shtml?topic=mmap&category=&submit=%F0%CF%CB%C1%DA%C1%D4%D8+man
  80. */
  81. extern void* mmap(void* start, size_t length, ProtectionMode prot, MapFlag flags, FileDescriptor fd, off_t offset);
  82. /**
  83. * @brief munmap - portable analogue of Unix munmap, see: https://www.opennet.ru/man.shtml?topic=mmap&category=&submit=%F0%CF%CB%C1%DA%C1%D4%D8+man
  84. */
  85. extern void munmap(void *addr, std::size_t length);
  86. /**
  87. * @brief msync - synchronize a file with a memory map: https://www.opennet.ru/man.shtml?topic=msync&russian=2&category=&submit=%F0%CF%CB%C1%DA%C1%D4%D8+man
  88. */
  89. extern int msync(FileDescriptor file_descriptor, void *start, size_t length, int flags);
  90. }
  91. #endif // WINMMAP_H