Changeset 7446


Ignore:
Timestamp:
Dec 21, 2008, 6:15:00 PM (12 years ago)
Author:
charles
Message:

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

Location:
trunk/libtransmission
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bandwidth.c

    r7443 r7446  
    113113}
    114114
    115 static int
    116 isBandwidth( const tr_bandwidth * b )
     115tr_bool
     116tr_isBandwidth( const tr_bandwidth * b )
    117117{
    118118    return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER );
    119 }
    120 
    121 static int
    122 isDirection( const tr_direction dir )
    123 {
    124     return ( dir == TR_UP ) || ( dir == TR_DOWN );
    125119}
    126120
     
    137131    b->peers = tr_ptrArrayNew( );
    138132    b->magicNumber = MAGIC_NUMBER;
    139     b->band[TR_UP].honorParentLimits = 1;
    140     b->band[TR_DOWN].honorParentLimits = 1;
     133    b->band[TR_UP].honorParentLimits = TRUE;
     134    b->band[TR_DOWN].honorParentLimits = TRUE;
    141135    tr_bandwidthSetParent( b, parent );
    142136    return b;
     
    146140tr_bandwidthFree( tr_bandwidth * b )
    147141{
    148     assert( isBandwidth( b ) );
     142    assert( tr_isBandwidth( b ) );
    149143
    150144    tr_bandwidthSetParent( b, NULL );
     
    163157                       tr_bandwidth  * parent )
    164158{
    165     assert( isBandwidth( b ) );
     159    assert( tr_isBandwidth( b ) );
    166160    assert( b != parent );
    167161
    168162    if( b->parent )
    169163    {
    170         assert( isBandwidth( b->parent ) );
     164        assert( tr_isBandwidth( b->parent ) );
    171165
    172166        tr_ptrArrayRemoveSorted( b->parent->children, b, comparePointers );
    173         b->parent= NULL;
     167        b->parent = NULL;
    174168    }
    175169
    176170    if( parent )
    177171    {
    178         assert( isBandwidth( parent ) );
     172        assert( tr_isBandwidth( parent ) );
    179173        assert( parent->parent != b );
    180174
     
    187181tr_bandwidthHonorParentLimits( tr_bandwidth  * b,
    188182                               tr_direction    dir,
    189                                int             honorParentLimits )
    190 {
    191     assert( isBandwidth( b ) );
    192     assert( isDirection( dir ) );
    193 
    194     b->band[dir].honorParentLimits = honorParentLimits != 0;
     183                               tr_bool         honorParentLimits )
     184{
     185    assert( tr_isBandwidth( b ) );
     186    assert( tr_isDirection( dir ) );
     187
     188    b->band[dir].honorParentLimits = honorParentLimits;
    195189}
    196190
     
    204198                             double          desiredSpeed )
    205199{
    206     assert( isBandwidth( b ) );
    207     assert( isDirection( dir ) );
     200    assert( tr_isBandwidth( b ) );
     201    assert( tr_isDirection( dir ) );
    208202
    209203    b->band[dir].desiredSpeed = desiredSpeed;
     
    214208                             tr_direction          dir )
    215209{
    216     assert( isBandwidth( b ) );
    217     assert( isDirection( dir ) );
     210    assert( tr_isBandwidth( b ) );
     211    assert( tr_isDirection( dir ) );
    218212
    219213    return b->band[dir].desiredSpeed;
     
    223217tr_bandwidthSetLimited( tr_bandwidth  * b,
    224218                        tr_direction    dir,
    225                         int             isLimited )
    226 {
    227     assert( isBandwidth( b ) );
    228     assert( isDirection( dir ) );
    229 
    230     b->band[dir].isLimited = isLimited != 0;
    231 }
    232 
    233 int
     219                        tr_bool         isLimited )
     220{
     221    assert( tr_isBandwidth( b ) );
     222    assert( tr_isDirection( dir ) );
     223
     224    b->band[dir].isLimited = isLimited;
     225}
     226
     227tr_bool
    234228tr_bandwidthIsLimited( const tr_bandwidth  * b,
    235229                       tr_direction          dir )
    236230{
    237     assert( isBandwidth( b ) );
    238     assert( isDirection( dir ) );
    239 
    240     return b->band[dir].isLimited != 0;
     231    assert( tr_isBandwidth( b ) );
     232    assert( tr_isDirection( dir ) );
     233
     234    return b->band[dir].isLimited;
    241235}
    242236
     
    252246                   tr_ptrArray   * peer_pool )
    253247{
    254     assert( isBandwidth( b ) );
    255     assert( isDirection( dir ) );
    256 
     248    assert( tr_isBandwidth( b ) );
     249    assert( tr_isDirection( dir ) );
     250
     251    /* set the available bandwidth */
    257252    if( b->band[dir].isLimited )
    258253    {
     
    269264    }
    270265
     266    /* traverse & repeat for the subtree */
    271267    {
    272268        int i;
     
    311307        tr_peerIoSetEnabled( peers[i], dir, FALSE );
    312308
    313     /* First phase of IO.  Tries to distribute bandwidth in a fair/even manner
    314      * to avoid "greedy peers" from starving out the other peers: loop through
    315      * peers in a round-robin fashion, giving each one of them them small chunks
    316      * of bandwidth to use.  (It's small to conserve some of the bandwidth
    317      * until the end of the loop).  Keep looping until we run out of bandwidth
    318      * or peers that are ready to use it. */
     309    /* First phase of IO.  Tries to distribute bandwidth fairly to keep faster
     310     * peers from starving the others.  Loop through the peers, giving each a
     311     * small chunk of bandwidth.  Keep looping until we run out of bandwidth
     312     * or pweers that can use it */
    319313    n = peerCount;
    320314    i = n ? tr_cryptoWeakRandInt( n ) : 0; /* pick a random starting point */
     
    339333    }
    340334
    341     /* Second phase of IO.  To help us scale well in high bandwidth situations
    342      * such as LANs, enable on-demand IO for peers with bandwidth left to burn.
    343      * This on-demand IO for a peer is enabled until either (1) the peer runs
    344      * out of bandwidth, or (2) the next tr_bandwidthAllocate() call, when we
    345      * start all over again. */
     335    /* Second phase of IO.  To help us scale in high bandwidth situations,
     336     * enable on-demand IO for peers with bandwidth left to burn.
     337     * This on-demand IO is enabled until (1) the peer runs out of bandwidth,
     338     * or (2) the next tr_bandwidthAllocate() call, when we start over again. */
    346339    for( i=0; i<peerCount; ++i )
    347340        if( tr_peerIoHasBandwidthLeft( peers[i], dir ) )
     
    360353                     tr_peerIo      * peerIo )
    361354{
    362     assert( isBandwidth( b ) );
    363     assert( peerIo );
     355    assert( tr_isBandwidth( b ) );
     356    assert( tr_isPeerIo( peerIo ) );
    364357
    365358    tr_ptrArrayInsertSorted( b->peers, peerIo, comparePointers );
     
    370363                        tr_peerIo     * peerIo )
    371364{
    372     assert( isBandwidth( b ) );
    373     assert( peerIo );
     365    assert( tr_isBandwidth( b ) );
     366    assert( tr_isPeerIo( peerIo ) );
    374367
    375368    tr_ptrArrayRemoveSorted( b->peers, peerIo, comparePointers );
     
    385378                   size_t                byteCount )
    386379{
    387     assert( isBandwidth( b ) );
    388     assert( isDirection( dir ) );
     380    assert( tr_isBandwidth( b ) );
     381    assert( tr_isDirection( dir ) );
    389382
    390383    if( b )
     
    403396tr_bandwidthGetRawSpeed( const tr_bandwidth * b, tr_direction dir )
    404397{
    405     assert( isBandwidth( b ) );
    406     assert( isDirection( dir ) );
     398    assert( tr_isBandwidth( b ) );
     399    assert( tr_isDirection( dir ) );
    407400
    408401    return getSpeed( &b->band[dir].raw, HISTORY_MSEC );
     
    412405tr_bandwidthGetPieceSpeed( const tr_bandwidth * b, tr_direction dir )
    413406{
    414     assert( isBandwidth( b ) );
    415     assert( isDirection( dir ) );
     407    assert( tr_isBandwidth( b ) );
     408    assert( tr_isDirection( dir ) );
    416409
    417410    return getSpeed( &b->band[dir].piece, HISTORY_MSEC );
     
    422415                  tr_direction    dir,
    423416                  size_t          byteCount,
    424                   int             isPieceData )
     417                  tr_bool         isPieceData )
    425418{
    426419    struct tr_band * band;
    427420    size_t oldBytesLeft;
    428421
    429     assert( isBandwidth( b ) );
    430     assert( isDirection( dir ) );
     422    assert( tr_isBandwidth( b ) );
     423    assert( tr_isDirection( dir ) );
    431424
    432425    band = &b->band[dir];
  • trunk/libtransmission/bandwidth.h

    r7419 r7446  
    7676void     tr_bandwidthFree             ( tr_bandwidth        * bandwidth );
    7777
     78/** @brief test to see if the pointer refers to a live bandwidth object */
     79tr_bool  tr_isBandwidth               ( const tr_bandwidth  * bandwidth );
     80
    7881/******
    7982*******
     
    101104void    tr_bandwidthSetLimited        ( tr_bandwidth        * bandwidth,
    102105                                        tr_direction          direction,
    103                                         int                   isLimited );
     106                                        tr_bool               isLimited );
    104107
    105108/**
    106109 * @return nonzero if this bandwidth throttles its iobufs' speeds
    107110 */
    108 int    tr_bandwidthIsLimited         ( const tr_bandwidth  * bandwidth,
     111tr_bool tr_bandwidthIsLimited         ( const tr_bandwidth  * bandwidth,
    109112                                        tr_direction          direction );
    110113
     
    146149                                        tr_direction          direction,
    147150                                        size_t                byteCount,
    148                                         int                   isPieceData );
     151                                        tr_bool               isPieceData );
    149152
    150153/******
     
    163166void    tr_bandwidthHonorParentLimits ( tr_bandwidth        * bandwidth,
    164167                                        tr_direction          direction,
    165                                         int                   isEnabled );
     168                                        tr_bool               isEnabled );
    166169
    167170/******
  • trunk/libtransmission/peer-io.c

    r7442 r7446  
    195195#define _isBool(b) (((b)==0 || (b)==1))
    196196
    197 static int
    198 isPeerIo( const tr_peerIo * io )
     197tr_bool
     198tr_isPeerIo( const tr_peerIo * io )
    199199{
    200200    return ( io != NULL )
     
    217217    const tr_direction dir = TR_DOWN;
    218218
    219     assert( isPeerIo( io ) );
     219    assert( tr_isPeerIo( io ) );
    220220
    221221    dbgmsg( io, "libevent says this peer is ready to read" );
     
    288288    const tr_direction dir = TR_UP;
    289289
    290     assert( isPeerIo( io ) );
     290    assert( tr_isPeerIo( io ) );
    291291
    292292    dbgmsg( io, "libevent says this peer is ready to write" );
     
    447447tr_peerIoGetSession( tr_peerIo * io )
    448448{
    449     assert( isPeerIo( io ) );
     449    assert( tr_isPeerIo( io ) );
    450450    assert( io->session );
    451451
     
    457457                           tr_port   * port )
    458458{
    459     assert( isPeerIo( io ) );
     459    assert( tr_isPeerIo( io ) );
    460460
    461461    if( port )
     
    538538                         const uint8_t * hash )
    539539{
    540     assert( isPeerIo( io ) );
     540    assert( tr_isPeerIo( io ) );
    541541
    542542    tr_cryptoSetTorrentHash( io->crypto, hash );
     
    546546tr_peerIoGetTorrentHash( tr_peerIo * io )
    547547{
    548     assert( isPeerIo( io ) );
     548    assert( tr_isPeerIo( io ) );
    549549    assert( io->crypto );
    550550
     
    555555tr_peerIoHasTorrentHash( const tr_peerIo * io )
    556556{
    557     assert( isPeerIo( io ) );
     557    assert( tr_isPeerIo( io ) );
    558558    assert( io->crypto );
    559559
     
    569569                     const uint8_t * peer_id )
    570570{
    571     assert( isPeerIo( io ) );
     571    assert( tr_isPeerIo( io ) );
    572572
    573573    if( ( io->peerIdIsSet = peer_id != NULL ) )
     
    580580tr_peerIoGetPeersId( const tr_peerIo * io )
    581581{
    582     assert( isPeerIo( io ) );
     582    assert( tr_isPeerIo( io ) );
    583583    assert( io->peerIdIsSet );
    584584
     
    594594                     tr_bool     flag )
    595595{
    596     assert( isPeerIo( io ) );
     596    assert( tr_isPeerIo( io ) );
    597597    assert( isFlag( flag ) );
    598598
     
    604604tr_peerIoSupportsFEXT( const tr_peerIo * io )
    605605{
    606     assert( isPeerIo( io ) );
     606    assert( tr_isPeerIo( io ) );
    607607
    608608    return io->fastExtensionSupported;
     
    617617                     tr_bool      flag )
    618618{
    619     assert( isPeerIo( io ) );
     619    assert( tr_isPeerIo( io ) );
    620620    assert( isFlag( flag ) );
    621621
     
    627627tr_peerIoSupportsLTEP( const tr_peerIo * io )
    628628{
    629     assert( isPeerIo( io ) );
     629    assert( tr_isPeerIo( io ) );
    630630
    631631    return io->extendedProtocolSupported;
     
    667667                       tr_bandwidth  * bandwidth )
    668668{
    669     assert( isPeerIo( io ) );
     669    assert( tr_isPeerIo( io ) );
    670670
    671671    if( io->bandwidth )
     
    692692                        int         encryptionMode )
    693693{
    694     assert( isPeerIo( io ) );
     694    assert( tr_isPeerIo( io ) );
    695695    assert( encryptionMode == PEER_ENCRYPTION_NONE
    696696          || encryptionMode == PEER_ENCRYPTION_RC4 );
     
    839839    uint16_t tmp;
    840840
     841    assert( tr_isPeerIo( io ) );
     842
    841843    tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) );
    842844    *setme = ntohs( tmp );
     
    850852    uint32_t tmp;
    851853
     854    assert( tr_isPeerIo( io ) );
     855
    852856    tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) );
    853857    *setme = ntohl( tmp );
     
    859863                size_t            byteCount )
    860864{
    861     uint8_t * tmp = tr_new( uint8_t, byteCount );
    862 
     865    uint8_t * tmp;
     866
     867    assert( tr_isPeerIo( io ) );
     868
     869    tmp = tr_new( uint8_t, byteCount );
    863870    tr_peerIoReadBytes( io, inbuf, tmp, byteCount );
    864871    tr_free( tmp );
     
    880887    int res;
    881888
    882     assert( isPeerIo( io ) );
     889    assert( tr_isPeerIo( io ) );
    883890
    884891    howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch );
     
    907914    int n;
    908915
    909     assert( isPeerIo( io ) );
     916    assert( tr_isPeerIo( io ) );
    910917
    911918    howmuch = tr_bandwidthClamp( io->bandwidth, TR_UP, howmuch );
     
    929936    int ret;
    930937
    931     assert( isPeerIo( io ) );
    932     assert( dir==TR_UP || dir==TR_DOWN );
     938    assert( tr_isPeerIo( io ) );
     939    assert( tr_isDirection( dir ) );
    933940
    934941    if( dir==TR_DOWN )
     
    943950tr_peerIoGetReadBuffer( tr_peerIo * io )
    944951{
    945     assert( isPeerIo( io ) );
     952    assert( tr_isPeerIo( io ) );
    946953
    947954    return io->inbuf;
     
    951958tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir )
    952959{
    953     assert( isPeerIo( io ) );
    954     assert( dir==TR_UP || dir==TR_DOWN );
     960    assert( tr_isPeerIo( io ) );
     961    assert( tr_isDirection( dir ) );
    955962
    956963    return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0;
     
    964971event_enable( tr_peerIo * io, short event )
    965972{
    966     assert( isPeerIo( io ) );
     973    assert( tr_isPeerIo( io ) );
    967974
    968975    if( event & EV_READ )
     
    976983event_disable( struct tr_peerIo * io, short event )
    977984{
    978     assert( isPeerIo( io ) );
     985    assert( tr_isPeerIo( io ) );
    979986
    980987    if( event & EV_READ )
     
    991998                     tr_bool        isEnabled )
    992999{
    993     const short event = dir == TR_UP ? EV_WRITE : EV_READ;
     1000    short event;
     1001
     1002    assert( tr_isPeerIo( io ) );
     1003    assert( tr_isDirection( dir ) );
     1004
     1005    event = dir == TR_UP ? EV_WRITE : EV_READ;
    9941006
    9951007    if( isEnabled )
  • trunk/libtransmission/peer-io.h

    r7441 r7446  
    4444void        tr_peerIoFree       ( tr_peerIo               * io );
    4545
     46tr_bool     tr_isPeerIo         ( const tr_peerIo         * io );
     47
    4648
    4749/**
  • trunk/libtransmission/session.c

    r7404 r7446  
    656656{
    657657    assert( session );
    658     assert( dir==TR_UP || dir==TR_DOWN );
     658    assert( tr_isDirection( dir ) );
    659659
    660660    session->isSpeedLimited[dir] = isLimited;
     
    668668{
    669669    assert( session );
    670     assert( dir==TR_UP || dir==TR_DOWN );
     670    assert( tr_isDirection( dir ) );
    671671
    672672    session->speedLimit[dir] = desiredSpeed;
     
    679679{
    680680    assert( session );
    681     assert( dir==TR_UP || dir==TR_DOWN );
     681    assert( tr_isDirection( dir ) );
    682682
    683683    return session->isSpeedLimited[dir];
     
    689689{
    690690    assert( session );
    691     assert( dir==TR_UP || dir==TR_DOWN );
     691    assert( tr_isDirection( dir ) );
    692692
    693693    return session->speedLimit[dir];
  • trunk/libtransmission/torrent.c

    r7397 r7446  
    143143{
    144144    assert( tor != NULL );
    145     assert( dir==TR_UP || dir==TR_DOWN );
     145    assert( tr_isDirection( dir ) );
    146146    assert( mode==TR_SPEEDLIMIT_GLOBAL || mode==TR_SPEEDLIMIT_SINGLE || mode==TR_SPEEDLIMIT_UNLIMITED  );
    147147
     
    157157{
    158158    assert( tor != NULL );
    159     assert( dir==TR_UP || dir==TR_DOWN );
     159    assert( tr_isDirection( dir ) );
    160160
    161161    return tor->speedLimitMode[dir];
  • trunk/libtransmission/transmission.h

    r7393 r7446  
    14061406                            time_t        doneDate );
    14071407
     1408/** @brief Sanity checker to test that the direction is TR_UP or TR_DOWN */
     1409tr_bool tr_isDirection( tr_direction );
     1410
     1411
    14081412/** @} */
    14091413
  • trunk/libtransmission/utils.c

    r7367 r7446  
    12561256}
    12571257
     1258tr_bool
     1259tr_isDirection( tr_direction dir )
     1260{
     1261    return dir==TR_UP || dir==TR_DOWN;
     1262}
Note: See TracChangeset for help on using the changeset viewer.