Changeset 2242 for trunk/libtransmission/completion.c
- Timestamp:
- Jun 30, 2007, 12:54:26 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/completion.c
r2207 r2242 33 33 tr_bitfield_t * pieceBitfield; 34 34 35 /* a block is missing if we don't have it AND there's not a request pending */36 int * missingBlocks;37 38 35 /* a block is complete if and only if we have it */ 39 36 int * completeBlocks; … … 45 42 }; 46 43 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) 49 47 50 48 tr_completion_t * tr_cpInit( tr_torrent_t * tor ) … … 57 55 cp->blockDownloaders = tr_new( uint8_t, tor->blockCount ); 58 56 cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount ); 59 cp->missingBlocks = tr_new( int, tor->info.pieceCount );60 57 cp->completeBlocks = tr_new( int, tor->info.pieceCount ); 61 58 … … 72 69 { 73 70 tr_free( cp->completeBlocks ); 74 tr_free( cp->missingBlocks );75 71 tr_bitfieldFree( cp->pieceBitfield ); 76 72 tr_free( cp->blockDownloaders ); … … 87 83 memset( cp->blockDownloaders, 0, tor->blockCount ); 88 84 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 ) 91 86 cp->completeBlocks[i] = 0; 92 }93 87 } 94 88 … … 113 107 void tr_cpPieceAdd( tr_completion_t * cp, int piece ) 114 108 { 115 int i;116 109 const tr_torrent_t * tor = cp->tor; 117 110 const int n_blocks = tr_cpCountBlocks( cp, piece ); … … 120 113 121 114 cp->completeBlocks[piece] = n_blocks; 122 123 for( i=startBlock; i<endBlock; ++i )124 if( !cp->blockDownloaders[i] )125 --cp->missingBlocks[piece];126 127 115 tr_bitfieldAddRange( cp->blockBitfield, startBlock, endBlock-1 ); 128 129 116 tr_bitfieldAdd( cp->pieceBitfield, piece ); 130 117 } … … 132 119 void tr_cpPieceRem( tr_completion_t * cp, int piece ) 133 120 { 134 int i;135 121 const tr_torrent_t * tor = cp->tor; 136 122 const int n_blocks = tr_cpCountBlocks( cp, piece ); … … 139 125 140 126 cp->completeBlocks[piece] = 0; 141 142 for( i=startBlock; i<endBlock; ++i )143 if( !cp->blockDownloaders[i] )144 ++cp->missingBlocks[piece];145 146 127 tr_bitfieldRemRange ( cp->blockBitfield, startBlock, endBlock-1 ); 147 148 128 tr_bitfieldRem( cp->pieceBitfield, piece ); 149 129 } … … 152 132 void tr_cpDownloaderAdd( tr_completion_t * cp, int block ) 153 133 { 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]; 160 135 } 161 136 162 137 void tr_cpDownloaderRem( tr_completion_t * cp, int block ) 163 138 { 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]; 176 140 } 177 141 178 142 int tr_cpBlockIsComplete( const tr_completion_t * cp, int block ) 179 143 { 180 assert( cp != NULL );181 assert( 0 <= block );182 183 144 return tr_bitfieldHas( cp->blockBitfield, block ); 184 145 } … … 202 163 tr_bitfieldAdd( cp->pieceBitfield, piece ); 203 164 204 if( !cp->blockDownloaders[block] )205 cp->missingBlocks[piece]--;206 207 165 tr_bitfieldAdd( cp->blockBitfield, block ); 208 166 } … … 236 194 237 195 return cp->completeBlocks[piece] / (double)tr_cpCountBlocks( cp, piece ); 196 } 197 198 int 199 tr_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; 238 213 } 239 214 … … 300 275 301 276 302 int303 tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )304 {305 assert( cp != NULL );306 307 return cp->missingBlocks[piece];308 }309 310 277 /*** 311 278 **** … … 370 337 for( i=0; i<info->pieceCount; ++i ) 371 338 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; 375 342 } 376 343
Note: See TracChangeset
for help on using the changeset viewer.