[+] Begin work on block compressors

[*] Bug fixes
This commit is contained in:
Reece Wilson 2021-09-13 22:13:41 +01:00
parent 5d18f3535c
commit 7c90933e97
7 changed files with 129 additions and 46 deletions

View File

@ -12,12 +12,22 @@ namespace Aurora::Compression
class ICompressionStream
{
public:
/// Ingest n bytes from the input stream assigned to the compression object
virtual AuStreamReadWrittenPair_t Ingest(AuUInt32 bytesFromUnprocessedInputSource) = 0;
// Limited stream API
/// Limited stream API
virtual bool ReadByProcessedN (void * /*opt*/, AuUInt32 minimumProcessed, AuStreamReadWrittenPair_t &pair, bool ingestUntilEOS = true) = 0;
virtual bool ReadByProcessedN (void * /*opt*/, AuUInt32 minimumInflated) = 0;
/// Limited stream API
virtual bool ReadByProcessedN (void * /*opt*/, AuUInt32 minimumProcessed) = 0;
/// Limited stream API
virtual bool GoBackByProcessedN (AuUInt32 offset) = 0;
/// Limited stream API
virtual bool GoForwardByProcessedN(AuUInt32 offset) = 0;
/// Compression only
virtual void Flush() = 0;
};
}

View File

@ -16,48 +16,110 @@
namespace Aurora::Compression
{
#if 0
AuStreamReadWrittenPair_t BaseStreamDeflate::Ingest(AuUInt32 input)
AuStreamReadWrittenPair_t BaseCompressionStream::Ingest(AuUInt32 input)
{
auto ingest = IngestImpl(input);
_count += ingest.first;
auto ingest = Process(input);
_count += ingest.second;
return ingest;
}
bool BaseStreamDeflate::Read(void * /*opt*/ buffer, AuUInt32 &len, bool ingestUntilError)
bool BaseCompressionStream::ReadByProcessedN(void *buffer, AuUInt32 minimumDeflated, AuStreamReadWrittenPair_t &pair, bool ingestUntilEOS)
{
AuUInt32 read {}, len {};
if (_count != _lastCount)
{
Flush();
}
if (ingestUntilError)
if (ingestUntilEOS)
{
while (this->_outbuffer.size() < len)
while (this->_outbuffer.RemainingBytes() < minimumDeflated)
{
if (Ingest(4096).second == 0)
auto toRead = minimumDeflated ? std::min(AuUInt32(4096), AuUInt32(minimumDeflated - this->_outbuffer.RemainingBytes())) : 4096;
if (Ingest(toRead).second == 0)
{
if (this->_outbuffer.size())
if (!this->_outbuffer.RemainingBytes())
{
break;
return false;
}
return false;
break;
}
read += toRead;
}
}
return StreamRead(buffer, len, this->_outbuffer);
len = this->_outbuffer.Read(buffer, minimumDeflated, buffer == nullptr);
pair = {read, len};
return len != 0;
}
AUKN_SYM ICompressionStream *CompressorNew(IO::IStreamReader *reader, const CompressionInfo &info)
void BaseCompressionStream::Flush()
{
return nullptr;
ProcessFlush();
_lastCount = _count;
}
bool BaseCompressionStream::ReadByProcessedN(void *buffer, AuUInt32 minimumDeflated)
{
AuUInt32 read {}, len {};
len = this->_outbuffer.Read(buffer, minimumDeflated, buffer == nullptr);
return len != 0;
}
bool BaseCompressionStream::GoBackByProcessedN(AuUInt32 offset)
{
return this->_outbuffer.ReaderTryGoBack(offset);
}
bool BaseCompressionStream::GoForwardByProcessedN(AuUInt32 offset)
{
return this->_outbuffer.ReaderTryGoForward(offset);
}
bool BaseCompressionStream::Write(const void *a, AuUInt32 length)
{
auto written = this->_outbuffer.Write(reinterpret_cast<const AuUInt8 *>(a),
length);
if (written != length)
{
auto increase = std::max(0, (int)length - (int)this->_outbuffer.RemainingWrite());
increase += this->_outbuffer.length;
if (increase > 64 * 1024 * 1024)
{
return false;
}
if (!this->_outbuffer.Resize(increase))
{
return false;
}
auto remaining = length - written;
written = this->_outbuffer.Write(reinterpret_cast<const AuUInt8 *>(a) + written,
remaining);
if (written != remaining)
{
return false;
}
}
return true;
}
AuUInt32 BaseCompressionStream::GetInternalBufferSize()
{
return this->_outbuffer.allocSize;
}
AUKN_SYM void CompressorRelease(ICompressionStream * stream)
{
SafeDelete<BaseStreamDeflate *>(stream);
SafeDelete<BaseCompressionStream *>(stream);
}
#endif
}

View File

@ -9,24 +9,32 @@
namespace Aurora::Compression
{
#if 0
class BaseStreamDeflate : public ICompressionStream
class BaseCompressionStream : public ICompressionStream
{
public:
virtual ~BaseStreamDeflate() { }
BaseCompressionStream(int bufferSize = 4096 * 4) : _outbuffer(bufferSize, true) {}
virtual AuStreamReadWrittenPair_t IngestImpl(AuUInt32 input);
virtual void Flush() = 0;
virtual ~BaseCompressionStream() {}
virtual bool Init(Aurora::IO::IStreamReader *reader, const CompressionInfo &info) = 0;
bool Read(void * /*opt*/ buffer, AuUInt32 &len, bool ingestUntilError) override;
AuStreamReadWrittenPair_t Ingest(AuUInt32 input) override;
virtual bool Init(const AuSPtr<Aurora::IO::IStreamReader> &reader) = 0;
virtual AuStreamReadWrittenPair_t Process(AuUInt32 len) = 0;
virtual void ProcessFlush() = 0;
virtual AuStreamReadWrittenPair_t Ingest(AuUInt32 bytesFromUnprocessedInputSource) override;
virtual void Flush() override;
virtual bool ReadByProcessedN(void * /*opt*/, AuUInt32 minimumDeflated) override;
virtual bool ReadByProcessedN(void * /*opt*/, AuUInt32 minimumDeflated, AuStreamReadWrittenPair_t &pair, bool ingestUntilEOS = true) override;
virtual bool GoBackByProcessedN(AuUInt32 offset) override;
virtual bool GoForwardByProcessedN(AuUInt32 offset) override;
/// @deprecated
virtual AuUInt32 GetInternalBufferSize();
bool Write(const void *a, AuUInt32 length);
protected:
AuUInt32 _count {};
AuUInt32 _lastCount {};
AuList<AuUInt8> _outbuffer;
Aurora::Memory::ByteBuffer _outbuffer;
AuUInt32 _lastCount, _count;
};
#endif
}

View File

@ -25,7 +25,8 @@ namespace Aurora::Compression
{
while (this->_outbuffer.RemainingBytes() < minimumInflated)
{
if (Ingest(4096).second == 0)
auto toRead = minimumInflated ? std::min(AuUInt32(4096), AuUInt32(minimumInflated - this->_outbuffer.RemainingBytes())) : 4096;
if (Ingest(toRead).second == 0)
{
if (!this->_outbuffer.RemainingBytes())
{
@ -35,7 +36,7 @@ namespace Aurora::Compression
break;
}
read += 4096;
read += toRead;
}
}

View File

@ -28,6 +28,8 @@ namespace Aurora::Compression
bool Write(const void *a, AuUInt32 length);
virtual void Flush() override {}
protected:
Aurora::Memory::ByteBuffer _outbuffer;
};

View File

@ -61,12 +61,12 @@ namespace Aurora::Compression
auto startingSize = out.size();
if (!AuTryResize(out, inflatedLength))
if (!AuTryResize(out, startingSize + inflatedLength))
{
return false;
}
auto ret = ZSTD_decompress(&out[0], out.size(), buffer, length);
auto ret = ZSTD_decompress(&out[startingSize], inflatedLength, buffer, length);
if (ZSTD_isError(ret))
{
return false;

View File

@ -11,18 +11,18 @@
#if defined(AURORA_IS_BSD_DERIVED)
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <sys/limits.h>
#include <vm/vm_param.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
#include <sys/limits.h>
#include <vm/vm_param.h>
#endif
#if defined(AURORA_IS_LINUX_DERIVED)
#include <sys/sysinfo.h>
#include <sys/sysinfo.h>
#endif
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include <psapi.h>
#include <psapi.h>
#endif
namespace Aurora::HWInfo
@ -37,7 +37,7 @@ namespace Aurora::HWInfo
#if defined(AURORA_IS_MODERNNT_DERIVED)
PROCESS_MEMORY_COUNTERS pmc;
if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(PROCESS_MEMORY_COUNTERS)))
{
return {};
@ -46,9 +46,9 @@ namespace Aurora::HWInfo
return RamStat {pmc.WorkingSetSize, max};
#elif defined(AURORA_IS_POSIX_DERIVED)
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
auto used = AuUInt64(usage.ru_maxrss) * 1024;
return RamStat {used, max};
@ -101,7 +101,7 @@ namespace Aurora::HWInfo
#elif defined(AURORA_IS_LINUX_DERIVED)
struct sysinfo info;
if (sysinfo(&info) != 0)
{
return {};
@ -109,7 +109,7 @@ namespace Aurora::HWInfo
return RamStat {info.totalram - info.freeram, info.totalram}
#else
#else
return {};
#endif