(trunk libT) use aggregation for the tr_bandwidth objects owned by tr_session and tr_torrent

This commit is contained in:
Jordan Lee 2011-03-31 14:53:22 +00:00
parent f481fd433a
commit 1b02eb8641
8 changed files with 44 additions and 58 deletions

View File

@ -95,7 +95,7 @@ comparePointers( const void * a, const void * b )
****
***/
tr_bandwidth*
void
tr_bandwidthConstruct( tr_bandwidth * b, tr_session * session, tr_bandwidth * parent )
{
b->session = session;
@ -104,10 +104,9 @@ tr_bandwidthConstruct( tr_bandwidth * b, tr_session * session, tr_bandwidth * pa
b->band[TR_UP].honorParentLimits = true;
b->band[TR_DOWN].honorParentLimits = true;
tr_bandwidthSetParent( b, parent );
return b;
}
tr_bandwidth*
void
tr_bandwidthDestruct( tr_bandwidth * b )
{
assert( tr_isBandwidth( b ) );
@ -116,7 +115,6 @@ tr_bandwidthDestruct( tr_bandwidth * b )
tr_ptrArrayDestruct( &b->children, NULL );
memset( b, ~0, sizeof( tr_bandwidth ) );
return b;
}
/***

View File

@ -122,23 +122,11 @@ tr_bandwidth;
***
**/
tr_bandwidth* tr_bandwidthConstruct( tr_bandwidth * bandwidth,
tr_session * session,
tr_bandwidth * parent );
void tr_bandwidthConstruct( tr_bandwidth * bandwidth,
tr_session * session,
tr_bandwidth * parent );
/** @brief create a new tr_bandwidth object */
static inline tr_bandwidth* tr_bandwidthNew( tr_session * session, tr_bandwidth * parent )
{
return tr_bandwidthConstruct( tr_new0( tr_bandwidth, 1 ), session, parent );
}
tr_bandwidth* tr_bandwidthDestruct( tr_bandwidth * bandwidth );
/** @brief free a tr_bandwidth object */
static inline void tr_bandwidthFree( tr_bandwidth * bandwidth )
{
tr_free( tr_bandwidthDestruct( bandwidth ) );
}
void tr_bandwidthDestruct( tr_bandwidth * bandwidth );
/** @brief test to see if the pointer refers to a live bandwidth object */
static inline bool tr_isBandwidth( const tr_bandwidth * b )
@ -258,8 +246,8 @@ static inline bool tr_bandwidthHonorParentLimits ( tr_bandwidth * bandwidth,
return didChange;
}
static inline bool tr_bandwidthAreParentLimitsHonored( tr_bandwidth * bandwidth,
tr_direction direction )
static inline bool tr_bandwidthAreParentLimitsHonored( const tr_bandwidth * bandwidth,
tr_direction direction )
{
assert( tr_isBandwidth( bandwidth ) );
assert( tr_isDirection( direction ) );

View File

@ -2030,7 +2030,7 @@ myHandshakeDoneCB( tr_handshake * handshake,
peer->io = tr_handshakeStealIO( handshake ); /* this steals its refcount too, which is
balanced by our unref in peerDelete() */
tr_peerIoSetParent( peer->io, t->tor->bandwidth );
tr_peerIoSetParent( peer->io, &t->tor->bandwidth );
tr_peerMsgsNew( t->tor, peer, peerCallbackFunc, t );
success = true;
@ -2078,7 +2078,7 @@ tr_peerMgrAddIncoming( tr_peerMgr * manager,
tr_peerIo * io;
tr_handshake * handshake;
io = tr_peerIoNewIncoming( session, session->bandwidth, addr, port, socket, utp_socket );
io = tr_peerIoNewIncoming( session, &session->bandwidth, addr, port, socket, utp_socket );
handshake = tr_handshakeNew( io,
session->encryptionMode,
@ -3047,7 +3047,7 @@ rechokeUploads( Torrent * t, const uint64_t now )
struct ChokeData * choke = tr_new0( struct ChokeData, peerCount );
const tr_session * session = t->manager->session;
const int chokeAll = !tr_torrentIsPieceTransferAllowed( t->tor, TR_CLIENT_TO_PEER );
const bool isMaxedOut = isBandwidthMaxedOut( t->tor->bandwidth, now, TR_UP );
const bool isMaxedOut = isBandwidthMaxedOut( &t->tor->bandwidth, now, TR_UP );
assert( torrentIsLocked( t ) );
@ -3542,8 +3542,8 @@ bandwidthPulse( int foo UNUSED, short bar UNUSED, void * vmgr )
pumpAllPeers( mgr );
/* allocate bandwidth to the peers */
tr_bandwidthAllocate( mgr->session->bandwidth, TR_UP, BANDWIDTH_PERIOD_MSEC );
tr_bandwidthAllocate( mgr->session->bandwidth, TR_DOWN, BANDWIDTH_PERIOD_MSEC );
tr_bandwidthAllocate( &mgr->session->bandwidth, TR_UP, BANDWIDTH_PERIOD_MSEC );
tr_bandwidthAllocate( &mgr->session->bandwidth, TR_DOWN, BANDWIDTH_PERIOD_MSEC );
/* possibly stop torrents that have seeded enough */
tor = NULL;
@ -3852,7 +3852,7 @@ getPeerCandidates( tr_session * session, int * candidateCount )
continue;
/* if we've already got enough speed in this torrent... */
if( tr_torrentIsSeed( tor ) && isBandwidthMaxedOut( tor->bandwidth, now_msec, TR_UP ) )
if( tr_torrentIsSeed( tor ) && isBandwidthMaxedOut( &tor->bandwidth, now_msec, TR_UP ) )
continue;
atoms = (struct peer_atom**) tr_ptrArrayPeek( &tor->torrentPeers->pool, &nAtoms );
@ -3895,7 +3895,7 @@ initiateConnection( tr_peerMgr * mgr, Torrent * t, struct peer_atom * atom )
tr_atomAddrStr( atom ) );
io = tr_peerIoNewOutgoing( mgr->session,
mgr->session->bandwidth,
&mgr->session->bandwidth,
&atom->addr,
atom->port,
t->tor->info.hash,

View File

@ -567,12 +567,12 @@ tr_sessionInit( const char * tag,
session = tr_new0( tr_session, 1 );
session->udp_socket = -1;
session->udp6_socket = -1;
session->bandwidth = tr_bandwidthNew( session, NULL );
session->lock = tr_lockNew( );
session->cache = tr_cacheNew( 1024*1024*2 );
session->tag = tr_strdup( tag );
session->magicNumber = SESSION_MAGIC_NUMBER;
session->buffer = tr_valloc( SESSION_BUFFER_SIZE );
tr_bandwidthConstruct( &session->bandwidth, session, NULL );
tr_peerIdInit( session->peer_id );
tr_bencInitList( &session->removedTorrents, 0 );
@ -661,7 +661,7 @@ tr_sessionInitImpl( void * vdata )
assert( tr_bencIsDict( clientSettings ) );
dbgmsg( "tr_sessionInit: the session's top-level bandwidth object is %p",
session->bandwidth );
&session->bandwidth );
tr_bencInitDict( &settings, 0 );
tr_sessionGetDefaultSettings( &settings );
@ -1244,9 +1244,9 @@ updateBandwidth( tr_session * session, tr_direction dir )
const bool isLimited = tr_sessionGetActiveSpeedLimit_Bps( session, dir, &limit_Bps );
const bool zeroCase = isLimited && !limit_Bps;
tr_bandwidthSetLimited( session->bandwidth, dir, isLimited && !zeroCase );
tr_bandwidthSetLimited( &session->bandwidth, dir, isLimited && !zeroCase );
tr_bandwidthSetDesiredSpeed_Bps( session->bandwidth, dir, limit_Bps );
tr_bandwidthSetDesiredSpeed_Bps( &session->bandwidth, dir, limit_Bps );
}
enum
@ -1680,13 +1680,13 @@ tr_sessionGetDeleteSource( const tr_session * session )
int
tr_sessionGetPieceSpeed_Bps( const tr_session * session, tr_direction dir )
{
return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed_Bps( session->bandwidth, 0, dir ) : 0;
return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed_Bps( &session->bandwidth, 0, dir ) : 0;
}
int
tr_sessionGetRawSpeed_Bps( const tr_session * session, tr_direction dir )
{
return tr_isSession( session ) ? tr_bandwidthGetRawSpeed_Bps( session->bandwidth, 0, dir ) : 0;
return tr_isSession( session ) ? tr_bandwidthGetRawSpeed_Bps( &session->bandwidth, 0, dir ) : 0;
}
double
tr_sessionGetRawSpeed_KBps( const tr_session * session, tr_direction dir )
@ -1854,7 +1854,7 @@ tr_sessionClose( tr_session * session )
/* free the session memory */
tr_bencFree( &session->removedTorrents );
tr_bandwidthFree( session->bandwidth );
tr_bandwidthDestruct( &session->bandwidth );
tr_bitfieldDestruct( &session->turtle.minutes );
tr_lockFree( session->lock );
if( session->metainfoLookup ) {

View File

@ -27,6 +27,7 @@
#endif
#endif
#include "bandwidth.h"
#include "bencode.h"
#include "bitfield.h"
#include "utils.h"
@ -46,7 +47,6 @@ struct evdns_base;
struct tr_address;
struct tr_announcer;
struct tr_announcer_udp;
struct tr_bandwidth;
struct tr_bindsockets;
struct tr_cache;
struct tr_fdInfo;
@ -197,7 +197,7 @@ struct tr_session
struct event * saveTimer;
/* monitors the "global pool" speeds */
struct tr_bandwidth * bandwidth;
struct tr_bandwidth bandwidth;
double desiredRatio;

View File

@ -179,7 +179,7 @@ tr_torrentSetSpeedLimit_Bps( tr_torrent * tor, tr_direction dir, int Bps )
assert( tr_isDirection( dir ) );
assert( Bps >= 0 );
if( tr_bandwidthSetDesiredSpeed_Bps( tor->bandwidth, dir, Bps ) )
if( tr_bandwidthSetDesiredSpeed_Bps( &tor->bandwidth, dir, Bps ) )
tr_torrentSetDirty( tor );
}
void
@ -194,7 +194,7 @@ tr_torrentGetSpeedLimit_Bps( const tr_torrent * tor, tr_direction dir )
assert( tr_isTorrent( tor ) );
assert( tr_isDirection( dir ) );
return tr_bandwidthGetDesiredSpeed_Bps( tor->bandwidth, dir );
return tr_bandwidthGetDesiredSpeed_Bps( &tor->bandwidth, dir );
}
int
tr_torrentGetSpeedLimit_KBps( const tr_torrent * tor, tr_direction dir )
@ -208,7 +208,7 @@ tr_torrentUseSpeedLimit( tr_torrent * tor, tr_direction dir, bool do_use )
assert( tr_isTorrent( tor ) );
assert( tr_isDirection( dir ) );
if( tr_bandwidthSetLimited( tor->bandwidth, dir, do_use ) )
if( tr_bandwidthSetLimited( &tor->bandwidth, dir, do_use ) )
tr_torrentSetDirty( tor );
}
@ -218,7 +218,7 @@ tr_torrentUsesSpeedLimit( const tr_torrent * tor, tr_direction dir )
assert( tr_isTorrent( tor ) );
assert( tr_isDirection( dir ) );
return tr_bandwidthIsLimited( tor->bandwidth, dir );
return tr_bandwidthIsLimited( &tor->bandwidth, dir );
}
void
@ -228,8 +228,8 @@ tr_torrentUseSessionLimits( tr_torrent * tor, bool doUse )
assert( tr_isTorrent( tor ) );
changed = tr_bandwidthHonorParentLimits( tor->bandwidth, TR_UP, doUse );
changed |= tr_bandwidthHonorParentLimits( tor->bandwidth, TR_DOWN, doUse );
changed = tr_bandwidthHonorParentLimits( &tor->bandwidth, TR_UP, doUse );
changed |= tr_bandwidthHonorParentLimits( &tor->bandwidth, TR_DOWN, doUse );
if( changed )
tr_torrentSetDirty( tor );
@ -240,7 +240,7 @@ tr_torrentUsesSessionLimits( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tr_bandwidthAreParentLimitsHonored( tor->bandwidth, TR_UP );
return tr_bandwidthAreParentLimitsHonored( &tor->bandwidth, TR_UP );
}
/***
@ -827,9 +827,9 @@ torrentInit( tr_torrent * tor, const tr_ctor * ctor )
if( tr_sessionIsIncompleteDirEnabled( session ) )
tor->incompleteDir = tr_strdup( dir );
tor->bandwidth = tr_bandwidthNew( session, session->bandwidth );
tr_bandwidthConstruct( &tor->bandwidth, session, &session->bandwidth );
tor->bandwidth->priority = tr_ctorGetBandwidthPriority( ctor );
tor->bandwidth.priority = tr_ctorGetBandwidthPriority( ctor );
tor->error = TR_STAT_OK;
@ -1168,10 +1168,10 @@ tr_torrentStat( tr_torrent * tor )
s->peersFrom );
now = tr_time_msec( );
s->rawUploadSpeed_KBps = toSpeedKBps( tr_bandwidthGetRawSpeed_Bps ( tor->bandwidth, now, TR_UP ) );
s->pieceUploadSpeed_KBps = toSpeedKBps( tr_bandwidthGetPieceSpeed_Bps( tor->bandwidth, now, TR_UP ) );
s->rawDownloadSpeed_KBps = toSpeedKBps( tr_bandwidthGetRawSpeed_Bps ( tor->bandwidth, now, TR_DOWN ) );
s->pieceDownloadSpeed_KBps = toSpeedKBps( tr_bandwidthGetPieceSpeed_Bps( tor->bandwidth, now, TR_DOWN ) );
s->rawUploadSpeed_KBps = toSpeedKBps( tr_bandwidthGetRawSpeed_Bps ( &tor->bandwidth, now, TR_UP ) );
s->pieceUploadSpeed_KBps = toSpeedKBps( tr_bandwidthGetPieceSpeed_Bps( &tor->bandwidth, now, TR_UP ) );
s->rawDownloadSpeed_KBps = toSpeedKBps( tr_bandwidthGetRawSpeed_Bps ( &tor->bandwidth, now, TR_DOWN ) );
s->pieceDownloadSpeed_KBps = toSpeedKBps( tr_bandwidthGetPieceSpeed_Bps( &tor->bandwidth, now, TR_DOWN ) );
s->percentComplete = tr_cpPercentComplete ( &tor->completion );
s->metadataPercentComplete = tr_torrentGetMetadataPercent( tor );
@ -1500,7 +1500,7 @@ freeTorrent( tr_torrent * tor )
assert( session->torrentCount >= 1 );
session->torrentCount--;
tr_bandwidthFree( tor->bandwidth );
tr_bandwidthDestruct( &tor->bandwidth );
tr_metainfoFree( inf );
tr_free( tor );
@ -2184,7 +2184,7 @@ tr_torrentGetPriority( const tr_torrent * tor )
{
assert( tr_isTorrent( tor ) );
return tor->bandwidth->priority;
return tor->bandwidth.priority;
}
void
@ -2193,9 +2193,9 @@ tr_torrentSetPriority( tr_torrent * tor, tr_priority_t priority )
assert( tr_isTorrent( tor ) );
assert( tr_isPriority( priority ) );
if( tor->bandwidth->priority != priority )
if( tor->bandwidth.priority != priority )
{
tor->bandwidth->priority = priority;
tor->bandwidth.priority = priority;
tr_torrentSetDirty( tor );
}

View File

@ -17,11 +17,11 @@
#ifndef TR_TORRENT_H
#define TR_TORRENT_H 1
#include "bandwidth.h" /* tr_bandwidth */
#include "completion.h" /* tr_completion */
#include "session.h" /* tr_sessionLock(), tr_sessionUnlock() */
#include "utils.h" /* TR_GNUC_PRINTF */
struct tr_bandwidth;
struct tr_torrent_tiers;
struct tr_magnet_info;
@ -253,7 +253,7 @@ struct tr_torrent
int uniqueId;
struct tr_bandwidth * bandwidth;
struct tr_bandwidth bandwidth;
struct tr_torrent_peers * torrentPeers;

View File

@ -347,7 +347,7 @@ tr_webseedNew( struct tr_torrent * tor,
w->callback = callback;
w->callback_data = callback_data;
//tr_rcConstruct( &w->download_rate );
tr_bandwidthConstruct( &w->bandwidth, tor->session, tor->bandwidth );
tr_bandwidthConstruct( &w->bandwidth, tor->session, &tor->bandwidth );
w->timer = evtimer_new( w->session->event_base, webseed_timer_func, w );
tr_timerAddMsec( w->timer, TR_IDLE_TIMER_MSEC );
return w;