Merged trunk at revision 161680 into branch.
[official-gcc.git] / libstdc++-v3 / include / ext / new_allocator.h
blob5450565420712b63f6081f501912be3af6e7a78d
1 // Allocator that wraps operator new -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file ext/new_allocator.h
27 * This file is a GNU extension to the Standard C++ Library.
30 #ifndef _NEW_ALLOCATOR_H
31 #define _NEW_ALLOCATOR_H 1
33 #include <bits/c++config.h>
34 #include <new>
35 #include <bits/functexcept.h>
36 #include <bits/move.h>
38 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
40 using std::size_t;
41 using std::ptrdiff_t;
43 /**
44 * @brief An allocator that uses global new, as per [20.4].
45 * @ingroup allocators
47 * This is precisely the allocator defined in the C++ Standard.
48 * - all allocation calls operator new
49 * - all deallocation calls operator delete
51 template<typename _Tp>
52 class new_allocator
54 public:
55 typedef size_t size_type;
56 typedef ptrdiff_t difference_type;
57 typedef _Tp* pointer;
58 typedef const _Tp* const_pointer;
59 typedef _Tp& reference;
60 typedef const _Tp& const_reference;
61 typedef _Tp value_type;
63 template<typename _Tp1>
64 struct rebind
65 { typedef new_allocator<_Tp1> other; };
67 new_allocator() throw() { }
69 new_allocator(const new_allocator&) throw() { }
71 template<typename _Tp1>
72 new_allocator(const new_allocator<_Tp1>&) throw() { }
74 ~new_allocator() throw() { }
76 pointer
77 address(reference __x) const { return std::__addressof(__x); }
79 const_pointer
80 address(const_reference __x) const { return std::__addressof(__x); }
82 // NB: __n is permitted to be 0. The C++ standard says nothing
83 // about what the return value is when __n == 0.
84 pointer
85 allocate(size_type __n, const void* = 0)
87 if (__n > this->max_size())
88 std::__throw_bad_alloc();
90 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
93 // __p is not permitted to be a null pointer.
94 void
95 deallocate(pointer __p, size_type)
96 { ::operator delete(__p); }
98 size_type
99 max_size() const throw()
100 { return size_t(-1) / sizeof(_Tp); }
102 // _GLIBCXX_RESOLVE_LIB_DEFECTS
103 // 402. wrong new expression in [some_] allocator::construct
104 void
105 construct(pointer __p, const _Tp& __val)
106 { ::new((void *)__p) _Tp(__val); }
108 #ifdef __GXX_EXPERIMENTAL_CXX0X__
109 template<typename... _Args>
110 void
111 construct(pointer __p, _Args&&... __args)
112 { ::new((void *)__p) _Tp(std::forward<_Args>(__args)...); }
113 #endif
115 void
116 destroy(pointer __p) { __p->~_Tp(); }
119 template<typename _Tp>
120 inline bool
121 operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
122 { return true; }
124 template<typename _Tp>
125 inline bool
126 operator!=(const new_allocator<_Tp>&, const new_allocator<_Tp>&)
127 { return false; }
129 _GLIBCXX_END_NAMESPACE
131 #endif