2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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.
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
23 # include "gnashconfig.h"
26 #include <boost/cstdint.hpp>
28 #include <sys/types.h>
29 #if !defined(HAVE_WINSOCK_H) && !defined(__riscos__) && !defined(__OS2__) && !defined(__HAIKU__)
32 # include <linux/shm.h>
36 #elif !defined(__riscos__) && !defined(__OS2__) && !defined(__HAIKU__)
43 #include "dsodefs.h" //For DSOEXPORT
45 // Forward declarations
53 const int MAP_INHERIT
= 0;
55 #ifndef MAP_HASSEMAPHORE
56 const int MAP_HASSEMAPHORE
= 0;
59 const int MAX_SHM_NAME_SIZE
= 48;
65 typedef boost::uint8_t* iterator
;
67 /// The beginning of the SharedMem section.
69 /// This is only valid after attach() has returned true. You can check
70 /// with the function attached().
71 iterator
begin() const {
75 /// The end of the SharedMem section.
77 /// This is only valid after attach() has returned true.
78 iterator
end() const {
82 /// Construct a SharedMem with the requested size.
84 /// @param size The size of the shared memory section. If successfully
85 /// created, the segment will be exactly this size and
87 DSOEXPORT
SharedMem(size_t size
);
90 DSOEXPORT
~SharedMem();
92 /// Initialize the shared memory segment
94 /// This is called by LocalConnection when either connect() or send()
96 DSOEXPORT
bool attach();
98 /// Use to get a scoped semaphore lock on the shared memory.
102 Lock(SharedMem
& s
) : _s(s
), _locked(s
.lock()) {}
103 ~Lock() { if (_locked
) _s
.unlock(); }
104 bool locked() const {
114 /// Get a semaphore lock if possible
116 /// @return true if successful, false if not.
117 DSOEXPORT
bool lock();
119 /// Release a semaphore lock if possible
121 /// @return true if successful, false if not.
122 DSOEXPORT
bool unlock();
134 #if !defined(HAVE_WINSOCK_H) || defined(__OS2__)
142 /// Check if the SharedMem has been attached.
144 /// This only checks whether the attach operation was successful, not whether
145 /// the shared memory still exists and is still attached where it was
146 /// initially. It is always possible for other processes to remove it while
147 /// Gnash is using it, but there is nothing we can do about this.
149 attached(const SharedMem
& mem
) {
150 return (mem
.begin());
153 } // end of gnash namespace
159 // indent-tabs-mode: t