23 fd_ = ::shm_open(key_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
25 throw std::runtime_error(
"CreateSharedMemoryRegion: unable to get shared memory descriptor for shared-memory key '" + key_ +
29 if (::ftruncate(fd_, size_) == -1) {
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");
36 address_ = ::mmap(
nullptr,
static_cast<std::size_t
>(size_), PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
37 if (address_ == MAP_FAILED) {
39 const int failed_fd = fd_;
42 throw std::runtime_error(
"MapSharedMemory: unable to process address space or shared-memory descriptor: " +
43 std::to_string(failed_fd));
46 const int mapped_fd = fd_;
47 if (::close(fd_) == -1) {
51 throw std::runtime_error(
"CloseSharedMemory: unable to close shared-memory descriptor: " + std::to_string(mapped_fd));
60 : key_{std::move(other.key_)},
61 address_{std::exchange(other.address_,
nullptr)},
62 fd_{std::exchange(other.fd_, -1)},
64 owns_name_{std::exchange(other.owns_name_,
false)} {}
74 std::uint8_t*
address() const noexcept {
return static_cast<std::uint8_t*
>(address_); }
75 const std::string&
key() const noexcept {
return key_; }
78 void unmapRegion() noexcept {
79 if (address_ !=
nullptr) {
80 ::munmap(address_,
static_cast<std::size_t
>(size_));
85 void closeDescriptor() noexcept {
92 void unlinkRegion() noexcept {
94 ::shm_unlink(key_.c_str());
100 void* address_{
nullptr};
103 bool owns_name_{
true};
Own a POSIX shared-memory mapping used by the Triton client.
SharedMemoryRegion(const SharedMemoryRegion &)=delete
SharedMemoryRegion(SharedMemoryRegion &&) noexcept=default
SharedMemoryRegion & operator=(const SharedMemoryRegion &)=delete
uint8_t * getAddress() const
std::string getKey() const
SharedMemoryRegion(const std::string &key, std::int64_t size)
Create and map a system shared-memory region.
std::uint8_t * address() const noexcept
PosixSharedMemory(const PosixSharedMemory &)=delete
PosixSharedMemory & operator=(PosixSharedMemory &&)=delete
~PosixSharedMemory() noexcept
PosixSharedMemory(std::string key, std::int64_t size)
const std::string & key() const noexcept
PosixSharedMemory & operator=(const PosixSharedMemory &)=delete
PosixSharedMemory(PosixSharedMemory &&other) noexcept