(trunk libT) comments, tr_bool correctness, better runtime tests.

This commit is contained in:
Charles Kerr 2008-12-21 18:15:00 +00:00
parent fefa352b11
commit 8556ce653f
8 changed files with 114 additions and 95 deletions

View File

@ -112,18 +112,12 @@ comparePointers( const void * a, const void * b )
return a - b;
}
static int
isBandwidth( const tr_bandwidth * b )
tr_bool
tr_isBandwidth( const tr_bandwidth * b )
{
return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER );
}
static int
isDirection( const tr_direction dir )
{
return ( dir == TR_UP ) || ( dir == TR_DOWN );
}
/***
****
***/
@ -136,8 +130,8 @@ tr_bandwidthNew( tr_session * session, tr_bandwidth * parent )
b->children = tr_ptrArrayNew( );
b->peers = tr_ptrArrayNew( );
b->magicNumber = MAGIC_NUMBER;
b->band[TR_UP].honorParentLimits = 1;
b->band[TR_DOWN].honorParentLimits = 1;
b->band[TR_UP].honorParentLimits = TRUE;
b->band[TR_DOWN].honorParentLimits = TRUE;
tr_bandwidthSetParent( b, parent );
return b;
}
@ -145,7 +139,7 @@ tr_bandwidthNew( tr_session * session, tr_bandwidth * parent )
void
tr_bandwidthFree( tr_bandwidth * b )
{
assert( isBandwidth( b ) );
assert( tr_isBandwidth( b ) );
tr_bandwidthSetParent( b, NULL );
tr_ptrArrayFree( b->peers, NULL );
@ -162,20 +156,20 @@ void
tr_bandwidthSetParent( tr_bandwidth * b,
tr_bandwidth * parent )
{
assert( isBandwidth( b ) );
assert( tr_isBandwidth( b ) );
assert( b != parent );
if( b->parent )
{
assert( isBandwidth( b->parent ) );
assert( tr_isBandwidth( b->parent ) );
tr_ptrArrayRemoveSorted( b->parent->children, b, comparePointers );
b->parent= NULL;
b->parent = NULL;
}
if( parent )
{
assert( isBandwidth( parent ) );
assert( tr_isBandwidth( parent ) );
assert( parent->parent != b );
tr_ptrArrayInsertSorted( parent->children, b, comparePointers );
@ -186,12 +180,12 @@ tr_bandwidthSetParent( tr_bandwidth * b,
void
tr_bandwidthHonorParentLimits( tr_bandwidth * b,
tr_direction dir,
int honorParentLimits )
tr_bool honorParentLimits )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
b->band[dir].honorParentLimits = honorParentLimits != 0;
b->band[dir].honorParentLimits = honorParentLimits;
}
/***
@ -203,8 +197,8 @@ tr_bandwidthSetDesiredSpeed( tr_bandwidth * b,
tr_direction dir,
double desiredSpeed )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
b->band[dir].desiredSpeed = desiredSpeed;
}
@ -213,8 +207,8 @@ double
tr_bandwidthGetDesiredSpeed( const tr_bandwidth * b,
tr_direction dir )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
return b->band[dir].desiredSpeed;
}
@ -222,22 +216,22 @@ tr_bandwidthGetDesiredSpeed( const tr_bandwidth * b,
void
tr_bandwidthSetLimited( tr_bandwidth * b,
tr_direction dir,
int isLimited )
tr_bool isLimited )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
b->band[dir].isLimited = isLimited != 0;
b->band[dir].isLimited = isLimited;
}
int
tr_bool
tr_bandwidthIsLimited( const tr_bandwidth * b,
tr_direction dir )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
return b->band[dir].isLimited != 0;
return b->band[dir].isLimited;
}
#if 0
@ -251,9 +245,10 @@ allocateBandwidth( tr_bandwidth * b,
int period_msec,
tr_ptrArray * peer_pool )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
/* set the available bandwidth */
if( b->band[dir].isLimited )
{
const double desiredSpeed = b->band[dir].desiredSpeed;
@ -268,6 +263,7 @@ allocateBandwidth( tr_bandwidth * b,
#endif
}
/* traverse & repeat for the subtree */
{
int i;
const int n = tr_ptrArraySize( b->peers );
@ -310,12 +306,10 @@ tr_bandwidthAllocate( tr_bandwidth * b,
for( i=0; i<peerCount; ++i )
tr_peerIoSetEnabled( peers[i], dir, FALSE );
/* First phase of IO. Tries to distribute bandwidth in a fair/even manner
* to avoid "greedy peers" from starving out the other peers: loop through
* peers in a round-robin fashion, giving each one of them them small chunks
* of bandwidth to use. (It's small to conserve some of the bandwidth
* until the end of the loop). Keep looping until we run out of bandwidth
* or peers that are ready to use it. */
/* First phase of IO. Tries to distribute bandwidth fairly to keep faster
* peers from starving the others. Loop through the peers, giving each a
* small chunk of bandwidth. Keep looping until we run out of bandwidth
* or pweers that can use it */
n = peerCount;
i = n ? tr_cryptoWeakRandInt( n ) : 0; /* pick a random starting point */
for( ; n>0; )
@ -338,11 +332,10 @@ tr_bandwidthAllocate( tr_bandwidth * b,
i = 0;
}
/* Second phase of IO. To help us scale well in high bandwidth situations
* such as LANs, enable on-demand IO for peers with bandwidth left to burn.
* This on-demand IO for a peer is enabled until either (1) the peer runs
* out of bandwidth, or (2) the next tr_bandwidthAllocate() call, when we
* start all over again. */
/* Second phase of IO. To help us scale in high bandwidth situations,
* enable on-demand IO for peers with bandwidth left to burn.
* This on-demand IO is enabled until (1) the peer runs out of bandwidth,
* or (2) the next tr_bandwidthAllocate() call, when we start over again. */
for( i=0; i<peerCount; ++i )
if( tr_peerIoHasBandwidthLeft( peers[i], dir ) )
tr_peerIoSetEnabled( peers[i], dir, TRUE );
@ -359,8 +352,8 @@ void
tr_bandwidthAddPeer( tr_bandwidth * b,
tr_peerIo * peerIo )
{
assert( isBandwidth( b ) );
assert( peerIo );
assert( tr_isBandwidth( b ) );
assert( tr_isPeerIo( peerIo ) );
tr_ptrArrayInsertSorted( b->peers, peerIo, comparePointers );
}
@ -369,8 +362,8 @@ void
tr_bandwidthRemovePeer( tr_bandwidth * b,
tr_peerIo * peerIo )
{
assert( isBandwidth( b ) );
assert( peerIo );
assert( tr_isBandwidth( b ) );
assert( tr_isPeerIo( peerIo ) );
tr_ptrArrayRemoveSorted( b->peers, peerIo, comparePointers );
}
@ -384,8 +377,8 @@ tr_bandwidthClamp( const tr_bandwidth * b,
tr_direction dir,
size_t byteCount )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
if( b )
{
@ -402,8 +395,8 @@ tr_bandwidthClamp( const tr_bandwidth * b,
double
tr_bandwidthGetRawSpeed( const tr_bandwidth * b, tr_direction dir )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
return getSpeed( &b->band[dir].raw, HISTORY_MSEC );
}
@ -411,8 +404,8 @@ tr_bandwidthGetRawSpeed( const tr_bandwidth * b, tr_direction dir )
double
tr_bandwidthGetPieceSpeed( const tr_bandwidth * b, tr_direction dir )
{
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
return getSpeed( &b->band[dir].piece, HISTORY_MSEC );
}
@ -421,13 +414,13 @@ void
tr_bandwidthUsed( tr_bandwidth * b,
tr_direction dir,
size_t byteCount,
int isPieceData )
tr_bool isPieceData )
{
struct tr_band * band;
size_t oldBytesLeft;
assert( isBandwidth( b ) );
assert( isDirection( dir ) );
assert( tr_isBandwidth( b ) );
assert( tr_isDirection( dir ) );
band = &b->band[dir];

View File

@ -75,6 +75,9 @@ tr_bandwidth*
/** @brief destroy a tr_bandwidth object */
void tr_bandwidthFree ( tr_bandwidth * bandwidth );
/** @brief test to see if the pointer refers to a live bandwidth object */
tr_bool tr_isBandwidth ( const tr_bandwidth * bandwidth );
/******
*******
******/
@ -100,12 +103,12 @@ double tr_bandwidthGetDesiredSpeed ( const tr_bandwidth * bandwidth,
*/
void tr_bandwidthSetLimited ( tr_bandwidth * bandwidth,
tr_direction direction,
int isLimited );
tr_bool isLimited );
/**
* @return nonzero if this bandwidth throttles its iobufs' speeds
*/
int tr_bandwidthIsLimited ( const tr_bandwidth * bandwidth,
tr_bool tr_bandwidthIsLimited ( const tr_bandwidth * bandwidth,
tr_direction direction );
/**
@ -145,7 +148,7 @@ double tr_bandwidthGetPieceSpeed ( const tr_bandwidth * bandwidth,
void tr_bandwidthUsed ( tr_bandwidth * bandwidth,
tr_direction direction,
size_t byteCount,
int isPieceData );
tr_bool isPieceData );
/******
*******
@ -162,7 +165,7 @@ void tr_bandwidthSetParent ( tr_bandwidth * bandwidth,
*/
void tr_bandwidthHonorParentLimits ( tr_bandwidth * bandwidth,
tr_direction direction,
int isEnabled );
tr_bool isEnabled );
/******
*******

View File

@ -194,8 +194,8 @@ canReadWrapper( tr_peerIo * io )
#define _isBool(b) (((b)==0 || (b)==1))
static int
isPeerIo( const tr_peerIo * io )
tr_bool
tr_isPeerIo( const tr_peerIo * io )
{
return ( io != NULL )
&& ( io->magicNumber == MAGIC_NUMBER )
@ -216,7 +216,7 @@ event_read_cb( int fd, short event UNUSED, void * vio )
const size_t howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, io->session->so_rcvbuf );
const tr_direction dir = TR_DOWN;
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
dbgmsg( io, "libevent says this peer is ready to read" );
@ -287,7 +287,7 @@ event_write_cb( int fd, short event UNUSED, void * vio )
size_t howmuch;
const tr_direction dir = TR_UP;
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
dbgmsg( io, "libevent says this peer is ready to write" );
@ -446,7 +446,7 @@ tr_peerIoFree( tr_peerIo * io )
tr_session*
tr_peerIoGetSession( tr_peerIo * io )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
assert( io->session );
return io->session;
@ -456,7 +456,7 @@ const tr_address*
tr_peerIoGetAddress( const tr_peerIo * io,
tr_port * port )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
if( port )
*port = io->port;
@ -537,7 +537,7 @@ void
tr_peerIoSetTorrentHash( tr_peerIo * io,
const uint8_t * hash )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
tr_cryptoSetTorrentHash( io->crypto, hash );
}
@ -545,7 +545,7 @@ tr_peerIoSetTorrentHash( tr_peerIo * io,
const uint8_t*
tr_peerIoGetTorrentHash( tr_peerIo * io )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
assert( io->crypto );
return tr_cryptoGetTorrentHash( io->crypto );
@ -554,7 +554,7 @@ tr_peerIoGetTorrentHash( tr_peerIo * io )
int
tr_peerIoHasTorrentHash( const tr_peerIo * io )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
assert( io->crypto );
return tr_cryptoHasTorrentHash( io->crypto );
@ -568,7 +568,7 @@ void
tr_peerIoSetPeersId( tr_peerIo * io,
const uint8_t * peer_id )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
if( ( io->peerIdIsSet = peer_id != NULL ) )
memcpy( io->peerId, peer_id, 20 );
@ -579,7 +579,7 @@ tr_peerIoSetPeersId( tr_peerIo * io,
const uint8_t*
tr_peerIoGetPeersId( const tr_peerIo * io )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
assert( io->peerIdIsSet );
return io->peerId;
@ -593,7 +593,7 @@ void
tr_peerIoEnableFEXT( tr_peerIo * io,
tr_bool flag )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
assert( isFlag( flag ) );
dbgmsg( io, "setting FEXT support flag to %d", (flag?1:0) );
@ -603,7 +603,7 @@ tr_peerIoEnableFEXT( tr_peerIo * io,
tr_bool
tr_peerIoSupportsFEXT( const tr_peerIo * io )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
return io->fastExtensionSupported;
}
@ -616,7 +616,7 @@ void
tr_peerIoEnableLTEP( tr_peerIo * io,
tr_bool flag )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
assert( isFlag( flag ) );
dbgmsg( io, "setting LTEP support flag to %d", (flag?1:0) );
@ -626,7 +626,7 @@ tr_peerIoEnableLTEP( tr_peerIo * io,
tr_bool
tr_peerIoSupportsLTEP( const tr_peerIo * io )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
return io->extendedProtocolSupported;
}
@ -666,7 +666,7 @@ void
tr_peerIoSetBandwidth( tr_peerIo * io,
tr_bandwidth * bandwidth )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
if( io->bandwidth )
tr_bandwidthRemovePeer( io->bandwidth, io );
@ -691,7 +691,7 @@ void
tr_peerIoSetEncryption( tr_peerIo * io,
int encryptionMode )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
assert( encryptionMode == PEER_ENCRYPTION_NONE
|| encryptionMode == PEER_ENCRYPTION_RC4 );
@ -838,6 +838,8 @@ tr_peerIoReadUint16( tr_peerIo * io,
{
uint16_t tmp;
assert( tr_isPeerIo( io ) );
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
*setme = ntohs( tmp );
}
@ -849,6 +851,8 @@ tr_peerIoReadUint32( tr_peerIo * io,
{
uint32_t tmp;
assert( tr_isPeerIo( io ) );
tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
*setme = ntohl( tmp );
}
@ -858,8 +862,11 @@ tr_peerIoDrain( tr_peerIo * io,
struct evbuffer * inbuf,
size_t byteCount )
{
uint8_t * tmp = tr_new( uint8_t, byteCount );
uint8_t * tmp;
assert( tr_isPeerIo( io ) );
tmp = tr_new( uint8_t, byteCount );
tr_peerIoReadBytes( io, inbuf, tmp, byteCount );
tr_free( tmp );
}
@ -879,7 +886,7 @@ tr_peerIoTryRead( tr_peerIo * io, size_t howmuch )
{
int res;
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch );
@ -906,7 +913,7 @@ tr_peerIoTryWrite( tr_peerIo * io, size_t howmuch )
{
int n;
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
howmuch = tr_bandwidthClamp( io->bandwidth, TR_UP, howmuch );
@ -928,8 +935,8 @@ tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit )
{
int ret;
assert( isPeerIo( io ) );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isPeerIo( io ) );
assert( tr_isDirection( dir ) );
if( dir==TR_DOWN )
ret = tr_peerIoTryRead( io, limit );
@ -942,7 +949,7 @@ tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit )
struct evbuffer *
tr_peerIoGetReadBuffer( tr_peerIo * io )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
return io->inbuf;
}
@ -950,8 +957,8 @@ tr_peerIoGetReadBuffer( tr_peerIo * io )
tr_bool
tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir )
{
assert( isPeerIo( io ) );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isPeerIo( io ) );
assert( tr_isDirection( dir ) );
return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0;
}
@ -963,7 +970,7 @@ tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir )
static void
event_enable( tr_peerIo * io, short event )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
if( event & EV_READ )
event_add( &io->event_read, NULL );
@ -975,7 +982,7 @@ event_enable( tr_peerIo * io, short event )
static void
event_disable( struct tr_peerIo * io, short event )
{
assert( isPeerIo( io ) );
assert( tr_isPeerIo( io ) );
if( event & EV_READ )
event_del( &io->event_read );
@ -990,7 +997,12 @@ tr_peerIoSetEnabled( tr_peerIo * io,
tr_direction dir,
tr_bool isEnabled )
{
const short event = dir == TR_UP ? EV_WRITE : EV_READ;
short event;
assert( tr_isPeerIo( io ) );
assert( tr_isDirection( dir ) );
event = dir == TR_UP ? EV_WRITE : EV_READ;
if( isEnabled )
event_enable( io, event );

View File

@ -43,6 +43,8 @@ tr_peerIo* tr_peerIoNewIncoming( tr_session * session,
void tr_peerIoFree ( tr_peerIo * io );
tr_bool tr_isPeerIo ( const tr_peerIo * io );
/**
***

View File

@ -655,7 +655,7 @@ tr_sessionSetSpeedLimitEnabled( tr_session * session,
tr_bool isLimited )
{
assert( session );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isDirection( dir ) );
session->isSpeedLimited[dir] = isLimited;
updateBandwidth( session, dir );
@ -667,7 +667,7 @@ tr_sessionSetSpeedLimit( tr_session * session,
int desiredSpeed )
{
assert( session );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isDirection( dir ) );
session->speedLimit[dir] = desiredSpeed;
updateBandwidth( session, dir );
@ -678,7 +678,7 @@ tr_sessionIsSpeedLimitEnabled( const tr_session * session,
tr_direction dir )
{
assert( session );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isDirection( dir ) );
return session->isSpeedLimited[dir];
}
@ -688,7 +688,7 @@ tr_sessionGetSpeedLimit( const tr_session * session,
tr_direction dir )
{
assert( session );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isDirection( dir ) );
return session->speedLimit[dir];
}

View File

@ -142,7 +142,7 @@ tr_torrentSetSpeedMode( tr_torrent * tor,
tr_speedlimit mode )
{
assert( tor != NULL );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isDirection( dir ) );
assert( mode==TR_SPEEDLIMIT_GLOBAL || mode==TR_SPEEDLIMIT_SINGLE || mode==TR_SPEEDLIMIT_UNLIMITED );
tor->speedLimitMode[dir] = mode;
@ -156,7 +156,7 @@ tr_torrentGetSpeedMode( const tr_torrent * tor,
tr_direction dir )
{
assert( tor != NULL );
assert( dir==TR_UP || dir==TR_DOWN );
assert( tr_isDirection( dir ) );
return tor->speedLimitMode[dir];
}

View File

@ -1405,6 +1405,10 @@ void tr_torrentSetActivityDate( tr_torrent * torrent,
void tr_torrentSetDoneDate( tr_torrent * torrent,
time_t doneDate );
/** @brief Sanity checker to test that the direction is TR_UP or TR_DOWN */
tr_bool tr_isDirection( tr_direction );
/** @} */
#ifdef __TRANSMISSION__

View File

@ -1255,3 +1255,8 @@ tr_base64_decode( const void * input,
return ret;
}
tr_bool
tr_isDirection( tr_direction dir )
{
return dir==TR_UP || dir==TR_DOWN;
}