7 #include <winapi/handle.hpp>
8 #include <winapi/shmem.hpp>
9 #include <winapi/utf8.hpp>
10 #include <winapi/utils.hpp>
22 void* do_map(
const Handle& mapping, std::size_t nb = 0) {
23 const auto addr = ::MapViewOfFile(
static_cast<HANDLE
>(mapping), FILE_MAP_ALL_ACCESS, 0, 0, nb);
26 throw error::windows(GetLastError(),
"MapViewOfFile");
34 void SharedMemory::Unmap::operator()(
void* ptr)
const {
35 const auto ret = ::UnmapViewOfFile(ptr);
37 WINAPI_UNUSED_PARAMETER(ret);
41 const auto nb64 =
static_cast<std::uint64_t
>(nb);
42 static_assert(
sizeof(nb64) == 2 *
sizeof(DWORD),
"sizeof(DWORD) != 32");
44 const auto nb_low =
static_cast<DWORD
>(nb64);
45 const auto nb_high =
static_cast<DWORD
>(nb64 >> 32);
47 const auto mapping_impl = ::CreateFileMappingW(
48 INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, nb_high, nb_low, widen(name).c_str());
50 if (mapping_impl == NULL) {
51 throw error::windows(GetLastError(),
"CreateFileMappingW");
54 Handle mapping{mapping_impl};
55 const auto addr = do_map(mapping);
56 return {std::move(mapping), addr};
60 const auto mapping_impl = ::OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, widen(name).c_str());
62 if (mapping_impl == NULL) {
63 throw error::windows(GetLastError(),
"OpenFileMappingW");
66 Handle mapping{mapping_impl};
67 const auto addr = do_map(mapping);
68 return {std::move(mapping), addr};
Named shared memory region.
static SharedMemory create(const std::string &name, std::size_t nb)
static SharedMemory open(const std::string &name)
Make std::system_error work with GetLastError().