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