Commit f59c30e7 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Remove explicit throw/try/catch from IOBuf

Summary:
[Folly] Remove explicit `throw`, `try`, and `catch` uses from `IOBuf`, preferring scope guards and `throw_exception`.

(Note: this ignores all push blocking failures!)

Differential Revision: D15442544

fbshipit-source-id: 2f182256ff12f5aad6166a7b40ed018c1b32e923
parent 7c86f5f6
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <folly/hash/SpookyHashV2.h> #include <folly/hash/SpookyHashV2.h>
#include <folly/io/Cursor.h> #include <folly/io/Cursor.h>
#include <folly/lang/Align.h> #include <folly/lang/Align.h>
#include <folly/lang/Exception.h>
#include <folly/memory/Malloc.h> #include <folly/memory/Malloc.h>
using std::unique_ptr; using std::unique_ptr;
...@@ -311,12 +312,11 @@ IOBuf::IOBuf( ...@@ -311,12 +312,11 @@ IOBuf::IOBuf(
capacity_(capacity), capacity_(capacity),
flagsAndSharedInfo_( flagsAndSharedInfo_(
packFlagsAndSharedInfo(kFlagFreeSharedInfo, nullptr)) { packFlagsAndSharedInfo(kFlagFreeSharedInfo, nullptr)) {
try { auto rollback = makeGuard([&] { //
setSharedInfo(new SharedInfo(freeFn, userData));
} catch (...) {
takeOwnershipError(freeOnError, buf, freeFn, userData); takeOwnershipError(freeOnError, buf, freeFn, userData);
throw; });
} setSharedInfo(new SharedInfo(freeFn, userData));
rollback.dismiss();
} }
unique_ptr<IOBuf> IOBuf::takeOwnership( unique_ptr<IOBuf> IOBuf::takeOwnership(
...@@ -327,7 +327,13 @@ unique_ptr<IOBuf> IOBuf::takeOwnership( ...@@ -327,7 +327,13 @@ unique_ptr<IOBuf> IOBuf::takeOwnership(
void* userData, void* userData,
bool freeOnError) { bool freeOnError) {
HeapFullStorage* storage = nullptr; HeapFullStorage* storage = nullptr;
try { auto rollback = makeGuard([&] {
if (storage) {
free(storage);
}
takeOwnershipError(freeOnError, buf, freeFn, userData);
});
size_t requiredStorage = sizeof(HeapFullStorage); size_t requiredStorage = sizeof(HeapFullStorage);
size_t mallocSize = goodMallocSize(requiredStorage); size_t mallocSize = goodMallocSize(requiredStorage);
storage = static_cast<HeapFullStorage*>(checkedMalloc(mallocSize)); storage = static_cast<HeapFullStorage*>(checkedMalloc(mallocSize));
...@@ -336,20 +342,17 @@ unique_ptr<IOBuf> IOBuf::takeOwnership( ...@@ -336,20 +342,17 @@ unique_ptr<IOBuf> IOBuf::takeOwnership(
new (&storage->shared) new (&storage->shared)
SharedInfo(freeFn, userData, true /*useHeapFullStorage*/); SharedInfo(freeFn, userData, true /*useHeapFullStorage*/);
return unique_ptr<IOBuf>(new (&storage->hs.buf) IOBuf( auto result = unique_ptr<IOBuf>(new (&storage->hs.buf) IOBuf(
InternalConstructor(), InternalConstructor(),
packFlagsAndSharedInfo(0, &storage->shared), packFlagsAndSharedInfo(0, &storage->shared),
static_cast<uint8_t*>(buf), static_cast<uint8_t*>(buf),
capacity, capacity,
static_cast<uint8_t*>(buf), static_cast<uint8_t*>(buf),
length)); length));
} catch (...) {
if (storage) { rollback.dismiss();
free(storage);
} return result;
takeOwnershipError(freeOnError, buf, freeFn, userData);
throw;
}
} }
IOBuf::IOBuf(WrapBufferOp, const void* buf, std::size_t capacity) noexcept IOBuf::IOBuf(WrapBufferOp, const void* buf, std::size_t capacity) noexcept
...@@ -725,7 +728,7 @@ void IOBuf::coalesceSlow(size_t maxLength) { ...@@ -725,7 +728,7 @@ void IOBuf::coalesceSlow(size_t maxLength) {
break; break;
} }
if (end == this) { if (end == this) {
throw std::overflow_error( throw_exception<std::overflow_error>(
"attempted to coalesce more data than " "attempted to coalesce more data than "
"available"); "available");
} }
...@@ -900,7 +903,7 @@ void IOBuf::reserveSlow(std::size_t minHeadroom, std::size_t minTailroom) { ...@@ -900,7 +903,7 @@ void IOBuf::reserveSlow(std::size_t minHeadroom, std::size_t minTailroom) {
if (copySlack * 2 <= length_) { if (copySlack * 2 <= length_) {
void* p = realloc(buf_, newAllocatedCapacity); void* p = realloc(buf_, newAllocatedCapacity);
if (UNLIKELY(p == nullptr)) { if (UNLIKELY(p == nullptr)) {
throw std::bad_alloc(); throw_exception<std::bad_alloc>();
} }
newBuffer = static_cast<uint8_t*>(p); newBuffer = static_cast<uint8_t*>(p);
newHeadroom = oldHeadroom; newHeadroom = oldHeadroom;
......
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