add inline wrapper functions to tr_torrent to decouple the rest of the code from tr_completion

This commit is contained in:
Jordan Lee 2013-08-18 13:06:39 +00:00
parent 4a904044e4
commit 35638e210e
9 changed files with 120 additions and 60 deletions

View File

@ -919,12 +919,12 @@ announce_request_new (const tr_announcer * announcer,
req->down = tier->byteCounts[TR_ANN_DOWN];
req->corrupt = tier->byteCounts[TR_ANN_CORRUPT];
req->leftUntilComplete = tr_torrentHasMetadata (tor)
? tor->info.totalSize - tr_cpHaveTotal (&tor->completion)
? tor->info.totalSize - tr_torrentHaveTotal (tor)
: ~ (uint64_t)0;
req->event = event;
req->numwant = event == TR_ANNOUNCE_EVENT_STOPPED ? 0 : NUMWANT;
req->key = announcer->key;
req->partial_seed = tr_cpGetStatus (&tor->completion) == TR_PARTIAL_SEED;
req->partial_seed = tr_torrentGetCompleteness (tor) == TR_PARTIAL_SEED;
tier_build_log_name (tier, req->log_name, sizeof (req->log_name));
return req;
}

View File

@ -105,7 +105,7 @@ getBlockRun (const tr_cache * cache, int pos, struct run_info * info)
{
const struct cache_block * b = blocks[i-1];
info->last_block_time = b->time;
info->is_piece_done = tr_cpPieceIsComplete (&b->tor->completion, b->piece);
info->is_piece_done = tr_torrentPieceIsComplete (b->tor, b->piece);
info->is_multi_piece = b->piece != blocks[pos]->piece ? true : false;
info->len = i - pos;
info->pos = pos;

View File

@ -23,28 +23,28 @@
typedef struct tr_completion
{
tr_torrent * tor;
tr_torrent * tor;
tr_bitfield blockBitfield;
tr_bitfield blockBitfield;
/* number of bytes we'll have when done downloading. [0..info.totalSize]
DON'T access this directly; it's a lazy field.
use tr_cpSizeWhenDone () instead! */
uint64_t sizeWhenDoneLazy;
/* number of bytes we'll have when done downloading. [0..info.totalSize]
DON'T access this directly; it's a lazy field.
use tr_cpSizeWhenDone () instead! */
uint64_t sizeWhenDoneLazy;
/* whether or not sizeWhenDone needs to be recalculated */
bool sizeWhenDoneIsDirty;
/* whether or not sizeWhenDone needs to be recalculated */
bool sizeWhenDoneIsDirty;
/* number of bytes we'll have when done downloading. [0..info.totalSize]
DON'T access this directly; it's a lazy field.
use tr_cpHaveValid () instead! */
uint64_t haveValidLazy;
/* number of bytes we'll have when done downloading. [0..info.totalSize]
DON'T access this directly; it's a lazy field.
use tr_cpHaveValid () instead! */
uint64_t haveValidLazy;
/* whether or not haveValidLazy needs to be recalculated */
bool haveValidIsDirty;
/* whether or not haveValidLazy needs to be recalculated */
bool haveValidIsDirty;
/* number of bytes we want or have now. [0..sizeWhenDone] */
uint64_t sizeNow;
/* number of bytes we want or have now. [0..sizeWhenDone] */
uint64_t sizeNow;
}
tr_completion;
@ -59,7 +59,7 @@ void tr_cpBlockInit (tr_completion * cp, const tr_bitfield * blocks);
static inline void
tr_cpDestruct (tr_completion * cp)
{
tr_bitfieldDestruct (&cp->blockBitfield);
tr_bitfieldDestruct (&cp->blockBitfield);
}
/**
@ -86,19 +86,19 @@ void tr_cpGetAmountDone (const tr_completion * completion,
static inline uint64_t
tr_cpHaveTotal (const tr_completion * cp)
{
return cp->sizeNow;
return cp->sizeNow;
}
static inline bool tr_cpHasAll (const tr_completion * cp)
{
return tr_torrentHasMetadata (cp->tor)
&& tr_bitfieldHasAll (&cp->blockBitfield);
return tr_torrentHasMetadata (cp->tor)
&& tr_bitfieldHasAll (&cp->blockBitfield);
}
static inline bool tr_cpHasNone (const tr_completion * cp)
{
return !tr_torrentHasMetadata (cp->tor)
|| tr_bitfieldHasNone (&cp->blockBitfield);
return !tr_torrentHasMetadata (cp->tor)
|| tr_bitfieldHasNone (&cp->blockBitfield);
}
/**
@ -116,7 +116,7 @@ size_t tr_cpMissingBytesInPiece (const tr_completion *, tr_piece_index_t);
static inline bool
tr_cpPieceIsComplete (const tr_completion * cp, tr_piece_index_t i)
{
return tr_cpMissingBlocksInPiece (cp, i) == 0;
return tr_cpMissingBlocksInPiece (cp, i) == 0;
}
/**
@ -128,7 +128,7 @@ void tr_cpBlockAdd (tr_completion * cp, tr_block_index_t i);
static inline bool
tr_cpBlockIsComplete (const tr_completion * cp, tr_block_index_t i)
{
return tr_bitfieldHas (&cp->blockBitfield, i);
return tr_bitfieldHas (&cp->blockBitfield, i);
}
/***
@ -142,7 +142,7 @@ void* tr_cpCreatePieceBitfield (const tr_completion * cp, size_t * byte_count);
static inline void
tr_cpInvalidateDND (tr_completion * cp)
{
cp->sizeWhenDoneIsDirty = true;
cp->sizeWhenDoneIsDirty = true;
}

View File

@ -881,7 +881,7 @@ testForEndgame (const tr_swarm * s)
/* we consider ourselves to be in endgame if the number of bytes
we've got requested is >= the number of bytes left to download */
return (s->requestCount * s->tor->blockSize)
>= tr_cpLeftUntilDone (&s->tor->completion);
>= tr_torrentGetLeftUntilDone (s->tor);
}
static void
@ -955,10 +955,10 @@ comparePieceByWeight (const void * va, const void * vb)
const uint16_t * rep = weightReplication;
/* primary key: weight */
missing = tr_cpMissingBlocksInPiece (&tor->completion, a->index);
missing = tr_torrentMissingBlocksInPiece (tor, a->index);
pending = a->requestCount;
ia = missing > pending ? missing - pending : (tor->blockCountInPiece + pending);
missing = tr_cpMissingBlocksInPiece (&tor->completion, b->index);
missing = tr_torrentMissingBlocksInPiece (tor, b->index);
pending = b->requestCount;
ib = missing > pending ? missing - pending : (tor->blockCountInPiece + pending);
if (ia < ib) return -1;
@ -1091,7 +1091,7 @@ pieceListRebuild (tr_swarm * s)
pool = tr_new (tr_piece_index_t, inf->pieceCount);
for (i=0; i<inf->pieceCount; ++i)
if (!inf->pieces[i].dnd)
if (!tr_cpPieceIsComplete (&tor->completion, i))
if (!tr_torrentPieceIsComplete (tor, i))
pool[poolCount++] = i;
pieceCount = poolCount;
pieces = tr_new0 (struct weighted_piece, pieceCount);
@ -1375,7 +1375,7 @@ tr_peerMgrGetNextRequests (tr_torrent * tor,
tr_peer ** peers;
/* don't request blocks we've already got */
if (tr_cpBlockIsComplete (&tor->completion, b))
if (tr_torrentBlockIsComplete (tor, b))
continue;
/* always add peer if this block has no peers yet */
@ -1580,7 +1580,7 @@ peerSuggestedPiece (tr_swarm * s UNUSED,
return;
/* don't ask for it if we've already got it */
if (tr_cpPieceIsComplete (t->tor->completion, pieceIndex))
if (tr_torrentPieceIsComplete (t->tor, pieceIndex))
return;
/* don't ask for it if they don't have it */
@ -1602,7 +1602,7 @@ peerSuggestedPiece (tr_swarm * s UNUSED,
for (b=first; b<=last; ++b)
{
if (!tr_cpBlockIsComplete (tor->completion, b))
if (tr_torrentBlockIsComplete (tor, b))
{
const uint32_t offset = getBlockOffsetInPiece (tor, b);
const uint32_t length = tr_torBlockCountBytes (tor, b);
@ -2544,13 +2544,13 @@ tr_peerMgrTorrentAvailability (const tr_torrent * tor,
const int peerCount = tr_ptrArraySize (&tor->swarm->peers);
const tr_peer ** peers = (const tr_peer**) tr_ptrArrayBase (&tor->swarm->peers);
const float interval = tor->info.pieceCount / (float)tabCount;
const bool isSeed = tr_cpGetStatus (&tor->completion) == TR_SEED;
const bool isSeed = tr_torrentGetCompleteness (tor) == TR_SEED;
for (i=0; i<tabCount; ++i)
{
const int piece = i * interval;
if (isSeed || tr_cpPieceIsComplete (&tor->completion, piece))
if (isSeed || tr_torrentPieceIsComplete (tor, piece))
{
tab[i] = -1;
}
@ -2635,7 +2635,7 @@ tr_peerMgrGetDesiredAvailable (const tr_torrent * tor)
const tr_peer ** peers = (const tr_peer**) tr_ptrArrayBase (&s->peers);
for (i=0; i<n; ++i)
if (peers[i]->atom && atomIsSeed (peers[i]->atom))
return tr_cpLeftUntilDone (&tor->completion);
return tr_torrentGetLeftUntilDone (tor);
}
if (!s->pieceReplication || !s->pieceReplicationSize)
@ -2646,7 +2646,7 @@ tr_peerMgrGetDesiredAvailable (const tr_torrent * tor)
desiredAvailable = 0;
for (i=0, n=MIN (tor->info.pieceCount, s->pieceReplicationSize); i<n; ++i)
if (!tor->info.pieces[i].dnd && (s->pieceReplication[i] > 0))
desiredAvailable += tr_cpMissingBytesInPiece (&tor->completion, i);
desiredAvailable += tr_torrentMissingBytesInPiece (tor, i);
assert (desiredAvailable <= tor->info.totalSize);
return desiredAvailable;
@ -2918,7 +2918,7 @@ rechokeDownloads (tr_swarm * s)
/* build a bitfield of interesting pieces... */
piece_is_interesting = tr_new (bool, n);
for (i=0; i<n; i++)
piece_is_interesting[i] = !tor->info.pieces[i].dnd && !tr_cpPieceIsComplete (&tor->completion, i);
piece_is_interesting[i] = !tor->info.pieces[i].dnd && !tr_torrentPieceIsComplete (tor, i);
/* decide WHICH peers to be interested in (based on their cancel-to-block ratio) */
for (i=0; i<peerCount; ++i)

View File

@ -1339,7 +1339,7 @@ peerMadeRequest (tr_peerMsgs * msgs, const struct peer_request * req)
{
const bool fext = tr_peerIoSupportsFEXT (msgs->io);
const int reqIsValid = requestIsValid (msgs, req);
const int clientHasPiece = reqIsValid && tr_cpPieceIsComplete (&msgs->torrent->completion, req->index);
const int clientHasPiece = reqIsValid && tr_torrentPieceIsComplete (msgs->torrent, req->index);
const int peerIsChoked = msgs->peer_is_choked;
int allow = false;
@ -1708,7 +1708,7 @@ clientGotBlock (tr_peerMsgs * msgs,
dbgmsg (msgs, "we didn't ask for this message...");
return 0;
}
if (tr_cpPieceIsComplete (&msgs->torrent->completion, req->index)) {
if (tr_torrentPieceIsComplete (msgs->torrent, req->index)) {
dbgmsg (msgs, "we did ask for this message, but the piece is already complete...");
return 0;
}
@ -2006,7 +2006,7 @@ fillOutputBuffer (tr_peerMsgs * msgs, time_t now)
--msgs->prefetchCount;
if (requestIsValid (msgs, &req)
&& tr_cpPieceIsComplete (&msgs->torrent->completion, req.index))
&& tr_torrentPieceIsComplete (msgs->torrent, req.index))
{
int err;
const uint32_t msglen = 4 + 1 + 4 + 4 + req.length;
@ -2126,7 +2126,7 @@ sendBitfield (tr_peerMsgs * msgs)
assert (tr_torrentHasMetadata (msgs->torrent));
bytes = tr_cpCreatePieceBitfield (&msgs->torrent->completion, &byte_count);
bytes = tr_torrentCreatePieceBitfield (msgs->torrent, &byte_count);
evbuffer_add_uint32 (out, sizeof (uint8_t) + byte_count);
evbuffer_add_uint8 (out, BT_BITFIELD);
evbuffer_add (out, bytes, byte_count);
@ -2141,15 +2141,15 @@ tellPeerWhatWeHave (tr_peerMsgs * msgs)
{
const bool fext = tr_peerIoSupportsFEXT (msgs->io);
if (fext && tr_cpHasAll (&msgs->torrent->completion))
if (fext && tr_torrentHasAll (msgs->torrent))
{
protocolSendHaveAll (msgs);
}
else if (fext && tr_cpHasNone (&msgs->torrent->completion))
else if (fext && tr_torrentHasNone (msgs->torrent))
{
protocolSendHaveNone (msgs);
}
else if (!tr_cpHasNone (&msgs->torrent->completion))
else if (!tr_torrentHasNone (msgs->torrent))
{
sendBitfield (msgs);
}

View File

@ -746,7 +746,7 @@ addField (tr_torrent * const tor,
if (tr_torrentHasMetadata (tor))
{
size_t byte_count = 0;
void * bytes = tr_cpCreatePieceBitfield (&tor->completion, &byte_count);
void * bytes = tr_torrentCreatePieceBitfield (tor, &byte_count);
char * str = tr_base64_encode (bytes, byte_count, NULL);
tr_variantDictAddStr (d, key, str!=NULL ? str : "");
tr_free (str);

View File

@ -838,7 +838,7 @@ hasAnyLocalData (const tr_torrent * tor)
static bool
setLocalErrorIfFilesDisappeared (tr_torrent * tor)
{
const bool disappeared = (tr_cpHaveTotal (&tor->completion) > 0) && !hasAnyLocalData (tor);
const bool disappeared = (tr_torrentHaveTotal (tor) > 0) && !hasAnyLocalData (tor);
if (disappeared)
{
@ -1293,7 +1293,7 @@ tr_torrentStat (tr_torrent * tor)
s->metadataPercentComplete = tr_torrentGetMetadataPercent (tor);
s->percentDone = tr_cpPercentDone (&tor->completion);
s->leftUntilDone = tr_cpLeftUntilDone (&tor->completion);
s->leftUntilDone = tr_torrentGetLeftUntilDone (tor);
s->sizeWhenDone = tr_cpSizeWhenDone (&tor->completion);
s->recheckProgress = s->activity == TR_STATUS_CHECK ? getVerifyProgress (tor) : 0;
s->activityDate = tor->activityDate;
@ -1308,7 +1308,7 @@ tr_torrentStat (tr_torrent * tor)
s->downloadedEver = tor->downloadedCur + tor->downloadedPrev;
s->uploadedEver = tor->uploadedCur + tor->uploadedPrev;
s->haveValid = tr_cpHaveValid (&tor->completion);
s->haveUnchecked = tr_cpHaveTotal (&tor->completion) - s->haveValid;
s->haveUnchecked = tr_torrentHaveTotal (tor) - s->haveValid;
s->desiredAvailable = tr_peerMgrGetDesiredAvailable (tor);
s->ratio = tr_getRatio (s->uploadedEver,
@ -1412,13 +1412,13 @@ countFileBytesCompleted (const tr_torrent * tor, tr_file_index_t index)
if (first == last)
{
if (tr_cpBlockIsComplete (&tor->completion, first))
if (tr_torrentBlockIsComplete (tor, first))
total = f->length;
}
else
{
/* the first block */
if (tr_cpBlockIsComplete (&tor->completion, first))
if (tr_torrentBlockIsComplete (tor, first))
total += tor->blockSize - (f->offset % tor->blockSize);
/* the middle blocks */
@ -1430,7 +1430,7 @@ countFileBytesCompleted (const tr_torrent * tor, tr_file_index_t index)
}
/* the last block */
if (tr_cpBlockIsComplete (&tor->completion, last))
if (tr_torrentBlockIsComplete (tor, last))
total += (f->offset + f->length) - ((uint64_t)tor->blockSize * last);
}
}
@ -3224,7 +3224,7 @@ tr_torrentPieceCompleted (tr_torrent * tor, tr_piece_index_t pieceIndex)
void
tr_torrentGotBlock (tr_torrent * tor, tr_block_index_t block)
{
const bool block_is_new = !tr_cpBlockIsComplete (&tor->completion, block);
const bool block_is_new = !tr_torrentBlockIsComplete (tor, block);
assert (tr_isTorrent (tor));
assert (tr_amInEventThread (tor->session));
@ -3237,7 +3237,7 @@ tr_torrentGotBlock (tr_torrent * tor, tr_block_index_t block)
tr_torrentSetDirty (tor);
p = tr_torBlockPiece (tor, block);
if (tr_cpPieceIsComplete (&tor->completion, p))
if (tr_torrentPieceIsComplete (tor, p))
{
tr_logAddTorDbg (tor, "[LAZY] checking just-completed piece %zu", (size_t)p);

View File

@ -323,10 +323,16 @@ tr_torrentExists (const tr_session * session, const uint8_t * torrentHash)
return tr_torrentFindFromHash ((tr_session*)session, torrentHash) != NULL;
}
static inline tr_completeness
tr_torrentGetCompleteness (const tr_torrent * tor)
{
return tor->completeness;
}
static inline bool
tr_torrentIsSeed (const tr_torrent * tor)
{
return tor->completeness != TR_LEECH;
return tr_torrentGetCompleteness(tor) != TR_LEECH;
}
static inline bool tr_torrentIsPrivate (const tr_torrent * tor)
@ -439,17 +445,71 @@ bool tr_torrentIsStalled (const tr_torrent * tor);
const unsigned char * tr_torrentGetPeerId (tr_torrent * tor);
static inline uint64_t
tr_torrentGetLeftUntilDone (const tr_torrent * tor)
{
return tr_cpLeftUntilDone (&tor->completion);
}
static inline bool
tr_torrentHasAll (const tr_torrent * tor)
{
return tr_cpHasAll (&tor->completion);
}
static inline bool
tr_torrentHasNone (const tr_torrent * tor)
{
return tr_cpHasNone (&tor->completion);
}
static inline bool
tr_torrentPieceIsComplete (const tr_torrent * tor, tr_piece_index_t i)
{
return tr_cpPieceIsComplete (&tor->completion, i);
}
static inline bool
tr_torrentBlockIsComplete (const tr_torrent * tor, tr_block_index_t i)
{
return tr_cpBlockIsComplete (&tor->completion, i);
}
static inline size_t
tr_torrentMissingBlocksInPiece (const tr_torrent * tor, tr_piece_index_t i)
{
return tr_cpMissingBlocksInPiece (&tor->completion, i);
}
static inline size_t
tr_torrentMissingBytesInPiece (const tr_torrent * tor, tr_piece_index_t i)
{
return tr_cpMissingBytesInPiece (&tor->completion, i);
}
static inline void *
tr_torrentCreatePieceBitfield (const tr_torrent * tor, size_t * byte_count)
{
return tr_cpCreatePieceBitfield (&tor->completion, byte_count);
}
static inline uint64_t
tr_torrentHaveTotal (const tr_torrent * tor)
{
return tr_cpHaveTotal (&tor->completion);
}
static inline bool
tr_torrentIsQueued (const tr_torrent * tor)
{
return tor->isQueued;
return tor->isQueued;
}
static inline tr_direction
tr_torrentGetQueueDirection (const tr_torrent * tor)
{
return tr_torrentIsSeed (tor) ? TR_UP : TR_DOWN;
return tr_torrentIsSeed (tor) ? TR_UP : TR_DOWN;
}
#endif

View File

@ -70,7 +70,7 @@ verifyTorrent (tr_torrent * tor, bool * stopFlag)
/* if we're starting a new piece... */
if (piecePos == 0)
hadPiece = tr_cpPieceIsComplete (&tor->completion, pieceIndex);
hadPiece = tr_torrentPieceIsComplete (tor, pieceIndex);
/* if we're starting a new file... */
if (!filePos && (fd<0) && (fileIndex!=prevFileIndex))