xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/ext/malloc_allocator.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj // Allocator that wraps "C" malloc -*- 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/malloc_allocator.h
26*38fd1498Szrj  *  This file is a GNU extension to the Standard C++ Library.
27*38fd1498Szrj  */
28*38fd1498Szrj 
29*38fd1498Szrj #ifndef _MALLOC_ALLOCATOR_H
30*38fd1498Szrj #define _MALLOC_ALLOCATOR_H 1
31*38fd1498Szrj 
32*38fd1498Szrj #include <cstdlib>
33*38fd1498Szrj #include <cstddef>
34*38fd1498Szrj #include <new>
35*38fd1498Szrj #include <bits/functexcept.h>
36*38fd1498Szrj #include <bits/move.h>
37*38fd1498Szrj #if __cplusplus >= 201103L
38*38fd1498Szrj #include <type_traits>
39*38fd1498Szrj #endif
40*38fd1498Szrj 
_GLIBCXX_VISIBILITY(default)41*38fd1498Szrj namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42*38fd1498Szrj {
43*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
44*38fd1498Szrj 
45*38fd1498Szrj   using std::size_t;
46*38fd1498Szrj   using std::ptrdiff_t;
47*38fd1498Szrj 
48*38fd1498Szrj   /**
49*38fd1498Szrj    *  @brief  An allocator that uses malloc.
50*38fd1498Szrj    *  @ingroup allocators
51*38fd1498Szrj    *
52*38fd1498Szrj    *  This is precisely the allocator defined in the C++ Standard.
53*38fd1498Szrj    *    - all allocation calls malloc
54*38fd1498Szrj    *    - all deallocation calls free
55*38fd1498Szrj    */
56*38fd1498Szrj   template<typename _Tp>
57*38fd1498Szrj     class malloc_allocator
58*38fd1498Szrj     {
59*38fd1498Szrj     public:
60*38fd1498Szrj       typedef size_t     size_type;
61*38fd1498Szrj       typedef ptrdiff_t  difference_type;
62*38fd1498Szrj       typedef _Tp*       pointer;
63*38fd1498Szrj       typedef const _Tp* const_pointer;
64*38fd1498Szrj       typedef _Tp&       reference;
65*38fd1498Szrj       typedef const _Tp& const_reference;
66*38fd1498Szrj       typedef _Tp        value_type;
67*38fd1498Szrj 
68*38fd1498Szrj       template<typename _Tp1>
69*38fd1498Szrj         struct rebind
70*38fd1498Szrj         { typedef malloc_allocator<_Tp1> other; };
71*38fd1498Szrj 
72*38fd1498Szrj #if __cplusplus >= 201103L
73*38fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
74*38fd1498Szrj       // 2103. propagate_on_container_move_assignment
75*38fd1498Szrj       typedef std::true_type propagate_on_container_move_assignment;
76*38fd1498Szrj #endif
77*38fd1498Szrj 
78*38fd1498Szrj       malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
79*38fd1498Szrj 
80*38fd1498Szrj       malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
81*38fd1498Szrj 
82*38fd1498Szrj       template<typename _Tp1>
83*38fd1498Szrj         malloc_allocator(const malloc_allocator<_Tp1>&)
84*38fd1498Szrj 	_GLIBCXX_USE_NOEXCEPT { }
85*38fd1498Szrj 
86*38fd1498Szrj       ~malloc_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* = 0)
100*38fd1498Szrj       {
101*38fd1498Szrj 	if (__n > this->max_size())
102*38fd1498Szrj 	  std::__throw_bad_alloc();
103*38fd1498Szrj 
104*38fd1498Szrj 	pointer __ret = 0;
105*38fd1498Szrj #if __cpp_aligned_new
106*38fd1498Szrj #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
107*38fd1498Szrj 	if (alignof(_Tp) > alignof(std::max_align_t))
108*38fd1498Szrj 	  {
109*38fd1498Szrj 	    __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
110*38fd1498Szrj 						      __n * sizeof(_Tp)));
111*38fd1498Szrj 	  }
112*38fd1498Szrj #else
113*38fd1498Szrj # define _GLIBCXX_CHECK_MALLOC_RESULT
114*38fd1498Szrj #endif
115*38fd1498Szrj #endif
116*38fd1498Szrj 	if (!__ret)
117*38fd1498Szrj 	  __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
118*38fd1498Szrj 	if (!__ret)
119*38fd1498Szrj 	  std::__throw_bad_alloc();
120*38fd1498Szrj #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
121*38fd1498Szrj #undef _GLIBCXX_CHECK_MALLOC_RESULT
122*38fd1498Szrj 	  if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
123*38fd1498Szrj 	    {
124*38fd1498Szrj 	      // Memory returned by malloc is not suitably aligned for _Tp.
125*38fd1498Szrj 	      deallocate(__ret, __n);
126*38fd1498Szrj 	      std::__throw_bad_alloc();
127*38fd1498Szrj 	    }
128*38fd1498Szrj #endif
129*38fd1498Szrj 	return __ret;
130*38fd1498Szrj       }
131*38fd1498Szrj 
132*38fd1498Szrj       // __p is not permitted to be a null pointer.
133*38fd1498Szrj       void
134*38fd1498Szrj       deallocate(pointer __p, size_type)
135*38fd1498Szrj       { std::free(static_cast<void*>(__p)); }
136*38fd1498Szrj 
137*38fd1498Szrj       size_type
138*38fd1498Szrj       max_size() const _GLIBCXX_USE_NOEXCEPT
139*38fd1498Szrj       { return size_t(-1) / sizeof(_Tp); }
140*38fd1498Szrj 
141*38fd1498Szrj #if __cplusplus >= 201103L
142*38fd1498Szrj       template<typename _Up, typename... _Args>
143*38fd1498Szrj         void
144*38fd1498Szrj         construct(_Up* __p, _Args&&... __args)
145*38fd1498Szrj 	{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
146*38fd1498Szrj 
147*38fd1498Szrj       template<typename _Up>
148*38fd1498Szrj         void
149*38fd1498Szrj         destroy(_Up* __p) { __p->~_Up(); }
150*38fd1498Szrj #else
151*38fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
152*38fd1498Szrj       // 402. wrong new expression in [some_] allocator::construct
153*38fd1498Szrj       void
154*38fd1498Szrj       construct(pointer __p, const _Tp& __val)
155*38fd1498Szrj       { ::new((void *)__p) value_type(__val); }
156*38fd1498Szrj 
157*38fd1498Szrj       void
158*38fd1498Szrj       destroy(pointer __p) { __p->~_Tp(); }
159*38fd1498Szrj #endif
160*38fd1498Szrj     };
161*38fd1498Szrj 
162*38fd1498Szrj   template<typename _Tp>
163*38fd1498Szrj     inline bool
164*38fd1498Szrj     operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
165*38fd1498Szrj     { return true; }
166*38fd1498Szrj 
167*38fd1498Szrj   template<typename _Tp>
168*38fd1498Szrj     inline bool
169*38fd1498Szrj     operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
170*38fd1498Szrj     { return false; }
171*38fd1498Szrj 
172*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
173*38fd1498Szrj } // namespace
174*38fd1498Szrj 
175*38fd1498Szrj #endif
176