41 for (power_of_two = 1; 1U<<power_of_two < sz; power_of_two++) {}
42 size = 1<<power_of_two;
59 void set (
size_t r,
size_t w) {
65 size_t read (T *dest,
size_t cnt);
66 size_t write (T
const * src,
size_t cnt);
132 template<
class T>
size_t
139 size_t priv_read_idx;
141 priv_read_idx = read_idx.load ();
143 if ((free_cnt = read_space ()) == 0) {
147 to_read = cnt > free_cnt ? free_cnt : cnt;
149 cnt2 = priv_read_idx + to_read;
152 n1 = size - priv_read_idx;
153 n2 = cnt2 & size_mask;
159 memcpy (dest, &buf[priv_read_idx], n1 *
sizeof (T));
160 priv_read_idx = (priv_read_idx + n1) & size_mask;
163 memcpy (dest+n1, buf, n2 *
sizeof (T));
167 read_idx.store (priv_read_idx);
171 template<
class T>
size_t
179 size_t priv_write_idx;
181 priv_write_idx = write_idx.load ();
183 if ((free_cnt = write_space ()) == 0) {
187 to_write = cnt > free_cnt ? free_cnt : cnt;
189 cnt2 = priv_write_idx + to_write;
192 n1 = size - priv_write_idx;
193 n2 = cnt2 & size_mask;
199 memcpy (&buf[priv_write_idx], src, n1 *
sizeof (T));
200 priv_write_idx = (priv_write_idx + n1) & size_mask;
203 memcpy (buf, src+n1, n2 *
sizeof (T));
207 write_idx.store (priv_write_idx);
211 template<
class T>
void
219 w = write_idx.load ();
220 r = read_idx.load ();
225 free_cnt = (w - r + size) & size_mask;
236 vec->
buf[0] = &buf[r];
237 vec->
len[0] = size - r;
239 vec->
len[1] = cnt2 & size_mask;
245 vec->
buf[0] = &buf[r];
246 vec->
len[0] = free_cnt;
252 template<
class T>
void
260 w = write_idx.load ();
261 r = read_idx.load ();
264 free_cnt = ((r - w + size) & size_mask) - 1;
266 free_cnt = (r - w) - 1;
280 vec->
buf[0] = &buf[w];
281 vec->
len[0] = size - w;
283 vec->
len[1] = cnt2 & size_mask;
285 vec->
buf[0] = &buf[w];
286 vec->
len[0] = free_cnt;
void increment_read_idx(size_t cnt)
void get_write_vector(rw_vector *)
RingBuffer(RingBuffer const &)
std::atomic< int > write_idx
void decrement_read_idx(size_t cnt)
void increment_write_idx(size_t cnt)
size_t read(T *dest, size_t cnt)
void set(size_t r, size_t w)
size_t write_space() const
std::atomic< int > read_idx
size_t get_write_idx() const
size_t write(T const *src, size_t cnt)
size_t get_read_idx() const
size_t read_space() const
void get_read_vector(rw_vector *)