source: trunk/libtransmission/completion.h @ 9565

Last change on this file since 9565 was 9565, checked in by livings124, 13 years ago

tr_cpPercentComplete() considers cases where size is 0

  • Property svn:keywords set to Date Rev Author Id
File size: 4.7 KB
Line 
1/*
2 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: completion.h 9565 2009-11-25 05:01:51Z livings124 $
11 */
12
13#ifndef TR_COMPLETION_H
14#define TR_COMPLETION_H
15
16#ifndef __TRANSMISSION__
17 #error only libtransmission should #include this header.
18#endif
19
20#include <assert.h>
21
22#include "transmission.h"
23#include "bitfield.h"
24#include "utils.h" /* tr_bitfield */
25
26typedef struct tr_completion
27{
28    tr_bool  sizeWhenDoneIsDirty;
29    tr_bool  haveValidIsDirty;
30
31    tr_torrent *    tor;
32
33    /* do we have this block? */
34    tr_bitfield    blockBitfield;
35
36    /* do we have this piece? */
37    tr_bitfield    pieceBitfield;
38
39    /* a block is complete if and only if we have it */
40    uint16_t *  completeBlocks;
41
42    /* number of bytes we'll have when done downloading. [0..info.totalSize]
43       DON'T access this directly; it's a lazy field.
44       use tr_cpSizeWhenDone() instead! */
45    uint64_t    sizeWhenDoneLazy;
46
47    /* number of bytes we'll have when done downloading. [0..info.totalSize]
48       DON'T access this directly; it's a lazy field.
49       use tr_cpHaveValid() instead! */
50    uint64_t    haveValidLazy;
51
52    /* number of bytes we want or have now. [0..sizeWhenDone] */
53    uint64_t    sizeNow;
54}
55tr_completion;
56
57/**
58*** Life Cycle
59**/
60
61tr_completion * tr_cpConstruct( tr_completion *, tr_torrent * );
62
63tr_completion * tr_cpDestruct( tr_completion * );
64
65static TR_INLINE tr_completion* tr_cpNew( tr_torrent * tor )
66{
67    return tr_cpConstruct( tr_new0( tr_completion, 1 ), tor );
68}
69
70static TR_INLINE void tr_cpFree( tr_completion * cp )
71{
72    tr_free( tr_cpDestruct( cp ) );
73}
74
75/**
76*** General
77**/
78
79tr_completeness            tr_cpGetStatus( const tr_completion * );
80
81uint64_t                   tr_cpHaveValid( const tr_completion * );
82
83uint64_t                   tr_cpSizeWhenDone( const tr_completion * );
84
85void                       tr_cpInvalidateDND( tr_completion * );
86
87void                       tr_cpGetAmountDone( const   tr_completion * completion,
88                                               float                 * tab,
89                                               int                     tabCount );
90
91static TR_INLINE uint64_t tr_cpHaveTotal( const tr_completion * cp )
92{
93    return cp->sizeNow;
94}
95
96static TR_INLINE uint64_t tr_cpLeftUntilComplete( const tr_completion * cp )
97{
98    return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow;
99}
100
101static TR_INLINE uint64_t tr_cpLeftUntilDone( const tr_completion * cp )
102{
103    return tr_cpSizeWhenDone( cp ) - cp->sizeNow;
104}
105
106static TR_INLINE float tr_cpPercentComplete( const tr_completion * cp )
107{
108    const double ratio = tr_getRatio( cp->sizeNow, tr_torrentInfo(cp->tor)->totalSize );
109    if( (int)ratio == TR_RATIO_NA )
110        return 0.0;
111    else if( (int)ratio == TR_RATIO_INF )
112        return 1.0;
113    else
114        return ratio;
115}
116
117static TR_INLINE float tr_cpPercentDone( const tr_completion * cp )
118{
119    const double ratio = tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone( cp ) );
120    return (ratio == TR_RATIO_NA ||  ratio == TR_RATIO_INF) ? 0.0f : ratio;
121}
122
123/**
124*** Pieces
125**/
126
127int tr_cpMissingBlocksInPiece( const tr_completion  * cp,
128                               tr_piece_index_t       piece );
129
130tr_bool  tr_cpPieceIsComplete( const tr_completion * cp,
131                               tr_piece_index_t      piece );
132
133void   tr_cpPieceAdd( tr_completion    * completion,
134                      tr_piece_index_t   piece );
135
136void   tr_cpPieceRem( tr_completion     * completion,
137                      tr_piece_index_t   piece );
138
139tr_bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t );
140
141/**
142*** Blocks
143**/
144
145static TR_INLINE tr_bool tr_cpBlockIsCompleteFast( const tr_completion * cp, tr_block_index_t block )
146{
147    return tr_bitfieldHasFast( &cp->blockBitfield, block );
148}
149
150static TR_INLINE tr_bool tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block )
151{
152    return tr_bitfieldHas( &cp->blockBitfield, block );
153}
154
155void      tr_cpBlockAdd( tr_completion * completion,
156                         tr_block_index_t block );
157
158tr_bool   tr_cpBlockBitfieldSet( tr_completion      * completion,
159                                 struct tr_bitfield * blocks );
160
161/***
162****
163***/
164
165static TR_INLINE const struct tr_bitfield * tr_cpPieceBitfield( const tr_completion * cp ) {
166    return &cp->pieceBitfield;
167}
168
169static TR_INLINE const struct tr_bitfield * tr_cpBlockBitfield( const tr_completion * cp ) {
170    assert( cp );
171    assert( cp->blockBitfield.bits );
172    assert( cp->blockBitfield.bitCount );
173    return &cp->blockBitfield;
174}
175
176#endif
Note: See TracBrowser for help on using the repository browser.