1 // Allocator that wraps "C" malloc -*- C++ -*-
2 
3 // Copyright (C) 2001-2021 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file ext/malloc_allocator.h
26  *  This file is a GNU extension to the Standard C++ Library.
27  */
28 
29 #ifndef _MALLOC_ALLOCATOR_H
30 #define _MALLOC_ALLOCATOR_H 1
31 
32 #include <cstdlib>
33 #include <cstddef>
34 #include <new>
35 #include <bits/functexcept.h>
36 #include <bits/move.h>
37 #if __cplusplus >= 201103L
38 #include <type_traits>
39 #endif
40 
_GLIBCXX_VISIBILITY(default)41 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42 {
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 
45   /**
46    *  @brief  An allocator that uses malloc.
47    *  @ingroup allocators
48    *
49    *  This is precisely the allocator defined in the C++ Standard.
50    *    - all allocation calls malloc
51    *    - all deallocation calls free
52    */
53   template<typename _Tp>
54     class malloc_allocator
55     {
56     public:
57       typedef _Tp        value_type;
58       typedef std::size_t     size_type;
59       typedef std::ptrdiff_t  difference_type;
60 #if __cplusplus <= 201703L
61       typedef _Tp*       pointer;
62       typedef const _Tp* const_pointer;
63       typedef _Tp&       reference;
64       typedef const _Tp& const_reference;
65 
66       template<typename _Tp1>
67         struct rebind
68         { typedef malloc_allocator<_Tp1> other; };
69 #endif
70 
71 #if __cplusplus >= 201103L
72       // _GLIBCXX_RESOLVE_LIB_DEFECTS
73       // 2103. propagate_on_container_move_assignment
74       typedef std::true_type propagate_on_container_move_assignment;
75 #endif
76 
77       _GLIBCXX20_CONSTEXPR
78       malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
79 
80       _GLIBCXX20_CONSTEXPR
81       malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
82 
83       template<typename _Tp1>
84 	_GLIBCXX20_CONSTEXPR
85         malloc_allocator(const malloc_allocator<_Tp1>&)
86 	_GLIBCXX_USE_NOEXCEPT { }
87 
88 #if __cplusplus <= 201703L
89       ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
90 
91       pointer
92       address(reference __x) const _GLIBCXX_NOEXCEPT
93       { return std::__addressof(__x); }
94 
95       const_pointer
96       address(const_reference __x) const _GLIBCXX_NOEXCEPT
97       { return std::__addressof(__x); }
98 #endif
99 
100       // NB: __n is permitted to be 0.  The C++ standard says nothing
101       // about what the return value is when __n == 0.
102       _GLIBCXX_NODISCARD _Tp*
103       allocate(size_type __n, const void* = 0)
104       {
105 #if __cplusplus >= 201103L
106 	 // _GLIBCXX_RESOLVE_LIB_DEFECTS
107 	 // 3308. std::allocator<void>().allocate(n)
108 	 static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");
109 #endif
110 
111 	if (__builtin_expect(__n > this->_M_max_size(), false))
112 	  {
113 	    // _GLIBCXX_RESOLVE_LIB_DEFECTS
114 	    // 3190. allocator::allocate sometimes returns too little storage
115 	    if (__n > (std::size_t(-1) / sizeof(_Tp)))
116 	      std::__throw_bad_array_new_length();
117 	    std::__throw_bad_alloc();
118 	  }
119 
120 	_Tp* __ret = 0;
121 #if __cpp_aligned_new
122 #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
123 	if (alignof(_Tp) > alignof(std::max_align_t))
124 	  {
125 	    __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
126 						      __n * sizeof(_Tp)));
127 	  }
128 #else
129 # define _GLIBCXX_CHECK_MALLOC_RESULT
130 #endif
131 #endif
132 	if (!__ret)
133 	  __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
134 	if (!__ret)
135 	  std::__throw_bad_alloc();
136 #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
137 #undef _GLIBCXX_CHECK_MALLOC_RESULT
138 	  if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
139 	    {
140 	      // Memory returned by malloc is not suitably aligned for _Tp.
141 	      deallocate(__ret, __n);
142 	      std::__throw_bad_alloc();
143 	    }
144 #endif
145 	return __ret;
146       }
147 
148       // __p is not permitted to be a null pointer.
149       void
150       deallocate(_Tp* __p, size_type)
151       { std::free(static_cast<void*>(__p)); }
152 
153 #if __cplusplus <= 201703L
154       size_type
155       max_size() const _GLIBCXX_USE_NOEXCEPT
156       { return _M_max_size(); }
157 
158 #if __cplusplus >= 201103L
159       template<typename _Up, typename... _Args>
160         void
161         construct(_Up* __p, _Args&&... __args)
162 	noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
163 	{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
164 
165       template<typename _Up>
166         void
167         destroy(_Up* __p)
168 	noexcept(std::is_nothrow_destructible<_Up>::value)
169 	{ __p->~_Up(); }
170 #else
171       // _GLIBCXX_RESOLVE_LIB_DEFECTS
172       // 402. wrong new expression in [some_] allocator::construct
173       void
174       construct(pointer __p, const _Tp& __val)
175       { ::new((void *)__p) value_type(__val); }
176 
177       void
178       destroy(pointer __p) { __p->~_Tp(); }
179 #endif
180 #endif // ! C++20
181 
182       template<typename _Up>
183 	friend _GLIBCXX20_CONSTEXPR bool
184 	operator==(const malloc_allocator&, const malloc_allocator<_Up>&)
185 	_GLIBCXX_NOTHROW
186 	{ return true; }
187 
188 #if __cpp_impl_three_way_comparison < 201907L
189       template<typename _Up>
190 	friend _GLIBCXX20_CONSTEXPR bool
191 	operator!=(const malloc_allocator&, const malloc_allocator<_Up>&)
192 	_GLIBCXX_NOTHROW
193 	{ return false; }
194 #endif
195 
196     private:
197       _GLIBCXX_CONSTEXPR size_type
198       _M_max_size() const _GLIBCXX_USE_NOEXCEPT
199       {
200 #if __PTRDIFF_MAX__ < __SIZE_MAX__
201 	return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
202 #else
203 	return std::size_t(-1) / sizeof(_Tp);
204 #endif
205       }
206     };
207 
208 _GLIBCXX_END_NAMESPACE_VERSION
209 } // namespace
210 
211 #endif
212