# new_allocator.h

    00001 // Allocator that wraps operator new -*- C++ -*-
    00002
    00003 // Copyright (C) 2001, 2002, 2003, 2004 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 #ifndef _NEW_ALLOCATOR_H
    00031 #define _NEW_ALLOCATOR_H 1
    00032
    00033 #include <new>
    00034
    00035 namespace __gnu_cxx
    00036 {
    00037   /**
    00038    *  @brief  An allocator that uses global new, as per [20.4].
    00039    *
    00040    *  This is precisely the allocator defined in the C++ Standard.
    00041    *    - all allocation calls operator new
    00042    *    - all deallocation calls operator delete
    00043    *
    00044    *  (See @link Allocators allocators info @endlink for more.)
    00045    */
    00046   template<typename _Tp>
    00047     class new_allocator
    00048     {
    00049     public:
    00050       typedef size_t     size_type;
    00051       typedef ptrdiff_t  difference_type;
    00052       typedef _Tp*       pointer;
    00053       typedef const _Tp* const_pointer;
    00054       typedef _Tp&       reference;
    00055       typedef const _Tp& const_reference;
    00056       typedef _Tp        value_type;
    00057
    00058       template<typename _Tp1>
    00059         struct rebind
    00060         { typedef new_allocator<_Tp1> other; };
    00061
    00062       new_allocator() throw() { }
    00063
    00064       new_allocator(const new_allocator&) throw() { }
    00065
    00066       template<typename _Tp1>
    00067         new_allocator(const new_allocator<_Tp1>&) throw() { }
    00068
    00069       ~new_allocator() throw() { }
    00070
    00071       pointer
    00072       address(reference __x) const { return &__x; }
    00073
    00074       const_pointer
    00075       address(const_reference __x) const { return &__x; }
    00076
    00077       // NB: __n is permitted to be 0.  The C++ standard says nothing
    00078       // about what the return value is when __n == 0.
    00079       pointer
    00080       allocate(size_type __n, const void* = 0)
    00081       { return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); }
    00082
    00083       // __p is not permitted to be a null pointer.
    00084       void
    00085       deallocate(pointer __p, size_type)
    00086       { ::operator delete(__p); }
    00087
    00088       size_type
    00089       max_size() const throw()
    00090       { return size_t(-1) / sizeof(_Tp); }
    00091
    00092       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00093       // 402. wrong new expression in [some_] allocator::construct
    00094       void
    00095       construct(pointer __p, const _Tp& __val)
    00096       { ::new(__p) _Tp(__val); }
    00097
    00098       void
    00099       destroy(pointer __p) { __p->~_Tp(); }
    00100     };
    00101
    00102   template<typename _Tp>
    00103     inline bool
    00104     operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
    00105     { return true; }
    00106
    00107   template<typename _Tp>
    00108     inline bool
    00109     operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
    00110     { return false; }
    00111 } // namespace __gnu_cxx
    00112
    00113 #endif