Avoid appending null byte to RTMP method name.
[gnash.git] / libbase / SharedMem.h
blob6218c839db2f34c1bfdd619db7a185fe6ab8df55
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // 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
19 #ifndef GNASH_SHM_H
20 #define GNASH_SHM_H
22 #ifdef HAVE_CONFIG_H
23 # include "gnashconfig.h"
24 #endif
26 #include <boost/cstdint.hpp>
28 #include <sys/types.h>
29 #if !defined(HAVE_WINSOCK_H) && !defined(__riscos__) && !defined(__OS2__) && !defined(__HAIKU__)
30 # include <sys/ipc.h>
31 #ifdef ANDROID
32 # include <linux/shm.h>
33 #else
34 # include <sys/shm.h>
35 #endif
36 #elif !defined(__riscos__) && !defined(__OS2__) && !defined(__HAIKU__)
37 # include <windows.h>
38 # include <process.h>
39 # include <fcntl.h>
40 # include <io.h>
41 #endif
43 #include "dsodefs.h" //For DSOEXPORT
45 // Forward declarations
46 namespace gnash {
47 class fn_call;
50 namespace gnash {
52 #ifndef MAP_INHERIT
53 const int MAP_INHERIT = 0;
54 #endif
55 #ifndef MAP_HASSEMAPHORE
56 const int MAP_HASSEMAPHORE = 0;
57 #endif
59 const int MAX_SHM_NAME_SIZE = 48;
61 class SharedMem
63 public:
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 {
72 return _addr;
75 /// The end of the SharedMem section.
77 /// This is only valid after attach() has returned true.
78 iterator end() const {
79 return _addr + _size;
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
86 /// is not resizable.
87 DSOEXPORT SharedMem(size_t size);
89 /// Destructor.
90 DSOEXPORT ~SharedMem();
92 /// Initialize the shared memory segment
94 /// This is called by LocalConnection when either connect() or send()
95 /// is called.
96 DSOEXPORT bool attach();
98 /// Use to get a scoped semaphore lock on the shared memory.
99 class Lock
101 public:
102 Lock(SharedMem& s) : _s(s), _locked(s.lock()) {}
103 ~Lock() { if (_locked) _s.unlock(); }
104 bool locked() const {
105 return _locked;
107 private:
108 SharedMem& _s;
109 bool _locked;
112 private:
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();
124 iterator _addr;
126 const size_t _size;
128 // Semaphore ID.
129 int _semid;
131 // Shared memory ID.
132 int _shmid;
134 #if !defined(HAVE_WINSOCK_H) || defined(__OS2__)
135 key_t _shmkey;
136 #else
137 long _shmkey;
138 HANDLE _shmhandle;
139 #endif
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.
148 inline bool
149 attached(const SharedMem& mem) {
150 return (mem.begin());
153 } // end of gnash namespace
155 #endif
157 // Local Variables:
158 // mode: C++
159 // indent-tabs-mode: t
160 // End: