10 #include <boost/assert.hpp> 14 std::size_t entries_count)
17 pool_.resize(entries_count);
18 for (
auto &entry :
pool_) {
19 entry.data.resize(entry_size_bytes,
'\0');
22 for (
size_t i = 0; i < entries_count; ++i) {
29 bool message_exceeds_max_size = message.length() >
entry_size_;
30 if (message_exceeds_max_size) {
37 if (not has_free_slot) {
41 auto slot = has_free_slot.value();
42 auto &entry =
pool_[slot];
43 entry.ref_count = ref_count;
44 entry.data_size = message.length();
45 memcpy(entry.data.data(), message.c_str(), message.length());
53 bool handle_is_valid =
55 if (not handle_is_valid) {
58 std::lock_guard lock(
mutex_);
61 auto &entry =
pool_[handle];
62 return ++entry.ref_count;
66 bool handle_is_valid =
68 if (not handle_is_valid) {
71 std::lock_guard lock(
mutex_);
72 auto &entry =
pool_[handle];
73 if (entry.ref_count > 0 and --entry.ref_count == 0) {
74 memset(entry.data.data(),
'\0', entry.data.size());
78 return entry.ref_count;
83 bool handle_is_valid =
85 if (not handle_is_valid) {
86 throw std::runtime_error(
"Bad access through invalid handle");
94 auto &entry =
pool_[handle];
95 return boost::asio::buffer(const_cast<uint8_t *>(entry.data.data()),
104 std::lock_guard lock(
mutex_);
109 auto slot = *free_slot;
std::unordered_set< std::size_t > free_slots_
RefCount release(MessageHandle handle)
std::size_t capacity() const
std::optional< MessageHandle > push(const std::string &message, int16_t ref_count)
const std::size_t entry_size_
std::optional< MessageHandle > nextFreeSlot()
performs quick lookup for a free slot
const std::size_t entries_count_
boost::asio::mutable_buffer operator[](MessageHandle handle) const
std::vector< Record > pool_
std::size_t MessageHandle
RefCount add_ref(MessageHandle handle)
MessagePool(std::size_t entry_size_bytes, std::size_t entries_count)