(libT) 1. tweak tr_bitfieldTestFast(). 2. add tr_memdup()

This commit is contained in:
Charles Kerr
2008-06-06 23:53:30 +00:00
parent 17f686af57
commit 7b88afdca7
2 changed files with 11 additions and 3 deletions

View 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;
}

View 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 );