122 : name(name), total_size(size + sizeof(header))
124 bool created =
false;
128 fd = shm_open(name.c_str(), O_RDWR, 0666);
131 throw std::runtime_error(
"Failed to open existing shared memory: " + std::string(strerror(errno)));
135 if (fstat(fd, &sb) == -1) {
137 throw std::runtime_error(
"Failed to get shared memory size: " + std::string(strerror(errno)));
139 total_size = sb.st_size;
143 fd = shm_open(name.c_str(), O_CREAT | O_EXCL | O_RDWR, 0666);
148 fd = shm_open(name.c_str(), O_RDWR, 0666);
151 throw std::runtime_error(
"Failed to open existing shared memory: " + std::string(strerror(errno)));
156 throw std::runtime_error(
"Failed to create shared memory: " + std::string(strerror(errno)));
165 if (created && ftruncate(fd, total_size) == -1)
168 shm_unlink(name.c_str());
169 throw std::runtime_error(
"Failed to set size of shared memory: " + std::string(strerror(errno)));
172 base_addr = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
173 if (base_addr == MAP_FAILED)
177 shm_unlink(name.c_str());
178 throw std::runtime_error(
"Failed to map shared memory: " + std::string(strerror(errno)));
181 hdr =
static_cast<header *
>(base_addr);
185 new (hdr) header{{1}, TableType{}};