Changeset 2488


Ignore:
Timestamp:
Jul 25, 2007, 1:59:46 AM (15 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
Location:
trunk/libtransmission
Files:
6 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;
  • trunk/libtransmission/internal.h

    r2480 r2488  
    122122void tr_torrentWriterUnlock  ( tr_torrent_t * );
    123123
    124 #define tr_blockPiece(a) _tr_blockPiece(tor,a)
    125 int _tr_blockPiece( const tr_torrent_t * tor, int block );
     124
     125#define TOR_BLOCK_PIECE(tor,block) \
     126    ( (block) / ( (tor)->info.pieceSize / (tor)->blockSize ) )
     127
     128#define TOR_PIECE_FIRST_BLOCK(tor,piece) \
     129    ( (tor)->info.pieces[(piece)].firstBlock )
     130
     131#define TR_BLOCKS_IN_PIECE(tor,piece) \
     132    ( (tor)->info.pieces[(piece)].blockCount )
    126133
    127134#define tr_blockSize(a) _tr_blockSize(tor,a)
    128135int _tr_blockSize( const tr_torrent_t * tor, int block );
    129 
    130 #define tr_pieceCountBlocks(a) _tr_pieceCountBlocks(tor,a)
    131 int _tr_pieceCountBlocks( const tr_torrent_t * tor, int piece );
    132 
    133 #define tr_pieceStartBlock(a) _tr_pieceStartBlock(tor,a)
    134 int _tr_pieceStartBlock( const tr_torrent_t * tor, int piece );
    135136
    136137#define tr_pieceSize(a) _tr_pieceSize(tor,a)
  • trunk/libtransmission/peer.c

    r2482 r2488  
    681681            {
    682682                const int piece = pool[p];
    683                 const int firstBlock = tr_pieceStartBlock( piece );
    684                 const int n = tr_pieceCountBlocks( piece );
     683                const int firstBlock = TOR_PIECE_FIRST_BLOCK( tor, piece );
     684                const int n = TR_BLOCKS_IN_PIECE( tor, piece );
    685685                const int end = firstBlock + n;
    686686                int block;
  • trunk/libtransmission/torrent.c

    r2481 r2488  
    169169
    170170    for( i=0; i<tor->info.pieceCount; ++i )
    171       tor->info.pieces[i].priority = calculatePiecePriority( tor, i );
     171    {
     172        tr_piece_t * piece = tor->info.pieces + i;
     173        uint64_t lastByte;
     174        int lastBlock;
     175        if( i == (tor->info.pieceCount-1))
     176            lastByte = tor->info.totalSize;
     177        else {
     178            lastByte = i + 1;
     179            lastByte *= tor->info.pieceSize;
     180            --lastByte;
     181        }
     182        lastBlock = lastByte / tor->blockSize;
     183
     184        piece->priority = calculatePiecePriority( tor, i );
     185        piece->firstBlock = i * (tor->info.pieceSize / tor->blockSize);
     186        piece->blockCount = lastBlock + 1 - piece->firstBlock;
     187
     188        assert( 0 <= piece->firstBlock );
     189        assert( piece->firstBlock <= lastBlock );
     190        assert( lastBlock < tor->blockCount );
     191        assert( piece->firstBlock + piece->blockCount <= tor->blockCount );
     192    }
    172193}
    173194
     
    190211
    191212    tor->destination = tr_strdup( destination );
    192 
    193     tr_torrentInitFilePieces( tor );
    194213
    195214    tor->handle   = h;
     
    213232                        tor->blockSize;
    214233    tor->completion = tr_cpInit( tor );
     234
     235    tr_torrentInitFilePieces( tor );
    215236
    216237    tor->thread = THREAD_EMPTY;
     
    686707    assert( (int)lastBlock < tor->blockCount );
    687708    assert( firstBlock <= lastBlock );
    688     assert( tr_blockPiece( firstBlock ) == file->firstPiece );
    689     assert( tr_blockPiece( lastBlock ) == file->lastPiece );
     709    assert( TOR_BLOCK_PIECE( tor, firstBlock ) == file->firstPiece );
     710    assert( TOR_BLOCK_PIECE( tor, lastBlock ) == file->lastPiece );
    690711
    691712    if( firstBlock == lastBlock )
     
    13681389***/
    13691390
    1370 int _tr_blockPiece( const tr_torrent_t * tor, int block )
    1371 {
    1372     return (block * tor->blockSize) / tor->info.pieceSize;
    1373 }
    1374 
    1375 int _tr_pieceStartBlock( const tr_torrent_t * tor, int piece )
    1376 {
    1377     return (piece * tor->info.pieceSize) / tor->blockSize;
    1378 }
    1379 
    13801391int _tr_blockSize( const tr_torrent_t * tor, int block )
    13811392{
     
    13901401
    13911402    return dummy;
    1392 }
    1393 
    1394 int _tr_pieceCountBlocks( const tr_torrent_t * tor, int piece )
    1395 {
    1396     const tr_info_t * inf = &tor->info;
    1397     if( piece < inf->pieceCount - 1 ||
    1398         !( tor->blockCount % ( inf->pieceSize / tor->blockSize ) ) )
    1399     {
    1400         return inf->pieceSize / tor->blockSize;
    1401     }
    1402     return tor->blockCount % ( inf->pieceSize / tor->blockSize );
    14031403}
    14041404
  • trunk/libtransmission/transmission.h

    r2474 r2488  
    515515    uint8_t  hash[SHA_DIGEST_LENGTH];  /* pieces hash */
    516516    int8_t   priority;                 /* TR_PRI_HIGH, _NORMAL, or _LOW */
    517     int8_t   dnd;                      /*  nonzero if the piece shouldn't be downloaded */
     517    int8_t   dnd;                      /* nonzero if the piece shouldn't be downloaded */
     518    int      firstBlock;               /* this piece's first byte is in this block */
     519    int      blockCount;               /* number of blocks this piece is in */
    518520}
    519521tr_piece_t;
  • trunk/libtransmission/utils.c

    r2485 r2488  
    575575}
    576576
    577 #define BIN(nth) (nth>>3)
     577#define BIN(nth) ((nth>>3))
    578578#define BIT(nth) (1<<(7-(nth%8)))
    579579
     
    588588tr_bitfieldAdd( tr_bitfield_t  * bitfield, size_t nth )
    589589{
     590    assert( bitfield != NULL );
     591    assert( 0 <= nth);
     592    assert( 0 <= BIN(nth) );
     593    assert( BIN(nth) < bitfield->len );
    590594    bitfield->bits[ BIN(nth) ] |= BIT(nth);
    591595}
Note: See TracChangeset for help on using the changeset viewer.