moving SharedMemory into namespace sls

This commit is contained in:
Erik Frojdh 2019-03-14 16:51:38 +01:00
parent 40d2f66146
commit bad44f5bf4
6 changed files with 20 additions and 11 deletions

View File

@ -26,6 +26,9 @@
#include <future>
#include <vector>
using sls::SharedMemory;
using sls::SharedMemoryError;
multiSlsDetector::multiSlsDetector(int id, bool verify, bool update)
: detId(id) {
setupMultiDetector(verify, update);

View File

@ -1959,7 +1959,7 @@ class multiSlsDetector : public virtual slsDetectorDefs,
int detId;
/** Shared Memory object */
SharedMemory<sharedMultiSlsDetector> multi_shm{0, -1};
sls::SharedMemory<sharedMultiSlsDetector> multi_shm{0, -1};
/** pointers to the slsDetector structures */
std::vector<std::unique_ptr<slsDetector>> detectors;

View File

@ -30,10 +30,11 @@
#include <iostream>
#include <string>
using sls::SharedMemoryError;
namespace sls {
template <typename T>
class SharedMemory {
public:
/**
* Constructor
@ -166,7 +167,7 @@ class SharedMemory {
void UnmapSharedMemory() {
if (shared_struct != nullptr) {
if (munmap(shared_struct, shmSize) < 0) {
std::string msg = "Unmapping shared memory " + name +" failed: " + strerror(errno);
std::string msg = "Unmapping shared memory " + name + " failed: " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd);
throw SharedMemoryError(msg);
@ -236,8 +237,7 @@ class SharedMemory {
std::string temp = ss.str();
if (temp.length() > NAME_MAX) {
std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n"
+ "Maximum is " + std::to_string(NAME_MAX) + ". Change the environment variable " + SHM_ENV_NAME;
std::string msg = "Shared memory initialization failed. " + temp + " has " + std::to_string(temp.length()) + " characters. \n" + "Maximum is " + std::to_string(NAME_MAX) + ". Change the environment variable " + SHM_ENV_NAME;
FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
}
@ -272,8 +272,7 @@ class SharedMemory {
struct stat sb;
// could not fstat
if (fstat(fd, &sb) < 0) {
std::string msg = "Could not verify existing shared memory " + name + " size match "
+ "(could not fstat): " + strerror(errno);
std::string msg = "Could not verify existing shared memory " + name + " size match " + "(could not fstat): " + strerror(errno);
FILE_LOG(logERROR) << msg;
close(fd);
throw SharedMemoryError(msg);
@ -282,8 +281,7 @@ class SharedMemory {
//size does not match
long unsigned int sz = (long unsigned int)sb.st_size;
if (sz != expectedSize) {
std::string msg = "Existing shared memory " + name + " size does not match"
+ "Expected " + std::to_string(expectedSize) + ", found " + std::to_string(sz);
std::string msg = "Existing shared memory " + name + " size does not match" + "Expected " + std::to_string(expectedSize) + ", found " + std::to_string(sz);
FILE_LOG(logERROR) << msg;
throw SharedMemoryError(msg);
return 1;
@ -302,3 +300,5 @@ class SharedMemory {
T *shared_struct{nullptr};
};
} // namespace sls

View File

@ -24,6 +24,10 @@
#include <sys/types.h>
#include <cassert>
using sls::SharedMemory;
using sls::SharedMemoryError;
using sls::RuntimeError;
#define DEFAULT_HOSTNAME "localhost"
slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify)
@ -290,7 +294,7 @@ void slsDetector::setDetectorSpecificParameters(detectorType type, detParameterL
break;
default:
FILE_LOG(logERROR) << "Unknown detector type! " << type;
throw std::exception();
throw RuntimeError("Unknown detector type");
}
}

View File

@ -1736,7 +1736,7 @@ class slsDetector : public virtual slsDetectorDefs, public virtual errorDefs {
int detId;
/** Shared Memory object */
SharedMemory<sharedSlsDetector> detector_shm{0,0};
sls::SharedMemory<sharedSlsDetector> detector_shm{0,0};
};

View File

@ -11,6 +11,8 @@ struct Data {
char mess[50];
};
using namespace sls;
TEST_CASE("Create SharedMemory read and write") {
SharedMemory<Data> shm(0, -1);