@@ -264,66 +264,44 @@ class SlickQueue {
264264#else
265265 void allocate_shm_data (const char * const shm_name, bool open_only) {
266266 size_t BF_SZ = 64 + sizeof (slot) * size_ + sizeof (T) * size_;
267- shm_name_ = shm_name;
268- if (open_only) {
269- shm_fd_ = shm_open (shm_name, O_RDWR, 0666 );
270- if (shm_fd_ == -1 ) {
271- throw std::runtime_error (" Failed to open shm. err=" + std::to_string (errno));
272- }
273-
274- void * tmp = mmap (nullptr , 64 , PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_, 0 );
275- if (tmp == MAP_FAILED) {
276- throw std::runtime_error (" Failed to map shm header. err=" + std::to_string (errno));
277- }
278- mask_ = *reinterpret_cast <uint32_t *>(reinterpret_cast <uint8_t *>(tmp) + sizeof (std::atomic_uint_fast64_t )) - 1 ;
279- size_ = mask_ + 1025 ;
280- munmap (tmp, 64 );
281-
282- lpvMem_ = mmap (nullptr , BF_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_, 0 );
283- if (lpvMem_ == MAP_FAILED) {
284- throw std::runtime_error (" Failed to map shm. err=" + std::to_string (errno));
285- }
286-
287- reserved_ = reinterpret_cast <std::atomic_uint_fast64_t *>(lpvMem_);
288- control_ = reinterpret_cast <slot*>((uint8_t *)lpvMem_ + 64 );
289- data_ = reinterpret_cast <T*>((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_);
290- own_ = false ;
291- } else {
292- shm_fd_ = shm_open (shm_name, O_RDWR | O_CREAT | O_EXCL, 0666 );
293- if (shm_fd_ == -1 ) {
294- if (errno != EEXIST) {
295- throw std::runtime_error (" Failed to create shm. err=" + std::to_string (errno));
296- }
267+ shm_name_ = shm_name;
268+ int flags = open_only ? O_RDWR : (O_RDWR | O_CREAT | O_EXCL);
269+ shm_fd_ = shm_open (shm_name, flags, 0666 );
270+ if (shm_fd_ == -1 ) {
271+ if (!open_only && errno == EEXIST) {
272+ // Try opening existing
297273 shm_fd_ = shm_open (shm_name, O_RDWR, 0666 );
298274 if (shm_fd_ == -1 ) {
299275 throw std::runtime_error (" Failed to open existing shm. err=" + std::to_string (errno));
300276 }
301277 own_ = false ;
302278 } else {
303- own_ = true ;
279+ throw std::runtime_error ( " Failed to open/create shm. err= " + std::to_string (errno)) ;
304280 }
281+ } else {
282+ own_ = !open_only;
283+ }
305284
306- if (own_) {
307- if (ftruncate (shm_fd_, BF_SZ) == -1 ) {
308- throw std::runtime_error (" Failed to size shm. err=" + std::to_string (errno));
309- }
285+ if (own_) {
286+ if (ftruncate (shm_fd_, BF_SZ) == -1 ) {
287+ throw std::runtime_error (" Failed to size shm. err=" + std::to_string (errno));
310288 }
289+ }
311290
312- lpvMem_ = mmap (nullptr , BF_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_, 0 );
313- if (lpvMem_ == MAP_FAILED) {
314- throw std::runtime_error (" Failed to map shm. err=" + std::to_string (errno));
315- }
291+ lpvMem_ = mmap (nullptr , BF_SZ, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd_, 0 );
292+ if (lpvMem_ == MAP_FAILED) {
293+ throw std::runtime_error (" Failed to map shm. err=" + std::to_string (errno));
294+ }
316295
317- if (own_) {
318- reserved_ = new (lpvMem_) std::atomic_uint_fast64_t { 0 };
319- *reinterpret_cast <uint32_t *>(reinterpret_cast <uint8_t *>(lpvMem_) + sizeof (std::atomic_uint_fast64_t )) = mask_ + 1 ;
320- control_ = new ((uint8_t *)lpvMem_ + 64 ) slot[size_];
321- data_ = new ((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_) T[size_];
322- } else {
323- reserved_ = reinterpret_cast <std::atomic_uint_fast64_t *>(lpvMem_);
324- control_ = reinterpret_cast <slot*>((uint8_t *)lpvMem_ + 64 );
325- data_ = reinterpret_cast <T*>((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_);
326- }
296+ if (own_) {
297+ reserved_ = new (lpvMem_) std::atomic_uint_fast64_t { 0 };
298+ *reinterpret_cast <uint32_t *>(reinterpret_cast <uint8_t *>(lpvMem_) + sizeof (std::atomic_uint_fast64_t )) = mask_ + 1 ;
299+ control_ = new ((uint8_t *)lpvMem_ + 64 ) slot[size_];
300+ data_ = new ((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_) T[size_];
301+ } else {
302+ reserved_ = reinterpret_cast <std::atomic_uint_fast64_t *>(lpvMem_);
303+ control_ = reinterpret_cast <slot*>((uint8_t *)lpvMem_ + 64 );
304+ data_ = reinterpret_cast <T*>((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_);
327305 }
328306 }
329307#endif
0 commit comments