diff options
Diffstat (limited to '')
-rw-r--r-- | compress.cc | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/compress.cc b/compress.cc index c4428ea..050fdc1 100644 --- a/compress.cc +++ b/compress.cc @@ -1,4 +1,4 @@ -/* Plzip - A parallel compressor compatible with lzip +/* Plzip - Parallel compressor compatible with lzip Copyright (C) 2009 Laszlo Ersek. Copyright (C) 2009, 2010, 2011, 2012, 2013 Antonio Diaz Diaz. @@ -80,61 +80,70 @@ int writeblock( const int fd, const uint8_t * const buf, const int size ) void xinit( pthread_mutex_t * const mutex ) { const int errcode = pthread_mutex_init( mutex, 0 ); - if( errcode ) { show_error( "pthread_mutex_init", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_mutex_init", errcode ); cleanup_and_fail(); } } void xinit( pthread_cond_t * const cond ) { const int errcode = pthread_cond_init( cond, 0 ); - if( errcode ) { show_error( "pthread_cond_init", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_cond_init", errcode ); cleanup_and_fail(); } } void xdestroy( pthread_mutex_t * const mutex ) { const int errcode = pthread_mutex_destroy( mutex ); - if( errcode ) { show_error( "pthread_mutex_destroy", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_mutex_destroy", errcode ); cleanup_and_fail(); } } void xdestroy( pthread_cond_t * const cond ) { const int errcode = pthread_cond_destroy( cond ); - if( errcode ) { show_error( "pthread_cond_destroy", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_cond_destroy", errcode ); cleanup_and_fail(); } } void xlock( pthread_mutex_t * const mutex ) { const int errcode = pthread_mutex_lock( mutex ); - if( errcode ) { show_error( "pthread_mutex_lock", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_mutex_lock", errcode ); cleanup_and_fail(); } } void xunlock( pthread_mutex_t * const mutex ) { const int errcode = pthread_mutex_unlock( mutex ); - if( errcode ) { show_error( "pthread_mutex_unlock", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_mutex_unlock", errcode ); cleanup_and_fail(); } } void xwait( pthread_cond_t * const cond, pthread_mutex_t * const mutex ) { const int errcode = pthread_cond_wait( cond, mutex ); - if( errcode ) { show_error( "pthread_cond_wait", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_cond_wait", errcode ); cleanup_and_fail(); } } void xsignal( pthread_cond_t * const cond ) { const int errcode = pthread_cond_signal( cond ); - if( errcode ) { show_error( "pthread_cond_signal", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_cond_signal", errcode ); cleanup_and_fail(); } } void xbroadcast( pthread_cond_t * const cond ) { const int errcode = pthread_cond_broadcast( cond ); - if( errcode ) { show_error( "pthread_cond_broadcast", errcode ); fatal(); } + if( errcode ) + { show_error( "pthread_cond_broadcast", errcode ); cleanup_and_fail(); } } @@ -317,10 +326,10 @@ extern "C" void * csplitter( void * arg ) for( bool first_post = true; ; first_post = false ) { uint8_t * const data = new( std::nothrow ) uint8_t[data_size]; - if( !data ) { pp( mem_msg ); fatal(); } + if( !data ) { pp( mem_msg ); cleanup_and_fail(); } const int size = readblock( infd, data, data_size ); if( size != data_size && errno ) - { pp(); show_error( "Read error", errno ); fatal(); } + { pp(); show_error( "Read error", errno ); cleanup_and_fail(); } if( size > 0 || first_post ) // first packet may be empty { @@ -365,7 +374,7 @@ extern "C" void * cworker( void * arg ) const int max_compr_size = 42 + packet->size + ( ( packet->size + 7 ) / 8 ); uint8_t * const new_data = new( std::nothrow ) uint8_t[max_compr_size]; - if( !new_data ) { pp( mem_msg ); fatal(); } + if( !new_data ) { pp( mem_msg ); cleanup_and_fail(); } const int dict_size = std::max( LZ_min_dictionary_size(), std::min( dictionary_size, packet->size ) ); LZ_Encoder * const encoder = @@ -376,7 +385,7 @@ extern "C" void * cworker( void * arg ) pp( mem_msg ); else internal_error( "invalid argument to encoder" ); - fatal(); + cleanup_and_fail(); } int written = 0; @@ -403,7 +412,7 @@ extern "C" void * cworker( void * arg ) if( verbosity >= 0 ) std::fprintf( stderr, "LZ_compress_read error: %s.\n", LZ_strerror( LZ_compress_errno( encoder ) ) ); - fatal(); + cleanup_and_fail(); } new_pos += rd; if( new_pos > max_compr_size ) @@ -412,8 +421,9 @@ extern "C" void * cworker( void * arg ) } if( LZ_compress_close( encoder ) < 0 ) - { pp( "LZ_compress_close failed" ); fatal(); } + { pp( "LZ_compress_close failed" ); cleanup_and_fail(); } + if( verbosity >= 2 && packet->size > 0 ) show_progress( packet->size ); packet->data = new_data; packet->size = new_pos; courier.collect_packet( packet ); @@ -441,7 +451,7 @@ void muxer( Packet_courier & courier, const Pretty_print & pp, const int outfd ) { const int wr = writeblock( outfd, opacket->data, opacket->size ); if( wr != opacket->size ) - { pp(); show_error( "Write error", errno ); fatal(); } + { pp(); show_error( "Write error", errno ); cleanup_and_fail(); } } delete[] opacket->data; delete opacket; @@ -475,7 +485,7 @@ int compress( const int data_size, const int dictionary_size, pthread_t splitter_thread; int errcode = pthread_create( &splitter_thread, 0, csplitter, &splitter_arg ); if( errcode ) - { show_error( "Can't create splitter thread", errcode ); fatal(); } + { show_error( "Can't create splitter thread", errcode ); cleanup_and_fail(); } Worker_arg worker_arg; worker_arg.courier = &courier; @@ -484,12 +494,12 @@ int compress( const int data_size, const int dictionary_size, worker_arg.match_len_limit = match_len_limit; pthread_t * worker_threads = new( std::nothrow ) pthread_t[num_workers]; - if( !worker_threads ) { pp( mem_msg ); fatal(); } + if( !worker_threads ) { pp( mem_msg ); cleanup_and_fail(); } for( int i = 0; i < num_workers; ++i ) { errcode = pthread_create( worker_threads + i, 0, cworker, &worker_arg ); if( errcode ) - { show_error( "Can't create worker threads", errcode ); fatal(); } + { show_error( "Can't create worker threads", errcode ); cleanup_and_fail(); } } muxer( courier, pp, outfd ); @@ -498,13 +508,13 @@ int compress( const int data_size, const int dictionary_size, { errcode = pthread_join( worker_threads[i], 0 ); if( errcode ) - { show_error( "Can't join worker threads", errcode ); fatal(); } + { show_error( "Can't join worker threads", errcode ); cleanup_and_fail(); } } delete[] worker_threads; errcode = pthread_join( splitter_thread, 0 ); if( errcode ) - { show_error( "Can't join splitter thread", errcode ); fatal(); } + { show_error( "Can't join splitter thread", errcode ); cleanup_and_fail(); } if( verbosity >= 1 ) { |