Ignore:
Timestamp:
Jul 25, 2007, 1:59:46 AM (14 years ago)
Author:
charles
Message:
  • fix overflow error from earlier today. (this was the crash talked about in IRC)
  • fix long-standing error that occurred when pieceSize isn't evenly divisible by blockSize
  • speed up and macro-ize piece-to-block conversions
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r2485 r2488  
    4141    /* a block is complete if and only if we have it */
    4242    int * completeBlocks;
    43 
    44     /* rather than calculating these over and over again in loops,
    45        just calculate them once */
    46     int nBlocksInPiece;
    47     int nBlocksInLastPiece;
    4843};
    49 
    50 #define tr_cpCountBlocks(cp,piece) (piece==cp->tor->info.pieceCount-1 \
    51     ? cp->nBlocksInLastPiece \
    52     : cp->nBlocksInPiece)
    5344
    5445tr_completion_t * tr_cpInit( tr_torrent_t * tor )
     
    6354    cp->completeBlocks   = tr_new( int, tor->info.pieceCount );
    6455
    65     cp->nBlocksInLastPiece = tr_pieceCountBlocks( tor->info.pieceCount - 1 );
    66     cp->nBlocksInPiece = tor->info.pieceCount==1 ? cp->nBlocksInLastPiece
    67                                                  : tr_pieceCountBlocks( 0 );
    68 
    6956    tr_cpReset( cp );
    7057
     
    10087int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece )
    10188{
    102     return cp->completeBlocks[piece] >= tr_cpCountBlocks(cp,piece);
     89    return cp->completeBlocks[piece] >= TR_BLOCKS_IN_PIECE(cp->tor,piece);
    10390}
    10491
     
    11198{
    11299    const tr_torrent_t * tor = cp->tor;
    113     const int n_blocks = tr_cpCountBlocks( cp, piece );
    114     const int startBlock = tr_pieceStartBlock( piece );
     100    const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece);
     101    const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece);
    115102    const int endBlock   = startBlock + n_blocks;
    116103
     
    123110{
    124111    const tr_torrent_t * tor = cp->tor;
    125     const int n_blocks = tr_cpCountBlocks( cp, piece );
    126     const int startBlock = tr_pieceStartBlock( piece );
     112    const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece);
     113    const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece);
    127114    const int endBlock   = startBlock + n_blocks;
    128115
     
    133120    assert( startBlock < tor->blockCount );
    134121    assert( startBlock <= endBlock );
    135     assert( endBlock < tor->blockCount );
     122    assert( endBlock <= tor->blockCount );
    136123
    137124    cp->completeBlocks[piece] = 0;
     
    158145void tr_cpBlockAdd( tr_completion_t * cp, int block )
    159146{
    160     const tr_torrent_t * tor;
    161 
    162     assert( cp != NULL );
    163     assert( cp->tor != NULL );
    164     assert( 0 <= block );
    165 
    166     tor = cp->tor;
     147    const tr_torrent_t * tor = cp->tor;
    167148
    168149    if( !tr_cpBlockIsComplete( cp, block ) )
    169150    {
    170         const int piece = block / cp->nBlocksInPiece;
     151        const int piece = TOR_BLOCK_PIECE(tor, block);
    171152
    172153        ++cp->completeBlocks[piece];
    173154
    174         if( cp->completeBlocks[piece] == tr_cpCountBlocks( cp, piece ) )
     155        if( cp->completeBlocks[piece] == TR_BLOCKS_IN_PIECE(tor,piece) )
    175156            tr_bitfieldAdd( cp->pieceBitfield, piece );
    176157
     
    205186    assert( cp != NULL );
    206187
    207     return cp->completeBlocks[piece] / (double)tr_cpCountBlocks( cp, piece );
     188    return cp->completeBlocks[piece] / (double)TR_BLOCKS_IN_PIECE(cp->tor,piece);
    208189}
    209190
     
    214195    int n;
    215196    const tr_torrent_t * tor = cp->tor;
    216     const int start = tr_pieceStartBlock( piece );
    217     const int end   = start + tr_cpCountBlocks( cp, piece );
     197    const int start = TOR_PIECE_FIRST_BLOCK(tor,piece);
     198    const int end   = start + TR_BLOCKS_IN_PIECE(tor,piece);
    218199
    219200    n = 0;
     
    229210    int i;
    230211    const tr_torrent_t * tor = cp->tor;
    231     const int start = tr_pieceStartBlock( piece );
    232     const int end   = start + tr_cpCountBlocks( cp, piece );
     212    const int start = TOR_PIECE_FIRST_BLOCK(tor,piece);
     213    const int end   = start + TR_BLOCKS_IN_PIECE(tor,piece);
    233214
    234215    for( i = start; i < end; ++i )
     
    280261    for( i=0; i<info->pieceCount; ++i )
    281262        if( !tr_cpPieceIsComplete( cp, i ) )
    282             b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
     263            b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] );
    283264
    284265    b *= tor->blockSize;
     
    306287    for( i=0; i<info->pieceCount; ++i )
    307288        if( !tr_cpPieceIsComplete( cp, i ) && !info->pieces[i].dnd )
    308             b += ( tr_cpCountBlocks( cp, i ) - cp->completeBlocks[ i ] );
     289            b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] );
    309290
    310291    b *= tor->blockSize;
Note: See TracChangeset for help on using the changeset viewer.