1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-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_MANAGED_EXTERNAL_BUFFER_HPP
12 #define BOOST_INTERPROCESS_MANAGED_EXTERNAL_BUFFER_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 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/interprocess/detail/managed_memory_impl.hpp>
26 #include <boost/move/utility_core.hpp>
27 #include <boost/assert.hpp>
28 //These includes needed to fulfill default template parameters of
29 //predeclarations in interprocess_fwd.hpp
30 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
31 #include <boost/interprocess/sync/mutex_family.hpp>
32 #include <boost/interprocess/indexes/iset_index.hpp>
33 
34 //!\file
35 //!Describes a named user memory allocation user class.
36 
37 namespace boost {
38 namespace interprocess {
39 
40 //!A basic user memory named object creation class. Inherits all
41 //!basic functionality from
42 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
43 template
44       <
45          class CharType,
46          class AllocationAlgorithm,
47          template<class IndexConfig> class IndexType
48       >
49 class basic_managed_external_buffer
50    : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType>
51 {
52    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
53    typedef ipcdetail::basic_managed_memory_impl
54       <CharType, AllocationAlgorithm, IndexType>    base_t;
55    BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_external_buffer)
56    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
57 
58    public:
59    typedef typename base_t::size_type              size_type;
60 
61    //!Default constructor. Does nothing.
62    //!Useful in combination with move semantics
basic_managed_external_buffer()63    basic_managed_external_buffer()
64    {}
65 
66    //!Creates and places the segment manager. This can throw
basic_managed_external_buffer(create_only_t,void * addr,size_type size)67    basic_managed_external_buffer
68       (create_only_t, void *addr, size_type size)
69    {
70       //Check if alignment is correct
71       BOOST_ASSERT((0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u)))));
72       if(!base_t::create_impl(addr, size)){
73          throw interprocess_exception("Could not initialize buffer in basic_managed_external_buffer constructor");
74       }
75    }
76 
77    //!Creates and places the segment manager. This can throw
basic_managed_external_buffer(open_only_t,void * addr,size_type size)78    basic_managed_external_buffer
79       (open_only_t, void *addr, size_type size)
80    {
81       //Check if alignment is correct
82       BOOST_ASSERT((0 == (((std::size_t)addr) & (AllocationAlgorithm::Alignment - size_type(1u)))));
83       if(!base_t::open_impl(addr, size)){
84          throw interprocess_exception("Could not initialize buffer in basic_managed_external_buffer constructor");
85       }
86    }
87 
88    //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
basic_managed_external_buffer(BOOST_RV_REF (basic_managed_external_buffer)moved)89    basic_managed_external_buffer(BOOST_RV_REF(basic_managed_external_buffer) moved)
90    {
91       this->swap(moved);
92    }
93 
94    //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
operator =(BOOST_RV_REF (basic_managed_external_buffer)moved)95    basic_managed_external_buffer &operator=(BOOST_RV_REF(basic_managed_external_buffer) moved)
96    {
97       basic_managed_external_buffer tmp(boost::move(moved));
98       this->swap(tmp);
99       return *this;
100    }
101 
grow(size_type extra_bytes)102    void grow(size_type extra_bytes)
103    {  base_t::grow(extra_bytes);   }
104 
105    //!Swaps the ownership of the managed heap memories managed by *this and other.
106    //!Never throws.
swap(basic_managed_external_buffer & other)107    void swap(basic_managed_external_buffer &other)
108    {  base_t::swap(other); }
109 };
110 
111 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
112 
113 //!Typedef for a default basic_managed_external_buffer
114 //!of narrow characters
115 typedef basic_managed_external_buffer
116    <char
117    ,rbtree_best_fit<null_mutex_family>
118    ,iset_index>
119 managed_external_buffer;
120 
121 //!Typedef for a default basic_managed_external_buffer
122 //!of wide characters
123 typedef basic_managed_external_buffer
124    <wchar_t
125    ,rbtree_best_fit<null_mutex_family>
126    ,iset_index>
127 wmanaged_external_buffer;
128 
129 #endif   //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
130 
131 }  //namespace interprocess {
132 }  //namespace boost {
133 
134 #include <boost/interprocess/detail/config_end.hpp>
135 
136 #endif   //BOOST_INTERPROCESS_MANAGED_EXTERNAL_BUFFER_HPP
137 
138