libshevek
shm.hh
1 /* shm.hh - shared memory
2  * Copyright 2007 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_SHM_HH
19 #define SHEVEK_SHM_HH
20 
21 #include "refbase.hh"
22 #include "error.hh"
23 #include <string>
24 #include <typeinfo>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 
30 namespace shevek
31 {
33  template <typename T> class shm : public refbase
34  {
35  shm (std::string const &name, bool l_create, bool writable, bool keep);
36  ~shm ();
37  T *m_data;
38  std::string m_name;
39  int m_fd;
40  bool m_destroy;
41  public:
43 
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; }
52  };
53 
54  template <typename T> shm <T>::shm (std::string const &name, bool l_create, bool writable, bool keep)
55  {
56  m_destroy = !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);
59  if (m_fd < 0)
60  {
61  shevek_error_errno ("unable to open shared memory file " + m_name);
62  throw "unable to open shared memory file";
63  }
64  if (l_create && ftruncate (m_fd, sizeof (T)) < 0)
65  {
66  shevek_error_errno ("unable to set size of shared memory file " + m_name);
67  throw "unable to set size of shared memory file";
68  }
69  m_data = reinterpret_cast <T *> (mmap (NULL, sizeof (T), writable ? PROT_READ | PROT_WRITE : PROT_READ, MAP_SHARED, m_fd, 0));
70  if (!m_data)
71  {
72  shevek_error_errno ("unable to map shared memory file " + m_name);
73  throw "unable to map shared memory";
74  }
75  }
76 
77  template <typename T> shm <T>::~shm ()
78  {
79  close (m_fd);
80  munmap (m_data, sizeof (T));
81  if (m_destroy)
82  shm_unlink (m_name.c_str ());
83  }
84 }
85 
86 #endif
static Glib::RefPtr< shm< T > > create(std::string const &name, bool keep=false)
Create a new block of shared memory.
Definition: shm.hh:45
T const * data() const
Access the shared data.
Definition: shm.hh:51
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
static Glib::RefPtr< shm< T > > open(std::string const &name, bool writable=true)
Open an existing block of shared memory.
Definition: shm.hh:47
T * data()
Access the shared data.
Definition: shm.hh:49
This class implements an interface for sharing memory between processes.
Definition: shm.hh:33