Commit ecdebb31 authored by Max Kellermann's avatar Max Kellermann

util/PeakBuffer: use DynamicFifoBuffer instead of struct fifo_buffer

Switch to the C++ version.
parent 1f523be7
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include "config.h" #include "config.h"
#include "FullyBufferedSocket.hxx" #include "FullyBufferedSocket.hxx"
#include "system/SocketError.hxx" #include "system/SocketError.hxx"
#include "util/fifo_buffer.h"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "Compiler.h" #include "Compiler.h"
......
...@@ -18,48 +18,39 @@ ...@@ -18,48 +18,39 @@
*/ */
#include "PeakBuffer.hxx" #include "PeakBuffer.hxx"
#include "HugeAllocator.hxx" #include "DynamicFifoBuffer.hxx"
#include "fifo_buffer.h"
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
#include <stdint.h>
#include <string.h> #include <string.h>
PeakBuffer::~PeakBuffer() PeakBuffer::~PeakBuffer()
{ {
if (normal_buffer != nullptr) delete normal_buffer;
fifo_buffer_free(normal_buffer); delete peak_buffer;
if (peak_buffer != nullptr)
HugeFree(peak_buffer, peak_size);
} }
bool bool
PeakBuffer::IsEmpty() const PeakBuffer::IsEmpty() const
{ {
return (normal_buffer == nullptr || return (normal_buffer == nullptr || normal_buffer->IsEmpty()) &&
fifo_buffer_is_empty(normal_buffer)) && (peak_buffer == nullptr || peak_buffer->IsEmpty());
(peak_buffer == nullptr ||
fifo_buffer_is_empty(peak_buffer));
} }
ConstBuffer<void> WritableBuffer<void>
PeakBuffer::Read() const PeakBuffer::Read() const
{ {
if (normal_buffer != nullptr) { if (normal_buffer != nullptr) {
size_t size; const auto p = normal_buffer->Read();
const void *p = fifo_buffer_read(normal_buffer, &size); if (!p.IsNull())
if (p != nullptr) return p.ToVoid();
return { p, size };
} }
if (peak_buffer != nullptr) { if (peak_buffer != nullptr) {
size_t size; const auto p = peak_buffer->Read();
const void *p = fifo_buffer_read(peak_buffer, &size); if (!p.IsNull())
if (p != nullptr) return p.ToVoid();
return { p, size };
} }
return nullptr; return nullptr;
...@@ -68,15 +59,15 @@ PeakBuffer::Read() const ...@@ -68,15 +59,15 @@ PeakBuffer::Read() const
void void
PeakBuffer::Consume(size_t length) PeakBuffer::Consume(size_t length)
{ {
if (normal_buffer != nullptr && !fifo_buffer_is_empty(normal_buffer)) { if (normal_buffer != nullptr && !normal_buffer->IsEmpty()) {
fifo_buffer_consume(normal_buffer, length); normal_buffer->Consume(length);
return; return;
} }
if (peak_buffer != nullptr && !fifo_buffer_is_empty(peak_buffer)) { if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) {
fifo_buffer_consume(peak_buffer, length); peak_buffer->Consume(length);
if (fifo_buffer_is_empty(peak_buffer)) { if (peak_buffer->IsEmpty()) {
HugeFree(peak_buffer, peak_size); delete peak_buffer;
peak_buffer = nullptr; peak_buffer = nullptr;
} }
...@@ -85,7 +76,7 @@ PeakBuffer::Consume(size_t length) ...@@ -85,7 +76,7 @@ PeakBuffer::Consume(size_t length)
} }
static size_t static size_t
AppendTo(fifo_buffer *buffer, const void *data, size_t length) AppendTo(DynamicFifoBuffer<uint8_t> &buffer, const void *data, size_t length)
{ {
assert(data != nullptr); assert(data != nullptr);
assert(length > 0); assert(length > 0);
...@@ -93,14 +84,13 @@ AppendTo(fifo_buffer *buffer, const void *data, size_t length) ...@@ -93,14 +84,13 @@ AppendTo(fifo_buffer *buffer, const void *data, size_t length)
size_t total = 0; size_t total = 0;
do { do {
size_t max_length; const auto p = buffer.Write();
void *p = fifo_buffer_write(buffer, &max_length); if (p.IsNull())
if (p == nullptr)
break; break;
const size_t nbytes = std::min(length, max_length); const size_t nbytes = std::min(length, p.size);
memcpy(p, data, nbytes); memcpy(p.data, data, nbytes);
fifo_buffer_append(buffer, nbytes); buffer.Append(nbytes);
data = (const uint8_t *)data + nbytes; data = (const uint8_t *)data + nbytes;
length -= nbytes; length -= nbytes;
...@@ -116,15 +106,15 @@ PeakBuffer::Append(const void *data, size_t length) ...@@ -116,15 +106,15 @@ PeakBuffer::Append(const void *data, size_t length)
if (length == 0) if (length == 0)
return true; return true;
if (peak_buffer != nullptr && !fifo_buffer_is_empty(peak_buffer)) { if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) {
size_t nbytes = AppendTo(peak_buffer, data, length); size_t nbytes = AppendTo(*peak_buffer, data, length);
return nbytes == length; return nbytes == length;
} }
if (normal_buffer == nullptr) if (normal_buffer == nullptr)
normal_buffer = fifo_buffer_new(normal_size); normal_buffer = new DynamicFifoBuffer<uint8_t>(normal_size);
size_t nbytes = AppendTo(normal_buffer, data, length); size_t nbytes = AppendTo(*normal_buffer, data, length);
if (nbytes > 0) { if (nbytes > 0) {
data = (const uint8_t *)data + nbytes; data = (const uint8_t *)data + nbytes;
length -= nbytes; length -= nbytes;
...@@ -133,13 +123,11 @@ PeakBuffer::Append(const void *data, size_t length) ...@@ -133,13 +123,11 @@ PeakBuffer::Append(const void *data, size_t length)
} }
if (peak_buffer == nullptr && peak_size > 0) { if (peak_buffer == nullptr && peak_size > 0) {
peak_buffer = (fifo_buffer *)HugeAllocate(peak_size); peak_buffer = new DynamicFifoBuffer<uint8_t>(peak_size);
if (peak_buffer == nullptr) if (peak_buffer == nullptr)
return false; return false;
fifo_buffer_init(peak_buffer, peak_size);
} }
nbytes = AppendTo(peak_buffer, data, length); nbytes = AppendTo(*peak_buffer, data, length);
return nbytes == length; return nbytes == length;
} }
...@@ -20,13 +20,14 @@ ...@@ -20,13 +20,14 @@
#ifndef MPD_PEAK_BUFFER_HXX #ifndef MPD_PEAK_BUFFER_HXX
#define MPD_PEAK_BUFFER_HXX #define MPD_PEAK_BUFFER_HXX
#include "ConstBuffer.hxx" #include "WritableBuffer.hxx"
#include "Compiler.h" #include "Compiler.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
struct fifo_buffer; template<typename T> struct WritableBuffer;
template<typename T> struct ConstBuffer; template<typename T> class DynamicFifoBuffer;
/** /**
* A FIFO-like buffer that will allocate more memory on demand to * A FIFO-like buffer that will allocate more memory on demand to
...@@ -36,7 +37,7 @@ template<typename T> struct ConstBuffer; ...@@ -36,7 +37,7 @@ template<typename T> struct ConstBuffer;
class PeakBuffer { class PeakBuffer {
size_t normal_size, peak_size; size_t normal_size, peak_size;
fifo_buffer *normal_buffer, *peak_buffer; DynamicFifoBuffer<uint8_t> *normal_buffer, *peak_buffer;
public: public:
PeakBuffer(size_t _normal_size, size_t _peak_size) PeakBuffer(size_t _normal_size, size_t _peak_size)
...@@ -60,7 +61,7 @@ public: ...@@ -60,7 +61,7 @@ public:
bool IsEmpty() const; bool IsEmpty() const;
gcc_pure gcc_pure
ConstBuffer<void> Read() const; WritableBuffer<void> Read() const;
void Consume(size_t length); void Consume(size_t length);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment