From 73f500e5bec5fb04740679a7b74aac557392bd79 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 2 Jan 2009 17:46:22 +0000 Subject: [PATCH] (trunk libT) inline parts of peer-io and bandwidth, too --- libtransmission/bandwidth.c | 88 ------------------ libtransmission/bandwidth.h | 121 +++++++++++++++++-------- libtransmission/peer-io.c | 125 +++----------------------- libtransmission/peer-io.h | 174 ++++++++++++++++++++++++++---------- libtransmission/peer-msgs.h | 6 +- 5 files changed, 222 insertions(+), 292 deletions(-) diff --git a/libtransmission/bandwidth.c b/libtransmission/bandwidth.c index 1388858bf..2ffa74e34 100644 --- a/libtransmission/bandwidth.c +++ b/libtransmission/bandwidth.c @@ -26,21 +26,6 @@ **** ***/ -enum -{ - HISTORY_MSEC = 2000, - INTERVAL_MSEC = HISTORY_MSEC, - GRANULARITY_MSEC = 50, - HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ), - MAGIC_NUMBER = 43143 -}; - -struct bratecontrol -{ - int newest; - struct { uint64_t date, size; } transfers[HISTORY_SIZE]; -}; - static float getSpeed( const struct bratecontrol * r, int interval_msec ) { @@ -82,30 +67,6 @@ bytesUsed( struct bratecontrol * r, size_t size ) ******* ******/ -struct tr_band -{ - tr_bool isLimited; - tr_bool honorParentLimits; - size_t bytesLeft; - double desiredSpeed; - struct bratecontrol raw; - struct bratecontrol piece; -}; - -struct tr_bandwidth -{ - struct tr_band band[2]; - struct tr_bandwidth * parent; - int magicNumber; - tr_session * session; - tr_ptrArray children; /* struct tr_bandwidth */ - tr_ptrArray peers; /* tr_peerIo */ -}; - -/*** -**** -***/ - static inline int comparePointers( const void * a, const void * b ) { @@ -115,12 +76,6 @@ comparePointers( const void * a, const void * b ) return 0; } -tr_bool -tr_isBandwidth( const tr_bandwidth * b ) -{ - return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER ); -} - /*** **** ***/ @@ -195,48 +150,6 @@ tr_bandwidthHonorParentLimits( tr_bandwidth * b, **** ***/ -void -tr_bandwidthSetDesiredSpeed( tr_bandwidth * b, - tr_direction dir, - double desiredSpeed ) -{ - assert( tr_isBandwidth( b ) ); - assert( tr_isDirection( dir ) ); - - b->band[dir].desiredSpeed = desiredSpeed; -} - -double -tr_bandwidthGetDesiredSpeed( const tr_bandwidth * b, - tr_direction dir ) -{ - assert( tr_isBandwidth( b ) ); - assert( tr_isDirection( dir ) ); - - return b->band[dir].desiredSpeed; -} - -void -tr_bandwidthSetLimited( tr_bandwidth * b, - tr_direction dir, - tr_bool isLimited ) -{ - assert( tr_isBandwidth( b ) ); - assert( tr_isDirection( dir ) ); - - b->band[dir].isLimited = isLimited; -} - -tr_bool -tr_bandwidthIsLimited( const tr_bandwidth * b, - tr_direction dir ) -{ - assert( tr_isBandwidth( b ) ); - assert( tr_isDirection( dir ) ); - - return b->band[dir].isLimited; -} - #if 0 #warning do not check the code in with this enabled #define DEBUG_DIRECTION TR_UP @@ -358,7 +271,6 @@ tr_bandwidthAddPeer( tr_bandwidth * b, assert( tr_isBandwidth( b ) ); assert( tr_isPeerIo( peerIo ) ); - tr_ptrArrayInsertSorted( &b->peers, peerIo, comparePointers ); } void diff --git a/libtransmission/bandwidth.h b/libtransmission/bandwidth.h index 52be60990..6016a1f80 100644 --- a/libtransmission/bandwidth.h +++ b/libtransmission/bandwidth.h @@ -17,6 +17,42 @@ #ifndef TR_BANDWIDTH_H #define TR_BANDWIDTH_H +#include "transmission.h" +#include "ptrarray.h" + +struct tr_peerIo; + +/* these are PRIVATE IMPLEMENTATION details that should not be touched. + * it's included in the header for inlining and composition. */ +enum +{ + HISTORY_MSEC = 2000, + INTERVAL_MSEC = HISTORY_MSEC, + GRANULARITY_MSEC = 50, + HISTORY_SIZE = ( INTERVAL_MSEC / GRANULARITY_MSEC ), + MAGIC_NUMBER = 43143 +}; + +/* these are PRIVATE IMPLEMENTATION details that should not be touched. + * it's included in the header for inlining and composition. */ +struct bratecontrol +{ + int newest; + struct { uint64_t date, size; } transfers[HISTORY_SIZE]; +}; + +/* these are PRIVATE IMPLEMENTATION details that should not be touched. + * it's included in the header for inlining and composition. */ +struct tr_band +{ + tr_bool isLimited; + tr_bool honorParentLimits; + size_t bytesLeft; + double desiredSpeed; + struct bratecontrol raw; + struct bratecontrol piece; +}; + /** * Bandwidth is an object for measuring and constraining bandwidth speeds. * @@ -56,10 +92,20 @@ * and call tr_bandwidthClamp() before performing I/O to see how much * bandwidth they can safely use. */ +typedef struct tr_bandwidth +{ + /* these are PRIVATE IMPLEMENTATION details that should not be touched. + * it's included in the header for inlining and composition. */ -typedef struct tr_bandwidth tr_bandwidth; + struct tr_band band[2]; + struct tr_bandwidth * parent; + int magicNumber; + tr_session * session; + tr_ptrArray children; /* struct tr_bandwidth */ + tr_ptrArray peers; /* tr_peerIo */ +} +tr_bandwidth; -struct tr_peerIo; /** *** @@ -74,8 +120,10 @@ tr_bandwidth* void tr_bandwidthFree ( tr_bandwidth * bandwidth ); /** @brief test to see if the pointer refers to a live bandwidth object */ -extern inline tr_bool - tr_isBandwidth ( const tr_bandwidth * bandwidth ); +static inline tr_bool tr_isBandwidth( const tr_bandwidth * b ) +{ + return ( b != NULL ) && ( b->magicNumber == MAGIC_NUMBER ); +} /****** ******* @@ -86,33 +134,42 @@ extern inline tr_bool * @see tr_bandwidthAllocate * @see tr_bandwidthGetDesiredSpeed */ -extern inline void - tr_bandwidthSetDesiredSpeed ( tr_bandwidth * bandwidth, - tr_direction direction, - double desiredSpeed ); +static inline void tr_bandwidthSetDesiredSpeed( tr_bandwidth * bandwidth, + tr_direction dir, + double desiredSpeed ) +{ + bandwidth->band[dir].desiredSpeed = desiredSpeed; +} /** * @brief Get the desired speed (in KiB/s) for ths bandwidth subtree. * @see tr_bandwidthSetDesiredSpeed */ -extern inline double - tr_bandwidthGetDesiredSpeed ( const tr_bandwidth * bandwidth, - tr_direction direction ); +static inline double +tr_bandwidthGetDesiredSpeed( const tr_bandwidth * bandwidth, + tr_direction dir ) +{ + return bandwidth->band[dir].desiredSpeed; +} /** * @brief Set whether or not this bandwidth should throttle its peer-io's speeds */ -extern inline void - tr_bandwidthSetLimited ( tr_bandwidth * bandwidth, - tr_direction direction, - tr_bool isLimited ); +static inline void tr_bandwidthSetLimited( tr_bandwidth * bandwidth, + tr_direction dir, + tr_bool isLimited ) +{ + bandwidth->band[dir].isLimited = isLimited; +} /** * @return nonzero if this bandwidth throttles its peer-ios speeds */ -extern inline tr_bool - tr_bandwidthIsLimited ( const tr_bandwidth * bandwidth, - tr_direction direction ); +static inline tr_bool tr_bandwidthIsLimited( const tr_bandwidth * bandwidth, + tr_direction dir ) +{ + return bandwidth->band[dir].isLimited; +} /** * @brief allocate the next period_msec's worth of bandwidth for the peer-ios to consume @@ -132,19 +189,13 @@ size_t tr_bandwidthClamp ( const tr_bandwidth * bandwidth, ******* ******/ -/** - * @brief Get the raw total of bytes read or sent by this bandwidth subtree. - */ -extern inline double - tr_bandwidthGetRawSpeed ( const tr_bandwidth * bandwidth, - tr_direction direction ); +/** @brief Get the raw total of bytes read or sent by this bandwidth subtree. */ +double tr_bandwidthGetRawSpeed( const tr_bandwidth * bandwidth, + tr_direction direction ); -/** - * @brief Get the number of piece data bytes read or sent by this bandwidth subtree. - */ -extern inline double - tr_bandwidthGetPieceSpeed ( const tr_bandwidth * bandwidth, - tr_direction direction ); +/** @brief Get the number of piece data bytes read or sent by this bandwidth subtree. */ +double tr_bandwidthGetPieceSpeed( const tr_bandwidth * bandwidth, + tr_direction direction ); /** * @brief Notify the bandwidth object that some of its allocated bandwidth has been consumed. @@ -180,15 +231,13 @@ void tr_bandwidthHonorParentLimits ( tr_bandwidth * bandwidth, * @brief add a tr_peerIo to this bandwidth's list. * They will be notified when more bandwidth is made available for them to consume. */ -extern inline void - tr_bandwidthAddPeer ( tr_bandwidth * bandwidth, - struct tr_peerIo * peerIo ); +void tr_bandwidthAddPeer( tr_bandwidth * bandwidth, + struct tr_peerIo * peerIo ); /** * @brief remove a peer-io from this bandwidth's list. */ -extern inline void - tr_bandwidthRemovePeer ( tr_bandwidth * bandwidth, - struct tr_peerIo * peerIo ); +void tr_bandwidthRemovePeer( tr_bandwidth * bandwidth, + struct tr_peerIo * peerIo ); #endif diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 4e6e09a6a..948b66a87 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -78,43 +78,6 @@ struct tr_datatype struct __tr_list head; }; -struct tr_peerIo -{ - tr_bool isEncrypted; - tr_bool isIncoming; - tr_bool peerIdIsSet; - tr_bool extendedProtocolSupported; - tr_bool fastExtensionSupported; - - int magicNumber; - - uint8_t encryptionMode; - tr_port port; - int socket; - - uint8_t peerId[SHA_DIGEST_LENGTH]; - time_t timeCreated; - - tr_session * session; - - tr_address addr; - - tr_can_read_cb canRead; - tr_did_write_cb didWrite; - tr_net_error_cb gotError; - void * userData; - - tr_bandwidth * bandwidth; - tr_crypto * crypto; - - struct evbuffer * inbuf; - struct evbuffer * outbuf; - struct __tr_list outbuf_datatypes; /* struct tr_datatype */ - - struct event event_read; - struct event event_write; -}; - /*** **** ***/ @@ -799,34 +762,6 @@ tr_peerIoWriteBytes( tr_peerIo * io, } } -void -tr_peerIoWriteUint8( tr_peerIo * io, - struct evbuffer * outbuf, - uint8_t writeme ) -{ - tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) ); -} - -void -tr_peerIoWriteUint16( tr_peerIo * io, - struct evbuffer * outbuf, - uint16_t writeme ) -{ - uint16_t tmp = htons( writeme ); - - tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) ); -} - -void -tr_peerIoWriteUint32( tr_peerIo * io, - struct evbuffer * outbuf, - uint32_t writeme ) -{ - uint32_t tmp = htonl( writeme ); - - tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) ); -} - /*** **** ***/ @@ -856,34 +791,6 @@ tr_peerIoReadBytes( tr_peerIo * io, } } -void -tr_peerIoReadUint8( tr_peerIo * io, - struct evbuffer * inbuf, - uint8_t * setme ) -{ - tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) ); -} - -void -tr_peerIoReadUint16( tr_peerIo * io, - struct evbuffer * inbuf, - uint16_t * setme ) -{ - uint16_t tmp; - tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) ); - *setme = ntohs( tmp ); -} - -void -tr_peerIoReadUint32( tr_peerIo * io, - struct evbuffer * inbuf, - uint32_t * setme ) -{ - uint32_t tmp; - tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) ); - *setme = ntohl( tmp ); -} - void tr_peerIoDrain( tr_peerIo * io, struct evbuffer * inbuf, @@ -899,12 +806,6 @@ tr_peerIoDrain( tr_peerIo * io, } } -int -tr_peerIoGetAge( const tr_peerIo * io ) -{ - return time( NULL ) - io->timeCreated; -} - /*** **** ***/ @@ -981,22 +882,6 @@ tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t limit ) return ret; } -struct evbuffer * -tr_peerIoGetReadBuffer( tr_peerIo * io ) -{ - assert( tr_isPeerIo( io ) ); - - return io->inbuf; -} - -tr_bool -tr_peerIoHasBandwidthLeft( const tr_peerIo * io, tr_direction dir ) -{ - assert( tr_isPeerIo( io ) ); - - return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0; -} - /*** **** ****/ @@ -1043,3 +928,13 @@ tr_peerIoSetEnabled( tr_peerIo * io, else event_disable( io, event ); } + +tr_bool +tr_peerIoHasBandwidthLeft( const tr_peerIo * io, + tr_direction dir ) +{ + assert( tr_isPeerIo( io ) ); + + return tr_bandwidthClamp( io->bandwidth, dir, 1024 ) > 0; +} + diff --git a/libtransmission/peer-io.h b/libtransmission/peer-io.h index 820fe2356..b027562bf 100644 --- a/libtransmission/peer-io.h +++ b/libtransmission/peer-io.h @@ -21,11 +21,77 @@ *** **/ +#include + +#include + +#include "transmission.h" +#include "list.h" /* __tr_list */ +#include "net.h" /* tr_address */ + struct evbuffer; -struct tr_address; struct tr_bandwidth; struct tr_crypto; -typedef struct tr_peerIo tr_peerIo; +struct tr_peerIo; + +typedef enum +{ + READ_NOW, + READ_LATER, + READ_ERR +} +ReadState; + +typedef ReadState ( *tr_can_read_cb )( struct tr_peerIo * io, + void * user_data, + size_t * setme_piece_byte_count ); + +typedef void ( *tr_did_write_cb )( struct tr_peerIo * io, + size_t bytesWritten, + int wasPieceData, + void * userData ); + +typedef void ( *tr_net_error_cb )( struct tr_peerIo * io, + short what, + void * userData ); + +typedef struct tr_peerIo +{ + tr_bool isEncrypted; + tr_bool isIncoming; + tr_bool peerIdIsSet; + tr_bool extendedProtocolSupported; + tr_bool fastExtensionSupported; + + int magicNumber; + + uint8_t encryptionMode; + tr_port port; + int socket; + + uint8_t peerId[SHA_DIGEST_LENGTH]; + time_t timeCreated; + + tr_session * session; + + tr_address addr; + + tr_can_read_cb canRead; + tr_did_write_cb didWrite; + tr_net_error_cb gotError; + void * userData; + + struct tr_bandwidth * bandwidth; + struct tr_crypto * crypto; + + struct evbuffer * inbuf; + struct evbuffer * outbuf; + struct __tr_list outbuf_datatypes; /* struct tr_datatype */ + + struct event event_read; + struct event event_write; +} +tr_peerIo; /** *** @@ -83,7 +149,10 @@ int tr_peerIoReconnect( tr_peerIo * io ); tr_bool tr_peerIoIsIncoming( const tr_peerIo * io ); -extern inline int tr_peerIoGetAge( const tr_peerIo * io ); +static inline int tr_peerIoGetAge( const tr_peerIo * io ) +{ + return time( NULL ) - io->timeCreated; +} /** @@ -99,27 +168,6 @@ const uint8_t* tr_peerIoGetPeersId( const tr_peerIo * io ); *** **/ -typedef enum -{ - READ_NOW, - READ_LATER, - READ_ERR -} -ReadState; - -typedef ReadState ( *tr_can_read_cb )( tr_peerIo * io, - void * user_data, - size_t * setme_piece_byte_count ); - -typedef void ( *tr_did_write_cb )( tr_peerIo * io, - size_t bytesWritten, - int wasPieceData, - void * userData ); - -typedef void ( *tr_net_error_cb )( tr_peerIo * io, - short what, - void * userData ); - void tr_peerIoSetIOFuncs ( tr_peerIo * io, tr_can_read_cb readcb, tr_did_write_cb writecb, @@ -163,37 +211,58 @@ void tr_peerIoWriteBytes( tr_peerIo * io, const void * bytes, size_t byteCount ); -void tr_peerIoWriteUint8( tr_peerIo * io, - struct evbuffer * outbuf, - uint8_t writeme ); +static inline void tr_peerIoWriteUint8( tr_peerIo * io, + struct evbuffer * outbuf, + uint8_t writeme ) +{ + tr_peerIoWriteBytes( io, outbuf, &writeme, sizeof( uint8_t ) ); +} -void tr_peerIoWriteUint16( tr_peerIo * io, - struct evbuffer * outbuf, - uint16_t writeme ); +static inline void tr_peerIoWriteUint16( tr_peerIo * io, + struct evbuffer * outbuf, + uint16_t writeme ) +{ + const uint16_t tmp = htons( writeme ); + tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint16_t ) ); +} -void tr_peerIoWriteUint32( tr_peerIo * io, - struct evbuffer * outbuf, - uint32_t writeme ); +static inline void tr_peerIoWriteUint32( tr_peerIo * io, + struct evbuffer * outbuf, + uint32_t writeme ) +{ + const uint32_t tmp = htonl( writeme ); + tr_peerIoWriteBytes( io, outbuf, &tmp, sizeof( uint32_t ) ); +} void tr_peerIoReadBytes( tr_peerIo * io, struct evbuffer * inbuf, void * bytes, size_t byteCount ); -extern inline void - tr_peerIoReadUint8( tr_peerIo * io, - struct evbuffer * inbuf, - uint8_t * setme ); +static inline void tr_peerIoReadUint8( tr_peerIo * io, + struct evbuffer * inbuf, + uint8_t * setme ) +{ + tr_peerIoReadBytes( io, inbuf, setme, sizeof( uint8_t ) ); +} -extern inline void - tr_peerIoReadUint16( tr_peerIo * io, - struct evbuffer * inbuf, - uint16_t * setme ); +static inline void tr_peerIoReadUint16( tr_peerIo * io, + struct evbuffer * inbuf, + uint16_t * setme ) +{ + uint16_t tmp; + tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint16_t ) ); + *setme = ntohs( tmp ); +} -extern inline void - tr_peerIoReadUint32( tr_peerIo * io, - struct evbuffer * inbuf, - uint32_t * setme ); +static inline void tr_peerIoReadUint32( tr_peerIo * io, + struct evbuffer * inbuf, + uint32_t * setme ) +{ + uint32_t tmp; + tr_peerIoReadBytes( io, inbuf, &tmp, sizeof( uint32_t ) ); + *setme = ntohl( tmp ); +} void tr_peerIoDrain( tr_peerIo * io, struct evbuffer * inbuf, @@ -213,13 +282,13 @@ void tr_peerIoBandwidthUsed( tr_peerIo * io, size_t byteCount, int isPieceData ); +tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo * io, + tr_direction dir ); + /** *** **/ -extern inline tr_bool tr_peerIoHasBandwidthLeft( const tr_peerIo * io, - tr_direction direction ); - void tr_peerIoSetEnabled( tr_peerIo * io, tr_direction dir, tr_bool isEnabled ); @@ -228,9 +297,16 @@ int tr_peerIoFlush( tr_peerIo * io, tr_direction dir, size_t byteLimit ); -extern inline struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io ); +/** +*** +**/ +static inline struct evbuffer * tr_peerIoGetReadBuffer( tr_peerIo * io ) +{ + assert( tr_isPeerIo( io ) ); + return io->inbuf; +} #endif diff --git a/libtransmission/peer-msgs.h b/libtransmission/peer-msgs.h index 820d202bb..7e82353aa 100644 --- a/libtransmission/peer-msgs.h +++ b/libtransmission/peer-msgs.h @@ -40,8 +40,7 @@ void tr_peerMsgsSetChoke( tr_peermsgs *, void tr_peerMsgsHave( tr_peermsgs * msgs, uint32_t pieceIndex ); -extern inline void - tr_peerMsgsPulse( tr_peermsgs * msgs ); +void tr_peerMsgsPulse( tr_peermsgs * msgs ); void tr_peerMsgsCancel( tr_peermsgs * msgs, uint32_t pieceIndex, @@ -56,8 +55,7 @@ tr_addreq_t tr_peerMsgsAddRequest( tr_peermsgs * peer, uint32_t offset, uint32_t length ); -extern inline void - tr_peerMsgsUnsubscribe( tr_peermsgs * peer, +void tr_peerMsgsUnsubscribe( tr_peermsgs * peer, tr_publisher_tag tag ); size_t tr_generateAllowedSet( tr_piece_index_t * setmePieces, -- 2.40.0