FairMQ  1.4.14
C++ Message Queuing Library and Framework
Common.h
1 /********************************************************************************
2  * Copyright (C) 2014 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 #ifndef FAIR_MQ_SHMEM_COMMON_H_
9 #define FAIR_MQ_SHMEM_COMMON_H_
10 
11 #include <atomic>
12 #include <string>
13 #include <unordered_map>
14 
15 #include <boost/interprocess/managed_shared_memory.hpp>
16 #include <boost/interprocess/allocators/allocator.hpp>
17 #include <boost/interprocess/containers/map.hpp>
18 #include <boost/interprocess/containers/string.hpp>
19 #include <boost/interprocess/containers/vector.hpp>
20 #include <boost/functional/hash.hpp>
21 
22 #include <unistd.h>
23 #include <sys/types.h>
24 
25 namespace fair
26 {
27 namespace mq
28 {
29 namespace shmem
30 {
31 
32 using SegmentManager = boost::interprocess::managed_shared_memory::segment_manager;
33 using VoidAlloc = boost::interprocess::allocator<void, SegmentManager>;
34 using CharAlloc = boost::interprocess::allocator<char, SegmentManager>;
35 using Str = boost::interprocess::basic_string<char, std::char_traits<char>, CharAlloc>;
36 using StrAlloc = boost::interprocess::allocator<Str, SegmentManager>;
37 using StrVector = boost::interprocess::vector<Str, StrAlloc>;
38 
39 struct RegionInfo
40 {
41  RegionInfo(const VoidAlloc& alloc)
42  : fPath("", alloc)
43  , fFlags(0)
44  , fUserFlags(0)
45  , fDestroyed(false)
46  {}
47 
48  RegionInfo(const char* path, const int flags, const uint64_t userFlags, const VoidAlloc& alloc)
49  : fPath(path, alloc)
50  , fFlags(flags)
51  , fUserFlags(userFlags)
52  , fDestroyed(false)
53  {}
54 
55  Str fPath;
56  int fFlags;
57  uint64_t fUserFlags;
58  bool fDestroyed;
59 };
60 
61 using Uint64RegionInfoPairAlloc = boost::interprocess::allocator<std::pair<const uint64_t, RegionInfo>, SegmentManager>;
62 using Uint64RegionInfoMap = boost::interprocess::map<uint64_t, RegionInfo, std::less<uint64_t>, Uint64RegionInfoPairAlloc>;
63 
65 {
66  DeviceCounter(unsigned int c)
67  : fCount(c)
68  {}
69 
70  std::atomic<unsigned int> fCount;
71 };
72 
74 {
75  RegionCounter(uint64_t c)
76  : fCount(c)
77  {}
78 
79  std::atomic<uint64_t> fCount;
80 };
81 
82 struct MetaHeader
83 {
84  size_t fSize;
85  size_t fRegionId;
86  size_t fHint;
87  boost::interprocess::managed_shared_memory::handle_t fHandle;
88 };
89 
91 {
92  RegionBlock()
93  : fHandle()
94  , fSize(0)
95  , fHint(0)
96  {}
97 
98  RegionBlock(boost::interprocess::managed_shared_memory::handle_t handle, size_t size, size_t hint)
99  : fHandle(handle)
100  , fSize(size)
101  , fHint(hint)
102  {}
103 
104  boost::interprocess::managed_shared_memory::handle_t fHandle;
105  size_t fSize;
106  size_t fHint;
107 };
108 
109 // find id for unique shmem name:
110 // a hash of user id + session id, truncated to 8 characters (to accommodate for name size limit on some systems (MacOS)).
111 inline std::string buildShmIdFromSessionIdAndUserId(const std::string& sessionId)
112 {
113  boost::hash<std::string> stringHash;
114  std::string shmId(std::to_string(stringHash(std::string((std::to_string(geteuid()) + sessionId)))));
115  shmId.resize(8, '_');
116  return shmId;
117 }
118 
119 } // namespace shmem
120 } // namespace mq
121 } // namespace fair
122 
123 #endif /* FAIR_MQ_SHMEM_COMMON_H_ */
Definition: Common.h:73
Definition: Common.h:90
Definition: Common.h:39
Definition: Common.h:64
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
Definition: Common.h:82

privacy