µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
memory_resource
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef CMSIS_PLUS_ISO_MEMORY_
29#define CMSIS_PLUS_ISO_MEMORY_
30
32
33#include <cstddef>
34#include <cerrno>
35#include <cassert>
36#include <limits>
37#include <memory>
38
39// ----------------------------------------------------------------------------
40
41#pragma GCC diagnostic push
42
43#if defined(__clang__)
44#pragma clang diagnostic ignored "-Wc++98-compat"
45#endif
46
47// ----------------------------------------------------------------------------
48
49namespace os
50{
51 namespace estd
52 {
53
54 // ========================================================================
55
56 [[noreturn]] void
57 __throw_bad_alloc (void);
58
59 namespace pmr
60 {
61 // ======================================================================
62
64
65 template<typename T>
67
68 template<typename T1, typename T2>
69 bool
71 const polymorphic_allocator<T2>& b) noexcept;
72
73 template<typename T1, typename T2>
74 bool
76 const polymorphic_allocator<T2>& b) noexcept;
77
95 new_delete_resource (void) noexcept;
96
104 null_memory_resource (void) noexcept;
105
113
121 get_default_resource (void) noexcept;
122
132
133 // ======================================================================
134
135 template<typename T>
137 {
138 public:
139
140 typedef T value_type;
141
142 polymorphic_allocator () noexcept;
143
145
147
148 template<typename U>
150 noexcept;
151
153 operator= (polymorphic_allocator const & a) = default;
154
156 allocate (std::size_t n);
157
158 void
159 deallocate (value_type* p, std::size_t n) noexcept;
160
161 std::size_t
162 max_size (void) const noexcept;
163
165 select_on_container_copy_construction (void) const noexcept;
166
168 resource (void) const noexcept;
169
170 private:
171
173 };
174
175 // ------------------------------------------------------------------------
176 } /* namespace pmr */
177 } /* namespace estd */
178} /* namespace os */
179
180// ===== Inline & template implementations ====================================
181
182namespace os
183{
184 namespace estd
185 {
186 namespace pmr
187 {
188
189 // ======================================================================
190
198 inline memory_resource*
199 get_default_resource (void) noexcept
200 {
201 rtos::memory::init_once_default_resource ();
202 return default_resource;
203 }
204
205 // ======================================================================
206
207 template<typename T>
209 res_(get_default_resource())
210 {
211 ;
212 }
213
214 template<typename T>
215 inline
217 res_(r)
218 {
219 ;
220 }
221
222 template<typename T>
223 template<typename U>
224 inline
226 polymorphic_allocator<U> const & other) noexcept :
227 res_(other.resource())
228 {
229 ;
230 }
231
232 template<typename T>
235 {
236 if (n > max_size ())
237 {
238 estd::__throw_system_error (
239 EINVAL, "polymorphic_allocator<T>::allocate(std::size_t n)"
240 " 'n' exceeds maximum supported size");
241 }
242
243 return static_cast<value_type*> (res_->allocate (
244 n * sizeof(value_type), alignof(value_type)));
245 }
246
247 template<typename T>
248 inline void
250 {
251 assert(n <= max_size ());
252 res_->deallocate (p, n * sizeof(value_type), alignof(value_type));
253 }
254
255 template<typename T>
256 inline std::size_t
258 {
259 return std::numeric_limits<std::size_t>::max () / sizeof(value_type);
260 }
261
262 template<typename T>
265 void) const noexcept
266 {
267 return polymorphic_allocator ();
268 }
269
270 template<typename T>
271 inline memory_resource*
273 {
274 return res_;
275 }
276
277 // ======================================================================
278
279 template<typename T, typename U>
280 inline bool
282 polymorphic_allocator<U> const & rhs) noexcept
283 {
284 return *lhs.resource () == *rhs.resource ();
285 }
286
287 template<typename T, typename U>
288 inline bool
290 polymorphic_allocator<U> const & rhs) noexcept
291 {
292 return !(lhs == rhs);
293 }
294
295 // ------------------------------------------------------------------------
296 } /* namespace pmr */
297 } /* namespace estd */
298} /* namespace os */
299
300#pragma GCC diagnostic pop
301
302// ----------------------------------------------------------------------------
303
304#endif /* CMSIS_PLUS_ISO_MEMORY_ */
memory_resource * resource(void) const noexcept
std::size_t max_size(void) const noexcept
void deallocate(value_type *p, std::size_t n) noexcept
polymorphic_allocator select_on_container_copy_construction(void) const noexcept
value_type * allocate(std::size_t n)
Memory resource manager (abstract class).
Definition os-memory.h:165
void deallocate(void *addr, std::size_t bytes, std::size_t alignment=max_align) noexcept
Deallocate the previously allocated memory block.
Definition os-memory.h:1316
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition os-memory.h:1294
memory_resource * new_delete_resource(void) noexcept
Get the address of a memory manager based on new/delete.
memory_resource * get_default_resource(void) noexcept
Get the default application memory manager.
memory_resource * set_default_resource(memory_resource *res) noexcept
Set the default application memory manager.
memory_resource * null_memory_resource(void) noexcept
Get the address of an ineffective memory manager.
memory_resource * default_resource
bool operator==(const polymorphic_allocator< T1 > &a, const polymorphic_allocator< T2 > &b) noexcept
bool operator!=(const polymorphic_allocator< T1 > &a, const polymorphic_allocator< T2 > &b) noexcept
void __throw_bad_alloc(void)
System namespace.
Standard std namespace.
bool operator==(thread::id x, thread::id y) noexcept
bool operator!=(thread::id x, thread::id y) noexcept