mirror of
https://github.com/transmission/transmission
synced 2024-12-26 01:27:28 +00:00
minor refactoring of tr_bitfield to (a) simplify the tests and (b) make things easier to read
This commit is contained in:
parent
8732aa3bbc
commit
041561f232
7 changed files with 34 additions and 45 deletions
|
@ -214,7 +214,7 @@ tr_cpBlockBitfield( const tr_completion * cp )
|
|||
assert( cp );
|
||||
assert( cp->blockBitfield );
|
||||
assert( cp->blockBitfield->bits );
|
||||
assert( cp->blockBitfield->len );
|
||||
assert( cp->blockBitfield->bitCount );
|
||||
|
||||
return cp->blockBitfield;
|
||||
}
|
||||
|
@ -228,7 +228,7 @@ tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * bitfield )
|
|||
assert( bitfield );
|
||||
assert( cp->blockBitfield );
|
||||
|
||||
if( !cp || !bitfield || ( bitfield->len != cp->blockBitfield->len ) )
|
||||
if( !cp || !bitfield || ( bitfield->byteCount != cp->blockBitfield->byteCount ) )
|
||||
return TR_ERROR_ASSERT;
|
||||
|
||||
tr_cpReset( cp );
|
||||
|
|
|
@ -429,7 +429,7 @@ parseProgress( tr_torrent * tor,
|
|||
|
||||
/* get the completion bitfield */
|
||||
memset( &bitfield, 0, sizeof bitfield );
|
||||
bitfield.len = FR_BLOCK_BITFIELD_LEN( tor );
|
||||
bitfield.byteCount = FR_BLOCK_BITFIELD_LEN( tor );
|
||||
bitfield.bits = (uint8_t*) walk;
|
||||
if( !tr_cpBlockBitfieldSet( tor->completion, &bitfield ) )
|
||||
ret = TR_FR_PROGRESS;
|
||||
|
|
|
@ -662,7 +662,7 @@ getPreferredBlocks( Torrent * t, tr_block_index_t * setmeCount )
|
|||
const tr_block_index_t end = begin + tr_torPieceCountBlocks( tor, index );
|
||||
tr_block_index_t block;
|
||||
|
||||
assert( tr_bitfieldTestFast( t->requested, end ) );
|
||||
assert( tr_bitfieldTestFast( t->requested, end-1 ) );
|
||||
|
||||
for( block=begin; block<end; ++block )
|
||||
{
|
||||
|
|
|
@ -502,7 +502,8 @@ isPeerInteresting( const tr_peermsgs * msgs )
|
|||
if( !msgs->info->have )
|
||||
return TRUE;
|
||||
|
||||
assert( bitfield->len == msgs->info->have->len );
|
||||
assert( bitfield->byteCount == msgs->info->have->byteCount );
|
||||
|
||||
for( i=0; i<torrent->info.pieceCount; ++i )
|
||||
if( isPieceInteresting( msgs, i ) )
|
||||
return TRUE;
|
||||
|
@ -1754,9 +1755,9 @@ sendBitfield( tr_peermsgs * msgs )
|
|||
struct evbuffer * out = msgs->outMessages;
|
||||
|
||||
dbgmsg( msgs, "sending peer a bitfield message" );
|
||||
tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + bitfield->len );
|
||||
tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + bitfield->byteCount );
|
||||
tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD );
|
||||
tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->len );
|
||||
tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->byteCount );
|
||||
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
|
||||
}
|
||||
|
||||
|
|
|
@ -269,7 +269,7 @@ saveProgress( tr_benc * dict, const tr_torrent * tor )
|
|||
/* add the bitfield */
|
||||
bitfield = tr_cpBlockBitfield( tor->completion );
|
||||
tr_bencDictAddRaw( p, KEY_PROGRESS_BITFIELD,
|
||||
bitfield->bits, bitfield->len );
|
||||
bitfield->bits, bitfield->byteCount );
|
||||
|
||||
/* cleanup */
|
||||
tr_free( mtimes );
|
||||
|
@ -319,7 +319,7 @@ loadProgress( tr_benc * dict, tr_torrent * tor )
|
|||
if(( b = tr_bencDictFindType( p, KEY_PROGRESS_BITFIELD, TYPE_STR )))
|
||||
{
|
||||
tr_bitfield tmp;
|
||||
tmp.len = b->val.s.i;
|
||||
tmp.byteCount = b->val.s.i;
|
||||
tmp.bits = (uint8_t*) b->val.s.s;
|
||||
if( tr_cpBlockBitfieldSet( tor->completion, &tmp ) ) {
|
||||
tr_torrentUncheck( tor );
|
||||
|
|
|
@ -677,21 +677,13 @@ tr_strerror( int i )
|
|||
*****
|
||||
****/
|
||||
|
||||
/* note that the argument is how many bits are needed, not bytes */
|
||||
tr_bitfield*
|
||||
tr_bitfieldNew( size_t bitcount )
|
||||
tr_bitfieldNew( size_t bitCount )
|
||||
{
|
||||
tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) );
|
||||
if( NULL == ret )
|
||||
return NULL;
|
||||
|
||||
ret->len = (bitcount+7u) / 8u;
|
||||
ret->bits = calloc( ret->len, 1 );
|
||||
if( NULL == ret->bits ) {
|
||||
free( ret );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tr_bitfield * ret = tr_new0( tr_bitfield, 1 );
|
||||
ret->bitCount = bitCount;
|
||||
ret->byteCount = (bitCount+7u) / 8u;
|
||||
ret->bits = tr_new0( uint8_t, ret->byteCount );
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -699,8 +691,9 @@ tr_bitfield*
|
|||
tr_bitfieldDup( const tr_bitfield * in )
|
||||
{
|
||||
tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) );
|
||||
ret->len = in->len;
|
||||
ret->bits = tr_memdup( in->bits, in->len );
|
||||
ret->bitCount = in->bitCount;
|
||||
ret->byteCount = in->byteCount;
|
||||
ret->bits = tr_memdup( in->bits, in->byteCount );
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -709,15 +702,15 @@ tr_bitfieldFree( tr_bitfield * bitfield )
|
|||
{
|
||||
if( bitfield )
|
||||
{
|
||||
free( bitfield->bits );
|
||||
free( bitfield );
|
||||
tr_free( bitfield->bits );
|
||||
tr_free( bitfield );
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
tr_bitfieldClear( tr_bitfield * bitfield )
|
||||
{
|
||||
memset( bitfield->bits, 0, bitfield->len );
|
||||
memset( bitfield->bits, 0, bitfield->byteCount );
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -725,7 +718,7 @@ tr_bitfieldIsEmpty( const tr_bitfield * bitfield )
|
|||
{
|
||||
size_t i;
|
||||
|
||||
for( i=0; i<bitfield->len; ++i )
|
||||
for( i=0; i<bitfield->byteCount; ++i )
|
||||
if( bitfield->bits[i] )
|
||||
return 0;
|
||||
|
||||
|
@ -742,16 +735,13 @@ tr_bitfieldHas( const tr_bitfield * bitfield, size_t nth )
|
|||
int
|
||||
tr_bitfieldAdd( tr_bitfield * bitfield, size_t nth )
|
||||
{
|
||||
const size_t i = nth >> 3u;
|
||||
|
||||
assert( bitfield != NULL );
|
||||
assert( bitfield->bits != NULL );
|
||||
|
||||
if( i >= bitfield->len )
|
||||
if( nth >= bitfield->bitCount )
|
||||
return -1;
|
||||
|
||||
bitfield->bits[i] |= (0x80 >> (nth&7u));
|
||||
/*assert( tr_bitfieldHas( bitfield, nth ) );*/
|
||||
bitfield->bits[nth>>3u] |= (0x80 >> (nth&7u));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -772,16 +762,13 @@ int
|
|||
tr_bitfieldRem( tr_bitfield * bitfield,
|
||||
size_t nth )
|
||||
{
|
||||
const size_t i = nth >> 3u;
|
||||
|
||||
assert( bitfield != NULL );
|
||||
assert( bitfield->bits != NULL );
|
||||
|
||||
if( i >= bitfield->len )
|
||||
if( nth >= bitfield->bitCount )
|
||||
return -1;
|
||||
|
||||
bitfield->bits[i] &= (0xff7f >> (nth&7u));
|
||||
/*assert( !tr_bitfieldHas( bitfield, nth ) );*/
|
||||
bitfield->bits[nth>>3u] &= (0xff7f >> (nth&7u));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -804,9 +791,9 @@ tr_bitfieldOr( tr_bitfield * a, const tr_bitfield * b )
|
|||
uint8_t *ait;
|
||||
const uint8_t *aend, *bit;
|
||||
|
||||
assert( a->len == b->len );
|
||||
assert( a->bitCount == b->bitCount );
|
||||
|
||||
for( ait=a->bits, bit=b->bits, aend=ait+a->len; ait!=aend; )
|
||||
for( ait=a->bits, bit=b->bits, aend=ait+a->byteCount; ait!=aend; )
|
||||
*ait++ |= *bit++;
|
||||
|
||||
return a;
|
||||
|
@ -819,9 +806,9 @@ tr_bitfieldDifference( tr_bitfield * a, const tr_bitfield * b )
|
|||
uint8_t *ait;
|
||||
const uint8_t *aend, *bit;
|
||||
|
||||
assert( a->len == b->len );
|
||||
assert( a->bitCount == b->bitCount );
|
||||
|
||||
for( ait=a->bits, bit=b->bits, aend=ait+a->len; ait!=aend; )
|
||||
for( ait=a->bits, bit=b->bits, aend=ait+a->byteCount; ait!=aend; )
|
||||
*ait++ &= ~(*bit++);
|
||||
}
|
||||
|
||||
|
@ -853,7 +840,7 @@ tr_bitfieldCountTrueBits( const tr_bitfield* b )
|
|||
if( !b )
|
||||
return 0;
|
||||
|
||||
for( it=b->bits, end=it+b->len; it!=end; ++it )
|
||||
for( it=b->bits, end=it+b->byteCount; it!=end; ++it )
|
||||
ret += trueBitCount[*it];
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -239,7 +239,8 @@ int tr_httpParseURL( const char * url,
|
|||
struct tr_bitfield
|
||||
{
|
||||
uint8_t * bits;
|
||||
size_t len;
|
||||
size_t bitCount;
|
||||
size_t byteCount;
|
||||
};
|
||||
|
||||
typedef struct tr_bitfield tr_bitfield;
|
||||
|
@ -272,7 +273,7 @@ tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );
|
|||
|
||||
/** @param high the highest nth bit you're going to access */
|
||||
#define tr_bitfieldTestFast(bitfield,high) \
|
||||
((bitfield) && ( (bitfield)->bits ) && ( (high) <= (bitfield)->len*8 ))
|
||||
( (bitfield) && ((bitfield)->bits) && ((high)<(bitfield)->bitCount ) )
|
||||
|
||||
double tr_getRatio( double numerator, double denominator );
|
||||
|
||||
|
|
Loading…
Reference in a new issue