1 // Allocator that wraps "C" malloc -*- C++ -*-
2
3 // Copyright (C) 2001-2018 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 using std::size_t;
46 using std::ptrdiff_t;
47
48 /**
49 * @brief An allocator that uses malloc.
50 * @ingroup allocators
51 *
52 * This is precisely the allocator defined in the C++ Standard.
53 * - all allocation calls malloc
54 * - all deallocation calls free
55 */
56 template<typename _Tp>
57 class malloc_allocator
58 {
59 public:
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;
67
68 template<typename _Tp1>
69 struct rebind
70 { typedef malloc_allocator<_Tp1> other; };
71
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
78 malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
79
80 malloc_allocator(const malloc_allocator&) _GLIBCXX_USE_NOEXCEPT { }
81
82 template<typename _Tp1>
83 malloc_allocator(const malloc_allocator<_Tp1>&)
84 _GLIBCXX_USE_NOEXCEPT { }
85
86 ~malloc_allocator() _GLIBCXX_USE_NOEXCEPT { }
87
88 pointer
89 address(reference __x) const _GLIBCXX_NOEXCEPT
90 { return std::__addressof(__x); }
91
92 const_pointer
93 address(const_reference __x) const _GLIBCXX_NOEXCEPT
94 { return std::__addressof(__x); }
95
96 // NB: __n is permitted to be 0. The C++ standard says nothing
97 // about what the return value is when __n == 0.
98 pointer
99 allocate(size_type __n, const void* = 0)
100 {
101 if (__n > this->max_size())
102 std::__throw_bad_alloc();
103
104 pointer __ret = 0;
105 #if __cpp_aligned_new
106 #if __cplusplus > 201402L && _GLIBCXX_HAVE_ALIGNED_ALLOC
107 if (alignof(_Tp) > alignof(std::max_align_t))
108 {
109 __ret = static_cast<_Tp*>(::aligned_alloc(alignof(_Tp),
110 __n * sizeof(_Tp)));
111 }
112 #else
113 # define _GLIBCXX_CHECK_MALLOC_RESULT
114 #endif
115 #endif
116 if (!__ret)
117 __ret = static_cast<_Tp*>(std::malloc(__n * sizeof(_Tp)));
118 if (!__ret)
119 std::__throw_bad_alloc();
120 #ifdef _GLIBCXX_CHECK_MALLOC_RESULT
121 #undef _GLIBCXX_CHECK_MALLOC_RESULT
122 if (reinterpret_cast<std::size_t>(__ret) % alignof(_Tp))
123 {
124 // Memory returned by malloc is not suitably aligned for _Tp.
125 deallocate(__ret, __n);
126 std::__throw_bad_alloc();
127 }
128 #endif
129 return __ret;
130 }
131
132 // __p is not permitted to be a null pointer.
133 void
134 deallocate(pointer __p, size_type)
135 { std::free(static_cast<void*>(__p)); }
136
137 size_type
138 max_size() const _GLIBCXX_USE_NOEXCEPT
139 { return size_t(-1) / sizeof(_Tp); }
140
141 #if __cplusplus >= 201103L
142 template<typename _Up, typename... _Args>
143 void
144 construct(_Up* __p, _Args&&... __args)
145 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
146
147 template<typename _Up>
148 void
149 destroy(_Up* __p) { __p->~_Up(); }
150 #else
151 // _GLIBCXX_RESOLVE_LIB_DEFECTS
152 // 402. wrong new expression in [some_] allocator::construct
153 void
154 construct(pointer __p, const _Tp& __val)
155 { ::new((void *)__p) value_type(__val); }
156
157 void
158 destroy(pointer __p) { __p->~_Tp(); }
159 #endif
160 };
161
162 template<typename _Tp>
163 inline bool
164 operator==(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
165 { return true; }
166
167 template<typename _Tp>
168 inline bool
169 operator!=(const malloc_allocator<_Tp>&, const malloc_allocator<_Tp>&)
170 { return false; }
171
172 _GLIBCXX_END_NAMESPACE_VERSION
173 } // namespace
174
175 #endif
176