source: trunk/libtransmission/completion.c @ 2518

Last change on this file since 2518 was 2518, checked in by charles, 14 years ago

save a little bit of memory per-torrent.

  • Property svn:keywords set to Date Rev Author Id
File size: 8.9 KB
Line 
1/******************************************************************************
2 * $Id: completion.c 2518 2007-07-27 23:07:26Z charles $
3 *
4 * Copyright (c) 2005 Transmission authors and contributors
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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 *****************************************************************************/
24
25#include "transmission.h"
26#include "completion.h"
27
28struct tr_completion_s
29{
30    tr_torrent_t * tor;
31
32    /* number of peers from whom we've requested this block */
33    uint8_t * blockDownloaders;
34
35    /* do we have this block? */
36    tr_bitfield_t * blockBitfield;
37
38    /* do we have this piece? */
39    tr_bitfield_t * pieceBitfield;
40
41    /* a block is complete if and only if we have it */
42    uint16_t * completeBlocks;
43};
44
45tr_completion_t * tr_cpInit( tr_torrent_t * tor )
46{
47    tr_completion_t * cp;
48
49    cp                   = tr_new( tr_completion_t, 1 );
50    cp->tor              = tor;
51    cp->blockBitfield    = tr_bitfieldNew( tor->blockCount );
52    cp->blockDownloaders = tr_new( uint8_t, tor->blockCount );
53    cp->pieceBitfield    = tr_bitfieldNew( tor->info.pieceCount );
54    cp->completeBlocks   = tr_new( uint16_t, tor->info.pieceCount );
55
56    tr_cpReset( cp );
57
58    return cp;
59}
60
61void tr_cpClose( tr_completion_t * cp )
62{
63    tr_free(         cp->completeBlocks );
64    tr_bitfieldFree( cp->pieceBitfield );
65    tr_free(         cp->blockDownloaders );
66    tr_bitfieldFree( cp->blockBitfield );
67    tr_free(         cp );
68}
69
70void tr_cpReset( tr_completion_t * cp )
71{
72    tr_torrent_t * tor = cp->tor;
73
74    tr_bitfieldClear( cp->blockBitfield );
75    memset( cp->blockDownloaders, 0, tor->blockCount );
76    tr_bitfieldClear( cp->pieceBitfield );
77    memset( cp->completeBlocks, 0, sizeof(uint16_t) * tor->info.pieceCount );
78}
79
80int tr_cpPieceHasAllBlocks( const tr_completion_t * cp, int piece )
81{
82    return tr_cpPieceIsComplete( cp, piece );
83}
84
85int tr_cpPieceIsComplete( const tr_completion_t * cp, int piece )
86{
87    return cp->completeBlocks[piece] >= TR_BLOCKS_IN_PIECE(cp->tor,piece);
88}
89
90const tr_bitfield_t * tr_cpPieceBitfield( const tr_completion_t * cp )
91{
92    return cp->pieceBitfield;
93}
94
95void tr_cpPieceAdd( tr_completion_t * cp, int piece )
96{
97    const tr_torrent_t * tor = cp->tor;
98    const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece);
99    const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece);
100    const int endBlock   = startBlock + n_blocks;
101
102    cp->completeBlocks[piece] = n_blocks;
103    tr_bitfieldAddRange( cp->blockBitfield, startBlock, endBlock );
104    tr_bitfieldAdd( cp->pieceBitfield, piece );
105}
106
107void tr_cpPieceRem( tr_completion_t * cp, int piece )
108{
109    const tr_torrent_t * tor = cp->tor;
110    const int n_blocks = TR_BLOCKS_IN_PIECE(tor,piece);
111    const int startBlock = TOR_PIECE_FIRST_BLOCK(tor,piece);
112    const int endBlock   = startBlock + n_blocks;
113
114    assert( cp != NULL );
115    assert( 0 <= piece );
116    assert( piece < tor->info.pieceCount );
117    assert( 0 <= startBlock );
118    assert( startBlock < tor->blockCount );
119    assert( startBlock <= endBlock );
120    assert( endBlock <= tor->blockCount );
121
122    cp->completeBlocks[piece] = 0;
123    tr_bitfieldRemRange ( cp->blockBitfield, startBlock, endBlock );
124    tr_bitfieldRem( cp->pieceBitfield, piece );
125}
126
127/* Blocks */
128void tr_cpDownloaderAdd( tr_completion_t * cp, int block )
129{
130    ++cp->blockDownloaders[block];
131}
132
133void tr_cpDownloaderRem( tr_completion_t * cp, int block )
134{
135    --cp->blockDownloaders[block];
136}
137
138int tr_cpBlockIsComplete( const tr_completion_t * cp, int block )
139{
140    return tr_bitfieldHas( cp->blockBitfield, block );
141}
142
143void tr_cpBlockAdd( tr_completion_t * cp, int block )
144{
145    const tr_torrent_t * tor = cp->tor;
146
147    if( !tr_cpBlockIsComplete( cp, block ) )
148    {
149        const int piece = TOR_BLOCK_PIECE(tor, block);
150
151        ++cp->completeBlocks[piece];
152
153        if( cp->completeBlocks[piece] == TR_BLOCKS_IN_PIECE(tor,piece) )
154            tr_bitfieldAdd( cp->pieceBitfield, piece );
155
156        tr_bitfieldAdd( cp->blockBitfield, block );
157    }
158}
159
160const tr_bitfield_t * tr_cpBlockBitfield( const tr_completion_t * cp )
161{
162    assert( cp != NULL );
163
164    return cp->blockBitfield;
165}
166
167void
168tr_cpBlockBitfieldSet( tr_completion_t * cp, tr_bitfield_t * bitfield )
169{
170    int i;
171
172    assert( cp != NULL );
173    assert( bitfield != NULL );
174
175    tr_cpReset( cp );
176
177    for( i=0; i < cp->tor->blockCount; ++i )
178        if( tr_bitfieldHas( bitfield, i ) )
179            tr_cpBlockAdd( cp, i );
180}
181
182float tr_cpPercentBlocksInPiece( const tr_completion_t * cp, int piece )
183{
184    assert( cp != NULL );
185
186    return cp->completeBlocks[piece] / (double)TR_BLOCKS_IN_PIECE(cp->tor,piece);
187}
188
189int
190tr_cpMissingBlocksForPiece( const tr_completion_t * cp, int piece )
191{
192    int i;
193    int n;
194    const tr_torrent_t * tor = cp->tor;
195    const int start = TOR_PIECE_FIRST_BLOCK(tor,piece);
196    const int end   = start + TR_BLOCKS_IN_PIECE(tor,piece);
197
198    n = 0;
199    for( i = start; i < end; ++i )
200        if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
201            ++n;
202
203    return n;
204}
205
206int tr_cpMissingBlockInPiece( const tr_completion_t * cp, int piece )
207{
208    int i;
209    const tr_torrent_t * tor = cp->tor;
210    const int start = TOR_PIECE_FIRST_BLOCK(tor,piece);
211    const int end   = start + TR_BLOCKS_IN_PIECE(tor,piece);
212
213    for( i = start; i < end; ++i )
214        if( !tr_cpBlockIsComplete( cp, i ) && !cp->blockDownloaders[i] )
215            return i;
216
217    return -1;
218}
219
220/***
221****
222***/
223
224cp_status_t
225tr_cpGetStatus ( const tr_completion_t * cp )
226{
227    int i;
228    int ret = TR_CP_COMPLETE;
229    const tr_info_t * info;
230
231    assert( cp != NULL );
232    assert( cp->tor != NULL );
233
234    info = &cp->tor->info;
235    for( i=0; i<info->pieceCount; ++i ) {
236        if( tr_cpPieceIsComplete( cp, i ) )
237            continue;
238        if( !info->pieces[i].dnd )
239            return TR_CP_INCOMPLETE;
240        ret = TR_CP_DONE;
241    }
242
243    return ret;
244}
245
246uint64_t
247tr_cpLeftUntilComplete ( const tr_completion_t * cp )
248{
249    int i;
250    uint64_t b=0;
251    const tr_torrent_t * tor;
252    const tr_info_t * info;
253
254    assert( cp != NULL );
255    assert( cp->tor != NULL );
256
257    tor = cp->tor;
258    info = &tor->info;
259    for( i=0; i<info->pieceCount; ++i )
260        if( !tr_cpPieceIsComplete( cp, i ) )
261            b += ( TR_BLOCKS_IN_PIECE(tor,i) - cp->completeBlocks[ i ] );
262
263    b *= tor->blockSize;
264
265    if( tor->blockCount && !tr_cpBlockIsComplete( cp, tor->blockCount - 1 ) )
266          b -= (tor->blockSize - (tor->info.totalSize % tor->blockSize));
267
268    return b;
269}
270
271void
272tr_cpDoneStats( const tr_completion_t  * cp ,
273                uint64_t               * setmeHaveBytes,
274                uint64_t               * setmeTotalBytes )
275{
276    const tr_torrent_t * tor = cp->tor;
277    const tr_info_t * info = &tor->info;
278    uint64_t have=0, total=0;
279    int i;
280
281    for( i=0; i<info->pieceCount; ++i ) {
282        if( !info->pieces[i].dnd ) {
283            total += info->pieceSize;
284            have += cp->completeBlocks[ i ];
285        }
286    }
287
288    have *= tor->blockSize;
289
290    /* the last piece/block is probably smaller than the others */
291    if( !info->pieces[info->pieceCount-1].dnd ) {
292        total -= ( info->pieceSize - ( info->totalSize % info->pieceSize ) );
293        if( tr_cpBlockIsComplete( cp, tor->blockCount-1 ) )
294            have -= ( tor->blockSize - ( info->totalSize % tor->blockSize ) );
295    }
296
297    assert( have <= total );
298    assert( total <= info->totalSize );
299
300    *setmeHaveBytes = have;
301    *setmeTotalBytes = total;
302}
303
304float
305tr_cpPercentComplete ( const tr_completion_t * cp )
306{
307    const uint64_t tilComplete = tr_cpLeftUntilComplete( cp );
308    const uint64_t total = cp->tor->info.totalSize;
309    const float f = 1.0 - (double)tilComplete / total;
310    return MAX(0.0, f);
311}
312
313uint64_t
314tr_cpDownloadedValid( const tr_completion_t * cp )
315{
316    uint64_t b = 0;
317    const tr_torrent_t * tor = cp->tor;
318    const tr_info_t * info = &tor->info;
319    int i;
320
321    for( i=0; i<info->pieceCount; ++i )
322        if( tr_cpPieceIsComplete( cp, i ) )
323            ++b;
324
325    b *= info->pieceSize;
326
327    if( tr_cpPieceIsComplete( cp, info->pieceCount-1 ) )
328        b -= (info->pieceSize - (info->totalSize % info->pieceSize));
329
330   return b;
331}
Note: See TracBrowser for help on using the repository browser.