shm: Throw RefCountBadAlloc if insufficient space in the ref count segment

This commit is contained in:
Alexey Rybalchenko 2023-11-15 10:10:42 +01:00
parent 961eca5276
commit f006fd8b78
2 changed files with 14 additions and 1 deletions

View File

@ -76,6 +76,11 @@ struct MessageBadAlloc : std::runtime_error
using std::runtime_error::runtime_error;
};
struct RefCountBadAlloc : std::runtime_error
{
using std::runtime_error::runtime_error;
};
} // namespace fair::mq
using fairmq_free_fn [[deprecated("Use fair::mq::FreeFn")]] = fair::mq::FreeFn;

View File

@ -279,7 +279,15 @@ class Message final : public fair::mq::Message
}
if (otherMsg.fShared < 0) {
// UR msg not yet shared, create the reference counting object with count 2
otherMsg.fShared = fRegionPtr->HandleFromAddress(&(fRegionPtr->MakeRefCount(2)));
try {
otherMsg.fShared = fRegionPtr->HandleFromAddress(&(fRegionPtr->MakeRefCount(2)));
} catch (boost::interprocess::bad_alloc& ba) {
throw RefCountBadAlloc(tools::ToString(
"Insufficient space in the reference count segment ",
otherMsg.fRegionId,
", original exception: bad_alloc: ",
ba.what()));
}
} else {
fRegionPtr->GetRefCountAddressFromHandle(otherMsg.fShared)->Increment();
}