From 760acc8f306d8fba2ecd9fd39bd512b9ce6fdac0 Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Wed, 19 Sep 2007 02:45:29 +0000 Subject: [PATCH] more asserts to bandwidth limiter --- .../include/libtorrent/bandwidth_manager.hpp | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/libtorrent/include/libtorrent/bandwidth_manager.hpp b/libtorrent/include/libtorrent/bandwidth_manager.hpp index 03d4f65ae..1e1b29699 100644 --- a/libtorrent/include/libtorrent/bandwidth_manager.hpp +++ b/libtorrent/include/libtorrent/bandwidth_manager.hpp @@ -180,7 +180,12 @@ struct bandwidth_manager , m_limit(bandwidth_limit::inf) , m_current_quota(0) , m_channel(channel) - {} + { + +#ifndef NDEBUG + m_in_hand_out_bandwidth = false; +#endif + } void throttle(int limit) throw() { @@ -329,6 +334,9 @@ private: void hand_out_bandwidth() throw() { #ifndef NDEBUG + assert(m_in_hand_out_bandwidth == false); + + m_in_hand_out_bandwidth = true; try { #endif INVARIANT_CHECK; @@ -361,6 +369,7 @@ private: if (qe.peer->is_disconnecting()) { t->expire_bandwidth(m_channel, qe.max_block_size); + assert(amount == limit - m_current_quota); continue; } @@ -374,6 +383,7 @@ private: if (max_assignable == 0) { t->expire_bandwidth(m_channel, qe.max_block_size); + assert(amount == limit - m_current_quota); continue; } @@ -388,15 +398,15 @@ private: // the history window is one second, and the block will be forgotten // after one second. int block_size = (std::min)(qe.peer->bandwidth_throttle(m_channel) - , m_limit / 10); + , limit / 10); if (block_size < min_bandwidth_block_size) { - block_size = (std::min)(int(min_bandwidth_block_size), m_limit); + block_size = (std::min)(int(min_bandwidth_block_size), limit); } else if (block_size > max_bandwidth_block_size) { - if (m_limit == bandwidth_limit::inf) + if (limit == bandwidth_limit::inf) { block_size = max_bandwidth_block_size; } @@ -407,8 +417,8 @@ private: // as possible // TODO: move this calculcation to where the limit // is changed - block_size = m_limit - / (m_limit / max_bandwidth_block_size); + block_size = limit + / (limit / max_bandwidth_block_size); } } if (block_size > qe.max_block_size) block_size = qe.max_block_size; @@ -428,17 +438,21 @@ private: int hand_out_amount = (std::min)((std::min)(block_size, max_assignable) , amount); assert(hand_out_amount > 0); + assert(amount == limit - m_current_quota); amount -= hand_out_amount; assert(hand_out_amount <= qe.max_block_size); t->assign_bandwidth(m_channel, hand_out_amount, qe.max_block_size); qe.peer->assign_bandwidth(m_channel, hand_out_amount); add_history_entry(history_entry( qe.peer, t, hand_out_amount, now + bw_window_size)); + assert(amount == limit - m_current_quota); } #ifndef NDEBUG } catch (std::exception& e) { assert(false); }; + + m_in_hand_out_bandwidth = false; #endif } @@ -472,9 +486,15 @@ private: // this is the channel within the consumers // that bandwidth is assigned to (upload or download) int m_channel; + +#ifndef NDEBUG + bool m_in_hand_out_bandwidth; +#endif + }; } #endif +