~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/pool/pool_alloc.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2000, 2001 Stephen Cleary (shammah@voyager.net)
 
2
//
 
3
// This file can be redistributed and/or modified under the terms found
 
4
//  in "copyright.html"
 
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.
 
7
//
 
8
// See http://www.boost.org for updates, documentation, and revision history.
 
9
 
 
10
#ifndef BOOST_POOL_ALLOC_HPP
 
11
#define BOOST_POOL_ALLOC_HPP
 
12
 
 
13
// std::numeric_limits
 
14
#include <boost/limits.hpp>
 
15
// new, std::bad_alloc
 
16
#include <new>
 
17
 
 
18
#include <boost/pool/poolfwd.hpp>
 
19
 
 
20
// boost::singleton_pool
 
21
#include <boost/pool/singleton_pool.hpp>
 
22
 
 
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
 
27
#endif
 
28
 
 
29
namespace boost {
 
30
 
 
31
struct pool_allocator_tag { };
 
32
 
 
33
template <typename T,
 
34
    typename UserAllocator,
 
35
    typename Mutex,
 
36
    unsigned NextSize>
 
37
class pool_allocator
 
38
{
 
39
  public:
 
40
    typedef T value_type;
 
41
    typedef UserAllocator user_allocator;
 
42
    typedef Mutex mutex;
 
43
    BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
 
44
 
 
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;
 
51
 
 
52
    template <typename U>
 
53
    struct rebind
 
54
    {
 
55
      typedef pool_allocator<U, UserAllocator, Mutex, NextSize> other;
 
56
    };
 
57
 
 
58
  public:
 
59
    pool_allocator() { }
 
60
 
 
61
    // default copy constructor
 
62
 
 
63
    // default assignment operator
 
64
 
 
65
    // not explicit, mimicking std::allocator [20.4.1]
 
66
    template <typename U>
 
67
    pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize> &)
 
68
    { }
 
69
 
 
70
    // default destructor
 
71
 
 
72
    static pointer address(reference r)
 
73
    { return &r; }
 
74
    static const_pointer address(const_reference s)
 
75
    { return &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)
 
79
    { new (ptr) T(t); }
 
80
    static void destroy(const pointer ptr)
 
81
    {
 
82
      ptr->~T();
 
83
      (void) ptr; // avoid unused variable warning
 
84
    }
 
85
 
 
86
    bool operator==(const pool_allocator &) const
 
87
    { return true; }
 
88
    bool operator!=(const pool_allocator &) const
 
89
    { return false; }
 
90
 
 
91
    static pointer allocate(const size_type n)
 
92
    {
 
93
      const pointer ret = static_cast<pointer>(
 
94
          singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
 
95
              NextSize>::ordered_malloc(n) );
 
96
      if (ret == 0)
 
97
        throw std::bad_alloc();
 
98
      return ret;
 
99
    }
 
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)
 
103
    {
 
104
#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
 
105
      if (ptr == 0 || n == 0)
 
106
        return;
 
107
#endif
 
108
      singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
 
109
          NextSize>::ordered_free(ptr, n);
 
110
    }
 
111
};
 
112
 
 
113
struct fast_pool_allocator_tag { };
 
114
 
 
115
template <typename T,
 
116
    typename UserAllocator,
 
117
    typename Mutex,
 
118
    unsigned NextSize>
 
119
class fast_pool_allocator
 
120
{
 
121
  public:
 
122
    typedef T value_type;
 
123
    typedef UserAllocator user_allocator;
 
124
    typedef Mutex mutex;
 
125
    BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
 
126
 
 
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;
 
133
 
 
134
    template <typename U>
 
135
    struct rebind
 
136
    {
 
137
      typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize> other;
 
138
    };
 
139
 
 
140
  public:
 
141
    fast_pool_allocator() { }
 
142
 
 
143
    // default copy constructor
 
144
 
 
145
    // default assignment operator
 
146
 
 
147
    // not explicit, mimicking std::allocator [20.4.1]
 
148
    template <typename U>
 
149
    fast_pool_allocator(
 
150
        const fast_pool_allocator<U, UserAllocator, Mutex, NextSize> &)
 
151
    { }
 
152
 
 
153
    // default destructor
 
154
 
 
155
    static pointer address(reference r)
 
156
    { return &r; }
 
157
    static const_pointer address(const_reference s)
 
158
    { return &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)
 
162
    { new (ptr) T(t); }
 
163
    void destroy(const pointer ptr)
 
164
    {
 
165
      ptr->~T();
 
166
      (void) ptr; // avoid unused variable warning
 
167
    }
 
168
 
 
169
    bool operator==(const fast_pool_allocator &) const
 
170
    { return true; }
 
171
    bool operator!=(const fast_pool_allocator &) const
 
172
    { return false; }
 
173
 
 
174
    static pointer allocate(const size_type n)
 
175
    {
 
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) );
 
183
      if (ret == 0)
 
184
        throw std::bad_alloc();
 
185
      return ret;
 
186
    }
 
187
    static pointer allocate(const size_type n, const void * const)
 
188
    { return allocate(n); }
 
189
    static pointer allocate()
 
190
    {
 
191
      const pointer ret = static_cast<pointer>(
 
192
          singleton_pool<fast_pool_allocator_tag, sizeof(T),
 
193
              UserAllocator, Mutex, NextSize>::malloc() );
 
194
      if (ret == 0)
 
195
        throw std::bad_alloc();
 
196
      return ret;
 
197
    }
 
198
    static void deallocate(const pointer ptr, const size_type n)
 
199
    {
 
200
#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
 
201
      if (ptr == 0 || n == 0)
 
202
        return;
 
203
#endif
 
204
      if (n == 1)
 
205
        singleton_pool<fast_pool_allocator_tag, sizeof(T),
 
206
            UserAllocator, Mutex, NextSize>::free(ptr);
 
207
      else
 
208
        singleton_pool<fast_pool_allocator_tag, sizeof(T),
 
209
            UserAllocator, Mutex, NextSize>::free(ptr, n);
 
210
    }
 
211
    static void deallocate(const pointer ptr)
 
212
    {
 
213
      singleton_pool<fast_pool_allocator_tag, sizeof(T),
 
214
          UserAllocator, Mutex, NextSize>::free(ptr);
 
215
    }
 
216
};
 
217
 
 
218
} // namespace boost
 
219
 
 
220
#endif