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