Update contrib.
1 // Copyright (C) 2000, 2001 Stephen Cleary
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org for updates, documentation, and revision history.
9 #ifndef BOOST_POOL_ALLOC_HPP
10 #define BOOST_POOL_ALLOC_HPP
12 // std::numeric_limits
13 #include <boost/limits.hpp>
14 // new, std::bad_alloc
17 #include <boost/pool/poolfwd.hpp>
19 // boost::singleton_pool
20 #include <boost/pool/singleton_pool.hpp>
22 #include <boost/detail/workaround.hpp>
24 // The following code will be put into Boost.Config in a later revision
25 #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
26 BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
27 #define BOOST_NO_PROPER_STL_DEALLOCATE
32 struct pool_allocator_tag { };
35 typename UserAllocator,
42 typedef UserAllocator user_allocator;
44 BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
46 typedef value_type * pointer;
47 typedef const value_type * const_pointer;
48 typedef value_type & reference;
49 typedef const value_type & const_reference;
50 typedef typename pool<UserAllocator>::size_type size_type;
51 typedef typename pool<UserAllocator>::difference_type difference_type;
56 typedef pool_allocator<U, UserAllocator, Mutex, NextSize> other;
62 // default copy constructor
64 // default assignment operator
66 // not explicit, mimicking std::allocator [20.4.1]
68 pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize> &)
73 static pointer address(reference r)
75 static const_pointer address(const_reference s)
77 static size_type max_size()
78 { return (std::numeric_limits<size_type>::max)(); }
79 static void construct(const pointer ptr, const value_type & t)
81 static void destroy(const pointer ptr)
84 (void) ptr; // avoid unused variable warning
87 bool operator==(const pool_allocator &) const
89 bool operator!=(const pool_allocator &) const
92 static pointer allocate(const size_type n)
94 const pointer ret = static_cast<pointer>(
95 singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
96 NextSize>::ordered_malloc(n) );
98 throw std::bad_alloc();
101 static pointer allocate(const size_type n, const void * const)
102 { return allocate(n); }
103 static void deallocate(const pointer ptr, const size_type n)
105 #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
106 if (ptr == 0 || n == 0)
109 singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
110 NextSize>::ordered_free(ptr, n);
114 struct fast_pool_allocator_tag { };
116 template <typename T,
117 typename UserAllocator,
120 class fast_pool_allocator
123 typedef T value_type;
124 typedef UserAllocator user_allocator;
126 BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
128 typedef value_type * pointer;
129 typedef const value_type * const_pointer;
130 typedef value_type & reference;
131 typedef const value_type & const_reference;
132 typedef typename pool<UserAllocator>::size_type size_type;
133 typedef typename pool<UserAllocator>::difference_type difference_type;
135 template <typename U>
138 typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize> other;
142 fast_pool_allocator() { }
144 // default copy constructor
146 // default assignment operator
148 // not explicit, mimicking std::allocator [20.4.1]
149 template <typename U>
151 const fast_pool_allocator<U, UserAllocator, Mutex, NextSize> &)
154 // default destructor
156 static pointer address(reference r)
158 static const_pointer address(const_reference s)
160 static size_type max_size()
161 { return (std::numeric_limits<size_type>::max)(); }
162 void construct(const pointer ptr, const value_type & t)
164 void destroy(const pointer ptr)
167 (void) ptr; // avoid unused variable warning
170 bool operator==(const fast_pool_allocator &) const
172 bool operator!=(const fast_pool_allocator &) const
175 static pointer allocate(const size_type n)
177 const pointer ret = (n == 1) ?
178 static_cast<pointer>(
179 singleton_pool<fast_pool_allocator_tag, sizeof(T),
180 UserAllocator, Mutex, NextSize>::malloc() ) :
181 static_cast<pointer>(
182 singleton_pool<fast_pool_allocator_tag, sizeof(T),
183 UserAllocator, Mutex, NextSize>::ordered_malloc(n) );
185 throw std::bad_alloc();
188 static pointer allocate(const size_type n, const void * const)
189 { return allocate(n); }
190 static pointer allocate()
192 const pointer ret = static_cast<pointer>(
193 singleton_pool<fast_pool_allocator_tag, sizeof(T),
194 UserAllocator, Mutex, NextSize>::malloc() );
196 throw std::bad_alloc();
199 static void deallocate(const pointer ptr, const size_type n)
201 #ifdef BOOST_NO_PROPER_STL_DEALLOCATE
202 if (ptr == 0 || n == 0)
206 singleton_pool<fast_pool_allocator_tag, sizeof(T),
207 UserAllocator, Mutex, NextSize>::free(ptr);
209 singleton_pool<fast_pool_allocator_tag, sizeof(T),
210 UserAllocator, Mutex, NextSize>::free(ptr, n);
212 static void deallocate(const pointer ptr)
214 singleton_pool<fast_pool_allocator_tag, sizeof(T),
215 UserAllocator, Mutex, NextSize>::free(ptr);