Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / managed_heap_memory.hpp
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_HEAP_MEMORY_HPP
12 #define BOOST_INTERPROCESS_MANAGED_HEAP_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 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/move/utility_core.hpp>
22 #include <vector>
23 #include <boost/interprocess/detail/managed_memory_impl.hpp>
24 #include <boost/detail/no_exceptions_support.hpp>
25 //These includes needed to fulfill default template parameters of
26 //predeclarations in interprocess_fwd.hpp
27 #include <boost/interprocess/mem_algo/rbtree_best_fit.hpp>
28 #include <boost/interprocess/sync/mutex_family.hpp>
29 #include <boost/interprocess/indexes/iset_index.hpp>
30
31 //!\file
32 //!Describes a named heap memory allocation user class.
33
34 namespace boost {
35 namespace interprocess {
36
37 //!A basic heap memory named object creation class. Initializes the
38 //!heap memory segment. Inherits all basic functionality from
39 //!basic_managed_memory_impl<CharType, AllocationAlgorithm, IndexType>*/
40 template
41       <
42          class CharType,
43          class AllocationAlgorithm,
44          template<class IndexConfig> class IndexType
45       >
46 class basic_managed_heap_memory
47    : public ipcdetail::basic_managed_memory_impl <CharType, AllocationAlgorithm, IndexType>
48 {
49    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
50    private:
51
52    typedef ipcdetail::basic_managed_memory_impl
53       <CharType, AllocationAlgorithm, IndexType>             base_t;
54    BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_managed_heap_memory)
55    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
56
57    public: //functions
58    typedef typename base_t::size_type              size_type;
59
60    //!Default constructor. Does nothing.
61    //!Useful in combination with move semantics
62    basic_managed_heap_memory(){}
63
64    //!Destructor. Liberates the heap memory holding the managed data.
65    //!Never throws.
66    ~basic_managed_heap_memory()
67    {  this->priv_close();  }
68
69    //!Creates heap memory and initializes the segment manager.
70    //!This can throw.
71    basic_managed_heap_memory(size_type size)
72       :  m_heapmem(size, char(0))
73    {
74       if(!base_t::create_impl(&m_heapmem[0], size)){
75          this->priv_close();
76          throw interprocess_exception("Could not initialize heap in basic_managed_heap_memory constructor");
77       }
78    }
79
80    //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
81    basic_managed_heap_memory(BOOST_RV_REF(basic_managed_heap_memory) moved)
82    {  this->swap(moved);   }
83
84    //!Moves the ownership of "moved"'s managed memory to *this. Does not throw
85    basic_managed_heap_memory &operator=(BOOST_RV_REF(basic_managed_heap_memory) moved)
86    {
87       basic_managed_heap_memory tmp(boost::move(moved));
88       this->swap(tmp);
89       return *this;
90    }
91
92    //!Tries to resize internal heap memory so that
93    //!we have room for more objects.
94    //!WARNING: If memory is reallocated, all the objects will
95    //!be binary-copied to the new buffer. To be able to use
96    //!this function, all pointers constructed in this buffer
97    //!must be offset pointers. Otherwise, the result is undefined.
98    //!Returns true if the growth has been successful, so you will
99    //!have some extra bytes to allocate new objects. If returns
100    //!false, the heap allocation has failed.
101    bool grow(size_type extra_bytes)
102    {
103       //If memory is reallocated, data will
104       //be automatically copied
105       BOOST_TRY{
106         m_heapmem.resize(m_heapmem.size()+extra_bytes);
107       }
108       BOOST_CATCH(...){
109          return false;
110       }
111       BOOST_CATCH_END
112
113       //Grow always works
114       base_t::close_impl();
115       base_t::open_impl(&m_heapmem[0], m_heapmem.size());
116       base_t::grow(extra_bytes);
117       return true;
118    }
119
120    //!Swaps the ownership of the managed heap memories managed by *this and other.
121    //!Never throws.
122    void swap(basic_managed_heap_memory &other)
123    {
124       base_t::swap(other);
125       m_heapmem.swap(other.m_heapmem);
126    }
127
128    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
129    private:
130    //!Frees resources. Never throws.
131    void priv_close()
132    {
133       base_t::destroy_impl();
134       std::vector<char>().swap(m_heapmem);
135    }
136
137    std::vector<char>  m_heapmem;
138    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
139 };
140
141 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
142
143 //!Typedef for a default basic_managed_heap_memory
144 //!of narrow characters
145 typedef basic_managed_heap_memory
146    <char
147    ,rbtree_best_fit<null_mutex_family>
148    ,iset_index>
149 managed_heap_memory;
150
151 //!Typedef for a default basic_managed_heap_memory
152 //!of wide characters
153 typedef basic_managed_heap_memory
154    <wchar_t
155    ,rbtree_best_fit<null_mutex_family>
156    ,iset_index>
157 wmanaged_heap_memory;
158
159 #endif   //#ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
160
161 }  //namespace interprocess {
162 }  //namespace boost {
163
164 #include <boost/interprocess/detail/config_end.hpp>
165
166 #endif   //BOOST_INTERPROCESS_MANAGED_HEAP_MEMORY_HPP
167