2009-06-15 00:11:06 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-06-15 00:11:06 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2009-06-15 00:11:06 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-06-15 00:11:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2011-03-30 04:14:57 +00:00
|
|
|
#include <stdlib.h> /* realloc() */
|
2009-06-15 00:11:06 +00:00
|
|
|
#include <string.h> /* memset */
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "bitfield.h"
|
2011-02-23 03:54:04 +00:00
|
|
|
#include "utils.h" /* tr_new0() */
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
const tr_bitfield TR_BITFIELD_INIT = { NULL, 0, 0, 0, false, false };
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
static const int8_t trueBitCount[256] =
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2011-03-28 16:31:05 +00:00
|
|
|
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
|
|
|
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
|
|
|
|
};
|
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
static size_t
|
|
|
|
countArray( const tr_bitfield * b )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t ret = 0;
|
|
|
|
|
|
|
|
for( i=0; i<b->alloc_count; ++i )
|
|
|
|
ret += trueBitCount[b->bits[i]];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
static size_t
|
|
|
|
countRange( const tr_bitfield * b, size_t begin, size_t end )
|
|
|
|
{
|
|
|
|
size_t ret = 0;
|
2011-03-30 04:14:57 +00:00
|
|
|
const size_t first_byte = begin >> 3u;
|
|
|
|
const size_t last_byte = ( end - 1 ) >> 3u;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2011-03-29 16:39:30 +00:00
|
|
|
if( !b->bit_count )
|
|
|
|
return 0;
|
2011-03-30 04:14:57 +00:00
|
|
|
if( first_byte >= b->alloc_count )
|
|
|
|
return 0;
|
2011-03-29 16:39:30 +00:00
|
|
|
|
|
|
|
assert( begin < end );
|
2011-03-28 16:31:05 +00:00
|
|
|
assert( b->bits != NULL );
|
|
|
|
|
|
|
|
if( first_byte == last_byte )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t val = b->bits[first_byte];
|
|
|
|
|
|
|
|
i = begin - (first_byte * 8);
|
|
|
|
val <<= i;
|
|
|
|
val >>= i;
|
|
|
|
i = (last_byte+1)*8 - end;
|
|
|
|
val >>= i;
|
|
|
|
val <<= i;
|
|
|
|
|
|
|
|
ret += trueBitCount[val];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
size_t i;
|
2011-03-28 16:31:05 +00:00
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
/* first byte */
|
|
|
|
i = begin - (first_byte * 8);
|
|
|
|
val = b->bits[first_byte];
|
|
|
|
val <<= i;
|
|
|
|
val >>= i;
|
|
|
|
ret += trueBitCount[val];
|
|
|
|
|
|
|
|
/* middle bytes */
|
2011-03-30 04:14:57 +00:00
|
|
|
for( i=first_byte+1; i<b->alloc_count && i<last_byte; ++i )
|
2011-05-10 03:43:06 +00:00
|
|
|
ret += trueBitCount[b->bits[i]];
|
2011-03-28 16:31:05 +00:00
|
|
|
|
|
|
|
/* last byte */
|
2011-03-30 04:14:57 +00:00
|
|
|
if( last_byte < b->alloc_count ) {
|
|
|
|
i = (last_byte+1)*8 - end;
|
|
|
|
val = b->bits[last_byte];
|
|
|
|
val >>= i;
|
|
|
|
val <<= i;
|
|
|
|
ret += trueBitCount[val];
|
|
|
|
}
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert( ret <= ( begin - end ) );
|
|
|
|
return ret;
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
size_t
|
|
|
|
tr_bitfieldCountRange( const tr_bitfield * b, size_t begin, size_t end )
|
|
|
|
{
|
|
|
|
if( tr_bitfieldHasAll( b ) )
|
|
|
|
return end - begin;
|
|
|
|
|
|
|
|
if( tr_bitfieldHasNone( b ) )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return countRange( b, begin, end );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static bool
|
|
|
|
tr_bitfieldIsValid( const tr_bitfield * b )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
assert( b != NULL );
|
|
|
|
assert( ( b->alloc_count == 0 ) == ( b->bits == 0 ) );
|
|
|
|
assert( !b->bits || ( b->true_count == countArray ( b ) ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-04-05 00:29:42 +00:00
|
|
|
size_t
|
|
|
|
tr_bitfieldCountTrueBits( const tr_bitfield * b )
|
|
|
|
{
|
|
|
|
tr_bitfieldIsValid( b );
|
|
|
|
return b->true_count;
|
|
|
|
}
|
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
static size_t
|
|
|
|
get_bytes_needed( size_t bit_count )
|
|
|
|
{
|
|
|
|
return ( bit_count + 7u ) / 8u;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_all_true( uint8_t * array, size_t bit_count )
|
|
|
|
{
|
|
|
|
const uint8_t val = 0xFF;
|
|
|
|
const size_t n = get_bytes_needed( bit_count );
|
|
|
|
memset( array, val, n-1 );
|
|
|
|
array[n-1] = val << (n*8 - bit_count);
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
tr_bitfieldGetRaw( const tr_bitfield * b, size_t * byte_count )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
const size_t n = get_bytes_needed( b->bit_count );
|
|
|
|
uint8_t * bits = tr_new0( uint8_t, n );
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
assert( b->bit_count > 0 );
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
if( b->alloc_count )
|
|
|
|
memcpy( bits, b->bits, b->alloc_count );
|
|
|
|
else if( tr_bitfieldHasAll( b ) )
|
|
|
|
set_all_true( bits, b->bit_count );
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
*byte_count = n;
|
2011-03-28 16:31:05 +00:00
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldEnsureBitsAlloced( tr_bitfield * b, size_t nth )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
size_t bytes_needed;
|
|
|
|
const bool has_all = tr_bitfieldHasAll( b );
|
2011-03-28 16:31:05 +00:00
|
|
|
|
|
|
|
assert( tr_bitfieldIsValid( b ) );
|
2011-03-30 04:14:57 +00:00
|
|
|
|
|
|
|
if( has_all )
|
|
|
|
bytes_needed = get_bytes_needed( MAX( nth, b->true_count ) + 1 );
|
|
|
|
else
|
|
|
|
bytes_needed = get_bytes_needed( nth + 1 );
|
|
|
|
|
|
|
|
if( b->alloc_count < bytes_needed )
|
|
|
|
{
|
|
|
|
const size_t old_count = countArray( b );
|
|
|
|
b->bits = tr_renew( uint8_t, b->bits, bytes_needed );
|
|
|
|
memset( b->bits + b->alloc_count, 0, bytes_needed - b->alloc_count );
|
|
|
|
b->alloc_count = bytes_needed;
|
|
|
|
assert( old_count == countArray( b ) );
|
|
|
|
|
|
|
|
if( has_all )
|
|
|
|
set_all_true( b->bits, b->true_count );
|
|
|
|
}
|
|
|
|
|
|
|
|
assert( tr_bitfieldIsValid( b ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_bitfieldFreeArray( tr_bitfield * b )
|
|
|
|
{
|
|
|
|
tr_free( b->bits );
|
|
|
|
b->bits = NULL;
|
|
|
|
b->alloc_count = 0;
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_bitfieldSetTrueCount( tr_bitfield * b, size_t n )
|
|
|
|
{
|
|
|
|
b->true_count = n;
|
|
|
|
|
|
|
|
if( tr_bitfieldHasAll( b ) || tr_bitfieldHasNone( b ) )
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldFreeArray( b );
|
2011-03-28 16:31:05 +00:00
|
|
|
|
|
|
|
assert( tr_bitfieldIsValid( b ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_bitfieldRebuildTrueCount( tr_bitfield * b )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldSetTrueCount( b, countArray( b ) );
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
static void
|
|
|
|
tr_bitfieldIncTrueCount( tr_bitfield * b, int i )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2011-03-28 16:31:05 +00:00
|
|
|
tr_bitfieldSetTrueCount( b, b->true_count + i );
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2011-02-23 03:54:04 +00:00
|
|
|
void
|
2011-03-28 16:31:05 +00:00
|
|
|
tr_bitfieldConstruct( tr_bitfield * b, size_t bit_count )
|
2011-02-23 03:54:04 +00:00
|
|
|
{
|
2011-03-28 16:31:05 +00:00
|
|
|
b->bit_count = bit_count;
|
|
|
|
b->true_count = 0;
|
|
|
|
b->bits = NULL;
|
2011-03-30 04:14:57 +00:00
|
|
|
b->alloc_count = 0;
|
2011-03-28 16:31:05 +00:00
|
|
|
b->have_all_hint = false;
|
|
|
|
b->have_none_hint = false;
|
|
|
|
|
|
|
|
assert( tr_bitfieldIsValid( b ) );
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
void
|
|
|
|
tr_bitfieldSetHasNone( tr_bitfield * b )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldFreeArray( b );
|
|
|
|
b->true_count = 0;
|
2011-03-28 16:31:05 +00:00
|
|
|
b->have_all_hint = false;
|
|
|
|
b->have_none_hint = true;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
|
|
|
assert( tr_bitfieldIsValid( b ) );
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
void
|
|
|
|
tr_bitfieldSetHasAll( tr_bitfield * b )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldFreeArray( b );
|
2011-03-28 16:31:05 +00:00
|
|
|
b->true_count = b->bit_count;
|
|
|
|
b->have_all_hint = true;
|
|
|
|
b->have_none_hint = false;
|
2011-03-30 04:14:57 +00:00
|
|
|
|
|
|
|
assert( tr_bitfieldIsValid( b ) );
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
void
|
2011-03-28 16:31:05 +00:00
|
|
|
tr_bitfieldSetFromBitfield( tr_bitfield * b, const tr_bitfield * src )
|
|
|
|
{
|
|
|
|
if( tr_bitfieldHasAll( src ) )
|
|
|
|
tr_bitfieldSetHasAll( b );
|
|
|
|
else if( tr_bitfieldHasNone( src ) )
|
|
|
|
tr_bitfieldSetHasNone( b );
|
|
|
|
else
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldSetRaw( b, src->bits, src->alloc_count );
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
void
|
2011-03-28 16:31:05 +00:00
|
|
|
tr_bitfieldSetRaw( tr_bitfield * b, const void * bits, size_t byte_count )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldFreeArray( b );
|
|
|
|
b->true_count = 0;
|
|
|
|
b->bits = tr_memdup( bits, byte_count );
|
|
|
|
b->alloc_count = byte_count;
|
|
|
|
tr_bitfieldRebuildTrueCount( b );
|
2011-03-28 16:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_bitfieldAdd( tr_bitfield * b, size_t nth )
|
|
|
|
{
|
|
|
|
if( !tr_bitfieldHas( b, nth ) )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldEnsureBitsAlloced( b, nth );
|
2011-03-28 16:31:05 +00:00
|
|
|
b->bits[nth >> 3u] |= ( 0x80 >> ( nth & 7u ) );
|
|
|
|
tr_bitfieldIncTrueCount( b, 1 );
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sets bit range [begin, end) to 1 */
|
2011-03-28 16:31:05 +00:00
|
|
|
void
|
|
|
|
tr_bitfieldAddRange( tr_bitfield * b, size_t begin, size_t end )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2011-03-28 16:31:05 +00:00
|
|
|
size_t sb, eb;
|
2009-06-15 00:11:06 +00:00
|
|
|
unsigned char sm, em;
|
2011-03-28 16:31:05 +00:00
|
|
|
const size_t diff = (end-begin) - tr_bitfieldCountRange( b, begin, end );
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
if( diff == 0 )
|
|
|
|
return;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
end--;
|
|
|
|
if( ( end >= b->bit_count ) || ( begin > end ) )
|
|
|
|
return;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
|
|
sb = begin >> 3;
|
|
|
|
sm = ~( 0xff << ( 8 - ( begin & 7 ) ) );
|
|
|
|
eb = end >> 3;
|
|
|
|
em = 0xff << ( 7 - ( end & 7 ) );
|
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldEnsureBitsAlloced( b, end );
|
2009-06-15 00:11:06 +00:00
|
|
|
if( sb == eb )
|
|
|
|
{
|
|
|
|
b->bits[sb] |= ( sm & em );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b->bits[sb] |= sm;
|
|
|
|
b->bits[eb] |= em;
|
|
|
|
if( ++sb < eb )
|
|
|
|
memset ( b->bits + sb, 0xff, eb - sb );
|
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
tr_bitfieldIncTrueCount( b, diff );
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
void
|
|
|
|
tr_bitfieldRem( tr_bitfield * b, size_t nth )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2011-03-28 16:31:05 +00:00
|
|
|
assert( tr_bitfieldIsValid( b ) );
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
if( !tr_bitfieldHas( b, nth ) )
|
|
|
|
{
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldEnsureBitsAlloced( b, nth );
|
2011-03-28 16:31:05 +00:00
|
|
|
b->bits[nth >> 3u] &= ( 0xff7f >> ( nth & 7u ) );
|
|
|
|
tr_bitfieldIncTrueCount( b, -1 );
|
|
|
|
}
|
2009-06-15 00:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clears bit range [begin, end) to 0 */
|
2011-03-28 16:31:05 +00:00
|
|
|
void
|
|
|
|
tr_bitfieldRemRange( tr_bitfield * b, size_t begin, size_t end )
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2011-03-28 16:31:05 +00:00
|
|
|
size_t sb, eb;
|
2009-06-15 00:11:06 +00:00
|
|
|
unsigned char sm, em;
|
2011-03-28 16:31:05 +00:00
|
|
|
const size_t diff = tr_bitfieldCountRange( b, begin, end );
|
|
|
|
|
|
|
|
if( !diff )
|
|
|
|
return;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
|
|
end--;
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
if( ( end >= b->bit_count ) || ( begin > end ) )
|
|
|
|
return;
|
2009-06-15 00:11:06 +00:00
|
|
|
|
|
|
|
sb = begin >> 3;
|
|
|
|
sm = 0xff << ( 8 - ( begin & 7 ) );
|
|
|
|
eb = end >> 3;
|
|
|
|
em = ~( 0xff << ( 7 - ( end & 7 ) ) );
|
|
|
|
|
2011-03-30 04:14:57 +00:00
|
|
|
tr_bitfieldEnsureBitsAlloced( b, end );
|
2009-06-15 00:11:06 +00:00
|
|
|
if( sb == eb )
|
|
|
|
{
|
|
|
|
b->bits[sb] &= ( sm | em );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b->bits[sb] &= sm;
|
|
|
|
b->bits[eb] &= em;
|
|
|
|
if( ++sb < eb )
|
|
|
|
memset ( b->bits + sb, 0, eb - sb );
|
|
|
|
}
|
|
|
|
|
2011-03-28 16:31:05 +00:00
|
|
|
tr_bitfieldIncTrueCount( b, -diff );
|
2011-02-23 03:54:04 +00:00
|
|
|
}
|