• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ALLOCATOR_V1_HPP
12 #define BOOST_INTERPROCESS_ALLOCATOR_V1_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/intrusive/pointer_traits.hpp>
22 
23 #include <boost/interprocess/interprocess_fwd.hpp>
24 #include <boost/interprocess/containers/allocation_type.hpp>
25 #include <boost/interprocess/detail/utilities.hpp>
26 #include <boost/interprocess/exceptions.hpp>
27 #include <boost/move/adl_move_swap.hpp>
28 #include <boost/static_assert.hpp>
29 
30 //!\file
31 //!Describes an allocator_v1 that allocates portions of fixed size
32 //!memory buffer (shared memory, mapped file...)
33 
34 namespace boost {
35 namespace interprocess {
36 namespace test {
37 
38 //!An STL compatible allocator_v1 that uses a segment manager as
39 //!memory source. The internal pointer type will of the same type (raw, smart) as
40 //!"typename SegmentManager::void_pointer" type. This allows
41 //!placing the allocator_v1 in shared memory, memory mapped-files, etc...*/
42 template<class T, class SegmentManager>
43 class allocator_v1
44 {
45  private:
46    typedef allocator_v1<T, SegmentManager>         self_t;
47    typedef SegmentManager                          segment_manager;
48    typedef typename segment_manager::void_pointer  aux_pointer_t;
49 
50    typedef typename boost::intrusive::
51       pointer_traits<aux_pointer_t>::template
52          rebind_pointer<const void>::type          cvoid_ptr;
53 
54    typedef typename boost::intrusive::
55       pointer_traits<cvoid_ptr>::template
56          rebind_pointer<segment_manager>::type     alloc_ptr_t;
57 
58    template<class T2, class SegmentManager2>
59    allocator_v1& operator=(const allocator_v1<T2, SegmentManager2>&);
60 
61    allocator_v1& operator=(const allocator_v1&);
62 
63    alloc_ptr_t mp_mngr;
64 
65  public:
66    typedef T                                    value_type;
67 
68    typedef typename boost::intrusive::
69       pointer_traits<cvoid_ptr>::template
70          rebind_pointer<T>::type                pointer;
71 
72    typedef typename boost::intrusive::
73       pointer_traits<cvoid_ptr>::template
74          rebind_pointer<const T>::type          const_pointer;
75 
76    typedef typename ipcdetail::add_reference
77                      <value_type>::type         reference;
78    typedef typename ipcdetail::add_reference
79                      <const value_type>::type   const_reference;
80    typedef typename segment_manager::size_type           size_type;
81    typedef typename segment_manager::difference_type     difference_type;
82 
83    //!Obtains an allocator_v1 of other type
84    template<class T2>
85    struct rebind
86    {
87       typedef allocator_v1<T2, SegmentManager>     other;
88    };
89 
90    //!Returns the segment manager. Never throws
get_segment_manager() const91    segment_manager* get_segment_manager()const
92    {  return ipcdetail::to_raw_pointer(mp_mngr);   }
93 /*
94    //!Returns address of mutable object. Never throws
95    pointer address(reference value) const
96    {  return pointer(addressof(value));  }
97 
98    //!Returns address of non mutable object. Never throws
99    const_pointer address(const_reference value) const
100    {  return const_pointer(addressof(value));  }
101 */
102    //!Constructor from the segment manager. Never throws
allocator_v1(segment_manager * segment_mngr)103    allocator_v1(segment_manager *segment_mngr)
104       : mp_mngr(segment_mngr) { }
105 
106    //!Constructor from other allocator_v1. Never throws
allocator_v1(const allocator_v1 & other)107    allocator_v1(const allocator_v1 &other)
108       : mp_mngr(other.get_segment_manager()){ }
109 
110    //!Constructor from related allocator_v1. Never throws
111    template<class T2>
allocator_v1(const allocator_v1<T2,SegmentManager> & other)112    allocator_v1(const allocator_v1<T2, SegmentManager> &other)
113       : mp_mngr(other.get_segment_manager()){}
114 
115    //!Allocates memory for an array of count elements.
116    //!Throws boost::interprocess::bad_alloc if there is no enough memory
allocate(size_type count,cvoid_ptr hint=0)117    pointer allocate(size_type count, cvoid_ptr hint = 0)
118    {
119       if(size_overflows<sizeof(T)>(count)){
120          throw bad_alloc();
121       }
122       (void)hint; return pointer(static_cast<T*>(mp_mngr->allocate(count*sizeof(T))));
123    }
124 
125    //!Deallocates memory previously allocated. Never throws
deallocate(const pointer & ptr,size_type)126    void deallocate(const pointer &ptr, size_type)
127    {  mp_mngr->deallocate((void*)ipcdetail::to_raw_pointer(ptr));  }
128 
129    //!Construct object, calling constructor.
130    //!Throws if T(const T&) throws
construct(const pointer & ptr,const_reference value)131    void construct(const pointer &ptr, const_reference value)
132    {  new((void*)ipcdetail::to_raw_pointer(ptr)) value_type(value);  }
133 
134    //!Destroys object. Throws if object's destructor throws
destroy(const pointer & ptr)135    void destroy(const pointer &ptr)
136    {  BOOST_ASSERT(ptr != 0); (*ptr).~value_type();  }
137 
138    //!Returns the number of elements that could be allocated. Never throws
max_size() const139    size_type max_size() const
140    {  return mp_mngr->get_size();   }
141 
142    //!Swap segment manager. Does not throw. If each allocator_v1 is placed in
143    //!different memory segments, the result is undefined.
swap(self_t & alloc1,self_t & alloc2)144    friend void swap(self_t &alloc1, self_t &alloc2)
145    {  ::boost::adl_move_swap(alloc1.mp_mngr, alloc2.mp_mngr);   }
146 };
147 
148 //!Equality test for same type of allocator_v1
149 template<class T, class SegmentManager> inline
operator ==(const allocator_v1<T,SegmentManager> & alloc1,const allocator_v1<T,SegmentManager> & alloc2)150 bool operator==(const allocator_v1<T , SegmentManager>  &alloc1,
151                 const allocator_v1<T, SegmentManager>  &alloc2)
152    {  return alloc1.get_segment_manager() == alloc2.get_segment_manager(); }
153 
154 //!Inequality test for same type of allocator_v1
155 template<class T, class SegmentManager> inline
operator !=(const allocator_v1<T,SegmentManager> & alloc1,const allocator_v1<T,SegmentManager> & alloc2)156 bool operator!=(const allocator_v1<T, SegmentManager>  &alloc1,
157                 const allocator_v1<T, SegmentManager>  &alloc2)
158    {  return alloc1.get_segment_manager() != alloc2.get_segment_manager(); }
159 
160 }  //namespace test {
161 }  //namespace interprocess {
162 }  //namespace boost {
163 
164 #include <boost/interprocess/detail/config_end.hpp>
165 
166 #endif   //BOOST_INTERPROCESS_ALLOCATOR_V1_HPP
167 
168