2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2013-01-15 01:12:08 +01:00
|
|
|
|
|
|
|
#ifndef MPD_PEAK_BUFFER_HXX
|
|
|
|
#define MPD_PEAK_BUFFER_HXX
|
|
|
|
|
2020-03-13 00:46:28 +01:00
|
|
|
#include <cstddef>
|
2022-05-10 17:26:41 +02:00
|
|
|
#include <span>
|
2013-01-15 01:12:08 +01:00
|
|
|
|
2013-12-15 22:32:05 +01:00
|
|
|
template<typename T> class DynamicFifoBuffer;
|
2013-01-15 01:12:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A FIFO-like buffer that will allocate more memory on demand to
|
|
|
|
* allow large peaks. This second buffer will be given back to the
|
|
|
|
* kernel when it has been consumed.
|
|
|
|
*/
|
|
|
|
class PeakBuffer {
|
2021-01-21 16:37:14 +01:00
|
|
|
std::size_t normal_size, peak_size;
|
2013-01-15 01:12:08 +01:00
|
|
|
|
2021-01-21 16:43:20 +01:00
|
|
|
DynamicFifoBuffer<std::byte> *normal_buffer, *peak_buffer;
|
2013-01-15 01:12:08 +01:00
|
|
|
|
|
|
|
public:
|
2021-01-21 16:41:57 +01:00
|
|
|
PeakBuffer(std::size_t _normal_size, std::size_t _peak_size) noexcept
|
2013-01-15 01:12:08 +01:00
|
|
|
:normal_size(_normal_size), peak_size(_peak_size),
|
|
|
|
normal_buffer(nullptr), peak_buffer(nullptr) {}
|
|
|
|
|
2021-01-21 16:41:57 +01:00
|
|
|
PeakBuffer(PeakBuffer &&other) noexcept
|
2013-01-15 01:12:08 +01:00
|
|
|
:normal_size(other.normal_size), peak_size(other.peak_size),
|
|
|
|
normal_buffer(other.normal_buffer),
|
|
|
|
peak_buffer(other.peak_buffer) {
|
|
|
|
other.normal_buffer = nullptr;
|
|
|
|
other.peak_buffer = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:41:57 +01:00
|
|
|
~PeakBuffer() noexcept;
|
2013-01-15 01:12:08 +01:00
|
|
|
|
|
|
|
PeakBuffer(const PeakBuffer &) = delete;
|
|
|
|
PeakBuffer &operator=(const PeakBuffer &) = delete;
|
|
|
|
|
2021-01-21 16:44:11 +01:00
|
|
|
std::size_t max_size() const noexcept {
|
|
|
|
return normal_size + peak_size;
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:19:31 +02:00
|
|
|
[[gnu::pure]]
|
2017-11-10 19:24:33 +01:00
|
|
|
bool empty() const noexcept;
|
2013-01-15 01:12:08 +01:00
|
|
|
|
2022-04-26 20:19:31 +02:00
|
|
|
[[gnu::pure]]
|
2022-05-10 17:26:41 +02:00
|
|
|
std::span<std::byte> Read() const noexcept;
|
2013-12-15 22:39:30 +01:00
|
|
|
|
2021-01-21 16:37:14 +01:00
|
|
|
void Consume(std::size_t length) noexcept;
|
2013-01-15 01:12:08 +01:00
|
|
|
|
2022-05-10 17:26:41 +02:00
|
|
|
bool Append(std::span<const std::byte> src);
|
2013-01-15 01:12:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|