1
// Copyright (C) 2000, 2001 Stephen Cleary (shammah@voyager.net)
3
// This file can be redistributed and/or modified under the terms found
5
// This software and its documentation is provided "as is" without express or
6
// implied warranty, and with no claim as to its suitability for any purpose.
8
// See http://www.boost.org for updates, documentation, and revision history.
10
#ifndef BOOST_POOL_ALLOC_HPP
11
#define BOOST_POOL_ALLOC_HPP
13
// std::numeric_limits
14
#include <boost/limits.hpp>
15
// new, std::bad_alloc
18
#include <boost/pool/poolfwd.hpp>
20
// boost::singleton_pool
21
#include <boost/pool/singleton_pool.hpp>
23
// The following code will be put into Boost.Config in a later revision
24
#if defined(_RWSTD_VER) || defined(__SGI_STL_PORT)
25
// Needed, as of bcc 5.5 and STLPort 4.5b8
26
#define BOOST_NO_PROPER_STL_DEALLOCATE
31
struct pool_allocator_tag { };
34
typename UserAllocator,
41
typedef UserAllocator user_allocator;
43
BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
45
typedef value_type * pointer;
46
typedef const value_type * const_pointer;
47
typedef value_type & reference;
48
typedef const value_type & const_reference;
49
typedef typename pool<UserAllocator>::size_type size_type;
50
typedef typename pool<UserAllocator>::difference_type difference_type;
55
typedef pool_allocator<U, UserAllocator, Mutex, NextSize> other;
61
// default copy constructor
63
// default assignment operator
65
// not explicit, mimicking std::allocator [20.4.1]
67
pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize> &)
72
static pointer address(reference r)
74
static const_pointer address(const_reference s)
76
static size_type max_size()
77
{ return std::numeric_limits<size_type>::max(); }
78
static void construct(const pointer ptr, const value_type & t)
80
static void destroy(const pointer ptr)
83
(void) ptr; // avoid unused variable warning
86
bool operator==(const pool_allocator &) const
88
bool operator!=(const pool_allocator &) const
91
static pointer allocate(const size_type n)
93
const pointer ret = static_cast<pointer>(
94
singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
95
NextSize>::ordered_malloc(n) );
97
throw std::bad_alloc();
100
static pointer allocate(const size_type n, const void * const)
101
{ return allocate(n); }
102
static void deallocate(const pointer ptr, const size_type n)
104
#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
105
if (ptr == 0 || n == 0)
108
singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
109
NextSize>::ordered_free(ptr, n);
113
struct fast_pool_allocator_tag { };
115
template <typename T,
116
typename UserAllocator,
119
class fast_pool_allocator
122
typedef T value_type;
123
typedef UserAllocator user_allocator;
125
BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
127
typedef value_type * pointer;
128
typedef const value_type * const_pointer;
129
typedef value_type & reference;
130
typedef const value_type & const_reference;
131
typedef typename pool<UserAllocator>::size_type size_type;
132
typedef typename pool<UserAllocator>::difference_type difference_type;
134
template <typename U>
137
typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize> other;
141
fast_pool_allocator() { }
143
// default copy constructor
145
// default assignment operator
147
// not explicit, mimicking std::allocator [20.4.1]
148
template <typename U>
150
const fast_pool_allocator<U, UserAllocator, Mutex, NextSize> &)
153
// default destructor
155
static pointer address(reference r)
157
static const_pointer address(const_reference s)
159
static size_type max_size()
160
{ return std::numeric_limits<size_type>::max(); }
161
void construct(const pointer ptr, const value_type & t)
163
void destroy(const pointer ptr)
166
(void) ptr; // avoid unused variable warning
169
bool operator==(const fast_pool_allocator &) const
171
bool operator!=(const fast_pool_allocator &) const
174
static pointer allocate(const size_type n)
176
const pointer ret = (n == 1) ?
177
static_cast<pointer>(
178
singleton_pool<fast_pool_allocator_tag, sizeof(T),
179
UserAllocator, Mutex, NextSize>::malloc() ) :
180
static_cast<pointer>(
181
singleton_pool<fast_pool_allocator_tag, sizeof(T),
182
UserAllocator, Mutex, NextSize>::ordered_malloc(n) );
184
throw std::bad_alloc();
187
static pointer allocate(const size_type n, const void * const)
188
{ return allocate(n); }
189
static pointer allocate()
191
const pointer ret = static_cast<pointer>(
192
singleton_pool<fast_pool_allocator_tag, sizeof(T),
193
UserAllocator, Mutex, NextSize>::malloc() );
195
throw std::bad_alloc();
198
static void deallocate(const pointer ptr, const size_type n)
200
#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
201
if (ptr == 0 || n == 0)
205
singleton_pool<fast_pool_allocator_tag, sizeof(T),
206
UserAllocator, Mutex, NextSize>::free(ptr);
208
singleton_pool<fast_pool_allocator_tag, sizeof(T),
209
UserAllocator, Mutex, NextSize>::free(ptr, n);
211
static void deallocate(const pointer ptr)
213
singleton_pool<fast_pool_allocator_tag, sizeof(T),
214
UserAllocator, Mutex, NextSize>::free(ptr);