1 ///////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2013. 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)
7 // See http://www.boost.org/libs/container for documentation.
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_EXPAND_BWD_TEST_ALLOCATOR_HPP
12 #define BOOST_CONTAINER_EXPAND_BWD_TEST_ALLOCATOR_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/container/container_fwd.hpp>
26 #include <boost/container/throw_exception.hpp>
28 #include <boost/container/detail/addressof.hpp>
29 #include <boost/container/detail/allocation_type.hpp>
30 #include <boost/container/detail/version_type.hpp>
32 #include <boost/move/adl_move_swap.hpp>
34 #include <boost/assert.hpp>
45 //This allocator just allows two allocations. The first one will return
46 //mp_buffer + m_offset configured in the constructor. The second one
47 //will return mp_buffer.
49 class expand_bwd_test_allocator
52 typedef expand_bwd_test_allocator<T> self_t;
53 typedef void * aux_pointer_t;
54 typedef const void * cvoid_ptr;
57 expand_bwd_test_allocator& operator=(const expand_bwd_test_allocator<T2>&);
59 expand_bwd_test_allocator& operator=(const expand_bwd_test_allocator&);
64 typedef const T * const_pointer;
65 typedef typename dtl::add_reference
66 <value_type>::type reference;
67 typedef typename dtl::add_reference
68 <const value_type>::type const_reference;
69 typedef std::size_t size_type;
70 typedef std::ptrdiff_t difference_type;
72 typedef boost::container::dtl::version_type<expand_bwd_test_allocator, 2> version;
74 //Dummy multiallocation chain
75 struct multiallocation_chain{};
79 { typedef expand_bwd_test_allocator<T2> other; };
81 //!Constructor from the segment manager. Never throws
82 expand_bwd_test_allocator(T *buffer, size_type sz, difference_type offset)
83 : mp_buffer(buffer), m_size(sz)
84 , m_offset(offset), m_allocations(0){ }
86 //!Constructor from other expand_bwd_test_allocator. Never throws
87 expand_bwd_test_allocator(const expand_bwd_test_allocator &other)
88 : mp_buffer(other.mp_buffer), m_size(other.m_size)
89 , m_offset(other.m_offset), m_allocations(0){ }
91 //!Constructor from related expand_bwd_test_allocator. Never throws
93 expand_bwd_test_allocator(const expand_bwd_test_allocator<T2> &other)
94 : mp_buffer(other.mp_buffer), m_size(other.m_size)
95 , m_offset(other.m_offset), m_allocations(0){ }
97 pointer address(reference value)
98 { return pointer(dtl::addressof(value)); }
100 const_pointer address(const_reference value) const
101 { return const_pointer(dtl::addressof(value)); }
103 pointer allocate(size_type , cvoid_ptr hint = 0)
104 { (void)hint; return 0; }
106 void deallocate(const pointer &, size_type)
109 template<class Convertible>
110 void construct(pointer ptr, const Convertible &value)
111 { new((void*)ptr) value_type(value); }
113 void destroy(pointer ptr)
114 { (*ptr).~value_type(); }
116 size_type max_size() const
119 friend void swap(self_t &alloc1, self_t &alloc2)
121 boost::adl_move_swap(alloc1.mp_buffer, alloc2.mp_buffer);
122 boost::adl_move_swap(alloc1.m_size, alloc2.m_size);
123 boost::adl_move_swap(alloc1.m_offset, alloc2.m_offset);
126 //Experimental version 2 expand_bwd_test_allocator functions
128 pointer allocation_command(boost::container::allocation_type command,
129 size_type limit_size,size_type &prefer_in_recvd_out_size,pointer &reuse)
131 (void)reuse; (void)command;
132 //This allocator only expands backwards!
133 assert(m_allocations == 0 || (command & boost::container::expand_bwd));
135 prefer_in_recvd_out_size = limit_size;
137 if(m_allocations == 0){
138 if((m_offset + limit_size) > m_size){
143 return (mp_buffer + m_offset);
145 else if(m_allocations == 1){
146 if(limit_size > m_size){
158 //!Returns maximum the number of objects the previously allocated memory
159 //!pointed by p can hold.
160 size_type size(const pointer &p) const
161 { (void)p; return m_size; }
163 //!Allocates just one object. Memory allocated with this function
164 //!must be deallocated only with deallocate_one().
165 //!Throws boost::container::bad_alloc if there is no enough memory
166 pointer allocate_one()
167 { return this->allocate(1); }
169 //!Deallocates memory previously allocated with allocate_one().
170 //!You should never use deallocate_one to deallocate memory allocated
171 //!with other functions different from allocate_one(). Never throws
172 void deallocate_one(const pointer &p)
173 { return this->deallocate(p, 1); }
177 difference_type m_offset;
181 //!Equality test for same type of expand_bwd_test_allocator
182 template<class T> inline
183 bool operator==(const expand_bwd_test_allocator<T> &,
184 const expand_bwd_test_allocator<T> &)
187 //!Inequality test for same type of expand_bwd_test_allocator
188 template<class T> inline
189 bool operator!=(const expand_bwd_test_allocator<T> &,
190 const expand_bwd_test_allocator<T> &)
194 } //namespace container {
195 } //namespace boost {
197 #include <boost/container/detail/config_end.hpp>
199 #endif //BOOST_CONTAINER_EXPAND_BWD_TEST_ALLOCATOR_HPP