Ignore:
Timestamp:
Sep 26, 2011, 10:50:42 PM (10 years ago)
Author:
jordan
Message:

(trunk libt) in tr_bitfieldSetRaw(), add a `bounded' argument for cases where we know how large the final bitfield will be. This can be used ensure that the excess bits at the end of the array are zeroed out and safe for bitfield.c's countArray() function.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bitfield.c

    r12904 r12921  
    282282        tr_bitfieldSetHasNone( b );
    283283    else
    284         tr_bitfieldSetRaw( b, src->bits, src->alloc_count );
    285 }
    286 
    287 void
    288 tr_bitfieldSetRaw( tr_bitfield * b, const void * bits, size_t byte_count )
     284        tr_bitfieldSetRaw( b, src->bits, src->alloc_count, true );
     285}
     286
     287void
     288tr_bitfieldSetRaw( tr_bitfield * b, const void * bits, size_t byte_count, bool bounded )
    289289{
    290290    tr_bitfieldFreeArray( b );
    291291    b->true_count = 0;
     292
     293    if( bounded )
     294        byte_count = MIN( byte_count, get_bytes_needed( b->bit_count ) );
     295
    292296    b->bits = tr_memdup( bits, byte_count );
    293297    b->alloc_count = byte_count;
     298
     299    if( bounded ) {
     300        /* ensure the excess bits are set to '0' */
     301        const int excess_bit_count = byte_count*8 - b->bit_count;
     302        assert( excess_bit_count >= 0 );
     303        assert( excess_bit_count <= 7 );
     304        if( excess_bit_count )
     305            b->bits[b->alloc_count-1] &= ((0xff) << excess_bit_count);
     306    }
     307
    294308    tr_bitfieldRebuildTrueCount( b );
    295309}
Note: See TracChangeset for help on using the changeset viewer.