# array_allocator.h

Go to the documentation of this file.

    00001 // array allocator -*- C++ -*-
    00002
    00003 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /** @file ext/array_allocator.h
    00031  *  This file is a GNU extension to the Standard C++ Library.
    00032  */
    00033
    00034 #ifndef _ARRAY_ALLOCATOR_H
    00035 #define _ARRAY_ALLOCATOR_H 1
    00036
    00037 #include <cstddef>
    00038 #include <new>
    00039 #include <bits/functexcept.h>
    00040 #include <tr1/array>
    00041
    00042 namespace __gnu_cxx
    00043 {
    00044   /// @brief  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(__p) value_type(__val); }
    00078
    00079       void
    00080       destroy(pointer __p) { __p->~_Tp(); }
    00081     };
    00082
    00083   /**
    00084    *  @brief  An allocator that uses previously allocated memory.
    00085    *  This memory can be externally, globally, or otherwise allocated.
    00086    */
    00087   template<typename _Tp, typename _Array = std::tr1::array<_Tp> >
    00088     class array_allocator : public array_allocator_base<_Tp>
    00089     {
    00090     public:
    00091       typedef size_t     size_type;
    00092       typedef ptrdiff_t  difference_type;
    00093       typedef _Tp*       pointer;
    00094       typedef const _Tp* const_pointer;
    00095       typedef _Tp&       reference;
    00096       typedef const _Tp& const_reference;
    00097       typedef _Tp        value_type;
    00098
    00099       typedef _Array    array_type;
    00100
    00101       array_type* _M_array;
    00102
    00103      template<typename _Tp1, typename _Array1 = _Array>
    00104         struct rebind
    00105         { typedef array_allocator<_Tp1, _Array1> other; };
    00106
    00107       array_allocator(array_type* __array = NULL) throw()
    00108       : _M_array(__array)
    00109       { }
    00110
    00111       array_allocator(const array_allocator& __o)  throw()
    00112       : _M_array(__o._M_array) { }
    00113
    00114       template<typename _Tp1, typename _Array1>
    00115         array_allocator(const array_allocator<_Tp1, _Array1>&) throw()
    00116     : _M_array(NULL) { }
    00117
    00118       ~array_allocator() throw() { }
    00119
    00120       pointer
    00121       allocate(size_type __n, const void* = 0)
    00122       {
    00123     static size_type __array_used;
    00124     if (_M_array == 0 || __array_used + __n > _M_array->size())
    00125       std::__throw_bad_alloc();
    00126     pointer __ret = _M_array->begin() + __array_used;
    00127     __array_used += __n;
    00128     return __ret;
    00129       }
    00130     };
    00131
    00132   template<typename _Tp, typename _Array>
    00133     inline bool
    00134     operator==(const array_allocator<_Tp, _Array>&,
    00135            const array_allocator<_Tp, _Array>&)
    00136     { return true; }
    00137
    00138   template<typename _Tp, typename _Array>
    00139     inline bool
    00140     operator!=(const array_allocator<_Tp, _Array>&,
    00141            const array_allocator<_Tp, _Array>&)
    00142     { return false; }
    00143 } // namespace __gnu_cxx
    00144
    00145 #endif