Belle II Software development
SharedMemory Class Reference

Public Member Functions

 SharedMemory (const std::string &path, size_t size)
 
 SharedMemory (const SharedMemory &)
 
bool open (const std::string &path, size_t size)
 
bool open ()
 
void close ()
 
bool unlink ()
 
bool seekTo (size_t)
 
bool seekBy (size_t)
 
void * map (size_t, size_t)
 
void * map ()
 
bool isOpened ()
 
const std::string & getPath () const
 
size_t size () const
 
int getfd () const
 
bool truncate (size_t size)
 
const SharedMemoryoperator= (const SharedMemory &)
 

Static Public Member Functions

static bool unlink (const std::string &path)
 

Private Attributes

int m_fd
 
std::string m_path
 
size_t m_size
 
void * m_addr
 

Detailed Description

Definition at line 19 of file SharedMemory.h.

Constructor & Destructor Documentation

◆ SharedMemory() [1/3]

Definition at line 24 of file SharedMemory.cc.

25 : m_fd(-1), m_path(), m_size(0), m_addr(NULL) {}

◆ SharedMemory() [2/3]

SharedMemory ( const std::string &  path,
size_t  size 
)

Definition at line 27 of file SharedMemory.cc.

28 : m_fd(-1), m_path(path), m_size(size), m_addr(NULL)
29{
30}

◆ SharedMemory() [3/3]

SharedMemory ( const SharedMemory file)

Definition at line 32 of file SharedMemory.cc.

33 : m_fd(file.m_fd), m_path(file.m_path),
34 m_size(file.m_size), m_addr(file.m_addr) {}

◆ ~SharedMemory()

Definition at line 36 of file SharedMemory.cc.

36{}

Member Function Documentation

◆ close()

void close ( )

Definition at line 64 of file SharedMemory.cc.

65{
66 if (m_fd > 0) {
67 if (m_addr != NULL) munmap(m_addr, m_size);
68 ::close(m_fd);
69 m_fd = 0;
70 }
71}

◆ getfd()

int getfd ( ) const
inline

Definition at line 44 of file SharedMemory.h.

44{ return m_fd; }

◆ getPath()

const std::string & getPath ( ) const
inline

Definition at line 42 of file SharedMemory.h.

42{ return m_path; }

◆ isOpened()

bool isOpened ( )

Definition at line 124 of file SharedMemory.cc.

125{
126 return (m_fd != 0);
127}

◆ map() [1/2]

void * map ( )

Definition at line 102 of file SharedMemory.cc.

103{
104 if (m_addr == NULL) m_addr = map(0, m_size);
105 return m_addr;
106}

◆ map() [2/2]

void * map ( size_t  offset,
size_t  size 
)

Definition at line 88 of file SharedMemory.cc.

89{
90 errno = 0;
91 void* addr = ::mmap(NULL, size, PROT_READ | PROT_WRITE,
92 MAP_SHARED, m_fd, offset);
93 if (addr == MAP_FAILED) {
94 perror("mmap");
95 addr = NULL;
96 }
97 m_addr = addr;
98 m_size = size;
99 return addr;
100}

◆ open() [1/2]

bool open ( )

Definition at line 59 of file SharedMemory.cc.

60{
61 return open(m_path, m_size);
62}

◆ open() [2/2]

bool open ( const std::string &  path,
size_t  size 
)

Definition at line 38 of file SharedMemory.cc.

39{
40 errno = 0;
41 int fd = ::shm_open(path.c_str(), O_CREAT | O_EXCL | O_RDWR, 0666);
42 if (fd < 0) {
43 if (errno != EEXIST) {
44 perror("shm_oepn");
45 return false;
46 }
47 fd = ::shm_open(path.c_str(), O_CREAT | O_RDWR, 0666);
48 if (fd < 0) {
49 perror("shm_oepn");
50 return false;
51 }
52 }
53 m_fd = fd;
54 m_path = path;
55 truncate(size);
56 return true;
57}

◆ operator=()

const SharedMemory & operator= ( const SharedMemory file)

Definition at line 129 of file SharedMemory.cc.

130{
131 m_fd = file.m_fd;
132 m_path = file.m_path;
133 m_size = file.m_size;
134 m_addr = file.m_addr;
135 return *this;
136}

◆ seekBy()

bool seekBy ( size_t  offset)

Definition at line 119 of file SharedMemory.cc.

120{
121 return (lseek(m_fd, offset, SEEK_CUR) == -1);
122}

◆ seekTo()

bool seekTo ( size_t  offset)

Definition at line 114 of file SharedMemory.cc.

115{
116 return (lseek(m_fd, offset, SEEK_SET) == -1);
117}

◆ size()

size_t size ( ) const
inline

Definition at line 43 of file SharedMemory.h.

43{ return m_size; }

◆ truncate()

bool truncate ( size_t  size)

Definition at line 73 of file SharedMemory.cc.

74{
75 if (size > 0) {
76 ::ftruncate(m_fd, size);
77 m_size = size;
78 return true;
79 } else {
80 struct stat st;
81 fstat(m_fd, &st);
82 m_size = st.st_size;
83 }
84 return false;
85}

◆ unlink() [1/2]

bool unlink ( )

Definition at line 108 of file SharedMemory.cc.

109{
110 close();
111 return (unlink(m_path));
112}

◆ unlink() [2/2]

bool unlink ( const std::string &  path)
static

Definition at line 19 of file SharedMemory.cc.

20{
21 return (::shm_unlink(path.c_str()) == -1);
22}

Member Data Documentation

◆ m_addr

void* m_addr
private

Definition at line 56 of file SharedMemory.h.

◆ m_fd

int m_fd
private

Definition at line 53 of file SharedMemory.h.

◆ m_path

std::string m_path
private

Definition at line 54 of file SharedMemory.h.

◆ m_size

size_t m_size
private

Definition at line 55 of file SharedMemory.h.


The documentation for this class was generated from the following files: