PageRenderTime 49ms CodeModel.GetById 37ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 182 lines | 104 code | 24 blank | 54 comment | 3 complexity | 9be06d321e17341a05b4160d2a43924e 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_MANAGED_WINDOWS_SHARED_MEMORY_HPP
 12#define BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP
 13
 14#if (defined _MSC_VER) && (_MSC_VER >= 1200)
 15#  pragma once
 16#endif
 17
 18#include <boost/interprocess/detail/config_begin.hpp>
 19#include <boost/interprocess/detail/workaround.hpp>
 20#include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
 21#include <boost/interprocess/detail/managed_memory_impl.hpp>
 22#include <boost/interprocess/creation_tags.hpp>
 23#include <boost/interprocess/windows_shared_memory.hpp>
 24#include <boost/interprocess/permissions.hpp>
 25#include <boost/interprocess/detail/move.hpp>
 26
 27namespace boost {
 28namespace interprocess {
 29
 30//!A basic managed windows shared memory creation class. Initializes the 
 31//!shared memory segment. Inherits all basic functionality from 
 32//!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
 33//!Unlike basic_managed_shared_memory, it has
 34//!no kernel persistence and the shared memory is destroyed
 35//!when all processes destroy all their windows_shared_memory
 36//!objects and mapped regions for the same shared memory
 37//!or the processes end/crash.
 38//!
 39//!Warning: basic_managed_windows_shared_memory and
 40//!basic_managed_shared_memory can't communicate between them.
 41template
 42      <
 43         class CharType, 
 44         class AllocationAlgorithm, 
 45         template<class IndexConfig> class IndexType
 46      >
 47class basic_managed_windows_shared_memory 
 48   : public detail::basic_managed_memory_impl
 49      <CharType, AllocationAlgorithm, IndexType
 50      ,detail::managed_open_or_create_impl<windows_shared_memory>::ManagedOpenOrCreateUserOffset>
 51{
 52   /// @cond
 53   private:
 54   typedef detail::basic_managed_memory_impl 
 55      <CharType, AllocationAlgorithm, IndexType,
 56      detail::managed_open_or_create_impl<windows_shared_memory>::ManagedOpenOrCreateUserOffset>   base_t;
 57   typedef detail::create_open_func<base_t>        create_open_func_t;
 58
 59   basic_managed_windows_shared_memory *get_this_pointer()
 60   {  return this;   }
 61
 62   private:
 63   typedef typename base_t::char_ptr_holder_t   char_ptr_holder_t;
 64   BOOST_INTERPROCESS_MOVABLE_BUT_NOT_COPYABLE(basic_managed_windows_shared_memory)
 65   /// @endcond
 66
 67   public: //functions
 68
 69   //!Default constructor. Does nothing.
 70   //!Useful in combination with move semantics
 71   basic_managed_windows_shared_memory()
 72   {}
 73
 74   //!Creates shared memory and creates and places the segment manager. 
 75   //!This can throw.
 76   basic_managed_windows_shared_memory
 77      (create_only_t create_only, const char *name,
 78       std::size_t size, const void *addr = 0, const permissions &perm = permissions())
 79      : m_wshm(create_only, name, size, read_write, addr, 
 80                create_open_func_t(get_this_pointer(), detail::DoCreate), perm)
 81   {}
 82
 83   //!Creates shared memory and creates and places the segment manager if
 84   //!segment was not created. If segment was created it connects to the
 85   //!segment.
 86   //!This can throw.
 87   basic_managed_windows_shared_memory
 88      (open_or_create_t open_or_create,
 89      const char *name, std::size_t size, 
 90      const void *addr = 0,
 91      const permissions &perm = permissions())
 92      : m_wshm(open_or_create, name, size, read_write, addr, 
 93                create_open_func_t(get_this_pointer(), 
 94                detail::DoOpenOrCreate), perm)
 95   {}
 96
 97   //!Connects to a created shared memory and its segment manager.
 98   //!This can throw.
 99   basic_managed_windows_shared_memory
100      (open_only_t open_only, const char* name, const void *addr = 0)
101      : m_wshm(open_only, name, read_write, addr, 
102                create_open_func_t(get_this_pointer(), 
103                detail::DoOpen))
104   {}
105
106   //!Connects to a created shared memory and its segment manager
107   //!in copy_on_write mode.
108   //!This can throw.
109   basic_managed_windows_shared_memory
110      (open_copy_on_write_t, const char* name, const void *addr = 0)
111      : m_wshm(open_only, name, copy_on_write, addr, 
112                create_open_func_t(get_this_pointer(), detail::DoOpen))
113   {}
114
115   //!Connects to a created shared memory and its segment manager
116   //!in read-only mode.
117   //!This can throw.
118   basic_managed_windows_shared_memory
119      (open_read_only_t, const char* name, const void *addr = 0)
120      : base_t()
121      , m_wshm(open_only, name, read_only, addr, 
122                create_open_func_t(get_this_pointer(), detail::DoOpen))
123   {}
124
125   //!Moves the ownership of "moved"'s managed memory to *this.
126   //!Does not throw
127   basic_managed_windows_shared_memory
128      (BOOST_INTERPROCESS_RV_REF(basic_managed_windows_shared_memory) moved)
129   {  this->swap(moved);   }
130
131   //!Moves the ownership of "moved"'s managed memory to *this.
132   //!Does not throw
133   basic_managed_windows_shared_memory &operator=(BOOST_INTERPROCESS_RV_REF(basic_managed_windows_shared_memory) moved)
134   {
135      basic_managed_windows_shared_memory tmp(boost::interprocess::move(moved));
136      this->swap(tmp);
137      return *this;
138   }
139
140   //!Destroys *this and indicates that the calling process is finished using
141   //!the resource. All mapped regions are still valid after
142   //!destruction. When all mapped regions and basic_managed_windows_shared_memory
143   //!objects referring the shared memory are destroyed, the
144   //!operating system will destroy the shared memory.
145   ~basic_managed_windows_shared_memory()
146   {}
147
148   //!Swaps the ownership of the managed mapped memories managed by *this and other.
149   //!Never throws.
150   void swap(basic_managed_windows_shared_memory &other)
151   {
152      base_t::swap(other);
153      m_wshm.swap(other.m_wshm);
154   }
155
156   /// @cond
157
158   //!Tries to find a previous named allocation address. Returns a memory
159   //!buffer and the object count. If not found returned pointer is 0.
160   //!Never throws.
161   template <class T>
162   std::pair<T*, std::size_t> find  (char_ptr_holder_t name)
163   {
164      if(m_wshm.get_mapped_region().get_mode() == read_only){
165         return base_t::template find_no_lock<T>(name);
166      }
167      else{
168         return base_t::template find<T>(name);
169      }
170   }
171
172   private:
173   detail::managed_open_or_create_impl<windows_shared_memory, false> m_wshm;
174   /// @endcond
175};
176
177}  //namespace interprocess {
178}  //namespace boost {
179
180#include <boost/interprocess/detail/config_end.hpp>
181
182#endif   //BOOST_INTERPROCESS_MANAGED_WINDOWS_SHARED_MEMORY_HPP