FairMQ  1.3.9
C++ Message Passing 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 <string>
27 #include <type_traits>
28 #include <unordered_map>
29 #include <utility>
30 #include <vector>
31 
32 namespace fair {
33 namespace mq {
34 
35 using byte = unsigned char;
36 namespace pmr = boost::container::pmr;
37 
41 class FairMQMemoryResource : public pmr::memory_resource
42 {
43  public:
49  virtual FairMQMessagePtr getMessage(void *p) = 0;
50  virtual void *setMessage(FairMQMessagePtr) = 0;
51  virtual FairMQTransportFactory *getTransportFactory() noexcept = 0;
52  virtual size_t getNumberOfMessages() const noexcept = 0;
53 };
54 
61 {
62  protected:
63  FairMQTransportFactory *factory{nullptr};
64  // TODO: for now a map to keep track of allocations, something else would
65  // probably be
66  // faster, but for now this does not need to be fast.
67  boost::container::flat_map<void *, FairMQMessagePtr> messageMap;
68 
69  public:
70  ChannelResource() = delete;
71 
74  , factory(_factory)
75  , messageMap()
76  {
77  if (!_factory) {
78  throw std::runtime_error("Tried to construct from a nullptr FairMQTransportFactory");
79  }
80  };
81 
82  FairMQMessagePtr getMessage(void *p) override
83  {
84  auto mes = std::move(messageMap[p]);
85  messageMap.erase(p);
86  return mes;
87  }
88 
89  void *setMessage(FairMQMessagePtr message) override
90  {
91  void *addr = message->GetData();
92  messageMap[addr] = std::move(message);
93  return addr;
94  }
95 
96  FairMQTransportFactory *getTransportFactory() noexcept override { return factory; }
97 
98  size_t getNumberOfMessages() const noexcept override { return messageMap.size(); }
99 
100  protected:
101  void *do_allocate(std::size_t bytes, std::size_t alignment) override;
102  void do_deallocate(void *p, std::size_t /*bytes*/, std::size_t /*alignment*/) override
103  {
104  messageMap.erase(p);
105  };
106 
107  bool do_is_equal(const pmr::memory_resource &other) const noexcept override
108  {
109  return this == &other;
110  };
111 };
112 
113 } /* namespace mq */
114 } /* namespace fair */
115 
116 #endif /* FAIR_MQ_MEMORY_RESOURCES_H */
virtual FairMQMessagePtr getMessage(void *p)=0
Definition: FairMQTransportFactory.h:28
Definition: MemoryResources.h:41
FairMQMessagePtr getMessage(void *p) override
Definition: MemoryResources.h:82
Definition: MemoryResources.h:60
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
void * do_allocate(std::size_t bytes, std::size_t alignment) override
Memory allocators and interfaces related to managing memory via the trasport layer.
Definition: MemoryResources.cxx:18

privacy