]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // | |
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) | |
6 | // | |
7 | // See http://www.boost.org/libs/container for documentation. | |
8 | // | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef BOOST_CONTAINER_EXPAND_BWD_TEST_ALLOCATOR_HPP | |
12 | #define BOOST_CONTAINER_EXPAND_BWD_TEST_ALLOCATOR_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/container/detail/config_begin.hpp> | |
23 | #include <boost/container/detail/workaround.hpp> | |
24 | #include <boost/container/container_fwd.hpp> | |
25 | ||
26 | #include <boost/container/throw_exception.hpp> | |
27 | ||
28 | #include <boost/container/detail/addressof.hpp> | |
29 | #include <boost/container/detail/allocation_type.hpp> | |
30 | #include <boost/container/detail/version_type.hpp> | |
31 | ||
32 | #include <boost/move/adl_move_swap.hpp> | |
33 | ||
34 | #include <boost/assert.hpp> | |
35 | ||
36 | #include <memory> | |
37 | #include <algorithm> | |
38 | #include <cstddef> | |
39 | #include <cassert> | |
40 | ||
41 | namespace boost { | |
42 | namespace container { | |
43 | namespace test { | |
44 | ||
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. | |
48 | template<class T> | |
49 | class expand_bwd_test_allocator | |
50 | { | |
51 | private: | |
52 | typedef expand_bwd_test_allocator<T> self_t; | |
53 | typedef void * aux_pointer_t; | |
54 | typedef const void * cvoid_ptr; | |
55 | ||
56 | template<class T2> | |
57 | expand_bwd_test_allocator& operator=(const expand_bwd_test_allocator<T2>&); | |
58 | ||
59 | expand_bwd_test_allocator& operator=(const expand_bwd_test_allocator&); | |
60 | ||
61 | public: | |
62 | typedef T value_type; | |
63 | typedef T * pointer; | |
64 | typedef const T * const_pointer; | |
11fdf7f2 | 65 | typedef typename dtl::add_reference |
7c673cae | 66 | <value_type>::type reference; |
11fdf7f2 | 67 | typedef typename dtl::add_reference |
7c673cae FG |
68 | <const value_type>::type const_reference; |
69 | typedef std::size_t size_type; | |
70 | typedef std::ptrdiff_t difference_type; | |
71 | ||
11fdf7f2 | 72 | typedef boost::container::dtl::version_type<expand_bwd_test_allocator, 2> version; |
7c673cae FG |
73 | |
74 | //Dummy multiallocation chain | |
75 | struct multiallocation_chain{}; | |
76 | ||
77 | template<class T2> | |
78 | struct rebind | |
79 | { typedef expand_bwd_test_allocator<T2> other; }; | |
80 | ||
81 | //!Constructor from the segment manager. Never throws | |
1e59de90 | 82 | expand_bwd_test_allocator(T *buffer, size_type sz, size_type offset) |
7c673cae FG |
83 | : mp_buffer(buffer), m_size(sz) |
84 | , m_offset(offset), m_allocations(0){ } | |
85 | ||
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){ } | |
90 | ||
91 | //!Constructor from related expand_bwd_test_allocator. Never throws | |
92 | template<class T2> | |
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){ } | |
96 | ||
97 | pointer address(reference value) | |
11fdf7f2 | 98 | { return pointer(dtl::addressof(value)); } |
7c673cae FG |
99 | |
100 | const_pointer address(const_reference value) const | |
11fdf7f2 | 101 | { return const_pointer(dtl::addressof(value)); } |
7c673cae FG |
102 | |
103 | pointer allocate(size_type , cvoid_ptr hint = 0) | |
104 | { (void)hint; return 0; } | |
105 | ||
106 | void deallocate(const pointer &, size_type) | |
107 | {} | |
108 | ||
109 | template<class Convertible> | |
110 | void construct(pointer ptr, const Convertible &value) | |
111 | { new((void*)ptr) value_type(value); } | |
112 | ||
113 | void destroy(pointer ptr) | |
114 | { (*ptr).~value_type(); } | |
115 | ||
116 | size_type max_size() const | |
117 | { return m_size; } | |
118 | ||
119 | friend void swap(self_t &alloc1, self_t &alloc2) | |
120 | { | |
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); | |
124 | } | |
125 | ||
126 | //Experimental version 2 expand_bwd_test_allocator functions | |
127 | ||
128 | pointer allocation_command(boost::container::allocation_type command, | |
129 | size_type limit_size,size_type &prefer_in_recvd_out_size,pointer &reuse) | |
130 | { | |
131 | (void)reuse; (void)command; | |
132 | //This allocator only expands backwards! | |
133 | assert(m_allocations == 0 || (command & boost::container::expand_bwd)); | |
134 | ||
135 | prefer_in_recvd_out_size = limit_size; | |
136 | ||
137 | if(m_allocations == 0){ | |
138 | if((m_offset + limit_size) > m_size){ | |
139 | assert(0); | |
140 | } | |
141 | ++m_allocations; | |
142 | reuse = 0; | |
143 | return (mp_buffer + m_offset); | |
144 | } | |
20effc67 TL |
145 | else{ |
146 | if(m_allocations != 1){ | |
147 | throw_bad_alloc(); | |
7c673cae | 148 | } |
20effc67 | 149 | assert(limit_size <= m_size); |
7c673cae FG |
150 | ++m_allocations; |
151 | return mp_buffer; | |
152 | } | |
7c673cae FG |
153 | } |
154 | ||
155 | //!Returns maximum the number of objects the previously allocated memory | |
156 | //!pointed by p can hold. | |
157 | size_type size(const pointer &p) const | |
158 | { (void)p; return m_size; } | |
159 | ||
160 | //!Allocates just one object. Memory allocated with this function | |
161 | //!must be deallocated only with deallocate_one(). | |
162 | //!Throws boost::container::bad_alloc if there is no enough memory | |
163 | pointer allocate_one() | |
164 | { return this->allocate(1); } | |
165 | ||
166 | //!Deallocates memory previously allocated with allocate_one(). | |
167 | //!You should never use deallocate_one to deallocate memory allocated | |
168 | //!with other functions different from allocate_one(). Never throws | |
169 | void deallocate_one(const pointer &p) | |
170 | { return this->deallocate(p, 1); } | |
171 | ||
172 | pointer mp_buffer; | |
173 | size_type m_size; | |
1e59de90 | 174 | size_type m_offset; |
7c673cae FG |
175 | char m_allocations; |
176 | }; | |
177 | ||
178 | //!Equality test for same type of expand_bwd_test_allocator | |
179 | template<class T> inline | |
92f5a8d4 TL |
180 | bool operator==(const expand_bwd_test_allocator<T> &, |
181 | const expand_bwd_test_allocator<T> &) | |
7c673cae FG |
182 | { return false; } |
183 | ||
184 | //!Inequality test for same type of expand_bwd_test_allocator | |
185 | template<class T> inline | |
92f5a8d4 TL |
186 | bool operator!=(const expand_bwd_test_allocator<T> &, |
187 | const expand_bwd_test_allocator<T> &) | |
7c673cae FG |
188 | { return true; } |
189 | ||
190 | } //namespace test { | |
191 | } //namespace container { | |
192 | } //namespace boost { | |
193 | ||
194 | #include <boost/container/detail/config_end.hpp> | |
195 | ||
196 | #endif //BOOST_CONTAINER_EXPAND_BWD_TEST_ALLOCATOR_HPP | |
197 |