From c07b018a5cedc4149071fc88cfa166726eb472ca Mon Sep 17 00:00:00 2001 From: cxl Date: Fri, 22 Jul 2016 19:04:37 +0000 Subject: [PATCH] lz4 fixed for ST git-svn-id: svn://ultimatepp.org/upp/trunk@10091 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/plugin/lz4/Compress.cpp | 13 +++++++++++-- uppsrc/plugin/lz4/Decompress.cpp | 2 -- uppsrc/plugin/lz4/lz4.h | 22 ++++++++++++++-------- uppsrc/plugin/lz4/lz4upp.cpp | 13 +++++++++++-- uppsrc/plugin/lz4/util.cpp | 18 ++++++++++-------- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/uppsrc/plugin/lz4/Compress.cpp b/uppsrc/plugin/lz4/Compress.cpp index 5f2e15a75..502c6282e 100644 --- a/uppsrc/plugin/lz4/Compress.cpp +++ b/uppsrc/plugin/lz4/Compress.cpp @@ -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 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(); } diff --git a/uppsrc/plugin/lz4/Decompress.cpp b/uppsrc/plugin/lz4/Decompress.cpp index 40ca03d69..39061449d 100644 --- a/uppsrc/plugin/lz4/Decompress.cpp +++ b/uppsrc/plugin/lz4/Decompress.cpp @@ -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; } diff --git a/uppsrc/plugin/lz4/lz4.h b/uppsrc/plugin/lz4/lz4.h index 950c68b67..267bb1d27 100644 --- a/uppsrc/plugin/lz4/lz4.h +++ b/uppsrc/plugin/lz4/lz4.h @@ -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 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 progress = false); String LZ4Decompress(const void *data, int64 len, Gate2 progress = false); String LZ4Decompress(const String& s, Gate2 progress = false); +#ifdef _MULTITHREADED int64 CoLZ4Compress(Stream& out, Stream& in, Gate2 progress = false); int64 CoLZ4Decompress(Stream& out, Stream& in, Gate2 progress = false); String CoLZ4Compress(const void *data, int64 len, Gate2 progress = false); String CoLZ4Compress(const String& s, Gate2 progress = false); String CoLZ4Decompress(const void *data, int64 len, Gate2 progress = false); String CoLZ4Decompress(const String& s, Gate2 progress = false); +#endif bool IsLZ4(Stream& s); diff --git a/uppsrc/plugin/lz4/lz4upp.cpp b/uppsrc/plugin/lz4/lz4upp.cpp index 6504889f9..bd02c33ef 100644 --- a/uppsrc/plugin/lz4/lz4upp.cpp +++ b/uppsrc/plugin/lz4/lz4upp.cpp @@ -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 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); } diff --git a/uppsrc/plugin/lz4/util.cpp b/uppsrc/plugin/lz4/util.cpp index efd211b51..b59631aa2 100644 --- a/uppsrc/plugin/lz4/util.cpp +++ b/uppsrc/plugin/lz4/util.cpp @@ -6,22 +6,18 @@ static void sCopy(Stream& out, Stream& in, Gate2 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 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 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 progress) return LZ4Decompress(~s, s.GetLength(), progress); } +#ifdef _MULTITHREADED + int64 CoLZ4Compress(Stream& out, Stream& in, Gate2 progress) { return sLZ4Compress(out, in, in.GetLeft(), progress, true); @@ -109,4 +109,6 @@ String CoLZ4Decompress(const String& s, Gate2 progress) return CoLZ4Decompress(~s, s.GetLength(), progress); } +#endif + }; \ No newline at end of file