util/*FifoBuffer: migrate from WritableBuffer to std::span

This commit is contained in:
Max Kellermann
2022-05-10 17:26:41 +02:00
committed by Max Kellermann
parent 570755f05a
commit bb7be9a4cd
23 changed files with 114 additions and 129 deletions

View File

@@ -103,7 +103,7 @@ public:
*/
pointer Write(size_type n) noexcept {
WantWrite(n);
return Write().data;
return Write().data();
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2003-2019 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2003-2022 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -27,14 +27,12 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FOREIGN_FIFO_BUFFER_HXX
#define FOREIGN_FIFO_BUFFER_HXX
#include "WritableBuffer.hxx"
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <span>
#include <utility>
/**
@@ -50,7 +48,7 @@ template<typename T>
class ForeignFifoBuffer {
public:
using size_type = std::size_t;
using Range = WritableBuffer<T>;
using Range = std::span<T>;
using pointer = typename Range::pointer;
using const_pointer = typename Range::const_pointer;
@@ -211,9 +209,9 @@ public:
size_type Read(pointer p, size_type n) noexcept {
auto range = Read();
if (n > range.size)
n = range.size;
std::copy_n(range.data, n, p);
if (n > range.size())
n = range.size();
std::copy_n(range.data(), n, p);
Consume(n);
return n;
}
@@ -227,7 +225,7 @@ public:
auto r = src.Read();
auto w = Write();
if (w.size < r.size && head > 0) {
if (w.size() < r.size() && head > 0) {
/* if the source contains more data than we
can append at the tail, try to make more
room by shifting the head to 0 */
@@ -235,9 +233,9 @@ public:
w = Write();
}
const auto n = std::min(r.size, w.size);
const auto n = std::min(r.size(), w.size());
std::move(r.data, r.data + n, w.data);
std::move(r.data(), r.data() + n, w.data());
Append(n);
src.Consume(n);
return n;
@@ -258,5 +256,3 @@ protected:
head = 0;
}
};
#endif

View File

@@ -23,8 +23,6 @@
#include <algorithm>
#include <cassert>
#include <string.h>
PeakBuffer::~PeakBuffer() noexcept
{
delete normal_buffer;
@@ -38,22 +36,22 @@ PeakBuffer::empty() const noexcept
(peak_buffer == nullptr || peak_buffer->empty());
}
WritableBuffer<void>
std::span<std::byte>
PeakBuffer::Read() const noexcept
{
if (normal_buffer != nullptr) {
const auto p = normal_buffer->Read();
if (!p.empty())
return p.ToVoid();
return p;
}
if (peak_buffer != nullptr) {
const auto p = peak_buffer->Read();
if (!p.empty())
return p.ToVoid();
return p;
}
return nullptr;
return {};
}
void
@@ -77,10 +75,9 @@ PeakBuffer::Consume(std::size_t length) noexcept
static std::size_t
AppendTo(DynamicFifoBuffer<std::byte> &buffer,
const void *data, size_t length) noexcept
std::span<const std::byte> src) noexcept
{
assert(data != nullptr);
assert(length > 0);
assert(!src.empty());
std::size_t total = 0;
@@ -89,37 +86,35 @@ AppendTo(DynamicFifoBuffer<std::byte> &buffer,
if (p.empty())
break;
const std::size_t nbytes = std::min(length, p.size);
memcpy(p.data, data, nbytes);
const std::size_t nbytes = std::min(src.size(), p.size());
std::copy_n(src.begin(), nbytes, p.begin());
buffer.Append(nbytes);
data = (const std::byte *)data + nbytes;
length -= nbytes;
src = src.subspan(nbytes);
total += nbytes;
} while (length > 0);
} while (!src.empty());
return total;
}
bool
PeakBuffer::Append(const void *data, std::size_t length)
PeakBuffer::Append(std::span<const std::byte> src)
{
if (length == 0)
if (src.empty())
return true;
if (peak_buffer != nullptr && !peak_buffer->empty()) {
std::size_t nbytes = AppendTo(*peak_buffer, data, length);
return nbytes == length;
std::size_t nbytes = AppendTo(*peak_buffer, src);
return nbytes == src.size();
}
if (normal_buffer == nullptr)
normal_buffer = new DynamicFifoBuffer<std::byte>(normal_size);
std::size_t nbytes = AppendTo(*normal_buffer, data, length);
std::size_t nbytes = AppendTo(*normal_buffer, src);
if (nbytes > 0) {
data = (const std::byte *)data + nbytes;
length -= nbytes;
if (length == 0)
src = src.subspan(nbytes);
if (src.empty())
return true;
}
@@ -130,6 +125,6 @@ PeakBuffer::Append(const void *data, std::size_t length)
return false;
}
nbytes = AppendTo(*peak_buffer, data, length);
return nbytes == length;
nbytes = AppendTo(*peak_buffer, src);
return nbytes == src.size();
}

View File

@@ -21,8 +21,8 @@
#define MPD_PEAK_BUFFER_HXX
#include <cstddef>
#include <span>
template<typename T> struct WritableBuffer;
template<typename T> class DynamicFifoBuffer;
/**
@@ -61,11 +61,11 @@ public:
bool empty() const noexcept;
[[gnu::pure]]
WritableBuffer<void> Read() const noexcept;
std::span<std::byte> Read() const noexcept;
void Consume(std::size_t length) noexcept;
bool Append(const void *data, std::size_t length);
bool Append(std::span<const std::byte> src);
};
#endif

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2003-2019 Max Kellermann <max.kellermann@gmail.com>
* Copyright 2003-2022 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -27,14 +27,12 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef STATIC_FIFO_BUFFER_HXX
#define STATIC_FIFO_BUFFER_HXX
#include "WritableBuffer.hxx"
#pragma once
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <span>
#include <utility>
/**
@@ -46,7 +44,7 @@ template<class T, size_t size>
class StaticFifoBuffer {
public:
using size_type = std::size_t;
using Range = WritableBuffer<T>;
using Range = std::span<T>;
protected:
size_type head = 0, tail = 0;
@@ -132,5 +130,3 @@ public:
head += n;
}
};
#endif

View File

@@ -37,16 +37,16 @@ char *
ReadBufferedLine(B &buffer)
{
auto r = buffer.Read();
char *newline = reinterpret_cast<char*>(std::memchr(r.data, '\n', r.size));
char *newline = reinterpret_cast<char*>(std::memchr(r.data(), '\n', r.size()));
if (newline == nullptr)
return nullptr;
buffer.Consume(newline + 1 - r.data);
buffer.Consume(newline + 1 - r.data());
if (newline > r.data && newline[-1] == '\r')
if (newline > r.data() && newline[-1] == '\r')
--newline;
*newline = 0;
return r.data;
return r.data();
}
#endif