Changeset 12003


Ignore:
Timestamp:
Feb 21, 2011, 1:40:19 AM (11 years ago)
Author:
jordan
Message:

(trunk libT) code cleanup: moving bitset functions to their own .c file.

Location:
trunk/libtransmission
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/Makefile.am

    r11920 r12003  
    2222    bencode.c \
    2323    bitfield.c \
     24    bitset.c \
    2425    blocklist.c \
    2526    cache.c \
  • trunk/libtransmission/bitfield.c

    r11709 r12003  
    211211    return ret;
    212212}
    213 
    214 /***
    215 ****
    216 ***/
    217 
    218 void
    219 tr_bitsetReserve( tr_bitset * b, size_t size )
    220 {
    221     if( b->bitfield.bitCount < size )
    222     {
    223         tr_bitfield * tmp = tr_bitfieldDup( &b->bitfield );
    224 
    225         tr_bitfieldDestruct( &b->bitfield );
    226         tr_bitfieldConstruct( &b->bitfield, size );
    227 
    228         if( ( tmp->bits != NULL ) && ( tmp->byteCount > 0 ) )
    229             memcpy( b->bitfield.bits, tmp->bits, tmp->byteCount );
    230 
    231         tr_bitfieldFree( tmp );
    232     }
    233 }
  • trunk/libtransmission/bitset.h

    r11709 r12003  
    3030tr_bitset;
    3131
    32 static inline void
    33 tr_bitsetConstructor( tr_bitset * b, size_t size )
    34 {
    35     tr_bitfieldConstruct( &b->bitfield, size );
    36 }
     32void tr_bitsetReserve( tr_bitset * b, size_t size );
     33void tr_bitsetConstructor( tr_bitset * b, size_t size );
     34void tr_bitsetDestructor( tr_bitset * b );
    3735
    38 static inline void
    39 tr_bitsetDestructor( tr_bitset * b )
    40 {
    41     tr_bitfieldDestruct( &b->bitfield );
    42 }
     36void tr_bitsetSetHaveAll( tr_bitset * b );
     37void tr_bitsetSetHaveNone( tr_bitset * b );
    4338
    44 void tr_bitsetReserve( tr_bitset * b, size_t size );
     39int  tr_bitsetAdd( tr_bitset * b, size_t i );
    4540
    46 static inline tr_bool
    47 tr_bitsetHasFast( const tr_bitset * b, const size_t nth )
    48 {
    49     if( b->haveAll ) return TRUE;
    50     if( b->haveNone ) return FALSE;
    51     if( nth >= b->bitfield.bitCount ) return FALSE;
    52     return tr_bitfieldHasFast( &b->bitfield, nth );
    53 }
     41/***
     42****
     43***/
    5444
    55 static inline tr_bool
    56 tr_bitsetHas( const tr_bitset * b, const size_t nth )
    57 {
    58     if( b->haveAll ) return TRUE;
    59     if( b->haveNone ) return FALSE;
    60     if( nth >= b->bitfield.bitCount ) return FALSE;
    61     return tr_bitfieldHas( &b->bitfield, nth );
    62 }
     45double tr_bitsetPercent( const tr_bitset * b );
    6346
    64 static inline void
    65 tr_bitsetOr( tr_bitfield * a, const tr_bitset * b )
    66 {
    67     if( b->haveAll )
    68         tr_bitfieldAddRange( a, 0, a->bitCount );
    69     else if( !b->haveNone )
    70         tr_bitfieldOr( a, &b->bitfield );
    71 }
     47tr_bool tr_bitsetHas( const tr_bitset * b, const size_t nth );
     48
     49void tr_bitsetOr( tr_bitfield * a, const tr_bitset * b );
    7250
    7351/* set 'a' to all the flags that were in 'a' but not 'b' */
    74 static inline void
    75 tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b )
    76 {
    77     if( b->haveAll )
    78         tr_bitfieldClear( a );
    79     else if( !b->haveNone )
    80         tr_bitfieldDifference( a, &b->bitfield );
    81 }
    82 
    83 static inline double
    84 tr_bitsetPercent( const tr_bitset * b )
    85 {
    86     if( b->haveAll ) return 1.0;
    87     if( b->haveNone ) return 0.0;
    88     if( b->bitfield.bitCount == 0 ) return 0.0;
    89     return tr_bitfieldCountTrueBits( &b->bitfield ) / (double)b->bitfield.bitCount;
    90 }
    91 
    92 static inline void
    93 tr_bitsetSetHaveAll( tr_bitset * b )
    94 {
    95     b->haveAll = 1;
    96     b->haveNone = 0;
    97 }
    98 
    99 static inline void
    100 tr_bitsetSetHaveNone( tr_bitset * b )
    101 {
    102     b->haveAll = 0;
    103     b->haveNone = 1;
    104 }
    105 
    106 static inline int
    107 tr_bitsetAdd( tr_bitset * b, size_t i )
    108 {
    109     int ret = 0;
    110     if( !b->haveAll ) {
    111         b->haveNone = 0;
    112         tr_bitsetReserve( b, i+1 );
    113         ret = tr_bitfieldAdd( &b->bitfield, i );
    114     }
    115     return ret;
    116 }
     52void tr_bitsetDifference( tr_bitfield * a, const tr_bitset * b );
    11753
    11854#endif
  • trunk/libtransmission/peer-mgr.c

    r11982 r12003  
    476476
    477477        for( peer_i=0; peer_i<peer_count; ++peer_i )
    478             if( tr_bitsetHasFast( &peers[peer_i]->have, piece_i ) )
     478            if( tr_bitsetHas( &peers[peer_i]->have, piece_i ) )
    479479                ++r;
    480480
     
    10241024
    10251025        for( peer_i=0; peer_i<peer_count; ++peer_i )
    1026             if( tr_bitsetHasFast( &peers[peer_i]->have, piece_i ) )
     1026            if( tr_bitsetHas( &peers[peer_i]->have, piece_i ) )
    10271027                ++r;
    10281028
     
    13341334
    13351335        /* if the peer has this piece that we want... */
    1336         if( tr_bitsetHasFast( have, p->index ) )
     1336        if( tr_bitsetHas( have, p->index ) )
    13371337        {
    13381338            tr_block_index_t b = tr_torPieceFirstBlock( tor, p->index );
Note: See TracChangeset for help on using the changeset viewer.