Changeset 7647


Ignore:
Timestamp:
Jan 9, 2009, 5:55:48 PM (12 years ago)
Author:
charles
Message:

(trunk libT) annotate tr_cpBlockBitfieldSet() as suggested by wereHamster.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/completion.c

    r7633 r7647  
    165165}
    166166
     167/* Initialize a completion object from a bitfield indicating which blocks we have */
    167168tr_bool
    168169tr_cpBlockBitfieldSet( tr_completion * cp, tr_bitfield * blockBitfield )
     
    173174    assert( blockBitfield );
    174175
     176    /* The bitfield of block flags is typically loaded from a resume file.
     177       Test the bitfield's length in case the resume file somehow got corrupted */
    175178    if(( success = blockBitfield->byteCount == cp->blockBitfield.byteCount ))
    176179    {
     
    182185        uint32_t blocksInCurrentPiece = tr_torPieceCountBlocks( cp->tor, p );
    183186
    184         assert( blockBitfield->byteCount == cp->blockBitfield.byteCount );
    185 
     187        /* start cp with a state where it thinks we have nothing */
    186188        tr_cpReset( cp );
    187189
     190        /* init our block bitfield from the one passed in */
     191        memcpy( cp->blockBitfield.bits, blockBitfield->bits, blockBitfield->byteCount );
     192
     193        /* invalidate the fields that are lazy-evaluated */
    188194        cp->sizeWhenDoneIsDirty = TRUE;
    189195        cp->haveValidIsDirty = TRUE;
    190         memcpy( cp->blockBitfield.bits, blockBitfield->bits, blockBitfield->byteCount );
    191 
     196
     197        /* to set the remaining fields, we walk through every block... */
    192198        while( b < cp->tor->blockCount )
    193199        {
     
    198204            ++pieceBlock;
    199205
     206            /* by the time we reach the end of a piece, we have enough info
     207               to update that piece's slot in cp.completeBlocks and cp.pieceBitfield */
    200208            if( pieceBlock == blocksInCurrentPiece )
    201209            {
    202210                cp->completeBlocks[p] = completeBlocksInPiece;
    203 
    204211                completeBlocksInTorrent += completeBlocksInPiece;
    205 
    206212                if( completeBlocksInPiece == blocksInCurrentPiece )
    207213                    tr_bitfieldAdd( &cp->pieceBitfield, p );
    208214
     215                /* reset the per-piece counters because we're starting on a new piece now */
    209216                ++p;
    210217                completeBlocksInPiece = 0;
     
    218225        cp->sizeNow *= tr_torBlockCountBytes( cp->tor, 0 );
    219226        if( tr_bitfieldHasFast( &cp->blockBitfield, cp->tor->blockCount-1 ) ) {
     227            /* the last block is usually smaller than the other blocks,
     228               so handle that special case or cp->sizeNow might be too large */
    220229            cp->sizeNow -= tr_torBlockCountBytes( cp->tor, 0 );
    221230            cp->sizeNow += tr_torBlockCountBytes( cp->tor, cp->tor->blockCount-1 );
    222231        }
    223 
    224 #if 1
    225 #warning these checks are to see if the implementation is good, since getting this function wrong could make Transmission think their downloaded data has disappeared.  But they are also expensive, so this block should be turned off after the nightly build users had a chance to smoke out any errors.
    226         /**
    227         ***  correctness checks
    228         **/
    229         for( b=0; b<cp->tor->blockCount; ++b )
    230             assert( tr_bitfieldHasFast( blockBitfield, b ) == tr_bitfieldHasFast( &cp->blockBitfield, b ) );
    231 
    232         assert( cp->sizeNow <= cp->tor->info.totalSize );
    233         for( p=0; p<cp->tor->info.pieceCount; ++p ) {
    234             const uint32_t blocksInCurrentPiece = tr_torPieceCountBlocks( cp->tor, p );
    235             const tr_block_index_t start = tr_torPieceFirstBlock( cp->tor, p );
    236             const tr_block_index_t end = start + tr_torPieceCountBlocks( cp->tor, p );
    237             uint32_t completeBlocksInPiece = 0;
    238 
    239             assert( tr_bitfieldHasFast( &cp->pieceBitfield, p ) == ( blocksInCurrentPiece == cp->completeBlocks[p] ) );
    240 
    241             for( b=start; b<end; ++b )
    242                 if( tr_bitfieldHasFast( &cp->blockBitfield, b ) )
    243                     ++completeBlocksInPiece;
    244             assert( completeBlocksInPiece == cp->completeBlocks[p] );
    245         }
    246 #endif
    247232    }
    248233
Note: See TracChangeset for help on using the changeset viewer.