tr_torrent * tor;
/* do we have this block? */
- tr_bitfield * blockBitfield;
+ tr_bitfield blockBitfield;
/* do we have this piece? */
- tr_bitfield * pieceBitfield;
+ tr_bitfield pieceBitfield;
/* a block is complete if and only if we have it */
uint16_t * completeBlocks;
static void
tr_cpReset( tr_completion * cp )
{
- tr_bitfieldClear( cp->pieceBitfield );
- tr_bitfieldClear( cp->blockBitfield );
+ tr_bitfieldClear( &cp->pieceBitfield );
+ tr_bitfieldClear( &cp->blockBitfield );
memset( cp->completeBlocks, 0,
sizeof( uint16_t ) * cp->tor->info.pieceCount );
cp->sizeNow = 0;
tr_completion * cp = tr_new( tr_completion, 1 );
cp->tor = tor;
- cp->blockBitfield = tr_bitfieldNew( tor->blockCount );
- cp->pieceBitfield = tr_bitfieldNew( tor->info.pieceCount );
cp->completeBlocks = tr_new( uint16_t, tor->info.pieceCount );
+ tr_bitfieldConstruct( &cp->blockBitfield, tor->blockCount );
+ tr_bitfieldConstruct( &cp->pieceBitfield, tor->info.pieceCount );
tr_cpReset( cp );
return cp;
}
void
tr_cpClose( tr_completion * cp )
{
- tr_free ( cp->completeBlocks );
- tr_bitfieldFree( cp->pieceBitfield );
- tr_bitfieldFree( cp->blockBitfield );
+ tr_free( cp->completeBlocks );
+ tr_bitfieldDestruct( &cp->pieceBitfield );
+ tr_bitfieldDestruct( &cp->blockBitfield );
tr_free ( cp );
}
const tr_bitfield *
tr_cpPieceBitfield( const tr_completion * cp )
{
- return cp->pieceBitfield;
+ return &cp->pieceBitfield;
}
void
cp->sizeWhenDoneIsDirty = 1;
cp->haveValidIsDirty = 1;
cp->completeBlocks[piece] = 0;
- tr_bitfieldRemRange ( cp->blockBitfield, start, end );
- tr_bitfieldRem( cp->pieceBitfield, piece );
+ tr_bitfieldRemRange ( &cp->blockBitfield, start, end );
+ tr_bitfieldRem( &cp->pieceBitfield, piece );
}
int
tr_cpBlockIsComplete( const tr_completion * cp,
tr_block_index_t block )
{
- return tr_bitfieldHas( cp->blockBitfield, block );
+ return tr_bitfieldHas( &cp->blockBitfield, block );
}
void
++cp->completeBlocks[piece];
if( tr_cpPieceIsComplete( cp, piece ) )
- tr_bitfieldAdd( cp->pieceBitfield, piece );
+ tr_bitfieldAdd( &cp->pieceBitfield, piece );
- tr_bitfieldAdd( cp->blockBitfield, block );
+ tr_bitfieldAdd( &cp->blockBitfield, block );
cp->sizeNow += blockSize;
tr_cpBlockBitfield( const tr_completion * cp )
{
assert( cp );
- assert( cp->blockBitfield );
- assert( cp->blockBitfield->bits );
- assert( cp->blockBitfield->bitCount );
+ assert( cp->blockBitfield.bits );
+ assert( cp->blockBitfield.bitCount );
- return cp->blockBitfield;
+ return &cp->blockBitfield;
}
int
assert( cp );
assert( bitfield );
- assert( cp->blockBitfield );
if( tr_bitfieldTestFast( bitfield, cp->tor->blockCount - 1 ) )
{
tor->error = 0;
- tor->checkedPieces = tr_bitfieldNew( tor->info.pieceCount );
+ tr_bitfieldConstruct( &tor->checkedPieces, tor->info.pieceCount );
tr_torrentUncheck( tor );
tor->addedDate = time( NULL ); /* this is a default value to be
tr_trackerFree( tor->tracker );
tor->tracker = NULL;
- tr_bitfieldFree( tor->checkedPieces );
+ tr_bitfieldDestruct( &tor->checkedPieces );
tr_free( tor->downloadDir );
tr_free( tor->peer_id );
tr_torrentIsPieceChecked( const tr_torrent * tor,
tr_piece_index_t piece )
{
- return tr_bitfieldHas( tor->checkedPieces, piece );
+ return tr_bitfieldHas( &tor->checkedPieces, piece );
}
void
tr_bool isChecked )
{
if( isChecked )
- tr_bitfieldAdd( tor->checkedPieces, piece );
+ tr_bitfieldAdd( &tor->checkedPieces, piece );
else
- tr_bitfieldRem( tor->checkedPieces, piece );
+ tr_bitfieldRem( &tor->checkedPieces, piece );
}
void
const tr_piece_index_t end = file->lastPiece + 1;
if( isChecked )
- tr_bitfieldAddRange ( tor->checkedPieces, begin, end );
+ tr_bitfieldAddRange ( &tor->checkedPieces, begin, end );
else
- tr_bitfieldRemRange ( tor->checkedPieces, begin, end );
+ tr_bitfieldRemRange ( &tor->checkedPieces, begin, end );
}
tr_bool
void
tr_torrentUncheck( tr_torrent * tor )
{
- tr_bitfieldRemRange ( tor->checkedPieces, 0, tor->info.pieceCount );
+ tr_bitfieldRemRange ( &tor->checkedPieces, 0, tor->info.pieceCount );
}
int
tr_torrentCountUncheckedPieces( const tr_torrent * tor )
{
- return tor->info.pieceCount - tr_bitfieldCountTrueBits(
- tor->checkedPieces );
+ return tor->info.pieceCount - tr_bitfieldCountTrueBits( &tor->checkedPieces );
}
time_t*
#error only libtransmission should #include this header.
#endif
+#include "utils.h" /* tr_bitfield */
+
#ifndef TR_TORRENT_H
#define TR_TORRENT_H 1
struct tr_completion * completion;
- struct tr_bitfield * checkedPieces;
+ struct tr_bitfield checkedPieces;
tr_completeness completeness;
struct tr_tracker * tracker;
****/
tr_bitfield*
-tr_bitfieldNew( size_t bitCount )
+tr_bitfieldConstruct( tr_bitfield * b, size_t bitCount )
{
- tr_bitfield * ret = tr_new0( tr_bitfield, 1 );
+ b->bitCount = bitCount;
+ b->byteCount = ( bitCount + 7u ) / 8u;
+ b->bits = tr_new0( uint8_t, b->byteCount );
+ return b;
+}
- ret->bitCount = bitCount;
- ret->byteCount = ( bitCount + 7u ) / 8u;
- ret->bits = tr_new0( uint8_t, ret->byteCount );
- return ret;
+void
+tr_bitfieldDestruct( tr_bitfield * b )
+{
+ tr_free( b->bits );
+}
+
+tr_bitfield*
+tr_bitfieldNew( size_t bitCount )
+{
+ return tr_bitfieldConstruct( tr_new0( tr_bitfield, 1 ), bitCount );
}
tr_bitfield*
{
if( bitfield )
{
- tr_free( bitfield->bits );
+ tr_bitfieldDestruct( bitfield );
tr_free( bitfield );
}
}
****
***/
-struct tr_bitfield
+typedef struct tr_bitfield
{
uint8_t * bits;
size_t bitCount;
size_t byteCount;
-};
+}
+tr_bitfield;
+
+tr_bitfield* tr_bitfieldConstruct( tr_bitfield*, size_t bitcount );
-typedef struct tr_bitfield tr_bitfield;
+void tr_bitfieldDestruct( tr_bitfield* );
tr_bitfield* tr_bitfieldNew( size_t bitcount ) TR_GNUC_MALLOC;
need to call tr_bitfieldTestFast() first before you
start looping. */
#define tr_bitfieldHasFast( bitfield, nth ) \
- ( ( bitfield->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 )
+ ( ( (bitfield)->bits[( nth ) >> 3u] << ( ( nth ) & 7u ) & 0x80 ) != 0 )
/** @param high the highest nth bit you're going to access */
#define tr_bitfieldTestFast( bitfield, high ) \