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

Mark some more IOBuf members as noexcept

Summary:
[Folly] Mark some more `IOBuf` members as `noexcept` and remove an explicit `abort` which is there to simulate `noexcept`.

(Note: this ignores all push blocking failures!)

Differential Revision: D15442545

fbshipit-source-id: adaef4d1f6c2e551e9c19b151e3f08c6ba11f717
parent 2cb9738a
......@@ -62,11 +62,14 @@ enum : std::size_t {
};
// Helper function for IOBuf::takeOwnership()
// The user's free function is not allowed to throw.
// (We are already in the middle of throwing an exception, so
// we cannot let this exception go unhandled.)
void takeOwnershipError(
bool freeOnError,
void* buf,
folly::IOBuf::FreeFunction freeFn,
void* userData) {
void* userData) noexcept {
if (!freeOnError) {
return;
}
......@@ -74,14 +77,7 @@ void takeOwnershipError(
free(buf);
return;
}
try {
freeFn(buf, userData);
} catch (...) {
// The user's free function is not allowed to throw.
// (We are already in the middle of throwing an exception, so
// we cannot let this exception go unhandled.)
abort();
}
freeFn(buf, userData);
}
} // namespace
......@@ -133,7 +129,7 @@ IOBuf::SharedInfo::SharedInfo(FreeFunction fn, void* arg, bool hfs)
refcount.store(1, std::memory_order_relaxed);
}
void IOBuf::SharedInfo::releaseStorage(SharedInfo* info) {
void IOBuf::SharedInfo::releaseStorage(SharedInfo* info) noexcept {
if (info->useHeapFullStorage) {
auto* storageAddr =
reinterpret_cast<uint8_t*>(info) - offsetof(HeapFullStorage, shared);
......@@ -167,7 +163,7 @@ void IOBuf::operator delete(void* /* ptr */, void* /* placement */) {
// constructor.
}
void IOBuf::releaseStorage(HeapStorage* storage, uint16_t freeFlags) {
void IOBuf::releaseStorage(HeapStorage* storage, uint16_t freeFlags) noexcept {
CHECK_EQ(storage->prefix.magic, static_cast<uint16_t>(kHeapMagic));
// Use relaxed memory order here. If we are unlucky and happen to get
......@@ -200,7 +196,7 @@ void IOBuf::releaseStorage(HeapStorage* storage, uint16_t freeFlags) {
}
}
void IOBuf::freeInternalBuf(void* /* buf */, void* userData) {
void IOBuf::freeInternalBuf(void* /* buf */, void* userData) noexcept {
auto* storage = static_cast<HeapStorage*>(userData);
releaseStorage(storage, kDataInUse);
}
......@@ -791,7 +787,7 @@ void IOBuf::coalesceAndReallocate(
}
}
void IOBuf::decrementRefcount() {
void IOBuf::decrementRefcount() noexcept {
// Externally owned buffers don't have a SharedInfo object and aren't managed
// by the reference count
SharedInfo* info = sharedInfo();
......@@ -945,19 +941,15 @@ void IOBuf::reserveSlow(std::size_t minHeadroom, std::size_t minTailroom) {
// length_ is unchanged
}
void IOBuf::freeExtBuffer() {
// The user's free function should never throw. Otherwise we might throw from
// the IOBuf destructor. Other code paths like coalesce() also assume that
// decrementRefcount() cannot throw.
void IOBuf::freeExtBuffer() noexcept {
SharedInfo* info = sharedInfo();
DCHECK(info);
if (info->freeFn) {
try {
info->freeFn(buf_, info->userData);
} catch (...) {
// The user's free function should never throw. Otherwise we might
// throw from the IOBuf destructor. Other code paths like coalesce()
// also assume that decrementRefcount() cannot throw.
abort();
}
info->freeFn(buf_, info->userData);
} else {
free(buf_);
}
......
......@@ -917,7 +917,7 @@ class IOBuf {
* is not nullptr, nullptr otherwise
*
**/
void* getUserData() const {
void* getUserData() const noexcept {
SharedInfo* info = sharedInfo();
return info ? info->userData : nullptr;
}
......@@ -927,7 +927,7 @@ class IOBuf {
* Returns free function if sharedInfo() is not nullputr, nullptr otherwise
*
**/
FreeFunction getFreeFn() const {
FreeFunction getFreeFn() const noexcept {
SharedInfo* info = sharedInfo();
return info ? info->freeFn : nullptr;
}
......@@ -955,7 +955,7 @@ class IOBuf {
* (and so the lifetime of the underlying memory can be extended by
* cloneOne()).
*/
bool isManagedOne() const {
bool isManagedOne() const noexcept {
return sharedInfo();
}
......@@ -984,7 +984,7 @@ class IOBuf {
*
* This only checks the current IOBuf, and not other IOBufs in the chain.
*/
bool isSharedOne() const {
bool isSharedOne() const noexcept {
// If this is a user-owned buffer, it is always considered shared
if (UNLIKELY(!sharedInfo())) {
return true;
......@@ -1391,7 +1391,7 @@ class IOBuf {
SharedInfo();
SharedInfo(FreeFunction fn, void* arg, bool hfs = false);
static void releaseStorage(SharedInfo* info);
static void releaseStorage(SharedInfo* info) noexcept;
// A pointer to a function to call to free the buffer when the refcount
// hits 0. If this is null, free() will be used instead.
......@@ -1437,9 +1437,9 @@ class IOBuf {
void coalesceAndReallocate(size_t newLength, IOBuf* end) {
coalesceAndReallocate(headroom(), newLength, end, end->prev_->tailroom());
}
void decrementRefcount();
void decrementRefcount() noexcept;
void reserveSlow(std::size_t minHeadroom, std::size_t minTailroom);
void freeExtBuffer();
void freeExtBuffer() noexcept;
static size_t goodExtBufferSize(std::size_t minCapacity);
static void initExtBuffer(
......@@ -1452,8 +1452,8 @@ class IOBuf {
uint8_t** bufReturn,
SharedInfo** infoReturn,
std::size_t* capacityReturn);
static void releaseStorage(HeapStorage* storage, uint16_t freeFlags);
static void freeInternalBuf(void* buf, void* userData);
static void releaseStorage(HeapStorage* storage, uint16_t freeFlags) noexcept;
static void freeInternalBuf(void* buf, void* userData) noexcept;
/*
* Member variables
......@@ -1485,39 +1485,41 @@ class IOBuf {
static inline uintptr_t packFlagsAndSharedInfo(
uintptr_t flags,
SharedInfo* info) {
SharedInfo* info) noexcept {
uintptr_t uinfo = reinterpret_cast<uintptr_t>(info);
DCHECK_EQ(flags & ~kFlagMask, 0u);
DCHECK_EQ(uinfo & kFlagMask, 0u);
return flags | uinfo;
}
inline SharedInfo* sharedInfo() const {
inline SharedInfo* sharedInfo() const noexcept {
return reinterpret_cast<SharedInfo*>(flagsAndSharedInfo_ & ~kFlagMask);
}
inline void setSharedInfo(SharedInfo* info) {
inline void setSharedInfo(SharedInfo* info) noexcept {
uintptr_t uinfo = reinterpret_cast<uintptr_t>(info);
DCHECK_EQ(uinfo & kFlagMask, 0u);
flagsAndSharedInfo_ = (flagsAndSharedInfo_ & kFlagMask) | uinfo;
}
inline uintptr_t flags() const {
inline uintptr_t flags() const noexcept {
return flagsAndSharedInfo_ & kFlagMask;
}
// flags_ are changed from const methods
inline void setFlags(uintptr_t flags) const {
inline void setFlags(uintptr_t flags) const noexcept {
DCHECK_EQ(flags & ~kFlagMask, 0u);
flagsAndSharedInfo_ |= flags;
}
inline void clearFlags(uintptr_t flags) const {
inline void clearFlags(uintptr_t flags) const noexcept {
DCHECK_EQ(flags & ~kFlagMask, 0u);
flagsAndSharedInfo_ &= ~flags;
}
inline void setFlagsAndSharedInfo(uintptr_t flags, SharedInfo* info) {
inline void setFlagsAndSharedInfo(
uintptr_t flags,
SharedInfo* info) noexcept {
flagsAndSharedInfo_ = packFlagsAndSharedInfo(flags, info);
}
......@@ -1541,7 +1543,7 @@ class IOBuf {
Deleter deleter_;
};
static void freeUniquePtrBuffer(void* ptr, void* userData) {
static void freeUniquePtrBuffer(void* ptr, void* userData) noexcept {
static_cast<DeleterBase*>(userData)->dispose(ptr);
}
};
......
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