refactor: remove outvar args from tr_torGetFooBlockRange() functions (#2041)

This commit is contained in:
Charles Kerr
2021-10-24 22:39:19 -05:00
committed by GitHub
parent 4a1ce7d645
commit 93d8a03d55
6 changed files with 45 additions and 79 deletions
+1 -3
View File
@@ -422,9 +422,7 @@ int tr_cacheFlushDone(tr_cache* cache)
int tr_cacheFlushFile(tr_cache* cache, tr_torrent* torrent, tr_file_index_t i)
{
auto first = tr_block_index_t{};
auto last = tr_block_index_t{};
tr_torGetFileBlockRange(torrent, i, &first, &last);
auto const [first, last] = tr_torGetFileBlockRange(torrent, i);
int pos = findBlockPos(cache, torrent, first);
dbgmsg("flushing file %d from cache to disk: blocks [%zu...%zu]", (int)i, (size_t)first, (size_t)last);
+19 -36
View File
@@ -78,12 +78,8 @@ tr_completeness tr_cpGetStatus(tr_completion const* cp)
void tr_cpPieceRem(tr_completion* cp, tr_piece_index_t piece)
{
tr_torrent const* tor = cp->tor;
auto f = tr_block_index_t{};
auto l = tr_block_index_t{};
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
for (tr_block_index_t i = f; i <= l; ++i)
auto const [first, last] = tr_torGetPieceBlockRange(cp->tor, piece);
for (tr_block_index_t i = first; i <= last; ++i)
{
if (tr_cpBlockIsComplete(cp, i))
{
@@ -93,16 +89,13 @@ void tr_cpPieceRem(tr_completion* cp, tr_piece_index_t piece)
cp->haveValidIsDirty = true;
cp->sizeWhenDoneIsDirty = true;
cp->blockBitfield->setRange(f, l + 1, false);
cp->blockBitfield->unsetRange(first, last + 1);
}
void tr_cpPieceAdd(tr_completion* cp, tr_piece_index_t piece)
{
auto f = tr_block_index_t{};
auto l = tr_block_index_t{};
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
for (tr_block_index_t i = f; i <= l; ++i)
auto const [first, last] = tr_torGetPieceBlockRange(cp->tor, piece);
for (tr_block_index_t i = first; i <= last; ++i)
{
tr_cpBlockAdd(cp, i);
}
@@ -178,14 +171,12 @@ uint64_t tr_cpSizeWhenDone(tr_completion const* ccp)
}
else
{
auto f = tr_block_index_t{};
auto l = tr_block_index_t{};
tr_torGetPieceBlockRange(cp->tor, p, &f, &l);
auto const [first, last] = tr_torGetPieceBlockRange(cp->tor, p);
n = cp->blockBitfield->count(f, l + 1);
n = cp->blockBitfield->count(first, last + 1);
n *= cp->tor->blockSize;
if (l == cp->tor->blockCount - 1 && cp->blockBitfield->test(l))
if (last == cp->tor->blockCount - 1 && cp->blockBitfield->test(last))
{
n -= cp->tor->blockSize - cp->tor->lastBlockSize;
}
@@ -229,10 +220,8 @@ void tr_cpGetAmountDone(tr_completion const* cp, float* tab, int tabCount)
else
{
tr_piece_index_t const piece = (tr_piece_index_t)i * interval;
auto f = tr_block_index_t{};
auto l = tr_block_index_t{};
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
tab[i] = cp->blockBitfield->count(f, l + 1) / (float)(l + 1 - f);
auto const [first, last] = tr_torGetPieceBlockRange(cp->tor, piece);
tab[i] = cp->blockBitfield->count(first, last + 1) / (float)(last + 1 - first);
}
}
}
@@ -244,10 +233,8 @@ size_t tr_cpMissingBlocksInPiece(tr_completion const* cp, tr_piece_index_t piece
return 0;
}
auto f = tr_block_index_t{};
auto l = tr_block_index_t{};
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
return (l + 1 - f) - cp->blockBitfield->count(f, l + 1);
auto const [first, last] = tr_torGetPieceBlockRange(cp->tor, piece);
return (last + 1 - first) - cp->blockBitfield->count(first, last + 1);
}
size_t tr_cpMissingBytesInPiece(tr_completion const* cp, tr_piece_index_t piece)
@@ -257,24 +244,22 @@ size_t tr_cpMissingBytesInPiece(tr_completion const* cp, tr_piece_index_t piece)
return 0;
}
auto f = tr_block_index_t{};
auto l = tr_block_index_t{};
size_t const pieceByteSize = tr_torPieceCountBytes(cp->tor, piece);
tr_torGetPieceBlockRange(cp->tor, piece, &f, &l);
auto const [first, last] = tr_torGetPieceBlockRange(cp->tor, piece);
auto haveBytes = size_t{};
if (f != l)
if (first != last)
{
/* nb: we don't pass the usual l+1 here to Bitfield::countRange().
It's faster to handle the last block separately because its size
needs to be checked separately. */
haveBytes = cp->blockBitfield->count(f, l);
haveBytes = cp->blockBitfield->count(first, last);
haveBytes *= cp->tor->blockSize;
}
if (cp->blockBitfield->test(l)) /* handle the last block */
if (cp->blockBitfield->test(last)) /* handle the last block */
{
haveBytes += tr_torBlockCountBytes(cp->tor, l);
haveBytes += tr_torBlockCountBytes(cp->tor, last);
}
TR_ASSERT(haveBytes <= pieceByteSize);
@@ -288,10 +273,8 @@ bool tr_cpFileIsComplete(tr_completion const* cp, tr_file_index_t i)
return true;
}
auto f = tr_block_index_t{};
auto l = tr_block_index_t{};
tr_torGetFileBlockRange(cp->tor, i, &f, &l);
return cp->blockBitfield->count(f, l + 1) == (l + 1 - f);
auto const [first, last] = tr_torGetFileBlockRange(cp->tor, i);
return cp->blockBitfield->count(first, last + 1) == (last + 1 - first);
}
std::vector<uint8_t> tr_cpCreatePieceBitfield(tr_completion const* cp)
+2 -7
View File
@@ -1319,9 +1319,7 @@ void tr_peerMgrGetNextRequests(
/* if the peer has this piece that we want... */
if (have->test(p->index))
{
auto first = tr_block_index_t{};
auto last = tr_block_index_t{};
tr_torGetPieceBlockRange(tor, p->index, &first, &last);
auto const [first, last] = tr_torGetPieceBlockRange(tor, p->index);
for (tr_block_index_t b = first; b <= last && (got < numwant || (get_intervals && setme[2 * got - 1] == b - 1));
++b)
@@ -1569,11 +1567,8 @@ static void peerSuggestedPiece(tr_swarm* /*s*/, tr_peer* /*peer*/, tr_piece_inde
/* request the blocks that we don't have in this piece */
{
tr_block_index_t first;
tr_block_index_t last;
tr_torrent const* tor = t->tor;
tr_torGetPieceBlockRange(t->tor, pieceIndex, &first, &last);
auto const [first, last] = tr_torGetPieceBlockRange(t->tor, pieceIndex);
for (tr_block_index_t b = first; b <= last; ++b)
{
+14 -20
View File
@@ -1434,9 +1434,7 @@ static uint64_t countFileBytesCompleted(tr_torrent const* tor, tr_file_index_t i
return 0;
}
auto first = tr_block_index_t{};
auto last = tr_block_index_t{};
tr_torGetFileBlockRange(tor, index, &first, &last);
auto const [first, last] = tr_torGetFileBlockRange(tor, index);
if (first == last)
{
@@ -2539,35 +2537,31 @@ uint64_t tr_pieceOffset(tr_torrent const* tor, tr_piece_index_t index, uint32_t
return ret;
}
void tr_torGetFileBlockRange(tr_torrent const* tor, tr_file_index_t const file, tr_block_index_t* first, tr_block_index_t* last)
tr_block_range tr_torGetFileBlockRange(tr_torrent const* tor, tr_file_index_t const file)
{
tr_file const* f = &tor->info.files[file];
uint64_t offset = f->offset;
*first = offset / tor->blockSize;
tr_block_index_t const first = offset / tor->blockSize;
if (f->length == 0)
{
*last = *first;
}
else
{
offset += f->length - 1;
*last = offset / tor->blockSize;
return { first, first };
}
offset += f->length - 1;
tr_block_index_t const last = offset / tor->blockSize;
return { first, last };
}
void tr_torGetPieceBlockRange(
tr_torrent const* tor,
tr_piece_index_t const piece,
tr_block_index_t* first,
tr_block_index_t* last)
tr_block_range tr_torGetPieceBlockRange(tr_torrent const* tor, tr_piece_index_t const piece)
{
uint64_t offset = tor->info.pieceSize;
offset *= piece;
*first = offset / tor->blockSize;
tr_block_index_t const first = offset / tor->blockSize;
offset += tr_torPieceCountBytes(tor, piece) - 1;
*last = offset / tor->blockSize;
tr_block_index_t const last = offset / tor->blockSize;
return { first, last };
}
/***
+8 -10
View File
@@ -81,17 +81,15 @@ void tr_torrentGetBlockLocation(
uint32_t* offset,
uint32_t* length);
void tr_torGetFileBlockRange(
tr_torrent const* tor,
tr_file_index_t const file,
tr_block_index_t* first,
tr_block_index_t* last);
struct tr_block_range
{
tr_block_index_t first;
tr_block_index_t last;
};
void tr_torGetPieceBlockRange(
tr_torrent const* tor,
tr_piece_index_t const piece,
tr_block_index_t* first,
tr_block_index_t* last);
tr_block_range tr_torGetFileBlockRange(tr_torrent const* tor, tr_file_index_t const file);
tr_block_range tr_torGetPieceBlockRange(tr_torrent const* tor, tr_piece_index_t const piece);
void tr_torrentInitFilePriority(tr_torrent* tor, tr_file_index_t fileIndex, tr_priority_t priority);
+1 -3
View File
@@ -96,9 +96,7 @@ TEST_P(IncompleteDirTest, incompleteDir)
data.tor = tor;
data.buf = evbuffer_new();
tr_block_index_t first;
tr_block_index_t last;
tr_torGetPieceBlockRange(tor, data.pieceIndex, &first, &last);
auto const [first, last] = tr_torGetPieceBlockRange(tor, data.pieceIndex);
for (tr_block_index_t block_index = first; block_index <= last; ++block_index)
{