@@ -145,17 +145,18 @@ BlockCompressor::BlockCompressor(const Context& ctx) :
145145 sserr << " Minimum block size is " << (MIN_BLOCK_SIZE / 1024 ) << " KiB (" ;
146146 sserr << MIN_BLOCK_SIZE << " bytes), got " << bl;
147147 sserr << (bl > 1 ? " bytes" : " byte" );
148- throw invalid_argument (sserr.str (). c_str () );
148+ throw invalid_argument (sserr.str ());
149149 }
150150
151151 if (bl > MAX_BLOCK_SIZE) {
152152 stringstream sserr;
153153 sserr << " Maximum block size is " << (MAX_BLOCK_SIZE / (1024 * 1024 * 1024 )) << " GiB (" ;
154154 sserr << MAX_BLOCK_SIZE << " bytes), got " << bl << " bytes" ;
155- throw invalid_argument (sserr.str (). c_str () );
155+ throw invalid_argument (sserr.str ());
156156 }
157157
158- _blockSize = min ((int (bl) + 15 ) & -16 , MAX_BLOCK_SIZE);
158+ bl = (bl + 15 ) & ~uint64 (15 );
159+ _blockSize = int (min (bl, uint64 (MAX_BLOCK_SIZE)));
159160 }
160161}
161162
@@ -303,7 +304,7 @@ int BlockCompressor::compress(uint64& outputSize)
303304 }
304305 else {
305306 if ((formattedOutName.size () != 0 ) && (specialOutput == false )) {
306- if ((STAT (formattedOutName.c_str (), &buffer) ! = 0 ) && ((buffer.st_mode & S_IFDIR) != 0 )) {
307+ if ((STAT (formattedOutName.c_str (), &buffer) = = 0 ) && ((buffer.st_mode & S_IFDIR) != 0 )) {
307308 cerr << " Output must be a file (or 'NONE')" << endl;
308309 return Error::ERR_CREATE_FILE;
309310 }
@@ -375,17 +376,19 @@ int BlockCompressor::compress(uint64& outputSize)
375376 oName = formattedOutName + iName.substr (formattedInName.size ()) + " .knz" ;
376377 }
377378
379+ int blockSize = _blockSize;
380+
378381 // Set the block size to optimize compression ratio when possible
379382 if ((_autoBlockSize == true ) && (_jobs > 0 )) {
380383 const int64 bl = files[i]._size / _jobs;
381- _blockSize = int (max (min ((bl + 63 ) & ~63 , int64 (MAX_BLOCK_SIZE)), int64 (MIN_BLOCK_SIZE)));
384+ blockSize = int (max (min ((bl + 63 ) & ~63 , int64 (MAX_BLOCK_SIZE)), int64 (MIN_BLOCK_SIZE)));
382385 }
383386
384387 Context taskCtx (_ctx);
385388 taskCtx.putLong (" fileSize" , files[i]._size );
386389 taskCtx.putString (" inputName" , iName);
387390 taskCtx.putString (" outputName" , oName);
388- taskCtx.putInt (" blockSize" , _blockSize );
391+ taskCtx.putInt (" blockSize" , blockSize );
389392#ifdef CONCURRENCY_ENABLED
390393 taskCtx.putInt (" jobs" , jobsPerTask[i]);
391394#else
@@ -401,7 +404,7 @@ int BlockCompressor::compress(uint64& outputSize)
401404 if (doConcurrent) {
402405 vector<FileCompressWorker<FCTask*, FileCompressResult>*> workers;
403406 vector<future<FileCompressResult> > results;
404- BoundedConcurrentQueue<FCTask*> queue (nbFiles, &tasks[0 ]);
407+ BoundedConcurrentQueue<FCTask*> queue (nbFiles, &tasks[0 ]); // !tasks.empty()
405408
406409 // Create one worker per job and run it. A worker calls several tasks sequentially.
407410 for (int i = 0 ; i < _jobs; i++) {
@@ -506,8 +509,8 @@ bool BlockCompressor::removeListener(Listener<Event>& bl)
506509
507510void BlockCompressor::notifyListeners (vector<Listener<Event>*>& listeners, const Event& evt)
508511{
509- for (vector<Listener<Event>*>::iterator it = listeners. begin (); it != listeners.end (); ++it )
510- (*it) ->processEvent (evt);
512+ for (size_t i = 0 ; i < listeners.size (); i++ )
513+ listeners[i] ->processEvent (evt);
511514}
512515
513516void BlockCompressor::getTransformAndCodec (int level, string tranformAndCodec[2 ])
@@ -616,7 +619,7 @@ T FileCompressTask<T>::run()
616619 if (samePaths (inputName, outputName)) {
617620 stringstream sserr;
618621 sserr << " The input and output files must be different" << endl;
619- return T (Error::ERR_CREATE_FILE, 0 , 0 , sserr.str (). c_str () );
622+ return T (Error::ERR_CREATE_FILE, 0 , 0 , sserr.str ());
620623 }
621624
622625 struct STAT buffer;
@@ -632,7 +635,7 @@ T FileCompressTask<T>::run()
632635 stringstream sserr;
633636 sserr << " File '" << outputName << " ' exists and the 'force' command "
634637 << " line option has not been provided" ;
635- return T (Error::ERR_OVERWRITE_FILE, 0 , 0 , sserr.str (). c_str () );
638+ return T (Error::ERR_OVERWRITE_FILE, 0 , 0 , sserr.str ());
636639 }
637640
638641 // Delete output file to ensure consistent performance
@@ -656,7 +659,7 @@ T FileCompressTask<T>::run()
656659
657660 if ((rmkd == 0 ) || (rmkd == EEXIST)) {
658661 delete os;
659- os = new ofstream (outputName.c_str (), ofstream::binary);
662+ os = new ofstream (outputName.c_str (), ofstream::out | ofstream:: binary);
660663 }
661664 else {
662665 errMsg = strerror (rmkd);
@@ -671,7 +674,7 @@ T FileCompressTask<T>::run()
671674 if (errMsg != " " )
672675 sserr << " : " << errMsg;
673676
674- return T (Error::ERR_CREATE_FILE, 0 , 0 , sserr.str (). c_str () );
677+ return T (Error::ERR_CREATE_FILE, 0 , 0 , sserr.str ());
675678 }
676679 }
677680 }
@@ -686,14 +689,14 @@ T FileCompressTask<T>::run()
686689 CLEANUP_COMP_OS
687690 stringstream sserr;
688691 sserr << " Cannot create compressed stream: " << e.what ();
689- return T (Error::ERR_CREATE_COMPRESSOR, 0 , 0 , sserr.str (). c_str () );
692+ return T (Error::ERR_CREATE_COMPRESSOR, 0 , 0 , sserr.str ());
690693 }
691694 }
692695 catch (const exception& e) {
693696 CLEANUP_COMP_OS
694697 stringstream sserr;
695698 sserr << " Cannot open output file '" << outputName << " ' for writing: " << e.what ();
696- return T (Error::ERR_CREATE_FILE, 0 , 0 , sserr.str (). c_str () );
699+ return T (Error::ERR_CREATE_FILE, 0 , 0 , sserr.str ());
697700 }
698701
699702#define CLEANUP_COMP_IS if ((_is != nullptr ) && (_is != &cin)) {\
@@ -717,20 +720,20 @@ T FileCompressTask<T>::run()
717720 _cos = nullptr ;
718721 stringstream sserr;
719722 sserr << " Cannot open input file '" << inputName << " '" ;
720- return T (Error::ERR_OPEN_FILE, 0 , 0 , sserr.str (). c_str () );
723+ return T (Error::ERR_OPEN_FILE, 0 , 0 , sserr.str ());
721724 }
722725
723726 _is = ifs;
724727 }
725728 }
726729 catch (const exception& e) {
727- CLEANUP_COMP_IS
728- CLEANUP_COMP_OS
729730 delete _cos;
730731 _cos = nullptr ;
732+ CLEANUP_COMP_IS
733+ CLEANUP_COMP_OS
731734 stringstream sserr;
732735 sserr << " Cannot open input file '" << inputName << " ': " << e.what ();
733- return T (Error::ERR_OPEN_FILE, 0 , 0 , sserr.str (). c_str () );
736+ return T (Error::ERR_OPEN_FILE, 0 , 0 , sserr.str ());
734737 }
735738
736739 // Compress
@@ -779,47 +782,40 @@ T FileCompressTask<T>::run()
779782 }
780783 }
781784 catch (const IOException& ioe) {
782- CLEANUP_COMP_IS
783- CLEANUP_COMP_OS
784785 const uint64 w = _cos->getWritten ();
785- delete[] buf;
786786 delete _cos;
787787 _cos = nullptr ;
788+ CLEANUP_COMP_IS
789+ CLEANUP_COMP_OS
790+ delete[] buf;
788791 return T (ioe.error (), read, w, ioe.what ());
789792 }
790793 catch (const exception& e) {
791- CLEANUP_COMP_IS
792- CLEANUP_COMP_OS
793794 const uint64 w = _cos->getWritten ();
794- delete[] buf;
795795 delete _cos;
796796 _cos = nullptr ;
797+ CLEANUP_COMP_IS
798+ CLEANUP_COMP_OS
799+ delete[] buf;
797800 stringstream sserr;
798801 sserr << " An unexpected condition happened. Exiting ..." << endl
799802 << e.what ();
800- return T (Error::ERR_UNKNOWN, read, w, sserr.str (). c_str () );
803+ return T (Error::ERR_UNKNOWN, read, w, sserr.str ());
801804 }
802805
803806 // Close streams to ensure all data are flushed
804807 dispose ();
805808
806809 uint64 encoded = _cos->getWritten ();
807810
808- // os destructor will call close if ofstream
809- CLEANUP_COMP_OS
810-
811811 // Clean up resources at the end of the method as the task may be
812812 // recycled in a threadpool and the destructor not called.
813813 delete _cos;
814814 _cos = nullptr ;
815815
816- try {
817- CLEANUP_COMP_IS
818- _is = nullptr ;
819- }
820- catch (const exception&) {
821- // Ignore: best effort
822- }
816+ // os destructor will call close if ofstream
817+ CLEANUP_COMP_OS
818+ CLEANUP_COMP_IS
823819
824820 stopClock.stop ();
825821 double delta = stopClock.elapsed ();
@@ -933,7 +929,6 @@ void FileCompressTask<T>::dispose()
933929 }
934930 catch (const exception& e) {
935931 cerr << " Compression failure: " << e.what () << endl;
936- exit (Error::ERR_WRITE_FILE);
937932 }
938933
939934 // _is destructor will call close if ifstream
0 commit comments