Changeset 7550


Ignore:
Timestamp:
Dec 30, 2008, 8:40:48 PM (12 years ago)
Author:
charles
Message:

(trunk libT) patch from wereHamster to recycle the zlib deflate stream s.t. we don't have to keep reallocating it

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/rpc-server.c

    r7549 r7550  
    5858    char *             whitelistStr;
    5959    tr_list *          whitelist;
     60
     61#ifdef HAVE_ZLIB
     62    z_stream           stream;
     63#endif
    6064};
    6165
     
    223227static void
    224228add_response( struct evhttp_request * req,
     229              struct tr_rpc_server *  server,
    225230              struct evbuffer *       out,
    226231              const void *            content,
     
    241246    {
    242247        int state;
    243         z_stream stream;
    244 
    245         stream.zalloc = (alloc_func) Z_NULL;
    246         stream.zfree = (free_func) Z_NULL;
    247         stream.opaque = (voidpf) Z_NULL;
    248         deflateInit( &stream, Z_BEST_COMPRESSION );
    249 
    250         stream.next_in = (Bytef*) content;
    251         stream.avail_in = content_len;
     248
     249        server->stream.next_in = (Bytef*) content;
     250        server->stream.avail_in = content_len;
    252251
    253252        /* allocate space for the raw data and call deflate() just once --
     
    255254         * so it's okay to let deflate() run out of output buffer space */
    256255        evbuffer_expand( out, content_len );
    257         stream.next_out = EVBUFFER_DATA( out );
    258         stream.avail_out = content_len;
    259 
    260         state = deflate( &stream, Z_FINISH );
     256        server->stream.next_out = EVBUFFER_DATA( out );
     257        server->stream.avail_out = content_len;
     258
     259        state = deflate( &server->stream, Z_FINISH );
    261260
    262261        if( state == Z_STREAM_END )
    263262        {
    264             EVBUFFER_LENGTH( out ) = content_len - stream.avail_out;
     263            EVBUFFER_LENGTH( out ) = content_len - server->stream.avail_out;
    265264
    266265            /* http://carsten.codimi.de/gzip.yaws/
     
    288287        }
    289288
    290         deflateEnd( &stream );
     289        deflateReset( &server->stream );
    291290    }
    292291#endif
     
    295294static void
    296295serve_file( struct evhttp_request * req,
     296            struct tr_rpc_server *  server,
    297297            const char *            filename )
    298298{
     
    324324            evhttp_add_header( req->output_headers, "Content-Type",
    325325                               mimetype_guess( filename ) );
    326             add_response( req, out, content, content_len );
     326            add_response( req, server, out, content, content_len );
    327327            evhttp_send_reply( req, HTTP_OK, "OK", out );
    328328
     
    368368                       subpath && *subpath ? subpath : "index.html" );
    369369
    370         serve_file( req, filename );
     370        serve_file( req, server, filename );
    371371
    372372        tr_free( filename );
     
    401401
    402402    buf = tr_getBuffer( );
    403     add_response( req, buf, out, len );
     403    add_response( req, server, buf, out, len );
    404404    evhttp_add_header( req->output_headers, "Content-Type",
    405405                       "application/json; charset=UTF-8" );
     
    692692    while(( tmp = tr_list_pop_front( &s->whitelist )))
    693693        tr_free( tmp );
     694#ifdef HAVE_ZLIB
     695    deflateEnd( &s->stream );
     696#endif
    694697    tr_free( s->whitelistStr );
    695698    tr_free( s->username );
     
    726729    s->isEnabled = isEnabled != 0;
    727730    tr_rpcSetWhitelist( s, whitelist ? whitelist : "127.0.0.1" );
     731
     732#ifdef HAVE_ZLIB
     733    s->stream.zalloc = (alloc_func) Z_NULL;
     734    s->stream.zfree = (free_func) Z_NULL;
     735    s->stream.opaque = (voidpf) Z_NULL;
     736    deflateInit( &s->stream, Z_BEST_COMPRESSION );
     737#endif
     738
    728739    if( isEnabled )
    729740        tr_runInEventThread( session, startServer, s );
Note: See TracChangeset for help on using the changeset viewer.