# array_allocator.h

Go to the documentation of this file.

    00001 // array allocator -*- C++ -*-
    00002
    00003 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 3, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // Under Section 7 of GPL version 3, you are granted additional
    00018 // permissions described in the GCC Runtime Library Exception, version
    00019 // 3.1, as published by the Free Software Foundation.
    00020
    00021 // You should have received a copy of the GNU General Public License and
    00022 // a copy of the GCC Runtime Library Exception along with this program;
    00023 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
    00024 // <http://www.gnu.org/licenses/>.
    00025
    00026 /** @file ext/array_allocator.h
    00027  *  This file is a GNU extension to the Standard C++ Library.
    00028  */
    00029
    00030 #ifndef _ARRAY_ALLOCATOR_H
    00031 #define _ARRAY_ALLOCATOR_H 1
    00032
    00033 #include <cstddef>
    00034 #include <new>
    00035 #include <bits/functexcept.h>
    00036 #include <tr1/array>
    00037 #include <bits/move.h>
    00038
    00039 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
    00040
    00041  using std::size_t;
    00042  using std::ptrdiff_t;
    00043
    00044   /// Base class.
    00045  template<typename _Tp>
    00046     class array_allocator_base
    00047     {
    00048     public:
    00049       typedef size_t        size_type;
    00050       typedef ptrdiff_t     difference_type;
    00051       typedef _Tp*          pointer;
    00052       typedef const _Tp*    const_pointer;
    00053       typedef _Tp&          reference;
    00054       typedef const _Tp&    const_reference;
    00055       typedef _Tp           value_type;
    00056
    00057       pointer
    00058       address(reference __x) const { return &__x; }
    00059
    00060       const_pointer
    00061       address(const_reference __x) const { return &__x; }
    00062
    00063       void
    00064       deallocate(pointer, size_type)
    00065       {
    00066     // Does nothing.
    00067       }
    00068
    00069       size_type
    00070       max_size() const throw()
    00071       { return size_t(-1) / sizeof(_Tp); }
    00072
    00073       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00074       // 402. wrong new expression in [some_] allocator::construct
    00075       void
    00076       construct(pointer __p, const _Tp& __val)
    00077       { ::new((void *)__p) value_type(__val); }
    00078
    00079 #ifdef __GXX_EXPERIMENTAL_CXX0X__
    00080       template<typename... _Args>
    00081         void
    00082         construct(pointer __p, _Args&&... __args)
    00083     { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
    00084 #endif
    00085
    00086       void
    00087       destroy(pointer __p) { __p->~_Tp(); }
    00088     };
    00089
    00090   /**
    00091    *  @brief  An allocator that uses previously allocated memory.
    00092    *  This memory can be externally, globally, or otherwise allocated.
    00093    *  @ingroup allocators
    00094    */
    00095   template<typename _Tp, typename _Array = std::tr1::array<_Tp, 1> >
    00096     class array_allocator : public array_allocator_base<_Tp>
    00097     {
    00098     public:
    00099       typedef size_t        size_type;
    00100       typedef ptrdiff_t     difference_type;
    00101       typedef _Tp*          pointer;
    00102       typedef const _Tp*    const_pointer;
    00103       typedef _Tp&          reference;
    00104       typedef const _Tp&    const_reference;
    00105       typedef _Tp           value_type;
    00106       typedef _Array        array_type;
    00107
    00108     private:
    00109       array_type*   _M_array;
    00110       size_type     _M_used;
    00111
    00112     public:
    00113      template<typename _Tp1, typename _Array1 = _Array>
    00114         struct rebind
    00115         { typedef array_allocator<_Tp1, _Array1> other; };
    00116
    00117       array_allocator(array_type* __array = NULL) throw()
    00118       : _M_array(__array), _M_used(size_type()) { }
    00119
    00120       array_allocator(const array_allocator& __o)  throw()
    00121       : _M_array(__o._M_array), _M_used(__o._M_used) { }
    00122
    00123       template<typename _Tp1, typename _Array1>
    00124         array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
    00125     : _M_array(NULL), _M_used(size_type()) { }
    00126
    00127       ~array_allocator() throw() { }
    00128
    00129       pointer
    00130       allocate(size_type __n, const void* = 0)
    00131       {
    00132     if (_M_array == 0 || _M_used + __n > _M_array->size())
    00133       std::__throw_bad_alloc();
    00134     pointer __ret = _M_array->begin() + _M_used;
    00135     _M_used += __n;
    00136     return __ret;
    00137       }
    00138     };
    00139
    00140   template<typename _Tp, typename _Array>
    00141     inline bool
    00142     operator==(const array_allocator<_Tp, _Array>&,
    00143            const array_allocator<_Tp, _Array>&)
    00144     { return true; }
    00145
    00146   template<typename _Tp, typename _Array>
    00147     inline bool
    00148     operator!=(const array_allocator<_Tp, _Array>&,
    00149            const array_allocator<_Tp, _Array>&)
    00150     { return false; }
    00151
    00152 _GLIBCXX_END_NAMESPACE
    00153
    00154 #endif