Skip to content
Snippets Groups Projects
Commit 8f68612e authored by Max Breitenfeldt's avatar Max Breitenfeldt
Browse files

Refactor shared memory initialization

parent 0379ba57
No related branches found
No related tags found
No related merge requests found
Pipeline #39509 failed
...@@ -18,24 +18,6 @@ ...@@ -18,24 +18,6 @@
using json = nlohmann::json; using json = nlohmann::json;
void* init_shm_mmap(const char *path, int len) {
int fd = shm_open(path, O_RDWR, 0);
if (fd == -1) {
throw "shm_open";
}
void *shm_buf = mmap(NULL, len,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
if (shm_buf == MAP_FAILED) {
throw "mmap";
}
return shm_buf;
}
BioTrackerTrackingAlgorithm::BioTrackerTrackingAlgorithm(IController *parent, IModel* parameter, IModel* trajectory) BioTrackerTrackingAlgorithm::BioTrackerTrackingAlgorithm(IController *parent, IModel* parameter, IModel* trajectory)
: IModelTrackingAlgorithm(parent) : IModelTrackingAlgorithm(parent)
{ {
...@@ -55,7 +37,7 @@ BioTrackerTrackingAlgorithm::~BioTrackerTrackingAlgorithm() ...@@ -55,7 +37,7 @@ BioTrackerTrackingAlgorithm::~BioTrackerTrackingAlgorithm()
stop_python(); stop_python();
} }
void BioTrackerTrackingAlgorithm::request_shared_memory() { void BioTrackerTrackingAlgorithm::init_shared_memory() {
json j = { json j = {
{ "type", "request_shared_memory" }, { "type", "request_shared_memory" },
{ "width", _imageX }, { "width", _imageX },
...@@ -65,7 +47,23 @@ void BioTrackerTrackingAlgorithm::request_shared_memory() { ...@@ -65,7 +47,23 @@ void BioTrackerTrackingAlgorithm::request_shared_memory() {
auto res = _sock.recv(_zmq_msg, zmq::recv_flags::none); auto res = _sock.recv(_zmq_msg, zmq::recv_flags::none);
auto msg = json::parse(_zmq_msg.to_string_view()); auto msg = json::parse(_zmq_msg.to_string_view());
std::string shm_path = msg["path"].get<std::string>(); std::string shm_path = msg["path"].get<std::string>();
_shm_img = (float*)init_shm_mmap(shm_path.c_str(), _imageX * _imageY * sizeof(float)); int shm_len = _imageX * _imageY * sizeof(float);
int fd = shm_open(shm_path.c_str(), O_RDWR, 0);
if (fd == -1) {
throw "shm_open";
}
void *shm_buf = mmap(NULL, shm_len,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
if (shm_buf == MAP_FAILED) {
throw "mmap";
}
_shm_img = (float*)shm_buf;
} }
void BioTrackerTrackingAlgorithm::stop_python() { void BioTrackerTrackingAlgorithm::stop_python() {
...@@ -112,7 +110,7 @@ void BioTrackerTrackingAlgorithm::doTracking(std::shared_ptr<cv::Mat> p_image, u ...@@ -112,7 +110,7 @@ void BioTrackerTrackingAlgorithm::doTracking(std::shared_ptr<cv::Mat> p_image, u
if (_imageX != p_image->size().width || _imageY != p_image->size().height) { if (_imageX != p_image->size().width || _imageY != p_image->size().height) {
_imageX = p_image->size().width; _imageX = p_image->size().width;
_imageY = p_image->size().height; _imageY = p_image->size().height;
request_shared_memory(); init_shared_memory();
Q_EMIT emitDimensionUpdate(_imageX, _imageY); Q_EMIT emitDimensionUpdate(_imageX, _imageY);
} }
......
...@@ -38,7 +38,7 @@ public Q_SLOTS: ...@@ -38,7 +38,7 @@ public Q_SLOTS:
void receiveParametersChanged(); void receiveParametersChanged();
private: private:
void request_shared_memory(); void init_shared_memory();
void start_python(); void start_python();
void stop_python(); void stop_python();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment