From 7b88afdca7055e42a0cf93c543c34a27d7ae5a40 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 6 Jun 2008 23:53:30 +0000 Subject: [PATCH] (libT) 1. tweak tr_bitfieldTestFast(). 2. add tr_memdup() --- libtransmission/utils.c | 11 +++++++++-- libtransmission/utils.h | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libtransmission/utils.c b/libtransmission/utils.c index b52f4a185..93c1aa443 100644 --- a/libtransmission/utils.c +++ b/libtransmission/utils.c @@ -588,6 +588,14 @@ tr_errorString( int code ) ***** ****/ +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 char * in ) { @@ -692,8 +700,7 @@ tr_bitfieldDup( const tr_bitfield * in ) { tr_bitfield * ret = calloc( 1, sizeof(tr_bitfield) ); ret->len = in->len; - ret->bits = malloc( ret->len ); - memcpy( ret->bits, in->bits, ret->len ); + ret->bits = tr_memdup( in->bits, in->len ); return ret; } diff --git a/libtransmission/utils.h b/libtransmission/utils.h index cfa61a622..3b8283695 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -190,6 +190,7 @@ void tr_free ( void* ); char* tr_strdup( const char * str ) TR_GNUC_MALLOC; char* tr_strndup( const char * str, int len ) TR_GNUC_MALLOC; +void* tr_memdup( const void * src, int byteCount ) TR_GNUC_MALLOC; char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 ) TR_GNUC_MALLOC; char* tr_base64_encode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC; char* tr_base64_decode( const void * input, int inlen, int *outlen ) TR_GNUC_MALLOC; @@ -271,7 +272,7 @@ tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* ); /** @param high the highest nth bit you're going to access */ #define tr_bitfieldTestFast(bitfield,high) \ - ((bitfield) && ( (bitfield)->bits ) && ( ((high)>>3u) < (bitfield)->len )) + ((bitfield) && ( (bitfield)->bits ) && ( (high) <= (bitfield)->len*8 )) double tr_getRatio( double numerator, double denominator ); -- 2.40.0