Changeset 8686


Ignore:
Timestamp:
Jun 14, 2009, 1:01:46 AM (13 years ago)
Author:
charles
Message:

(trunk libT) avoid an unnecessary malloc/free in tr_strdup_printf()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.c

    r8685 r8686  
    733733tr_strdup_printf( const char * fmt, ... )
    734734{
    735     char *            ret = NULL;
    736     struct evbuffer * buf;
    737     va_list           ap;
    738 
    739     buf = evbuffer_new( );
     735    va_list ap;
     736    char * ret;
     737    size_t len;
     738    char statbuf[2048];
     739
    740740    va_start( ap, fmt );
    741 
    742     if( evbuffer_add_vprintf( buf, fmt, ap ) != -1 )
    743         ret = tr_strdup( EVBUFFER_DATA( buf ) );
    744 
     741    len = evutil_vsnprintf( statbuf, sizeof( statbuf ), fmt, ap );
    745742    va_end( ap );
    746     evbuffer_free( buf );
     743    if( len < sizeof( statbuf ) )
     744        ret = tr_strndup( statbuf, len );
     745    else {
     746        ret = tr_new( char, len + 1 );
     747        va_start( ap, fmt );
     748        evutil_vsnprintf( ret, len + 1, fmt, ap );
     749        va_end( ap );
     750    }
     751
    747752    return ret;
    748753}
Note: See TracChangeset for help on using the changeset viewer.