Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
global_object_pool.h
1 /*
2  * Copyright (c) 2021 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <hicn/transport/core/packet.h>
19 #include <hicn/transport/utils/fixed_block_allocator.h>
20 #include <hicn/transport/utils/singleton.h>
21 
22 #include <array>
23 #include <mutex>
24 
25 namespace transport {
26 
27 namespace core {
28 
29 template <std::size_t packet_pool_size = 1024, std::size_t chunk_size = 2048>
31  : public utils::Singleton<PacketManager<packet_pool_size, chunk_size>> {
32  friend class utils::Singleton<PacketManager<packet_pool_size, chunk_size>>;
33 
34  public:
36  using RawBuffer = std::pair<uint8_t *, std::size_t>;
37 
38  struct PacketStorage {
39  std::array<uint8_t, 256> packet_and_shared_ptr;
40  std::max_align_t align;
41  };
42 
43  utils::MemBuf::Ptr getMemBuf() {
44  utils::MemBuf *memory = nullptr;
45 
46  memory = reinterpret_cast<utils::MemBuf *>(memory_pool_.allocateBlock());
47 
49  &memory_pool_);
50  auto offset = offsetof(PacketStorage, align);
51  auto ret = std::allocate_shared<utils::MemBuf>(
52  allocator, utils::MemBuf::WRAP_BUFFER, (uint8_t *)memory + offset, 0,
53  chunk_size - offset);
54  ret->clear();
55 
56  return ret;
57  }
58 
59  utils::MemBuf::Ptr getMemBuf(uint8_t *buffer, std::size_t length) {
60  auto offset = offsetof(PacketStorage, align);
61  auto memory = buffer - offset;
63  (utils::MemBuf *)memory, &memory_pool_);
64  auto ret = std::allocate_shared<utils::MemBuf>(
65  allocator, utils::MemBuf::WRAP_BUFFER, (uint8_t *)buffer, length,
66  chunk_size - offset);
67 
68  return ret;
69  }
70 
71  template <
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;
76 
77  memory = reinterpret_cast<PacketType *>(memory_pool_.allocateBlock());
79  &memory_pool_);
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)...);
84 
85  return ret;
86  }
87 
88  std::pair<uint8_t *, std::size_t> getRawBuffer() {
89  uint8_t *memory = nullptr;
90  memory = reinterpret_cast<uint8_t *>(memory_pool_.allocateBlock());
91 
92  auto offset = offsetof(PacketStorage, align);
93  memory += offset;
94 
95  return std::make_pair(memory, chunk_size - offset);
96  }
97 
98  template <typename PacketType, typename... Args>
99  typename PacketType::Ptr getPacketFromExistingBuffer(uint8_t *buffer,
100  std::size_t length,
101  Args &&... args) {
102  auto offset = offsetof(PacketStorage, align);
103  auto memory = reinterpret_cast<PacketType *>(buffer - offset);
105  &memory_pool_);
106  auto ret = std::allocate_shared<PacketType>(
107  allocator, PacketType::WRAP_BUFFER, (uint8_t *)buffer, length,
108  chunk_size - offset, std::forward<Args>(args)...);
109 
110  return ret;
111  }
112 
113  private:
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_;
118 };
119 
120 } // end namespace core
121 
122 } // end namespace transport
utils::STLAllocator
Definition: fixed_block_allocator.h:134
utils::FixedBlockAllocator
Definition: fixed_block_allocator.h:18
transport::core::PacketManager
Definition: global_object_pool.h:30
transport
Definition: forwarder_config.h:32
utils::Singleton
Definition: singleton.h:23
transport::core::PacketManager::PacketStorage
Definition: global_object_pool.h:38
utils::MemBuf
Definition: membuf.h:45