FairMQ  1.4.33
C++ Message Queuing Library and Framework
ControlMessages.h
1 /********************************************************************************
2  * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_OFI_CONTROLMESSAGES_H
10 #define FAIR_MQ_OFI_CONTROLMESSAGES_H
11 
12 #include <FairMQLogger.h>
13 #include <boost/asio/buffer.hpp>
14 #include <boost/container/pmr/memory_resource.hpp>
15 #include <cstdint>
16 #include <functional>
17 #include <memory>
18 #include <type_traits>
19 
20 namespace boost::asio
21 {
22 
23 template<typename PodType>
24 auto buffer(const PodType& obj) -> boost::asio::const_buffer
25 {
26  return boost::asio::const_buffer(static_cast<const void*>(&obj), sizeof(PodType));
27 }
28 
29 } // namespace boost::asio
30 
31 namespace fair::mq::ofi
32 {
33 
34 enum class ControlMessageType
35 {
36  Empty = 1,
37  PostBuffer,
38  PostMultiPartStartBuffer
39 };
40 
41 struct Empty
42 {};
43 
44 struct PostBuffer
45 {
46  uint64_t size; // buffer size (size_t)
47 };
48 
50 {
51  uint32_t numParts; // buffer size (size_t)
52  uint64_t size; // buffer size (size_t)
53 };
54 
56 {
57  PostBuffer postBuffer;
58  PostMultiPartStartBuffer postMultiPartStartBuffer;
59 };
60 
62 {
63  ControlMessageType type;
65 };
66 
67 template<typename T>
68 using unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
69 
70 template<typename T, typename... Args>
71 auto MakeControlMessageWithPmr(boost::container::pmr::memory_resource& pmr, Args&&... args)
72  -> ofi::unique_ptr<ControlMessage>
73 {
74  void* mem = pmr.allocate(sizeof(ControlMessage));
75  ControlMessage* ctrl = new (mem) ControlMessage();
76 
77  if (std::is_same<T, PostBuffer>::value) {
78  ctrl->type = ControlMessageType::PostBuffer;
79  ctrl->msg.postBuffer = PostBuffer(std::forward<Args>(args)...);
80  } else if (std::is_same<T, PostMultiPartStartBuffer>::value) {
81  ctrl->type = ControlMessageType::PostMultiPartStartBuffer;
82  ctrl->msg.postMultiPartStartBuffer = PostMultiPartStartBuffer(std::forward<Args>(args)...);
83  } else if (std::is_same<T, Empty>::value) {
84  ctrl->type = ControlMessageType::Empty;
85  }
86 
87  return ofi::unique_ptr<ControlMessage>(ctrl, [&pmr](ControlMessage* p) {
88  p->~ControlMessage();
89  pmr.deallocate(p, sizeof(T));
90  });
91 }
92 
93 template<typename T, typename... Args>
94 auto MakeControlMessage(Args&&... args) -> ControlMessage
95 {
96  ControlMessage ctrl;
97 
98  if (std::is_same<T, PostBuffer>::value) {
99  ctrl.type = ControlMessageType::PostBuffer;
100  } else if (std::is_same<T, PostMultiPartStartBuffer>::value) {
101  ctrl.type = ControlMessageType::PostMultiPartStartBuffer;
102  } else if (std::is_same<T, Empty>::value) {
103  ctrl.type = ControlMessageType::Empty;
104  }
105  ctrl.msg = T(std::forward<Args>(args)...);
106 
107  return ctrl;
108 }
109 
110 } // namespace fair::mq::ofi
111 
112 #endif /* FAIR_MQ_OFI_CONTROLMESSAGES_H */
fair::mq::ofi::ControlMessage
Definition: ControlMessages.h:62
fair::mq::ofi::Empty
Definition: ControlMessages.h:42
fair::mq::ofi::PostMultiPartStartBuffer
Definition: ControlMessages.h:50
fair::mq::ofi::PostBuffer
Definition: ControlMessages.h:45
fair::mq::ofi::ControlMessageContent
Definition: ControlMessages.h:56

privacy