22 : key_{std::move(
key)}, size_{size} {
23 fd_ = ::shm_open(key_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
24 if (fd_ == -1) {
25 throw std::runtime_error("CreateSharedMemoryRegion: unable to get shared memory descriptor for shared-memory key '" + key_ +
26 "'");
27 }
28
29 if (::ftruncate(fd_, size_) == -1) {
30 closeDescriptor();
31 unlinkRegion();
32 throw std::runtime_error("CreateSharedMemoryRegion: unable to initialize shared-memory key '" + key_ +
33 "' to requested size: " + std::to_string(static_cast<std::size_t>(size_)) + " bytes");
34 }
35
36 address_ = ::mmap(nullptr, static_cast<std::size_t>(size_), PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
37 if (address_ == MAP_FAILED) {
38 address_ = nullptr;
39 const int failed_fd = fd_;
40 closeDescriptor();
41 unlinkRegion();
42 throw std::runtime_error("MapSharedMemory: unable to process address space or shared-memory descriptor: " +
43 std::to_string(failed_fd));
44 }
45
46 const int mapped_fd = fd_;
47 if (::close(fd_) == -1) {
48 fd_ = -1;
49 unmapRegion();
50 unlinkRegion();
51 throw std::runtime_error("CloseSharedMemory: unable to close shared-memory descriptor: " + std::to_string(mapped_fd));
52 }
53 fd_ = -1;
54 }
const std::string & key() const noexcept