iceoryx_posh  2.0.2
memory_provider.hpp
1 // Copyright (c) 2020 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_ROUDI_MEMORY_MEMORY_PROVIDER_HPP
18 #define IOX_POSH_ROUDI_MEMORY_MEMORY_PROVIDER_HPP
19 
20 #include "iceoryx_posh/iceoryx_posh_types.hpp"
21 
22 #include "iceoryx_hoofs/cxx/expected.hpp"
23 #include "iceoryx_hoofs/cxx/helplets.hpp"
24 #include "iceoryx_hoofs/cxx/optional.hpp"
25 #include "iceoryx_hoofs/cxx/vector.hpp"
26 
27 #include <cstdint>
28 
29 namespace iox
30 {
31 namespace roudi
32 {
33 class MemoryBlock;
34 
36 {
61 };
62 
69 {
70  friend class RouDiMemoryManager;
71 
72  public:
73  MemoryProvider() noexcept = default;
74  virtual ~MemoryProvider() noexcept;
75 
78  MemoryProvider(const MemoryProvider&) = delete;
79  MemoryProvider(MemoryProvider&&) = delete;
80  MemoryProvider& operator=(const MemoryProvider&) = delete;
81  MemoryProvider& operator=(MemoryProvider&&) = delete;
82 
87  cxx::expected<MemoryProviderError> addMemoryBlock(cxx::not_null<MemoryBlock*> memoryBlock) noexcept;
88 
92  cxx::expected<MemoryProviderError> create() noexcept;
93 
96  void announceMemoryAvailable() noexcept;
97 
102  cxx::expected<MemoryProviderError> destroy() noexcept;
103 
107  cxx::optional<void*> baseAddress() const noexcept;
108 
111  uint64_t size() const noexcept;
112 
116  cxx::optional<uint64_t> segmentId() const noexcept;
117 
120  bool isAvailable() const noexcept;
121 
124  bool isAvailableAnnounced() const noexcept;
125 
126  protected:
134  virtual cxx::expected<void*, MemoryProviderError> createMemory(const uint64_t size,
135  const uint64_t alignment) noexcept = 0;
136 
140  virtual cxx::expected<MemoryProviderError> destroyMemory() noexcept = 0;
141 
142  static const char* getErrorString(const MemoryProviderError error) noexcept;
143 
144  private:
145  void* m_memory{nullptr};
146  uint64_t m_size{0};
147  uint64_t m_segmentId{0};
148  bool m_memoryAvailableAnnounced{false};
149  cxx::vector<MemoryBlock*, MAX_NUMBER_OF_MEMORY_BLOCKS_PER_MEMORY_PROVIDER> m_memoryBlocks;
150 };
151 } // namespace roudi
152 } // namespace iox
153 
154 #endif // IOX_POSH_ROUDI_MEMORY_MEMORY_PROVIDER_HPP
This class creates memory which is requested by the MemoryBlocks. Once the memory is available,...
Definition: memory_provider.hpp:69
virtual cxx::expected< MemoryProviderError > destroyMemory() noexcept=0
This function needs to be implemented to free the actual memory, e.g. in case of POSIX SHM,...
bool isAvailableAnnounced() const noexcept
This function can be used to check if the availability of the memory was announced to the MemoryBlock...
cxx::expected< MemoryProviderError > create() noexcept
With this call the memory requested by the MemoryBlocks need to be created. The function should be ca...
uint64_t size() const noexcept
This function provides the size of the created memory.
void announceMemoryAvailable() noexcept
This function announces the availability of the memory to the MemoryBlocks. The function should be ca...
bool isAvailable() const noexcept
This function can be used to check if the requested memory is already available.
cxx::expected< MemoryProviderError > addMemoryBlock(cxx::not_null< MemoryBlock * > memoryBlock) noexcept
This function add a MemoryBlock to the list of memory requester.
MemoryProvider(const MemoryProvider &)=delete
cxx::expected< MemoryProviderError > destroy() noexcept
This function destroys the previously allocated memory. Before the destruction, all MemoryBlocks are ...
virtual cxx::expected< void *, MemoryProviderError > createMemory(const uint64_t size, const uint64_t alignment) noexcept=0
This function needs to be implemented to provide the actual memory, e.g. in case of POSIX SHM,...
cxx::optional< void * > baseAddress() const noexcept
This function provides the base address of the created memory.
cxx::optional< uint64_t > segmentId() const noexcept
This function provides the segment id of the relocatable memory segment which is owned by the MemoryP...
Definition: roudi_memory_manager.hpp:53
MemoryProviderError
Definition: memory_provider.hpp:36
@ MEMORY_DEALLOCATION_FAILED
memory destruction failed at deallocating memory
@ MEMORY_DESTRUCTION_FAILED
generic error if memory destruction failed
@ MEMORY_ALIGNMENT_EXCEEDS_PAGE_SIZE
attempt to create memory with an alignment bigger than the page size
@ MEMORY_CREATION_FAILED
generic error if memory creation failed
@ MEMORY_ALREADY_CREATED
attempt to create memory although it already was created
@ MEMORY_BLOCKS_EXHAUSTED
attempt to add more memory blocks than the capacity allows
@ MEMORY_UNMAPPING_FAILED
memory destruction failed at unmapping memory
@ NO_MEMORY_BLOCKS_PRESENT
an action was performed which requires memory blocks
@ MEMORY_ALLOCATION_FAILED
memory creation failed at allocating memory
@ MEMORY_NOT_AVAILABLE
an action was performed which requires memory
@ MEMORY_MAPPING_FAILED
memory creation failed at mapping memory
@ SIGACTION_CALL_FAILED
Setup or teardown of SIGBUS failed.