(trunk libT) more completion and bitfield cleanup: (1) fix regression in tr_cpSizeWhenDone() reported by Waldorf, (2) make simple one-liner functions inlined
This commit is contained in:
parent
033845babc
commit
947134bbb3
|
@ -202,12 +202,6 @@ tr_bitfieldConstruct( tr_bitfield * b, size_t bit_count )
|
|||
assert( tr_bitfieldIsValid( b ) );
|
||||
}
|
||||
|
||||
void
|
||||
tr_bitfieldDestruct( tr_bitfield * b )
|
||||
{
|
||||
tr_bitfieldSetHasNone( b );
|
||||
}
|
||||
|
||||
static void
|
||||
tr_bitfieldClear( tr_bitfield * b )
|
||||
{
|
||||
|
|
|
@ -35,16 +35,6 @@ typedef struct tr_bitfield
|
|||
}
|
||||
tr_bitfield;
|
||||
|
||||
/***
|
||||
**** life cycle
|
||||
***/
|
||||
|
||||
extern const tr_bitfield TR_BITFIELD_INIT;
|
||||
|
||||
void tr_bitfieldConstruct( tr_bitfield*, size_t bit_count );
|
||||
|
||||
void tr_bitfieldDestruct( tr_bitfield* );
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
@ -61,6 +51,20 @@ void tr_bitfieldAddRange( tr_bitfield*, size_t begin, size_t end );
|
|||
|
||||
void tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );
|
||||
|
||||
/***
|
||||
**** life cycle
|
||||
***/
|
||||
|
||||
extern const tr_bitfield TR_BITFIELD_INIT;
|
||||
|
||||
void tr_bitfieldConstruct( tr_bitfield*, size_t bit_count );
|
||||
|
||||
static inline void
|
||||
tr_bitfieldDestruct( tr_bitfield * b )
|
||||
{
|
||||
tr_bitfieldSetHasNone( b );
|
||||
}
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
|
|
@ -36,12 +36,6 @@ tr_cpConstruct( tr_completion * cp, tr_torrent * tor )
|
|||
tr_cpReset( cp );
|
||||
}
|
||||
|
||||
void
|
||||
tr_cpDestruct( tr_completion * cp )
|
||||
{
|
||||
tr_bitfieldDestruct( &cp->blockBitfield );
|
||||
}
|
||||
|
||||
bool
|
||||
tr_cpBlockInit( tr_completion * cp, const tr_bitfield * b )
|
||||
{
|
||||
|
@ -65,27 +59,15 @@ tr_cpBlockInit( tr_completion * cp, const tr_bitfield * b )
|
|||
****
|
||||
***/
|
||||
|
||||
static inline bool
|
||||
isSeed( const tr_completion * cp )
|
||||
{
|
||||
return tr_bitfieldHasAll( &cp->blockBitfield );
|
||||
}
|
||||
|
||||
tr_completeness
|
||||
tr_cpGetStatus( const tr_completion * cp )
|
||||
{
|
||||
if( isSeed( cp ) ) return TR_SEED;
|
||||
if( tr_cpHasAll( cp ) ) return TR_SEED;
|
||||
if( !tr_torrentHasMetadata( cp->tor ) ) return TR_LEECH;
|
||||
if( cp->sizeNow == tr_cpSizeWhenDone( cp ) ) return TR_PARTIAL_SEED;
|
||||
return TR_LEECH;
|
||||
}
|
||||
|
||||
void
|
||||
tr_cpInvalidateDND( tr_completion * cp )
|
||||
{
|
||||
cp->sizeWhenDoneIsDirty = true;
|
||||
}
|
||||
|
||||
void
|
||||
tr_cpPieceRem( tr_completion * cp, tr_piece_index_t piece )
|
||||
{
|
||||
|
@ -159,15 +141,34 @@ tr_cpSizeWhenDone( const tr_completion * ccp )
|
|||
{
|
||||
if( ccp->sizeWhenDoneIsDirty )
|
||||
{
|
||||
tr_piece_index_t i;
|
||||
uint64_t size = 0;
|
||||
tr_completion * cp = (tr_completion *) ccp; /* mutable */
|
||||
const tr_torrent * tor = ccp->tor;
|
||||
const tr_info * info = &tor->info;
|
||||
tr_completion * cp = (tr_completion *) ccp; /* mutable */
|
||||
|
||||
for( i=0; i<info->pieceCount; ++i )
|
||||
if( !info->pieces[i].dnd || tr_cpPieceIsComplete( cp, i ) )
|
||||
size += tr_torPieceCountBytes( tor, i );
|
||||
if( tr_cpHasAll( ccp ) )
|
||||
{
|
||||
size = tor->info.totalSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
tr_piece_index_t p;
|
||||
|
||||
for( p=0; p<tor->info.pieceCount; ++p )
|
||||
{
|
||||
if( !tor->info.pieces[p].dnd )
|
||||
{
|
||||
size += tr_torPieceCountBytes( tor, p );
|
||||
}
|
||||
else
|
||||
{
|
||||
tr_block_index_t b, f, l;
|
||||
tr_torGetPieceBlockRange( cp->tor, p, &f, &l );
|
||||
for( b=f; b<=l; ++b )
|
||||
if( tr_cpBlockIsComplete( cp, b ) )
|
||||
size += tr_torBlockCountBytes( tor, b );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cp->sizeWhenDoneLazy = size;
|
||||
cp->sizeWhenDoneIsDirty = false;
|
||||
|
@ -176,23 +177,11 @@ tr_cpSizeWhenDone( const tr_completion * ccp )
|
|||
return ccp->sizeWhenDoneLazy;
|
||||
}
|
||||
|
||||
uint64_t
|
||||
tr_cpLeftUntilDone( const tr_completion * cp )
|
||||
{
|
||||
return tr_cpSizeWhenDone( cp ) - cp->sizeNow;
|
||||
}
|
||||
|
||||
uint64_t
|
||||
tr_cpLeftUntilComplete( const tr_completion * cp )
|
||||
{
|
||||
return cp->tor->info.totalSize - cp->sizeNow;
|
||||
}
|
||||
|
||||
void
|
||||
tr_cpGetAmountDone( const tr_completion * cp, float * tab, int tabCount )
|
||||
{
|
||||
int i;
|
||||
const bool seed = isSeed( cp );
|
||||
const bool seed = tr_cpHasAll( cp );
|
||||
const float interval = cp->tor->info.pieceCount / (float)tabCount;
|
||||
|
||||
for( i=0; i<tabCount; ++i ) {
|
||||
|
@ -211,7 +200,7 @@ tr_cpGetAmountDone( const tr_completion * cp, float * tab, int tabCount )
|
|||
size_t
|
||||
tr_cpMissingBlocksInPiece( const tr_completion * cp, tr_piece_index_t piece )
|
||||
{
|
||||
if( isSeed( cp ) )
|
||||
if( tr_cpHasAll( cp ) )
|
||||
return 0;
|
||||
else {
|
||||
tr_block_index_t f, l;
|
||||
|
@ -223,7 +212,7 @@ tr_cpMissingBlocksInPiece( const tr_completion * cp, tr_piece_index_t piece )
|
|||
size_t
|
||||
tr_cpMissingBytesInPiece( const tr_completion * cp, tr_piece_index_t piece )
|
||||
{
|
||||
if( isSeed( cp ) )
|
||||
if( tr_cpHasAll( cp ) )
|
||||
return 0;
|
||||
else {
|
||||
size_t haveBytes = 0;
|
||||
|
|
|
@ -25,7 +25,6 @@ typedef struct tr_completion
|
|||
{
|
||||
tr_torrent * tor;
|
||||
|
||||
/* do we have this block? */
|
||||
tr_bitfield blockBitfield;
|
||||
|
||||
/* number of bytes we'll have when done downloading. [0..info.totalSize]
|
||||
|
@ -53,9 +52,15 @@ tr_completion;
|
|||
*** Life Cycle
|
||||
**/
|
||||
|
||||
void tr_cpConstruct( tr_completion *, tr_torrent * );
|
||||
void tr_cpConstruct( tr_completion *, tr_torrent * );
|
||||
|
||||
void tr_cpDestruct( tr_completion * );
|
||||
bool tr_cpBlockInit( tr_completion * cp, const tr_bitfield * blocks );
|
||||
|
||||
static inline void
|
||||
tr_cpDestruct( tr_completion * cp )
|
||||
{
|
||||
tr_bitfieldDestruct( &cp->blockBitfield );
|
||||
}
|
||||
|
||||
/**
|
||||
*** General
|
||||
|
@ -71,14 +76,29 @@ uint64_t tr_cpHaveValid( const tr_completion * );
|
|||
|
||||
uint64_t tr_cpSizeWhenDone( const tr_completion * );
|
||||
|
||||
uint64_t tr_cpLeftUntilComplete( const tr_completion * );
|
||||
|
||||
uint64_t tr_cpLeftUntilDone( const tr_completion * );
|
||||
|
||||
void tr_cpGetAmountDone( const tr_completion * completion,
|
||||
float * tab,
|
||||
int tabCount );
|
||||
|
||||
|
||||
static inline uint64_t
|
||||
tr_cpHaveTotal( const tr_completion * cp )
|
||||
{
|
||||
return cp->sizeNow;
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
tr_cpLeftUntilComplete( const tr_completion * cp )
|
||||
{
|
||||
return tr_torrentInfo(cp->tor)->totalSize - cp->sizeNow;
|
||||
}
|
||||
|
||||
static inline uint64_t
|
||||
tr_cpLeftUntilDone( const tr_completion * cp )
|
||||
{
|
||||
return tr_cpSizeWhenDone( cp ) - cp->sizeNow;
|
||||
}
|
||||
|
||||
static inline bool tr_cpHasAll( const tr_completion * cp )
|
||||
{
|
||||
return tr_bitfieldHasAll( &cp->blockBitfield );
|
||||
|
@ -89,11 +109,6 @@ static inline bool tr_cpHasNone( const tr_completion * cp )
|
|||
return tr_bitfieldHasNone( &cp->blockBitfield );
|
||||
}
|
||||
|
||||
static inline uint64_t tr_cpHaveTotal( const tr_completion * cp )
|
||||
{
|
||||
return cp->sizeNow;
|
||||
}
|
||||
|
||||
/**
|
||||
*** Pieces
|
||||
**/
|
||||
|
@ -116,25 +131,27 @@ tr_cpPieceIsComplete( const tr_completion * cp, tr_piece_index_t i )
|
|||
*** Blocks
|
||||
**/
|
||||
|
||||
void tr_cpBlockAdd( tr_completion * cp, tr_block_index_t i );
|
||||
|
||||
static inline bool
|
||||
tr_cpBlockIsComplete( const tr_completion * cp, tr_block_index_t i )
|
||||
{
|
||||
return tr_bitfieldHas( &cp->blockBitfield, i );
|
||||
}
|
||||
|
||||
void tr_cpBlockAdd( tr_completion * cp, tr_block_index_t i );
|
||||
|
||||
bool tr_cpBlockInit( tr_completion * cp, const tr_bitfield * blocks );
|
||||
|
||||
/***
|
||||
**** Misc
|
||||
***/
|
||||
|
||||
bool tr_cpFileIsComplete( const tr_completion * cp, tr_file_index_t );
|
||||
|
||||
void tr_cpInvalidateDND( tr_completion * );
|
||||
|
||||
void* tr_cpCreatePieceBitfield( const tr_completion * cp, size_t * byte_count );
|
||||
|
||||
static inline void
|
||||
tr_cpInvalidateDND( tr_completion * cp )
|
||||
{
|
||||
cp->sizeWhenDoneIsDirty = true;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue