Kagome
Polkadot Runtime Engine in C++17
message_pool.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_MESSAGE_POOL_HPP
7 #define KAGOME_MESSAGE_POOL_HPP
8 
9 #include <cstddef>
10 #include <cstdint>
11 #include <optional>
12 #include <string>
13 #include <unordered_set>
14 
15 #include <boost/asio/buffer.hpp>
16 #include "common/spin_lock.hpp"
17 
18 namespace kagome::telemetry {
19 
20  using MessageHandle = std::size_t;
21 
36  class MessagePool {
37  public:
45  using RefCount = int16_t;
51  MessagePool(std::size_t entry_size_bytes, std::size_t entries_count);
52 
62  std::optional<MessageHandle> push(const std::string &message,
63  int16_t ref_count);
64 
72 
80 
87  boost::asio::mutable_buffer operator[](MessageHandle handle) const;
88 
93  std::size_t capacity() const;
94 
95  private:
97  std::optional<MessageHandle> nextFreeSlot();
98 
99  struct Record {
100  std::vector<uint8_t> data;
101  std::size_t data_size = 0;
102  std::size_t ref_count = 0;
103  };
104 
105  const std::size_t entry_size_;
106  const std::size_t entries_count_;
107  std::vector<Record> pool_;
108  std::unordered_set<std::size_t> free_slots_;
110  };
111 
112 } // namespace kagome::telemetry
113 
114 #endif // KAGOME_MESSAGE_POOL_HPP
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)
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)