Commit cef3decf authored by Andrew Hall's avatar Andrew Hall Committed by Facebook Github Bot

Add free function accessor

Summary: Adds an accessor for the free function.

Reviewed By: yfeldblum

Differential Revision: D13648280

fbshipit-source-id: ca92054b88b27569d6afec2ed4d1bd68351857cc
parent 01ed0e7d
...@@ -923,6 +923,16 @@ class IOBuf { ...@@ -923,6 +923,16 @@ class IOBuf {
return info ? info->userData : nullptr; return info ? info->userData : nullptr;
} }
/**
*
* Returns free function if sharedInfo() is not nullputr, nullptr otherwise
*
**/
FreeFunction getFreeFn() const {
SharedInfo* info = sharedInfo();
return info ? info->freeFn : nullptr;
}
/** /**
* Return true if all IOBufs in this chain are managed by the usual * Return true if all IOBufs in this chain are managed by the usual
* refcounting mechanism (and so the lifetime of the underlying memory * refcounting mechanism (and so the lifetime of the underlying memory
......
...@@ -192,6 +192,22 @@ TEST(IOBuf, GetUserData) { ...@@ -192,6 +192,22 @@ TEST(IOBuf, GetUserData) {
} }
} }
TEST(IOBuf, GetFreeFn) {
const uint32_t size = 4576;
uint8_t* data = static_cast<uint8_t*>(malloc(size));
folly::IOBuf::FreeFunction someFreeFn = [](void* buf, void* userData) {
EXPECT_EQ(buf, userData);
free(userData);
};
unique_ptr<IOBuf> someBuf(IOBuf::wrapBuffer(data, size));
unique_ptr<IOBuf> someOtherBuf(
IOBuf::takeOwnership(data, size, someFreeFn, data));
EXPECT_EQ(someBuf->getFreeFn(), nullptr);
EXPECT_EQ(someOtherBuf->getFreeFn(), someFreeFn);
}
TEST(IOBuf, WrapBuffer) { TEST(IOBuf, WrapBuffer) {
const uint32_t size1 = 1234; const uint32_t size1 = 1234;
uint8_t buf1[size1]; uint8_t buf1[size1];
......
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