Gnash  0.8.10
SharedMem.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   Free Software Foundation, Inc
00004 // 
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation; either version 3 of the License, or
00008 // (at your option) any later version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019 #ifndef GNASH_SHM_H
00020 #define GNASH_SHM_H
00021 
00022 #ifdef HAVE_CONFIG_H
00023 # include "gnashconfig.h"
00024 #endif
00025 
00026 #include <boost/cstdint.hpp>
00027 
00028 #if defined (WIN32)
00029 // Include for HANDLE
00030 #include <windows.h>
00031 #else
00032 // key_t
00033 #include <sys/types.h>
00034 #endif
00035 
00036 #include "dsodefs.h" //For DSOEXPORT
00037 
00038 // Forward declarations
00039 namespace gnash {
00040         class fn_call;
00041 }
00042 
00043 namespace gnash {
00044 
00045 class SharedMem
00046 {
00047 public:
00048 
00049     typedef boost::uint8_t* iterator;
00050 
00052     //
00055     iterator begin() const {
00056         return _addr;
00057     }
00058 
00060     //
00062     iterator end() const {
00063         return _addr + _size;
00064     }
00065 
00067     //
00071     DSOEXPORT SharedMem(size_t size);
00072 
00074     DSOEXPORT ~SharedMem();
00075     
00077     //
00080     DSOEXPORT bool attach();
00081 
00083     class Lock
00084     {
00085     public:
00086         Lock(const SharedMem& s) : _s(s), _locked(s.lock()) {}
00087         ~Lock() { if (_locked) _s.unlock(); }
00088         bool locked() const {
00089             return _locked;
00090         }
00091     private:
00092         const SharedMem& _s;
00093         bool _locked;
00094     };
00095 
00096 private:
00097     
00099     //
00101     DSOEXPORT bool lock() const;
00102     
00104     //
00106     DSOEXPORT bool unlock() const;
00107 
00108     iterator _addr;
00109 
00110     const size_t _size;
00111 
00112     // Semaphore ID.
00113     int _semid;
00114 
00115     // Shared memory ID.
00116     int _shmid;
00117 
00118 #if !defined(WIN32) 
00119     key_t _shmkey;
00120 #else
00121     long _shmkey;
00122     HANDLE _shmhandle;
00123 #endif
00124 };
00125 
00127 //
00132 inline bool
00133 attached(const SharedMem& mem) {
00134     return (mem.begin());
00135 }
00136 
00137 } // end of gnash namespace
00138 
00139 #endif
00140 
00141 // Local Variables:
00142 // mode: C++
00143 // indent-tabs-mode: t
00144 // End: