1 ////////////////////////////////////////////////////////////////////////////// 2 // 3 // (C) Copyright Ion Gaztanaga 2009-2012. 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_BASIC_GLOBAL_MEMORY_HPP 12 #define BOOST_INTERPROCESS_BASIC_GLOBAL_MEMORY_HPP 13 14 #ifndef BOOST_CONFIG_HPP 15 # include <boost/config.hpp> 16 #endif 17 # 18 #if defined(BOOST_HAS_PRAGMA_ONCE) 19 #pragma once 20 #endif 21 22 #include <boost/interprocess/detail/config_begin.hpp> 23 #include <boost/interprocess/detail/workaround.hpp> 24 25 #include <boost/interprocess/offset_ptr.hpp> 26 #include <boost/interprocess/sync/spin/mutex.hpp> 27 #include <boost/interprocess/sync/spin/recursive_mutex.hpp> 28 #include <boost/interprocess/detail/managed_memory_impl.hpp> 29 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp> 30 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp> 31 #include <boost/interprocess/indexes/iset_index.hpp> 32 #include <boost/interprocess/creation_tags.hpp> 33 #include <boost/interprocess/permissions.hpp> 34 35 namespace boost{ 36 namespace interprocess{ 37 namespace ipcdetail{ 38 39 struct intermodule_singleton_mutex_family 40 { 41 typedef boost::interprocess::ipcdetail::spin_mutex mutex_type; 42 typedef boost::interprocess::ipcdetail::spin_recursive_mutex recursive_mutex_type; 43 }; 44 45 struct intermodule_types 46 { 47 //We must use offset_ptr since a loaded DLL can map the singleton holder shared memory 48 //at a different address than other DLLs or the main executable 49 typedef rbtree_best_fit<intermodule_singleton_mutex_family, offset_ptr<void> > mem_algo; 50 template<class Device, bool FileBased> 51 struct open_or_create 52 { 53 typedef managed_open_or_create_impl 54 <Device, mem_algo::Alignment, FileBased, false> type; 55 }; 56 }; 57 58 //we must implement our own managed shared memory to avoid circular dependencies 59 template<class Device, bool FileBased> 60 class basic_managed_global_memory 61 : public basic_managed_memory_impl 62 < char 63 , intermodule_types::mem_algo 64 , iset_index 65 , intermodule_types::open_or_create<Device, FileBased>::type::ManagedOpenOrCreateUserOffset 66 > 67 , private intermodule_types::open_or_create<Device, FileBased>::type 68 { 69 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 70 typedef typename intermodule_types::template open_or_create<Device, FileBased>::type base2_t; 71 72 typedef basic_managed_memory_impl 73 < char 74 , intermodule_types::mem_algo 75 , iset_index 76 , base2_t::ManagedOpenOrCreateUserOffset 77 > base_t; 78 79 typedef create_open_func<base_t> create_open_func_t; 80 get_this_pointer()81 basic_managed_global_memory *get_this_pointer() 82 { return this; } 83 84 public: 85 typedef typename base_t::size_type size_type; 86 87 private: 88 typedef typename base_t::char_ptr_holder_t char_ptr_holder_t; 89 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_global_memory) 90 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 91 92 public: //functions 93 basic_managed_global_memory(open_or_create_t open_or_create,const char * name,size_type size,const void * addr=0,const permissions & perm=permissions ())94 basic_managed_global_memory (open_or_create_t open_or_create, 95 const char *name, size_type size, 96 const void *addr = 0, const permissions& perm = permissions()) 97 : base_t() 98 , base2_t(open_or_create, name, size, read_write, addr, 99 create_open_func_t(get_this_pointer(), 100 DoOpenOrCreate), perm) 101 {} 102 basic_managed_global_memory(open_only_t open_only,const char * name,const void * addr=0)103 basic_managed_global_memory (open_only_t open_only, const char* name, 104 const void *addr = 0) 105 : base_t() 106 , base2_t(open_only, name, read_write, addr, 107 create_open_func_t(get_this_pointer(), 108 DoOpen)) 109 {} 110 }; 111 112 113 } //namespace ipcdetail{ 114 } //namespace interprocess{ 115 } //namespace boost{ 116 117 #include <boost/interprocess/detail/config_end.hpp> 118 119 #endif //#ifndef BOOST_INTERPROCESS_BASIC_GLOBAL_MEMORY_HPP 120