µOS++ IIIe Reference  v6.3.15
“Perfekt ist nicht gut genug”
The third edition of µOS++, a POSIX inspired open source system, written in C++.
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 namespace os
42 {
43  namespace estd
44  {
45 
46  // ========================================================================
47 
48  [[noreturn]] void
49  __throw_bad_alloc (void);
50 
51  namespace pmr
52  {
53  // ======================================================================
54 
56 
57  template<typename T>
59 
60  template<typename T1, typename T2>
61  bool
63  const polymorphic_allocator<T2>& b) noexcept;
64 
65  template<typename T1, typename T2>
66  bool
68  const polymorphic_allocator<T2>& b) noexcept;
69 
87  new_delete_resource (void) noexcept;
88 
96  null_memory_resource (void) noexcept;
97 
104  set_default_resource (memory_resource* res) noexcept;
105 
113  get_default_resource (void) noexcept;
114 
124 
125  // ======================================================================
126 
127  template<typename T>
129  {
130  public:
131 
132  typedef T value_type;
133 
134  polymorphic_allocator () noexcept;
135 
137 
138  polymorphic_allocator (polymorphic_allocator const & a) = default;
139 
140  template<typename U>
142  noexcept;
143 
145  operator= (polymorphic_allocator const & a) = default;
146 
147  value_type*
148  allocate (std::size_t n);
149 
150  void
151  deallocate (value_type* p, std::size_t n) noexcept;
152 
153  std::size_t
154  max_size (void) const noexcept;
155 
157  select_on_container_copy_construction (void) const noexcept;
158 
160  resource (void) const noexcept;
161 
162  private:
163 
165  };
166 
167  // ------------------------------------------------------------------------
168  } /* namespace pmr */
169  } /* namespace estd */
170 } /* namespace os */
171 
172 // ===== Inline & template implementations ====================================
173 
174 namespace os
175 {
176  namespace estd
177  {
178  namespace pmr
179  {
180 
181  // ======================================================================
182 
190  inline memory_resource*
191  get_default_resource (void) noexcept
192  {
194  return default_resource;
195  }
196 
197  // ======================================================================
198 
199  template<typename T>
201  res_(get_default_resource())
202  {
203  ;
204  }
205 
206  template<typename T>
207  inline
209  res_(r)
210  {
211  ;
212  }
213 
214  template<typename T>
215  template<typename U>
216  inline
218  polymorphic_allocator<U> const & other) noexcept :
219  res_(other.resource())
220  {
221  ;
222  }
223 
224  template<typename T>
225  inline typename polymorphic_allocator<T>::value_type*
227  {
228  if (n > max_size ())
229  {
231  EINVAL, "polymorphic_allocator<T>::allocate(std::size_t n)"
232  " 'n' exceeds maximum supported size");
233  }
234 
235  return static_cast<value_type*> (res_->allocate (
236  n * sizeof(value_type), alignof(value_type)));
237  }
238 
239  template<typename T>
240  inline void
241  polymorphic_allocator<T>::deallocate (value_type* p, std::size_t n) noexcept
242  {
243  assert(n <= max_size ());
244  res_->deallocate (p, n * sizeof(value_type), alignof(value_type));
245  }
246 
247  template<typename T>
248  inline std::size_t
250  {
252  }
253 
254  template<typename T>
257  void) const noexcept
258  {
259  return polymorphic_allocator ();
260  }
261 
262  template<typename T>
263  inline memory_resource*
265  {
266  return res_;
267  }
268 
269  // ======================================================================
270 
271  template<typename T, typename U>
272  inline bool
274  polymorphic_allocator<U> const & rhs) noexcept
275  {
276  return *lhs.resource () == *rhs.resource ();
277  }
278 
279  template<typename T, typename U>
280  inline bool
282  polymorphic_allocator<U> const & rhs) noexcept
283  {
284  return !(lhs == rhs);
285  }
286 
287  // ------------------------------------------------------------------------
288  } /* namespace pmr */
289  } /* namespace estd */
290 } /* namespace os */
291 
292 // ----------------------------------------------------------------------------
293 
294 #endif /* CMSIS_PLUS_ISO_MEMORY_ */
Memory resource manager (abstract class).
Definition: os-memory.h:153
memory_resource * get_default_resource(void) noexcept
Get the default application memory manager.
rtos::memory::memory_resource memory_resource
Definition: memory_resource:55
void deallocate(value_type *p, std::size_t n) noexcept
constexpr std::size_t max(std::size_t a, std::size_t b)
Definition: os-memory.h:74
polymorphic_allocator select_on_container_copy_construction(void) const noexcept
Standard std namespace.
memory_resource * resource(void) const noexcept
System namespace.
bool operator==(const polymorphic_allocator< T1 > &a, const polymorphic_allocator< T2 > &b) noexcept
std::size_t max_size(void) const noexcept
memory_resource * set_default_resource(memory_resource *res) noexcept
Set the default application memory manager.
void init_once_default_resource(void)
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition: os-memory.h:1285
memory_resource * new_delete_resource(void) noexcept
Get the address of a memory manager based on new/delete.
value_type * allocate(std::size_t n)
void __throw_system_error(int ev, const char *what_arg)
memory_resource * default_resource
memory_resource * null_memory_resource(void) noexcept
Get the address of an ineffective memory manager.
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:1307
void __throw_bad_alloc(void)
bool operator!=(const polymorphic_allocator< T1 > &a, const polymorphic_allocator< T2 > &b) noexcept