#4496 'freeze when having a huge torrent' -- add a bitfield helper function to init the bitfield from an array of flags.

This commit is contained in:
Jordan Lee 2011-09-20 23:39:40 +00:00
parent 3362a73a22
commit 0560b14cfe
3 changed files with 27 additions and 2 deletions

View File

@ -300,6 +300,27 @@ tr_bitfieldSetRaw( tr_bitfield * b, const void * bits, size_t byte_count )
tr_bitfieldRebuildTrueCount( b );
}
void
tr_bitfieldSetFromFlags( tr_bitfield * b, const bool * flags, size_t n )
{
size_t i;
size_t trueCount = 0;
tr_bitfieldFreeArray( b );
tr_bitfieldEnsureBitsAlloced( b, n );
for( i=0; i<n; ++i )
{
if( flags[i] )
{
++trueCount;
b->bits[i >> 3u] |= ( 0x80 >> ( i & 7u ) );
}
}
tr_bitfieldSetTrueCount( b, trueCount );
}
void
tr_bitfieldAdd( tr_bitfield * b, size_t nth )
{

View File

@ -70,6 +70,8 @@ tr_bitfieldDestruct( tr_bitfield * b )
****
***/
void tr_bitfieldSetFromFlags( tr_bitfield*, const bool * bytes, size_t n );
void tr_bitfieldSetFromBitfield( tr_bitfield*, const tr_bitfield* );
void tr_bitfieldSetRaw( tr_bitfield*, const void * bits, size_t byte_count );

View File

@ -255,9 +255,11 @@ tr_cpCreatePieceBitfield( const tr_completion * cp, size_t * byte_count )
tr_bitfieldSetHasAll( &pieces );
else if( !tr_cpHasNone( cp ) ) {
tr_piece_index_t i;
bool * flags = tr_new( bool, n );
for( i=0; i<n; ++i )
if( tr_cpPieceIsComplete( cp, i ) )
tr_bitfieldAdd( &pieces, i );
flags[i] = tr_cpPieceIsComplete( cp, i );
tr_bitfieldSetFromFlags( &pieces, flags, n );
tr_free( flags );
}
ret = tr_bitfieldGetRaw( &pieces, byte_count );