diff options
Diffstat (limited to 'src/util/PeakBuffer.cxx')
-rw-r--r-- | src/util/PeakBuffer.cxx | 74 |
1 files changed, 32 insertions, 42 deletions
diff --git a/src/util/PeakBuffer.cxx b/src/util/PeakBuffer.cxx index d9b193dd1..e4624bbec 100644 --- a/src/util/PeakBuffer.cxx +++ b/src/util/PeakBuffer.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2013 The Music Player Daemon Project + * Copyright (C) 2003-2014 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -18,46 +18,39 @@ */ #include "PeakBuffer.hxx" -#include "HugeAllocator.hxx" -#include "fifo_buffer.h" +#include "DynamicFifoBuffer.hxx" #include <algorithm> #include <assert.h> -#include <stdint.h> #include <string.h> PeakBuffer::~PeakBuffer() { - if (normal_buffer != nullptr) - fifo_buffer_free(normal_buffer); - - if (peak_buffer != nullptr) - HugeFree(peak_buffer, peak_size); + delete normal_buffer; + delete peak_buffer; } bool PeakBuffer::IsEmpty() const { - return (normal_buffer == nullptr || - fifo_buffer_is_empty(normal_buffer)) && - (peak_buffer == nullptr || - fifo_buffer_is_empty(peak_buffer)); + return (normal_buffer == nullptr || normal_buffer->IsEmpty()) && + (peak_buffer == nullptr || peak_buffer->IsEmpty()); } -const void * -PeakBuffer::Read(size_t *length_r) const +WritableBuffer<void> +PeakBuffer::Read() const { if (normal_buffer != nullptr) { - const void *p = fifo_buffer_read(normal_buffer, length_r); - if (p != nullptr) - return p; + const auto p = normal_buffer->Read(); + if (!p.IsEmpty()) + return p.ToVoid(); } if (peak_buffer != nullptr) { - const void *p = fifo_buffer_read(peak_buffer, length_r); - if (p != nullptr) - return p; + const auto p = peak_buffer->Read(); + if (!p.IsEmpty()) + return p.ToVoid(); } return nullptr; @@ -66,15 +59,15 @@ PeakBuffer::Read(size_t *length_r) const void PeakBuffer::Consume(size_t length) { - if (normal_buffer != nullptr && !fifo_buffer_is_empty(normal_buffer)) { - fifo_buffer_consume(normal_buffer, length); + if (normal_buffer != nullptr && !normal_buffer->IsEmpty()) { + normal_buffer->Consume(length); return; } - if (peak_buffer != nullptr && !fifo_buffer_is_empty(peak_buffer)) { - fifo_buffer_consume(peak_buffer, length); - if (fifo_buffer_is_empty(peak_buffer)) { - HugeFree(peak_buffer, peak_size); + if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) { + peak_buffer->Consume(length); + if (peak_buffer->IsEmpty()) { + delete peak_buffer; peak_buffer = nullptr; } @@ -83,7 +76,7 @@ PeakBuffer::Consume(size_t length) } 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(length > 0); @@ -91,14 +84,13 @@ AppendTo(fifo_buffer *buffer, const void *data, size_t length) size_t total = 0; do { - size_t max_length; - void *p = fifo_buffer_write(buffer, &max_length); - if (p == nullptr) + const auto p = buffer.Write(); + if (p.IsEmpty()) break; - const size_t nbytes = std::min(length, max_length); - memcpy(p, data, nbytes); - fifo_buffer_append(buffer, nbytes); + const size_t nbytes = std::min(length, p.size); + memcpy(p.data, data, nbytes); + buffer.Append(nbytes); data = (const uint8_t *)data + nbytes; length -= nbytes; @@ -114,15 +106,15 @@ PeakBuffer::Append(const void *data, size_t length) if (length == 0) return true; - if (peak_buffer != nullptr && !fifo_buffer_is_empty(peak_buffer)) { - size_t nbytes = AppendTo(peak_buffer, data, length); + if (peak_buffer != nullptr && !peak_buffer->IsEmpty()) { + size_t nbytes = AppendTo(*peak_buffer, data, length); return nbytes == length; } 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) { data = (const uint8_t *)data + nbytes; length -= nbytes; @@ -132,13 +124,11 @@ PeakBuffer::Append(const void *data, size_t length) if (peak_buffer == nullptr) { if (peak_size > 0) - peak_buffer = (fifo_buffer *)HugeAllocate(peak_size); + peak_buffer = new DynamicFifoBuffer<uint8_t>(peak_size); if (peak_buffer == nullptr) return false; - - fifo_buffer_init(peak_buffer, peak_size); } - nbytes = AppendTo(peak_buffer, data, length); + nbytes = AppendTo(*peak_buffer, data, length); return nbytes == length; } |