]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/malloc_allocator.h
Relocation (= move+destroy)
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / malloc_allocator.h
CommitLineData
d38d4e5d 1// Allocator that wraps "C" malloc -*- C++ -*-
1ff9402d 2
85ec4feb 3// Copyright (C) 2001-2018 Free Software Foundation, Inc.
1ff9402d
BK
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
1ff9402d
BK
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
748086b7
JJ
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/>.
1ff9402d 24
0aa06b18
BK
25/** @file ext/malloc_allocator.h
26 * This file is a GNU extension to the Standard C++ Library.
27 */
28
1ff9402d
BK
29#ifndef _MALLOC_ALLOCATOR_H
30#define _MALLOC_ALLOCATOR_H 1
31
4ec3604f 32#include <cstdlib>
ace4c2f0 33#include <cstddef>
d38d4e5d 34#include <new>
a063e891 35#include <bits/functexcept.h>
ca0f8fd1 36#include <bits/move.h>
1b5dc776
JW
37#if __cplusplus >= 201103L
38#include <type_traits>
39#endif
1ff9402d 40
12ffa228
BK
41namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
42{
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 44
05a2763e
MG
45 using std::size_t;
46 using std::ptrdiff_t;
47
1ff9402d 48 /**
0aa06b18 49 * @brief An allocator that uses malloc.
5b9daa7e 50 * @ingroup allocators
d38d4e5d
BK
51 *
52 * This is precisely the allocator defined in the C++ Standard.
53 * - all allocation calls malloc
54 * - all deallocation calls free
1ff9402d 55 */
d38d4e5d
BK
56 template<typename _Tp>
57 class malloc_allocator
1ff9402d 58 {
1ff9402d 59 public:
d38d4e5d
BK
60 typedef size_t size_type;
61 typedef ptrdiff_t difference_type;
62 typedef _Tp* pointer;
63 typedef const _Tp* const_pointer;
64 typedef _Tp& reference;
65 typedef const _Tp& const_reference;
66 typedef _Tp value_type;
1ff9402d 67
d38d4e5d
BK
68 template<typename _Tp1>
69 struct rebind
70 { typedef malloc_allocator<_Tp1> other; };
1ff9402d 71
1b5dc776
JW
72#if __cplusplus >= 201103L
73 // _GLIBCXX_RESOLVE_LIB_DEFECTS
74 // 2103. propagate_on_container_move_assignment
75 typedef std::true_type propagate_on_container_move_assignment;
76#endif
77
3be9ded2 78 _GLIBCXX20_CONSTEXPR
7d9cb054 79 malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
1ff9402d 80
3be9ded2 81 _GLIBCXX20_CONSTEXPR
7d9cb054 82 malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
d38d4e5d
BK
83
84 template<typename _Tp1>
3be9ded2 85 _GLIBCXX20_CONSTEXPR
7d9cb054
PC
86 malloc_allocator(const malloc_allocator<_Tp1>&)
87 _GLIBCXX_USE_NOEXCEPT { }
d38d4e5d 88
7d9cb054 89 ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
d38d4e5d
BK
90
91 pointer
7d9cb054
PC
92 address(reference __x) const _GLIBCXX_NOEXCEPT
93 { return std::__addressof(__x); }
d38d4e5d
BK
94
95 const_pointer
7d9cb054
PC
96 address(const_reference __x) const _GLIBCXX_NOEXCEPT
97 { return std::__addressof(__x); }
d38d4e5d
BK
98
99 // NB: __n is permitted to be 0. The C++ standard says nothing
100 // about what the return value is when __n == 0.
101 pointer
90f8b692 102 allocate(size_type __n, const void* = 0)
58c95921 103 {
e762c6f4 104 if (__n > this->max_size())
a063e891
PC
105 std::__throw_bad_alloc();
106
b08c2bc7 107 pointer __ret = 0;
ace4c2f0
JW
108#if __cpp_aligned_new
109#if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
110 if (alignof(_Tp) > alignof(std::max_align_t))
111 {
112 __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
113 __n * sizeof(_Tp)));
114 }
115#else
116# define _GLIBCXX_CHECK_MALLOC_RESULT
117#endif
118#endif
119 if (!__ret)
120 __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
58c95921 121 if (!__ret)
a063e891 122 std::__throw_bad_alloc();
ace4c2f0
JW
123#ifdef _GLIBCXX_CHECK_MALLOC_RESULT
124#undef _GLIBCXX_CHECK_MALLOC_RESULT
125 if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
126 {
127 // Memory returned by malloc is not suitably aligned for _Tp.
128 deallocate(__ret, __n);
129 std::__throw_bad_alloc();
130 }
131#endif
58c95921
DM
132 return __ret;
133 }
d38d4e5d
BK
134
135 // __p is not permitted to be a null pointer.
136 void
dcec0389 137 deallocate(pointer __p, size_type)
113008b5 138 { std::free(static_cast<void*>(__p)); }
d38d4e5d
BK
139
140 size_type
7d9cb054 141 max_size() const _GLIBCXX_USE_NOEXCEPT
422a9f77
JW
142 {
143#if __PTRDIFF_MAX__ < __SIZE_MAX__
144 return size_t(__PTRDIFF_MAX__) / sizeof(_Tp);
145#else
146 return size_t(-1) / sizeof(_Tp);
147#endif
148 }
d38d4e5d 149
734f5023 150#if __cplusplus >= 201103L
45ba8f9f
JW
151 template<typename _Up, typename... _Args>
152 void
153 construct(_Up* __p, _Args&&... __args)
0f317ef7
MG
154 noexcept(noexcept(::new((void *)__p)
155 _Up(std::forward<_Args>(__args)...)))
45ba8f9f
JW
156 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
157
158 template<typename _Up>
159 void
0f317ef7
MG
160 destroy(_Up* __p)
161 noexcept(noexcept(__p->~_Up()))
162 { __p->~_Up(); }
45ba8f9f 163#else
d38d4e5d
BK
164 // _GLIBCXX_RESOLVE_LIB_DEFECTS
165 // 402. wrong new expression in [some_] allocator::construct
166 void
167 construct(pointer __p, const _Tp& __val)
61fcb9fb
PC
168 { ::new((void *)__p) value_type(__val); }
169
d38d4e5d
BK
170 void
171 destroy(pointer __p) { __p->~_Tp(); }
45ba8f9f 172#endif
f263b26e 173
c7790bdb
JW
174 template<typename _Up>
175 friend bool
176 operator==(const malloc_allocator&, const malloc_allocator<_Up>&)
177 _GLIBCXX_NOTHROW
178 { return true; }
179
180 template<typename _Up>
181 friend bool
182 operator!=(const malloc_allocator&, const malloc_allocator<_Up>&)
183 _GLIBCXX_NOTHROW
184 { return false; }
185 };
3cbc7af0 186
12ffa228
BK
187_GLIBCXX_END_NAMESPACE_VERSION
188} // namespace
1ff9402d
BK
189
190#endif