mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2026-01-17 09:29:55 +01:00
in progress with integratign new shm in multi
This commit is contained in:
165
slsDetectorSoftware/sharedMemory/SharedMemory.cpp
Normal file
165
slsDetectorSoftware/sharedMemory/SharedMemory.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
#include "SharedMemory.h"
|
||||
#include "sls_detector_exceptions.h"
|
||||
#include "ansi.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h> // printf
|
||||
#include <cerrno> // errno
|
||||
#include <cstring> // strerror
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h> // O_CREAT, O_TRUNC..
|
||||
#include <sys/stat.h> // fstat
|
||||
#include <sys/mman.h> // shared memory
|
||||
#include <sstream>
|
||||
|
||||
|
||||
SharedMemory::SharedMemory(int multiId, int singleId):
|
||||
fd(-1),
|
||||
shmSize(0)
|
||||
{
|
||||
name = ConstructSharedMemoryName(multiId, singleId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
SharedMemory::~SharedMemory(){
|
||||
if (fd >= 0)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
|
||||
bool SharedMemory::IsExisting(std::string name) {
|
||||
bool ret = true;
|
||||
int fd = shm_open(name.c_str(), O_RDWR, 0);
|
||||
if ((fd < 0) && (errno == ENOENT)) {
|
||||
ret = false;
|
||||
}
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string SharedMemory::GetName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
void* SharedMemory::CreateSharedMemory(size_t sz){
|
||||
// create
|
||||
fd = shm_open(name.c_str(), O_CREAT | O_TRUNC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
if (fd < 0) {
|
||||
cprintf(RED, "Error: Create shared memory %s failed: %s\n",
|
||||
name.c_str(), strerror(errno));
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
// resize
|
||||
if (ftruncate(fd, sz) < 0) {
|
||||
cprintf(RED, "Error: Create shared memory %s failed at ftruncate: %s\n",
|
||||
name.c_str(), strerror(errno));
|
||||
close(fd);
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
// map
|
||||
return MapSharedMemory(sz);
|
||||
}
|
||||
|
||||
void* SharedMemory::OpenSharedMemory(size_t sz, bool verify){
|
||||
// open
|
||||
fd = shm_open(name.c_str(), O_RDWR, 0);
|
||||
if (fd < 0) {
|
||||
cprintf(RED, "Error: Open existing shared memory %s failed: %s\n",
|
||||
name.c_str(), strerror(errno));
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
// verification required and size does not match
|
||||
if (verify)
|
||||
VerifySizeMatch(sz);
|
||||
|
||||
// map
|
||||
return MapSharedMemory(sz);
|
||||
}
|
||||
|
||||
|
||||
void SharedMemory::UnmapSharedMemory(void* addr) {
|
||||
if (munmap(addr, shmSize) < 0) {
|
||||
cprintf(RED, "Error: Unmapping shared memory %s failed: %s\n",
|
||||
name.c_str(), strerror(errno));
|
||||
close(fd);
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
}
|
||||
|
||||
void SharedMemory::RemoveSharedMemory() {
|
||||
RemoveSharedMemory(name.c_str());
|
||||
}
|
||||
|
||||
|
||||
void* SharedMemory::MapSharedMemory(size_t sz) {
|
||||
void* addr = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (addr == MAP_FAILED) {
|
||||
cprintf(RED, "Error: Mapping shared memory %s failed: %s\n",
|
||||
name.c_str(), strerror(errno));
|
||||
close(fd);
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
shmSize = sz;
|
||||
close(fd);
|
||||
return addr;
|
||||
}
|
||||
|
||||
|
||||
std::string SharedMemory::ConstructSharedMemoryName(int multiId, int singleId) {
|
||||
stringstream ss;
|
||||
if (singleId < 0)
|
||||
ss << "/slsDetectorPackage_multi_" << multiId;
|
||||
else
|
||||
ss << "/slsDetectorPackage_multi_" << multiId << "_single_" << singleId;
|
||||
|
||||
std::string temp = ss.str();
|
||||
if (temp.length() > NAME_MAX) {
|
||||
cprintf(RED, "Error: Shared memory initialization %s failed: %s\n",
|
||||
name.c_str(), strerror(errno));
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
int SharedMemory::VerifySizeMatch(size_t expectedSize) {
|
||||
struct stat sb;
|
||||
// could not fstat
|
||||
if (fstat(fd, &sb) < 0) {
|
||||
cprintf(RED, "Error: Could not verify existing shared memory %s size match "
|
||||
"(could not fstat): %s\n", name.c_str(), strerror(errno));
|
||||
close(fd);
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
|
||||
//size does not match
|
||||
long unsigned int sz = (long unsigned int)sb.st_size;
|
||||
if (sz != expectedSize) {
|
||||
cprintf(RED, "Warning: Existing shared memory %s size does not match.\n",
|
||||
name.c_str());
|
||||
#ifdef VERBOSE
|
||||
cprintf(RED, " Expected %ld, found %ld\n", expectedSize, sz);
|
||||
#endif
|
||||
throw SharedMemoryException();
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SharedMemory::RemoveSharedMemory(std::string name) {
|
||||
if (shm_unlink(name.c_str()) < 0) {
|
||||
// silent exit if shm did not exist anyway
|
||||
if (errno == ENOENT)
|
||||
return;
|
||||
cprintf(RED, "Error: Free Shared Memory %s Failed: %s\n",
|
||||
name.c_str(), strerror(errno));
|
||||
throw SharedMemoryException();
|
||||
}
|
||||
printf("Shared memory deleted %s \n", name.c_str());
|
||||
}
|
||||
|
||||
117
slsDetectorSoftware/sharedMemory/SharedMemory.h
Normal file
117
slsDetectorSoftware/sharedMemory/SharedMemory.h
Normal file
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
/************************************************
|
||||
* @file SharedMemory.h
|
||||
* @short functions basic implemenation of
|
||||
* shared memory
|
||||
***********************************************/
|
||||
/**
|
||||
*@short functions basic implemenation of shared memory
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class SharedMemory{
|
||||
public:
|
||||
/**
|
||||
* Constructor
|
||||
* creates the single/multi detector shared memory name
|
||||
* @param multiId multi detector id
|
||||
* @param singleId sls detector id, -1 if a multi detector shared memory
|
||||
*/
|
||||
SharedMemory(int multiId, int singleId);
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
~SharedMemory();
|
||||
|
||||
/**
|
||||
* Verify if it exists
|
||||
* @param name of shared memory
|
||||
* @return true if exists, else false
|
||||
*/
|
||||
static bool IsExisting(std::string name);
|
||||
|
||||
/**
|
||||
* Get shared memory name
|
||||
*/
|
||||
std::string GetName();
|
||||
|
||||
/**
|
||||
* Create Shared memory and call MapSharedMemory to map it to an address
|
||||
* throws a SharedMemoryException exception on failure to create, ftruncate or map
|
||||
* @param sz of shared memory
|
||||
* @param addr double pointer to address to be mapped
|
||||
*/
|
||||
void* CreateSharedMemory(size_t sz);
|
||||
|
||||
/**
|
||||
* Open existing Shared memory and call MapSharedMemory to map it to an address
|
||||
* throws a SharedMemoryException exception on failure to open, incorrect size if verify true, or map
|
||||
* @param sz of shared memory
|
||||
* @param addr double pointer to address to be mapped
|
||||
* @param verify true to verify if the size matches existing one and return fail if does not match
|
||||
*/
|
||||
void* OpenSharedMemory(size_t sz, bool verify = true);
|
||||
|
||||
/**
|
||||
* Unmap shared memory from an address
|
||||
* throws a SharedMemoryException exception on failure
|
||||
* @param addr double pointer to address to be mapped
|
||||
*/
|
||||
void UnmapSharedMemory(void* addr);
|
||||
|
||||
/**
|
||||
* Remove existing Shared memory
|
||||
*/
|
||||
void RemoveSharedMemory();
|
||||
|
||||
/**
|
||||
* Maximum length of name as from man pages
|
||||
*/
|
||||
static const int NAME_MAX = 255;
|
||||
|
||||
private:
|
||||
/**
|
||||
* Create Shared memory name
|
||||
* throws exception if name created is longer than required 255(manpages)
|
||||
* @param multiId multi detector id
|
||||
* @param singleId sls detector id, -1 if a multi detector shared memory
|
||||
* @returns shared memory name
|
||||
*/
|
||||
std::string ConstructSharedMemoryName(int multiId, int singleId);
|
||||
|
||||
/**
|
||||
* Map shared memory to an address
|
||||
* throws a SharedMemoryException exception on failure
|
||||
* @param addr double pointer to address to be mapped
|
||||
* @param sz of shared memory
|
||||
*/
|
||||
//template <typename myType>
|
||||
// void MapSharedMemory(myType*& addr, size_t sz);
|
||||
void* MapSharedMemory(size_t sz);
|
||||
|
||||
/**
|
||||
* Verify if existing shared memory size matches expected size
|
||||
* @param sz expected size of shared memory, replaced with smaller size if size does not match
|
||||
* @return 0 for success, 1 for fail
|
||||
*/
|
||||
int VerifySizeMatch(size_t expectedSize);
|
||||
|
||||
/**
|
||||
* Remove existing Shared memory
|
||||
* @param name name of shared memory (should be less than NAME_MAX)
|
||||
*/
|
||||
void RemoveSharedMemory(std::string name);
|
||||
|
||||
/** Shared memory name */
|
||||
std::string name;
|
||||
|
||||
/** File descriptor */
|
||||
int fd;
|
||||
|
||||
/** shm size */
|
||||
size_t shmSize;
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user