Changeset 7580


Ignore:
Timestamp:
Jan 2, 2009, 7:56:06 PM (12 years ago)
Author:
charles
Message:

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

Location:
trunk/libtransmission
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bandwidth.c

    r7579 r7580  
    8282
    8383tr_bandwidth*
    84 tr_bandwidthNew( tr_session * session, tr_bandwidth * parent )
    85 {
    86     tr_bandwidth * b = tr_new0( tr_bandwidth, 1 );
     84tr_bandwidthConstruct( tr_bandwidth * b, tr_session * session, tr_bandwidth * parent )
     85{
    8786    b->session = session;
    8887    b->children = TR_PTR_ARRAY_INIT;
    89     b->peers = TR_PTR_ARRAY_INIT;
    9088    b->magicNumber = MAGIC_NUMBER;
    9189    b->band[TR_UP].honorParentLimits = TRUE;
     
    9593}
    9694
    97 void
    98 tr_bandwidthFree( tr_bandwidth * b )
     95tr_bandwidth*
     96tr_bandwidthDestruct( tr_bandwidth * b )
    9997{
    10098    assert( tr_isBandwidth( b ) );
    10199
    102100    tr_bandwidthSetParent( b, NULL );
    103     tr_ptrArrayDestruct( &b->peers, NULL );
    104101    tr_ptrArrayDestruct( &b->children, NULL );
    105102    b->magicNumber = 0xDEAD;
    106     tr_free( b );
     103
     104    return b;
    107105}
    108106
     
    134132        b->parent = parent;
    135133    }
    136 }
    137 
    138 void
    139 tr_bandwidthHonorParentLimits( tr_bandwidth  * b,
    140                                tr_direction    dir,
    141                                tr_bool         honorParentLimits )
    142 {
    143     assert( tr_isBandwidth( b ) );
    144     assert( tr_isDirection( dir ) );
    145 
    146     b->band[dir].honorParentLimits = honorParentLimits;
    147134}
    148135
     
    180167    }
    181168
    182     /* traverse & repeat for the subtree */
    183     {
    184         int i;
    185         const int n = TR_PTR_ARRAY_LENGTH( &b->peers );
    186         for( i=0; i<n; ++i )
    187             tr_ptrArrayAppend( peer_pool, tr_ptrArrayNth( &b->peers, i ) );
    188     }
     169    /* add this bandwidth's peer, if any, to the peer pool */
     170    if( b->peer != NULL )
     171        tr_ptrArrayAppend( peer_pool, b->peer );
    189172
    190173#ifdef DEBUG_DIRECTION
     
    193176#endif
    194177
    195     /* all children should reallocate too */
     178    /* traverse & repeat for the subtree */
    196179    if( 1 ) {
    197180        int i;
     
    217200     * 2. accumulate an array of all the peerIos from b and its subtree. */
    218201    allocateBandwidth( b, dir, period_msec, &tmp );
    219     peers = (struct tr_peerIo**) tr_ptrArrayPeek( &tmp, &peerCount );
     202    peers = (struct tr_peerIo**) TR_PTR_ARRAY_DATA( &tmp );
     203    peerCount = TR_PTR_ARRAY_LENGTH( &tmp );
    220204
    221205    /* Stop all peers from listening for the socket to be ready for IO.
     
    229213     * and/or peers that can use it */
    230214    n = peerCount;
     215#ifdef DEBUG_DIRECTION
     216if( dir == DEBUG_DIRECTION ) fprintf( stderr, "bandwidth.c: allocate: number of peerIos to go round-robin: %d\n", n );
     217#endif
    231218    i = n ? tr_cryptoWeakRandInt( n ) : 0; /* pick a random starting point */
    232219    while( n > 1 )
    233220    {
    234221        const int increment = 1024;
    235         const int bytesUsed = tr_peerIoFlush( peers[i], dir, increment);
     222        const int bytesUsed = tr_peerIoFlush( peers[i], dir, increment );
    236223
    237224        if( bytesUsed == increment )
     
    261248}
    262249
    263 /***
    264 ****
    265 ***/
    266 
    267250void
    268 tr_bandwidthAddPeer( tr_bandwidth   * b,
    269                      tr_peerIo      * peerIo )
    270 {
    271     assert( tr_isBandwidth( b ) );
    272     assert( tr_isPeerIo( peerIo ) );
    273 
    274 }
    275 
    276 void
    277 tr_bandwidthRemovePeer( tr_bandwidth  * b,
    278                         tr_peerIo     * peerIo )
    279 {
    280     assert( tr_isBandwidth( b ) );
    281     assert( tr_isPeerIo( peerIo ) );
    282 
    283     tr_ptrArrayRemoveSorted( &b->peers, peerIo, comparePointers );
     251tr_bandwidthSetPeer( tr_bandwidth * b, tr_peerIo * peer )
     252{
     253    assert( tr_isBandwidth( b ) );
     254    assert( ( peer == NULL ) || tr_isPeerIo( peer ) );
     255
     256    b->peer = peer;
    284257}
    285258
  • trunk/libtransmission/bandwidth.h

    r7579 r7580  
    2020#include "transmission.h"
    2121#include "ptrarray.h"
     22#include "utils.h" /* tr_new(), tr_free() */
    2223
    2324struct tr_peerIo;
     
    103104    tr_session * session;
    104105    tr_ptrArray children; /* struct tr_bandwidth */
    105     tr_ptrArray peers; /* tr_peerIo */
     106    struct tr_peerIo * peer;
    106107}
    107108tr_bandwidth;
     
    112113**/
    113114
     115tr_bandwidth* tr_bandwidthConstruct( tr_bandwidth * bandwidth,
     116                                     tr_session   * session,
     117                                     tr_bandwidth * parent );
     118
    114119/** @brief create a new tr_bandwidth object */
    115 tr_bandwidth*
    116          tr_bandwidthNew              ( tr_session          * session,
    117                                         tr_bandwidth        * parent );
    118 
    119 /** @brief destroy a tr_bandwidth object */
    120 void     tr_bandwidthFree             ( tr_bandwidth        * bandwidth );
     120static inline tr_bandwidth* tr_bandwidthNew( tr_session * session, tr_bandwidth * parent )
     121{
     122    return tr_bandwidthConstruct( tr_new0( tr_bandwidth, 1 ), session, parent );
     123}
     124
     125tr_bandwidth* tr_bandwidthDestruct( tr_bandwidth * bandwidth );
     126
     127/** @brief free a tr_bandwidth object */
     128static inline void tr_bandwidthFree( tr_bandwidth * bandwidth )
     129{
     130    tr_free( tr_bandwidthDestruct( bandwidth ) );
     131}
    121132
    122133/** @brief test to see if the pointer refers to a live bandwidth object */
     
    220231 * in that particular case we want to ignore the global speed limit...
    221232 */
    222 void    tr_bandwidthHonorParentLimits ( tr_bandwidth        * bandwidth,
    223                                         tr_direction          direction,
    224                                         tr_bool               isEnabled );
    225 
    226 /******
    227 *******
    228 ******/
    229 
    230 /**
    231  * @brief add a tr_peerIo to this bandwidth's list.
    232  * They will be notified when more bandwidth is made available for them to consume.
    233  */
    234 void tr_bandwidthAddPeer( tr_bandwidth        * bandwidth,
     233static inline void tr_bandwidthHonorParentLimits ( tr_bandwidth        * bandwidth,
     234                                                   tr_direction          direction,
     235                                                   tr_bool               isEnabled )
     236{
     237    assert( tr_isBandwidth( bandwidth ) );
     238    assert( tr_isDirection( direction ) );
     239
     240    bandwidth->band[direction].honorParentLimits = isEnabled;
     241}
     242
     243/******
     244*******
     245******/
     246
     247void tr_bandwidthSetPeer( tr_bandwidth        * bandwidth,
    235248                          struct tr_peerIo    * peerIo );
    236249
    237 /**
    238  * @brief remove a peer-io from this bandwidth's list.
    239  */
    240 void tr_bandwidthRemovePeer( tr_bandwidth        * bandwidth,
    241                              struct tr_peerIo    * peerIo );
    242 
    243250#endif
  • trunk/libtransmission/peer-io.c

    r7579 r7580  
    9393        const size_t overhead = getPacketOverhead( payload );
    9494
    95         tr_bandwidthUsed( io->bandwidth, TR_UP, payload, next->isPieceData );
     95        tr_bandwidthUsed( &io->bandwidth, TR_UP, payload, next->isPieceData );
    9696
    9797        if( overhead > 0 )
    98             tr_bandwidthUsed( io->bandwidth, TR_UP, overhead, FALSE );
     98            tr_bandwidthUsed( &io->bandwidth, TR_UP, overhead, FALSE );
    9999
    100100        if( io->didWrite )
     
    134134            assert( tr_isPeerIo( io ) );
    135135
    136             if( io->bandwidth != NULL )
    137             {
    138                 if( piece )
    139                     tr_bandwidthUsed( io->bandwidth, TR_DOWN, piece, TRUE );
    140 
    141                 if( used != piece )
    142                     tr_bandwidthUsed( io->bandwidth, TR_DOWN, used - piece, FALSE );
    143             }
     136            if( piece )
     137                tr_bandwidthUsed( &io->bandwidth, TR_DOWN, piece, TRUE );
     138
     139            if( used != piece )
     140                tr_bandwidthUsed( &io->bandwidth, TR_DOWN, used - piece, FALSE );
    144141
    145142            switch( ret )
     
    165162}
    166163
    167 #define _isBool(b) (((b)==0 || (b)==1))
    168 
    169164tr_bool
    170165tr_isPeerIo( const tr_peerIo * io )
     
    172167    return ( io != NULL )
    173168        && ( io->magicNumber == MAGIC_NUMBER )
     169        && ( tr_isBandwidth( &io->bandwidth ) )
    174170        && ( tr_isAddress( &io->addr ) )
    175         && ( _isBool( io->isEncrypted ) )
    176         && ( _isBool( io->isIncoming ) )
    177         && ( _isBool( io->peerIdIsSet ) )
    178         && ( _isBool( io->extendedProtocolSupported ) )
    179         && ( _isBool( io->fastExtensionSupported ) );
     171        && ( tr_isBool( io->isEncrypted ) )
     172        && ( tr_isBool( io->isIncoming ) )
     173        && ( tr_isBool( io->peerIdIsSet ) )
     174        && ( tr_isBool( io->extendedProtocolSupported ) )
     175        && ( tr_isBool( io->fastExtensionSupported ) );
    180176}
    181177
     
    194190
    195191    howmuch = curlen >= max ? 0 : max - curlen;
    196     howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch );
     192    howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch );
    197193
    198194    assert( tr_isPeerIo( io ) );
     
    279275    /* Write as much as possible, since the socket is non-blocking, write() will
    280276     * return if it can't write any more data without blocking */
    281     howmuch = tr_bandwidthClamp( io->bandwidth, dir, EVBUFFER_LENGTH( io->outbuf ) );
     277    howmuch = tr_bandwidthClamp( &io->bandwidth, dir, EVBUFFER_LENGTH( io->outbuf ) );
    282278
    283279    /* if we don't have any bandwidth left, stop writing */
     
    336332static tr_peerIo*
    337333tr_peerIoNew( tr_session       * session,
     334              tr_bandwidth     * parent,
    338335              const tr_address * addr,
    339336              tr_port            port,
     
    358355    io->inbuf = evbuffer_new( );
    359356    io->outbuf = evbuffer_new( );
     357    tr_bandwidthConstruct( &io->bandwidth, session, parent );
     358    tr_bandwidthSetPeer( &io->bandwidth, io );
     359    dbgmsg( io, "bandwidth is %p; its parent is %p", &io->bandwidth, parent );
    360360
    361361    event_set( &io->event_read, io->socket, EV_READ, event_read_cb, io );
     
    364364    __tr_list_init( &io->outbuf_datatypes );
    365365
    366     tr_peerIoSetBandwidth( io, session->bandwidth );
    367 
    368366    return io;
    369367}
    370368
    371369tr_peerIo*
    372 tr_peerIoNewIncoming( tr_session       * session,
    373                       const tr_address * addr,
    374                       tr_port            port,
    375                       int                socket )
     370tr_peerIoNewIncoming( tr_session        * session,
     371                      tr_bandwidth      * parent,
     372                      const tr_address  * addr,
     373                      tr_port             port,
     374                      int                 socket )
    376375{
    377376    assert( session );
     
    379378    assert( socket >= 0 );
    380379
    381     return tr_peerIoNew( session, addr, port, NULL, 1, socket );
     380    return tr_peerIoNew( session, parent, addr, port, NULL, 1, socket );
    382381}
    383382
    384383tr_peerIo*
    385 tr_peerIoNewOutgoing( tr_session       * session,
    386                       const tr_address * addr,
    387                       tr_port            port,
    388                       const uint8_t    * torrentHash )
     384tr_peerIoNewOutgoing( tr_session        * session,
     385                      tr_bandwidth      * parent,
     386                      const tr_address  * addr,
     387                      tr_port             port,
     388                      const uint8_t     * torrentHash )
    389389{
    390390    int socket;
     
    398398    return socket < 0
    399399           ? NULL
    400            : tr_peerIoNew( session, addr, port, torrentHash, 0, socket );
     400           : tr_peerIoNew( session, parent, addr, port, torrentHash, 0, socket );
    401401}
    402402
     
    415415    event_del( &io->event_read );
    416416    event_del( &io->event_write );
    417     tr_peerIoSetBandwidth( io, NULL );
     417    tr_bandwidthDestruct( &io->bandwidth );
    418418    evbuffer_free( io->outbuf );
    419419    evbuffer_free( io->inbuf );
     
    438438}
    439439
    440 tr_session*
    441 tr_peerIoGetSession( tr_peerIo * io )
    442 {
    443     assert( tr_isPeerIo( io ) );
    444     assert( io->session );
    445 
    446     return io->session;
    447 }
    448 
    449440const tr_address*
    450441tr_peerIoGetAddress( const tr_peerIo * io,
     
    471462}
    472463
    473 const char*
    474 tr_peerIoGetAddrStr( const tr_peerIo * io )
    475 {
    476     return tr_peerIoAddrStr( &io->addr, io->port );
    477 }
    478 
    479464void
    480465tr_peerIoSetIOFuncs( tr_peerIo        * io,
     
    490475}
    491476
    492 tr_bool
    493 tr_peerIoIsIncoming( const tr_peerIo * c )
    494 {
    495     return c->isIncoming != 0;
    496 }
    497 
    498477int
    499478tr_peerIoReconnect( tr_peerIo * io )
     
    504483        tr_netClose( io->socket );
    505484
    506     io->socket = tr_netOpenTCP( io->session, &io->addr, io->port );
    507 
     485    io->socket = tr_netOpenTCP( io->session, &io->addr, io->port );
    508486    if( io->socket >= 0 )
    509487    {
    510         tr_bandwidth * bandwidth = io->bandwidth;
    511         tr_peerIoSetBandwidth( io, NULL );
    512488        tr_netSetTOS( io->socket, io->session->peerSocketTOS );
    513         tr_peerIoSetBandwidth( io, bandwidth );
    514489        return 0;
    515490    }
     
    565540}
    566541
    567 const uint8_t*
    568 tr_peerIoGetPeersId( const tr_peerIo * io )
    569 {
    570     assert( tr_isPeerIo( io ) );
    571     assert( io->peerIdIsSet );
    572 
    573     return io->peerId;
    574 }
    575 
    576542/**
    577543***
     
    583549{
    584550    assert( tr_isPeerIo( io ) );
    585     assert( _isBool( flag ) );
     551    assert( tr_isBool( flag ) );
    586552
    587553    dbgmsg( io, "setting FEXT support flag to %d", (flag!=0) );
     
    589555}
    590556
    591 tr_bool
    592 tr_peerIoSupportsFEXT( const tr_peerIo * io )
    593 {
    594     assert( tr_isPeerIo( io ) );
    595 
    596     return io->fastExtensionSupported;
    597 }
    598 
    599 /**
    600 ***
    601 **/
    602 
    603557void
    604558tr_peerIoEnableLTEP( tr_peerIo  * io,
     
    606560{
    607561    assert( tr_isPeerIo( io ) );
    608     assert( _isBool( flag ) );
     562    assert( tr_isBool( flag ) );
    609563
    610564    dbgmsg( io, "setting LTEP support flag to %d", (flag!=0) );
    611565    io->extendedProtocolSupported = flag;
    612 }
    613 
    614 tr_bool
    615 tr_peerIoSupportsLTEP( const tr_peerIo * io )
    616 {
    617     assert( tr_isPeerIo( io ) );
    618 
    619     return io->extendedProtocolSupported;
    620566}
    621567
     
    632578     * It's okay to tweak this as needed */
    633579    const double maxBlockSize = 16 * 1024; /* 16 KiB is from BT spec */
    634     const double currentSpeed = tr_bandwidthGetPieceSpeed( io->bandwidth, TR_UP );
     580    const double currentSpeed = tr_bandwidthGetPieceSpeed( &io->bandwidth, TR_UP );
    635581    const double period = 20; /* arbitrary */
    636582    const double numBlocks = 5.5; /* the 5 is arbitrary; the .5 is to leave room for messages */
     
    651597}
    652598
    653 void
    654 tr_peerIoSetBandwidth( tr_peerIo     * io,
    655                        tr_bandwidth  * bandwidth )
    656 {
    657     assert( tr_isPeerIo( io ) );
    658 
    659     if( io->bandwidth )
    660         tr_bandwidthRemovePeer( io->bandwidth, io );
    661 
    662     io->bandwidth = bandwidth;
    663 
    664     if( io->bandwidth )
    665         tr_bandwidthAddPeer( io->bandwidth, io );
    666 }
    667 
    668599/**
    669600***
    670601**/
    671602
    672 tr_crypto*
    673 tr_peerIoGetCrypto( tr_peerIo * c )
    674 {
    675     return c->crypto;
    676 }
    677 
    678603void
    679604tr_peerIoSetEncryption( tr_peerIo * io,
     
    682607    assert( tr_isPeerIo( io ) );
    683608    assert( encryptionMode == PEER_ENCRYPTION_NONE
    684           || encryptionMode == PEER_ENCRYPTION_RC4 );
     609         || encryptionMode == PEER_ENCRYPTION_RC4 );
    685610
    686611    io->encryptionMode = encryptionMode;
    687 }
    688 
    689 int
    690 tr_peerIoIsEncrypted( const tr_peerIo * io )
    691 {
    692     return io != NULL && io->encryptionMode == PEER_ENCRYPTION_RC4;
    693612}
    694613
     
    724643{
    725644    const size_t n = EVBUFFER_LENGTH( buf );
    726 
    727645    tr_peerIoWrite( io, EVBUFFER_DATA( buf ), n, isPieceData );
    728646    evbuffer_drain( buf, n );
     
    816734    int res = 0;
    817735
    818     if(( howmuch = tr_bandwidthClamp( io->bandwidth, TR_DOWN, howmuch )))
     736    if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_DOWN, howmuch )))
    819737    {
    820738        int e;
     
    846764    int n;
    847765
    848     if(( howmuch = tr_bandwidthClamp( io->bandwidth, TR_UP, howmuch )))
     766    if(( howmuch = tr_bandwidthClamp( &io->bandwidth, TR_UP, howmuch )))
    849767    {
    850768        int e;
     
    870788tr_peerIoFlush( tr_peerIo  * io, tr_direction dir, size_t limit )
    871789{
    872     int ret;
     790    int bytesUsed;
    873791
    874792    assert( tr_isPeerIo( io ) );
     
    876794
    877795    if( dir==TR_DOWN )
    878         ret = tr_peerIoTryRead( io, limit );
     796        bytesUsed = tr_peerIoTryRead( io, limit );
    879797    else
    880         ret = tr_peerIoTryWrite( io, limit );
    881 
    882     return ret;
     798        bytesUsed = tr_peerIoTryWrite( io, limit );
     799
     800    dbgmsg( io, "flushing peer-io, direction %d, limit %zu, bytesUsed %d", (int)dir, limit, bytesUsed );
     801    return bytesUsed;
    883802}
    884803
     
    929848        event_disable( io, event );
    930849}
    931 
    932 tr_bool
    933 tr_peerIoHasBandwidthLeft( const tr_peerIo  * io,
    934                            tr_direction       dir )
    935 {
    936     assert( tr_isPeerIo( io ) );
    937 
    938     return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0;
    939 }
    940 
  • trunk/libtransmission/peer-io.h

    r7579 r7580  
    2727
    2828#include "transmission.h"
     29#include "bandwidth.h"
    2930#include "list.h" /* __tr_list */
    3031#include "net.h" /* tr_address */
     
    5859typedef struct tr_peerIo
    5960{
    60     tr_bool            isEncrypted;
    61     tr_bool            isIncoming;
    62     tr_bool            peerIdIsSet;
    63     tr_bool            extendedProtocolSupported;
    64     tr_bool            fastExtensionSupported;
    65 
    66     int                magicNumber;
    67 
    68     uint8_t            encryptionMode;
    69     tr_port            port;
    70     int                socket;
    71 
    72     uint8_t            peerId[SHA_DIGEST_LENGTH];
    73     time_t             timeCreated;
    74 
    75     tr_session       * session;
    76 
    77     tr_address         addr;
    78 
    79     tr_can_read_cb     canRead;
    80     tr_did_write_cb    didWrite;
    81     tr_net_error_cb    gotError;
    82     void *             userData;
    83 
    84     struct tr_bandwidth * bandwidth;
     61    tr_bool               isEncrypted;
     62    tr_bool               isIncoming;
     63    tr_bool               peerIdIsSet;
     64    tr_bool               extendedProtocolSupported;
     65    tr_bool               fastExtensionSupported;
     66
     67    int                   magicNumber;
     68
     69    uint8_t               encryptionMode;
     70    tr_port               port;
     71    int                   socket;
     72
     73    uint8_t               peerId[SHA_DIGEST_LENGTH];
     74    time_t                timeCreated;
     75
     76    tr_session          * session;
     77
     78    tr_address            addr;
     79
     80    tr_can_read_cb        canRead;
     81    tr_did_write_cb       didWrite;
     82    tr_net_error_cb       gotError;
     83    void *                userData;
     84
     85    struct tr_bandwidth   bandwidth;
    8586    struct tr_crypto    * crypto;
    8687
    87     struct evbuffer  * inbuf;
    88     struct evbuffer  * outbuf;
    89     struct __tr_list   outbuf_datatypes; /* struct tr_datatype */
    90 
    91     struct event       event_read;
    92     struct event       event_write;
     88    struct evbuffer     * inbuf;
     89    struct evbuffer     * outbuf;
     90    struct __tr_list      outbuf_datatypes; /* struct tr_datatype */
     91
     92    struct event          event_read;
     93    struct event          event_write;
    9394}
    9495tr_peerIo;
     
    99100
    100101tr_peerIo*  tr_peerIoNewOutgoing( tr_session              * session,
     102                                  struct tr_bandwidth     * parent,
    101103                                  const struct tr_address * addr,
    102104                                  tr_port                   port,
     
    104106
    105107tr_peerIo*  tr_peerIoNewIncoming( tr_session              * session,
     108                                  struct tr_bandwidth     * parent,
    106109                                  const struct tr_address * addr,
    107110                                  tr_port                   port,
     
    119122void        tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag );
    120123
    121 tr_bool     tr_peerIoSupportsLTEP( const tr_peerIo * io );
     124static inline tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io )
     125{
     126    assert( tr_isPeerIo( io ) );
     127
     128    return io->extendedProtocolSupported;
     129}
    122130
    123131void        tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag );
    124132
    125 tr_bool     tr_peerIoSupportsFEXT( const tr_peerIo * io );
    126 
    127 /**
    128 ***
    129 **/
    130 
    131 tr_session* tr_peerIoGetSession ( tr_peerIo * io );
     133static inline tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io )
     134{
     135    assert( tr_isPeerIo( io ) );
     136
     137    return io->fastExtensionSupported;
     138}
     139
     140/**
     141***
     142**/
     143
     144static inline tr_session* tr_peerIoGetSession ( tr_peerIo * io )
     145{
     146    assert( tr_isPeerIo( io ) );
     147    assert( io->session );
     148
     149    return io->session;
     150}
    132151
    133152const char* tr_peerIoAddrStr( const struct tr_address * addr,
    134153                              tr_port                   port );
    135154
    136 const char* tr_peerIoGetAddrStr( const tr_peerIo * io );
     155static inline const char* tr_peerIoGetAddrStr( const tr_peerIo * io )
     156{
     157    return tr_peerIoAddrStr( &io->addr, io->port );
     158}
    137159
    138160const struct tr_address * tr_peerIoGetAddress( const tr_peerIo * io,
     
    148170int                  tr_peerIoReconnect( tr_peerIo * io );
    149171
    150 tr_bool              tr_peerIoIsIncoming( const tr_peerIo * io );
     172static inline tr_bool tr_peerIoIsIncoming( const tr_peerIo * io )
     173{
     174    return io->isIncoming;
     175}
    151176
    152177static inline int    tr_peerIoGetAge( const tr_peerIo * io )
     
    163188                                          const uint8_t * peer_id );
    164189
    165 const uint8_t*       tr_peerIoGetPeersId( const tr_peerIo * io );
     190static inline const uint8_t* tr_peerIoGetPeersId( const tr_peerIo * io )
     191{
     192    assert( tr_isPeerIo( io ) );
     193    assert( io->peerIdIsSet );
     194
     195    return io->peerId;
     196}
    166197
    167198/**
     
    192223**/
    193224
    194 struct tr_crypto* tr_peerIoGetCrypto( tr_peerIo * io );
     225static inline struct tr_crypto * tr_peerIoGetCrypto( tr_peerIo * io )
     226{
     227    return io->crypto;
     228}
    195229
    196230typedef enum
     
    205239                                  int         encryptionMode );
    206240
    207 int       tr_peerIoIsEncrypted( const tr_peerIo * io );
     241static inline tr_bool tr_peerIoIsEncrypted( const tr_peerIo * io )
     242{
     243    return ( io != NULL ) && ( io->encryptionMode == PEER_ENCRYPTION_RC4 );
     244}
    208245
    209246void      tr_peerIoWriteBytes( tr_peerIo *       io,
     
    275312size_t    tr_peerIoGetWriteBufferSpace( const tr_peerIo * io );
    276313
    277 void      tr_peerIoSetBandwidth( tr_peerIo            * io,
    278                                  struct tr_bandwidth  * bandwidth );
     314static inline void tr_peerIoSetParent( tr_peerIo            * io,
     315                                       struct tr_bandwidth  * parent )
     316{
     317    assert( tr_isPeerIo( io ) );
     318
     319    tr_bandwidthSetParent( &io->bandwidth, parent );
     320}
    279321
    280322void      tr_peerIoBandwidthUsed( tr_peerIo           * io,
     
    283325                                  int                   isPieceData );
    284326
    285 tr_bool   tr_peerIoHasBandwidthLeft( const tr_peerIo  * io,
    286                                      tr_direction       dir );
     327static inline tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo  * io,
     328                                                 tr_direction       dir )
     329{
     330    assert( tr_isPeerIo( io ) );
     331
     332    return tr_bandwidthClamp( &io->bandwidth, dir, 1024 ) > 0;
     333}
     334
     335static inline double tr_peerIoGetPieceSpeed( const tr_peerIo * io, tr_direction dir )
     336{
     337    assert( tr_isPeerIo( io ) );
     338    assert( tr_isDirection( dir ) );
     339
     340    return tr_bandwidthGetPieceSpeed( &io->bandwidth, dir );
     341}
    287342
    288343/**
  • trunk/libtransmission/peer-mgr-private.h

    r7404 r7580  
    7373    struct tr_peermsgs     * msgs;
    7474    tr_publisher_tag         msgsTag;
    75 
    76     struct tr_bandwidth    * bandwidth;
    7775}
    7876tr_peer;
  • trunk/libtransmission/peer-mgr.c

    r7578 r7580  
    319319
    320320static tr_peer*
    321 peerConstructor( tr_torrent * tor, const tr_address * addr )
     321peerConstructor( const tr_address * addr )
    322322{
    323323    tr_peer * p;
    324324    p = tr_new0( tr_peer, 1 );
    325325    p->addr = *addr;
    326     p->bandwidth = tr_bandwidthNew( tor->session, tor->bandwidth );
    327326    return p;
    328327}
     
    340339    if( peer == NULL )
    341340    {
    342         peer = peerConstructor( torrent->tor, addr );
     341        peer = peerConstructor( addr );
    343342        tr_ptrArrayInsertSorted( &torrent->peers, peer, peerCompare );
    344343    }
     
    363362    tr_bitfieldFree( peer->blame );
    364363    tr_free( peer->client );
    365 
    366     tr_bandwidthFree( peer->bandwidth );
    367364
    368365    tr_free( peer );
     
    12711268                peer->port = port;
    12721269                peer->io = tr_handshakeStealIO( handshake );
     1270                tr_peerIoSetParent( peer->io, t->tor->bandwidth );
    12731271                tr_peerMsgsNew( t->tor, peer, peerCallbackFunc, t, &peer->msgsTag );
    1274                 tr_peerIoSetBandwidth( io, peer->bandwidth );
    12751272
    12761273                success = TRUE;
     
    13101307        tr_handshake * handshake;
    13111308
    1312         io = tr_peerIoNewIncoming( manager->session, addr, port, socket );
     1309        io = tr_peerIoNewIncoming( manager->session, manager->session->bandwidth, addr, port, socket );
    13131310
    13141311        handshake = tr_handshakeNew( io,
     
    18181815tr_peerGetPieceSpeed( const tr_peer * peer, tr_direction direction )
    18191816{
    1820     assert( peer );
    1821     assert( direction==TR_CLIENT_TO_PEER || direction==TR_PEER_TO_CLIENT );
    1822 
    1823     return tr_bandwidthGetPieceSpeed( peer->bandwidth, direction );
     1817    return peer->io ? tr_peerIoGetPieceSpeed( peer->io, direction ) : 0.0;
    18241818}
    18251819
     
    23162310                   tr_peerIoAddrStr( &atom->addr, atom->port ) );
    23172311
    2318             io = tr_peerIoNewOutgoing( mgr->session, &atom->addr, atom->port, t->hash );
     2312            io = tr_peerIoNewOutgoing( mgr->session, mgr->session->bandwidth, &atom->addr, atom->port, t->hash );
     2313
    23192314            if( io == NULL )
    23202315            {
  • trunk/libtransmission/session.c

    r7553 r7580  
    4141#include "web.h"
    4242#include "crypto.h"
     43
     44#define dbgmsg( ... ) \
     45    do { \
     46        if( tr_deepLoggingIsActive( ) ) \
     47            tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
     48    } while( 0 )
    4349
    4450static tr_port
     
    369375    session->lock = tr_lockNew( );
    370376    session->tag = tr_strdup( tag );
     377    dbgmsg( "tr_sessionInit: the session's top-level bandwidth object is %p", session->bandwidth );
    371378
    372379    tr_bencInitDict( &settings, 0 );
     
    800807
    801808#define SHUTDOWN_MAX_SECONDS 30
    802 
    803 #define dbgmsg( ... ) \
    804     do { \
    805         if( tr_deepLoggingIsActive( ) ) \
    806             tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \
    807     } while( 0 )
    808809
    809810void
  • trunk/libtransmission/transmission.h

    r7578 r7580  
    14181418static inline tr_bool tr_isDirection( tr_direction d ) { return d==TR_UP || d==TR_DOWN; }
    14191419
     1420/** @brief Sanity checker to test that a bool is TRUE or FALSE */
     1421static inline tr_bool tr_isBool( tr_bool b ) { return b==1 || b==0; }
     1422
    14201423/** @} */
    14211424
Note: See TracChangeset for help on using the changeset viewer.