From d9e4ca49b560a8dca343cfc824f477f61df28283 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 30 Jun 2010 06:03:55 +0000 Subject: [PATCH] (trunk) avoid a few unnecessary type conversions. remove a few unnecessary mutex locks. --- libtransmission/history.c | 16 ++++++++-------- libtransmission/history.h | 6 +++--- libtransmission/utils.c | 20 ++------------------ libtransmission/utils.h | 12 ++++++------ 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/libtransmission/history.c b/libtransmission/history.c index 054ee9f67..e0bd93e81 100644 --- a/libtransmission/history.c +++ b/libtransmission/history.c @@ -16,7 +16,7 @@ struct history_slice { - double n; + unsigned int n; uint64_t date; }; @@ -24,12 +24,12 @@ struct tr_recentHistory { int newest; int sliceCount; - int precision_msec; + unsigned int precision_msec; struct history_slice * slices; }; void -tr_historyAdd( tr_recentHistory * h, uint64_t now, double n ) +tr_historyAdd( tr_recentHistory * h, uint64_t now, unsigned int n ) { if( h->slices[h->newest].date + h->precision_msec >= now ) h->slices[h->newest].n += n; @@ -40,10 +40,10 @@ tr_historyAdd( tr_recentHistory * h, uint64_t now, double n ) } } -double -tr_historyGet( const tr_recentHistory * h, uint64_t now, int msec ) +unsigned int +tr_historyGet( const tr_recentHistory * h, uint64_t now, unsigned int msec ) { - double n = 0; + unsigned int n = 0; const uint64_t cutoff = (now?now:tr_date()) - msec; int i = h->newest; @@ -62,13 +62,13 @@ tr_historyGet( const tr_recentHistory * h, uint64_t now, int msec ) } tr_recentHistory * -tr_historyNew( int seconds, int bins_per_second ) +tr_historyNew( unsigned int seconds, unsigned int bins_per_second ) { tr_recentHistory * h; h = tr_new0( tr_recentHistory, 1 ); h->precision_msec = 1000 / bins_per_second; - h->sliceCount = seconds * bins_per_second; + h->sliceCount = (int)(seconds * bins_per_second); h->slices = tr_new0( struct history_slice, h->sliceCount ); return h; diff --git a/libtransmission/history.h b/libtransmission/history.h index 5839b67fb..27c7de738 100644 --- a/libtransmission/history.h +++ b/libtransmission/history.h @@ -32,7 +32,7 @@ typedef struct tr_recentHistory tr_recentHistory; * @param precision how precise the history should be, in fractions of a second. * For a precision of 1/20th of a second, use a precision of 20. */ -tr_recentHistory * tr_historyNew( int seconds, int precision ); +tr_recentHistory * tr_historyNew( unsigned int seconds, unsigned int precision ); /** @brief destroy an existing tr_recentHistory object. */ void tr_historyFree( tr_recentHistory * ); @@ -42,13 +42,13 @@ void tr_historyFree( tr_recentHistory * ); * @param when the current time in msec, such as from tr_date() * @param n how many items to add to the history's counter */ -void tr_historyAdd( tr_recentHistory *, uint64_t when, double n ); +void tr_historyAdd( tr_recentHistory *, uint64_t when, unsigned int n ); /** * @brief count how many events have occurred in the last N seconds. * @param when the current time in msec, such as from tr_date() * @param seconds how many seconds to count back through. */ -double tr_historyGet( const tr_recentHistory *, uint64_t when, int seconds ); +unsigned int tr_historyGet( const tr_recentHistory *, uint64_t when, unsigned int seconds ); #endif diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 48495455d..bdaa4b783 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -122,35 +122,19 @@ tr_setMessageLevel( int level ) int tr_getMessageLevel( void ) { - int ret; - tr_lockLock( messageLock ); - - ret = messageLevel; - - tr_lockUnlock( messageLock ); - return ret; + return messageLevel; } void tr_setMessageQueuing( tr_bool enabled ) { - tr_lockLock( messageLock ); - messageQueuing = enabled; - - tr_lockUnlock( messageLock ); } tr_bool tr_getMessageQueuing( void ) { - int ret; - tr_lockLock( messageLock ); - - ret = messageQueuing; - - tr_lockUnlock( messageLock ); - return ret; + return messageQueuing != 0; } tr_msg_list * diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 07d9862db..5b7476bb6 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -322,7 +322,7 @@ static inline void tr_free( void * p ) * @param byteCount the number of bytes to copy * @return a newly-allocated copy of `src' that can be freed with tr_free() */ -static inline void* tr_memdup( const void * src, int byteCount ) +static inline void* tr_memdup( const void * src, size_t byteCount ) { return memcpy( tr_malloc( byteCount ), src, byteCount ); } @@ -353,7 +353,7 @@ char* tr_strndup( const void * in, int len ) TR_GNUC_MALLOC; */ static inline char* tr_strdup( const void * in ) { - return tr_strndup( in, in ? strlen( (const char *) in ) : 0 ); + return tr_strndup( in, in ? (int)strlen((const char *)in) : 0 ); } /** @brief similar to bsearch() but returns the index of the lower bound */ @@ -521,10 +521,10 @@ int tr_moveFile( const char * oldpath, const char * newpath, tr_bool * renamed ) TR_GNUC_NONNULL(1,2); /** @brief convenience function to remove an item from an array */ -static inline void tr_removeElementFromArray( void * array, - int index_to_remove, - size_t sizeof_element, - size_t nmemb ) +static inline void tr_removeElementFromArray( void * array, + unsigned int index_to_remove, + size_t sizeof_element, + size_t nmemb ) { char * a = (char*) array;