Skip to content

iceoryx_posh/roudi/memory/memory_provider.hpp🔗

Namespaces🔗

Name
iox
iox::roudi

Classes🔗

Name
class iox::roudi::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.

Source code🔗

// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_POSH_ROUDI_MEMORY_MEMORY_PROVIDER_HPP
#define IOX_POSH_ROUDI_MEMORY_MEMORY_PROVIDER_HPP

#include "iceoryx_posh/iceoryx_posh_types.hpp"

#include "iceoryx_hoofs/cxx/expected.hpp"
#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/cxx/vector.hpp"

#include <cstdint>

namespace iox
{
namespace roudi
{
class MemoryBlock;

enum class MemoryProviderError
{
    MEMORY_BLOCKS_EXHAUSTED,
    NO_MEMORY_BLOCKS_PRESENT,
    MEMORY_ALREADY_CREATED,
    MEMORY_CREATION_FAILED,
    MEMORY_ALIGNMENT_EXCEEDS_PAGE_SIZE,
    MEMORY_ALLOCATION_FAILED,
    MEMORY_MAPPING_FAILED,
    MEMORY_NOT_AVAILABLE,
    MEMORY_DESTRUCTION_FAILED,
    MEMORY_DEALLOCATION_FAILED,
    MEMORY_UNMAPPING_FAILED,
    SIGACTION_CALL_FAILED,
};

class MemoryProvider
{
    friend class RouDiMemoryManager;

  public:
    MemoryProvider() noexcept = default;
    virtual ~MemoryProvider() noexcept;

    MemoryProvider(const MemoryProvider&) = delete;
    MemoryProvider(MemoryProvider&&) = delete;
    MemoryProvider& operator=(const MemoryProvider&) = delete;
    MemoryProvider& operator=(MemoryProvider&&) = delete;

    cxx::expected<MemoryProviderError> addMemoryBlock(cxx::not_null<MemoryBlock*> memoryBlock) noexcept;

    cxx::expected<MemoryProviderError> create() noexcept;

    void announceMemoryAvailable() noexcept;

    cxx::expected<MemoryProviderError> destroy() noexcept;

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

    uint64_t size() const noexcept;

    cxx::optional<uint64_t> segmentId() const noexcept;

    bool isAvailable() const noexcept;

    bool isAvailableAnnounced() const noexcept;

  protected:
    virtual cxx::expected<void*, MemoryProviderError> createMemory(const uint64_t size,
                                                                   const uint64_t alignment) noexcept = 0;

    virtual cxx::expected<MemoryProviderError> destroyMemory() noexcept = 0;

    static const char* getErrorString(const MemoryProviderError error) noexcept;

  private:
    void* m_memory{nullptr};
    uint64_t m_size{0};
    uint64_t m_segmentId{0};
    bool m_memoryAvailableAnnounced{false};
    cxx::vector<MemoryBlock*, MAX_NUMBER_OF_MEMORY_BLOCKS_PER_MEMORY_PROVIDER> m_memoryBlocks;
};
} // namespace roudi
} // namespace iox

#endif // IOX_POSH_ROUDI_MEMORY_MEMORY_PROVIDER_HPP

Updated on 18 December 2023 at 13:11:43 CET