1
/******************************************************************************
2
* $Id: completion.c 261 2006-05-29 21:27:31Z titer $
4
* Copyright (c) 2005 Transmission authors and contributors
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the "Software"),
8
* to deal in the Software without restriction, including without limitation
9
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
* and/or sell copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following conditions:
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
* DEALINGS IN THE SOFTWARE.
23
*****************************************************************************/
25
#include "transmission.h"
27
tr_completion_t * tr_cpInit( tr_torrent_t * tor )
31
cp = malloc( sizeof( tr_completion_t ) );
33
cp->blockBitfield = malloc( ( tor->blockCount + 7 ) / 8 );
34
cp->blockDownloaders = malloc( tor->blockCount );
35
cp->pieceBitfield = malloc( ( tor->info.pieceCount + 7 ) / 8 );
36
cp->missingBlocks = malloc( tor->info.pieceCount * sizeof( int ) );
43
void tr_cpClose( tr_completion_t * cp )
45
free( cp->blockBitfield );
46
free( cp->blockDownloaders );
47
free( cp->pieceBitfield );
48
free( cp->missingBlocks );
52
void tr_cpReset( tr_completion_t * cp )
54
tr_torrent_t * tor = cp->tor;
58
memset( cp->blockBitfield, 0, ( tor->blockCount + 7 ) / 8 );
59
memset( cp->blockDownloaders, 0, tor->blockCount );
60
memset( cp->pieceBitfield, 0, ( tor->info.pieceCount + 7 ) / 8 );
61
for( i = 0; i < tor->info.pieceCount; i++ )
63
cp->missingBlocks[i] = tr_pieceCountBlocks( i );
67
float tr_cpCompletionAsFloat( tr_completion_t * cp )
69
return (float) cp->blockCount / (float) cp->tor->blockCount;
72
uint64_t tr_cpLeftBytes( tr_completion_t * cp )
74
tr_torrent_t * tor = cp->tor;
76
left = (uint64_t) ( cp->tor->blockCount - cp->blockCount ) *
77
(uint64_t) tor->blockSize;
78
if( !tr_bitfieldHas( cp->blockBitfield, cp->tor->blockCount - 1 ) &&
79
tor->info.totalSize % tor->blockSize )
81
left += tor->info.totalSize % tor->blockSize;
82
left -= tor->blockSize;
88
int tr_cpPieceIsComplete( tr_completion_t * cp, int piece )
90
return tr_bitfieldHas( cp->pieceBitfield, piece );
93
uint8_t * tr_cpPieceBitfield( tr_completion_t * cp )
95
return cp->pieceBitfield;
98
void tr_cpPieceAdd( tr_completion_t * cp, int piece )
100
tr_torrent_t * tor = cp->tor;
101
int startBlock, endBlock, i;
103
startBlock = tr_pieceStartBlock( piece );
104
endBlock = startBlock + tr_pieceCountBlocks( piece );
105
for( i = startBlock; i < endBlock; i++ )
107
tr_cpBlockAdd( cp, i );
110
tr_bitfieldAdd( cp->pieceBitfield, piece );
114
void tr_cpDownloaderAdd( tr_completion_t * cp, int block )
116
tr_torrent_t * tor = cp->tor;
117
if( !cp->blockDownloaders[block] && !tr_cpBlockIsComplete( cp, block ) )
119
cp->missingBlocks[tr_blockPiece(block)]--;
121
(cp->blockDownloaders[block])++;
124
void tr_cpDownloaderRem( tr_completion_t * cp, int block )
126
tr_torrent_t * tor = cp->tor;
127
(cp->blockDownloaders[block])--;
128
if( !cp->blockDownloaders[block] && !tr_cpBlockIsComplete( cp, block ) )
130
cp->missingBlocks[tr_blockPiece(block)]++;
134
int tr_cpBlockIsComplete( tr_completion_t * cp, int block )
136
return tr_bitfieldHas( cp->blockBitfield, block );
139
void tr_cpBlockAdd( tr_completion_t * cp, int block )
141
tr_torrent_t * tor = cp->tor;
142
if( !tr_cpBlockIsComplete( cp, block ) )
145
if( !cp->blockDownloaders[block] )
147
(cp->missingBlocks[tr_blockPiece(block)])--;
150
tr_bitfieldAdd( cp->blockBitfield, block );
153
void tr_cpBlockRem( tr_completion_t * cp, int block )
155
tr_torrent_t * tor = cp->tor;
156
if( tr_cpBlockIsComplete( cp, block ) )
159
if( !cp->blockDownloaders[block] )
161
(cp->missingBlocks[tr_blockPiece(block)])++;
164
tr_bitfieldRem( cp->blockBitfield, block );
167
uint8_t * tr_cpBlockBitfield( tr_completion_t * cp )
169
return cp->blockBitfield;
172
void tr_cpBlockBitfieldSet( tr_completion_t * cp, uint8_t * bitfield )
174
tr_torrent_t * tor = cp->tor;
176
int startBlock, endBlock;
179
for( i = 0; i < cp->tor->info.pieceCount; i++ )
181
startBlock = tr_pieceStartBlock( i );
182
endBlock = startBlock + tr_pieceCountBlocks( i );
185
for( j = startBlock; j < endBlock; j++ )
187
if( tr_bitfieldHas( bitfield, j ) )
189
tr_cpBlockAdd( cp, j );
198
tr_cpPieceAdd( cp, i );
203
int tr_cpMissingBlockInPiece( tr_completion_t * cp, int piece )
205
tr_torrent_t * tor = cp->tor;
206
int start, count, end, i;
208
start = tr_pieceStartBlock( piece );
209
count = tr_pieceCountBlocks( piece );
212
for( i = start; i < end; i++ )
214
if( tr_cpBlockIsComplete( cp, i ) || cp->blockDownloaders[i] )
224
int tr_cpMostMissingBlockInPiece( tr_completion_t * cp, int piece,
227
tr_torrent_t * tor = cp->tor;
228
int start, count, end, i;
229
int * pool, poolSize, min, ret;
231
start = tr_pieceStartBlock( piece );
232
count = tr_pieceCountBlocks( piece );
235
pool = malloc( count * sizeof( int ) );
239
for( i = start; i < end; i++ )
241
if( tr_cpBlockIsComplete( cp, i ) || cp->blockDownloaders[i] > min )
245
if( cp->blockDownloaders[i] < min )
247
min = cp->blockDownloaders[i];
250
if( cp->blockDownloaders[i] <= min )
252
pool[poolSize++] = i;