source: trunk/libtransmission/completion.h @ 12204

Last change on this file since 12204 was 12204, checked in by jordan, 11 years ago

(trunk) #4138 "use stdbool.h instead of tr_bool" -- done.

  • Property svn:keywords set to Date Rev Author Id
File size: 4.3 KB
Line 
1/*
2 * This file Copyright (C) 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 12204 2011-03-22 15:19:54Z jordan $
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 "bitset.h"
24#include "utils.h" /* tr_getRatio() */
25
26typedef struct tr_completion
27{
28    bool  sizeWhenDoneIsDirty;
29    bool  blocksWantedIsDirty;
30    bool  haveValidIsDirty;
31
32    tr_torrent *    tor;
33
34    /* do we have this block? */
35    tr_bitset    blockBitset;
36
37    /* a block is complete if and only if we have it */
38    uint16_t *  completeBlocks;
39
40    /* total number of blocks that we want downloaded
41       DON'T access this directly; it's a lazy field.
42       Used by tr_cpBlocksMissing(). */
43    tr_block_index_t    blocksWantedLazy;
44
45    /* total number of completed blocks that we want downloaded
46       DON'T access this directly; it's a lazy field.
47       Used by tr_cpBlocksMissing(). */
48    tr_block_index_t    blocksWantedCompleteLazy;
49
50    /* number of bytes we'll have when done downloading. [0..info.totalSize]
51       DON'T access this directly; it's a lazy field.
52       use tr_cpSizeWhenDone() instead! */
53    uint64_t    sizeWhenDoneLazy;
54
55    /* number of bytes we'll have when done downloading. [0..info.totalSize]
56       DON'T access this directly; it's a lazy field.
57       use tr_cpHaveValid() instead! */
58    uint64_t    haveValidLazy;
59
60    /* number of bytes we want or have now. [0..sizeWhenDone] */
61    uint64_t    sizeNow;
62}
63tr_completion;
64
65/**
66*** Life Cycle
67**/
68
69tr_completion * tr_cpConstruct( tr_completion *, tr_torrent * );
70
71tr_completion * tr_cpDestruct( tr_completion * );
72
73/**
74*** General
75**/
76
77tr_completeness    tr_cpGetStatus( const tr_completion * );
78
79uint64_t           tr_cpHaveValid( const tr_completion * );
80
81tr_block_index_t   tr_cpBlocksMissing( 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 inline uint64_t tr_cpHaveTotal( const tr_completion * cp )
92{
93    return cp->sizeNow;
94}
95
96static inline uint64_t tr_cpLeftUntilComplete( const tr_completion * cp )
97{
98    return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow;
99}
100
101static inline uint64_t tr_cpLeftUntilDone( const tr_completion * cp )
102{
103    return tr_cpSizeWhenDone( cp ) - cp->sizeNow;
104}
105
106static inline double 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 inline double tr_cpPercentDone( const tr_completion * cp )
118{
119    const double ratio = tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone( cp ) );
120    const int iratio = (int)ratio;
121    return ((iratio == TR_RATIO_NA) || (iratio == TR_RATIO_INF)) ? 0.0 : ratio;
122}
123
124/**
125*** Pieces
126**/
127
128int tr_cpMissingBlocksInPiece( const tr_completion * cp, tr_piece_index_t i );
129
130static inline bool
131tr_cpPieceIsComplete( const tr_completion * cp, tr_piece_index_t i )
132{
133    return tr_cpMissingBlocksInPiece( cp, i ) == 0;
134}
135
136void tr_cpPieceAdd( tr_completion * cp, tr_piece_index_t i );
137
138void tr_cpPieceRem( tr_completion * cp, tr_piece_index_t i );
139
140bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t );
141
142/**
143*** Blocks
144**/
145
146static inline bool
147tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t i )
148{
149    return tr_bitsetHas( &cp->blockBitset, i );
150}
151
152void tr_cpBlockAdd( tr_completion * cp, tr_block_index_t i );
153
154bool tr_cpBlockBitsetInit( tr_completion * cp, const tr_bitset * blocks );
155
156/***
157****
158***/
159
160static inline const tr_bitset *
161tr_cpBlockBitset( const tr_completion * cp )
162{
163    return &cp->blockBitset;
164}
165
166tr_bitfield * tr_cpCreatePieceBitfield( const tr_completion * cp );
167
168#endif
Note: See TracBrowser for help on using the repository browser.