2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2021-11-24 14:48:52 +00:00
|
|
|
|
|
|
|
#include <event2/util.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#include "block-info.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
#include "tr-assert.h"
|
2021-11-24 14:48:52 +00:00
|
|
|
|
|
|
|
void tr_block_info::initSizes(uint64_t total_size_in, uint64_t piece_size_in)
|
|
|
|
{
|
|
|
|
total_size = total_size_in;
|
|
|
|
piece_size = piece_size_in;
|
|
|
|
|
2022-02-23 20:25:06 +00:00
|
|
|
TR_ASSERT(piece_size == 0 || piece_size >= BlockSize);
|
|
|
|
|
|
|
|
if (piece_size == 0)
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2021-11-25 20:30:13 +00:00
|
|
|
*this = {};
|
2021-11-24 14:48:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
n_pieces = (total_size + piece_size - 1) / piece_size;
|
|
|
|
|
|
|
|
auto remainder = total_size % piece_size;
|
2022-02-07 04:28:36 +00:00
|
|
|
final_piece_size = remainder != 0U ? remainder : piece_size;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-02-23 20:25:06 +00:00
|
|
|
remainder = total_size % BlockSize;
|
|
|
|
final_block_size = remainder != 0U ? remainder : BlockSize;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-02-23 20:25:06 +00:00
|
|
|
n_blocks = (total_size + BlockSize - 1) / BlockSize;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
|
|
|
#ifdef TR_ENABLE_ASSERTS
|
|
|
|
uint64_t t = n_pieces - 1;
|
|
|
|
t *= piece_size;
|
|
|
|
t += final_piece_size;
|
|
|
|
TR_ASSERT(t == total_size);
|
|
|
|
|
|
|
|
t = n_blocks - 1;
|
2022-02-23 20:25:06 +00:00
|
|
|
t *= BlockSize;
|
2021-11-24 14:48:52 +00:00
|
|
|
t += final_block_size;
|
|
|
|
TR_ASSERT(t == total_size);
|
|
|
|
#endif
|
|
|
|
}
|