2021-11-24 19:41:05 +01:00
|
|
|
#include "Utilities/simple_ringbuf.h"
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
simple_ringbuf::simple_ringbuf(u64 size)
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
|
|
|
set_buf_size(size);
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
simple_ringbuf::~simple_ringbuf()
|
|
|
|
{
|
|
|
|
rw_ptr.load(); // Sync
|
|
|
|
}
|
|
|
|
|
|
|
|
simple_ringbuf::simple_ringbuf(const simple_ringbuf& other)
|
|
|
|
{
|
|
|
|
ctr_state old = other.rw_ptr.load();
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
buf = other.buf;
|
|
|
|
rw_ptr = old;
|
|
|
|
|
|
|
|
const ctr_state current = other.rw_ptr.load();
|
|
|
|
if (old == current)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
old = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
simple_ringbuf& simple_ringbuf::operator=(const simple_ringbuf& other)
|
|
|
|
{
|
|
|
|
if (this == &other) return *this;
|
|
|
|
|
|
|
|
ctr_state old = other.rw_ptr.load();
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
buf = other.buf;
|
|
|
|
rw_ptr = old;
|
|
|
|
|
|
|
|
const ctr_state current = other.rw_ptr.load();
|
|
|
|
if (old == current)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
old = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-01-10 01:43:54 +01:00
|
|
|
simple_ringbuf::simple_ringbuf(simple_ringbuf&& other)
|
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
const ctr_state other_rw_ptr = other.rw_ptr.load();
|
2022-01-10 01:43:54 +01:00
|
|
|
buf = std::move(other.buf);
|
2022-05-05 15:47:44 +02:00
|
|
|
rw_ptr = other_rw_ptr;
|
2022-01-10 01:43:54 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
other.rw_ptr.store({});
|
2022-01-10 01:43:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
simple_ringbuf& simple_ringbuf::operator=(simple_ringbuf&& other)
|
|
|
|
{
|
|
|
|
if (this == &other) return *this;
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
const ctr_state other_rw_ptr = other.rw_ptr.load();
|
2022-01-10 01:43:54 +01:00
|
|
|
buf = std::move(other.buf);
|
2022-05-05 15:47:44 +02:00
|
|
|
rw_ptr = other_rw_ptr;
|
2022-01-10 01:43:54 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
other.rw_ptr.store({});
|
2022-01-10 01:43:54 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 simple_ringbuf::get_free_size() const
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
return get_free_size(rw_ptr);
|
2021-11-24 19:41:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 simple_ringbuf::get_used_size() const
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
return get_used_size(rw_ptr);
|
2021-11-24 19:41:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 simple_ringbuf::get_total_size() const
|
2022-01-10 01:43:54 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
rw_ptr.load(); // Sync
|
|
|
|
return buf.size() - 1;
|
2022-01-10 01:43:54 +01:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 simple_ringbuf::get_free_size(ctr_state val) const
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
const u64 buf_size = buf.size();
|
|
|
|
const u64 rd = val.read_ptr % buf_size;
|
|
|
|
const u64 wr = val.write_ptr % buf_size;
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
return (wr >= rd ? buf_size + rd - wr : rd - wr) - 1;
|
2021-11-24 19:41:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 simple_ringbuf::get_used_size(ctr_state val) const
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
const u64 buf_size = buf.size();
|
|
|
|
const u64 rd = val.read_ptr % buf_size;
|
|
|
|
const u64 wr = val.write_ptr % buf_size;
|
|
|
|
|
|
|
|
return wr >= rd ? wr - rd : buf_size + wr - rd;
|
2021-11-24 19:41:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
void simple_ringbuf::set_buf_size(u64 size)
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
ensure(size != umax);
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
buf.resize(size + 1);
|
|
|
|
rw_ptr.store({});
|
|
|
|
}
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
void simple_ringbuf::writer_flush(u64 cnt)
|
|
|
|
{
|
|
|
|
rw_ptr.atomic_op([&](ctr_state& val)
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
const u64 used = get_used_size(val);
|
|
|
|
if (used == 0) return;
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
val.write_ptr += buf.size() - std::min<u64>(used, cnt);
|
2021-11-24 19:41:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
void simple_ringbuf::reader_flush(u64 cnt)
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
rw_ptr.atomic_op([&](ctr_state& val)
|
|
|
|
{
|
|
|
|
val.read_ptr += std::min(get_used_size(val), cnt);
|
|
|
|
});
|
|
|
|
}
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 simple_ringbuf::push(const void* data, u64 size, bool force)
|
|
|
|
{
|
|
|
|
ensure(data != nullptr);
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
return rw_ptr.atomic_op([&](ctr_state& val) -> u64
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
const u64 buf_size = buf.size();
|
|
|
|
const u64 old = val.write_ptr % buf_size;
|
|
|
|
const u64 free_size = get_free_size(val);
|
|
|
|
const u64 to_push = std::min(size, free_size);
|
|
|
|
const auto b_data = static_cast<const u8*>(data);
|
|
|
|
|
|
|
|
if (!to_push || (!force && free_size < size))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old + to_push > buf_size)
|
|
|
|
{
|
|
|
|
const auto first_write_sz = buf_size - old;
|
|
|
|
memcpy(&buf[old], b_data, first_write_sz);
|
|
|
|
memcpy(&buf[0], b_data + first_write_sz, to_push - first_write_sz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&buf[old], b_data, to_push);
|
|
|
|
}
|
|
|
|
|
|
|
|
val.write_ptr += to_push;
|
|
|
|
|
|
|
|
return to_push;
|
|
|
|
});
|
|
|
|
}
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 simple_ringbuf::pop(void* data, u64 size, bool force)
|
|
|
|
{
|
|
|
|
ensure(data != nullptr);
|
|
|
|
|
|
|
|
return rw_ptr.atomic_op([&](ctr_state& val) -> u64
|
2021-11-24 19:41:05 +01:00
|
|
|
{
|
2022-05-05 15:47:44 +02:00
|
|
|
const u64 buf_size = buf.size();
|
|
|
|
const u64 old = val.read_ptr % buf_size;
|
|
|
|
const u64 used_size = get_used_size(val);
|
|
|
|
const u64 to_pop = std::min(size, used_size);
|
|
|
|
const auto b_data = static_cast<u8*>(data);
|
|
|
|
|
|
|
|
if (!to_pop || (!force && used_size < size))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old + to_pop > buf_size)
|
|
|
|
{
|
|
|
|
const auto first_read_sz = buf_size - old;
|
|
|
|
memcpy(b_data, &buf[old], first_read_sz);
|
|
|
|
memcpy(b_data + first_read_sz, &buf[0], to_pop - first_read_sz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(b_data, &buf[old], to_pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
val.read_ptr += to_pop;
|
|
|
|
|
|
|
|
return to_pop;
|
2021-11-24 19:41:05 +01:00
|
|
|
});
|
|
|
|
}
|