µ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++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef CMSIS_PLUS_ISO_MEMORY_
13#define CMSIS_PLUS_ISO_MEMORY_
14
16
17#include <cstddef>
18#include <cerrno>
19#include <cassert>
20#include <limits>
21#include <memory>
22
23// ----------------------------------------------------------------------------
24
25#pragma GCC diagnostic push
26#if defined(__clang__)
27#pragma clang diagnostic ignored "-Wc++98-compat"
28#endif
29
30// ----------------------------------------------------------------------------
31
32namespace os
33{
34 namespace estd
35 {
36
37 // ========================================================================
38
39 // in os-memory.h
40 // [[noreturn]] void
41 // __throw_bad_alloc (void);
42
43 namespace pmr
44 {
45 // ======================================================================
46
48
49 template <typename T>
51
52 template <typename T1, typename T2>
53 bool
55 const polymorphic_allocator<T2>& b) noexcept;
56
57 template <typename T1, typename T2>
58 bool
60 const polymorphic_allocator<T2>& b) noexcept;
61
79 new_delete_resource (void) noexcept;
80
88 null_memory_resource (void) noexcept;
89
97
105 get_default_resource (void) noexcept;
106
116
117 // ======================================================================
118
119 template <typename T>
121 {
122 public:
123 typedef T value_type;
124
125 polymorphic_allocator () noexcept;
126
128
130
131 template <typename U>
132 polymorphic_allocator (polymorphic_allocator<U> const& other) noexcept;
133
135 operator= (polymorphic_allocator const& a)
136 = default;
137
139 allocate (std::size_t n);
140
141 void
142 deallocate (value_type* p, std::size_t n) noexcept;
143
144 std::size_t
145 max_size (void) const noexcept;
146
148 select_on_container_copy_construction (void) const noexcept;
149
151 resource (void) const noexcept;
152
153 private:
155 };
156
157 // ----------------------------------------------------------------------
158 } /* namespace pmr */
159 } /* namespace estd */
160} /* namespace os */
161
162// ===== Inline & template implementations ====================================
163
164namespace os
165{
166 namespace estd
167 {
168 namespace pmr
169 {
170
171 // ======================================================================
172
180 inline memory_resource*
181 get_default_resource (void) noexcept
182 {
183 rtos::memory::init_once_default_resource ();
184 return default_resource;
185 }
186
187 // ======================================================================
188
189 template <typename T>
191 : res_ (get_default_resource ())
192 {
193 }
194
195 template <typename T>
197 memory_resource* r) noexcept
198 : res_ (r)
199 {
200 }
201
202 template <typename T>
203 template <typename U>
205 polymorphic_allocator<U> const& other) noexcept
206 : res_ (other.resource ())
207 {
208 }
209
210 template <typename T>
213 {
214 if (n > max_size ())
215 {
216 estd::__throw_system_error (
217 EINVAL, "polymorphic_allocator<T>::allocate(std::size_t n)"
218 " 'n' exceeds maximum supported size");
219 }
220
221 return static_cast<value_type*> (
222 res_->allocate (n * sizeof (value_type), alignof (value_type)));
223 }
224
225 template <typename T>
226 inline void
228 std::size_t n) noexcept
229 {
230 assert (n <= max_size ());
231 res_->deallocate (p, n * sizeof (value_type), alignof (value_type));
232 }
233
234 template <typename T>
235 inline std::size_t
237 {
238 return std::numeric_limits<std::size_t>::max () / sizeof (value_type);
239 }
240
241 template <typename T>
244 void) const noexcept
245 {
246 return polymorphic_allocator ();
247 }
248
249 template <typename T>
250 inline memory_resource*
252 {
253 return res_;
254 }
255
256 // ======================================================================
257
258 template <typename T, typename U>
259 inline bool
261 polymorphic_allocator<U> const& rhs) noexcept
262 {
263 return *lhs.resource () == *rhs.resource ();
264 }
265
266 template <typename T, typename U>
267 inline bool
269 polymorphic_allocator<U> const& rhs) noexcept
270 {
271 return !(lhs == rhs);
272 }
273
274 // ----------------------------------------------------------------------
275 } /* namespace pmr */
276 } /* namespace estd */
277} /* namespace os */
278
279#pragma GCC diagnostic pop
280
281// ----------------------------------------------------------------------------
282
283#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:159
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:1312
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition os-memory.h:1290
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