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
41namespace boost {
42namespace container {
43namespace 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.
48template<class T>
49class 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;
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;
71
72 typedef boost::container::dtl::version_type<expand_bwd_test_allocator, 2> version;
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
82 expand_bwd_test_allocator(T *buffer, size_type sz, size_type offset)
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)
98 { return pointer(dtl::addressof(value)); }
99
100 const_pointer address(const_reference value) const
101 { return const_pointer(dtl::addressof(value)); }
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 throw_bad_alloc();
141 }
142 ++m_allocations;
143 reuse = 0;
144 return (mp_buffer + m_offset);
145 }
146 else{
147 if(m_allocations != 1){
148 throw_bad_alloc();
149 }
150 assert(limit_size <= m_size);
151 ++m_allocations;
152 return mp_buffer;
153 }
154 }
155
156 //!Returns maximum the number of objects the previously allocated memory
157 //!pointed by p can hold.
158 size_type size(const pointer &p) const
159 { (void)p; return m_size; }
160
161 //!Allocates just one object. Memory allocated with this function
162 //!must be deallocated only with deallocate_one().
163 //!Throws boost::container::bad_alloc if there is no enough memory
164 pointer allocate_one()
165 { return this->allocate(1); }
166
167 //!Deallocates memory previously allocated with allocate_one().
168 //!You should never use deallocate_one to deallocate memory allocated
169 //!with other functions different from allocate_one(). Never throws
170 void deallocate_one(const pointer &p)
171 { return this->deallocate(p, 1); }
172
173 pointer mp_buffer;
174 size_type m_size;
175 size_type m_offset;
176 char m_allocations;
177};
178
179//!Equality test for same type of expand_bwd_test_allocator
180template<class T> inline
181bool operator==(const expand_bwd_test_allocator<T> &,
182 const expand_bwd_test_allocator<T> &)
183{ return false; }
184
185//!Inequality test for same type of expand_bwd_test_allocator
186template<class T> inline
187bool operator!=(const expand_bwd_test_allocator<T> &,
188 const expand_bwd_test_allocator<T> &)
189{ return true; }
190
191} //namespace test {
192} //namespace container {
193} //namespace boost {
194
195#include <boost/container/detail/config_end.hpp>
196
197#endif //BOOST_CONTAINER_EXPAND_BWD_TEST_ALLOCATOR_HPP
198
199

source code of boost/libs/container/test/expand_bwd_test_allocator.hpp