1
0
mirror of https://github.com/facebookincubator/mvfst.git synced 2025-11-25 15:43:13 +03:00

Back out "BufQueue::trimAtMost"

Summary:
Original commit changeset: 0ace20d07d8b

Original Phabricator Diff: D46776767

Reviewed By: lnicco

Differential Revision: D47007928

fbshipit-source-id: 6c8760683692d1b50168f8f5d82d79c98ebe7c31
This commit is contained in:
Hani Damlaj
2023-06-25 16:44:20 -07:00
committed by Facebook GitHub Bot
parent 69ff02e35f
commit 711f0e30fc
3 changed files with 45 additions and 20 deletions

View File

@@ -840,22 +840,20 @@ QuicFrame parseFrame(
throw QuicTransportException(
"Invalid frame-type field", TransportErrorCode::FRAME_ENCODING_ERROR);
}
queue.trimStart(frameTypeInt->second);
queue.trimStart(cursor - queue.front());
bool consumedQueue = false;
bool error = false;
SCOPE_EXIT {
if (consumedQueue || error) {
return;
}
if (!queue.empty()) {
queue.trimStart(cursor - queue.front());
}
queue.trimStart(cursor - queue.front());
};
// trimStart() may have free'd the IOBuf;
folly::IOBuf emptyBuf{};
cursor.reset(queue.empty() ? &emptyBuf : queue.front());
cursor.reset(queue.front());
FrameType frameType = static_cast<FrameType>(frameTypeInt->first);
try {
try
{
switch (frameType) {
case FrameType::PADDING:
return QuicFrame(decodePaddingFrame(cursor));

View File

@@ -59,9 +59,45 @@ Buf BufQueue::splitAtMost(size_t len) {
}
size_t BufQueue::trimStartAtMost(size_t amount) {
auto oldChainLength = chainLength_;
splitAtMost(amount);
return oldChainLength - chainLength_;
auto original = amount;
folly::IOBuf* current = chain_.get();
if (current == nullptr || amount == 0) {
return 0;
}
while (amount > 0) {
if (current->length() >= amount) {
current->trimStart(amount);
amount = 0;
break;
}
amount -= current->length();
current = current->next();
if (current == chain_.get()) {
break;
}
}
auto prev = current->prev();
/** We are potentially in 2 states here,
* 1. we found the entire amount
* 2. or we did not.
* If we did not find the entire amount, then current ==
* chain and we can remove the entire chain.
* If we did, then we can split from the chain head to the previous buffer and
* then keep the current buffer.
*/
if (prev != current && current != chain_.get()) {
auto chain = chain_.release();
current->separateChain(chain, prev);
chain_ = std::unique_ptr<folly::IOBuf>(current);
} else if (amount > 0) {
DCHECK_EQ(current, chain_.get());
chain_ = nullptr;
}
size_t trimmed = original - amount;
DCHECK_GE(chainLength_, trimmed);
chainLength_ -= trimmed;
DCHECK(chainLength_ == 0 || !chain_->empty());
return trimmed;
}
// TODO replace users with trimStartAtMost

View File

@@ -187,15 +187,6 @@ TEST(BufQueue, TrimStartAtMost) {
checkConsistency(queue);
}
TEST(BufQueue, TrimStartOneByte) {
BufQueue queue;
queue.append(IOBuf::copyBuffer(SCL("H")));
checkConsistency(queue);
queue.trimStart(1);
checkConsistency(queue);
EXPECT_TRUE(queue.front() == nullptr);
}
TEST(BufQueue, CloneBufNull) {
BufQueue queue;
auto buf = queue.clone();