Commit 9037e3ac authored by Dan Melnic's avatar Dan Melnic Committed by Facebook Github Bot

Use folly::sizedFree for the ext buffer

Summary: Use folly::sizedFree for the ext buffer

Reviewed By: yfeldblum

Differential Revision: D19934204

fbshipit-source-id: 63662ab547f15addc7cdb249704964dfc70ca1f3
parent 8bf79388
...@@ -358,6 +358,10 @@ IOBuf::IOBuf( ...@@ -358,6 +358,10 @@ IOBuf::IOBuf(
capacity_(capacity), capacity_(capacity),
flagsAndSharedInfo_( flagsAndSharedInfo_(
packFlagsAndSharedInfo(kFlagFreeSharedInfo, nullptr)) { packFlagsAndSharedInfo(kFlagFreeSharedInfo, nullptr)) {
// do not allow only user data without a freeFn
// since we use that for folly::sizedFree
DCHECK(!userData || (userData && freeFn));
auto rollback = makeGuard([&] { // auto rollback = makeGuard([&] { //
takeOwnershipError(freeOnError, buf, freeFn, userData); takeOwnershipError(freeOnError, buf, freeFn, userData);
}); });
...@@ -372,6 +376,10 @@ unique_ptr<IOBuf> IOBuf::takeOwnership( ...@@ -372,6 +376,10 @@ unique_ptr<IOBuf> IOBuf::takeOwnership(
FreeFunction freeFn, FreeFunction freeFn,
void* userData, void* userData,
bool freeOnError) { bool freeOnError) {
// do not allow only user data without a freeFn
// since we use that for folly::sizedFree
DCHECK(!userData || (userData && freeFn));
HeapFullStorage* storage = nullptr; HeapFullStorage* storage = nullptr;
auto rollback = makeGuard([&] { auto rollback = makeGuard([&] {
if (storage) { if (storage) {
...@@ -940,6 +948,8 @@ void IOBuf::reserveSlow(std::size_t minHeadroom, std::size_t minTailroom) { ...@@ -940,6 +948,8 @@ void IOBuf::reserveSlow(std::size_t minHeadroom, std::size_t minTailroom) {
if (xallocx(p, newAllocatedCapacity, 0, 0) == newAllocatedCapacity) { if (xallocx(p, newAllocatedCapacity, 0, 0) == newAllocatedCapacity) {
newBuffer = static_cast<uint8_t*>(p); newBuffer = static_cast<uint8_t*>(p);
newHeadroom = oldHeadroom; newHeadroom = oldHeadroom;
// update the userData
info->userData = reinterpret_cast<void*>(newAllocatedCapacity);
} }
// if xallocx failed, do nothing, fall back to malloc/memcpy/free // if xallocx failed, do nothing, fall back to malloc/memcpy/free
} }
...@@ -1004,10 +1014,15 @@ void IOBuf::freeExtBuffer() noexcept { ...@@ -1004,10 +1014,15 @@ void IOBuf::freeExtBuffer() noexcept {
if (info->freeFn) { if (info->freeFn) {
info->freeFn(buf_, info->userData); info->freeFn(buf_, info->userData);
} else {
// this will invoke free if info->userData is 0
size_t size = reinterpret_cast<size_t>(info->userData);
if (size) {
folly::sizedFree(buf_, size);
} else { } else {
free(buf_); free(buf_);
} }
}
SharedInfo::invokeAndDeleteEachObserver( SharedInfo::invokeAndDeleteEachObserver(
observerListHead, [](auto& entry) { entry.afterFreeExtBuffer(); }); observerListHead, [](auto& entry) { entry.afterFreeExtBuffer(); });
} }
...@@ -1020,6 +1035,11 @@ void IOBuf::allocExtBuffer( ...@@ -1020,6 +1035,11 @@ void IOBuf::allocExtBuffer(
size_t mallocSize = goodExtBufferSize(minCapacity); size_t mallocSize = goodExtBufferSize(minCapacity);
auto buf = static_cast<uint8_t*>(checkedMalloc(mallocSize)); auto buf = static_cast<uint8_t*>(checkedMalloc(mallocSize));
initExtBuffer(buf, mallocSize, infoReturn, capacityReturn); initExtBuffer(buf, mallocSize, infoReturn, capacityReturn);
// the userData and the freeFn are nullptr here
// just store the mallocSize in userData
(*infoReturn)->userData = reinterpret_cast<void*>(mallocSize);
*bufReturn = buf; *bufReturn = buf;
} }
......
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