Skip to content

Commit 77ffb15

Browse files
committed
factor out a helper for payload_blocks_in_piece()
1 parent 8de7a29 commit 77ffb15

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

include/libtorrent/aux_/piece_picker.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ namespace libtorrent::aux {
408408
// returns the number of blocks there is in the given piece
409409
int blocks_in_piece(piece_index_t) const;
410410

411+
// number of blocks in the given piece that carry real payload,
412+
// i.e. excluding any trailing pad blocks
413+
int payload_blocks_in_piece(piece_index_t) const;
414+
411415
// return the peer pointers to all peers that participated in
412416
// this piece
413417
std::vector<aux::torrent_peer*> get_downloaders(piece_index_t) const;

src/piece_picker.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ namespace libtorrent::aux {
266266
// the number of non-pad blocks in this piece. Any blocks past this will
267267
// be assumed we have already
268268

269-
int const payload_blocks = blocks_per_piece() - pad_bytes_in_piece(piece) / block_size();
269+
int const payload_blocks = payload_blocks_in_piece(piece);
270270

271271
int block_idx = 0;
272272
for (auto& info : mutable_blocks_for_piece(ret))
@@ -2531,6 +2531,11 @@ namespace {
25312531
return blocks_per_piece();
25322532
}
25332533

2534+
int piece_picker::payload_blocks_in_piece(piece_index_t const index) const
2535+
{
2536+
return blocks_in_piece(index) - pad_bytes_in_piece(index) / block_size();
2537+
}
2538+
25342539
bool piece_picker::is_piece_free(piece_index_t const piece
25352540
, typed_bitfield<piece_index_t> const& bitmask) const
25362541
{
@@ -2653,7 +2658,7 @@ namespace {
26532658
}
26542659

26552660
// pick a new piece
2656-
int payload_blocks = blocks_in_piece(piece) - pad_bytes_in_piece(piece) / block_size();
2661+
int payload_blocks = payload_blocks_in_piece(piece);
26572662

26582663
if (prefer_contiguous_blocks == 0)
26592664
{
@@ -2683,7 +2688,7 @@ namespace {
26832688
ignore.push_back(k);
26842689

26852690
TORRENT_ASSERT(m_piece_map[k].priority(this) > 0);
2686-
payload_blocks = blocks_in_piece(k) - pad_bytes_in_piece(k) / block_size();
2691+
payload_blocks = payload_blocks_in_piece(k);
26872692
TORRENT_ASSERT(is_piece_free(k, pieces));
26882693
for (int j = 0; j < payload_blocks; ++j)
26892694
{

test/test_piece_picker.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,33 @@ TORRENT_TEST(set_pad_bytes)
23242324
TEST_EQUAL(blocks[3].state, piece_picker::block_info::state_finished);
23252325
}
23262326

2327+
TORRENT_TEST(set_pad_bytes_short_last_piece)
2328+
{
2329+
// the last piece is shorter than a full piece (2 blocks instead of 4)
2330+
// and its last block is a pad block. add_download_piece() must use the
2331+
// actual number of blocks in this short piece, not the nominal
2332+
// per-piece block count, to tell payload blocks from pad blocks.
2333+
auto p = std::make_shared<piece_picker>(
2334+
std::int64_t(default_piece_size) + 2 * default_block_size, default_piece_size);
2335+
p->set_pad_bytes(1_piece, default_block_size);
2336+
2337+
bool const ret = p->mark_as_downloading({1_piece, 0}, tmp_peer);
2338+
TEST_EQUAL(ret, true);
2339+
2340+
auto const dl = p->get_download_queue();
2341+
2342+
TEST_EQUAL(dl.size(), 1);
2343+
TEST_EQUAL(dl[0].finished, 1);
2344+
TEST_EQUAL(dl[0].writing, 0);
2345+
TEST_EQUAL(dl[0].requested, 1);
2346+
TEST_EQUAL(dl[0].index, 1_piece);
2347+
2348+
auto const blocks = p->blocks_for_piece(dl[0]);
2349+
TEST_EQUAL(blocks.size(), 2);
2350+
TEST_EQUAL(blocks[0].state, piece_picker::block_info::state_requested);
2351+
TEST_EQUAL(blocks[1].state, piece_picker::block_info::state_finished);
2352+
}
2353+
23272354
TORRENT_TEST(set_pad_bytes_overflow)
23282355
{
23292356
int const ps = file_storage::max_piece_size;

0 commit comments

Comments
 (0)