diff --git a/core-buffer/include/RamBufferWriter.hpp b/core-buffer/include/RamBufferWriter.hpp new file mode 100644 index 0000000..4f689f3 --- /dev/null +++ b/core-buffer/include/RamBufferWriter.hpp @@ -0,0 +1,22 @@ +#ifndef SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP +#define SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP + +#include + +class RamBufferWriter { + const int module_n_; + const std::string detector_name_; + + int shm_fd_; + +public: + RamBufferWriter(const std::string& detector_name, + const int module_n, + const size_t n_modules, + const size_t n_slots); + ~RamBufferWriter(); + +}; + + +#endif //SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP diff --git a/core-buffer/src/RamBufferWriter.cpp b/core-buffer/src/RamBufferWriter.cpp new file mode 100644 index 0000000..49d11cf --- /dev/null +++ b/core-buffer/src/RamBufferWriter.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include +#include "RamBufferWriter.hpp" +#include "buffer_config.hpp" + +using namespace std; +using namespace buffer_config; + +RamBufferWriter::RamBufferWriter( + const string &detector_name, + const int module_n, + const size_t n_modules, + const size_t n_slots) : + detector_name_(detector_name), + module_n_(module_n) + +{ + shm_fd_ = shm_open(detector_name_.c_str(), O_RDWR | O_CREAT, 0777); + if (shm_fd_ < 0) { + throw runtime_error(strerror(errno)); + } + + size_t slot_size = MODULE_N_BYTES + sizeof(ModuleFrame); + size_t buffer_size = slot_size * n_modules * n_slots; + + if ((ftruncate(shm_fd_, buffer_size)) == -1) { + throw runtime_error(strerror(errno)); + } + + void* buffer = mmap(NULL, buffer_size, PROT_WRITE, MAP_SHARED, shm_fd_, 0); + if (buffer == (void*)-1) { + throw runtime_error(strerror(errno)); + } +} + +RamBufferWriter::~RamBufferWriter() +{ + shm_unlink(detector_name_.c_str()); +}