FairMQ  1.4.33
C++ Message Queuing Library and Framework
MemoryResources.h
1 /********************************************************************************
2  * Copyright (C) 2018 CERN and copyright holders of ALICE O2 *
3  * Copyright (C) 2018 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 
14 
15 #ifndef FAIR_MQ_MEMORY_RESOURCES_H
16 #define FAIR_MQ_MEMORY_RESOURCES_H
17 
18 #include <fairmq/FairMQMessage.h>
20 
21 #include <boost/container/container_fwd.hpp>
22 #include <boost/container/flat_map.hpp>
23 #include <boost/container/pmr/memory_resource.hpp>
24 
25 #include <cstring>
26 #include <stdexcept>
27 #include <utility>
28 
29 namespace fair::mq
30 {
31 
32 using byte = unsigned char;
33 namespace pmr = boost::container::pmr;
34 
38 class FairMQMemoryResource : public pmr::memory_resource
39 {
40  public:
46  virtual FairMQMessagePtr getMessage(void *p) = 0;
47  virtual void *setMessage(FairMQMessagePtr) = 0;
48  virtual FairMQTransportFactory *getTransportFactory() noexcept = 0;
49  virtual size_t getNumberOfMessages() const noexcept = 0;
50 };
51 
58 {
59  protected:
60  FairMQTransportFactory *factory{nullptr};
61  // TODO: for now a map to keep track of allocations, something else would
62  // probably be
63  // faster, but for now this does not need to be fast.
64  boost::container::flat_map<void *, FairMQMessagePtr> messageMap;
65 
66  public:
67  ChannelResource() = delete;
68 
71  , factory(_factory)
72  , messageMap()
73  {
74  if (!_factory) {
75  throw std::runtime_error("Tried to construct from a nullptr FairMQTransportFactory");
76  }
77  };
78 
79  FairMQMessagePtr getMessage(void *p) override
80  {
81  auto mes = std::move(messageMap[p]);
82  messageMap.erase(p);
83  return mes;
84  }
85 
86  void *setMessage(FairMQMessagePtr message) override
87  {
88  void *addr = message->GetData();
89  messageMap[addr] = std::move(message);
90  return addr;
91  }
92 
93  FairMQTransportFactory *getTransportFactory() noexcept override { return factory; }
94 
95  size_t getNumberOfMessages() const noexcept override { return messageMap.size(); }
96 
97  protected:
98  void *do_allocate(std::size_t bytes, std::size_t alignment) override;
99  void do_deallocate(void *p, std::size_t /*bytes*/, std::size_t /*alignment*/) override
100  {
101  messageMap.erase(p);
102  };
103 
104  bool do_is_equal(const pmr::memory_resource &other) const noexcept override
105  {
106  return this == &other;
107  };
108 };
109 
110 } // namespace fair::mq
111 
112 #endif /* FAIR_MQ_MEMORY_RESOURCES_H */
fair::mq::FairMQMemoryResource::getMessage
virtual FairMQMessagePtr getMessage(void *p)=0
fair::mq
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
fair::mq::ChannelResource::getMessage
FairMQMessagePtr getMessage(void *p) override
Definition: MemoryResources.h:79
fair::mq::ChannelResource
Definition: MemoryResources.h:58
fair::mq::FairMQMemoryResource
Definition: MemoryResources.h:39
FairMQTransportFactory
Definition: FairMQTransportFactory.h:30

privacy