@@ -38,6 +38,7 @@ class SlickQueue {
3838 };
3939
4040 uint32_t size_;
41+ uint32_t buffered_size_;
4142 uint32_t mask_;
4243 T* data_ = nullptr ;
4344 slot* control_ = nullptr ;
@@ -56,10 +57,11 @@ class SlickQueue {
5657
5758public:
5859 SlickQueue (uint32_t size, const char * const shm_name = nullptr )
59- : size_(size + 1024 ) // add some buffer at the end
60+ : size_(size)
61+ , buffered_size_(size + 1024 ) // add some buffer at the end
6062 , mask_(size - 1 )
61- , data_(shm_name ? nullptr : new T[size_ ])
62- , control_(shm_name ? nullptr : new slot[size_ ])
63+ , data_(shm_name ? nullptr : new T[buffered_size_ ])
64+ , control_(shm_name ? nullptr : new slot[buffered_size_ ])
6365 , reserved_(shm_name ? nullptr : &reserved_local_)
6466 , own_(shm_name == nullptr )
6567 , use_shm_(shm_name != nullptr )
@@ -95,7 +97,7 @@ class SlickQueue {
9597 }
9698#else
9799 if (lpvMem_) {
98- auto BF_SZ = static_cast <size_t >(64 + sizeof (slot) * size_ + sizeof (T) * size_ );
100+ auto BF_SZ = static_cast <size_t >(64 + sizeof (slot) * buffered_size_ + sizeof (T) * buffered_size_ );
99101 munmap (lpvMem_, BF_SZ);
100102 lpvMem_ = nullptr ;
101103 }
@@ -119,6 +121,7 @@ class SlickQueue {
119121
120122 bool own_buffer () const noexcept { return own_; }
121123 bool use_shm () const noexcept { return use_shm_; }
124+ constexpr uint32_t size () const noexcept { return mask_ + 1 ; }
122125
123126 uint64_t initial_reading_index () const noexcept {
124127 return reserved_->load (std::memory_order_relaxed);
@@ -194,7 +197,7 @@ class SlickQueue {
194197
195198#if defined(_MSC_VER)
196199 void allocate_shm_data (const char * const shm_name, bool open_only) {
197- DWORD BF_SZ = 64 + sizeof (slot) * size_ + sizeof (T) * size_ ;
200+ DWORD BF_SZ = 64 + sizeof (slot) * buffered_size_ + sizeof (T) * buffered_size_ ;
198201 hMapFile_ = NULL ;
199202 if (open_only) {
200203#ifndef UNICODE
@@ -213,8 +216,9 @@ class SlickQueue {
213216 auto err = GetLastError ();
214217 throw std::runtime_error (" Failed to map shm. err=" + std::to_string (err));
215218 }
216- mask_ = *reinterpret_cast <uint32_t *>(reinterpret_cast <uint8_t *>(lpvMem_) + sizeof (std::atomic_uint_fast64_t )) - 1 ;
217- size_ = mask_ + 1025 ;
219+ size_ = *reinterpret_cast <uint32_t *>(reinterpret_cast <uint8_t *>(lpvMem_) + sizeof (std::atomic_uint_fast64_t ));
220+ mask_ = size_ - 1 ;
221+ buffered_size_ = size_ + 1024 ;
218222 UnmapViewOfFile (lpvMem_);
219223 lpvMem_ = nullptr ;
220224 }
@@ -252,18 +256,18 @@ class SlickQueue {
252256 if (own_) {
253257 reserved_ = new (lpvMem_) std::atomic_uint_fast64_t { 0 };
254258 *reinterpret_cast <uint32_t *>(reinterpret_cast <uint8_t *>(lpvMem_) + sizeof (std::atomic_uint_fast64_t )) = mask_ + 1 ;
255- control_ = new ((uint8_t *)lpvMem_ + 64 ) slot[size_ ];
256- data_ = new ((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_ ) T[size_ ];
259+ control_ = new ((uint8_t *)lpvMem_ + 64 ) slot[buffered_size_ ];
260+ data_ = new ((uint8_t *)lpvMem_ + 64 + sizeof (slot) * buffered_size_ ) T[buffered_size_ ];
257261 }
258262 else {
259263 reserved_ = reinterpret_cast <std::atomic_uint_fast64_t *>(lpvMem_);
260264 control_ = reinterpret_cast <slot*>((uint8_t *)lpvMem_ + 64 );
261- data_ = reinterpret_cast <T*>((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_ );
265+ data_ = reinterpret_cast <T*>((uint8_t *)lpvMem_ + 64 + sizeof (slot) * buffered_size_ );
262266 }
263267 }
264268#else
265269 void allocate_shm_data (const char * const shm_name, bool open_only) {
266- size_t BF_SZ = 64 + sizeof (slot) * size_ + sizeof (T) * size_ ;
270+ size_t BF_SZ = 64 + sizeof (slot) * buffered_size_ + sizeof (T) * buffered_size_ ;
267271 shm_name_ = shm_name;
268272 int flags = open_only ? O_RDWR : (O_RDWR | O_CREAT | O_EXCL);
269273 shm_fd_ = shm_open (shm_name, flags, 0666 );
@@ -296,12 +300,12 @@ class SlickQueue {
296300 if (own_) {
297301 reserved_ = new (lpvMem_) std::atomic_uint_fast64_t { 0 };
298302 *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_ ];
303+ control_ = new ((uint8_t *)lpvMem_ + 64 ) slot[buffered_size_ ];
304+ data_ = new ((uint8_t *)lpvMem_ + 64 + sizeof (slot) * buffered_size_ ) T[buffered_size_ ];
301305 } else {
302306 reserved_ = reinterpret_cast <std::atomic_uint_fast64_t *>(lpvMem_);
303307 control_ = reinterpret_cast <slot*>((uint8_t *)lpvMem_ + 64 );
304- data_ = reinterpret_cast <T*>((uint8_t *)lpvMem_ + 64 + sizeof (slot) * size_ );
308+ data_ = reinterpret_cast <T*>((uint8_t *)lpvMem_ + 64 + sizeof (slot) * buffered_size_ );
305309 }
306310 }
307311#endif
0 commit comments