From 713c412682d5e600fda926c25def4caaa100af89 Mon Sep 17 00:00:00 2001 From: Andrei Kurushin Date: Sun, 12 Mar 2017 18:34:42 +0300 Subject: [PATCH] loop conversion #2 (#1804) loop conversions. replace unsafe "erase(iter++)" with "iter = erase(iter)" --- examples/client_test.cpp | 4 +- src/file_pool.cpp | 10 +-- src/kademlia/dht_storage.cpp | 6 +- src/kademlia/rpc_manager.cpp | 2 +- src/session_impl.cpp | 146 ++++++++++++++--------------------- src/smart_ban.cpp | 9 +-- src/string_util.cpp | 15 ++-- src/torrent_info.cpp | 5 +- src/ut_pex.cpp | 9 +-- src/utp_socket_manager.cpp | 9 +-- src/utp_stream.cpp | 32 ++++---- src/web_connection_base.cpp | 7 +- 12 files changed, 107 insertions(+), 147 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index e024231c7..fa73325ea 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -781,7 +781,7 @@ void scan_dir(std::string const& dir_path torrent_handle& h = i->second; if (!h.is_valid()) { - files.erase(i++); + i = files.erase(i); continue; } @@ -792,7 +792,7 @@ void scan_dir(std::string const& dir_path h.save_resume_data(); ++num_outstanding_resume_data; - files.erase(i++); + i = files.erase(i); } } diff --git a/src/file_pool.cpp b/src/file_pool.cpp index c1f8f20d6..193bb7b51 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -239,16 +239,14 @@ namespace libtorrent { std::unique_lock l(m_mutex); - auto begin = m_files.lower_bound(std::make_pair(st, file_index_t(0))); + auto const begin = m_files.lower_bound(std::make_pair(st, file_index_t(0))); auto const end = m_files.upper_bound(std::make_pair(st , std::numeric_limits::max())); std::vector to_close; - while (begin != end) - { - to_close.push_back(std::move(begin->second.file_ptr)); - m_files.erase(begin++); - } + for (auto it = begin; it != end; ++it) + to_close.push_back(std::move(it->second.file_ptr)); + if (!to_close.empty()) m_files.erase(begin, end); l.unlock(); // the files are closed here while the lock is not held } diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index 691a6fd20..1c409c184 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -497,7 +497,7 @@ namespace } // if there are no more peers, remove the entry altogether - m_map.erase(i++); + i = m_map.erase(i); m_counters.torrents -= 1;// peers is decreased by purge_peers } @@ -515,7 +515,7 @@ namespace ++i; continue; } - m_immutable_table.erase(i++); + i = m_immutable_table.erase(i); m_counters.immutable_data -= 1; } @@ -526,7 +526,7 @@ namespace ++i; continue; } - m_mutable_table.erase(i++); + i = m_mutable_table.erase(i); m_counters.mutable_data -= 1; } } diff --git a/src/kademlia/rpc_manager.cpp b/src/kademlia/rpc_manager.cpp index a1a1b8c5e..828a37853 100644 --- a/src/kademlia/rpc_manager.cpp +++ b/src/kademlia/rpc_manager.cpp @@ -403,7 +403,7 @@ time_duration rpc_manager::tick() , print_endpoint(o->target_ep()).c_str()); } #endif - m_transactions.erase(i++); + i = m_transactions.erase(i); timeouts.push_back(o); continue; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index d2e839a14..e63bd0b04 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1022,9 +1022,8 @@ namespace aux { // Close connections whose endpoint is filtered // by the new ip-filter - for (torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) - i->second->set_ip_filter(m_ip_filter); + for (auto& i : m_torrents) + i.second->set_ip_filter(m_ip_filter); } void session_impl::ban_ip(address addr) @@ -1032,9 +1031,8 @@ namespace aux { TORRENT_ASSERT(is_single_thread()); if (!m_ip_filter) m_ip_filter = std::make_shared(); m_ip_filter->add_rule(addr, addr, ip_filter::blocked); - for (torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) - i->second->set_ip_filter(m_ip_filter); + for (auto& i : m_torrents) + i.second->set_ip_filter(m_ip_filter); } ip_filter const& session_impl::get_ip_filter() @@ -1367,11 +1365,10 @@ namespace aux { tcp::endpoint session_impl::get_ipv6_interface() const { #if TORRENT_USE_IPV6 - for (std::list::const_iterator i = m_listen_sockets.begin() - , end(m_listen_sockets.end()); i != end; ++i) + for (auto const& i : m_listen_sockets) { - if (!i->local_endpoint.address().is_v6()) continue; - return tcp::endpoint(i->local_endpoint.address(), std::uint16_t(i->tcp_external_port)); + if (!i.local_endpoint.address().is_v6()) continue; + return tcp::endpoint(i.local_endpoint.address(), std::uint16_t(i.tcp_external_port)); } #endif return tcp::endpoint(); @@ -1379,11 +1376,10 @@ namespace aux { tcp::endpoint session_impl::get_ipv4_interface() const { - for (std::list::const_iterator i = m_listen_sockets.begin() - , end(m_listen_sockets.end()); i != end; ++i) + for (auto const& i : m_listen_sockets) { - if (!i->local_endpoint.address().is_v4()) continue; - return tcp::endpoint(i->local_endpoint.address(), std::uint16_t(i->tcp_external_port)); + if (!i.local_endpoint.address().is_v4()) continue; + return tcp::endpoint(i.local_endpoint.address(), std::uint16_t(i.tcp_external_port)); } return tcp::endpoint(); } @@ -2176,22 +2172,21 @@ namespace aux { // for now, just pick the first socket with a matching address family // TODO: 3 for proper multi-homed support, we may want to do something // else here. Probably let the caller decide which interface to send over - for (std::list::iterator i = m_listen_sockets.begin() - , end(m_listen_sockets.end()); i != end; ++i) + for (auto& i : m_listen_sockets) { - if (!i->udp_sock) continue; - if (i->ssl) continue; + if (!i.udp_sock) continue; + if (i.ssl) continue; - i->udp_sock->send_hostname(hostname, port, p, ec, flags); + i.udp_sock->send_hostname(hostname, port, p, ec, flags); if ((ec == error::would_block || ec == error::try_again) - && !i->udp_write_blocked) + && !i.udp_write_blocked) { - i->udp_write_blocked = true; + i.udp_write_blocked = true; ADD_OUTSTANDING_ASYNC("session_impl::on_udp_writeable"); - i->udp_sock->async_write(std::bind(&session_impl::on_udp_writeable - , this, std::weak_ptr(i->udp_sock), _1)); + i.udp_sock->async_write(std::bind(&session_impl::on_udp_writeable + , this, std::weak_ptr(i.udp_sock), _1)); } return; } @@ -2207,24 +2202,23 @@ namespace aux { // for now, just pick the first socket with a matching address family // TODO: 3 for proper multi-homed support, we may want to do something // else here. Probably let the caller decide which interface to send over - for (std::list::iterator i = m_listen_sockets.begin() - , end(m_listen_sockets.end()); i != end; ++i) + for (auto& i : m_listen_sockets) { - if (i->ssl != ssl) continue; - if (!i->udp_sock) continue; - if (i->local_endpoint.address().is_v4() != ep.address().is_v4()) + if (i.ssl != ssl) continue; + if (!i.udp_sock) continue; + if (i.local_endpoint.address().is_v4() != ep.address().is_v4()) continue; - i->udp_sock->send(ep, p, ec, flags); + i.udp_sock->send(ep, p, ec, flags); if ((ec == error::would_block || ec == error::try_again) - && !i->udp_write_blocked) + && !i.udp_write_blocked) { - i->udp_write_blocked = true; + i.udp_write_blocked = true; ADD_OUTSTANDING_ASYNC("session_impl::on_udp_writeable"); - i->udp_sock->async_write(std::bind(&session_impl::on_udp_writeable - , this, std::weak_ptr(i->udp_sock), _1)); + i.udp_sock->async_write(std::bind(&session_impl::on_udp_writeable + , this, std::weak_ptr(i.udp_sock), _1)); } return; } @@ -2796,10 +2790,9 @@ namespace aux { if (!m_settings.get_bool(settings_pack::incoming_starts_queued_torrents)) { bool has_active_torrent = false; - for (torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) + for (auto& i : m_torrents) { - if (!i->second->is_torrent_paused()) + if (!i.second->is_torrent_paused()) { has_active_torrent = true; break; @@ -3146,10 +3139,9 @@ namespace aux { m_created += hours(4); const int four_hours = 60 * 60 * 4; - for (torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) + for (auto& i : m_torrents) { - i->second->step_session_time(four_hours); + i.second->step_session_time(four_hours); } } @@ -3617,11 +3609,8 @@ namespace aux { void session_impl::auto_manage_checking_torrents(std::vector& list , int& limit) { - for (std::vector::iterator i = list.begin() - , end(list.end()); i != end; ++i) + for (auto& t : list) { - torrent* t = *i; - TORRENT_ASSERT(t->state() == torrent_status::checking_files); TORRENT_ASSERT(t->is_auto_managed()); if (limit <= 0) @@ -3641,11 +3630,8 @@ namespace aux { , int& dht_limit, int& tracker_limit , int& lsd_limit, int& hard_limit, int type_limit) { - for (std::vector::iterator i = list.begin() - , end(list.end()); i != end; ++i) + for (auto& t : list) { - torrent* t = *i; - TORRENT_ASSERT(t->state() != torrent_status::checking_files); // inactive torrents don't count (and if you configured them to do so, @@ -4307,10 +4293,9 @@ namespace aux { // torrent pointers. Maybe this logic could be simplified if (p >= 0 && me->queue_position() == -1) { - for (session_impl::torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) + for (auto& i : m_torrents) { - torrent* t = i->second.get(); + torrent* t = i.second.get(); if (t->queue_position() >= p) { t->set_queue_position_impl(t->queue_position()+1); @@ -4325,10 +4310,9 @@ namespace aux { { TORRENT_ASSERT(me->queue_position() >= 0); TORRENT_ASSERT(p == -1); - for (session_impl::torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) + for (auto& i : m_torrents) { - torrent* t = i->second.get(); + torrent* t = i.second.get(); if (t == me) continue; if (t->queue_position() == -1) continue; if (t->queue_position() >= me->queue_position()) @@ -4342,10 +4326,9 @@ namespace aux { } else if (p < me->queue_position()) { - for (session_impl::torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) + for (auto& i : m_torrents) { - torrent* t = i->second.get(); + torrent* t = i.second.get(); if (t == me) continue; if (t->queue_position() == -1) continue; if (t->queue_position() >= p @@ -4359,10 +4342,9 @@ namespace aux { } else if (p > me->queue_position()) { - for (session_impl::torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) + for (auto& i : m_torrents) { - torrent* t = i->second.get(); + torrent* t = i.second.get(); int pos = t->queue_position(); if (t == me) continue; if (pos == -1) continue; @@ -4580,12 +4562,10 @@ namespace aux { { std::vector ret; - for (torrent_map::const_iterator i - = m_torrents.begin(), end(m_torrents.end()); - i != end; ++i) + for (auto const& i : m_torrents) { - if (i->second->is_aborted()) continue; - ret.push_back(torrent_handle(i->second)); + if (i.second->is_aborted()) continue; + ret.push_back(torrent_handle(i.second)); } return ret; } @@ -5165,18 +5145,14 @@ namespace aux { void session_impl::update_auto_sequential() { - for (torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) - i->second->update_auto_sequential(); + for (auto& i : m_torrents) + i.second->update_auto_sequential(); } void session_impl::update_max_failcount() { - for (torrent_map::iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) - { - i->second->update_max_failcount(); - } + for (auto& i : m_torrents) + i.second->update_max_failcount(); } void session_impl::update_close_file_interval() @@ -5197,10 +5173,9 @@ namespace aux { // in case we just set a socks proxy, we might have to // open the socks incoming connection if (!m_socks_listen_socket) open_new_incoming_socks_connection(); - for (std::list::iterator i = m_listen_sockets.begin() - , end(m_listen_sockets.end()); i != end; ++i) + for (auto& i : m_listen_sockets) { - i->udp_sock->set_proxy_settings(proxy()); + i.udp_sock->set_proxy_settings(proxy()); } } @@ -5523,10 +5498,9 @@ namespace aux { // this loop is potentially expensive. It could be optimized by // simply keeping a global counter int peerlist_size = 0; - for (torrent_map::const_iterator i = m_torrents.begin() - , end(m_torrents.end()); i != end; ++i) + for (auto const& i : m_torrents) { - peerlist_size += i->second->num_known_peers(); + peerlist_size += i.second->num_known_peers(); } s.peerlist_size = peerlist_size; @@ -6236,17 +6210,16 @@ namespace aux { void session_impl::update_force_proxy() { - for (std::list::iterator i = m_listen_sockets.begin() - , end(m_listen_sockets.end()); i != end; ++i) + for (auto& i : m_listen_sockets) { - i->udp_sock->set_force_proxy(m_settings.get_bool(settings_pack::force_proxy)); + i.udp_sock->set_force_proxy(m_settings.get_bool(settings_pack::force_proxy)); // close the TCP listen sockets - if (i->sock) + if (i.sock) { error_code ec; - i->sock->close(ec); - i->sock.reset(); + i.sock->close(ec); + i.sock.reset(); } } @@ -6769,10 +6742,9 @@ namespace aux { for (int l = 0; l < num_torrent_lists; ++l) { std::vector const& list = m_torrent_lists[l]; - for (std::vector::const_iterator i = list.begin() - , end(list.end()); i != end; ++i) + for (auto const& i : list) { - TORRENT_ASSERT((*i)->m_links[l].in_list()); + TORRENT_ASSERT(i->m_links[l].in_list()); } } diff --git a/src/smart_ban.cpp b/src/smart_ban.cpp index 2179839bf..8753b7b04 100644 --- a/src/smart_ban.cpp +++ b/src/smart_ban.cpp @@ -97,7 +97,7 @@ namespace , r, std::bind(&smart_ban_plugin::on_read_ok_block , shared_from_this(), *i, i->second.peer->address(), _1, r.length, _2, _3) , reinterpret_cast(1)); - m_block_hashes.erase(i++); + i = m_block_hashes.erase(i); } else { @@ -141,10 +141,9 @@ namespace int size = m_torrent.torrent_file().piece_size(p); peer_request r = {p, 0, (std::min)(16*1024, size)}; piece_block pb(p, 0); - for (std::vector::iterator i = downloaders.begin() - , end(downloaders.end()); i != end; ++i) + for (auto const& i : downloaders) { - if (*i != nullptr) + if (i != nullptr) { // for very sad and involved reasons, this read need to force a copy out of the cache // since the piece has failed, this block is very likely to be replaced with a newly @@ -152,7 +151,7 @@ namespace // block read will have been deleted by the time it gets back to the network thread m_torrent.session().disk_thread().async_read(m_torrent.storage(), r , std::bind(&smart_ban_plugin::on_read_failed_block - , shared_from_this(), pb, (*i)->address(), _1, r.length, _2, _3) + , shared_from_this(), pb, i->address(), _1, r.length, _2, _3) , reinterpret_cast(1) , disk_io_job::force_copy); } diff --git a/src/string_util.cpp b/src/string_util.cpp index 662184c79..16376c2be 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -179,29 +179,28 @@ namespace libtorrent std::string print_listen_interfaces(std::vector const& in) { std::string ret; - for (std::vector::const_iterator i = in.begin() - , end(in.end()); i != end; ++i) + for (auto const& i : in) { - if (i != in.begin()) ret += ","; + if (!ret.empty()) ret += ","; #if TORRENT_USE_IPV6 error_code ec; - address_v6::from_string(i->device, ec); + address_v6::from_string(i.device, ec); if (!ec) { // IPv6 addresses must be wrapped in square brackets ret += "["; - ret += i->device; + ret += i.device; ret += "]"; } else #endif { - ret += i->device; + ret += i.device; } ret += ":"; - ret += to_string(i->port).data(); - if (i->ssl) ret += "s"; + ret += to_string(i.port).data(); + if (i.ssl) ret += "s"; } return ret; diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 5ff8370a3..c77f7fed4 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -727,10 +727,9 @@ namespace libtorrent // insert all directories first, to make sure no files // are allowed to collied with them - for (std::vector::const_iterator i = paths.begin() - , end(paths.end()); i != end; ++i) + for (auto const& i : paths) { - std::string p = combine_path(m_files.name(), *i); + std::string p = combine_path(m_files.name(), i); files.insert(p); while (has_parent_path(p)) { diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index f39dbb850..3a52f00ff 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -200,14 +200,13 @@ namespace libtorrent { namespace } } - for (std::set::const_iterator i = dropped.begin() - , end(dropped.end()); i != end; ++i) + for (auto const& i : dropped) { - if (i->address().is_v4()) - detail::write_endpoint(*i, pld_out); + if (i.address().is_v4()) + detail::write_endpoint(i, pld_out); #if TORRENT_USE_IPV6 else - detail::write_endpoint(*i, pld6_out); + detail::write_endpoint(i, pld6_out); #endif ++m_peers_in_message; } diff --git a/src/utp_socket_manager.cpp b/src/utp_socket_manager.cpp index 83e7f512e..5664ed231 100644 --- a/src/utp_socket_manager.cpp +++ b/src/utp_socket_manager.cpp @@ -67,23 +67,22 @@ namespace libtorrent utp_socket_manager::~utp_socket_manager() { - for (socket_map_t::iterator i = m_utp_sockets.begin() - , end(m_utp_sockets.end()); i != end; ++i) + for (auto& i : m_utp_sockets) { - delete_utp_impl(i->second); + delete_utp_impl(i.second); } } void utp_socket_manager::tick(time_point now) { - for (socket_map_t::iterator i = m_utp_sockets.begin() + for (auto i = m_utp_sockets.begin() , end(m_utp_sockets.end()); i != end;) { if (should_delete(i->second)) { delete_utp_impl(i->second); if (m_last_socket == i->second) m_last_socket = nullptr; - m_utp_sockets.erase(i++); + i = m_utp_sockets.erase(i); continue; } tick_utp_impl(i->second, now); diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index 5c6b9e476..78803ba52 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -936,11 +936,10 @@ void utp_stream::add_write_buffer(void const* buf, size_t len) #if TORRENT_USE_ASSERTS int write_buffer_size = 0; - for (std::vector::iterator i = m_impl->m_write_buffer.begin() - , end(m_impl->m_write_buffer.end()); i != end; ++i) + for (auto const& i : m_impl->m_write_buffer) { - TORRENT_ASSERT(std::numeric_limits::max() - int(i->len) > write_buffer_size); - write_buffer_size += int(i->len); + TORRENT_ASSERT(std::numeric_limits::max() - int(i.len) > write_buffer_size); + write_buffer_size += int(i.len); } TORRENT_ASSERT(m_impl->m_write_buffer_size == write_buffer_size); #endif @@ -950,11 +949,10 @@ void utp_stream::add_write_buffer(void const* buf, size_t len) #if TORRENT_USE_ASSERTS write_buffer_size = 0; - for (std::vector::iterator i = m_impl->m_write_buffer.begin() - , end(m_impl->m_write_buffer.end()); i != end; ++i) + for (auto const& i : m_impl->m_write_buffer) { - TORRENT_ASSERT(std::numeric_limits::max() - int(i->len) > write_buffer_size); - write_buffer_size += int(i->len); + TORRENT_ASSERT(std::numeric_limits::max() - int(i.len) > write_buffer_size); + write_buffer_size += int(i.len); } TORRENT_ASSERT(m_impl->m_write_buffer_size == write_buffer_size); #endif @@ -1000,7 +998,7 @@ size_t utp_stream::read_some(bool clear_buffers) return 0; } - std::vector::iterator target = m_impl->m_read_buffer.begin(); + auto target = m_impl->m_read_buffer.begin(); std::size_t ret = 0; @@ -1544,17 +1542,16 @@ void utp_socket_impl::write_payload(std::uint8_t* ptr, int size) #if TORRENT_USE_ASSERTS int write_buffer_size = 0; - for (std::vector::iterator i = m_write_buffer.begin() - , end(m_write_buffer.end()); i != end; ++i) + for (auto const& i : m_write_buffer) { - TORRENT_ASSERT(std::numeric_limits::max() - int(i->len) > write_buffer_size); - write_buffer_size += int(i->len); + TORRENT_ASSERT(std::numeric_limits::max() - int(i.len) > write_buffer_size); + write_buffer_size += int(i.len); } TORRENT_ASSERT(m_write_buffer_size == write_buffer_size); #endif TORRENT_ASSERT(!m_write_buffer.empty() || size == 0); TORRENT_ASSERT(m_write_buffer_size >= size); - std::vector::iterator i = m_write_buffer.begin(); + auto i = m_write_buffer.begin(); if (size == 0) return; @@ -1583,11 +1580,10 @@ void utp_socket_impl::write_payload(std::uint8_t* ptr, int size) #if TORRENT_USE_ASSERTS write_buffer_size = 0; - for (std::vector::iterator j = m_write_buffer.begin() - , end(m_write_buffer.end()); j != end; ++j) + for (auto const& j : m_write_buffer) { - TORRENT_ASSERT(std::numeric_limits::max() - int(j->len) > write_buffer_size); - write_buffer_size += int(j->len); + TORRENT_ASSERT(std::numeric_limits::max() - int(j.len) > write_buffer_size); + write_buffer_size += int(j.len); } TORRENT_ASSERT(m_write_buffer_size == write_buffer_size); #endif diff --git a/src/web_connection_base.cpp b/src/web_connection_base.cpp index 529608db5..7812cf1b8 100644 --- a/src/web_connection_base.cpp +++ b/src/web_connection_base.cpp @@ -142,12 +142,11 @@ namespace libtorrent request += base64encode(sett.get_str(settings_pack::proxy_username) + ":" + sett.get_str(settings_pack::proxy_password)); } - for (web_seed_entry::headers_t::const_iterator it = m_extra_headers.begin(); - it != m_extra_headers.end(); ++it) { + for (auto const& h : m_extra_headers) { request += "\r\n"; - request += it->first; + request += h.first; request += ": "; - request += it->second; + request += h.second; } if (using_proxy) { request += "\r\nProxy-Connection: keep-alive";