PageRenderTime 51ms CodeModel.GetById 12ms app.highlight 34ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/interprocess/windows_shared_memory.hpp

http://hadesmem.googlecode.com/
C++ Header | 232 lines | 137 code | 40 blank | 55 comment | 6 complexity | 5a848c5d436a7e0e50ce509c8b917ff8 MD5 | raw file
  1//////////////////////////////////////////////////////////////////////////////
  2//
  3// (C) Copyright Ion Gaztanaga 2005-2009. Distributed under the Boost
  4// Software License, Version 1.0. (See accompanying file
  5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6//
  7// See http://www.boost.org/libs/interprocess for documentation.
  8//
  9//////////////////////////////////////////////////////////////////////////////
 10
 11#ifndef BOOST_INTERPROCESS_WINDOWS_SHARED_MEMORY_HPP
 12#define BOOST_INTERPROCESS_WINDOWS_SHARED_MEMORY_HPP
 13
 14#include <boost/interprocess/detail/config_begin.hpp>
 15#include <boost/interprocess/detail/workaround.hpp>
 16#include <boost/detail/workaround.hpp>
 17#include <boost/interprocess/permissions.hpp>
 18
 19#if !defined(BOOST_INTERPROCESS_WINDOWS)
 20#error "This header can only be used in Windows operating systems"
 21#endif
 22
 23#include <boost/interprocess/creation_tags.hpp>
 24#include <boost/interprocess/exceptions.hpp>
 25#include <boost/interprocess/detail/utilities.hpp>
 26#include <boost/interprocess/detail/os_file_functions.hpp>
 27#include <boost/interprocess/interprocess_fwd.hpp>
 28#include <boost/interprocess/exceptions.hpp>
 29#include <boost/interprocess/detail/win32_api.hpp>
 30#include <cstddef>
 31#include <boost/cstdint.hpp>
 32#include <string>
 33
 34//!\file
 35//!Describes a class representing a native windows shared memory.
 36
 37namespace boost {
 38namespace interprocess {
 39
 40//!A class that wraps the native Windows shared memory
 41//!that is implemented as a file mapping of the paging file.
 42//!Unlike shared_memory_object, windows_shared_memory has
 43//!no kernel persistence and the shared memory is destroyed
 44//!when all processes destroy all their windows_shared_memory
 45//!objects and mapped regions for the same shared memory
 46//!or the processes end/crash.
 47//!
 48//!Warning: Windows native shared memory and interprocess portable
 49//!shared memory (boost::interprocess::shared_memory_object)
 50//!can't communicate between them.
 51class windows_shared_memory
 52{
 53   /// @cond
 54   //Non-copyable and non-assignable
 55   BOOST_INTERPROCESS_MOVABLE_BUT_NOT_COPYABLE(windows_shared_memory)
 56   /// @endcond
 57
 58   public:
 59   //!Default constructor.
 60   //!Represents an empty windows_shared_memory.
 61   windows_shared_memory();
 62
 63   //!Creates a new native shared memory with name "name" and mode "mode",
 64   //!with the access mode "mode".
 65   //!If the file previously exists, throws an error.
 66   windows_shared_memory(create_only_t, const char *name, mode_t mode, std::size_t size, const permissions& perm = permissions())
 67   {  this->priv_open_or_create(detail::DoCreate, name, mode, size, perm);  }
 68
 69   //!Tries to create a shared memory object with name "name" and mode "mode", with the
 70   //!access mode "mode". If the file previously exists, it tries to open it with mode "mode".
 71   //!Otherwise throws an error.
 72   windows_shared_memory(open_or_create_t, const char *name, mode_t mode, std::size_t size, const permissions& perm = permissions())
 73   {  this->priv_open_or_create(detail::DoOpenOrCreate, name, mode, size, perm);  }
 74
 75   //!Tries to open a shared memory object with name "name", with the access mode "mode". 
 76   //!If the file does not previously exist, it throws an error.
 77   windows_shared_memory(open_only_t, const char *name, mode_t mode)
 78   {  this->priv_open_or_create(detail::DoOpen, name, mode, 0, permissions());  }
 79
 80   //!Moves the ownership of "moved"'s shared memory object to *this. 
 81   //!After the call, "moved" does not represent any shared memory object. 
 82   //!Does not throw
 83   windows_shared_memory(BOOST_INTERPROCESS_RV_REF(windows_shared_memory) moved)
 84      : m_handle(0)
 85   {  this->swap(moved);   }
 86
 87   //!Moves the ownership of "moved"'s shared memory to *this.
 88   //!After the call, "moved" does not represent any shared memory. 
 89   //!Does not throw
 90   windows_shared_memory &operator=(BOOST_INTERPROCESS_RV_REF(windows_shared_memory) moved)
 91   {  
 92      windows_shared_memory tmp(boost::interprocess::move(moved));
 93      this->swap(tmp);
 94      return *this;  
 95   }
 96
 97   //!Swaps to shared_memory_objects. Does not throw
 98   void swap(windows_shared_memory &other);
 99
100   //!Destroys *this. All mapped regions are still valid after
101   //!destruction. When all mapped regions and windows_shared_memory
102   //!objects referring the shared memory are destroyed, the
103   //!operating system will destroy the shared memory.
104   ~windows_shared_memory();
105
106   //!Returns the name of the shared memory.
107   const char *get_name() const;
108
109   //!Returns access mode
110   mode_t get_mode() const;
111
112   //!Returns the mapping handle. Never throws
113   mapping_handle_t get_mapping_handle() const;
114
115   /// @cond
116   private:
117
118   //!Closes a previously opened file mapping. Never throws.
119   void priv_close();
120
121   //!Closes a previously opened file mapping. Never throws.
122   bool priv_open_or_create(detail::create_enum_t type, const char *filename, mode_t mode, std::size_t size, const permissions& perm = permissions());
123
124   void *         m_handle;
125   mode_t         m_mode;
126   std::string    m_name;
127   /// @endcond
128};
129
130/// @cond
131
132inline windows_shared_memory::windows_shared_memory() 
133   :  m_handle(0)
134{}
135
136inline windows_shared_memory::~windows_shared_memory() 
137{  this->priv_close(); }
138
139inline const char *windows_shared_memory::get_name() const
140{  return m_name.c_str(); }
141
142inline void windows_shared_memory::swap(windows_shared_memory &other)
143{  
144   std::swap(m_handle,  other.m_handle);
145   std::swap(m_mode,    other.m_mode);
146   m_name.swap(other.m_name);   
147}
148
149inline mapping_handle_t windows_shared_memory::get_mapping_handle() const
150{  mapping_handle_t mhnd = { m_handle, true};   return mhnd;   }
151
152inline mode_t windows_shared_memory::get_mode() const
153{  return m_mode; }
154
155inline bool windows_shared_memory::priv_open_or_create
156   (detail::create_enum_t type, const char *filename, mode_t mode, std::size_t size, const permissions& perm)
157{
158   m_name = filename ? filename : "";
159
160   unsigned long file_map_access = 0;
161   unsigned long map_access = 0;
162
163   switch(mode)
164   {
165      case read_only:
166         file_map_access   |= winapi::page_readonly;
167         map_access        |= winapi::file_map_read;
168      break;
169      case read_write:
170         file_map_access   |= winapi::page_readwrite;
171         map_access        |= winapi::file_map_write;
172      break;
173      case copy_on_write:
174         file_map_access   |= winapi::page_writecopy;
175         map_access        |= winapi::file_map_copy;
176      break;
177      default:
178         {
179            error_info err(mode_error);
180            throw interprocess_exception(err);
181         }
182      break;
183   }
184
185   switch(type){
186      case detail::DoOpen:
187         m_handle = winapi::open_file_mapping
188            (map_access, filename);
189      break;
190      case detail::DoCreate:
191      case detail::DoOpenOrCreate:
192      {
193         __int64 s = size;
194         unsigned long high_size(s >> 32), low_size((boost::uint32_t)s);
195         m_handle = winapi::create_file_mapping
196            ( winapi::invalid_handle_value, file_map_access, high_size, low_size, filename
197            , (winapi::interprocess_security_attributes*)perm.get_permissions());
198      }
199      break;
200      default:
201         {
202            error_info err = other_error;
203            throw interprocess_exception(err);
204         }
205   }
206
207   if(!m_handle || (type == detail::DoCreate && winapi::get_last_error() == winapi::error_already_exists)){
208      error_info err = system_error_code();
209      this->priv_close();
210      throw interprocess_exception(err);
211   }
212
213   m_mode = mode;
214   return true;
215}
216
217inline void windows_shared_memory::priv_close()
218{
219   if(m_handle){
220      winapi::close_handle(m_handle);
221      m_handle = 0;
222   }
223}
224
225///@endcond
226
227}  //namespace interprocess {
228}  //namespace boost {
229
230#include <boost/interprocess/detail/config_end.hpp>
231
232#endif   //BOOST_INTERPROCESS_WINDOWS_SHARED_MEMORY_HPP