/******************************************************************************** * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ #ifndef FAIR_MQ_SHMEM_COMMON_H_ #define FAIR_MQ_SHMEM_COMMON_H_ #include #include #include #include // std::equal_to #include #include #include #include #include #include #include #include #include namespace fair { namespace mq { namespace shmem { using SegmentManager = boost::interprocess::managed_shared_memory::segment_manager; using VoidAlloc = boost::interprocess::allocator; using CharAlloc = boost::interprocess::allocator; using Str = boost::interprocess::basic_string, CharAlloc>; using StrAlloc = boost::interprocess::allocator; using StrVector = boost::interprocess::vector; struct RegionInfo { RegionInfo(const VoidAlloc& alloc) : fPath("", alloc) , fFlags(0) , fUserFlags(0) , fDestroyed(false) {} RegionInfo(const char* path, const int flags, const uint64_t userFlags, const VoidAlloc& alloc) : fPath(path, alloc) , fFlags(flags) , fUserFlags(userFlags) , fDestroyed(false) {} Str fPath; int fFlags; uint64_t fUserFlags; bool fDestroyed; }; using Uint64RegionInfoPairAlloc = boost::interprocess::allocator, SegmentManager>; using Uint64RegionInfoMap = boost::interprocess::map, Uint64RegionInfoPairAlloc>; struct DeviceCounter { DeviceCounter(unsigned int c) : fCount(c) {} std::atomic fCount; }; #ifdef FAIRMQ_DEBUG_MODE struct MsgCounter { MsgCounter(unsigned int c) : fCount(c) {} std::atomic fCount; }; #endif struct RegionCounter { RegionCounter(uint64_t c) : fCount(c) {} std::atomic fCount; }; struct MetaHeader { size_t fSize; size_t fRegionId; size_t fHint; boost::interprocess::managed_shared_memory::handle_t fHandle; }; #ifdef FAIRMQ_DEBUG_MODE struct MsgDebug { MsgDebug(pid_t pid, size_t size, const uint64_t creationTime) : fPid(pid) , fSize(size) , fCreationTime(creationTime) {} pid_t fPid; size_t fSize; uint64_t fCreationTime; }; using Uint64MsgDebugPairAlloc = boost::interprocess::allocator, SegmentManager>; using Uint64MsgDebugHashMap = boost::unordered_map, std::equal_to, Uint64MsgDebugPairAlloc>; using Uint64MsgDebugMap = boost::interprocess::map, Uint64MsgDebugPairAlloc>; #endif struct RegionBlock { RegionBlock() : fHandle() , fSize(0) , fHint(0) {} RegionBlock(boost::interprocess::managed_shared_memory::handle_t handle, size_t size, size_t hint) : fHandle(handle) , fSize(size) , fHint(hint) {} boost::interprocess::managed_shared_memory::handle_t fHandle; size_t fSize; size_t fHint; }; // find id for unique shmem name: // a hash of user id + session id, truncated to 8 characters (to accommodate for name size limit on some systems (MacOS)). inline std::string buildShmIdFromSessionIdAndUserId(const std::string& sessionId) { std::string seed((std::to_string(geteuid()) + sessionId)); // generate a 8-digit hex value out of sha256 hash std::vector hash(4); picosha2::hash256(seed.begin(), seed.end(), hash.begin(), hash.end()); std::string shmId = picosha2::bytes_to_hex_string(hash.begin(), hash.end()); return shmId; } } // namespace shmem } // namespace mq } // namespace fair #endif /* FAIR_MQ_SHMEM_COMMON_H_ */