35 shm (std::string
const &name,
bool l_create,
bool writable,
bool keep);
45 static Glib::RefPtr <shm <T> >
create (std::string
const &name,
bool keep =
false) {
return Glib::RefPtr <shm <T> > (
new shm (name,
true,
true, keep)); }
47 static Glib::RefPtr <shm <T> >
open (std::string
const &name,
bool writable =
true) {
return Glib::RefPtr <shm <T> > (
new shm (name,
false, writable,
true)); }
49 T *
data () {
return m_data; }
51 T
const *
data ()
const {
return m_data; }
54 template <
typename T> shm <T>::shm (std::string
const &name,
bool l_create,
bool writable,
bool keep)
57 m_name = std::string (
"/") +
typeid (T).name () +
'-' + name;
58 m_fd = shm_open (m_name.c_str (), l_create ? O_CREAT | O_EXCL | O_RDWR : writable ? O_RDWR : O_RDONLY, 0666);
61 shevek_error_errno (
"unable to open shared memory file " + m_name);
62 throw "unable to open shared memory file";
64 if (l_create && ftruncate (m_fd,
sizeof (T)) < 0)
66 shevek_error_errno (
"unable to set size of shared memory file " + m_name);
67 throw "unable to set size of shared memory file";
69 m_data = reinterpret_cast <T *> (mmap (NULL,
sizeof (T), writable ? PROT_READ | PROT_WRITE : PROT_READ, MAP_SHARED, m_fd, 0));
72 shevek_error_errno (
"unable to map shared memory file " + m_name);
73 throw "unable to map shared memory";
77 template <
typename T> shm <T>::~shm ()
80 munmap (m_data,
sizeof (T));
82 shm_unlink (m_name.c_str ());