source: trunk/libtransmission/completion.h @ 10918

Last change on this file since 10918 was 10918, checked in by charles, 12 years ago

(trunk libT) fix more issues found by -Wconversion

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