Changeset 7581
- Timestamp:
- Jan 2, 2009, 8:12:23 PM (12 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/crypto.c
r7404 r7581 31 31 #include "utils.h" 32 32 33 typedef uint8_t tr_bool;34 35 33 /** 36 34 *** -
trunk/libtransmission/utils.c
r7574 r7581 643 643 ****/ 644 644 645 void*646 tr_memdup( const void * in,647 int byteCount )648 {649 void * out = tr_new( uint8_t, byteCount );650 651 memcpy( out, in, byteCount );652 return out;653 }654 655 char*656 tr_strdup( const void * in )657 {658 return tr_strndup( in, in ? strlen( (const char*)in ) : 0 );659 }660 661 645 char* 662 646 tr_strndup( const void * in, … … 695 679 tr_releaseBuffer( buf ); 696 680 return ret; 697 }698 699 void*700 tr_malloc( size_t size )701 {702 return size ? malloc( size ) : NULL;703 }704 705 void*706 tr_malloc0( size_t size )707 {708 return size ? calloc( 1, size ) : NULL;709 }710 711 void712 tr_free( void * p )713 {714 if( p )715 free( p );716 681 } 717 682 … … 767 732 } 768 733 769 void 734 tr_bitfield* 770 735 tr_bitfieldDestruct( tr_bitfield * b ) 771 736 { 772 tr_free( b->bits ); 773 } 774 775 tr_bitfield* 776 tr_bitfieldNew( size_t bitCount ) 777 { 778 return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitCount ); 737 if( b ) 738 tr_free( b->bits ); 739 return b; 779 740 } 780 741 … … 788 749 ret->bits = tr_memdup( in->bits, in->byteCount ); 789 750 return ret; 790 }791 792 void793 tr_bitfieldFree( tr_bitfield * bitfield )794 {795 if( bitfield )796 {797 tr_bitfieldDestruct( bitfield );798 tr_free( bitfield );799 }800 751 } 801 752 -
trunk/libtransmission/utils.h
r7549 r7581 28 28 #include <inttypes.h> 29 29 #include <stdarg.h> 30 #include <stddef.h> /* forsize_t */30 #include <stddef.h> /* size_t */ 31 31 #include <stdio.h> /* FILE* */ 32 #include <string.h> /* memcpy()* */ 33 #include <stdlib.h> /* malloc() */ 32 34 #include <time.h> /* time_t* */ 35 36 #include "transmission.h" 33 37 34 38 #ifdef __cplusplus … … 278 282 ***/ 279 283 284 static inline void* tr_malloc( size_t size ) 285 { 286 return size ? malloc( size ) : NULL; 287 } 288 static inline void* tr_malloc0( size_t size ) 289 { 290 return size ? calloc( 1, size ) : NULL; 291 } 292 static inline void tr_free( void * p ) 293 { 294 if( p != NULL ) 295 free( p ); 296 } 297 static inline void* tr_memdup( const void * src, int byteCount ) 298 { 299 return memcpy( tr_malloc( byteCount ), src, byteCount ); 300 } 301 280 302 #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 284 305 #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 288 308 #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 311 char* tr_strndup( const void * str, int len ) TR_GNUC_MALLOC; 312 313 static inline char* tr_strdup( const void * in ) 314 { 315 return tr_strndup( in, in ? strlen( (const char*)in ) : 0 ); 316 } 307 317 308 318 char* tr_strdup_printf( const char * fmt, … … 374 384 tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount ); 375 385 376 void tr_bitfieldDestruct( tr_bitfield* ); 377 378 tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC; 386 tr_bitfield* tr_bitfieldDestruct( tr_bitfield* ); 387 388 static inline tr_bitfield* tr_bitfieldNew( size_t bitcount ) 389 { 390 return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount ); 391 } 392 393 static inline void tr_bitfieldFree( tr_bitfield * b ) 394 { 395 tr_free( tr_bitfieldDestruct( b ) ); 396 } 379 397 380 398 tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC; 381 382 void tr_bitfieldFree( tr_bitfield* );383 399 384 400 void tr_bitfieldClear( tr_bitfield* ); … … 405 421 need to call tr_bitfieldTestFast() first before you 406 422 start looping. */ 407 #define tr_bitfieldHasFast( bitfield, nth ) \ 408 ( ( (bitfield)->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 ) 423 static 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 } 409 427 410 428 /** @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 ) ) 429 static 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 436 static inline tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth ) 437 { 438 return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth ); 439 } 418 440 419 441 double tr_getRatio( double numerator, double denominator );
Note: See TracChangeset
for help on using the changeset viewer.