Ignore:
Timestamp:
Feb 23, 2011, 3:54:04 AM (11 years ago)
Author:
jordan
Message:

(trunk libT) #4048 "use bitsets instead of bitfield in tr_completion" -- done.

Excuse the sprawl. Much of this didn't fit into self-contained commits.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/torrent.c

    r11987 r12012  
    13091309    if( f->length )
    13101310    {
    1311         const tr_block_index_t firstBlock = f->offset / tor->blockSize;
    1312         const uint64_t lastByte = f->offset + f->length - 1;
    1313         const tr_block_index_t lastBlock = lastByte / tor->blockSize;
    1314 
    1315         if( firstBlock == lastBlock )
     1311        tr_block_index_t first;
     1312        tr_block_index_t last;
     1313        tr_torGetFileBlockRange( tor, index, &first, &last );
     1314
     1315        if( first == last )
    13161316        {
    1317             if( tr_cpBlockIsCompleteFast( &tor->completion, firstBlock ) )
     1317            if( tr_cpBlockIsComplete( &tor->completion, first ) )
    13181318                total = f->length;
    13191319        }
    13201320        else
    13211321        {
    1322             tr_block_index_t i;
    1323 
    13241322            /* the first block */
    1325             if( tr_cpBlockIsCompleteFast( &tor->completion, firstBlock ) )
     1323            if( tr_cpBlockIsComplete( &tor->completion, first ) )
    13261324                total += tor->blockSize - ( f->offset % tor->blockSize );
    13271325
    13281326            /* the middle blocks */
    1329             if( f->firstPiece == f->lastPiece )
    1330             {
    1331                 for( i=firstBlock+1; i<lastBlock; ++i )
    1332                     if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
    1333                         total += tor->blockSize;
     1327            if( first + 1 < last ) {
     1328                uint64_t u = tr_bitsetCountRange( tr_cpBlockBitset( &tor->completion ), first+1, last );
     1329                u *= tor->blockSize;
     1330                total += u;
    13341331            }
    1335             else
    1336             {
    1337                 uint64_t b = 0;
    1338                 const tr_block_index_t firstBlockOfLastPiece
    1339                            = tr_torPieceFirstBlock( tor, f->lastPiece );
    1340                 const tr_block_index_t lastBlockOfFirstPiece
    1341                            = tr_torPieceFirstBlock( tor, f->firstPiece )
    1342                              + tr_torPieceCountBlocks( tor, f->firstPiece ) - 1;
    1343 
    1344                 /* the rest of the first piece */
    1345                 for( i=firstBlock+1; i<lastBlock && i<=lastBlockOfFirstPiece; ++i )
    1346                     if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
    1347                         ++b;
    1348 
    1349                 /* the middle pieces */
    1350                 if( f->firstPiece + 1 < f->lastPiece )
    1351                     for( i=f->firstPiece+1; i<f->lastPiece; ++i )
    1352                         b += tor->blockCountInPiece - tr_cpMissingBlocksInPiece( &tor->completion, i );
    1353 
    1354                 /* the rest of the last piece */
    1355                 for( i=firstBlockOfLastPiece; i<lastBlock; ++i )
    1356                     if( tr_cpBlockIsCompleteFast( &tor->completion, i ) )
    1357                         ++b;
    1358 
    1359                 b *= tor->blockSize;
    1360                 total += b;
    1361             }
    13621332
    13631333            /* the last block */
    1364             if( tr_cpBlockIsCompleteFast( &tor->completion, lastBlock ) )
    1365                 total += ( f->offset + f->length ) - ( (uint64_t)tor->blockSize * lastBlock );
     1334            if( tr_cpBlockIsComplete( &tor->completion, last ) )
     1335                total += ( f->offset + f->length ) - ( (uint64_t)tor->blockSize * last );
    13661336        }
    13671337    }
     
    22832253***/
    22842254
     2255void
     2256tr_torrentGetBlockLocation( const tr_torrent * tor,
     2257                            tr_block_index_t   block,
     2258                            tr_piece_index_t * piece,
     2259                            uint32_t         * offset,
     2260                            uint32_t         * length )
     2261{
     2262    uint64_t pos = block;
     2263    pos *= tor->blockSize;
     2264    *piece = pos / tor->info.pieceSize;
     2265    *offset = pos - ( *piece * tor->info.pieceSize );
     2266    *length = tr_torBlockCountBytes( tor, block );
     2267}
     2268
     2269
    22852270tr_block_index_t
    22862271_tr_block( const tr_torrent * tor,
     
    23442329    return ret;
    23452330}
     2331
     2332void
     2333tr_torGetFileBlockRange( const tr_torrent        * tor,
     2334                         const tr_file_index_t     file,
     2335                         tr_block_index_t        * first,
     2336                         tr_block_index_t        * last )
     2337{
     2338    const tr_file * f = &tor->info.files[file];
     2339    uint64_t offset = f->offset;
     2340    *first = offset / tor->blockSize;
     2341    if( !f->length )
     2342        *last = *first;
     2343    else {
     2344        offset += f->length - 1;
     2345        *last = offset / tor->blockSize;
     2346    }
     2347}
     2348
     2349void
     2350tr_torGetPieceBlockRange( const tr_torrent        * tor,
     2351                          const tr_piece_index_t    piece,
     2352                          tr_block_index_t        * first,
     2353                          tr_block_index_t        * last )
     2354{
     2355    uint64_t offset = tor->info.pieceSize;
     2356    offset *= piece;
     2357    *first = offset / tor->blockSize;
     2358    offset += ( tr_torPieceCountBytes( tor, piece ) - 1 );
     2359    *last = offset / tor->blockSize;
     2360}
     2361
    23462362
    23472363/***
Note: See TracChangeset for help on using the changeset viewer.