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