µ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-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_ISO_MEMORY_
14#define CMSIS_PLUS_ISO_MEMORY_
15
17
18#include <cstddef>
19#include <cerrno>
20#include <cassert>
21#include <limits>
22#include <memory>
23
24// ----------------------------------------------------------------------------
25
26#pragma GCC diagnostic push
27#if defined(__clang__)
28#pragma clang diagnostic ignored "-Wc++98-compat"
29#endif
30
31// ----------------------------------------------------------------------------
32
33namespace os
34{
35 namespace estd
36 {
37
38 // ========================================================================
39
40 // in os-memory.h
41 // [[noreturn]] void
42 // __throw_bad_alloc (void);
43
44 namespace pmr
45 {
46 // ======================================================================
47
49
50 template<typename T>
52
53 template<typename T1, typename T2>
54 bool
56 const polymorphic_allocator<T2>& b) noexcept;
57
58 template<typename T1, typename T2>
59 bool
61 const polymorphic_allocator<T2>& b) noexcept;
62
80 new_delete_resource (void) noexcept;
81
89 null_memory_resource (void) noexcept;
90
98
106 get_default_resource (void) noexcept;
107
117
118 // ======================================================================
119
120 template<typename T>
122 {
123 public:
124
125 typedef T value_type;
126
127 polymorphic_allocator () noexcept;
128
130
132
133 template<typename U>
135 noexcept;
136
138 operator= (polymorphic_allocator const & a) = default;
139
141 allocate (std::size_t n);
142
143 void
144 deallocate (value_type* p, std::size_t n) noexcept;
145
146 std::size_t
147 max_size (void) const noexcept;
148
150 select_on_container_copy_construction (void) const noexcept;
151
153 resource (void) const noexcept;
154
155 private:
156
158 };
159
160 // ------------------------------------------------------------------------
161 } /* namespace pmr */
162 } /* namespace estd */
163} /* namespace os */
164
165// ===== Inline & template implementations ====================================
166
167namespace os
168{
169 namespace estd
170 {
171 namespace pmr
172 {
173
174 // ======================================================================
175
183 inline memory_resource*
184 get_default_resource (void) noexcept
185 {
186 rtos::memory::init_once_default_resource ();
187 return default_resource;
188 }
189
190 // ======================================================================
191
192 template<typename T>
194 res_(get_default_resource())
195 {
196 }
197
198 template<typename T>
199 inline
201 res_(r)
202 {
203 }
204
205 template<typename T>
206 template<typename U>
207 inline
209 polymorphic_allocator<U> const & other) noexcept :
210 res_(other.resource())
211 {
212 }
213
214 template<typename T>
217 {
218 if (n > max_size ())
219 {
220 estd::__throw_system_error (
221 EINVAL, "polymorphic_allocator<T>::allocate(std::size_t n)"
222 " 'n' exceeds maximum supported size");
223 }
224
225 return static_cast<value_type*> (res_->allocate (
226 n * sizeof(value_type), alignof(value_type)));
227 }
228
229 template<typename T>
230 inline void
232 {
233 assert(n <= max_size ());
234 res_->deallocate (p, n * sizeof(value_type), alignof(value_type));
235 }
236
237 template<typename T>
238 inline std::size_t
240 {
241 return std::numeric_limits<std::size_t>::max () / sizeof(value_type);
242 }
243
244 template<typename T>
247 void) const noexcept
248 {
249 return polymorphic_allocator ();
250 }
251
252 template<typename T>
253 inline memory_resource*
255 {
256 return res_;
257 }
258
259 // ======================================================================
260
261 template<typename T, typename U>
262 inline bool
264 polymorphic_allocator<U> const & rhs) noexcept
265 {
266 return *lhs.resource () == *rhs.resource ();
267 }
268
269 template<typename T, typename U>
270 inline bool
272 polymorphic_allocator<U> const & rhs) noexcept
273 {
274 return !(lhs == rhs);
275 }
276
277 // ------------------------------------------------------------------------
278 } /* namespace pmr */
279 } /* namespace estd */
280} /* namespace os */
281
282#pragma GCC diagnostic pop
283
284// ----------------------------------------------------------------------------
285
286#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:160
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:1311
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition os-memory.h:1289
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
System namespace.
Standard std namespace.
bool operator==(thread::id x, thread::id y) noexcept
bool operator!=(thread::id x, thread::id y) noexcept