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