(trunk libT) make the tr_bandwidth macros into safer inline funcs. inline utils' one-liners.

This commit is contained in:
Charles Kerr 2009-01-02 20:12:23 +00:00
parent b41cf9f231
commit 5e9af46612
3 changed files with 61 additions and 90 deletions

View File

@ -30,8 +30,6 @@
#include "crypto.h"
#include "utils.h"
typedef uint8_t tr_bool;
/**
***
**/

View File

@ -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 )
{

View File

@ -27,10 +27,14 @@
#include <inttypes.h>
#include <stdarg.h>
#include <stddef.h> /* for size_t */
#include <stddef.h> /* size_t */
#include <stdio.h> /* FILE* */
#include <string.h> /* memcpy()* */
#include <stdlib.h> /* malloc() */
#include <time.h> /* 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 );