23 #include "spdlog/spdlog.h"
27 , _channels( channels )
32 auto buf = (
char*)malloc(
_size);
34 throw "Could not allocate memory";
38 spdlog::debug(
"Created {}-channel ringbuffer with size {}",
_channels,
_size );
44 if (buffer) free(buffer);
51 std::lock_guard<std::mutex> lock(_mutex);
52 std::vector<void*> buffers(_channels,
nullptr);
56 auto tail = (_head + _used) % _size;
58 *writeable = _head - tail;
60 *writeable = _size - tail;
62 for (
auto ch = 0; ch < _channels; ch++) {
63 buffers[ch] = (
void*)(_buffers[ch] + tail);
73 assert(written <= free_size());
74 std::lock_guard<std::mutex> lock(_mutex);
81 assert(dest.size() >= _channels);
82 assert(size <= used_size());
85 std::lock_guard<std::mutex> lock(_mutex);
86 auto end = (_head + size) % _size;
89 auto first_part = _size - _head;
90 auto second_part = size - first_part;
91 for (
auto ch = 0; ch < _channels; ch++) {
92 memcpy(dest[ch], _buffers[ch] + _head, first_part);
93 memcpy(dest[ch] + first_part, _buffers[ch], second_part);
96 for (
auto ch = 0; ch < _channels; ch++) {
97 memcpy(dest[ch], _buffers[ch] + _head, size);
100 _head = (_head + size) % _size;
void commit(size_t written)
void read(std::vector< char * > dest, size_t bytes)
std::vector< char * > _buffers
std::vector< void * > write_head(size_t *writeable)
MultichannelRingbuffer(size_t size, size_t channels)
virtual ~MultichannelRingbuffer()