iceoryx_doc  1.0.1
mem_pool.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_POSH_MEPOO_MEM_POOL_HPP
18 #define IOX_POSH_MEPOO_MEM_POOL_HPP
19 
20 #include "iceoryx_posh/mepoo/chunk_header.hpp"
21 #include "iceoryx_utils/cxx/helplets.hpp"
22 #include "iceoryx_utils/internal/concurrent/loffli.hpp"
23 #include "iceoryx_utils/internal/posix_wrapper/shared_memory_object/allocator.hpp"
24 #include "iceoryx_utils/internal/relocatable_pointer/relative_pointer.hpp"
25 
26 #include <atomic>
27 #include <cstdint>
28 
29 
30 namespace iox
31 {
32 namespace mepoo
33 {
35 {
36  MemPoolInfo(const uint32_t usedChunks,
37  const uint32_t minFreeChunks,
38  const uint32_t numChunks,
39  const uint32_t chunkSize) noexcept;
40 
41  uint32_t m_usedChunks{0};
42  uint32_t m_minFreeChunks{0};
43  uint32_t m_numChunks{0};
44  uint32_t m_chunkSize{0};
45 };
46 
47 class MemPool
48 {
49  public:
50  using freeList_t = concurrent::LoFFLi;
51  static constexpr uint64_t CHUNK_MEMORY_ALIGNMENT = 8U; // default alignment for 64 bit
52 
53  MemPool(const cxx::greater_or_equal<uint32_t, CHUNK_MEMORY_ALIGNMENT> chunkSize,
54  const cxx::greater_or_equal<uint32_t, 1> numberOfChunks,
55  posix::Allocator& managementAllocator,
56  posix::Allocator& chunkMemoryAllocator) noexcept;
57 
58  MemPool(const MemPool&) = delete;
59  MemPool(MemPool&&) = delete;
60  MemPool& operator=(const MemPool&) = delete;
61  MemPool& operator=(MemPool&&) = delete;
62 
63  void* getChunk() noexcept;
64  uint32_t getChunkSize() const noexcept;
65  uint32_t getChunkCount() const noexcept;
66  uint32_t getUsedChunks() const noexcept;
67  uint32_t getMinFree() const noexcept;
68  MemPoolInfo getInfo() const noexcept;
69 
70  void freeChunk(const void* chunk) noexcept;
71 
72  private:
73  void adjustMinFree() noexcept;
74  bool isMultipleOfAlignment(const uint32_t value) const noexcept;
75 
76  rp::RelativePointer<uint8_t> m_rawMemory;
77 
78  uint32_t m_chunkSize{0U};
81  uint32_t m_numberOfChunks{0U};
82 
84  std::atomic<uint32_t> m_usedChunks{0U};
85  std::atomic<uint32_t> m_minFree{0U};
87 
88  freeList_t m_freeIndices;
89 };
90 
91 } // namespace mepoo
92 } // namespace iox
93 
94 #endif // IOX_POSH_MEPOO_MEM_POOL_HPP
Definition: mem_pool.hpp:48
Definition: service_description.hpp:29
Definition: mem_pool.hpp:35