2021-11-24 19:41:05 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "util/types.hpp"
|
|
|
|
#include "util/atomic.hpp"
|
2022-05-05 15:47:44 +02:00
|
|
|
#include <vector>
|
2021-11-24 19:41:05 +01:00
|
|
|
|
|
|
|
// Single reader/writer simple ringbuffer.
|
|
|
|
class simple_ringbuf
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
simple_ringbuf(u64 size = 0);
|
|
|
|
virtual ~simple_ringbuf();
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
simple_ringbuf(const simple_ringbuf& other);
|
|
|
|
simple_ringbuf& operator=(const simple_ringbuf& other);
|
2022-01-10 01:43:54 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
// Thread unsafe functions.
|
2022-01-10 01:43:54 +01:00
|
|
|
simple_ringbuf(simple_ringbuf&& other);
|
|
|
|
simple_ringbuf& operator=(simple_ringbuf&& other);
|
2022-05-05 15:47:44 +02:00
|
|
|
void set_buf_size(u64 size);
|
2022-01-10 01:43:54 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
// Helper functions
|
|
|
|
u64 get_free_size() const;
|
|
|
|
u64 get_used_size() const;
|
|
|
|
u64 get_total_size() const;
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
// Writer functions
|
|
|
|
u64 push(const void* data, u64 size, bool force = false);
|
|
|
|
void writer_flush(u64 cnt = umax);
|
|
|
|
|
|
|
|
// Reader functions
|
|
|
|
u64 pop(void* data, u64 size, bool force = false);
|
|
|
|
void reader_flush(u64 cnt = umax);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
struct ctr_state
|
|
|
|
{
|
|
|
|
alignas(sizeof(u64) * 2)
|
|
|
|
u64 read_ptr = 0;
|
|
|
|
u64 write_ptr = 0;
|
|
|
|
|
|
|
|
auto operator<=>(const ctr_state& other) const = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(ctr_state) == sizeof(u64) * 2);
|
|
|
|
|
|
|
|
atomic_t<ctr_state> rw_ptr{};
|
|
|
|
std::vector<u8> buf{};
|
2021-11-24 19:41:05 +01:00
|
|
|
|
2022-05-05 15:47:44 +02:00
|
|
|
u64 get_free_size(ctr_state val) const;
|
|
|
|
u64 get_used_size(ctr_state val) const;
|
2021-11-24 19:41:05 +01:00
|
|
|
};
|