xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/ext/malloc_allocator.h (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
136ac495dSmrg // Allocator that wraps "C" malloc -*- C++ -*-
236ac495dSmrg 
3*8feb0f0bSmrg // Copyright (C) 2001-2020 Free Software Foundation, Inc.
436ac495dSmrg //
536ac495dSmrg // This file is part of the GNU ISO C++ Library.  This library is free
636ac495dSmrg // software; you can redistribute it and/or modify it under the
736ac495dSmrg // terms of the GNU General Public License as published by the
836ac495dSmrg // Free Software Foundation; either version 3, or (at your option)
936ac495dSmrg // any later version.
1036ac495dSmrg 
1136ac495dSmrg // This library is distributed in the hope that it will be useful,
1236ac495dSmrg // but WITHOUT ANY WARRANTY; without even the implied warranty of
1336ac495dSmrg // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1436ac495dSmrg // GNU General Public License for more details.
1536ac495dSmrg 
1636ac495dSmrg // Under Section 7 of GPL version 3, you are granted additional
1736ac495dSmrg // permissions described in the GCC Runtime Library Exception, version
1836ac495dSmrg // 3.1, as published by the Free Software Foundation.
1936ac495dSmrg 
2036ac495dSmrg // You should have received a copy of the GNU General Public License and
2136ac495dSmrg // a copy of the GCC Runtime Library Exception along with this program;
2236ac495dSmrg // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2336ac495dSmrg // <http://www.gnu.org/licenses/>.
2436ac495dSmrg 
2536ac495dSmrg /** @file ext/malloc_allocator.h
2636ac495dSmrg  *  This file is a GNU extension to the Standard C++ Library.
2736ac495dSmrg  */
2836ac495dSmrg 
2936ac495dSmrg #ifndef _MALLOC_ALLOCATOR_H
3036ac495dSmrg #define _MALLOC_ALLOCATOR_H 1
3136ac495dSmrg 
3236ac495dSmrg #include <cstdlib>
3336ac495dSmrg #include <cstddef>
3436ac495dSmrg #include <new>
3536ac495dSmrg #include <bits/functexcept.h>
3636ac495dSmrg #include <bits/move.h>
3736ac495dSmrg #if __cplusplus >= 201103L
3836ac495dSmrg #include <type_traits>
3936ac495dSmrg #endif
4036ac495dSmrg 
_GLIBCXX_VISIBILITY(default)4136ac495dSmrg namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
4236ac495dSmrg {
4336ac495dSmrg _GLIBCXX_BEGIN_NAMESPACE_VERSION
4436ac495dSmrg 
4536ac495dSmrg   /**
4636ac495dSmrg    *  @brief  An allocator that uses malloc.
4736ac495dSmrg    *  @ingroup allocators
4836ac495dSmrg    *
4936ac495dSmrg    *  This is precisely the allocator defined in the C++ Standard.
5036ac495dSmrg    *    - all allocation calls malloc
5136ac495dSmrg    *    - all deallocation calls free
5236ac495dSmrg    */
5336ac495dSmrg   template<typename _Tp>
5436ac495dSmrg     class malloc_allocator
5536ac495dSmrg     {
5636ac495dSmrg     public:
57*8feb0f0bSmrg       typedef _Tp        value_type;
58*8feb0f0bSmrg       typedef std::size_t     size_type;
59*8feb0f0bSmrg       typedef std::ptrdiff_t  difference_type;
60*8feb0f0bSmrg #if __cplusplus <= 201703L
6136ac495dSmrg       typedef _Tp*       pointer;
6236ac495dSmrg       typedef const _Tp* const_pointer;
6336ac495dSmrg       typedef _Tp&       reference;
6436ac495dSmrg       typedef const _Tp& const_reference;
6536ac495dSmrg 
6636ac495dSmrg       template<typename _Tp1>
6736ac495dSmrg         struct rebind
6836ac495dSmrg         { typedef malloc_allocator<_Tp1> other; };
69*8feb0f0bSmrg #endif
7036ac495dSmrg 
7136ac495dSmrg #if __cplusplus >= 201103L
7236ac495dSmrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
7336ac495dSmrg       // 2103. propagate_on_container_move_assignment
7436ac495dSmrg       typedef std::true_type propagate_on_container_move_assignment;
7536ac495dSmrg #endif
7636ac495dSmrg 
77c0a68be4Smrg       _GLIBCXX20_CONSTEXPR
7836ac495dSmrg       malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
7936ac495dSmrg 
80c0a68be4Smrg       _GLIBCXX20_CONSTEXPR
8136ac495dSmrg       malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
8236ac495dSmrg 
8336ac495dSmrg       template<typename _Tp1>
84c0a68be4Smrg 	_GLIBCXX20_CONSTEXPR
8536ac495dSmrg         malloc_allocator(const malloc_allocator<_Tp1>&)
8636ac495dSmrg 	_GLIBCXX_USE_NOEXCEPT { }
8736ac495dSmrg 
88*8feb0f0bSmrg #if __cplusplus <= 201703L
8936ac495dSmrg       ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
9036ac495dSmrg 
9136ac495dSmrg       pointer
9236ac495dSmrg       address(reference __x) const _GLIBCXX_NOEXCEPT
9336ac495dSmrg       { return std::__addressof(__x); }
9436ac495dSmrg 
9536ac495dSmrg       const_pointer
9636ac495dSmrg       address(const_reference __x) const _GLIBCXX_NOEXCEPT
9736ac495dSmrg       { return std::__addressof(__x); }
98*8feb0f0bSmrg #endif
9936ac495dSmrg 
10036ac495dSmrg       // NB: __n is permitted to be 0.  The C++ standard says nothing
10136ac495dSmrg       // about what the return value is when __n == 0.
102*8feb0f0bSmrg       _GLIBCXX_NODISCARD _Tp*
10336ac495dSmrg       allocate(size_type __n, const void* = 0)
10436ac495dSmrg       {
105*8feb0f0bSmrg #if __cplusplus >= 201103L
106*8feb0f0bSmrg 	 // _GLIBCXX_RESOLVE_LIB_DEFECTS
107*8feb0f0bSmrg 	 // 3308. std::allocator<void>().allocate(n)
108*8feb0f0bSmrg 	 static_assert(sizeof(_Tp) != 0, "cannot allocate incomplete types");
109*8feb0f0bSmrg #endif
110*8feb0f0bSmrg 
111*8feb0f0bSmrg 	if (__n > this->_M_max_size())
11236ac495dSmrg 	  std::__throw_bad_alloc();
11336ac495dSmrg 
114*8feb0f0bSmrg 	_Tp* __ret = 0;
11536ac495dSmrg #if __cpp_aligned_new
11636ac495dSmrg #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
11736ac495dSmrg 	if (alignof(_Tp) > alignof(std::max_align_t))
11836ac495dSmrg 	  {
11936ac495dSmrg 	    __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
12036ac495dSmrg 						      __n * sizeof(_Tp)));
12136ac495dSmrg 	  }
12236ac495dSmrg #else
12336ac495dSmrg # define _GLIBCXX_CHECK_MALLOC_RESULT
12436ac495dSmrg #endif
12536ac495dSmrg #endif
12636ac495dSmrg 	if (!__ret)
12736ac495dSmrg 	  __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
12836ac495dSmrg 	if (!__ret)
12936ac495dSmrg 	  std::__throw_bad_alloc();
13036ac495dSmrg #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
13136ac495dSmrg #undef _GLIBCXX_CHECK_MALLOC_RESULT
13236ac495dSmrg 	  if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
13336ac495dSmrg 	    {
13436ac495dSmrg 	      // Memory returned by malloc is not suitably aligned for _Tp.
13536ac495dSmrg 	      deallocate(__ret, __n);
13636ac495dSmrg 	      std::__throw_bad_alloc();
13736ac495dSmrg 	    }
13836ac495dSmrg #endif
13936ac495dSmrg 	return __ret;
14036ac495dSmrg       }
14136ac495dSmrg 
14236ac495dSmrg       // __p is not permitted to be a null pointer.
14336ac495dSmrg       void
144*8feb0f0bSmrg       deallocate(_Tp* __p, size_type)
14536ac495dSmrg       { std::free(static_cast<void*>(__p)); }
14636ac495dSmrg 
147*8feb0f0bSmrg #if __cplusplus <= 201703L
14836ac495dSmrg       size_type
14936ac495dSmrg       max_size() const _GLIBCXX_USE_NOEXCEPT
150*8feb0f0bSmrg       { return _M_max_size(); }
15136ac495dSmrg 
15236ac495dSmrg #if __cplusplus >= 201103L
15336ac495dSmrg       template<typename _Up, typename... _Args>
15436ac495dSmrg         void
15536ac495dSmrg         construct(_Up* __p, _Args&&... __args)
156*8feb0f0bSmrg 	noexcept(std::is_nothrow_constructible<_Up, _Args...>::value)
15736ac495dSmrg 	{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
15836ac495dSmrg 
15936ac495dSmrg       template<typename _Up>
16036ac495dSmrg         void
161c0a68be4Smrg         destroy(_Up* __p)
162*8feb0f0bSmrg 	noexcept(std::is_nothrow_destructible<_Up>::value)
163c0a68be4Smrg 	{ __p->~_Up(); }
16436ac495dSmrg #else
16536ac495dSmrg       // _GLIBCXX_RESOLVE_LIB_DEFECTS
16636ac495dSmrg       // 402. wrong new expression in [some_] allocator::construct
16736ac495dSmrg       void
16836ac495dSmrg       construct(pointer __p, const _Tp& __val)
16936ac495dSmrg       { ::new((void *)__p) value_type(__val); }
17036ac495dSmrg 
17136ac495dSmrg       void
17236ac495dSmrg       destroy(pointer __p) { __p->~_Tp(); }
17336ac495dSmrg #endif
174*8feb0f0bSmrg #endif // ! C++20
17536ac495dSmrg 
176c0a68be4Smrg       template<typename _Up>
177*8feb0f0bSmrg 	friend _GLIBCXX20_CONSTEXPR bool
178c0a68be4Smrg 	operator==(const malloc_allocator&, const malloc_allocator<_Up>&)
179c0a68be4Smrg 	_GLIBCXX_NOTHROW
18036ac495dSmrg 	{ return true; }
18136ac495dSmrg 
182*8feb0f0bSmrg #if __cpp_impl_three_way_comparison < 201907L
183c0a68be4Smrg       template<typename _Up>
184*8feb0f0bSmrg 	friend _GLIBCXX20_CONSTEXPR bool
185c0a68be4Smrg 	operator!=(const malloc_allocator&, const malloc_allocator<_Up>&)
186c0a68be4Smrg 	_GLIBCXX_NOTHROW
18736ac495dSmrg 	{ return false; }
188*8feb0f0bSmrg #endif
189*8feb0f0bSmrg 
190*8feb0f0bSmrg     private:
191*8feb0f0bSmrg       _GLIBCXX_CONSTEXPR size_type
192*8feb0f0bSmrg       _M_max_size() const _GLIBCXX_USE_NOEXCEPT
193*8feb0f0bSmrg       {
194*8feb0f0bSmrg #if __PTRDIFF_MAX__ < __SIZE_MAX__
195*8feb0f0bSmrg 	return std::size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
196*8feb0f0bSmrg #else
197*8feb0f0bSmrg 	return std::size_t(-1) / sizeof(_Tp);
198*8feb0f0bSmrg #endif
199*8feb0f0bSmrg       }
200c0a68be4Smrg     };
20136ac495dSmrg 
20236ac495dSmrg _GLIBCXX_END_NAMESPACE_VERSION
20336ac495dSmrg } // namespace
20436ac495dSmrg 
20536ac495dSmrg #endif
206