PageRenderTime 57ms CodeModel.GetById 6ms app.highlight 36ms RepoModel.GetById 7ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/pool/pool_alloc.hpp

http://hadesmem.googlecode.com/
C++ Header | 286 lines | 216 code | 35 blank | 35 comment | 19 complexity | 3ee8ee3fef952a23d87afc7574ccf2ae MD5 | raw file
  1// Copyright (C) 2000, 2001 Stephen Cleary
  2//
  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)
  6//
  7// See http://www.boost.org for updates, documentation, and revision history.
  8
  9#ifndef BOOST_POOL_ALLOC_HPP
 10#define BOOST_POOL_ALLOC_HPP
 11
 12// std::numeric_limits
 13#include <boost/limits.hpp>
 14// new, std::bad_alloc
 15#include <new>
 16
 17#include <boost/throw_exception.hpp>
 18#include <boost/pool/poolfwd.hpp>
 19
 20// boost::singleton_pool
 21#include <boost/pool/singleton_pool.hpp>
 22
 23#include <boost/detail/workaround.hpp>
 24
 25// The following code will be put into Boost.Config in a later revision
 26#if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
 27    BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
 28 #define BOOST_NO_PROPER_STL_DEALLOCATE
 29#endif
 30
 31namespace boost {
 32
 33struct pool_allocator_tag { };
 34
 35template <typename T,
 36    typename UserAllocator,
 37    typename Mutex,
 38    unsigned NextSize,
 39    unsigned MaxSize>
 40class pool_allocator
 41{
 42  public:
 43    typedef T value_type;
 44    typedef UserAllocator user_allocator;
 45    typedef Mutex mutex;
 46    BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
 47
 48    typedef value_type * pointer;
 49    typedef const value_type * const_pointer;
 50    typedef value_type & reference;
 51    typedef const value_type & const_reference;
 52    typedef typename pool<UserAllocator>::size_type size_type;
 53    typedef typename pool<UserAllocator>::difference_type difference_type;
 54
 55    template <typename U>
 56    struct rebind
 57    {
 58      typedef pool_allocator<U, UserAllocator, Mutex, NextSize,MaxSize> other;
 59    };
 60
 61  public:
 62    pool_allocator()
 63    {
 64      // Required to ensure construction of singleton_pool IFF an
 65      // instace of this allocator is constructed during global
 66      // initialization. See ticket #2359 for a complete explaination
 67      // ( http://svn.boost.org/trac/boost/ticket/2359 )
 68      singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
 69                     NextSize, MaxSize>::is_from(0);
 70    }
 71
 72    // default copy constructor
 73
 74    // default assignment operator
 75
 76    // not explicit, mimicking std::allocator [20.4.1]
 77    template <typename U>
 78    pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
 79    {
 80      // Required to ensure construction of singleton_pool IFF an
 81      // instace of this allocator is constructed during global
 82      // initialization. See ticket #2359 for a complete explaination
 83      // ( http://svn.boost.org/trac/boost/ticket/2359 )
 84      singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
 85                     NextSize, MaxSize>::is_from(0);
 86    }
 87
 88    // default destructor
 89
 90    static pointer address(reference r)
 91    { return &r; }
 92    static const_pointer address(const_reference s)
 93    { return &s; }
 94    static size_type max_size()
 95    { return (std::numeric_limits<size_type>::max)(); }
 96    static void construct(const pointer ptr, const value_type & t)
 97    { new (ptr) T(t); }
 98    static void destroy(const pointer ptr)
 99    {
100      ptr->~T();
101      (void) ptr; // avoid unused variable warning
102    }
103
104    bool operator==(const pool_allocator &) const
105    { return true; }
106    bool operator!=(const pool_allocator &) const
107    { return false; }
108
109    static pointer allocate(const size_type n)
110    {
111      const pointer ret = static_cast<pointer>(
112          singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
113              NextSize, MaxSize>::ordered_malloc(n) );
114      if (ret == 0)
115        boost::throw_exception(std::bad_alloc());
116      return ret;
117    }
118    static pointer allocate(const size_type n, const void * const)
119    { return allocate(n); }
120    static void deallocate(const pointer ptr, const size_type n)
121    {
122#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
123      if (ptr == 0 || n == 0)
124        return;
125#endif
126      singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
127          NextSize, MaxSize>::ordered_free(ptr, n);
128    }
129};
130
131template<
132    typename UserAllocator,
133    typename Mutex,
134    unsigned NextSize,
135    unsigned MaxSize>
136class pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
137{
138public:
139    typedef void*       pointer;
140    typedef const void* const_pointer;
141    typedef void        value_type;
142    template <class U> struct rebind {
143        typedef pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
144    };
145};
146
147struct fast_pool_allocator_tag { };
148
149template <typename T,
150    typename UserAllocator,
151    typename Mutex,
152    unsigned NextSize,
153    unsigned MaxSize>
154class fast_pool_allocator
155{
156  public:
157    typedef T value_type;
158    typedef UserAllocator user_allocator;
159    typedef Mutex mutex;
160    BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
161
162    typedef value_type * pointer;
163    typedef const value_type * const_pointer;
164    typedef value_type & reference;
165    typedef const value_type & const_reference;
166    typedef typename pool<UserAllocator>::size_type size_type;
167    typedef typename pool<UserAllocator>::difference_type difference_type;
168
169    template <typename U>
170    struct rebind
171    {
172      typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
173    };
174
175  public:
176    fast_pool_allocator()
177    {
178      // Required to ensure construction of singleton_pool IFF an
179      // instace of this allocator is constructed during global
180      // initialization. See ticket #2359 for a complete explaination
181      // ( http://svn.boost.org/trac/boost/ticket/2359 )
182      singleton_pool<fast_pool_allocator_tag, sizeof(T),
183                     UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
184    }
185    
186    // default copy constructor
187
188    // default assignment operator
189
190    // not explicit, mimicking std::allocator [20.4.1]
191    template <typename U>
192    fast_pool_allocator(
193        const fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> &)
194    {
195      // Required to ensure construction of singleton_pool IFF an
196      // instace of this allocator is constructed during global
197      // initialization. See ticket #2359 for a complete explaination
198      // ( http://svn.boost.org/trac/boost/ticket/2359 )
199      singleton_pool<fast_pool_allocator_tag, sizeof(T),
200                     UserAllocator, Mutex, NextSize, MaxSize>::is_from(0);
201    }
202
203    // default destructor
204
205    static pointer address(reference r)
206    { return &r; }
207    static const_pointer address(const_reference s)
208    { return &s; }
209    static size_type max_size()
210    { return (std::numeric_limits<size_type>::max)(); }
211    void construct(const pointer ptr, const value_type & t)
212    { new (ptr) T(t); }
213    void destroy(const pointer ptr)
214    {
215      ptr->~T();
216      (void) ptr; // avoid unused variable warning
217    }
218
219    bool operator==(const fast_pool_allocator &) const
220    { return true; }
221    bool operator!=(const fast_pool_allocator &) const
222    { return false; }
223
224    static pointer allocate(const size_type n)
225    {
226      const pointer ret = (n == 1) ? 
227          static_cast<pointer>(
228              (singleton_pool<fast_pool_allocator_tag, sizeof(T),
229                  UserAllocator, Mutex, NextSize, MaxSize>::malloc)() ) :
230          static_cast<pointer>(
231              singleton_pool<fast_pool_allocator_tag, sizeof(T),
232                  UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) );
233      if (ret == 0)
234        boost::throw_exception(std::bad_alloc());
235      return ret;
236    }
237    static pointer allocate(const size_type n, const void * const)
238    { return allocate(n); }
239    static pointer allocate()
240    {
241      const pointer ret = static_cast<pointer>(
242          (singleton_pool<fast_pool_allocator_tag, sizeof(T),
243              UserAllocator, Mutex, NextSize, MaxSize>::malloc)() );
244      if (ret == 0)
245        boost::throw_exception(std::bad_alloc());
246      return ret;
247    }
248    static void deallocate(const pointer ptr, const size_type n)
249    {
250#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
251      if (ptr == 0 || n == 0)
252        return;
253#endif
254      if (n == 1)
255        (singleton_pool<fast_pool_allocator_tag, sizeof(T),
256            UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
257      else
258        (singleton_pool<fast_pool_allocator_tag, sizeof(T),
259            UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr, n);
260    }
261    static void deallocate(const pointer ptr)
262    {
263      (singleton_pool<fast_pool_allocator_tag, sizeof(T),
264          UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr);
265    }
266};
267
268template<
269    typename UserAllocator,
270    typename Mutex,
271    unsigned NextSize,
272    unsigned MaxSize>
273class fast_pool_allocator<void, UserAllocator, Mutex, NextSize, MaxSize>
274{
275public:
276    typedef void*       pointer;
277    typedef const void* const_pointer;
278    typedef void        value_type;
279    template <class U> struct rebind {
280        typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize, MaxSize> other;
281    };
282};
283
284} // namespace boost
285
286#endif