PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 189 lines | 111 code | 31 blank | 47 comment | 3 complexity | 36ee7201a87caeb00bba5e2499f160e0 MD5 | raw file
  1//////////////////////////////////////////////////////////////////////////////
  2//
  3// (C) Copyright Ion Gaztanaga 2008-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_XSI_SHARED_MEMORY_HPP
 12#define BOOST_INTERPROCESS_MANAGED_XSI_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
 21#if !defined(BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS)
 22#error "This header can't be used in operating systems without XSI (System V) shared memory support"
 23#endif
 24
 25#include <boost/interprocess/detail/managed_memory_impl.hpp>
 26#include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
 27#include <boost/interprocess/detail/xsi_shared_memory_file_wrapper.hpp>
 28#include <boost/interprocess/creation_tags.hpp>
 29
 30namespace boost {
 31
 32namespace interprocess {
 33
 34//!A basic X/Open System Interface (XSI) shared memory named object creation class. Initializes the 
 35//!shared memory segment. Inherits all basic functionality from 
 36//!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
 37template
 38      <
 39         class CharType, 
 40         class AllocationAlgorithm, 
 41         template<class IndexConfig> class IndexType
 42      >
 43class basic_managed_xsi_shared_memory 
 44   : public detail::basic_managed_memory_impl
 45      <CharType, AllocationAlgorithm, IndexType
 46      ,detail::managed_open_or_create_impl<xsi_shared_memory_file_wrapper, false, true>::ManagedOpenOrCreateUserOffset>
 47   , private detail::managed_open_or_create_impl<xsi_shared_memory_file_wrapper, false, true>
 48{
 49   /// @cond
 50   public:
 51   typedef xsi_shared_memory_file_wrapper device_type;
 52
 53   public:
 54   typedef detail::managed_open_or_create_impl
 55      <xsi_shared_memory_file_wrapper, false, true>            base2_t;
 56   typedef detail::basic_managed_memory_impl 
 57      <CharType, AllocationAlgorithm, IndexType,
 58      base2_t::ManagedOpenOrCreateUserOffset>   base_t;
 59
 60   typedef detail::create_open_func<base_t>        create_open_func_t;
 61
 62   basic_managed_xsi_shared_memory *get_this_pointer()
 63   {  return this;   }
 64
 65   private:
 66   typedef typename base_t::char_ptr_holder_t   char_ptr_holder_t;
 67   BOOST_INTERPROCESS_MOVABLE_BUT_NOT_COPYABLE(basic_managed_xsi_shared_memory)
 68   /// @endcond
 69
 70   public: //functions
 71
 72   //!Destroys *this and indicates that the calling process is finished using
 73   //!the resource. The destructor function will deallocate
 74   //!any system resources allocated by the system for use by this process for
 75   //!this resource. The resource can still be opened again calling
 76   //!the open constructor overload. To erase the resource from the system
 77   //!use remove().
 78   ~basic_managed_xsi_shared_memory()
 79   {}
 80
 81   //!Default constructor. Does nothing.
 82   //!Useful in combination with move semantics
 83   basic_managed_xsi_shared_memory()
 84   {}
 85
 86   //!Creates shared memory and creates and places the segment manager. 
 87   //!This can throw.
 88   basic_managed_xsi_shared_memory(create_only_t create_only, const xsi_key &key,
 89                             std::size_t size, const void *addr = 0, const permissions& perm = permissions())
 90      : base_t()
 91      , base2_t(create_only, key, size, read_write, addr, 
 92                create_open_func_t(get_this_pointer(), detail::DoCreate), perm)
 93   {}
 94
 95   //!Creates shared memory and creates and places the segment manager if
 96   //!segment was not created. If segment was created it connects to the
 97   //!segment.
 98   //!This can throw.
 99   basic_managed_xsi_shared_memory (open_or_create_t open_or_create,
100                              const xsi_key &key, std::size_t size, 
101                              const void *addr = 0, const permissions& perm = permissions())
102      : base_t()
103      , base2_t(open_or_create, key, size, read_write, addr, 
104                create_open_func_t(get_this_pointer(), 
105                detail::DoOpenOrCreate), perm)
106   {}
107
108   //!Connects to a created shared memory and its segment manager.
109   //!in read-only mode.
110   //!This can throw.
111   basic_managed_xsi_shared_memory (open_read_only_t, const xsi_key &key, 
112                                const void *addr = 0)
113      : base_t()
114      , base2_t(open_only, key, read_only, addr, 
115                create_open_func_t(get_this_pointer(), 
116                detail::DoOpen))
117   {}
118
119   //!Connects to a created shared memory and its segment manager.
120   //!This can throw.
121   basic_managed_xsi_shared_memory (open_only_t open_only, const xsi_key &key, 
122                                const void *addr = 0)
123      : base_t()
124      , base2_t(open_only, key, read_write, addr, 
125                create_open_func_t(get_this_pointer(), 
126                detail::DoOpen))
127   {}
128
129   //!Moves the ownership of "moved"'s managed memory to *this.
130   //!Does not throw
131   basic_managed_xsi_shared_memory(BOOST_INTERPROCESS_RV_REF(basic_managed_xsi_shared_memory) moved)
132   {
133      basic_managed_xsi_shared_memory tmp;
134      this->swap(moved);
135      tmp.swap(moved);
136   }
137
138   //!Moves the ownership of "moved"'s managed memory to *this.
139   //!Does not throw
140   basic_managed_xsi_shared_memory &operator=(BOOST_INTERPROCESS_RV_REF(basic_managed_xsi_shared_memory) moved)
141   {
142      basic_managed_xsi_shared_memory tmp(boost::interprocess::move(moved));
143      this->swap(tmp);
144      return *this;
145   }
146
147   //!Swaps the ownership of the managed shared memories managed by *this and other.
148   //!Never throws.
149   void swap(basic_managed_xsi_shared_memory &other)
150   {
151      base_t::swap(other);
152      base2_t::swap(other);
153   }
154
155   //!Erases a XSI shared memory object identified by shmid
156   //!from the system.
157   //!Returns false on error. Never throws
158   static bool remove(int shmid)
159   {  return device_type::remove(shmid); }
160
161   int get_shmid() const
162   {  return base2_t::get_device().get_shmid(); }
163
164   /// @cond
165
166   //!Tries to find a previous named allocation address. Returns a memory
167   //!buffer and the object count. If not found returned pointer is 0.
168   //!Never throws.
169   template <class T>
170   std::pair<T*, std::size_t> find  (char_ptr_holder_t name)
171   {
172      if(base2_t::get_mapped_region().get_mode() == read_only){
173         return base_t::template find_no_lock<T>(name);
174      }
175      else{
176         return base_t::template find<T>(name);
177      }
178   }
179
180   /// @endcond
181};
182
183}  //namespace interprocess {
184}  //namespace boost {
185
186#include <boost/interprocess/detail/config_end.hpp>
187
188#endif   //BOOST_INTERPROCESS_MANAGED_XSI_SHARED_MEMORY_HPP
189