Commit f434460f authored by Nicolas J. Treat's avatar Nicolas J. Treat Committed by Facebook GitHub Bot

Add computeChainCapacity function to IOBuf

Summary:
This diff adds a computeChainCapacity method to IOBuf.

This method will loop through each chain of an IOBuf and return the total capacity of all IOBuf chains. This method is used very similarly to computeChainDataLength(), except rather than returning total data length, it returns total capacity.

Reviewed By: yfeldblum

Differential Revision: D28875393

fbshipit-source-id: 7e5f4a93916fa6c30eef8f8ee8c346a4537302af
parent ea968c30
...@@ -676,6 +676,14 @@ std::size_t IOBuf::computeChainDataLength() const { ...@@ -676,6 +676,14 @@ std::size_t IOBuf::computeChainDataLength() const {
return fullLength; return fullLength;
} }
std::size_t IOBuf::computeChainCapacity() const {
std::size_t fullCapacity = capacity_;
for (IOBuf* current = next_; current != this; current = current->next_) {
fullCapacity += current->capacity_;
}
return fullCapacity;
}
void IOBuf::prependChain(unique_ptr<IOBuf>&& iobuf) { void IOBuf::prependChain(unique_ptr<IOBuf>&& iobuf) {
// Take ownership of the specified IOBuf // Take ownership of the specified IOBuf
IOBuf* other = iobuf.release(); IOBuf* other = iobuf.release();
......
...@@ -839,6 +839,13 @@ class IOBuf { ...@@ -839,6 +839,13 @@ class IOBuf {
*/ */
std::size_t computeChainDataLength() const; std::size_t computeChainDataLength() const;
/**
* Get the capacity all IOBuf chains
*
* Beware that this method has to walk the entire chain.
*/
std::size_t computeChainCapacity() const;
/** /**
* Insert another IOBuf chain immediately before this IOBuf. * Insert another IOBuf chain immediately before this IOBuf.
* *
......
...@@ -1727,3 +1727,55 @@ TEST(IOBuf, FreeFn) { ...@@ -1727,3 +1727,55 @@ TEST(IOBuf, FreeFn) {
EXPECT_EQ(freeVal, 3); EXPECT_EQ(freeVal, 3);
EXPECT_EQ(releaseVal, 0); EXPECT_EQ(releaseVal, 0);
} }
// Compute the chained capacity of a single non-chained IOBuf of capacity zero
TEST(IOBuf, computeChainCapacityOfZeroSizeIOBuf) {
size_t size = 0;
uint8_t data[size];
// Create buffer of capacity 0
unique_ptr<IOBuf> buf(IOBuf::wrapBuffer(data, size));
EXPECT_EQ(buf->computeChainCapacity(), 0);
}
// Compute the chained capacity of a single non-chained IOBuf of capacity
// non-zero
TEST(IOBuf, computeChainCapacityOfNonZeroSizeIOBuf) {
size_t size = 20;
uint8_t data[size];
// Create buffer of capacity 20
unique_ptr<IOBuf> buf(IOBuf::wrapBuffer(data, size));
EXPECT_EQ(buf->computeChainCapacity(), 20);
}
// Compute the chained capacity of a chained IOBuf with chains having a variety
// of zero and non-zero capacities.
TEST(IOBuf, computeChainCapacityOfMixedCapacityChainedIOBuf) {
// Total capacity is 100
uint8_t data1[20];
uint8_t data2[0];
uint8_t data3[60];
uint8_t data4[15];
uint8_t data5[0];
uint8_t data6[5];
// Create IOBuf at head of chain
unique_ptr<IOBuf> buf(IOBuf::wrapBuffer(data1, sizeof(data1)));
// Create IOBuf chains
auto temp = buf.get();
temp->appendChain(IOBuf::wrapBuffer(data2, sizeof(data2)));
temp = temp->next();
temp->appendChain(IOBuf::wrapBuffer(data3, sizeof(data3)));
temp = temp->next();
temp->appendChain(IOBuf::wrapBuffer(data4, sizeof(data4)));
temp = temp->next();
temp->appendChain(IOBuf::wrapBuffer(data5, sizeof(data5)));
temp = temp->next();
temp->appendChain(IOBuf::wrapBuffer(data6, sizeof(data6)));
EXPECT_EQ(buf->computeChainCapacity(), 100);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment