util/CircularBuffer: use std::span internally

This commit is contained in:
Max Kellermann 2024-07-29 22:06:26 +02:00
parent 596d2d93dd
commit dbaa72cb40
6 changed files with 28 additions and 31 deletions

View File

@ -20,7 +20,6 @@ AsyncInputStream::AsyncInputStream(EventLoop &event_loop, std::string_view _url,
deferred_resume(event_loop, BIND_THIS_METHOD(DeferredResume)),
deferred_seek(event_loop, BIND_THIS_METHOD(DeferredSeek)),
allocation(_buffer_size),
buffer(&allocation.front(), allocation.size()),
resume_at(_resume_at)
{
allocation.SetName("InputStream");

View File

@ -23,7 +23,7 @@ class AsyncInputStream : public InputStream {
HugeArray<std::byte> allocation;
CircularBuffer<std::byte> buffer;
CircularBuffer<std::byte> buffer{allocation};
const size_t resume_at;
enum class SeekState : uint_least8_t {

View File

@ -16,8 +16,7 @@ ThreadInputStream::ThreadInputStream(const char *_plugin,
:InputStream(_uri, _mutex),
plugin(_plugin),
thread(BIND_THIS_METHOD(ThreadFunc)),
allocation(_buffer_size),
buffer(&allocation.front(), allocation.size())
allocation(_buffer_size)
{
allocation.SetName("InputStream");
allocation.ForkCow(false);

View File

@ -43,7 +43,7 @@ class ThreadInputStream : public InputStream {
HugeArray<std::byte> allocation;
CircularBuffer<std::byte> buffer;
CircularBuffer<std::byte> buffer{allocation};
/**
* Shall the stream be closed?

View File

@ -37,19 +37,18 @@ protected:
*/
size_type tail = 0;
const size_type capacity;
const pointer data;
const std::span<T> buffer;
public:
constexpr CircularBuffer(pointer _data, size_type _capacity) noexcept
:capacity(_capacity), data(_data) {}
explicit constexpr CircularBuffer(Range _buffer) noexcept
:buffer(_buffer) {}
CircularBuffer(const CircularBuffer &other) = delete;
CircularBuffer &operator=(const CircularBuffer &other) = delete;
protected:
constexpr size_type Next(size_type i) const noexcept {
return i + 1 == capacity
return i + 1 == buffer.size()
? 0
: i + 1;
}
@ -60,7 +59,7 @@ public:
}
constexpr size_type GetCapacity() const noexcept {
return capacity;
return buffer.size();
}
constexpr bool empty() const noexcept {
@ -77,7 +76,7 @@ public:
constexpr size_type GetSize() const noexcept {
return head <= tail
? tail - head
: capacity - head + tail;
: buffer.size() - head + tail;
}
/**
@ -87,7 +86,7 @@ public:
constexpr size_type GetSpace() const noexcept {
/* space = capacity - size - 1 */
return (head <= tail
? capacity - tail + head
? buffer.size() - tail + head
: head - tail)
- 1;
}
@ -97,17 +96,17 @@ public:
* When you are finished, call Append().
*/
constexpr Range Write() noexcept {
assert(head < capacity);
assert(tail < capacity);
assert(head < buffer.size());
assert(tail < buffer.size());
size_type end = tail < head
? head - 1
/* the "head==0" is there so we don't write
the last cell, as this situation cannot be
represented by head/tail */
: capacity - (head == 0);
: buffer.size() - (head == 0);
return Range(data + tail, end - tail);
return buffer.subspan(tail, end - tail);
}
/**
@ -115,15 +114,15 @@ public:
* to the buffer returned by Write().
*/
constexpr void Append(size_type n) noexcept {
assert(head < capacity);
assert(tail < capacity);
assert(n < capacity);
assert(tail + n <= capacity);
assert(head < buffer.size());
assert(tail < buffer.size());
assert(n < buffer.size());
assert(tail + n <= buffer.size());
assert(head <= tail || tail + n < head);
tail += n;
if (tail == capacity) {
if (tail == buffer.size()) {
assert(head > 0);
tail = 0;
}
@ -134,24 +133,24 @@ public:
* writable, to allow modifications while parsing.
*/
constexpr Range Read() noexcept {
assert(head < capacity);
assert(tail < capacity);
assert(head < buffer.size());
assert(tail < buffer.size());
return Range(data + head, (tail < head ? capacity : tail) - head);
return buffer.subspan(head, (tail < head ? buffer.size() : tail) - head);
}
/**
* Marks a chunk as consumed.
*/
constexpr void Consume(size_type n) noexcept {
assert(head < capacity);
assert(tail < capacity);
assert(n < capacity);
assert(head + n <= capacity);
assert(head < buffer.size());
assert(tail < buffer.size());
assert(n < buffer.size());
assert(head + n <= buffer.size());
assert(tail < head || head + n <= tail);
head += n;
if (head == capacity)
if (head == buffer.size())
head = 0;
}
};

View File

@ -10,7 +10,7 @@ TEST(CircularBuffer, Basic)
{
constexpr size_t N = 8;
int data[N];
CircularBuffer<int> buffer(data, N);
CircularBuffer<int> buffer{data};
EXPECT_EQ(size_t(N), buffer.GetCapacity());