mmf.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "include/mmf.hxx"
  2. #ifdef _WIN32
  3. #define OS_DEP(UNIX_EXPRESSION, WINDOWS_EXPRESSION) WINDOWS_EXPRESSION
  4. #include <windows.h>
  5. #include <io.h>
  6. #define F_OK 0
  7. #define access _access
  8. #else
  9. #define OS_DEP(UNIX_EXPRESSION, WINDOWS_EXPRESSION) UNIX_EXPRESSION
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <sys/mman.h>
  13. #include <sys/stat.h>
  14. #endif
  15. using namespace pmmf;
  16. inline int checkProtoFlag(ProtectionMode protection_mode, ProtectionMode flag) { return (int)protection_mode & (int)flag; }
  17. size_t MappedFile::getSystemPageSize() {
  18. #ifdef _WIN32
  19. SYSTEM_INFO sysinfo = {0};
  20. ::GetSystemInfo(&sysinfo);
  21. return sysinfo.dwAllocationGranularity;
  22. #else
  23. return sysconf(_SC_PAGESIZE);
  24. #endif
  25. }
  26. FileDescriptor MappedFile::openFile(std::string file_path, ProtectionMode protection_mode) {
  27. if(access(file_path.c_str(), F_OK) == 0) {
  28. #ifdef _WIN32
  29. return ::CreateFile(
  30. file_path.c_str(),
  31. ((DWORD)0x00)
  32. | ((checkProtoFlag(protection_mode, ProtectionMode::read) || checkProtoFlag(protection_mode, ProtectionMode::exec)) ? GENERIC_READ : ((DWORD)0x00))
  33. | (checkProtoFlag(protection_mode, ProtectionMode::write) ? GENERIC_WRITE : ((DWORD)0x00))
  34. | (checkProtoFlag(protection_mode, ProtectionMode::exec) ? GENERIC_EXECUTE : ((DWORD)0x00)),
  35. checkProtoFlag(protection_mode, ProtectionMode::write) ? 0x00 : FILE_SHARE_READ,
  36. 0,
  37. checkProtoFlag(protection_mode, ProtectionMode::trunc) ? TRUNCATE_EXISTING : OPEN_EXISTING,
  38. FILE_ATTRIBUTE_NORMAL,
  39. 0
  40. );
  41. #else
  42. return ::open(
  43. file_path.c_str(),
  44. ((int)0x00
  45. | (checkProtoFlag(protection_mode, ProtectionMode::trunc) ? O_TRUNC : 0x00)
  46. | (
  47. (checkProtoFlag(protection_mode, ProtectionMode::read) && checkProtoFlag(protection_mode, ProtectionMode::write))
  48. ? O_RDWR
  49. : checkProtoFlag(protection_mode, ProtectionMode::read)
  50. ? O_RDONLY
  51. : checkProtoFlag(protection_mode, ProtectionMode::write)
  52. ? O_WRONLY
  53. : 0x00
  54. )
  55. ),
  56. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
  57. );
  58. #endif
  59. } else {
  60. #ifdef _WIN32
  61. return ::CreateFile(
  62. file_path.c_str(),
  63. ((DWORD)0x00)
  64. | ((checkProtoFlag(protection_mode, ProtectionMode::read) || checkProtoFlag(protection_mode, ProtectionMode::exec)) ? GENERIC_READ : 0x00)
  65. | (checkProtoFlag(protection_mode, ProtectionMode::write) ? GENERIC_WRITE : 0x00),
  66. (checkProtoFlag(protection_mode, ProtectionMode::write) ? 0x00 : FILE_SHARE_READ),
  67. 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
  68. #else
  69. return ::open(
  70. file_path.c_str(),
  71. ((int)O_CREAT
  72. | (checkProtoFlag(protection_mode, ProtectionMode::trunc) ? O_TRUNC : 0x00)
  73. | (
  74. (checkProtoFlag(protection_mode, ProtectionMode::read) && checkProtoFlag(protection_mode, ProtectionMode::write))
  75. ? O_RDWR
  76. : checkProtoFlag(protection_mode, ProtectionMode::read)
  77. ? O_RDONLY
  78. : checkProtoFlag(protection_mode, ProtectionMode::write)
  79. ? O_WRONLY
  80. : 0x00
  81. )
  82. ),
  83. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
  84. );
  85. #endif
  86. }
  87. }
  88. MappedFile::MappedFile(std::string file_path, ProtectionMode protection_mode, MapFlag map_flag)
  89. : file_descriptor(openFile(std::move(file_path), protection_mode)), proterction_mode(protection_mode), map_flag(map_flag) {
  90. if(!isFileOpen()) {
  91. protection_mode = ProtectionMode(0x00);
  92. map_flag = MapFlag(0x00);
  93. }
  94. }
  95. MappedFile::~MappedFile() {
  96. if(isFileOpen())
  97. #ifdef _WIN32
  98. ::CloseHandle(file_descriptor);
  99. #else
  100. ::close(file_descriptor);
  101. #endif
  102. }
  103. size_t MappedFile::getFileSize() const {
  104. #ifdef _WIN32
  105. DWORD high_size;
  106. DWORD low_size = ::GetFileSize(file_descriptor, &high_size);
  107. return (static_cast<std::size_t>(high_size) << 32) | low_size;
  108. #else
  109. struct stat f_stat_buf;
  110. if(::fstat(file_descriptor, &f_stat_buf)) return 0;
  111. return f_stat_buf.st_size;
  112. #endif
  113. }