2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +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.
|
2009-06-15 00:11:06 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
2009-06-15 00:11:06 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2009-06-15 00:11:06 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
#include <cstddef>
|
2022-03-09 23:43:42 +00:00
|
|
|
#include <cstdint>
|
2021-10-16 14:04:19 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2022-12-23 21:21:40 +00:00
|
|
|
#include "tr-macros.h"
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
/**
|
|
|
|
* @brief Implementation of the BitTorrent spec's Bitfield array of bits.
|
|
|
|
*
|
|
|
|
* This is for tracking the pieces a peer has. Its functionality is like
|
|
|
|
* a bitset or vector<bool> with some added use cases:
|
|
|
|
*
|
|
|
|
* - It needs to be able to read/write the left-to-right bitfield format
|
|
|
|
* specified in the bittorrent spec. This is what raw() and getRaw()
|
|
|
|
* are for.
|
|
|
|
*
|
|
|
|
* - "Have all" is a special case where we know the peer has all the
|
|
|
|
* pieces and don't need to check the bit array. This is useful since
|
|
|
|
* (a) it's very common (i.e. seeds) and saves memory and work of
|
|
|
|
* allocating a bit array and doing lookups, and (b) if we have a
|
|
|
|
* magnet link and haven't gotten the metainfo yet, we may not know
|
|
|
|
* how many pieces there are -- but we can still know "this peer has
|
|
|
|
* all of them".
|
|
|
|
*
|
|
|
|
* - "Have none" is another special case that has the same advantages
|
|
|
|
* and motivations as "Have all".
|
|
|
|
*/
|
|
|
|
class tr_bitfield
|
2009-06-15 00:11:06 +00:00
|
|
|
{
|
2021-10-11 17:29:14 +00:00
|
|
|
public:
|
2021-10-24 20:43:36 +00:00
|
|
|
explicit tr_bitfield(size_t bit_count);
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_has_all() noexcept;
|
|
|
|
void set_has_none() noexcept;
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
// set one or more bits
|
2022-08-03 06:15:37 +00:00
|
|
|
void set(size_t nth, bool value = true);
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_span(size_t begin, size_t end, bool value = true);
|
2021-10-24 20:43:36 +00:00
|
|
|
void unset(size_t bit)
|
2021-10-16 14:04:19 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
set(bit, false);
|
2021-10-16 14:04:19 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
void unset_span(size_t begin, size_t end)
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
set_span(begin, end, false);
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_from_bools(bool const* flags, size_t n);
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
// "raw" here is in BEP0003 format: "The first byte of the bitfield
|
|
|
|
// corresponds to indices 0 - 7 from high bit to low bit, respectively.
|
2022-04-21 14:28:38 +00:00
|
|
|
// The next one 8-15, etc. Spare bits at the end are set to zero."
|
2023-04-23 01:25:55 +00:00
|
|
|
void set_raw(uint8_t const* raw, size_t byte_count);
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] std::vector<uint8_t> raw() const;
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool has_all() const noexcept
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return have_all_hint_ || (bit_count_ > 0 && bit_count_ == true_count_);
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] constexpr bool has_none() const noexcept
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return have_none_hint_ || (bit_count_ > 0 && true_count_ == 0);
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 17:34:36 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool test(size_t bit) const
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return has_all() || (!has_none() && test_flag(bit));
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr size_t count() const noexcept
|
2021-10-11 17:29:14 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return true_count_;
|
2021-10-16 14:04:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
[[nodiscard]] size_t count(size_t begin, size_t end) const;
|
2021-10-16 14:04:19 +00:00
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr size_t size() const noexcept
|
2021-10-16 14:04:19 +00:00
|
|
|
{
|
2021-10-24 20:43:36 +00:00
|
|
|
return bit_count_;
|
2021-10-11 17:29:14 +00:00
|
|
|
}
|
2011-03-30 04:14:57 +00:00
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr size_t empty() const noexcept
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] bool is_valid() const;
|
2011-03-28 16:31:05 +00:00
|
|
|
|
2022-11-23 05:26:10 +00:00
|
|
|
[[nodiscard]] constexpr auto percent() const noexcept
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (has_all())
|
2022-11-23 05:26:10 +00:00
|
|
|
{
|
|
|
|
return 1.0F;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (has_none() || empty())
|
2022-11-23 05:26:10 +00:00
|
|
|
{
|
|
|
|
return 0.0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<float>(count()) / size();
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_bitfield& operator|=(tr_bitfield const& that) noexcept;
|
|
|
|
tr_bitfield& operator&=(tr_bitfield const& that) noexcept;
|
2023-04-14 23:45:46 +00:00
|
|
|
[[nodiscard]] bool intersects(tr_bitfield const& that) const noexcept;
|
2022-11-23 05:26:10 +00:00
|
|
|
|
2021-10-24 20:43:36 +00:00
|
|
|
private:
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] size_t count_flags() const noexcept;
|
|
|
|
[[nodiscard]] size_t count_flags(size_t begin, size_t end) const noexcept;
|
2022-04-02 00:48:09 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
[[nodiscard]] TR_CONSTEXPR20 bool test_flag(size_t n) const
|
2022-04-02 00:48:09 +00:00
|
|
|
{
|
|
|
|
if (n >> 3U >= std::size(flags_))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = (flags_[n >> 3U] << (n & 7U) & 0x80) != 0;
|
|
|
|
return ret;
|
|
|
|
}
|
2021-10-11 17:29:14 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void ensure_bits_alloced(size_t n);
|
|
|
|
[[nodiscard]] bool ensure_nth_bit_alloced(size_t nth);
|
2021-10-16 14:04:19 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void free_array() noexcept
|
2023-01-27 20:25:08 +00:00
|
|
|
{
|
|
|
|
// move-assign to ensure the reserve memory is cleared
|
|
|
|
flags_ = std::vector<uint8_t>{};
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void increment_true_count(size_t inc) noexcept;
|
|
|
|
void decrement_true_count(size_t dec) noexcept;
|
|
|
|
void set_true_count(size_t n) noexcept;
|
|
|
|
void rebuild_true_count() noexcept
|
2023-01-27 20:25:08 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
set_true_count(count_flags());
|
2023-01-27 20:25:08 +00:00
|
|
|
}
|
2021-10-16 14:04:19 +00:00
|
|
|
|
2022-04-28 16:52:07 +00:00
|
|
|
std::vector<uint8_t> flags_;
|
|
|
|
|
2021-10-16 14:04:19 +00:00
|
|
|
size_t bit_count_ = 0;
|
|
|
|
size_t true_count_ = 0;
|
2021-10-24 20:43:36 +00:00
|
|
|
|
|
|
|
/* Special cases for when full or empty but we don't know the bitCount.
|
|
|
|
This occurs when a magnet link's peers send have all / have none */
|
|
|
|
bool have_all_hint_ = false;
|
|
|
|
bool have_none_hint_ = false;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|