PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 206 lines | 119 code | 29 blank | 58 comment | 3 complexity | 05ee7ac4b106e2212ead8615ab517190 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_MAPPED_FILE_HPP
 12#define BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_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/detail/file_wrapper.hpp>
 24#include <boost/interprocess/detail/move.hpp>
 25#include <boost/interprocess/file_mapping.hpp>
 26#include <boost/interprocess/permissions.hpp>
 27
 28namespace boost {
 29namespace interprocess {
 30
 31//!A basic mapped file named object creation class. Initializes the 
 32//!mapped file. Inherits all basic functionality from 
 33//!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>
 34template
 35      <
 36         class CharType, 
 37         class AllocationAlgorithm, 
 38         template<class IndexConfig> class IndexType
 39      >
 40class basic_managed_mapped_file 
 41   : public detail::basic_managed_memory_impl
 42      <CharType, AllocationAlgorithm, IndexType
 43      ,detail::managed_open_or_create_impl<detail::file_wrapper>::ManagedOpenOrCreateUserOffset>
 44{
 45   /// @cond
 46   public:
 47   typedef detail::basic_managed_memory_impl 
 48      <CharType, AllocationAlgorithm, IndexType,
 49      detail::managed_open_or_create_impl<detail::file_wrapper>::ManagedOpenOrCreateUserOffset>   base_t;
 50   typedef detail::file_wrapper device_type;
 51
 52   private:
 53
 54   typedef detail::create_open_func<base_t>        create_open_func_t;   
 55   typedef detail::managed_open_or_create_impl<detail::file_wrapper> managed_open_or_create_type;
 56
 57   basic_managed_mapped_file *get_this_pointer()
 58   {  return this;   }
 59
 60   private:
 61   typedef typename base_t::char_ptr_holder_t   char_ptr_holder_t;
 62   BOOST_INTERPROCESS_MOVABLE_BUT_NOT_COPYABLE(basic_managed_mapped_file)
 63   /// @endcond
 64
 65   public: //functions
 66
 67   //!Creates mapped file and creates and places the segment manager. 
 68   //!This can throw.
 69   basic_managed_mapped_file()
 70   {}
 71
 72   //!Creates mapped file and creates and places the segment manager. 
 73   //!This can throw.
 74   basic_managed_mapped_file(create_only_t create_only, const char *name,
 75                             std::size_t size, const void *addr = 0, const permissions &perm = permissions())
 76      : m_mfile(create_only, name, size, read_write, addr, 
 77                create_open_func_t(get_this_pointer(), detail::DoCreate), perm)
 78   {}
 79
 80   //!Creates mapped file and creates and places the segment manager if
 81   //!segment was not created. If segment was created it connects to the
 82   //!segment.
 83   //!This can throw.
 84   basic_managed_mapped_file (open_or_create_t open_or_create,
 85                              const char *name, std::size_t size, 
 86                              const void *addr = 0, const permissions &perm = permissions())
 87      : m_mfile(open_or_create, name, size, read_write, addr,
 88                create_open_func_t(get_this_pointer(), 
 89                detail::DoOpenOrCreate), perm)
 90   {}
 91
 92   //!Connects to a created mapped file and its segment manager.
 93   //!This can throw.
 94   basic_managed_mapped_file (open_only_t open_only, const char* name, 
 95                              const void *addr = 0)
 96      : m_mfile(open_only, name, read_write, addr,
 97                create_open_func_t(get_this_pointer(), 
 98                detail::DoOpen))
 99   {}
100
101   //!Connects to a created mapped file and its segment manager
102   //!in copy_on_write mode.
103   //!This can throw.
104   basic_managed_mapped_file (open_copy_on_write_t, const char* name, 
105                              const void *addr = 0)
106      : m_mfile(open_only, name, copy_on_write, addr, 
107                create_open_func_t(get_this_pointer(), 
108                detail::DoOpen))
109   {}
110
111   //!Connects to a created mapped file and its segment manager
112   //!in read-only mode.
113   //!This can throw.
114   basic_managed_mapped_file (open_read_only_t, const char* name, 
115                              const void *addr = 0)
116      : m_mfile(open_only, name, read_only, addr, 
117                create_open_func_t(get_this_pointer(), 
118                detail::DoOpen))
119   {}
120
121   //!Moves the ownership of "moved"'s managed memory to *this.
122   //!Does not throw
123   basic_managed_mapped_file(BOOST_INTERPROCESS_RV_REF(basic_managed_mapped_file) moved)
124   {
125      this->swap(moved);
126   }
127
128   //!Moves the ownership of "moved"'s managed memory to *this.
129   //!Does not throw
130   basic_managed_mapped_file &operator=(BOOST_INTERPROCESS_RV_REF(basic_managed_mapped_file) moved)
131   {
132      basic_managed_mapped_file tmp(boost::interprocess::move(moved));
133      this->swap(tmp);
134      return *this;
135   }
136
137   //!Destroys *this and indicates that the calling process is finished using
138   //!the resource. The destructor function will deallocate
139   //!any system resources allocated by the system for use by this process for
140   //!this resource. The resource can still be opened again calling
141   //!the open constructor overload. To erase the resource from the system
142   //!use remove().
143   ~basic_managed_mapped_file()
144   {}
145
146   //!Swaps the ownership of the managed mapped memories managed by *this and other.
147   //!Never throws.
148   void swap(basic_managed_mapped_file &other)
149   {
150      base_t::swap(other);
151      m_mfile.swap(other.m_mfile);
152   }
153
154   //!Flushes cached data to file.
155   //!Never throws
156   bool flush()
157   {  return m_mfile.flush();  }
158
159   //!Tries to resize mapped file so that we have room for 
160   //!more objects. 
161   //!
162   //!This function is not synchronized so no other thread or process should
163   //!be reading or writing the file
164   static bool grow(const char *filename, std::size_t extra_bytes)
165   {
166      return base_t::template grow
167         <basic_managed_mapped_file>(filename, extra_bytes);
168   }
169
170   //!Tries to resize mapped file to minimized the size of the file.
171   //!
172   //!This function is not synchronized so no other thread or process should
173   //!be reading or writing the file
174   static bool shrink_to_fit(const char *filename)
175   {
176      return base_t::template shrink_to_fit
177         <basic_managed_mapped_file>(filename);
178   }
179
180   /// @cond
181
182   //!Tries to find a previous named allocation address. Returns a memory
183   //!buffer and the object count. If not found returned pointer is 0.
184   //!Never throws.
185   template <class T>
186   std::pair<T*, std::size_t> find  (char_ptr_holder_t name)
187   {
188      if(m_mfile.get_mapped_region().get_mode() == read_only){
189         return base_t::template find_no_lock<T>(name);
190      }
191      else{
192         return base_t::template find<T>(name);
193      }
194   }
195
196   private:
197   managed_open_or_create_type m_mfile;
198   /// @endcond
199};
200
201}  //namespace interprocess {
202}  //namespace boost {
203
204#include <boost/interprocess/detail/config_end.hpp>
205
206#endif   //BOOST_INTERPROCESS_MANAGED_MAPPED_FILE_HPP