Gnash  0.8.11dev
SharedMem.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #ifndef GNASH_SHM_H
20 #define GNASH_SHM_H
21 
22 #ifdef HAVE_CONFIG_H
23 # include "gnashconfig.h"
24 #endif
25 
26 #include <boost/cstdint.hpp>
27 
28 #if defined (WIN32)
29 // Include for HANDLE
30 #include <windows.h>
31 #else
32 // key_t
33 #include <sys/types.h>
34 #endif
35 
36 #include "dsodefs.h" //For DSOEXPORT
37 
38 // Forward declarations
39 namespace gnash {
40  class fn_call;
41 }
42 
43 namespace gnash {
44 
45 class SharedMem
46 {
47 public:
48 
49  typedef boost::uint8_t* iterator;
50 
52  //
55  iterator begin() const {
56  return _addr;
57  }
58 
60  //
62  iterator end() const {
63  return _addr + _size;
64  }
65 
67  //
71  DSOEXPORT SharedMem(size_t size);
72 
75 
77  //
80  DSOEXPORT bool attach();
81 
83  class Lock
84  {
85  public:
86  Lock(const SharedMem& s) : _s(s), _locked(s.lock()) {}
87  ~Lock() { if (_locked) _s.unlock(); }
88  bool locked() const {
89  return _locked;
90  }
91  private:
92  const SharedMem& _s;
93  bool _locked;
94  };
95 
96 private:
97 
99  //
101  DSOEXPORT bool lock() const;
102 
104  //
106  DSOEXPORT bool unlock() const;
107 
108  iterator _addr;
109 
110  const size_t _size;
111 
112  // Semaphore ID.
113  int _semid;
114 
115  // Shared memory ID.
116  int _shmid;
117 
118 #if !defined(WIN32)
119  key_t _shmkey;
120 #else
121  long _shmkey;
122  HANDLE _shmhandle;
123 #endif
124 };
125 
127 //
132 inline bool
133 attached(const SharedMem& mem) {
134  return (mem.begin());
135 }
136 
137 } // end of gnash namespace
138 
139 #endif
140 
141 // Local Variables:
142 // mode: C++
143 // indent-tabs-mode: t
144 // End: