util/CircularBuffer: use std::span

This commit is contained in:
Max Kellermann
2022-05-20 10:59:53 +02:00
parent b37c031fd1
commit b50173ae8b
6 changed files with 51 additions and 55 deletions

View File

@@ -117,9 +117,9 @@ AsyncInputStream::Seek(std::unique_lock<Mutex> &lock,
break;
const size_t nbytes =
new_offset - offset < (offset_type)r.size
? new_offset - offset
: r.size;
new_offset - offset < (offset_type)r.size()
? new_offset - offset
: r.size();
buffer.Consume(nbytes);
offset += nbytes;
@@ -193,8 +193,8 @@ AsyncInputStream::Read(std::unique_lock<Mutex> &lock,
cond_handler.cond.wait(lock);
}
const size_t nbytes = std::min(read_size, r.size);
memcpy(ptr, r.data, nbytes);
const size_t nbytes = std::min(read_size, r.size());
memcpy(ptr, r.data(), nbytes);
buffer.Consume(nbytes);
offset += (offset_type)nbytes;
@@ -222,17 +222,17 @@ AsyncInputStream::AppendToBuffer(const void *data, size_t append_size) noexcept
auto w = buffer.Write();
assert(!w.empty());
size_t nbytes = std::min(w.size, append_size);
memcpy(w.data, data, nbytes);
size_t nbytes = std::min(w.size(), append_size);
memcpy(w.data(), data, nbytes);
buffer.Append(nbytes);
const size_t remaining = append_size - nbytes;
if (remaining > 0) {
w = buffer.Write();
assert(!w.empty());
assert(w.size >= remaining);
assert(w.size() >= remaining);
memcpy(w.data, (const uint8_t *)data + nbytes, remaining);
memcpy(w.data(), (const uint8_t *)data + nbytes, remaining);
buffer.Append(remaining);
}

View File

@@ -93,7 +93,7 @@ ThreadInputStream::ThreadFunc() noexcept
try {
const ScopeUnlock unlock(mutex);
nbytes = ThreadRead(w.data, w.size);
nbytes = ThreadRead(w.data(), w.size());
} catch (...) {
postponed_exception = std::current_exception();
InvokeOnAvailable();
@@ -145,8 +145,8 @@ ThreadInputStream::Read(std::unique_lock<Mutex> &lock,
auto r = buffer.Read();
if (!r.empty()) {
size_t nbytes = std::min(read_size, r.size);
memcpy(ptr, r.data, nbytes);
size_t nbytes = std::min(read_size, r.size());
memcpy(ptr, r.data(), nbytes);
buffer.Consume(nbytes);
wake_cond.notify_all();
offset += nbytes;

View File

@@ -240,7 +240,7 @@ AlsaInputStream::DispatchSockets() noexcept
const std::scoped_lock<Mutex> protect(mutex);
auto w = PrepareWriteBuffer();
const snd_pcm_uframes_t w_frames = w.size / frame_size;
const snd_pcm_uframes_t w_frames = w.size() / frame_size;
if (w_frames == 0) {
/* buffer is full */
Pause();
@@ -249,7 +249,7 @@ AlsaInputStream::DispatchSockets() noexcept
snd_pcm_sframes_t n_frames;
while ((n_frames = snd_pcm_readi(capture_handle,
w.data, w_frames)) < 0) {
w.data(), w_frames)) < 0) {
if (n_frames == -EAGAIN)
return;

View File

@@ -123,7 +123,7 @@ UringInputStream::SubmitRead() noexcept
read_operation = std::make_unique<Uring::ReadOperation>();
read_operation->Start(uring, fd, next_offset,
std::min(w.size, URING_MAX_READ),
std::min(w.size(), URING_MAX_READ),
*this);
}
@@ -158,8 +158,8 @@ UringInputStream::OnRead(std::unique_ptr<std::byte[]> data,
}
auto w = PrepareWriteBuffer();
assert(w.size >= nbytes);
memcpy(w.data, data.get(), nbytes);
assert(w.size() >= nbytes);
memcpy(w.data(), data.get(), nbytes);
CommitWriteBuffer(nbytes);
next_offset += nbytes;
SubmitRead();