From 798bac0fcbd0539ea09aa24f497f955418e90bf0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 29 Dec 2008 19:01:47 +0000 Subject: [PATCH] (trunk libT) omit unnecessary malloc/free calls in peer-io --- libtransmission/inout.c | 2 +- libtransmission/peer-io.c | 26 +++++++++++++++++--------- libtransmission/platform.h | 2 ++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/libtransmission/inout.c b/libtransmission/inout.c index fe03ea653..6780e94ab 100644 --- a/libtransmission/inout.c +++ b/libtransmission/inout.c @@ -229,7 +229,7 @@ recalculateHash( const tr_torrent * tor, while( bytesLeft ) { - uint8_t buf[8192]; + uint8_t buf[MAX_STACK_ARRAY_SIZE]; const int len = MIN( bytesLeft, sizeof( buf ) ); success = !tr_ioRead( tor, pieceIndex, offset, len, buf ); if( !success ) diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 7831335c7..f97da46ec 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -31,6 +31,7 @@ #include "list.h" #include "net.h" #include "peer-io.h" +#include "platform.h" /* MAX_STACK_ARRAY_SIZE */ #include "trevent.h" #include "utils.h" @@ -769,7 +770,7 @@ tr_peerIoWriteBytes( tr_peerIo * io, const void * bytes, size_t byteCount ) { - uint8_t * tmp; + uint8_t tmp[MAX_STACK_ARRAY_SIZE]; switch( io->encryptionMode ) { @@ -778,10 +779,14 @@ tr_peerIoWriteBytes( tr_peerIo * io, break; case PEER_ENCRYPTION_RC4: - tmp = tr_new( uint8_t, byteCount ); - tr_cryptoEncrypt( io->crypto, byteCount, bytes, tmp ); - evbuffer_add( outbuf, tmp, byteCount ); - tr_free( tmp ); + evbuffer_expand( outbuf, byteCount ); + while( byteCount > 0 ) { + const size_t thisPass = MIN( byteCount, sizeof( tmp ) ); + tr_cryptoEncrypt( io->crypto, thisPass, bytes, tmp ); + evbuffer_add( outbuf, tmp, thisPass ); + bytes += thisPass; + byteCount -= thisPass; + } break; default: @@ -884,13 +889,16 @@ tr_peerIoDrain( tr_peerIo * io, struct evbuffer * inbuf, size_t byteCount ) { - uint8_t * tmp; + uint8_t tmp[MAX_STACK_ARRAY_SIZE]; assert( tr_isPeerIo( io ) ); - tmp = tr_new( uint8_t, byteCount ); - tr_peerIoReadBytes( io, inbuf, tmp, byteCount ); - tr_free( tmp ); + while( byteCount > 0 ) + { + const size_t thisPass = MIN( byteCount, sizeof( tmp ) ); + tr_peerIoReadBytes( io, inbuf, tmp, thisPass ); + byteCount -= thisPass; + } } int diff --git a/libtransmission/platform.h b/libtransmission/platform.h index 9a885655d..3dfc598db 100644 --- a/libtransmission/platform.h +++ b/libtransmission/platform.h @@ -39,6 +39,8 @@ #define MAX_PATH_LENGTH 1024 #endif +#define MAX_STACK_ARRAY_SIZE 8192 + typedef struct tr_lock tr_lock; typedef struct tr_thread tr_thread;