Coverage Report

Created: 2025-08-28 06:21

/src/libtorrent/src/bandwidth_limit.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
3
Copyright (c) 2009-2011, 2016-2017, 2019-2020, Arvid Norberg
4
Copyright (c) 2009, Georg Rudoy
5
Copyright (c) 2017, Andrei Kurushin
6
Copyright (c) 2018, 2020, Alden Torres
7
All rights reserved.
8
9
Redistribution and use in source and binary forms, with or without
10
modification, are permitted provided that the following conditions
11
are met:
12
13
    * Redistributions of source code must retain the above copyright
14
      notice, this list of conditions and the following disclaimer.
15
    * Redistributions in binary form must reproduce the above copyright
16
      notice, this list of conditions and the following disclaimer in
17
      the documentation and/or other materials provided with the distribution.
18
    * Neither the name of the author nor the names of its
19
      contributors may be used to endorse or promote products derived
20
      from this software without specific prior written permission.
21
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
POSSIBILITY OF SUCH DAMAGE.
33
34
*/
35
36
#include "libtorrent/aux_/bandwidth_limit.hpp"
37
#include <algorithm>
38
39
namespace libtorrent {
40
namespace aux {
41
42
  bandwidth_channel::bandwidth_channel()
43
0
    : tmp(0)
44
0
    , distribute_quota(0)
45
0
    , m_quota_left(0)
46
0
    , m_limit(0)
47
0
  {}
48
49
  // 0 means infinite
50
  void bandwidth_channel::throttle(int const limit)
51
0
  {
52
0
    TORRENT_ASSERT_VAL(limit >= 0, limit);
53
    // if the throttle is more than this, we might overflow
54
0
    TORRENT_ASSERT_VAL(limit < inf, limit);
55
0
    m_limit = limit;
56
0
  }
57
58
  int bandwidth_channel::quota_left() const
59
0
  {
60
0
    if (m_limit == 0) return inf;
61
0
    return std::max(int(m_quota_left), 0);
62
0
  }
63
64
  void bandwidth_channel::update_quota(int const dt_milliseconds)
65
0
  {
66
0
    TORRENT_ASSERT_VAL(m_limit >= 0, m_limit);
67
0
    TORRENT_ASSERT_VAL(m_limit < inf, m_limit);
68
69
0
    if (m_limit == 0) return;
70
71
    // "to_add" should never have int64 overflow: "m_limit" contains < "<int>::max"
72
0
    std::int64_t const to_add = (std::int64_t(m_limit) * dt_milliseconds + 500) / 1000;
73
74
0
    if (to_add > inf - m_quota_left)
75
0
    {
76
0
      m_quota_left = inf;
77
0
    }
78
0
    else
79
0
    {
80
0
      m_quota_left += to_add;
81
0
      if (m_quota_left / 3 > m_limit) m_quota_left = std::int64_t(m_limit) * 3;
82
      // "m_quota_left" will never have int64 overflow but may exceed "<int>::max"
83
0
      m_quota_left = std::min(m_quota_left, std::int64_t(inf));
84
0
    }
85
86
0
    distribute_quota = int(std::max(m_quota_left, std::int64_t(0)));
87
0
  }
88
89
  // this is used when connections disconnect with
90
  // some quota left. It's returned to its bandwidth
91
  // channels.
92
  void bandwidth_channel::return_quota(int const amount)
93
0
  {
94
0
    TORRENT_ASSERT(amount >= 0);
95
0
    if (m_limit == 0) return;
96
0
    TORRENT_ASSERT(m_quota_left <= m_quota_left + amount);
97
0
    m_quota_left += amount;
98
0
  }
99
100
  void bandwidth_channel::use_quota(int const amount)
101
0
  {
102
0
    TORRENT_ASSERT(amount >= 0);
103
0
    TORRENT_ASSERT(m_limit >= 0);
104
0
    if (m_limit == 0) return;
105
106
0
    m_quota_left -= amount;
107
0
  }
108
109
}
110
}