]> granicus.if.org Git - transmission/commitdiff
(libT) 1. tweak tr_bitfieldTestFast(). 2. add tr_memdup()
authorCharles Kerr <charles@transmissionbt.com>
Fri, 6 Jun 2008 23:53:30 +0000 (23:53 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Fri, 6 Jun 2008 23:53:30 +0000 (23:53 +0000)
libtransmission/utils.c
libtransmission/utils.h

index b52f4a1853413e376144705e60c50fba970ecd74..93c1aa4434ac9c9e6efc5d334ed351fe34a17547 100644 (file)
@@ -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;
 }
 
index cfa61a6220ce2c027a9d8ab3662734a38bfb969d..3b8283695c89b0573aa2927b1a6f61876d794fea 100644 (file)
@@ -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 );