CMS 3D CMS Logo

WriteBuffer.h
Go to the documentation of this file.
1 #ifndef FWCore_SharedMemory_WriteBuffer_h
2 #define FWCore_SharedMemory_WriteBuffer_h
3 // -*- C++ -*-
4 //
5 // Package: FWCore/SharedMemory
6 // Class : WriteBuffer
7 //
17 //
18 // Original Author: Chris Jones
19 // Created: 21/01/2020
20 //
21 
22 // system include files
23 #include <array>
24 #include <memory>
25 #include <string>
26 #include <cassert>
27 #include <algorithm>
28 #include "boost/interprocess/managed_shared_memory.hpp"
29 
30 // user include files
33 
34 // forward declarations
35 
36 namespace edm::shared_memory {
37  class WriteBuffer {
38  public:
43  WriteBuffer(std::string const& iUniqueName, BufferInfo* iBufferInfo)
44  : bufferSize_{0}, buffer_{nullptr}, bufferInfo_{iBufferInfo} {
45  bufferNames_[0] = iUniqueName + buffer_names::kBuffer0;
46  bufferNames_[1] = iUniqueName + buffer_names::kBuffer1;
48  }
49  WriteBuffer(const WriteBuffer&) = delete;
50  const WriteBuffer& operator=(const WriteBuffer&) = delete;
51  WriteBuffer(WriteBuffer&&) = delete;
52  const WriteBuffer& operator=(WriteBuffer&&) = delete;
53 
54  ~WriteBuffer();
55 
56  // ---------- member functions ---------------------------
57  void copyToBuffer(const char* iStart, std::size_t iLength) {
58  if (iLength > bufferSize_) {
59  growBuffer(iLength);
60  }
61  std::copy(iStart, iStart + iLength, buffer_);
62  }
63 
64  private:
65  void growBuffer(std::size_t iLength);
66 
67  // ---------- member data --------------------------------
68  std::size_t bufferSize_;
69  char* buffer_;
71  std::array<std::string, 2> bufferNames_;
72  struct SMOwner {
73  SMOwner() = default;
74  SMOwner(std::string const& iName, std::size_t iLength);
75  ~SMOwner();
77  boost::interprocess::managed_shared_memory* operator->() { return sm_.get(); }
78  boost::interprocess::managed_shared_memory* get() { return sm_.get(); }
79  operator bool() const { return bool(sm_); }
80  void reset();
81 
82  private:
84  std::unique_ptr<boost::interprocess::managed_shared_memory> sm_;
85  } sm_;
86  };
87 } // namespace edm::shared_memory
88 
89 #endif
constexpr char const *const kBuffer0
Definition: buffer_names.h:30
struct edm::shared_memory::WriteBuffer::SMOwner sm_
boost::interprocess::managed_shared_memory * operator->()
Definition: WriteBuffer.h:77
assert(be >=bs)
std::unique_ptr< boost::interprocess::managed_shared_memory > sm_
Definition: WriteBuffer.h:84
void growBuffer(std::size_t iLength)
Definition: WriteBuffer.cc:64
std::array< std::string, 2 > bufferNames_
Definition: WriteBuffer.h:71
const WriteBuffer & operator=(const WriteBuffer &)=delete
constexpr char const *const kBuffer1
Definition: buffer_names.h:31
WriteBuffer(std::string const &iUniqueName, BufferInfo *iBufferInfo)
Definition: WriteBuffer.h:43
void copyToBuffer(const char *iStart, std::size_t iLength)
Definition: WriteBuffer.h:57