Class MemoryProvider

Inheritance Relationships

Derived Type

Class Documentation

class MemoryProvider

This class creates memory which is requested by the MemoryBlocks. Once the memory is available, this is announced to the blocks, so that they can consume the memory for their needs. When the Memory is release, the blocks will also called to handle this appropriately, e.g. calling the destructor of the underlying type. This class is an interface with some default behavior and needs an implementation for real memory supply, e.g. a PosixShmMemoryProvider.

Subclassed by iox::roudi::PosixShmMemoryProvider

Public Functions

MemoryProvider() noexcept = default
virtual ~MemoryProvider() noexcept
MemoryProvider(const MemoryProvider&) = delete

Note

this is intentional not movable/copyable, since a pointer to the memory provider is registered at the RouDiMemoryManager and therefore an instance of a MemoryProvider must be pinned to memory

MemoryProvider(MemoryProvider&&) = delete
MemoryProvider &operator=(const MemoryProvider&) = delete
MemoryProvider &operator=(MemoryProvider&&) = delete
cxx::expected<MemoryProviderError> addMemoryBlock(cxx::not_null<MemoryBlock*> memoryBlock) noexcept

This function add a MemoryBlock to the list of memory requester.

Parameters:

memoryBlock[in] is a pointer to a user defined MemoryBlock

Returns:

an MemoryProviderError::MEMORY_BLOCKS_EXHAUSTED error if no further memory blocks can be added, otherwise success

cxx::expected<MemoryProviderError> create() noexcept

With this call the memory requested by the MemoryBlocks need to be created. The function should be called from a MemoryManager which handles one or more MemoryProvider.

Returns:

an MemoryProviderError if memory allocation was not successful, otherwise success

void announceMemoryAvailable() noexcept

This function announces the availability of the memory to the MemoryBlocks. The function should be called from a MemoryManager which handles one or more MemoryProvider.

cxx::expected<MemoryProviderError> destroy() noexcept

This function destroys the previously allocated memory. Before the destruction, all MemoryBlocks are requested to handle this appropriately, e.g. call the destructor of the underlying type. The function should be called from a MemoryManager which handles one or more MemoryProvider.

Returns:

an error if memory destruction was not successful, otherwise success

cxx::optional<void*> baseAddress() const noexcept

This function provides the base address of the created memory.

Returns:

an optional pointer to the base address of the created memory if the memory is available, otherwise a cxx::nullopt_t

uint64_t size() const noexcept

This function provides the size of the created memory.

Returns:

the size 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 MemoryProvider.

Returns:

an optional segment id for the created memory if the memory is available, otherwise cxx::nullopt_t

bool isAvailable() const noexcept

This function can be used to check if the requested memory is already available.

Returns:

true if the requested memory is available, false otherwise

bool isAvailableAnnounced() const noexcept

This function can be used to check if the availability of the memory was announced to the MemoryBlocks.

Returns:

true if the availability of the memory was announced to the MemoryBlocks, false otherwise

Protected Functions

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, shm_open and mmap would need to be called in the implementation of this function.

Parameters:
  • size[in] is the size in bytes for the requested memory, the size should already be calculated according to the alignment requirements

  • alignment[in] the required alignment for the memory

Returns:

the pointer of the begin of the created memory or a MemoryProviderError if the memory could not be created

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, shm_unlink and munmap would need to be called in the implementation of this function.

Returns:

a MemoryProviderError if the destruction failed, otherwise success

Protected Static Functions

static const char *getErrorString(const MemoryProviderError error) noexcept