Add stub implementation of RamBufferWriter

This commit is contained in:
2020-09-02 13:39:26 +02:00
parent 0f478c87aa
commit 5b9b650803
2 changed files with 65 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
#ifndef SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP
#define SF_DAQ_BUFFER_RAMBUFFERWRITER_HPP
#include <string>
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
+43
View File
@@ -0,0 +1,43 @@
#include <sys/mman.h>
#include <bits/fcntl-linux.h>
#include <cstring>
#include <stdexcept>
#include <formats.hpp>
#include <unistd.h>
#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());
}