mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-08-30 23:02:39 -06:00
lz4 fixed for ST
git-svn-id: svn://ultimatepp.org/upp/trunk@10091 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
dbe3da0a2d
commit
c07b018a5c
5 changed files with 46 additions and 22 deletions
|
|
@ -10,7 +10,9 @@ void LZ4CompressStream::Init()
|
|||
pos = 0;
|
||||
header = false;
|
||||
xxh.Reset();
|
||||
#ifdef _MULTITHREADED
|
||||
outblock = inblock = 0;
|
||||
#endif
|
||||
SetupBuffer();
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +63,8 @@ void LZ4CompressStream::FlushOut()
|
|||
pos += origsize;
|
||||
|
||||
WhenPos(pos);
|
||||
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
if(co) {
|
||||
String bs = buffer;
|
||||
int inblk = inblock++;
|
||||
|
|
@ -78,7 +81,9 @@ void LZ4CompressStream::FlushOut()
|
|||
xxh.Put(~bs, origsize);
|
||||
SetupBuffer();
|
||||
}
|
||||
else {
|
||||
else
|
||||
#endif
|
||||
{
|
||||
Buffer<char> outbuf(4 + LZ4_compressBound(BLOCK_BYTES));
|
||||
xxh.Put(~buffer, origsize);
|
||||
int clen = LZ4_compress(~buffer, ~outbuf + 4, origsize);
|
||||
|
|
@ -91,11 +96,13 @@ void LZ4CompressStream::Close()
|
|||
{
|
||||
ASSERT(compress >= 0);
|
||||
FlushOut();
|
||||
#ifdef _MULTITHREADED
|
||||
if(co) {
|
||||
Mutex::Lock __(lock);
|
||||
while(outblock != inblock)
|
||||
cond.Wait(lock);
|
||||
}
|
||||
#endif
|
||||
byte h[8];
|
||||
Poke32le(h, 0);
|
||||
Poke32le(h + 4, xxh.Finish());
|
||||
|
|
@ -143,7 +150,9 @@ void LZ4CompressStream::_Put(const void *data, dword size)
|
|||
|
||||
LZ4CompressStream::LZ4CompressStream()
|
||||
{
|
||||
#ifdef _MULTITHREADED
|
||||
co = false;
|
||||
#endif
|
||||
out = NULL;
|
||||
Init();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ void LZ4DecompressStream::Init()
|
|||
buffer.Clear();
|
||||
ptr = rdlim = (byte *)~buffer;
|
||||
xxh.Reset();
|
||||
outblock = inblock = 0;
|
||||
ClearError();
|
||||
}
|
||||
|
||||
|
|
@ -175,7 +174,6 @@ dword LZ4DecompressStream::_Get(void *data, dword size)
|
|||
|
||||
LZ4DecompressStream::LZ4DecompressStream()
|
||||
{
|
||||
co = false;
|
||||
in = NULL;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,13 +40,15 @@ class Lz4 { // Filter is deprecated, use Streams instead, will be removed soon
|
|||
String header_data;
|
||||
|
||||
String out;
|
||||
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
bool parallel;
|
||||
CoWork co;
|
||||
Mutex lock;
|
||||
ConditionVariable cond;
|
||||
int outblock;
|
||||
int inblock;
|
||||
#endif
|
||||
|
||||
void TryHeader();
|
||||
|
||||
|
|
@ -71,8 +73,10 @@ public:
|
|||
|
||||
void Compress();
|
||||
void Decompress();
|
||||
|
||||
|
||||
#ifdef _MUTLITHREADED
|
||||
void Parallel(bool b = true) { parallel = b; }
|
||||
#endif
|
||||
|
||||
bool IsError() const { return error; }
|
||||
|
||||
|
|
@ -101,11 +105,13 @@ protected:
|
|||
byte lz4hdr;
|
||||
String header_data;
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
bool co;
|
||||
Mutex lock;
|
||||
ConditionVariable cond;
|
||||
int outblock;
|
||||
int inblock;
|
||||
#endif
|
||||
|
||||
void Init();
|
||||
void SetupBuffer();
|
||||
|
|
@ -115,7 +121,9 @@ protected:
|
|||
public:
|
||||
Event<int64> WhenPos;
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
void Concurrent(bool b = true) { co = b; }
|
||||
#endif
|
||||
void Open(Stream& out_) { Init(); out = &out_; }
|
||||
|
||||
LZ4CompressStream();
|
||||
|
|
@ -145,12 +153,6 @@ private:
|
|||
byte lz4hdr;
|
||||
bool eof;
|
||||
|
||||
bool co;
|
||||
Mutex lock;
|
||||
ConditionVariable cond;
|
||||
int outblock;
|
||||
int inblock;
|
||||
|
||||
void TryHeader();
|
||||
|
||||
void Init();
|
||||
|
|
@ -164,7 +166,9 @@ public:
|
|||
|
||||
bool Open(Stream& in);
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
void Concurrent(bool b = true) { co = b; }
|
||||
#endif
|
||||
|
||||
LZ4DecompressStream();
|
||||
LZ4DecompressStream(Stream& in) : LZ4DecompressStream() { Open(in); }
|
||||
|
|
@ -179,12 +183,14 @@ String LZ4Compress(const String& s, Gate2<int64, int64> progress = false);
|
|||
String LZ4Decompress(const void *data, int64 len, Gate2<int64, int64> progress = false);
|
||||
String LZ4Decompress(const String& s, Gate2<int64, int64> progress = false);
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
int64 CoLZ4Compress(Stream& out, Stream& in, Gate2<int64, int64> progress = false);
|
||||
int64 CoLZ4Decompress(Stream& out, Stream& in, Gate2<int64, int64> progress = false);
|
||||
String CoLZ4Compress(const void *data, int64 len, Gate2<int64, int64> progress = false);
|
||||
String CoLZ4Compress(const String& s, Gate2<int64, int64> progress = false);
|
||||
String CoLZ4Decompress(const void *data, int64 len, Gate2<int64, int64> progress = false);
|
||||
String CoLZ4Decompress(const String& s, Gate2<int64, int64> progress = false);
|
||||
#endif
|
||||
|
||||
bool IsLZ4(Stream& s);
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,9 @@ void Lz4::Init()
|
|||
pos = 0;
|
||||
header = false;
|
||||
xxh.Reset();
|
||||
#ifdef _MULTITHREADED
|
||||
outblock = inblock = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Lz4::Compress()
|
||||
|
|
@ -72,7 +74,8 @@ void Lz4::FlushOut()
|
|||
int origsize = pos;
|
||||
|
||||
pos = 0;
|
||||
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
if(parallel) {
|
||||
String bs = buffer;
|
||||
int inblk = inblock++;
|
||||
|
|
@ -93,7 +96,9 @@ void Lz4::FlushOut()
|
|||
xxh.Put(~bs, origsize);
|
||||
buffer.SetCount(BLOCK_BYTES);
|
||||
}
|
||||
else {
|
||||
else
|
||||
#endif
|
||||
{
|
||||
Buffer<char> outbuf(4 + LZ4_compressBound(maxblock));
|
||||
xxh.Put(~buffer, origsize);
|
||||
int clen = LZ4_compress(~buffer, ~outbuf + 4, origsize);
|
||||
|
|
@ -106,11 +111,13 @@ void Lz4::End()
|
|||
ASSERT(compress >= 0);
|
||||
if(compress) {
|
||||
FlushOut();
|
||||
#ifdef _MULTITHREADED
|
||||
Mutex::Lock __(lock);
|
||||
{ RTIMING("Waiting for order");
|
||||
while(outblock != inblock)
|
||||
cond.Wait(lock);
|
||||
}
|
||||
#endif
|
||||
byte h[8];
|
||||
Poke32le(h, 0);
|
||||
Poke32le(h + 4, xxh.Finish());
|
||||
|
|
@ -261,7 +268,9 @@ void Lz4::Put(const void *ptr_, int size)
|
|||
Lz4::Lz4()
|
||||
{
|
||||
compress = -1;
|
||||
#ifdef _MULTITHREADED
|
||||
parallel = false;
|
||||
#endif
|
||||
|
||||
WhenOut = callback(this, &Lz4::PutOut);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,22 +6,18 @@ static void sCopy(Stream& out, Stream& in, Gate2<int64, int64> progress)
|
|||
{
|
||||
while(!in.IsEof()) { // TODO: progress!!!
|
||||
String h;
|
||||
{
|
||||
RTIMING("sCopy GET");
|
||||
h = in.Get(4 * 1024*1024);
|
||||
}
|
||||
{
|
||||
RTIMING("sCopy PUT");
|
||||
out.Put(h);
|
||||
}
|
||||
h = in.Get(4 * 1024*1024);
|
||||
out.Put(h);
|
||||
}
|
||||
}
|
||||
|
||||
static int64 sLZ4Compress(Stream& out, Stream& in, int64 size, Gate2<int64, int64> progress, bool co)
|
||||
{
|
||||
LZ4CompressStream outs(out);
|
||||
#ifdef _MULTITHREADED
|
||||
if(co)
|
||||
outs.Concurrent();
|
||||
#endif
|
||||
sCopy(outs, in, progress);
|
||||
outs.Close();
|
||||
if(!out.IsError() && !outs.IsError())
|
||||
|
|
@ -32,8 +28,10 @@ static int64 sLZ4Compress(Stream& out, Stream& in, int64 size, Gate2<int64, int6
|
|||
static int64 sLZ4Decompress(Stream& out, Stream& in, int64 size, Gate2<int64, int64> progress, bool co)
|
||||
{
|
||||
LZ4DecompressStream ins(in);
|
||||
#ifdef _MULTITHREADED
|
||||
if(co)
|
||||
ins.Concurrent();
|
||||
#endif
|
||||
sCopy(out, ins, progress);
|
||||
ins.Close();
|
||||
if(!out.IsError() && !ins.IsError())
|
||||
|
|
@ -75,6 +73,8 @@ String LZ4Decompress(const String& s, Gate2<int64, int64> progress)
|
|||
return LZ4Decompress(~s, s.GetLength(), progress);
|
||||
}
|
||||
|
||||
#ifdef _MULTITHREADED
|
||||
|
||||
int64 CoLZ4Compress(Stream& out, Stream& in, Gate2<int64, int64> progress)
|
||||
{
|
||||
return sLZ4Compress(out, in, in.GetLeft(), progress, true);
|
||||
|
|
@ -109,4 +109,6 @@ String CoLZ4Decompress(const String& s, Gate2<int64, int64> progress)
|
|||
return CoLZ4Decompress(~s, s.GetLength(), progress);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue