input/thread: use class HugeArray instead of the low-level function HugeAllocate()

This commit is contained in:
Max Kellermann 2017-09-21 21:52:44 +02:00
parent bc93c7a1fc
commit 18c4ef09d5
2 changed files with 14 additions and 26 deletions

View File

@ -20,8 +20,6 @@
#include "config.h"
#include "ThreadInputStream.hxx"
#include "thread/Name.hxx"
#include "util/CircularBuffer.hxx"
#include "util/HugeAllocator.hxx"
#include <assert.h>
#include <string.h>
@ -33,8 +31,10 @@ ThreadInputStream::ThreadInputStream(const char *_plugin,
:InputStream(_uri, _mutex, _cond),
plugin(_plugin),
thread(BIND_THIS_METHOD(ThreadFunc)),
buffer_size(_buffer_size)
allocation(_buffer_size),
buffer(&allocation.front(), allocation.size())
{
allocation.ForkCow(false);
}
ThreadInputStream::~ThreadInputStream()
@ -49,25 +49,12 @@ ThreadInputStream::~ThreadInputStream()
thread.Join();
if (buffer != nullptr) {
buffer->Clear();
HugeFree(buffer->Write().data, buffer_size);
delete buffer;
}
buffer.Clear();
}
void
ThreadInputStream::Start()
{
assert(buffer == nullptr);
auto allocation = HugeAllocate(buffer_size);
assert(allocation != nullptr);
HugeForkCow(allocation.data, allocation.size, false);
buffer = new CircularBuffer<uint8_t>((uint8_t *)allocation.data,
allocation.size);
thread.Start();
}
@ -92,7 +79,7 @@ ThreadInputStream::ThreadFunc()
while (!close) {
assert(!postponed_exception);
auto w = buffer->Write();
auto w = buffer.Write();
if (w.IsEmpty()) {
wake_cond.wait(mutex);
} else {
@ -114,7 +101,7 @@ ThreadInputStream::ThreadFunc()
break;
}
buffer->Append(nbytes);
buffer.Append(nbytes);
}
}
@ -135,7 +122,7 @@ ThreadInputStream::IsAvailable() noexcept
{
assert(!thread.IsInside());
return !buffer->IsEmpty() || eof || postponed_exception;
return !buffer.IsEmpty() || eof || postponed_exception;
}
inline size_t
@ -147,11 +134,11 @@ ThreadInputStream::Read(void *ptr, size_t read_size)
if (postponed_exception)
std::rethrow_exception(postponed_exception);
auto r = buffer->Read();
auto r = buffer.Read();
if (!r.IsEmpty()) {
size_t nbytes = std::min(read_size, r.size);
memcpy(ptr, r.data, nbytes);
buffer->Consume(nbytes);
buffer.Consume(nbytes);
wake_cond.broadcast();
offset += nbytes;
return nbytes;

View File

@ -24,13 +24,13 @@
#include "InputStream.hxx"
#include "thread/Thread.hxx"
#include "thread/Cond.hxx"
#include "util/HugeAllocator.hxx"
#include "util/CircularBuffer.hxx"
#include <exception>
#include <stdint.h>
template<typename T> class CircularBuffer;
/**
* Helper class for moving InputStream implementations with blocking
* backend library implementation to a dedicated thread. Data is
@ -54,8 +54,9 @@ class ThreadInputStream : public InputStream {
std::exception_ptr postponed_exception;
const size_t buffer_size;
CircularBuffer<uint8_t> *buffer = nullptr;
HugeArray<uint8_t> allocation;
CircularBuffer<uint8_t> buffer;
/**
* Shall the stream be closed?