aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/includes/hicn/transport/core/global_object_pool.h
blob: e0b6e373fa6bc318b95176b8a2cd12521fafe500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <hicn/transport/core/packet.h>
#include <hicn/transport/utils/fixed_block_allocator.h>
#include <hicn/transport/utils/singleton.h>

#include <array>
#include <mutex>

namespace transport {

namespace core {

template <std::size_t packet_pool_size = 1024, std::size_t chunk_size = 2048>
class PacketManager
    : public utils::Singleton<PacketManager<packet_pool_size, chunk_size>> {
  friend class utils::Singleton<PacketManager<packet_pool_size, chunk_size>>;

 public:
  using MemoryPool = utils::FixedBlockAllocator<chunk_size, packet_pool_size>;
  using RawBuffer = std::pair<uint8_t *, std::size_t>;

  struct PacketStorage {
    std::array<uint8_t, 256> packet_and_shared_ptr;
    std::max_align_t align;
  };

  utils::MemBuf::Ptr getMemBuf() {
    utils::MemBuf *memory = nullptr;

    memory = reinterpret_cast<utils::MemBuf *>(memory_pool_.allocateBlock());

    utils::STLAllocator<utils::MemBuf, MemoryPool> allocator(memory,
                                                             &memory_pool_);
    auto offset = offsetof(PacketStorage, align);
    auto ret = std::allocate_shared<utils::MemBuf>(
        allocator, utils::MemBuf::WRAP_BUFFER, (uint8_t *)memory + offset, 0,
        chunk_size - offset);
    ret->clear();

    return ret;
  }

  utils::MemBuf::Ptr getMemBuf(uint8_t *buffer, std::size_t length) {
    auto offset = offsetof(PacketStorage, align);
    auto memory = buffer - offset;
    utils::STLAllocator<utils::MemBuf, MemoryPool> allocator(
        (utils::MemBuf *)memory, &memory_pool_);
    auto ret = std::allocate_shared<utils::MemBuf>(
        allocator, utils::MemBuf::WRAP_BUFFER, (uint8_t *)buffer, length,
        chunk_size - offset);

    return ret;
  }

  template <
      typename PacketType, typename... Args,
      typename = std::enable_if_t<std::is_base_of<Packet, PacketType>::value>>
  typename PacketType::Ptr getPacket(Args &&...args) {
    PacketType *memory = nullptr;

    memory = reinterpret_cast<PacketType *>(memory_pool_.allocateBlock());
    utils::STLAllocator<PacketType, MemoryPool> allocator(memory,
                                                          &memory_pool_);
    auto offset = offsetof(PacketStorage, align);
    auto ret = std::allocate_shared<PacketType>(
        allocator, PacketType::CREATE, (uint8_t *)memory + offset, 0,
        chunk_size - offset, std::forward<Args>(args)...);

    return ret;
  }

  std::pair<uint8_t *, std::size_t> getRawBuffer() {
    uint8_t *memory = nullptr;
    memory = reinterpret_cast<uint8_t *>(memory_pool_.allocateBlock());

    auto offset = offsetof(PacketStorage, align);
    memory += offset;

    return std::make_pair(memory, chunk_size - offset);
  }

  template <typename PacketType, typename... Args>
  typename PacketType::Ptr getPacketFromExistingBuffer(uint8_t *buffer,
                                                       std::size_t length,
                                                       Args &&...args) {
    auto offset = offsetof(PacketStorage, align);
    auto memory = reinterpret_cast<PacketType *>(buffer - offset);
    utils::STLAllocator<PacketType, MemoryPool> allocator(memory,
                                                          &memory_pool_);
    auto ret = std::allocate_shared<PacketType>(
        allocator, PacketType::WRAP_BUFFER, (uint8_t *)buffer, length,
        chunk_size - offset, std::forward<Args>(args)...);

    return ret;
  }

 private:
  PacketManager(std::size_t size = packet_pool_size)
      : memory_pool_(MemoryPool::getInstance()), size_(0) {}
  MemoryPool &memory_pool_;
  std::atomic<size_t> size_;
};

}  // end namespace core

}  // end namespace transport