Changeset 6071


Ignore:
Timestamp:
Jun 7, 2008, 1:44:54 AM (13 years ago)
Author:
charles
Message:

minor refactoring of tr_bitfield to (a) simplify the tests and (b) make things easier to read

Location:
trunk/libtransmission
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r6049 r6071  
    215215    assert( cp->blockBitfield );
    216216    assert( cp->blockBitfield->bits );
    217     assert( cp->blockBitfield->len );
     217    assert( cp->blockBitfield->bitCount );
    218218
    219219    return cp->blockBitfield;
     
    229229    assert( cp->blockBitfield );
    230230
    231     if( !cp || !bitfield || ( bitfield->len != cp->blockBitfield->len ) )
     231    if( !cp || !bitfield || ( bitfield->byteCount != cp->blockBitfield->byteCount ) )
    232232        return TR_ERROR_ASSERT;
    233233
  • trunk/libtransmission/fastresume.c

    r5843 r6071  
    430430        /* get the completion bitfield */
    431431        memset( &bitfield, 0, sizeof bitfield );
    432         bitfield.len = FR_BLOCK_BITFIELD_LEN( tor );
     432        bitfield.byteCount = FR_BLOCK_BITFIELD_LEN( tor );
    433433        bitfield.bits = (uint8_t*) walk;
    434434        if( !tr_cpBlockBitfieldSet( tor->completion, &bitfield ) )
  • trunk/libtransmission/peer-mgr.c

    r6058 r6071  
    663663        tr_block_index_t block;
    664664
    665         assert( tr_bitfieldTestFast( t->requested, end ) );
     665        assert( tr_bitfieldTestFast( t->requested, end-1 ) );
    666666
    667667        for( block=begin; block<end; ++block )
  • trunk/libtransmission/peer-msgs.c

    r5980 r6071  
    503503        return TRUE;
    504504
    505     assert( bitfield->len == msgs->info->have->len );
     505    assert( bitfield->byteCount == msgs->info->have->byteCount );
     506
    506507    for( i=0; i<torrent->info.pieceCount; ++i )
    507508        if( isPieceInteresting( msgs, i ) )
     
    17551756
    17561757    dbgmsg( msgs, "sending peer a bitfield message" );
    1757     tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + bitfield->len );
     1758    tr_peerIoWriteUint32( msgs->io, out, sizeof(uint8_t) + bitfield->byteCount );
    17581759    tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD );
    1759     tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->len );
     1760    tr_peerIoWriteBytes ( msgs->io, out, bitfield->bits, bitfield->byteCount );
    17601761    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    17611762}
  • trunk/libtransmission/resume.c

    r6016 r6071  
    270270    bitfield = tr_cpBlockBitfield( tor->completion );
    271271    tr_bencDictAddRaw( p, KEY_PROGRESS_BITFIELD,
    272                        bitfield->bits, bitfield->len );
     272                       bitfield->bits, bitfield->byteCount );
    273273
    274274    /* cleanup */
     
    320320        {
    321321            tr_bitfield tmp;
    322             tmp.len = b->val.s.i;
     322            tmp.byteCount = b->val.s.i;
    323323            tmp.bits = (uint8_t*) b->val.s.s;
    324324            if( tr_cpBlockBitfieldSet( tor->completion, &tmp ) ) {
  • trunk/libtransmission/utils.c

    r6070 r6071  
    678678****/
    679679
    680 /* note that the argument is how many bits are needed, not bytes */
    681680tr_bitfield*
    682 tr_bitfieldNew( size_t bitcount )
    683 {
    684     tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) );
    685     if( NULL == ret )
    686         return NULL;
    687 
    688     ret->len = (bitcount+7u) / 8u;
    689     ret->bits = calloc( ret->len, 1 );
    690     if( NULL == ret->bits ) {
    691         free( ret );
    692         return NULL;
    693     }
    694 
     681tr_bitfieldNew( size_t bitCount )
     682{
     683    tr_bitfield * ret = tr_new0( tr_bitfield, 1 );
     684    ret->bitCount = bitCount;
     685    ret->byteCount = (bitCount+7u) / 8u;
     686    ret->bits = tr_new0( uint8_t, ret->byteCount );
    695687    return ret;
    696688}
     
    700692{
    701693    tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) );
    702     ret->len = in->len;
    703     ret->bits = tr_memdup( in->bits, in->len );
     694    ret->bitCount = in->bitCount;
     695    ret->byteCount = in->byteCount;
     696    ret->bits = tr_memdup( in->bits, in->byteCount );
    704697    return ret;
    705698}
     
    710703    if( bitfield )
    711704    {
    712         free( bitfield->bits );
    713         free( bitfield );
     705        tr_free( bitfield->bits );
     706        tr_free( bitfield );
    714707    }
    715708}
     
    718711tr_bitfieldClear( tr_bitfield * bitfield )
    719712{
    720     memset( bitfield->bits, 0, bitfield->len );
     713    memset( bitfield->bits, 0, bitfield->byteCount );
    721714}
    722715
     
    726719    size_t i;
    727720
    728     for( i=0; i<bitfield->len; ++i )
     721    for( i=0; i<bitfield->byteCount; ++i )
    729722        if( bitfield->bits[i] )
    730723            return 0;
     
    743736tr_bitfieldAdd( tr_bitfield  * bitfield, size_t nth )
    744737{
    745     const size_t i = nth >> 3u;
    746 
    747738    assert( bitfield != NULL );
    748739    assert( bitfield->bits != NULL );
    749740
    750     if( i >= bitfield->len )
     741    if( nth >= bitfield->bitCount )
    751742        return -1;
    752743
    753     bitfield->bits[i] |= (0x80 >> (nth&7u));
    754     /*assert( tr_bitfieldHas( bitfield, nth ) );*/
     744    bitfield->bits[nth>>3u] |= (0x80 >> (nth&7u));
    755745    return 0;
    756746}
     
    773763                size_t          nth )
    774764{
    775     const size_t i = nth >> 3u;
    776 
    777765    assert( bitfield != NULL );
    778766    assert( bitfield->bits != NULL );
    779767
    780     if( i >= bitfield->len )
     768    if( nth >= bitfield->bitCount )
    781769        return -1;
    782770
    783     bitfield->bits[i] &= (0xff7f >> (nth&7u));
    784     /*assert( !tr_bitfieldHas( bitfield, nth ) );*/
     771    bitfield->bits[nth>>3u] &= (0xff7f >> (nth&7u));
    785772    return 0;
    786773}
     
    805792    const uint8_t *aend, *bit;
    806793
    807     assert( a->len == b->len );
    808 
    809     for( ait=a->bits, bit=b->bits, aend=ait+a->len; ait!=aend; )
     794    assert( a->bitCount == b->bitCount );
     795
     796    for( ait=a->bits, bit=b->bits, aend=ait+a->byteCount; ait!=aend; )
    810797        *ait++ |= *bit++;
    811798
     
    820807    const uint8_t *aend, *bit;
    821808
    822     assert( a->len == b->len );
    823 
    824     for( ait=a->bits, bit=b->bits, aend=ait+a->len; ait!=aend; )
     809    assert( a->bitCount == b->bitCount );
     810
     811    for( ait=a->bits, bit=b->bits, aend=ait+a->byteCount; ait!=aend; )
    825812        *ait++ &= ~(*bit++);
    826813}
     
    854841        return 0;
    855842
    856     for( it=b->bits, end=it+b->len; it!=end; ++it )
     843    for( it=b->bits, end=it+b->byteCount; it!=end; ++it )
    857844        ret += trueBitCount[*it];
    858845
  • trunk/libtransmission/utils.h

    r6069 r6071  
    240240{
    241241    uint8_t * bits;
    242     size_t len;
     242    size_t bitCount;
     243    size_t byteCount;
    243244};
    244245
     
    273274/** @param high the highest nth bit you're going to access */
    274275#define tr_bitfieldTestFast(bitfield,high) \
    275     ((bitfield) && ( (bitfield)->bits ) && ( (high) <= (bitfield)->len*8 ))
     276    ( (bitfield) && ((bitfield)->bits) && ((high)<(bitfield)->bitCount ) )
    276277
    277278double tr_getRatio( double numerator, double denominator );
Note: See TracChangeset for help on using the changeset viewer.