os/ossrv/ossrv_pub/boost_apis/boost/pool/pool_alloc.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (C) 2000, 2001 Stephen Cleary
sl@0
     2
//
sl@0
     3
// Distributed under the Boost Software License, Version 1.0. (See
sl@0
     4
// accompanying file LICENSE_1_0.txt or copy at
sl@0
     5
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     6
//
sl@0
     7
// See http://www.boost.org for updates, documentation, and revision history.
sl@0
     8
sl@0
     9
#ifndef BOOST_POOL_ALLOC_HPP
sl@0
    10
#define BOOST_POOL_ALLOC_HPP
sl@0
    11
sl@0
    12
// std::numeric_limits
sl@0
    13
#include <boost/limits.hpp>
sl@0
    14
// new, std::bad_alloc
sl@0
    15
#include <new>
sl@0
    16
sl@0
    17
#include <boost/pool/poolfwd.hpp>
sl@0
    18
sl@0
    19
// boost::singleton_pool
sl@0
    20
#include <boost/pool/singleton_pool.hpp>
sl@0
    21
sl@0
    22
#include <boost/detail/workaround.hpp>
sl@0
    23
sl@0
    24
// The following code will be put into Boost.Config in a later revision
sl@0
    25
#if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \
sl@0
    26
    BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
sl@0
    27
 #define BOOST_NO_PROPER_STL_DEALLOCATE
sl@0
    28
#endif
sl@0
    29
sl@0
    30
namespace boost {
sl@0
    31
sl@0
    32
struct pool_allocator_tag { };
sl@0
    33
sl@0
    34
template <typename T,
sl@0
    35
    typename UserAllocator,
sl@0
    36
    typename Mutex,
sl@0
    37
    unsigned NextSize>
sl@0
    38
class pool_allocator
sl@0
    39
{
sl@0
    40
  public:
sl@0
    41
    typedef T value_type;
sl@0
    42
    typedef UserAllocator user_allocator;
sl@0
    43
    typedef Mutex mutex;
sl@0
    44
    BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
sl@0
    45
sl@0
    46
    typedef value_type * pointer;
sl@0
    47
    typedef const value_type * const_pointer;
sl@0
    48
    typedef value_type & reference;
sl@0
    49
    typedef const value_type & const_reference;
sl@0
    50
    typedef typename pool<UserAllocator>::size_type size_type;
sl@0
    51
    typedef typename pool<UserAllocator>::difference_type difference_type;
sl@0
    52
sl@0
    53
    template <typename U>
sl@0
    54
    struct rebind
sl@0
    55
    {
sl@0
    56
      typedef pool_allocator<U, UserAllocator, Mutex, NextSize> other;
sl@0
    57
    };
sl@0
    58
sl@0
    59
  public:
sl@0
    60
    pool_allocator() { }
sl@0
    61
sl@0
    62
    // default copy constructor
sl@0
    63
sl@0
    64
    // default assignment operator
sl@0
    65
sl@0
    66
    // not explicit, mimicking std::allocator [20.4.1]
sl@0
    67
    template <typename U>
sl@0
    68
    pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize> &)
sl@0
    69
    { }
sl@0
    70
sl@0
    71
    // default destructor
sl@0
    72
sl@0
    73
    static pointer address(reference r)
sl@0
    74
    { return &r; }
sl@0
    75
    static const_pointer address(const_reference s)
sl@0
    76
    { return &s; }
sl@0
    77
    static size_type max_size()
sl@0
    78
    { return (std::numeric_limits<size_type>::max)(); }
sl@0
    79
    static void construct(const pointer ptr, const value_type & t)
sl@0
    80
    { new (ptr) T(t); }
sl@0
    81
    static void destroy(const pointer ptr)
sl@0
    82
    {
sl@0
    83
      ptr->~T();
sl@0
    84
      (void) ptr; // avoid unused variable warning
sl@0
    85
    }
sl@0
    86
sl@0
    87
    bool operator==(const pool_allocator &) const
sl@0
    88
    { return true; }
sl@0
    89
    bool operator!=(const pool_allocator &) const
sl@0
    90
    { return false; }
sl@0
    91
sl@0
    92
    static pointer allocate(const size_type n)
sl@0
    93
    {
sl@0
    94
      const pointer ret = static_cast<pointer>(
sl@0
    95
          singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
sl@0
    96
              NextSize>::ordered_malloc(n) );
sl@0
    97
      if (ret == 0)
sl@0
    98
        throw std::bad_alloc();
sl@0
    99
      return ret;
sl@0
   100
    }
sl@0
   101
    static pointer allocate(const size_type n, const void * const)
sl@0
   102
    { return allocate(n); }
sl@0
   103
    static void deallocate(const pointer ptr, const size_type n)
sl@0
   104
    {
sl@0
   105
#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
sl@0
   106
      if (ptr == 0 || n == 0)
sl@0
   107
        return;
sl@0
   108
#endif
sl@0
   109
      singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex,
sl@0
   110
          NextSize>::ordered_free(ptr, n);
sl@0
   111
    }
sl@0
   112
};
sl@0
   113
sl@0
   114
struct fast_pool_allocator_tag { };
sl@0
   115
sl@0
   116
template <typename T,
sl@0
   117
    typename UserAllocator,
sl@0
   118
    typename Mutex,
sl@0
   119
    unsigned NextSize>
sl@0
   120
class fast_pool_allocator
sl@0
   121
{
sl@0
   122
  public:
sl@0
   123
    typedef T value_type;
sl@0
   124
    typedef UserAllocator user_allocator;
sl@0
   125
    typedef Mutex mutex;
sl@0
   126
    BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize);
sl@0
   127
sl@0
   128
    typedef value_type * pointer;
sl@0
   129
    typedef const value_type * const_pointer;
sl@0
   130
    typedef value_type & reference;
sl@0
   131
    typedef const value_type & const_reference;
sl@0
   132
    typedef typename pool<UserAllocator>::size_type size_type;
sl@0
   133
    typedef typename pool<UserAllocator>::difference_type difference_type;
sl@0
   134
sl@0
   135
    template <typename U>
sl@0
   136
    struct rebind
sl@0
   137
    {
sl@0
   138
      typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize> other;
sl@0
   139
    };
sl@0
   140
sl@0
   141
  public:
sl@0
   142
    fast_pool_allocator() { }
sl@0
   143
sl@0
   144
    // default copy constructor
sl@0
   145
sl@0
   146
    // default assignment operator
sl@0
   147
sl@0
   148
    // not explicit, mimicking std::allocator [20.4.1]
sl@0
   149
    template <typename U>
sl@0
   150
    fast_pool_allocator(
sl@0
   151
        const fast_pool_allocator<U, UserAllocator, Mutex, NextSize> &)
sl@0
   152
    { }
sl@0
   153
sl@0
   154
    // default destructor
sl@0
   155
sl@0
   156
    static pointer address(reference r)
sl@0
   157
    { return &r; }
sl@0
   158
    static const_pointer address(const_reference s)
sl@0
   159
    { return &s; }
sl@0
   160
    static size_type max_size()
sl@0
   161
    { return (std::numeric_limits<size_type>::max)(); }
sl@0
   162
    void construct(const pointer ptr, const value_type & t)
sl@0
   163
    { new (ptr) T(t); }
sl@0
   164
    void destroy(const pointer ptr)
sl@0
   165
    {
sl@0
   166
      ptr->~T();
sl@0
   167
      (void) ptr; // avoid unused variable warning
sl@0
   168
    }
sl@0
   169
sl@0
   170
    bool operator==(const fast_pool_allocator &) const
sl@0
   171
    { return true; }
sl@0
   172
    bool operator!=(const fast_pool_allocator &) const
sl@0
   173
    { return false; }
sl@0
   174
sl@0
   175
    static pointer allocate(const size_type n)
sl@0
   176
    {
sl@0
   177
      const pointer ret = (n == 1) ? 
sl@0
   178
          static_cast<pointer>(
sl@0
   179
              singleton_pool<fast_pool_allocator_tag, sizeof(T),
sl@0
   180
                  UserAllocator, Mutex, NextSize>::malloc() ) :
sl@0
   181
          static_cast<pointer>(
sl@0
   182
              singleton_pool<fast_pool_allocator_tag, sizeof(T),
sl@0
   183
                  UserAllocator, Mutex, NextSize>::ordered_malloc(n) );
sl@0
   184
      if (ret == 0)
sl@0
   185
        throw std::bad_alloc();
sl@0
   186
      return ret;
sl@0
   187
    }
sl@0
   188
    static pointer allocate(const size_type n, const void * const)
sl@0
   189
    { return allocate(n); }
sl@0
   190
    static pointer allocate()
sl@0
   191
    {
sl@0
   192
      const pointer ret = static_cast<pointer>(
sl@0
   193
          singleton_pool<fast_pool_allocator_tag, sizeof(T),
sl@0
   194
              UserAllocator, Mutex, NextSize>::malloc() );
sl@0
   195
      if (ret == 0)
sl@0
   196
        throw std::bad_alloc();
sl@0
   197
      return ret;
sl@0
   198
    }
sl@0
   199
    static void deallocate(const pointer ptr, const size_type n)
sl@0
   200
    {
sl@0
   201
#ifdef BOOST_NO_PROPER_STL_DEALLOCATE
sl@0
   202
      if (ptr == 0 || n == 0)
sl@0
   203
        return;
sl@0
   204
#endif
sl@0
   205
      if (n == 1)
sl@0
   206
        singleton_pool<fast_pool_allocator_tag, sizeof(T),
sl@0
   207
            UserAllocator, Mutex, NextSize>::free(ptr);
sl@0
   208
      else
sl@0
   209
        singleton_pool<fast_pool_allocator_tag, sizeof(T),
sl@0
   210
            UserAllocator, Mutex, NextSize>::free(ptr, n);
sl@0
   211
    }
sl@0
   212
    static void deallocate(const pointer ptr)
sl@0
   213
    {
sl@0
   214
      singleton_pool<fast_pool_allocator_tag, sizeof(T),
sl@0
   215
          UserAllocator, Mutex, NextSize>::free(ptr);
sl@0
   216
    }
sl@0
   217
};
sl@0
   218
sl@0
   219
} // namespace boost
sl@0
   220
sl@0
   221
#endif