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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 
Note: See TracChangeset for help on using the changeset viewer.