2025-02-24 03:26:25 +01:00
|
|
|
/* Plzip - A parallel compressor compatible with lzip
|
2025-02-24 03:22:05 +01:00
|
|
|
Copyright (C) 2009 Laszlo Ersek.
|
2025-02-24 03:44:03 +01:00
|
|
|
Copyright (C) 2009, 2010, 2011, 2012 Antonio Diaz Diaz.
|
2025-02-24 03:22:05 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2025-02-24 03:26:25 +01:00
|
|
|
class Pretty_print
|
|
|
|
{
|
|
|
|
const char * const stdin_name;
|
|
|
|
unsigned int longest_name;
|
|
|
|
std::string name_;
|
|
|
|
mutable bool first_post;
|
|
|
|
|
|
|
|
public:
|
2025-02-24 03:44:03 +01:00
|
|
|
explicit Pretty_print( const std::vector< std::string > & filenames )
|
2025-02-24 03:33:34 +01:00
|
|
|
: stdin_name( "(stdin)" ), longest_name( 0 ), first_post( false )
|
2025-02-24 03:26:25 +01:00
|
|
|
{
|
2025-02-24 03:33:34 +01:00
|
|
|
const unsigned int stdin_name_len = std::strlen( stdin_name );
|
2025-02-24 03:26:25 +01:00
|
|
|
for( unsigned int i = 0; i < filenames.size(); ++i )
|
|
|
|
{
|
|
|
|
const std::string & s = filenames[i];
|
|
|
|
const unsigned int len = ( ( s == "-" ) ? stdin_name_len : s.size() );
|
|
|
|
if( len > longest_name ) longest_name = len;
|
|
|
|
}
|
|
|
|
if( longest_name == 0 ) longest_name = stdin_name_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_name( const std::string & filename )
|
|
|
|
{
|
|
|
|
if( filename.size() && filename != "-" ) name_ = filename;
|
|
|
|
else name_ = stdin_name;
|
|
|
|
first_post = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() const throw() { if( name_.size() ) first_post = true; }
|
|
|
|
const char * name() const throw() { return name_.c_str(); }
|
|
|
|
void operator()( const char * const msg = 0 ) const throw();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*--------------------- Defined in compress.cc ---------------------*/
|
|
|
|
|
2025-02-24 03:44:03 +01:00
|
|
|
void xinit( pthread_mutex_t * const mutex );
|
|
|
|
void xinit( pthread_cond_t * const cond );
|
|
|
|
void xdestroy( pthread_mutex_t * const mutex );
|
|
|
|
void xdestroy( pthread_cond_t * const cond );
|
|
|
|
void xlock( pthread_mutex_t * const mutex );
|
|
|
|
void xunlock( pthread_mutex_t * const mutex );
|
|
|
|
void xwait( pthread_cond_t * const cond, pthread_mutex_t * const mutex );
|
|
|
|
void xsignal( pthread_cond_t * const cond );
|
|
|
|
void xbroadcast( pthread_cond_t * const cond );
|
2025-02-24 03:26:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Slot_tally
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
unsigned long check_counter;
|
|
|
|
unsigned long wait_counter;
|
|
|
|
private:
|
|
|
|
const int num_slots; // total slots
|
|
|
|
int num_free; // remaining free slots
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t slot_av; // free slot available
|
|
|
|
|
2025-02-24 03:44:03 +01:00
|
|
|
Slot_tally( const Slot_tally & ); // declared as private
|
|
|
|
void operator=( const Slot_tally & ); // declared as private
|
|
|
|
|
2025-02-24 03:26:25 +01:00
|
|
|
public:
|
2025-02-24 03:44:03 +01:00
|
|
|
explicit Slot_tally( const int slots )
|
2025-02-24 03:26:25 +01:00
|
|
|
: check_counter( 0 ), wait_counter( 0 ),
|
|
|
|
num_slots( slots ), num_free( slots )
|
2025-02-24 03:44:03 +01:00
|
|
|
{ xinit( &mutex ); xinit( &slot_av ); }
|
2025-02-24 03:26:25 +01:00
|
|
|
|
2025-02-24 03:44:03 +01:00
|
|
|
~Slot_tally() { xdestroy( &slot_av ); xdestroy( &mutex ); }
|
2025-02-24 03:26:25 +01:00
|
|
|
|
|
|
|
bool all_free() { return ( num_free == num_slots ); }
|
|
|
|
|
|
|
|
void get_slot() // wait for a free slot
|
|
|
|
{
|
|
|
|
xlock( &mutex );
|
|
|
|
++check_counter;
|
2025-02-24 03:26:51 +01:00
|
|
|
while( num_free <= 0 )
|
2025-02-24 03:27:29 +01:00
|
|
|
{ ++wait_counter; xwait( &slot_av, &mutex ); ++check_counter; }
|
2025-02-24 03:26:25 +01:00
|
|
|
--num_free;
|
|
|
|
xunlock( &mutex );
|
|
|
|
}
|
|
|
|
|
|
|
|
void leave_slot() // return a slot to the tally
|
|
|
|
{
|
|
|
|
xlock( &mutex );
|
2025-02-24 03:26:51 +01:00
|
|
|
if( ++num_free == 1 ) xsignal( &slot_av );
|
2025-02-24 03:26:25 +01:00
|
|
|
xunlock( &mutex );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2025-02-24 03:25:27 +01:00
|
|
|
int compress( const int data_size, const int dictionary_size,
|
|
|
|
const int match_len_limit, const int num_workers,
|
2025-02-24 03:44:03 +01:00
|
|
|
const int infd, const int outfd,
|
2025-02-24 03:26:51 +01:00
|
|
|
const Pretty_print & pp, const int debug_level );
|
2025-02-24 03:25:27 +01:00
|
|
|
|
|
|
|
|
2025-02-24 03:26:25 +01:00
|
|
|
/*-------------------- Defined in decompress.cc --------------------*/
|
|
|
|
|
2025-02-24 03:44:03 +01:00
|
|
|
int decompress( const int num_workers, const int infd, const int outfd,
|
|
|
|
const Pretty_print & pp, const int debug_level,
|
|
|
|
const bool testing );
|
2025-02-24 03:26:25 +01:00
|
|
|
|
|
|
|
|
2025-02-24 03:25:27 +01:00
|
|
|
/*----------------------- Defined in main.cc -----------------------*/
|
|
|
|
|
2025-02-24 03:26:25 +01:00
|
|
|
extern int verbosity;
|
|
|
|
|
2025-02-24 03:33:34 +01:00
|
|
|
void fatal(); // terminate the program
|
2025-02-24 03:27:29 +01:00
|
|
|
|
|
|
|
void show_error( const char * const msg, const int errcode = 0, const bool help = false ) throw();
|
2025-02-24 03:44:03 +01:00
|
|
|
void internal_error( const char * const msg ) throw();
|
2025-02-24 03:27:29 +01:00
|
|
|
int readblock( const int fd, uint8_t * const buf, const int size ) throw();
|
|
|
|
int writeblock( const int fd, const uint8_t * const buf, const int size ) throw();
|