1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 18:18:10 +00:00

refactor: use a std:: container in getBlockRequestPeers() (#1863)

* refactor: use std::vector in getBlockRequestPeers()
This commit is contained in:
Charles Kerr 2021-09-30 10:08:34 -05:00 committed by GitHub
parent 766d95b352
commit 7c4f66c44e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -11,6 +11,7 @@
#include <climits> /* INT_MAX */ #include <climits> /* INT_MAX */
#include <cstdlib> /* qsort */ #include <cstdlib> /* qsort */
#include <cstring> /* memcpy, memcmp, strstr */ #include <cstring> /* memcpy, memcmp, strstr */
#include <vector>
#include <event2/event.h> #include <event2/event.h>
@ -724,15 +725,13 @@ static struct block_request* requestListLookup(tr_swarm* s, tr_block_index_t blo
* Find the peers are we currently requesting the block * Find the peers are we currently requesting the block
* with index @a block from and append them to @a peerArr. * with index @a block from and append them to @a peerArr.
*/ */
static void getBlockRequestPeers(tr_swarm* s, tr_block_index_t block, tr_ptrArray* peerArr) static auto getBlockRequestPeers(tr_swarm* s, tr_block_index_t block)
{ {
bool exact; auto peers = std::vector<tr_peer*>{};
int pos;
struct block_request key;
key.block = block; auto const key = block_request{ block, nullptr, 0 };
key.peer = nullptr; bool exact;
pos = tr_lowerBound(&key, s->requests, s->requestCount, sizeof(struct block_request), compareReqByBlock, &exact); int const pos = tr_lowerBound(&key, s->requests, s->requestCount, sizeof(struct block_request), compareReqByBlock, &exact);
TR_ASSERT(!exact); /* shouldn't have a request with .peer == NULL */ TR_ASSERT(!exact); /* shouldn't have a request with .peer == NULL */
@ -743,8 +742,10 @@ static void getBlockRequestPeers(tr_swarm* s, tr_block_index_t block, tr_ptrArra
break; break;
} }
tr_ptrArrayAppend(peerArr, s->requests[i].peer); peers.push_back(s->requests[i].peer);
} }
return peers;
} }
static void decrementPendingReqCount(struct block_request const* b) static void decrementPendingReqCount(struct block_request const* b)
@ -1356,16 +1357,12 @@ void tr_peerMgrGetNextRequests(
{ {
tr_block_index_t first; tr_block_index_t first;
tr_block_index_t last; tr_block_index_t last;
tr_ptrArray peerArr = {};
tr_torGetPieceBlockRange(tor, p->index, &first, &last); tr_torGetPieceBlockRange(tor, p->index, &first, &last);
for (tr_block_index_t b = first; b <= last && (got < numwant || (get_intervals && setme[2 * got - 1] == b - 1)); for (tr_block_index_t b = first; b <= last && (got < numwant || (get_intervals && setme[2 * got - 1] == b - 1));
++b) ++b)
{ {
int peerCount;
tr_peer** peers;
/* don't request blocks we've already got */ /* don't request blocks we've already got */
if (tr_torrentBlockIsComplete(tor, b)) if (tr_torrentBlockIsComplete(tor, b))
{ {
@ -1373,10 +1370,8 @@ void tr_peerMgrGetNextRequests(
} }
/* always add peer if this block has no peers yet */ /* always add peer if this block has no peers yet */
tr_ptrArrayClear(&peerArr); auto const peers = getBlockRequestPeers(s, b);
getBlockRequestPeers(s, b, &peerArr); auto const peerCount = std::size(peers);
peers = (tr_peer**)tr_ptrArrayPeek(&peerArr, &peerCount);
if (peerCount != 0) if (peerCount != 0)
{ {
/* don't make a second block request until the endgame */ /* don't make a second block request until the endgame */
@ -1430,8 +1425,6 @@ void tr_peerMgrGetNextRequests(
requestListAdd(s, b, peer); requestListAdd(s, b, peer);
++p->requestCount; ++p->requestCount;
} }
tr_ptrArrayDestruct(&peerArr, nullptr);
} }
} }
@ -1679,16 +1672,8 @@ static void peerDeclinedAllRequests(tr_swarm* s, tr_peer const* peer)
static void cancelAllRequestsForBlock(tr_swarm* s, tr_block_index_t block, tr_peer* no_notify) static void cancelAllRequestsForBlock(tr_swarm* s, tr_block_index_t block, tr_peer* no_notify)
{ {
auto peerArr = tr_ptrArray{}; for (auto* p : getBlockRequestPeers(s, block))
getBlockRequestPeers(s, block, &peerArr);
int peerCount;
tr_peer** peers = (tr_peer**)tr_ptrArrayPeek(&peerArr, &peerCount);
for (int i = 0; i < peerCount; ++i)
{ {
tr_peer* p = peers[i];
if (p != no_notify && tr_isPeerMsgs(p)) if (p != no_notify && tr_isPeerMsgs(p))
{ {
tr_historyAdd(&p->cancelsSentToPeer, tr_time(), 1); tr_historyAdd(&p->cancelsSentToPeer, tr_time(), 1);
@ -1697,8 +1682,6 @@ static void cancelAllRequestsForBlock(tr_swarm* s, tr_block_index_t block, tr_pe
removeRequestFromTables(s, block, p); removeRequestFromTables(s, block, p);
} }
tr_ptrArrayDestruct(&peerArr, nullptr);
} }
void tr_peerMgrPieceCompleted(tr_torrent* tor, tr_piece_index_t p) void tr_peerMgrPieceCompleted(tr_torrent* tor, tr_piece_index_t p)