Ignore:
Timestamp:
Jan 2, 2009, 8:12:23 PM (12 years ago)
Author:
charles
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/utils.h

    r7549 r7581  
    2828#include <inttypes.h>
    2929#include <stdarg.h>
    30 #include <stddef.h> /* for size_t */
     30#include <stddef.h> /* size_t */
    3131#include <stdio.h> /* FILE* */
     32#include <string.h> /* memcpy()* */
     33#include <stdlib.h> /* malloc() */
    3234#include <time.h> /* time_t* */
     35
     36#include "transmission.h"
    3337
    3438#ifdef __cplusplus
     
    278282***/
    279283
     284static inline void* tr_malloc( size_t size )
     285{
     286    return size ? malloc( size ) : NULL;
     287}
     288static inline void* tr_malloc0( size_t size )
     289{
     290    return size ? calloc( 1, size ) : NULL;
     291}
     292static inline void tr_free( void * p )
     293{
     294    if( p != NULL )
     295        free( p );
     296}
     297static inline void* tr_memdup( const void * src, int byteCount )
     298{
     299    return memcpy( tr_malloc( byteCount ), src, byteCount );
     300}
     301
    280302#define tr_new( struct_type, n_structs )           \
    281     ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( (\
    282                                                                              size_t) (\
    283                                                                              n_structs ) ) ) )
     303    ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
     304
    284305#define tr_new0( struct_type, n_structs )          \
    285     ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( (\
    286                                                                               size_t) (\
    287                                                                               n_structs ) ) ) )
     306    ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
     307
    288308#define tr_renew( struct_type, mem, n_structs )    \
    289     ( (struct_type *) realloc ( ( mem ),\
    290                                ( (size_t) sizeof ( struct_type ) ) * ( (\
    291                                                                           size_t) (\
    292                                                                           n_structs ) ) ) )
    293 
    294 void*       tr_malloc( size_t ) TR_GNUC_MALLOC;
    295 
    296 void*       tr_malloc0( size_t ) TR_GNUC_MALLOC;
    297 
    298 void        tr_free( void* );
    299 
    300 char*       tr_strdup( const void * str ) TR_GNUC_MALLOC;
    301 
    302 char*       tr_strndup( const void * str,
    303                         int          len ) TR_GNUC_MALLOC;
    304 
    305 void*       tr_memdup( const void * src,
    306                        int          byteCount ) TR_GNUC_MALLOC;
     309    ( (struct_type *) realloc ( ( mem ), ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
     310
     311char*       tr_strndup( const void * str, int len ) TR_GNUC_MALLOC;
     312
     313static inline char* tr_strdup( const void * in )
     314{
     315    return tr_strndup( in, in ? strlen( (const char*)in ) : 0 );
     316}
    307317
    308318char*       tr_strdup_printf( const char * fmt,
     
    374384tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );
    375385
    376 void         tr_bitfieldDestruct( tr_bitfield* );
    377 
    378 tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
     386tr_bitfield* tr_bitfieldDestruct( tr_bitfield* );
     387
     388static inline tr_bitfield* tr_bitfieldNew( size_t bitcount )
     389{
     390    return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount );
     391}
     392
     393static inline void tr_bitfieldFree( tr_bitfield * b )
     394{
     395    tr_free( tr_bitfieldDestruct( b ) );
     396}
    379397
    380398tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;
    381 
    382 void         tr_bitfieldFree( tr_bitfield* );
    383399
    384400void         tr_bitfieldClear( tr_bitfield* );
     
    405421    need to call tr_bitfieldTestFast() first before you
    406422    start looping. */
    407 #define tr_bitfieldHasFast( bitfield, nth ) \
    408     ( ( (bitfield)->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 )
     423static inline tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth )
     424{
     425    return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0;
     426}
    409427
    410428/** @param high the highest nth bit you're going to access */
    411 #define tr_bitfieldTestFast( bitfield, high ) \
    412     ( ( bitfield ) && ( ( bitfield )->bits )\
    413     && ( ( high ) < ( bitfield )->bitCount ) )
    414 
    415 #define tr_bitfieldHas( bitfield, nth ) \
    416     ( tr_bitfieldTestFast( bitfield, nth )    \
    417     && tr_bitfieldHasFast( bitfield, nth ) )
     429static inline tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )
     430{
     431    return ( b != NULL )
     432        && ( b->bits != NULL )
     433        && ( high < b->bitCount );
     434}
     435
     436static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth )
     437{
     438    return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth );
     439}
    418440
    419441double tr_getRatio( double numerator, double denominator );
Note: See TracChangeset for help on using the changeset viewer.