Changeset 12393


Ignore:
Timestamp:
Apr 27, 2011, 9:38:45 PM (10 years ago)
Author:
jordan
Message:

(trunk libT) heap pruning: use tr_bencToBuf() instead of tr_bencToStr() when building LTEP messages. This saves us from a handful of unnecessary malloc+memcpy+free calls in each instance where the change is made.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-msgs.c

    r12367 r12393  
    789789{
    790790    tr_benc val, *m;
    791     char * buf;
    792     int len;
    793791    bool allow_pex;
    794792    bool allow_metadata_xfer;
     793    struct evbuffer * payload;
    795794    struct evbuffer * out = msgs->outMessages;
    796795    const unsigned char * ipv6 = tr_globalIPv6();
     
    833832        tr_bencDictAddInt( m, "ut_pex", UT_PEX_ID );
    834833
    835     buf = tr_bencToStr( &val, TR_FMT_BENC, &len );
    836 
    837     evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + len );
     834    payload = tr_bencToBuf( &val, TR_FMT_BENC );
     835
     836    evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + evbuffer_get_length( payload ) );
    838837    evbuffer_add_uint8 ( out, BT_LTEP );
    839838    evbuffer_add_uint8 ( out, LTEP_HANDSHAKE );
    840     evbuffer_add       ( out, buf, len );
     839    evbuffer_add_buffer( out, payload );
    841840    pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
    842841    dbgOutMessageLen( msgs );
    843842
    844843    /* cleanup */
     844    evbuffer_free( payload );
    845845    tr_bencFree( &val );
    846     tr_free( buf );
    847846}
    848847
     
    998997        {
    999998            tr_benc tmp;
    1000             int payloadLen;
    1001             char * payload;
     999            struct evbuffer * payload;
    10021000            struct evbuffer * out = msgs->outMessages;
    10031001
     
    10061004            tr_bencDictAddInt( &tmp, "msg_type", METADATA_MSG_TYPE_REJECT );
    10071005            tr_bencDictAddInt( &tmp, "piece", piece );
    1008             payload = tr_bencToStr( &tmp, TR_FMT_BENC, &payloadLen );
    1009             tr_bencFree( &tmp );
     1006            payload = tr_bencToBuf( &tmp, TR_FMT_BENC );
    10101007
    10111008            /* write it out as a LTEP message to our outMessages buffer */
    1012             evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen );
     1009            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + evbuffer_get_length( payload ) );
    10131010            evbuffer_add_uint8 ( out, BT_LTEP );
    10141011            evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
    1015             evbuffer_add       ( out, payload, payloadLen );
     1012            evbuffer_add_buffer( out, payload );
    10161013            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    10171014            dbgOutMessageLen( msgs );
    10181015
    1019             tr_free( payload );
     1016            /* cleanup */
     1017            evbuffer_free( payload );
     1018            tr_bencFree( &tmp );
    10201019        }
    10211020    }
     
    17181717    {
    17191718        tr_benc tmp;
    1720         int payloadLen;
    1721         char * payload;
     1719        struct evbuffer * payload;
    17221720        struct evbuffer * out = msgs->outMessages;
    17231721
     
    17261724        tr_bencDictAddInt( &tmp, "msg_type", METADATA_MSG_TYPE_REQUEST );
    17271725        tr_bencDictAddInt( &tmp, "piece", piece );
    1728         payload = tr_bencToStr( &tmp, TR_FMT_BENC, &payloadLen );
    1729         tr_bencFree( &tmp );
     1726        payload = tr_bencToBuf( &tmp, TR_FMT_BENC );
    17301727
    17311728        dbgmsg( msgs, "requesting metadata piece #%d", piece );
    17321729
    17331730        /* write it out as a LTEP message to our outMessages buffer */
    1734         evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen );
     1731        evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + evbuffer_get_length( payload ) );
    17351732        evbuffer_add_uint8 ( out, BT_LTEP );
    17361733        evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
    1737         evbuffer_add       ( out, payload, payloadLen );
     1734        evbuffer_add_buffer( out, payload );
    17381735        pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    17391736        dbgOutMessageLen( msgs );
    17401737
    1741         tr_free( payload );
     1738        /* cleanup */
     1739        evbuffer_free( payload );
     1740        tr_bencFree( &tmp );
    17421741    }
    17431742}
     
    18111810        {
    18121811            tr_benc tmp;
    1813             int payloadLen;
    1814             char * payload;
     1812            struct evbuffer * payload;
    18151813            struct evbuffer * out = msgs->outMessages;
    18161814
     
    18201818            tr_bencDictAddInt( &tmp, "piece", piece );
    18211819            tr_bencDictAddInt( &tmp, "total_size", msgs->torrent->infoDictLength );
    1822             payload = tr_bencToStr( &tmp, TR_FMT_BENC, &payloadLen );
    1823             tr_bencFree( &tmp );
     1820            payload = tr_bencToBuf( &tmp, TR_FMT_BENC );
    18241821
    18251822            /* write it out as a LTEP message to our outMessages buffer */
    1826             evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen + dataLen );
     1823            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + evbuffer_get_length( payload ) + dataLen );
    18271824            evbuffer_add_uint8 ( out, BT_LTEP );
    18281825            evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
    1829             evbuffer_add       ( out, payload, payloadLen );
     1826            evbuffer_add_buffer( out, payload );
    18301827            evbuffer_add       ( out, data, dataLen );
    18311828            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    18321829            dbgOutMessageLen( msgs );
    18331830
    1834             tr_free( payload );
     1831            evbuffer_free( payload );
     1832            tr_bencFree( &tmp );
    18351833            tr_free( data );
    18361834
     
    18411839        {
    18421840            tr_benc tmp;
    1843             int payloadLen;
    1844             char * payload;
     1841            struct evbuffer * payload;
    18451842            struct evbuffer * out = msgs->outMessages;
    18461843
     
    18491846            tr_bencDictAddInt( &tmp, "msg_type", METADATA_MSG_TYPE_REJECT );
    18501847            tr_bencDictAddInt( &tmp, "piece", piece );
    1851             payload = tr_bencToStr( &tmp, TR_FMT_BENC, &payloadLen );
    1852             tr_bencFree( &tmp );
     1848            payload = tr_bencToBuf( &tmp, TR_FMT_BENC );
    18531849
    18541850            /* write it out as a LTEP message to our outMessages buffer */
    1855             evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + payloadLen );
     1851            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + evbuffer_get_length( payload ) );
    18561852            evbuffer_add_uint8 ( out, BT_LTEP );
    18571853            evbuffer_add_uint8 ( out, msgs->ut_metadata_id );
    1858             evbuffer_add       ( out, payload, payloadLen );
     1854            evbuffer_add_buffer( out, payload );
    18591855            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    18601856            dbgOutMessageLen( msgs );
    18611857
    1862             tr_free( payload );
     1858            evbuffer_free( payload );
     1859            tr_bencFree( &tmp );
    18631860        }
    18641861    }
     
    21932190            int  i;
    21942191            tr_benc val;
    2195             char * benc;
    2196             int bencLen;
    21972192            uint8_t * tmp, *walk;
     2193            struct evbuffer * payload;
    21982194            struct evbuffer * out = msgs->outMessages;
    21992195
     
    22852281
    22862282            /* write the pex message */
    2287             benc = tr_bencToStr( &val, TR_FMT_BENC, &bencLen );
    2288             evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + bencLen );
     2283            payload = tr_bencToBuf( &val, TR_FMT_BENC );
     2284            evbuffer_add_uint32( out, 2 * sizeof( uint8_t ) + evbuffer_get_length( payload ) );
    22892285            evbuffer_add_uint8 ( out, BT_LTEP );
    22902286            evbuffer_add_uint8 ( out, msgs->ut_pex_id );
    2291             evbuffer_add       ( out, benc, bencLen );
     2287            evbuffer_add_buffer( out, payload );
    22922288            pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
    22932289            dbgmsg( msgs, "sending a pex message; outMessage size is now %zu", evbuffer_get_length( out ) );
    22942290            dbgOutMessageLen( msgs );
    22952291
    2296             tr_free( benc );
     2292            evbuffer_free( payload );
    22972293            tr_bencFree( &val );
    22982294        }
Note: See TracChangeset for help on using the changeset viewer.