Changeset 8695
- Timestamp:
- Jun 15, 2009, 12:11:06 AM (13 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/Makefile.am
r8690 r8695 13 13 bandwidth.c \ 14 14 bencode.c \ 15 bitfield.c \ 15 16 blocklist.c \ 16 17 clients.c \ … … 60 61 bandwidth.h \ 61 62 bencode.h \ 63 bitfield.h \ 62 64 blocklist.h \ 63 65 clients.h \ -
trunk/libtransmission/completion.h
r8212 r8695 21 21 22 22 #include "transmission.h" 23 #include "bitfield.h" 23 24 #include "utils.h" /* tr_bitfield */ 24 25 -
trunk/libtransmission/peer-mgr.h
r8561 r8695 24 24 #endif 25 25 26 #include "bitfield.h" 26 27 #include "net.h" 27 28 #include "publish.h" /* tr_publisher_tag */ 29 #include "utils.h" 28 30 29 31 /** … … 55 57 56 58 struct tr_bandwidth; 57 struct tr_bitfield;58 59 struct tr_peerIo; 59 60 struct tr_peermsgs; -
trunk/libtransmission/utils-test.c
r8478 r8695 1 1 #include <stdio.h> /* fprintf */ 2 2 #include <string.h> /* strcmp */ 3 3 4 #include "transmission.h" 5 #include "bitfield.h" 4 6 #include "ConvertUTF.h" /* tr_utf8_validate*/ 5 7 #include "platform.h" -
trunk/libtransmission/utils.c
r8686 r8695 794 794 ***** 795 795 ****/ 796 797 tr_bitfield*798 tr_bitfieldConstruct( tr_bitfield * b, size_t bitCount )799 {800 b->bitCount = bitCount;801 b->byteCount = ( bitCount + 7u ) / 8u;802 b->bits = tr_new0( uint8_t, b->byteCount );803 return b;804 }805 806 tr_bitfield*807 tr_bitfieldDestruct( tr_bitfield * b )808 {809 if( b )810 tr_free( b->bits );811 return b;812 }813 814 tr_bitfield*815 tr_bitfieldDup( const tr_bitfield * in )816 {817 tr_bitfield * ret = tr_new0( tr_bitfield, 1 );818 819 ret->bitCount = in->bitCount;820 ret->byteCount = in->byteCount;821 ret->bits = tr_memdup( in->bits, in->byteCount );822 return ret;823 }824 825 void826 tr_bitfieldClear( tr_bitfield * bitfield )827 {828 memset( bitfield->bits, 0, bitfield->byteCount );829 }830 831 int832 tr_bitfieldIsEmpty( const tr_bitfield * bitfield )833 {834 size_t i;835 836 for( i = 0; i < bitfield->byteCount; ++i )837 if( bitfield->bits[i] )838 return 0;839 840 return 1;841 }842 843 int844 tr_bitfieldAdd( tr_bitfield * bitfield,845 size_t nth )846 {847 assert( bitfield );848 assert( bitfield->bits );849 850 if( nth >= bitfield->bitCount )851 return -1;852 853 bitfield->bits[nth >> 3u] |= ( 0x80 >> ( nth & 7u ) );854 return 0;855 }856 857 /* Sets bit range [begin, end) to 1 */858 int859 tr_bitfieldAddRange( tr_bitfield * b,860 size_t begin,861 size_t end )862 {863 size_t sb, eb;864 unsigned char sm, em;865 866 end--;867 868 if( ( end >= b->bitCount ) || ( begin > end ) )869 return -1;870 871 sb = begin >> 3;872 sm = ~( 0xff << ( 8 - ( begin & 7 ) ) );873 eb = end >> 3;874 em = 0xff << ( 7 - ( end & 7 ) );875 876 if( sb == eb )877 {878 b->bits[sb] |= ( sm & em );879 }880 else881 {882 b->bits[sb] |= sm;883 b->bits[eb] |= em;884 if( ++sb < eb )885 memset ( b->bits + sb, 0xff, eb - sb );886 }887 888 return 0;889 }890 891 int892 tr_bitfieldRem( tr_bitfield * bitfield,893 size_t nth )894 {895 assert( bitfield );896 assert( bitfield->bits );897 898 if( nth >= bitfield->bitCount )899 return -1;900 901 bitfield->bits[nth >> 3u] &= ( 0xff7f >> ( nth & 7u ) );902 return 0;903 }904 905 /* Clears bit range [begin, end) to 0 */906 int907 tr_bitfieldRemRange( tr_bitfield * b,908 size_t begin,909 size_t end )910 {911 size_t sb, eb;912 unsigned char sm, em;913 914 end--;915 916 if( ( end >= b->bitCount ) || ( begin > end ) )917 return -1;918 919 sb = begin >> 3;920 sm = 0xff << ( 8 - ( begin & 7 ) );921 eb = end >> 3;922 em = ~( 0xff << ( 7 - ( end & 7 ) ) );923 924 if( sb == eb )925 {926 b->bits[sb] &= ( sm | em );927 }928 else929 {930 b->bits[sb] &= sm;931 b->bits[eb] &= em;932 if( ++sb < eb )933 memset ( b->bits + sb, 0, eb - sb );934 }935 936 return 0;937 }938 939 tr_bitfield*940 tr_bitfieldOr( tr_bitfield * a,941 const tr_bitfield * b )942 {943 uint8_t * ait;944 const uint8_t *aend, *bit;945 946 assert( a->bitCount == b->bitCount );947 948 for( ait = a->bits, bit = b->bits, aend = ait + a->byteCount;949 ait != aend; )950 *ait++ |= *bit++;951 952 return a;953 }954 955 /* set 'a' to all the flags that were in 'a' but not 'b' */956 void957 tr_bitfieldDifference( tr_bitfield * a,958 const tr_bitfield * b )959 {960 uint8_t * ait;961 const uint8_t *aend, *bit;962 963 assert( a->bitCount == b->bitCount );964 965 for( ait = a->bits, bit = b->bits, aend = ait + a->byteCount;966 ait != aend; )967 *ait++ &= ~( *bit++ );968 }969 970 size_t971 tr_bitfieldCountTrueBits( const tr_bitfield* b )972 {973 size_t ret = 0;974 const uint8_t * it, *end;975 static const int trueBitCount[512] = {976 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3,977 4, 2, 3, 3, 4, 3, 4, 4, 5,978 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,979 5, 3, 4, 4, 5, 4, 5, 5, 6,980 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,981 5, 3, 4, 4, 5, 4, 5, 5, 6,982 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,983 6, 4, 5, 5, 6, 5, 6, 6, 7,984 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4,985 5, 3, 4, 4, 5, 4, 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6, 6, 7, 6, 7, 7, 8, 5, 6, 6, 7, 6, 7, 7,1007 8, 6, 7, 7, 8, 7, 8, 8, 91008 };1009 1010 if( !b )1011 return 0;1012 1013 for( it = b->bits, end = it + b->byteCount; it != end; ++it )1014 ret += trueBitCount[*it];1015 1016 return ret;1017 }1018 1019 /***1020 ****1021 ***/1022 796 1023 797 uint64_t -
trunk/libtransmission/utils.h
r8685 r8695 380 380 char ** setme_path ); 381 381 382 383 /***384 ****385 ***/386 387 typedef struct tr_bitfield388 {389 uint8_t * bits;390 size_t bitCount;391 size_t byteCount;392 }393 tr_bitfield;394 395 tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );396 397 tr_bitfield* tr_bitfieldDestruct( tr_bitfield* );398 399 static TR_INLINE tr_bitfield* tr_bitfieldNew( size_t bitcount )400 {401 return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitcount );402 }403 404 static TR_INLINE void tr_bitfieldFree( tr_bitfield * b )405 {406 tr_free( tr_bitfieldDestruct( b ) );407 }408 409 tr_bitfield* tr_bitfieldDup( const tr_bitfield* ) TR_GNUC_MALLOC;410 411 void tr_bitfieldClear( tr_bitfield* );412 413 int tr_bitfieldAdd( tr_bitfield*, size_t bit );414 415 int tr_bitfieldRem( tr_bitfield*, size_t bit );416 417 int tr_bitfieldAddRange( tr_bitfield *, size_t begin, size_t end );418 419 int tr_bitfieldRemRange( tr_bitfield*, size_t begin, size_t end );420 421 void tr_bitfieldDifference( tr_bitfield *, const tr_bitfield * );422 423 int tr_bitfieldIsEmpty( const tr_bitfield* );424 425 size_t tr_bitfieldCountTrueBits( const tr_bitfield* );426 427 tr_bitfield* tr_bitfieldOr( tr_bitfield*, const tr_bitfield* );428 429 /** A stripped-down version of bitfieldHas to be used430 for speed when you're looping quickly. This version431 has none of tr_bitfieldHas()'s safety checks, so you432 need to call tr_bitfieldTestFast() first before you433 start looping. */434 static TR_INLINE tr_bool tr_bitfieldHasFast( const tr_bitfield * b, const size_t nth )435 {436 return ( b->bits[nth>>3u] << ( nth & 7u ) & 0x80 ) != 0;437 }438 439 /** @param high the highest nth bit you're going to access */440 static TR_INLINE tr_bool tr_bitfieldTestFast( const tr_bitfield * b, const size_t high )441 {442 return ( b != NULL )443 && ( b->bits != NULL )444 && ( high < b->bitCount );445 }446 447 static TR_INLINE tr_bool tr_bitfieldHas( const tr_bitfield * b, size_t nth )448 {449 return tr_bitfieldTestFast( b, nth ) && tr_bitfieldHasFast( b, nth );450 }451 452 382 double tr_getRatio( double numerator, double denominator ); 453 383
Note: See TracChangeset
for help on using the changeset viewer.