25 : head(0), tail(0), fifoSize(s)
27 data =
new char[fifoSize];
32 if(data)
delete[] data;
39 const auto current_tail = tail.load(std::memory_order_seq_cst);
41 if( itemSize >= fifoSize - ((fifoSize + current_tail - head.load(std::memory_order_seq_cst)) % fifoSize) )
43 ERROR_MSG(
"AtomicFifo::write : fifo full, data lost\n");
47 if(itemSize > fifoSize - current_tail)
49 size_t m = fifoSize - current_tail;
50 memcpy(data + current_tail, buffer, m);
51 memcpy(data,
reinterpret_cast<const char *
>(buffer) + m, itemSize - m);
54 memcpy(data + current_tail, buffer, itemSize);
56 tail.store(increment(current_tail, itemSize));
65 const auto current_head = head.load(std::memory_order_seq_cst);
69 if( ((fifoSize + tail.load(std::memory_order_seq_cst) - current_head) % fifoSize) < itemSize )
75 if(fifoSize - current_head < itemSize)
77 size_t m = fifoSize - current_head;
78 memcpy(buffer, data + current_head, m);
79 memcpy(
reinterpret_cast<char *
>(buffer) + m, data, itemSize - m );
82 memcpy(buffer, data + current_head, itemSize);
84 head.store(increment(current_head, itemSize));
89 size_t AtomicFifo::increment(
size_t current_ptr,
size_t itemSize)
const 91 return (current_ptr + itemSize) % fifoSize;
96 return (tail.is_lock_free() && head.is_lock_free());