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

privacy