file_mapping.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // file_mapping.hpp --------------------------------------------------------------//
  2. // Copyright 2010 Vicente J. Botet Escriba
  3. // Copyright 2015 Andrey Semashev
  4. // Copyright 2016 Jorge Lodos
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. #ifndef BOOST_DETAIL_WINAPI_FILE_MAPPING_HPP
  8. #define BOOST_DETAIL_WINAPI_FILE_MAPPING_HPP
  9. #include <boost/detail/winapi/basic_types.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. #pragma once
  12. #endif
  13. #if !defined( BOOST_USE_WINDOWS_H )
  14. extern "C" {
  15. #if !defined( BOOST_NO_ANSI_APIS )
  16. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  17. CreateFileMappingA(
  18. boost::detail::winapi::HANDLE_ hFile,
  19. ::_SECURITY_ATTRIBUTES* lpFileMappingAttributes,
  20. boost::detail::winapi::DWORD_ flProtect,
  21. boost::detail::winapi::DWORD_ dwMaximumSizeHigh,
  22. boost::detail::winapi::DWORD_ dwMaximumSizeLow,
  23. boost::detail::winapi::LPCSTR_ lpName);
  24. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  25. OpenFileMappingA(
  26. boost::detail::winapi::DWORD_ dwDesiredAccess,
  27. boost::detail::winapi::BOOL_ bInheritHandle,
  28. boost::detail::winapi::LPCSTR_ lpName);
  29. #endif
  30. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  31. CreateFileMappingW(
  32. boost::detail::winapi::HANDLE_ hFile,
  33. ::_SECURITY_ATTRIBUTES* lpFileMappingAttributes,
  34. boost::detail::winapi::DWORD_ flProtect,
  35. boost::detail::winapi::DWORD_ dwMaximumSizeHigh,
  36. boost::detail::winapi::DWORD_ dwMaximumSizeLow,
  37. boost::detail::winapi::LPCWSTR_ lpName);
  38. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  39. OpenFileMappingW(
  40. boost::detail::winapi::DWORD_ dwDesiredAccess,
  41. boost::detail::winapi::BOOL_ bInheritHandle,
  42. boost::detail::winapi::LPCWSTR_ lpName);
  43. BOOST_SYMBOL_IMPORT boost::detail::winapi::LPVOID_ WINAPI
  44. MapViewOfFile(
  45. boost::detail::winapi::HANDLE_ hFileMappingObject,
  46. boost::detail::winapi::DWORD_ dwDesiredAccess,
  47. boost::detail::winapi::DWORD_ dwFileOffsetHigh,
  48. boost::detail::winapi::DWORD_ dwFileOffsetLow,
  49. boost::detail::winapi::SIZE_T_ dwNumberOfBytesToMap);
  50. BOOST_SYMBOL_IMPORT boost::detail::winapi::LPVOID_ WINAPI
  51. MapViewOfFileEx(
  52. boost::detail::winapi::HANDLE_ hFileMappingObject,
  53. boost::detail::winapi::DWORD_ dwDesiredAccess,
  54. boost::detail::winapi::DWORD_ dwFileOffsetHigh,
  55. boost::detail::winapi::DWORD_ dwFileOffsetLow,
  56. boost::detail::winapi::SIZE_T_ dwNumberOfBytesToMap,
  57. boost::detail::winapi::LPVOID_ lpBaseAddress);
  58. BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI
  59. FlushViewOfFile(
  60. boost::detail::winapi::LPCVOID_ lpBaseAddress,
  61. boost::detail::winapi::SIZE_T_ dwNumberOfBytesToFlush);
  62. BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI
  63. UnmapViewOfFile(boost::detail::winapi::LPCVOID_ lpBaseAddress);
  64. }
  65. #endif
  66. namespace boost {
  67. namespace detail {
  68. namespace winapi {
  69. #if defined( BOOST_USE_WINDOWS_H )
  70. const DWORD_ SEC_FILE_ = SEC_FILE;
  71. const DWORD_ SEC_IMAGE_ = SEC_IMAGE;
  72. const DWORD_ SEC_RESERVE_ = SEC_RESERVE;
  73. const DWORD_ SEC_COMMIT_ = SEC_COMMIT;
  74. const DWORD_ SEC_NOCACHE_ = SEC_NOCACHE;
  75. // These permission flags are undocumented and some of them are equivalent to the FILE_MAP_* flags.
  76. // SECTION_QUERY enables NtQuerySection.
  77. // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FSection%2FNtQuerySection.html
  78. const DWORD_ SECTION_QUERY_ = SECTION_QUERY;
  79. const DWORD_ SECTION_MAP_WRITE_ = SECTION_MAP_WRITE;
  80. const DWORD_ SECTION_MAP_READ_ = SECTION_MAP_READ;
  81. const DWORD_ SECTION_MAP_EXECUTE_ = SECTION_MAP_EXECUTE;
  82. const DWORD_ SECTION_EXTEND_SIZE_ = SECTION_EXTEND_SIZE;
  83. const DWORD_ SECTION_ALL_ACCESS_ = SECTION_ALL_ACCESS;
  84. const DWORD_ FILE_MAP_COPY_ = FILE_MAP_COPY;
  85. const DWORD_ FILE_MAP_WRITE_ = FILE_MAP_WRITE;
  86. const DWORD_ FILE_MAP_READ_ = FILE_MAP_READ;
  87. const DWORD_ FILE_MAP_ALL_ACCESS_ = FILE_MAP_ALL_ACCESS;
  88. #else // defined( BOOST_USE_WINDOWS_H )
  89. const DWORD_ SEC_FILE_ = 0x800000;
  90. const DWORD_ SEC_IMAGE_ = 0x1000000;
  91. const DWORD_ SEC_RESERVE_ = 0x4000000;
  92. const DWORD_ SEC_COMMIT_ = 0x8000000;
  93. const DWORD_ SEC_NOCACHE_ = 0x10000000;
  94. // These permission flags are undocumented and some of them are equivalent to the FILE_MAP_* flags.
  95. // SECTION_QUERY enables NtQuerySection.
  96. // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FSection%2FNtQuerySection.html
  97. const DWORD_ SECTION_QUERY_ = 0x00000001;
  98. const DWORD_ SECTION_MAP_WRITE_ = 0x00000002;
  99. const DWORD_ SECTION_MAP_READ_ = 0x00000004;
  100. const DWORD_ SECTION_MAP_EXECUTE_ = 0x00000008;
  101. const DWORD_ SECTION_EXTEND_SIZE_ = 0x00000010;
  102. const DWORD_ SECTION_ALL_ACCESS_ = 0x000F001F; // STANDARD_RIGHTS_REQUIRED | SECTION_*
  103. const DWORD_ FILE_MAP_COPY_ = SECTION_QUERY_;
  104. const DWORD_ FILE_MAP_WRITE_ = SECTION_MAP_WRITE_;
  105. const DWORD_ FILE_MAP_READ_ = SECTION_MAP_READ_;
  106. const DWORD_ FILE_MAP_ALL_ACCESS_ = SECTION_ALL_ACCESS_;
  107. #endif // defined( BOOST_USE_WINDOWS_H )
  108. // These constants are not defined in Windows SDK up until the one shipped with MSVC 8 and MinGW (as of 2016-02-14)
  109. const DWORD_ SECTION_MAP_EXECUTE_EXPLICIT_ = 0x00000020; // not included in SECTION_ALL_ACCESS
  110. const DWORD_ FILE_MAP_EXECUTE_ = SECTION_MAP_EXECUTE_EXPLICIT_; // not included in FILE_MAP_ALL_ACCESS
  111. // These constants are not defined in Windows SDK up until 6.0A and MinGW (as of 2016-02-14)
  112. const DWORD_ SEC_PROTECTED_IMAGE_ = 0x2000000;
  113. const DWORD_ SEC_WRITECOMBINE_ = 0x40000000;
  114. const DWORD_ SEC_LARGE_PAGES_ = 0x80000000;
  115. const DWORD_ SEC_IMAGE_NO_EXECUTE_ = (SEC_IMAGE_ | SEC_NOCACHE_);
  116. #if !defined( BOOST_NO_ANSI_APIS )
  117. using ::OpenFileMappingA;
  118. #endif
  119. using ::OpenFileMappingW;
  120. using ::MapViewOfFile;
  121. using ::MapViewOfFileEx;
  122. using ::FlushViewOfFile;
  123. using ::UnmapViewOfFile;
  124. #if !defined( BOOST_NO_ANSI_APIS )
  125. BOOST_FORCEINLINE HANDLE_ CreateFileMappingA(
  126. HANDLE_ hFile,
  127. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  128. DWORD_ flProtect,
  129. DWORD_ dwMaximumSizeHigh,
  130. DWORD_ dwMaximumSizeLow,
  131. LPCSTR_ lpName)
  132. {
  133. return ::CreateFileMappingA(
  134. hFile,
  135. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  136. flProtect,
  137. dwMaximumSizeHigh,
  138. dwMaximumSizeLow,
  139. lpName);
  140. }
  141. #endif
  142. BOOST_FORCEINLINE HANDLE_ CreateFileMappingW(
  143. HANDLE_ hFile,
  144. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  145. DWORD_ flProtect,
  146. DWORD_ dwMaximumSizeHigh,
  147. DWORD_ dwMaximumSizeLow,
  148. LPCWSTR_ lpName)
  149. {
  150. return ::CreateFileMappingW(
  151. hFile,
  152. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  153. flProtect,
  154. dwMaximumSizeHigh,
  155. dwMaximumSizeLow,
  156. lpName);
  157. }
  158. #if !defined( BOOST_NO_ANSI_APIS )
  159. BOOST_FORCEINLINE HANDLE_ create_file_mapping(
  160. HANDLE_ hFile,
  161. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  162. DWORD_ flProtect,
  163. DWORD_ dwMaximumSizeHigh,
  164. DWORD_ dwMaximumSizeLow,
  165. LPCSTR_ lpName)
  166. {
  167. return ::CreateFileMappingA(
  168. hFile,
  169. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  170. flProtect,
  171. dwMaximumSizeHigh,
  172. dwMaximumSizeLow,
  173. lpName);
  174. }
  175. BOOST_FORCEINLINE HANDLE_ open_file_mapping(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCSTR_ lpName)
  176. {
  177. return ::OpenFileMappingA(dwDesiredAccess, bInheritHandle, lpName);
  178. }
  179. #endif
  180. BOOST_FORCEINLINE HANDLE_ create_file_mapping(
  181. HANDLE_ hFile,
  182. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  183. DWORD_ flProtect,
  184. DWORD_ dwMaximumSizeHigh,
  185. DWORD_ dwMaximumSizeLow,
  186. LPCWSTR_ lpName)
  187. {
  188. return ::CreateFileMappingW(
  189. hFile,
  190. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  191. flProtect,
  192. dwMaximumSizeHigh,
  193. dwMaximumSizeLow,
  194. lpName);
  195. }
  196. BOOST_FORCEINLINE HANDLE_ open_file_mapping(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCWSTR_ lpName)
  197. {
  198. return ::OpenFileMappingW(dwDesiredAccess, bInheritHandle, lpName);
  199. }
  200. }
  201. }
  202. }
  203. #endif // BOOST_DETAIL_WINAPI_FILE_MAPPING_HPP