source: trunk/libtransmission/completion.h @ 7578

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

(trunk libT) avoid some unnecessary memory fragmentation... for composited objects that have a tr_completion, contain the it directly rather than a pointer to one allocated elsewhere on the heap.

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