From 3a73f6790ef4d52a5032e643dcf81c001469ceca Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 29 Jun 2007 06:24:55 +0000 Subject: [PATCH] Fix endgame bug that caused the last 2-3% to drag out forever in recent nightlies. Thanks SoftwareElves --- libtransmission/peer.c | 17 +++++++++++++---- libtransmission/peerutils.h | 18 ++++-------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/libtransmission/peer.c b/libtransmission/peer.c index f4edab469..9c3f1236c 100644 --- a/libtransmission/peer.c +++ b/libtransmission/peer.c @@ -613,10 +613,19 @@ writeEnd: && peer->inRequestCount < peer->inRequestMax ) { int i; - int poolSize=0, endgame=0; - int * pool = getPreferredPieces ( tor, peer, &poolSize, &endgame ); + int poolSize = 0; + int * pool = getPreferredPieces ( tor, peer, &poolSize ); + const int endgame = !poolSize; - for( i=0; iinRequestCountinRequestMax; ) + if( endgame ) /* endgame -- request everything we don't already have */ + { + const tr_bitfield_t * blocks = tr_cpBlockBitfield( tor->completion ); + for( i=0; iblockCount && peer->inRequestCountinRequestMax; ++i ) { + if( tr_bitfieldHas( blocks, i ) ) + sendRequest( tor, peer, i ); + } + } + else for( i=0; iinRequestCountinRequestMax; ) { int unused; const int piece = pool[i]; @@ -629,7 +638,7 @@ writeEnd: else ++i; } - free( pool ); + tr_free( pool ); } return TR_OK; diff --git a/libtransmission/peerutils.h b/libtransmission/peerutils.h index aec36007d..1196b62aa 100644 --- a/libtransmission/peerutils.h +++ b/libtransmission/peerutils.h @@ -277,29 +277,19 @@ int comparePieces (const void * aIn, const void * bIn) static int* getPreferredPieces( const tr_torrent_t * tor, const tr_peer_t * peer, - int * pieceCount, - int * endgame ) + int * pieceCount ) { const tr_info_t * inf = &tor->info; int i; int poolSize = 0; - int * pool = malloc ( inf->pieceCount * sizeof(int) ); - - *endgame = 0; + int * pool = tr_new( int, inf->pieceCount ); for( i=0; ipieceCount; ++i ) if( isPieceInteresting( tor, peer, i ) ) if( tr_cpMissingBlocksForPiece( tor->completion, i ) ) pool[poolSize++] = i; - if( !poolSize ) { - *endgame = 1; - for( i=0; ipieceCount; ++i ) - if( isPieceInteresting( tor, peer, i ) ) - pool[poolSize++] = i; - } - #if 0 fprintf (stderr, "old pool: "); for (i=0; i<15 && i 1 ) { - PieceCompareData * p = malloc ( poolSize * sizeof(PieceCompareData) ); + PieceCompareData * p = tr_new( PieceCompareData, poolSize ); for( i=0; i