diff --git a/libtransmission/crypto.c b/libtransmission/crypto.c index 6ef4897fb..e754c1015 100644 --- a/libtransmission/crypto.c +++ b/libtransmission/crypto.c @@ -30,8 +30,6 @@ #include "crypto.h" #include "utils.h" -typedef uint8_t tr_bool; - /** *** **/ diff --git a/libtransmission/utils.c b/libtransmission/utils.c index 97a925060..7cbf913ed 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -642,22 +642,6 @@ tr_buildPath( const char *first_element, ... ) ***** ****/ -void* -tr_memdup( const void * in, - int byteCount ) -{ - void * out = tr_new( uint8_t, byteCount ); - - memcpy( out, in, byteCount ); - return out; -} - -char* -tr_strdup( const void * in ) -{ - return tr_strndup( in, in ? strlen( (const char*)in ) : 0 ); -} - char* tr_strndup( const void * in, int len ) @@ -696,25 +680,6 @@ tr_strdup_printf( const char * fmt, ... ) return ret; } -void* -tr_malloc( size_t size ) -{ - return size ? malloc( size ) : NULL; -} - -void* -tr_malloc0( size_t size ) -{ - return size ? calloc( 1, size ) : NULL; -} - -void -tr_free( void * p ) -{ - if( p ) - free( p ); -} - const char* tr_strerror( int i ) { @@ -766,16 +731,12 @@ tr_bitfieldConstruct( tr_bitfield * b, size_t bitCount ) return b; } -void +tr_bitfield* tr_bitfieldDestruct( tr_bitfield * b ) { - tr_free( b->bits ); -} - -tr_bitfield* -tr_bitfieldNew( size_t bitCount ) -{ - return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitCount ); + if( b ) + tr_free( b->bits ); + return b; } tr_bitfield* @@ -789,16 +750,6 @@ tr_bitfieldDup( const tr_bitfield * in ) return ret; } -void -tr_bitfieldFree( tr_bitfield * bitfield ) -{ - if( bitfield ) - { - tr_bitfieldDestruct( bitfield ); - tr_free( bitfield ); - } -} - void tr_bitfieldClear( tr_bitfield * bitfield ) { diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 20c751058..a887ccbe1 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -27,10 +27,14 @@ #include #include -#include /* for size_t */ +#include /* size_t */ #include /* FILE* */ +#include /* memcpy()* */ +#include /* malloc() */ #include /* time_t* */ +#include "transmission.h" + #ifdef __cplusplus extern "C" { #endif @@ -277,33 +281,39 @@ void tr_releaseBuffer( struct evbuffer * buf ); **** ***/ +static inline void* tr_malloc( size_t size ) +{ + return size ? malloc( size ) : NULL; +} +static inline void* tr_malloc0( size_t size ) +{ + return size ? calloc( 1, size ) : NULL; +} +static inline void tr_free( void * p ) +{ + if( p != NULL ) + free( p ); +} +static inline void* tr_memdup( const void * src, int byteCount ) +{ + return memcpy( tr_malloc( byteCount ), src, byteCount ); +} + #define tr_new( struct_type, n_structs ) \ - ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( (\ - size_t) (\ - n_structs ) ) ) ) + ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) ) + #define tr_new0( struct_type, n_structs ) \ - ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( (\ - size_t) (\ - n_structs ) ) ) ) + ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) ) + #define tr_renew( struct_type, mem, n_structs ) \ - ( (struct_type *) realloc ( ( mem ),\ - ( (size_t) sizeof ( struct_type ) ) * ( (\ - size_t) (\ - n_structs ) ) ) ) + ( (struct_type *) realloc ( ( mem ), ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) ) -void* tr_malloc( size_t ) TR_GNUC_MALLOC; +char* tr_strndup( const void * str, int len ) TR_GNUC_MALLOC; -void* tr_malloc0( size_t ) TR_GNUC_MALLOC; - -void tr_free( void* ); - -char* tr_strdup( const void * str ) TR_GNUC_MALLOC; - -char* tr_strndup( const void * str, - int len ) TR_GNUC_MALLOC; - -void* tr_memdup( const void * src, - int byteCount ) TR_GNUC_MALLOC; +static inline char* tr_strdup( const void * in ) +{ + return tr_strndup( in, in ? strlen( (const char*)in ) : 0 ); +} char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC; @@ -373,14 +383,20 @@ tr_bitfield; tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount ); -void tr_bitfieldDestruct( tr_bitfield* ); +tr_bitfield* tr_bitfieldDestruct( tr_bitfield* ); -tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC; +static inline tr_bitfield* tr_bitfieldNew( size_t bitcount ) +{ + return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount ); +} + +static inline void tr_bitfieldFree( tr_bitfield * b ) +{ + tr_free( tr_bitfieldDestruct( b ) ); +} tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC; -void tr_bitfieldFree( tr_bitfield* ); - void tr_bitfieldClear( tr_bitfield* ); int tr_bitfieldAdd( tr_bitfield*, size_t bit ); @@ -404,17 +420,23 @@ tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* ); has none of tr_bitfieldHas()'s safety checks, so you need to call tr_bitfieldTestFast() first before you start looping. */ -#define tr_bitfieldHasFast( bitfield, nth ) \ - ( ( (bitfield)->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 ) +static inline tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth ) +{ + return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0; +} /** @param high the highest nth bit you're going to access */ -#define tr_bitfieldTestFast( bitfield, high ) \ - ( ( bitfield ) && ( ( bitfield )->bits )\ - && ( ( high ) < ( bitfield )->bitCount ) ) +static inline tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high ) +{ + return ( b != NULL ) + && ( b->bits != NULL ) + && ( high < b->bitCount ); +} -#define tr_bitfieldHas( bitfield, nth ) \ - ( tr_bitfieldTestFast( bitfield, nth ) \ - && tr_bitfieldHasFast( bitfield, nth ) ) +static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth ) +{ + return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth ); +} double tr_getRatio( double numerator, double denominator );