18 #include <hicn/transport/core/packet.h>
19 #include <hicn/transport/utils/fixed_block_allocator.h>
20 #include <hicn/transport/utils/singleton.h>
29 template <std::
size_t packet_pool_size = 1024, std::
size_t chunk_size = 2048>
36 using RawBuffer = std::pair<uint8_t *, std::size_t>;
39 std::array<uint8_t, 256> packet_and_shared_ptr;
40 std::max_align_t align;
43 utils::MemBuf::Ptr getMemBuf() {
46 memory =
reinterpret_cast<utils::MemBuf *
>(memory_pool_.allocateBlock());
51 auto ret = std::allocate_shared<utils::MemBuf>(
52 allocator, utils::MemBuf::WRAP_BUFFER, (uint8_t *)memory + offset, 0,
59 utils::MemBuf::Ptr getMemBuf(uint8_t *buffer, std::size_t length) {
60 auto offset = offsetof(PacketStorage, align);
61 auto memory = buffer - offset;
64 auto ret = std::allocate_shared<utils::MemBuf>(
65 allocator, utils::MemBuf::WRAP_BUFFER, (uint8_t *)buffer, length,
72 typename PacketType,
typename... Args,
73 typename = std::enable_if_t<std::is_base_of<Packet, PacketType>::value>>
74 typename PacketType::Ptr getPacket(Args &&... args) {
75 PacketType *memory =
nullptr;
77 memory =
reinterpret_cast<PacketType *
>(memory_pool_.allocateBlock());
80 auto offset = offsetof(PacketStorage, align);
81 auto ret = std::allocate_shared<PacketType>(
82 allocator, PacketType::CREATE, (uint8_t *)memory + offset, 0,
83 chunk_size - offset, std::forward<Args>(args)...);
88 std::pair<uint8_t *, std::size_t> getRawBuffer() {
89 uint8_t *memory =
nullptr;
90 memory =
reinterpret_cast<uint8_t *
>(memory_pool_.allocateBlock());
92 auto offset = offsetof(PacketStorage, align);
95 return std::make_pair(memory, chunk_size - offset);
98 template <
typename PacketType,
typename... Args>
99 typename PacketType::Ptr getPacketFromExistingBuffer(uint8_t *buffer,
102 auto offset = offsetof(PacketStorage, align);
103 auto memory =
reinterpret_cast<PacketType *
>(buffer - offset);
106 auto ret = std::allocate_shared<PacketType>(
107 allocator, PacketType::WRAP_BUFFER, (uint8_t *)buffer, length,
108 chunk_size - offset, std::forward<Args>(args)...);
114 PacketManager(std::size_t size = packet_pool_size)
115 : memory_pool_(MemoryPool::getInstance()), size_(0) {}
116 MemoryPool &memory_pool_;
117 std::atomic<size_t> size_;