8#include "daq/slc/system/SharedMemory.h"
19bool SharedMemory::unlink(
const std::string& path)
21 return (::shm_unlink(path.c_str()) == -1);
24SharedMemory::SharedMemory()
25 : m_fd(-1), m_path(), m_size(0), m_addr(NULL) {}
27SharedMemory::SharedMemory(
const std::string& path,
size_t size)
28 : m_fd(-1), m_path(path), m_size(size), m_addr(NULL)
33 : m_fd(file.m_fd), m_path(file.m_path),
34 m_size(file.m_size), m_addr(file.m_addr) {}
36SharedMemory::~SharedMemory() {}
38bool SharedMemory::open(
const std::string& path,
size_t size)
41 int fd = ::shm_open(path.c_str(), O_CREAT | O_EXCL | O_RDWR, 0666);
43 if (errno != EEXIST) {
47 fd = ::shm_open(path.c_str(), O_CREAT | O_RDWR, 0666);
59bool SharedMemory::open()
61 return open(m_path, m_size);
64void SharedMemory::close()
67 if (m_addr != NULL) munmap(m_addr, m_size);
73bool SharedMemory::truncate(
size_t size)
76 ::ftruncate(m_fd, size);
88void* SharedMemory::map(
size_t offset,
size_t size)
91 void* addr = ::mmap(NULL, size, PROT_READ | PROT_WRITE,
92 MAP_SHARED, m_fd, offset);
93 if (addr == MAP_FAILED) {
102void* SharedMemory::map()
104 if (m_addr == NULL) m_addr = map(0, m_size);
108bool SharedMemory::unlink()
111 return (unlink(m_path));
114bool SharedMemory::seekTo(
size_t offset)
116 return (lseek(m_fd, offset, SEEK_SET) == -1);
119bool SharedMemory::seekBy(
size_t offset)
121 return (lseek(m_fd, offset, SEEK_CUR) == -1);
124bool SharedMemory::isOpened()
132 m_path = file.m_path;
133 m_size = file.m_size;
134 m_addr = file.m_addr;
Abstract base class for different kinds of events.