Ignore:
Timestamp:
Jun 30, 2007, 12:54:26 PM (14 years ago)
Author:
charles
Message:
  • make tr_cpLeftUntilDone() slightly more accurate
  • simplify completion code a little
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r2207 r2242  
    3333    tr_bitfield_t     * pieceBitfield;
    3434
    35     /* a block is missing if we don't have it AND there's not a request pending */
    36     int * missingBlocks;
    37 
    3835    /* a block is complete if and only if we have it */
    3936    int * completeBlocks;
     
    4542};
    4643
    47 #define tr_cpCountBlocks(cp,piece) \
    48     (piece==cp->tor->info.pieceCount-1 ? cp->nBlocksInLastPiece : cp->nBlocksInPiece)
     44#define tr_cpCountBlocks(cp,piece) (piece==cp->tor->info.pieceCount-1 \
     45    ? cp->nBlocksInLastPiece \
     46    : cp->nBlocksInPiece)
    4947
    5048tr_completion_t * tr_cpInit( tr_torrent_t * tor )
     
    5755    cp->blockDownloaders = tr_new( uint8_t, tor->blockCount );
    5856    cp->pieceBitfield    = tr_bitfieldNew( tor->info.pieceCount );
    59     cp->missingBlocks    = tr_new( int, tor->info.pieceCount );
    6057    cp->completeBlocks   = tr_new( int, tor->info.pieceCount );
    6158
     
    7269{
    7370    tr_free(         cp->completeBlocks );
    74     tr_free(         cp->missingBlocks );
    7571    tr_bitfieldFree( cp->pieceBitfield );
    7672    tr_free(         cp->blockDownloaders );
     
    8783    memset( cp->blockDownloaders, 0, tor->blockCount );
    8884    tr_bitfieldClear( cp->pieceBitfield );
    89     for( i = 0; i < tor->info.pieceCount; ++i ) {
    90         cp->missingBlocks[i] = tr_cpCountBlocks( cp, i );
     85    for( i = 0; i < tor->info.pieceCount; ++i )
    9186        cp->completeBlocks[i] = 0;
    92     }
    9387}
    9488
     
    113107void tr_cpPieceAdd( tr_completion_t * cp, int piece )
    114108{
    115     int i;
    116109    const tr_torrent_t * tor = cp->tor;
    117110    const int n_blocks = tr_cpCountBlocks( cp, piece );
     
    120113
    121114    cp->completeBlocks[piece] = n_blocks;
    122 
    123     for( i=startBlock; i<endBlock; ++i )
    124         if( !cp->blockDownloaders[i] )
    125             --cp->missingBlocks[piece];
    126 
    127115    tr_bitfieldAddRange( cp->blockBitfield, startBlock, endBlock-1 );
    128 
    129116    tr_bitfieldAdd( cp->pieceBitfield, piece );
    130117}
     
    132119void tr_cpPieceRem( tr_completion_t * cp, int piece )
    133120{
    134     int i;
    135121    const tr_torrent_t * tor = cp->tor;
    136122    const int n_blocks = tr_cpCountBlocks( cp, piece );
     
    139125
    140126    cp->completeBlocks[piece] = 0;
    141 
    142     for( i=startBlock; i<endBlock; ++i )
    143         if( !cp->blockDownloaders[i] )
    144             ++cp->missingBlocks[piece];
    145 
    146127    tr_bitfieldRemRange ( cp->blockBitfield, startBlock, endBlock-1 );
    147 
    148128    tr_bitfieldRem( cp->pieceBitfield, piece );
    149129}
     
    152132void tr_cpDownloaderAdd( tr_completion_t * cp, int block )
    153133{
    154     tr_torrent_t * tor = cp->tor;
    155     if( !cp->blockDownloaders[block] && !tr_cpBlockIsComplete( cp, block ) )
    156     {
    157         cp->missingBlocks[tr_blockPiece(block)]--;
    158     }
    159     (cp->blockDownloaders[block])++;
     134    ++cp->blockDownloaders[block];
    160135}
    161136
    162137void tr_cpDownloaderRem( tr_completion_t * cp, int block )
    163138{
    164     tr_torrent_t * tor;
    165 
    166     assert( cp != NULL );
    167     assert( cp->tor != NULL );
    168     assert( 0 <= block );
    169 
    170     tor = cp->tor;
    171     (cp->blockDownloaders[block])--;
    172     if( !cp->blockDownloaders[block] && !tr_cpBlockIsComplete( cp, block ) )
    173     {
    174         cp->missingBlocks[tr_blockPiece(block)]++;
    175     }
     139    --cp->blockDownloaders[block];
    176140}
    177141
    178142int tr_cpBlockIsComplete( const tr_completion_t * cp, int block )
    179143{
    180     assert( cp != NULL );
    181     assert( 0 <= block );
    182 
    183144    return tr_bitfieldHas( cp->blockBitfield, block );
    184145}
     
    202163            tr_bitfieldAdd( cp->pieceBitfield, piece );
    203164
    204         if( !cp->blockDownloaders[block] )
    205             cp->missingBlocks[piece]--;
    206 
    207165        tr_bitfieldAdd( cp->blockBitfield, block );
    208166    }
     
    236194
    237195    return cp->completeBlocks[piece] / (double)tr_cpCountBlocks( cp, piece );
     196}
     197
     198int
     199tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )
     200{
     201    int i;
     202    int n;
     203    const tr_torrent_t * tor = cp->tor;
     204    const int start = tr_pieceStartBlock( piece );
     205    const int end   = start + tr_cpCountBlocks( cp, piece );
     206
     207    n = 0;
     208    for( i = start; i < end; ++i )
     209        if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
     210            ++n;
     211
     212    return n;
    238213}
    239214
     
    300275
    301276
    302 int
    303 tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )
    304 {
    305     assert( cp != NULL );
    306 
    307     return cp->missingBlocks[piece];
    308 }
    309 
    310277/***
    311278****
     
    370337    for( i=0; i<info->pieceCount; ++i )
    371338        if( !tr_cpPieceIsComplete( cp, i ) && info->pieces[i].priority != TR_PRI_DND )
    372             b += tor->blockSize * (tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
    373 
    374     return b;
     339            b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
     340
     341    return b * tor->blockSize;
    375342}
    376343
Note: See TracChangeset for help on using the changeset viewer.