source: trunk/libtransmission/completion.h @ 8889

Last change on this file since 8889 was 8889, checked in by charles, 13 years ago

(trunk) remove trailing spaces

  • Property svn:keywords set to Date Rev Author Id
File size: 4.6 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 8889 2009-08-10 20:04:08Z charles $
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    return tr_getRatio( cp->sizeNow, tr_torrentInfo(cp->tor)->totalSize );
109}
110
111static TR_INLINE float tr_cpPercentDone( const tr_completion * cp )
112{
113    const double ratio = tr_getRatio( cp->sizeNow, tr_cpSizeWhenDone( cp ) );
114    return (ratio == TR_RATIO_NA ||  ratio == TR_RATIO_INF) ? 0.0f : ratio;
115}
116
117/**
118*** Pieces
119**/
120
121int tr_cpMissingBlocksInPiece( const tr_completion  * cp,
122                               tr_piece_index_t       piece );
123
124tr_bool  tr_cpPieceIsComplete( const tr_completion * cp,
125                               tr_piece_index_t      piece );
126
127void   tr_cpPieceAdd( tr_completion    * completion,
128                      tr_piece_index_t   piece );
129
130void   tr_cpPieceRem( tr_completion     * completion,
131                      tr_piece_index_t   piece );
132
133tr_bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t );
134
135/**
136*** Blocks
137**/
138
139static TR_INLINE tr_bool tr_cpBlockIsCompleteFast( const tr_completion * cp, tr_block_index_t block )
140{
141    return tr_bitfieldHasFast( &cp->blockBitfield, block );
142}
143
144static TR_INLINE tr_bool tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t block )
145{
146    return tr_bitfieldHas( &cp->blockBitfield, block );
147}
148
149void      tr_cpBlockAdd( tr_completion * completion,
150                         tr_block_index_t block );
151
152tr_bool   tr_cpBlockBitfieldSet( tr_completion      * completion,
153                                 struct tr_bitfield * blocks );
154
155/***
156****
157***/
158
159static TR_INLINE const struct tr_bitfield * tr_cpPieceBitfield( const tr_completion * cp ) {
160    return &cp->pieceBitfield;
161}
162
163static TR_INLINE const struct tr_bitfield * tr_cpBlockBitfield( const tr_completion * cp ) {
164    assert( cp );
165    assert( cp->blockBitfield.bits );
166    assert( cp->blockBitfield.bitCount );
167    return &cp->blockBitfield;
168}
169
170#endif
Note: See TracBrowser for help on using the repository browser.