Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-17 03:58:08

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWCore/SharedMemory
0004 // Class  :     WriteBuffer
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  21/01/2020
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "FWCore/SharedMemory/interface/WriteBuffer.h"
0017 #include "FWCore/Utilities/interface/Exception.h"
0018 //
0019 // constants, enums and typedefs
0020 //
0021 using namespace edm::shared_memory;
0022 
0023 //
0024 // static data member definitions
0025 //
0026 
0027 //
0028 // constructors and destructor
0029 //
0030 
0031 WriteBuffer::~WriteBuffer() {
0032   if (sm_) {
0033     sm_->destroy<char>(buffer_names::kBuffer);
0034     sm_.reset();
0035     boost::interprocess::shared_memory_object::remove(bufferNames_[bufferInfo_->index_].c_str());
0036   }
0037 }
0038 //
0039 // member functions
0040 //
0041 void WriteBuffer::growBuffer(std::size_t iLength) {
0042   int newBuffer = (bufferInfo_->index_ + 1) % 2;
0043   if (sm_) {
0044     try {
0045       sm_->destroy<char>(buffer_names::kBuffer);
0046     } catch (boost::interprocess::interprocess_exception const& iExcept) {
0047       throw cms::Exception("SharedMemory")
0048           << "in growBuffer while destroying the shared memory object the following exception was caught\n"
0049           << iExcept.what();
0050     }
0051     sm_.reset();
0052     try {
0053       boost::interprocess::shared_memory_object::remove(bufferNames_[bufferInfo_->index_].c_str());
0054     } catch (boost::interprocess::interprocess_exception const& iExcept) {
0055       throw cms::Exception("SharedMemory")
0056           << "in growBuffer while removing the shared memory object named '" << bufferNames_[bufferInfo_->index_]
0057           << "' the following exception was caught\n"
0058           << iExcept.what();
0059     }
0060   }
0061   try {
0062     sm_ = std::make_unique<boost::interprocess::managed_shared_memory>(
0063         boost::interprocess::open_or_create, bufferNames_[newBuffer].c_str(), iLength + 1024);
0064   } catch (boost::interprocess::interprocess_exception const& iExcept) {
0065     throw cms::Exception("SharedMemory") << "in growBuffer while creating the shared memory object '"
0066                                          << bufferNames_[newBuffer] << "' of length " << iLength + 1024
0067                                          << " the following exception was caught\n"
0068                                          << iExcept.what();
0069   }
0070   assert(sm_.get());
0071   bufferSize_ = iLength;
0072   bufferInfo_->index_ = newBuffer;
0073   bufferInfo_->identifier_ = bufferInfo_->identifier_ + 1;
0074   buffer_ = sm_->construct<char>(buffer_names::kBuffer)[iLength](0);
0075   assert(buffer_);
0076 }
0077 
0078 //
0079 // const member functions
0080 //
0081 
0082 //
0083 // static member functions
0084 //