Coverage Report

Created: 2026-03-25 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtorrent/src/peer_class.cpp
Line
Count
Source
1
/*
2
3
Copyright (c) 2014, 2017, 2019, Arvid Norberg
4
Copyright (c) 2018, Alden Torres
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions
9
are met:
10
11
    * Redistributions of source code must retain the above copyright
12
      notice, this list of conditions and the following disclaimer.
13
    * Redistributions in binary form must reproduce the above copyright
14
      notice, this list of conditions and the following disclaimer in
15
      the documentation and/or other materials provided with the distribution.
16
    * Neither the name of the author nor the names of its
17
      contributors may be used to endorse or promote products derived
18
      from this software without specific prior written permission.
19
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
POSSIBILITY OF SUCH DAMAGE.
31
32
*/
33
34
#include "libtorrent/peer_class.hpp"
35
#include "libtorrent/peer_connection.hpp"
36
37
namespace libtorrent {
38
39
  void peer_class::set_upload_limit(int limit)
40
0
  {
41
0
    TORRENT_ASSERT(limit >= -1);
42
0
    if (limit < 0) limit = 0;
43
0
    if (limit < 10 && limit > 0) limit = 10;
44
0
    channel[peer_connection::upload_channel].throttle(limit);
45
0
  }
46
47
  void peer_class::set_download_limit(int limit)
48
0
  {
49
0
    TORRENT_ASSERT(limit >= -1);
50
0
    if (limit < 0) limit = 0;
51
0
    if (limit < 10 && limit > 0) limit = 10;
52
0
    channel[peer_connection::download_channel].throttle(limit);
53
0
  }
54
55
  void peer_class::get_info(peer_class_info* pci) const
56
0
  {
57
0
    pci->ignore_unchoke_slots = ignore_unchoke_slots;
58
0
    pci->connection_limit_factor = connection_limit_factor;
59
0
    pci->label = label;
60
0
    pci->upload_limit = channel[peer_connection::upload_channel].throttle();
61
0
    pci->download_limit = channel[peer_connection::download_channel].throttle();
62
0
    pci->upload_priority = priority[peer_connection::upload_channel];
63
0
    pci->download_priority = priority[peer_connection::download_channel];
64
0
  }
65
66
  void peer_class::set_info(peer_class_info const* pci)
67
0
  {
68
0
    ignore_unchoke_slots = pci->ignore_unchoke_slots;
69
0
    connection_limit_factor = pci->connection_limit_factor;
70
0
    label = pci->label;
71
0
    set_upload_limit(pci->upload_limit);
72
0
    set_download_limit(pci->download_limit);
73
0
    priority[peer_connection::upload_channel] = std::max(1, std::min(255, pci->upload_priority));
74
0
    priority[peer_connection::download_channel] = std::max(1, std::min(255, pci->download_priority));
75
0
  }
76
77
  peer_class_t peer_class_pool::new_peer_class(std::string label)
78
6.38k
  {
79
6.38k
    peer_class_t ret{0};
80
6.38k
    if (!m_free_list.empty())
81
0
    {
82
0
      ret = m_free_list.back();
83
0
      m_free_list.pop_back();
84
0
      m_peer_classes[ret] = peer_class(std::move(label));
85
0
    }
86
6.38k
    else
87
6.38k
    {
88
6.38k
      ret = m_peer_classes.end_index();
89
6.38k
      m_peer_classes.emplace_back(std::move(label));
90
6.38k
    }
91
92
6.38k
    return ret;
93
6.38k
  }
94
95
  void peer_class_pool::decref(peer_class_t c)
96
702
  {
97
702
    TORRENT_ASSERT(c < m_peer_classes.end_index());
98
702
    TORRENT_ASSERT(m_peer_classes[c].in_use);
99
702
    TORRENT_ASSERT(m_peer_classes[c].references > 0);
100
101
702
    --m_peer_classes[c].references;
102
702
    if (m_peer_classes[c].references) return;
103
351
    m_peer_classes[c].clear();
104
351
    m_free_list.push_back(c);
105
351
  }
106
107
  void peer_class_pool::incref(peer_class_t c)
108
351
  {
109
351
    TORRENT_ASSERT(c < m_peer_classes.end_index());
110
351
    TORRENT_ASSERT(m_peer_classes[c].in_use);
111
112
351
    ++m_peer_classes[c].references;
113
351
  }
114
115
  peer_class* peer_class_pool::at(peer_class_t c)
116
12.6k
  {
117
12.6k
    if (c >= m_peer_classes.end_index() || !m_peer_classes[c].in_use) return nullptr;
118
12.6k
    return &m_peer_classes[c];
119
12.6k
  }
120
121
  peer_class const* peer_class_pool::at(peer_class_t c) const
122
2
  {
123
2
    if (c >= m_peer_classes.end_index() || !m_peer_classes[c].in_use) return nullptr;
124
2
    return &m_peer_classes[c];
125
2
  }
126
}