Skip to content

Source · fairmq/shmem/

Common.cxxsource

Described in How shared memory works, Sessions and segments, Out-of-band buffers, Tune and debug.

fairmq/shmem/Common.cxx56 lines · C++GitHub ↗
1/********************************************************************************2 *    Copyright (C) 2021 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#include "Common.h"10 11#include <picosha2.h>12 13#include <unistd.h>14 15#include <iomanip>16#include <sstream>17#include <string>18 19namespace fair::mq::shmem20{21 22std::string makeShmIdStr(const std::string& sessionId, const std::string& userId)23{24    std::string seed(userId + sessionId);25    // generate a 8-digit hex value out of sha256 hash26    std::vector<unsigned char> hash(4);27    picosha2::hash256(seed.begin(), seed.end(), hash.begin(), hash.end());28 29    return picosha2::bytes_to_hex_string(hash.begin(), hash.end());30}31 32std::string makeShmIdStr(const std::string& sessionId)33{34    return makeShmIdStr(sessionId, std::to_string(geteuid()));35}36 37uint64_t makeShmIdUint64(const std::string& sessionId)38{39    std::string shmId = makeShmIdStr(sessionId);40    uint64_t id = 0;41    std::stringstream ss;42    ss << std::hex << shmId;43    ss >> id;44 45    return id;46}47 48std::string makeShmIdStr(uint64_t val)49{50    std::stringstream ss;51    ss << std::setfill('0') << std::setw(8) << std::hex << val;52    return ss.str();53}54 55 56}   // namespace fair::mq::shmem