Skip to content

Commit

Permalink
util/CircularBuffer: use std::span internally
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jul 29, 2024
1 parent 596d2d9 commit dbaa72c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/input/AsyncInputStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/input/AsyncInputStream.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/input/ThreadInputStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/input/ThreadInputStream.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
49 changes: 24 additions & 25 deletions src/util/CircularBuffer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -60,7 +59,7 @@ public:
}

constexpr size_type GetCapacity() const noexcept {
return capacity;
return buffer.size();
}

constexpr bool empty() const noexcept {
Expand All @@ -77,7 +76,7 @@ public:
constexpr size_type GetSize() const noexcept {
return head <= tail
? tail - head
: capacity - head + tail;
: buffer.size() - head + tail;
}

/**
Expand All @@ -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;
}
Expand All @@ -97,33 +96,33 @@ 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);
}

/**
* Expands the tail of the buffer, after data has been written
* 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;
}
Expand All @@ -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;
}
};
2 changes: 1 addition & 1 deletion test/util/TestCircularBuffer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit dbaa72c

Please sign in to comment.