Skip to content

Source · fairmq/

MemoryResources.hpublic header

Defines class fair::mq::ChannelResource, class fair::mq::MemoryResource.

Described in Use std::pmr memory resources.

fairmq/MemoryResources.h110 lines · C++API: ChannelResourceAPI: MemoryResourceGitHub ↗
1/********************************************************************************2 *    Copyright (C) 2018 CERN and copyright holders of ALICE O2                 *3 * Copyright (C) 2018-2022 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH  *4 *                                                                              *5 *              This software is distributed under the terms of the             *6 *              GNU Lesser General Public Licence (LGPL) version 3,             *7 *                  copied verbatim in the file "LICENSE"                       *8 ********************************************************************************/9 10/// @brief Memory allocators and interfaces related to managing memory via the11/// trasport layer12///13/// @author Mikolaj Krzewicki, mkrzewic@cern.ch14 15#ifndef FAIR_MQ_MEMORY_RESOURCES_H16#define FAIR_MQ_MEMORY_RESOURCES_H17 18#include <boost/container/container_fwd.hpp>19#include <boost/container/flat_map.hpp>20#include <memory_resource>21#include <cstring>22#include <fairmq/Message.h>23#include <stdexcept>24#include <utility>25 26namespace fair::mq {27 28class TransportFactory;29using byte = unsigned char;30namespace pmr = std::pmr;31 32/// All FairMQ related memory resources need to inherit from this interface33/// class for the34/// getMessage() api.35class MemoryResource : public pmr::memory_resource36{37  public:38    /// return the message containing data associated with the pointer (to start39    /// of40    /// buffer), e.g. pointer returned by std::vector::data() return nullptr if41    /// returning42    /// a message does not make sense!43    virtual MessagePtr getMessage(void* p) = 0;44    virtual void* setMessage(MessagePtr) = 0;45    virtual TransportFactory* getTransportFactory() noexcept = 0;46    virtual size_t getNumberOfMessages() const noexcept = 0;47};48 49/// This is the allocator that interfaces to FairMQ memory management. All50/// allocations are51/// delegated to FairMQ so standard (e.g. STL) containers can construct their52/// stuff in53/// memory regions appropriate for the data channel configuration.54class ChannelResource : public MemoryResource55{56  protected:57    TransportFactory* factory{nullptr};58    // TODO: for now a map to keep track of allocations, something else would59    // probably be60    // faster, but for now this does not need to be fast.61    boost::container::flat_map<void*, MessagePtr> messageMap;62 63  public:64    ChannelResource() = delete;65 66    ChannelResource(TransportFactory* _factory)67        : factory(_factory)68    {69        if (!_factory) {70            throw std::runtime_error(71                "Tried to construct from a nullptr fair::mq::TransportFactory");72        }73    };74 75    MessagePtr getMessage(void* p) override76    {77        auto mes = std::move(messageMap[p]);78        messageMap.erase(p);79        return mes;80    }81 82    void* setMessage(MessagePtr message) override83    {84        void* addr = message->GetData();85        messageMap[addr] = std::move(message);86        return addr;87    }88 89    TransportFactory* getTransportFactory() noexcept override { return factory; }90 91    size_t getNumberOfMessages() const noexcept override { return messageMap.size(); }92 93  protected:94    void* do_allocate(std::size_t bytes, std::size_t alignment) override;95    void do_deallocate(void* p, std::size_t /*bytes*/, std::size_t /*alignment*/) override96    {97        messageMap.erase(p);98    };99 100    bool do_is_equal(const pmr::memory_resource& other) const noexcept override101    {102        return this == &other;103    };104};105 106using FairMQMemoryResource [[deprecated("Use fair::mq::MemoryResource")]] = MemoryResource;107 108}   // namespace fair::mq109 110#endif /* FAIR_MQ_MEMORY_RESOURCES_H */