1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-01-31 03:12:44 +00:00

(trunk libT) avoid some unnecessary memory fragmentation... for composited objects that have a tr_ratecontrol, contain the it directly rather than a pointer to one allocated elsewhere on the heap.

This commit is contained in:
Charles Kerr 2009-01-02 20:42:35 +00:00
parent 8318642989
commit 86d3e70121
6 changed files with 45 additions and 56 deletions

View file

@ -1457,7 +1457,7 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen, size_t
return READ_ERR;
}
updatePeerProgress( msgs );
tr_rcTransferred( msgs->torrent->swarmSpeed,
tr_rcTransferred( &msgs->torrent->swarmSpeed,
msgs->torrent->info.pieceSize );
break;

View file

@ -29,27 +29,6 @@
#include "ratecontrol.h"
#include "utils.h"
enum
{
INTERVAL_MSEC = TR_RATECONTROL_HISTORY_MSEC,
GRANULARITY_MSEC = 250,
HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC )
};
struct tr_transfer
{
uint64_t date;
uint64_t size;
};
struct tr_ratecontrol
{
int newest;
struct tr_transfer transfers[HISTORY_SIZE];
};
/* return the xfer rate over the last `interval' seconds in KiB/sec */
static float
rateForInterval( const tr_ratecontrol * r,
@ -66,7 +45,7 @@ rateForInterval( const tr_ratecontrol * r,
bytes += r->transfers[i].size;
if( --i == -1 ) i = HISTORY_SIZE - 1; /* circular history */
if( --i == -1 ) i = TR_RC_HISTORY_SIZE - 1; /* circular history */
if( i == r->newest ) break; /* we've come all the way around */
}
@ -77,30 +56,13 @@ rateForInterval( const tr_ratecontrol * r,
****
***/
tr_ratecontrol*
tr_rcInit( void )
{
return tr_new0( tr_ratecontrol, 1 );
}
void
tr_rcClose( tr_ratecontrol * r )
{
memset( r, 0, sizeof( tr_ratecontrol ) );
tr_free( r );
}
/***
****
***/
float
tr_rcRate( const tr_ratecontrol * r )
{
float ret = 0.0f;
if( r )
ret = rateForInterval( r, INTERVAL_MSEC );
ret = rateForInterval( r, TR_RC_HISTORY_MSEC );
return ret;
}
@ -115,11 +77,11 @@ tr_rcTransferred( tr_ratecontrol * r,
{
const uint64_t now = tr_date ( );
if( r->transfers[r->newest].date + GRANULARITY_MSEC >= now )
if( r->transfers[r->newest].date + TR_RC_GRANULARITY_MSEC >= now )
r->transfers[r->newest].size += size;
else
{
if( ++r->newest == HISTORY_SIZE ) r->newest = 0;
if( ++r->newest == TR_RC_HISTORY_SIZE ) r->newest = 0;
r->transfers[r->newest].date = now;
r->transfers[r->newest].size = size;
}

View file

@ -29,17 +29,43 @@
#ifndef _TR_RATECONTROL_H_
#define _TR_RATECONTROL_H_
#include <string.h> /* memset() */
#include "transmission.h"
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
* it's included in the header for inlining and composition. */
enum
{
TR_RATECONTROL_HISTORY_MSEC = 2000
TR_RC_HISTORY_MSEC = 2000,
TR_RC_GRANULARITY_MSEC = 250,
TR_RC_HISTORY_SIZE = ( TR_RC_HISTORY_MSEC / TR_RC_GRANULARITY_MSEC )
};
typedef struct tr_ratecontrol tr_ratecontrol;
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
* it's included in the header for inlining and composition. */
struct tr_transfer
{
uint64_t date;
uint64_t size;
};
/* these are PRIVATE IMPLEMENTATION details that should not be touched.
* it's included in the header for inlining and composition. */
typedef struct tr_ratecontrol
{
int newest;
struct tr_transfer transfers[TR_RC_HISTORY_SIZE];
}
tr_ratecontrol;
tr_ratecontrol * tr_rcInit ( void );
/***
****
***/
void tr_rcClose ( tr_ratecontrol * ratecontrol );
static inline void tr_rcConstruct ( tr_ratecontrol * rc ) { memset( rc, 0, sizeof( tr_ratecontrol ) ); }
static inline void tr_rcDestruct ( tr_ratecontrol * rc ) { memset( rc, 0xDEAD, sizeof( tr_ratecontrol ) ); }
void tr_rcTransferred ( tr_ratecontrol * ratecontrol,
size_t byteCount );

View file

@ -501,7 +501,7 @@ torrentRealInit( tr_session * session,
tr_torrentInitFilePieces( tor );
tor->swarmSpeed = tr_rcInit( );
tr_rcConstruct( &tor->swarmSpeed );
tr_sha1( tor->obfuscatedHash, "req2", 4,
info->hash, SHA_DIGEST_LENGTH,
@ -792,7 +792,7 @@ tr_torrentStat( tr_torrent * tor )
(double) tor->info.pieceCount )
: 0.0;
s->swarmSpeed = tr_rcRate( tor->swarmSpeed );
s->swarmSpeed = tr_rcRate( &tor->swarmSpeed );
s->activityDate = tor->activityDate;
s->addedDate = tor->addedDate;
@ -1034,7 +1034,7 @@ freeTorrent( tr_torrent * tor )
tr_cpDestruct( &tor->completion );
tr_rcClose( tor->swarmSpeed );
tr_rcDestruct( &tor->swarmSpeed );
tr_trackerUnsubscribe( tor->tracker, tor->trackerSubscription );
tr_trackerFree( tor->tracker );

View file

@ -30,6 +30,7 @@
#define TR_TORRENT_H 1
#include "completion.h" /* tr_completion */
#include "ratecontrol.h" /* tr_ratecontrol */
#include "session.h" /* tr_globalLock(), tr_globalUnlock() */
#include "utils.h" /* tr_bitfield */
@ -146,7 +147,7 @@ struct tr_torrent
tr_speedlimit speedLimitMode[2];
struct tr_ratecontrol * swarmSpeed;
struct tr_ratecontrol swarmSpeed;
int error;
char errorString[128];

View file

@ -40,7 +40,7 @@ struct tr_webseed
uint32_t pieceOffset;
uint32_t byteCount;
tr_ratecontrol * rateDown;
tr_ratecontrol rateDown;
tr_session * session;
@ -169,7 +169,7 @@ webResponseFunc( tr_session * session,
if( !w->dead )
{
fireClientGotData( w, response_byte_count );
tr_rcTransferred( w->rateDown, response_byte_count );
tr_rcTransferred( &w->rateDown, response_byte_count );
}
if( EVBUFFER_LENGTH( w->content ) < w->byteCount )
@ -256,7 +256,7 @@ tr_webseedGetSpeed( const tr_webseed * w, float * setme_KiBs )
{
const int isActive = tr_webseedIsActive( w );
*setme_KiBs = isActive ? tr_rcRate( w->rateDown ) : 0.0f;
*setme_KiBs = isActive ? tr_rcRate( &w->rateDown ) : 0.0f;
return isActive;
}
@ -275,10 +275,10 @@ tr_webseedNew( struct tr_torrent * torrent,
memcpy( w->hash, torrent->info.hash, SHA_DIGEST_LENGTH );
w->session = torrent->session;
w->content = evbuffer_new( );
w->rateDown = tr_rcInit( );
w->url = tr_strdup( url );
w->callback = callback;
w->callback_userdata = callback_userdata;
tr_rcConstruct( &w->rateDown );
/*fprintf( stderr, "w->callback_userdata is %p\n", w->callback_userdata );*/
return w;
}
@ -295,7 +295,7 @@ tr_webseedFree( tr_webseed * w )
else
{
evbuffer_free( w->content );
tr_rcClose( w->rateDown );
tr_rcDestruct( &w->rateDown );
tr_free( w->url );
tr_free( w );
}