Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:14

0001 #ifndef FWCore_SharedMemory_WriteBuffer_h
0002 #define FWCore_SharedMemory_WriteBuffer_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/SharedMemory
0006 // Class  :     WriteBuffer
0007 //
0008 /**\class WriteBuffer WriteBuffer.h " FWCore/SharedMemory/interface/WriteBuffer.h"
0009 
0010  Description: Manages a shared memory buffer used for writing
0011 
0012  Usage:
0013     Handles writing to a shared memory buffer. The buffer size grows automatically when necessary.
0014  This works in conjunction with ReadBuffer.
0015 
0016 */
0017 //
0018 // Original Author:  Chris Jones
0019 //         Created:  21/01/2020
0020 //
0021 
0022 // system include files
0023 #include <array>
0024 #include <memory>
0025 #include <string>
0026 #include <cassert>
0027 #include <algorithm>
0028 #include "boost/interprocess/managed_shared_memory.hpp"
0029 
0030 // user include files
0031 #include "FWCore/SharedMemory/interface/buffer_names.h"
0032 #include "FWCore/SharedMemory/interface/BufferInfo.h"
0033 
0034 // forward declarations
0035 
0036 namespace edm::shared_memory {
0037   class WriteBuffer {
0038   public:
0039     /** iUniqueName : must be unique for all processes running on a system.
0040         iBufferInfo : is a pointer to a shared_memory address where the same address needs to be shared by ReadBuffer and WriteBuffer.
0041     */
0042 
0043     WriteBuffer(std::string const& iUniqueName, BufferInfo* iBufferInfo)
0044         : bufferSize_{0}, buffer_{nullptr}, bufferInfo_{iBufferInfo} {
0045       bufferNames_[0] = iUniqueName + buffer_names::kBuffer0;
0046       bufferNames_[1] = iUniqueName + buffer_names::kBuffer1;
0047       assert(bufferInfo_);
0048     }
0049     WriteBuffer(const WriteBuffer&) = delete;
0050     const WriteBuffer& operator=(const WriteBuffer&) = delete;
0051     WriteBuffer(WriteBuffer&&) = delete;
0052     const WriteBuffer& operator=(WriteBuffer&&) = delete;
0053 
0054     ~WriteBuffer();
0055 
0056     // ---------- member functions ---------------------------
0057     void copyToBuffer(const char* iStart, std::size_t iLength) {
0058       if (iLength > bufferSize_) {
0059         growBuffer(iLength);
0060       }
0061       std::copy(iStart, iStart + iLength, buffer_);
0062     }
0063 
0064   private:
0065     void growBuffer(std::size_t iLength);
0066 
0067     // ---------- member data --------------------------------
0068     std::size_t bufferSize_;
0069     char* buffer_;
0070     BufferInfo* bufferInfo_;
0071     std::array<std::string, 2> bufferNames_;
0072     std::unique_ptr<boost::interprocess::managed_shared_memory> sm_;
0073   };
0074 }  // namespace edm::shared_memory
0075 
0076 #endif