µ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++.
os-memory.h
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_RTOS_OS_MEMORY_H_
29 #define CMSIS_PLUS_RTOS_OS_MEMORY_H_
30 
31 // ----------------------------------------------------------------------------
32 
33 #if defined(__cplusplus)
34 
36 
37 #include <limits>
38 #include <new>
39 #include <cerrno>
40 #include <mutex>
41 
42 // ----------------------------------------------------------------------------
43 
44 // These definitions refer only to the RTOS allocators.
45 // The application should use the similar ones from the
46 // os::estd:: namespace.
47 
48 namespace os
49 {
50  namespace estd
51  {
52  [[noreturn]] void
53  __throw_bad_alloc (void);
54 
55  template<typename L>
56  class lock_guard;
57 
58  }
59 
60  namespace rtos
61  {
62  namespace scheduler
63  {
64  class critical_section;
65  }
66 
67  class null_locker;
68 
69  namespace memory
70  {
71  // ----------------------------------------------------------------------
72 
73  constexpr std::size_t
74  max (std::size_t a, std::size_t b)
75  {
76  return (a >= b) ? a : b;
77  }
78 
85  constexpr std::size_t
86  align_size (std::size_t size, std::size_t align) noexcept
87  {
88  return ((size) + (align) - 1L) & ~((align) - 1L);
89  }
90 
91  class memory_resource;
92 
93  // ----------------------------------------------------------------------
94 
112  malloc_resource (void) noexcept;
113 
120  set_default_resource (memory_resource* res) noexcept;
121 
129  get_default_resource (void) noexcept;
130 
131  void
133 
138  // ======================================================================
142  using out_of_memory_handler_t = void (*)(void);
143 
154  {
155 
156  public:
157 
162  static constexpr std::size_t max_align = alignof(std::max_align_t);
163 
173  memory_resource () = default;
174 
179  memory_resource (const char* name);
180 
185  // The rule of five.
186  memory_resource (const memory_resource&) = delete;
187  memory_resource (memory_resource&&) = delete;
189  operator= (const memory_resource&) = delete;
191  operator= (memory_resource&&) = delete;
192 
200  virtual
201  ~memory_resource ();
202 
207  public:
208 
220  void*
221  allocate (std::size_t bytes, std::size_t alignment = max_align);
222 
231  void
232  deallocate (void* addr, std::size_t bytes, std::size_t alignment =
233  max_align) noexcept;
234 
241  bool
242  is_equal (memory_resource const & other) const noexcept;
243 
251  void
252  reset (void) noexcept;
253 
261  bool
262  coalesce (void) noexcept;
263 
270  std::size_t
271  max_size (void) const noexcept;
272 
279  out_of_memory_handler (out_of_memory_handler_t handler);
280 
288  out_of_memory_handler (void);
289 
294  std::size_t
295  total_bytes (void);
296 
303  std::size_t
304  allocated_bytes (void);
305 
312  std::size_t
313  max_allocated_bytes (void);
314 
321  std::size_t
322  free_bytes (void);
323 
330  std::size_t
331  allocated_chunks (void);
332 
339  std::size_t
340  free_chunks (void);
341 
348  std::size_t
349  allocations (void);
350 
357  std::size_t
358  deallocations (void);
359 
367  void
368  trace_print_statistics (void);
369 
374  protected:
375 
387  virtual void*
388  do_allocate (std::size_t bytes, std::size_t alignment) = 0;
389 
398  virtual void
399  do_deallocate (void* addr, std::size_t bytes, std::size_t alignment)
400  noexcept = 0;
401 
408  virtual bool
409  do_is_equal (memory_resource const &other) const noexcept;
410 
417  virtual std::size_t
418  do_max_size (void) const noexcept;
419 
427  virtual void
428  do_reset (void) noexcept;
429 
437  virtual bool
438  do_coalesce (void) noexcept;
439 
446  void
447  internal_increase_allocated_statistics (std::size_t bytes) noexcept;
448 
455  void
456  internal_decrease_allocated_statistics (std::size_t bytes) noexcept;
457 
462  protected:
463 
468  out_of_memory_handler_t out_of_memory_handler_ = nullptr;
469 
470  std::size_t total_bytes_ = 0;
471  std::size_t allocated_bytes_ = 0;
472  std::size_t free_bytes_ = 0;
473  std::size_t allocated_chunks_ = 0;
474  std::size_t free_chunks_ = 0;
475  std::size_t max_allocated_bytes_ = 0;
476  std::size_t allocations_ = 0;
477  std::size_t deallocations_ = 0;
478 
483  };
484 
497  bool
498  operator== (const memory_resource& lhs, const memory_resource& rhs)
499  noexcept;
500 
508  bool
509  operator!= (const memory_resource& lhs, const memory_resource& rhs)
510  noexcept;
511 
516  // ======================================================================
531  template<typename T>
533  {
534  public:
535 
539  using value_type = T;
540 
550  allocator_stateless_default_resource () noexcept = default;
551 
557  allocator_stateless_default_resource const & other) = default;
558 
563  template<typename U>
565  allocator_stateless_default_resource<U> const & other) noexcept;
566 
572  allocator_stateless_default_resource && other) = default;
573 
578 
594  operator= (allocator_stateless_default_resource const & other) = default;
595 
602  operator= (allocator_stateless_default_resource && other) = default;
603 
608  public:
609 
620  value_type*
621  allocate (std::size_t elements);
622 
630  void
631  deallocate (value_type* addr, std::size_t elements) noexcept;
632 
638  std::size_t
639  max_size (void) const noexcept;
640 
645  protected:
646 
647  // This class should have no member variables, to meet the
648  // default allocator stateless requirements.
649  };
650 
651  // ======================================================================
656  template<typename L>
657  class lock_guard;
658 
659  using F = memory_resource* (void);
660 
672  template<typename T, typename L, F get_resource>
674  {
675  public:
676 
677  using value_type = T;
678  using locker_type = L;
679 
689  allocator_stateless_polymorphic_synchronized () noexcept;
690 
695  allocator_stateless_polymorphic_synchronized (
696  allocator_stateless_polymorphic_synchronized const & other) = default;
697 
702  template<typename U>
703  allocator_stateless_polymorphic_synchronized (
704  allocator_stateless_polymorphic_synchronized<U, L, get_resource> const & other)
705  noexcept;
706 
711  allocator_stateless_polymorphic_synchronized (
712  allocator_stateless_polymorphic_synchronized && other) = default;
713 
717  ~allocator_stateless_polymorphic_synchronized () = default;
718 
733  allocator_stateless_polymorphic_synchronized&
734  operator= (allocator_stateless_polymorphic_synchronized const & other) = default;
735 
741  allocator_stateless_polymorphic_synchronized&
742  operator= (allocator_stateless_polymorphic_synchronized && other) = default;
743 
748  public:
749 
764  template<typename U>
765  struct rebind
766  {
768  };
769 
775  value_type*
776  allocate (std::size_t elements);
777 
785  void
786  deallocate (value_type* addr, std::size_t elements) noexcept;
787 
793  std::size_t
794  max_size (void) const noexcept;
795 
796 #if 0
797  allocator_stateless_polymorphic_synchronized
798  select_on_container_copy_construction (void) const noexcept;
799 
801  resource (void) const noexcept;
802 #endif
803 
808  private:
809 
810  // This class should have no member variables, to meet the
811  // default allocator stateless requirements.
812  };
813 
814  template<typename T1, typename T2, typename L, F get_resource>
815  bool
816  operator== (
818  get_resource>& lhs,
820  get_resource>& rhs) noexcept;
821 
822  template<typename T1, typename T2, typename L, F get_resource>
823  bool
824  operator!= (
826  get_resource>& lhs,
828  get_resource>& rhs) noexcept;
829 
840  template<typename A>
842  {
843  public:
844 
848  using allocator_type = A;
849 
853  using allocator_traits = std::allocator_traits<A>;
854 
855  using pointer = typename allocator_traits::pointer;
856 
866 
871  allocator_deleter (const allocator_type& other);
872 
877  allocator_deleter (allocator_deleter&& other) = default;
878 
882  ~allocator_deleter () = default;
883 
899  operator= (const allocator_deleter& other) = default;
900 
907  operator= (allocator_deleter&& other) = default;
908 
913  void
914  operator() (pointer addr) const;
915 
920  protected:
921 
926  allocator_type a_;
927 
931  };
932 
943  template<typename T, typename A, typename ... Args>
944  auto
945  allocate_unique (const A& allocator, Args&&... args);
946 
947  // ----------------------------------------------------------------------
948 
959  template<typename T>
961  set_resource_typed (memory_resource* res) noexcept;
962 
967  template<typename T>
969  get_resource_typed (void) noexcept;
970 
971  // ----------------------------------------------------------------------
972 
973  template<>
976 
977  template<>
979  get_resource_typed<thread> (void) noexcept;
980 
981  // ----------------------------------------------------------------------
982 
983  template<>
986 
987  template<>
990 
991  // ----------------------------------------------------------------------
992 
993  template<>
996 
997  template<>
999  get_resource_typed<event_flags> (void) noexcept;
1000 
1001  // ----------------------------------------------------------------------
1002 
1003  template<>
1006 
1007  template<>
1009  get_resource_typed<memory_pool> (void) noexcept;
1010 
1011  // ----------------------------------------------------------------------
1012 
1013  template<>
1016 
1017  template<>
1019  get_resource_typed<message_queue> (void) noexcept;
1020 
1021  // ----------------------------------------------------------------------
1022 
1023  template<>
1026 
1027  template<>
1029  get_resource_typed<mutex> (void) noexcept;
1030 
1031  // ----------------------------------------------------------------------
1032 
1033  template<>
1036 
1037  template<>
1039  get_resource_typed<semaphore> (void) noexcept;
1040 
1041  // ----------------------------------------------------------------------
1042 
1043  template<>
1046 
1047  template<>
1049  get_resource_typed<timer> (void) noexcept;
1050 
1055  // ----------------------------------------------------------------------
1064  template<typename T, typename U = T>
1066 
1075  template<typename T, typename U = T>
1076  using unique_ptr = std::unique_ptr<T, allocator_deleter<allocator_typed<T, U>>>;
1077 
1082  // ------------------------------------------------------------------------
1083  } /* namespace memory */
1084  } /* namespace rtos */
1085 } /* namespace os */
1086 
1087 // ===== Inline & template implementations ====================================
1088 
1089 namespace os
1090 {
1091  namespace rtos
1092  {
1093  namespace memory
1094  {
1095  // ----------------------------------------------------------------------
1096 
1102 
1103  extern memory_resource* resource_thread;
1104  extern memory_resource* resource_condition_variable;
1105  extern memory_resource* resource_event_flags;
1106  extern memory_resource* resource_memory_pool;
1107  extern memory_resource* resource_message_queue;
1108  extern memory_resource* resource_mutex;
1109  extern memory_resource* resource_semaphore;
1110  extern memory_resource* resource_timer;
1111 
1116  // ----------------------------------------------------------------------
1124  inline memory_resource*
1125  get_default_resource (void) noexcept
1126  {
1128  return default_resource;
1129  }
1130 
1138  template<>
1139  inline memory_resource*
1141  {
1143  return resource_thread;
1144  }
1145 
1153  template<>
1154  inline memory_resource*
1156  {
1158  return resource_condition_variable;
1159  }
1160 
1168  template<>
1169  inline memory_resource*
1171  {
1173  return resource_event_flags;
1174  }
1175 
1183  template<>
1184  inline memory_resource*
1186  {
1188  return resource_memory_pool;
1189  }
1190 
1198  template<>
1199  inline memory_resource*
1201  {
1203  return resource_message_queue;
1204  }
1205 
1213  template<>
1214  inline memory_resource*
1216  {
1218  return resource_mutex;
1219  }
1220 
1228  template<>
1229  inline memory_resource*
1231  {
1233  return resource_semaphore;
1234  }
1235 
1243  template<>
1244  inline memory_resource*
1246  {
1248  return resource_timer;
1249  }
1250 
1251  // ======================================================================
1252 
1256  inline
1258  object_named
1259  { name }
1260  {
1261  ;
1262  }
1263 
1284  inline void*
1285  memory_resource::allocate (std::size_t bytes, std::size_t alignment)
1286  {
1287  ++allocations_;
1288  return do_allocate (bytes, alignment);
1289  }
1290 
1306  inline void
1307  memory_resource::deallocate (void* addr, std::size_t bytes,
1308  std::size_t alignment) noexcept
1309  {
1310  ++deallocations_;
1311  do_deallocate (addr, bytes, alignment);
1312  }
1313 
1325  inline bool
1326  memory_resource::is_equal (memory_resource const & other) const noexcept
1327  {
1328  return do_is_equal (other);
1329  }
1330 
1336  inline std::size_t
1337  memory_resource::max_size (void) const noexcept
1338  {
1339  return do_max_size ();
1340  }
1341 
1347  inline void
1348  memory_resource::reset (void) noexcept
1349  {
1350  do_reset ();
1351  }
1352 
1363  inline bool
1365  {
1366  return do_coalesce ();
1367  }
1368 
1377  {
1378  trace::printf ("%s(%p) @%p %s\n", __func__, handler, this, name ());
1379 
1380  out_of_memory_handler_t tmp = out_of_memory_handler_;
1381  out_of_memory_handler_ = handler;
1382 
1383  return tmp;
1384  }
1385 
1394  {
1395  return out_of_memory_handler_;
1396  }
1397 
1398  inline std::size_t
1400  {
1401  return total_bytes_;
1402  }
1403 
1404  inline std::size_t
1406  {
1407  return allocated_bytes_;
1408  }
1409 
1410  inline std::size_t
1412  {
1413  return max_allocated_bytes_;
1414  }
1415 
1416  inline std::size_t
1418  {
1419  return free_bytes_;
1420  }
1421 
1422  inline std::size_t
1424  {
1425  return allocated_chunks_;
1426  }
1427 
1428  inline std::size_t
1430  {
1431  return free_chunks_;
1432  }
1433 
1434  inline std::size_t
1436  {
1437  return allocations_;
1438  }
1439 
1440  inline std::size_t
1442  {
1443  return deallocations_;
1444  }
1445 
1446  inline void
1448  {
1449 #if defined(TRACE)
1450  trace::printf ("Memory '%s' @%p: \n"
1451  "\ttotal: %u bytes, \n"
1452  "\tallocated: %u bytes in %u chunk(s), \n"
1453  "\tfree: %u bytes in %u chunk(s), \n"
1454  "\tmax: %u bytes, \n"
1455  "\tcalls: %u allocs, %u deallocs\n",
1456  name (), this, total_bytes (), allocated_bytes (),
1459  deallocations ());
1460 #endif /* defined(TRACE) */
1461  }
1462 
1463  // ======================================================================
1464 
1465  inline bool
1466  operator== (memory_resource const & lhs, memory_resource const & rhs) noexcept
1467  {
1468  return &lhs == &rhs || lhs.is_equal (rhs);
1469  }
1470 
1471  inline bool
1472  operator!= (memory_resource const & lhs, memory_resource const & rhs) noexcept
1473  {
1474  return !(lhs == rhs);
1475  }
1476 
1477  // ======================================================================
1478 
1479  template<typename T>
1480  template<typename U>
1481  inline
1483  allocator_stateless_default_resource<U> const & other __attribute__((unused))) noexcept
1484  {
1485  ;
1486  }
1487 
1488  template<typename T>
1491  {
1493 
1494  return static_cast<value_type*> (get_default_resource ()->allocate (
1495  elements * sizeof(value_type)));
1496  }
1497 
1498  template<typename T>
1499  inline void
1501  value_type* addr, std::size_t elements) noexcept
1502  {
1504 
1505  get_default_resource ()->deallocate (addr,
1506  elements * sizeof(value_type));
1507  }
1508 
1509  template<typename T>
1510  inline std::size_t
1512  {
1513  return get_default_resource ()->max_size () / sizeof(value_type);
1514  }
1515 
1516  // ======================================================================
1517 
1518  template<typename T, typename U, typename L, F get_resource>
1519  inline bool
1523  {
1524  return *lhs.resource () == *rhs.resource ();
1525  }
1526 
1527  template<typename T, typename U, typename L, F get_resource>
1528  inline bool
1532  {
1533  return !(lhs == rhs);
1534  }
1535 
1536  // ======================================================================
1537 
1538  template<typename T, typename L, F get_resource>
1539  inline
1541  {
1542  trace::printf ("%s() @%p %p\n", __func__, this, get_resource ());
1543  }
1544 
1545  template<typename T, typename L, F get_resource>
1546  template<typename U>
1547  inline
1549  allocator_stateless_polymorphic_synchronized<U, L, get_resource> const & other __attribute__((unused))) noexcept
1550  {
1551  ;
1552  }
1553 
1554  template<typename T, typename L, F get_resource>
1557  std::size_t elements)
1558  {
1559  trace::printf ("%s(%u) @%p\n", __func__, elements, this);
1560 
1561 #if 0
1562  std::size_t ms = max_size ();
1563  if ((ms > 0) && (elements > max_size ()))
1564  {
1566  EINVAL,
1567  "allocator_stateless_polymorphic_synchronized<T>::allocate(size_t n)"
1568  " 'n' exceeds maximum supported size");
1569  }
1570 #endif
1571 
1572  locker_type lk;
1573  std::lock_guard<locker_type> ulk
1574  { lk };
1575 
1576  return static_cast<value_type*> (get_resource ()->allocate (
1577  elements * sizeof(value_type), alignof(value_type)));
1578  }
1579 
1580  template<typename T, typename L, F get_resource>
1581  void
1583  value_type* addr, std::size_t elements) noexcept
1584  {
1585  trace::printf ("%s(%p,%u) @%p\n", __func__, addr, elements, this);
1586 
1587 #if 0
1588  std::size_t ms = max_size ();
1589  if (ms > 0)
1590  {
1591  assert (elements <= max_size ());
1592  }
1593 #endif
1594 
1595  locker_type lk;
1596  std::lock_guard<locker_type> ulk
1597  { lk };
1598 
1599  get_resource ()->deallocate (addr, elements * sizeof(value_type),
1600  alignof(value_type));
1601  }
1602 
1603  template<typename T, typename L, F get_resource>
1604  inline std::size_t
1606  void) const noexcept
1607  {
1608  return get_resource ()->max_size () / sizeof(T);
1609  }
1610 
1611 #if 0
1612  template<typename T, typename L, F get_resource>
1615  void) const noexcept
1616  {
1618  }
1619 
1620  template<typename T, typename L, F get_resource>
1621  inline memory_resource*
1623  void) const noexcept
1624  {
1625  return get_resource ();
1626  }
1627 #endif
1628 
1629  // ======================================================================
1630 
1631  template<typename A>
1632  inline
1634  {
1635  ;
1636  }
1637 
1638  template<typename A>
1639  inline
1641  a_
1642  { other }
1643  {
1644  ;
1645  }
1646 
1654  template<typename A>
1655  inline void
1657  {
1658  // Local allocator, without it many errors are issued.
1659  // TODO: understand exactly why.
1660  allocator_type alloc
1661  { a_ };
1662 
1663  // Call the object destructor.
1664  allocator_traits::destroy (alloc, std::addressof (*addr));
1665 
1666  // Deallocate the object, using the same allocator
1667  // used to allocate the object.
1668  allocator_traits::deallocate (alloc, addr, 1);
1669  }
1670 
1671  // ======================================================================
1672 
1684  template<typename T, typename A, typename ... Args>
1685  auto
1686  allocate_unique (const A& allocator, Args&&... args)
1687  {
1691  using allocator_type = A;
1692 
1696  using allocator_traits = std::allocator_traits<A>;
1697 
1698  static_assert(std::is_same<typename allocator_traits::value_type, std::remove_cv_t<T>>::value
1699  || std::is_base_of<typename allocator_traits::value_type, std::remove_cv_t<T>>::value,
1700  "Allocator must be of same type or derived.");
1701 
1702  static_assert(sizeof(T) <= sizeof(typename allocator_traits::value_type),
1703  "Derived type must not be larger.");
1704 
1705  allocator_type alloc
1706  { allocator };
1707 
1708  // Allocate space for 1 object instance of type T.
1709  auto p = allocator_traits::allocate (alloc, 1);
1710 
1711 #if defined(__EXCEPTIONS)
1712 
1713  try
1714  {
1715  // Use placement new to construct the object.
1716  allocator_traits::construct (alloc, std::addressof (*p),
1717  std::forward<Args>(args)...);
1718 
1719  // Figure out the deleter type.
1720  using D = allocator_deleter<A>;
1721 
1722  // Make the unique pointer with the object and the deleter.
1723  return std::unique_ptr<T, D> (p, D (alloc));
1724  }
1725  catch (...)
1726  {
1727  allocator_traits::deallocate (alloc, p, 1);
1728  throw;
1729  }
1730 
1731 #else
1732 
1733  // Use placement new to construct the object.
1734  allocator_traits::construct (alloc, std::addressof (*p),
1735  std::forward<Args>(args)...);
1736 
1737  // Figure out the deleter type.
1738  using D = allocator_deleter<A>;
1739 
1740  // Make the unique pointer with the object and the deleter.
1741  return std::unique_ptr<T, D> (p, D (alloc));
1742 
1743 #endif /* defined(__EXCEPTIONS) */
1744  }
1745 
1746  // ------------------------------------------------------------------------
1747  } /* namespace memory */
1748  } /* namespace rtos */
1749 } /* namespace os */
1750 
1751 // ----------------------------------------------------------------------------
1752 
1753 #endif /* __cplusplus */
1754 
1755 #endif /* CMSIS_PLUS_RTOS_OS_MEMORY_H_ */
Memory resource manager (abstract class).
Definition: os-memory.h:153
memory_resource * set_resource_typed< mutex >(memory_resource *res) noexcept
Definition: os-memory.cpp:318
std::size_t allocations(void)
Get the number of allocations.
Definition: os-memory.h:1435
memory_resource * get_resource_typed< condition_variable >(void) noexcept
Definition: os-memory.h:1155
memory_resource * set_resource_typed< message_queue >(memory_resource *res) noexcept
Definition: os-memory.cpp:300
std::size_t max_size(void) const noexcept
The maximum number of elements that can be passed to allocate().
Definition: os-memory.h:1605
virtual bool do_coalesce(void) noexcept
Implementation of the function to coalesce free blocks.
Definition: os-memory.cpp:470
memory_resource * get_resource_typed< thread >(void) noexcept
Definition: os-memory.h:1140
virtual void do_reset(void) noexcept
Implementation of the function to reset the memory manager.
Definition: os-memory.cpp:454
std::size_t allocated_bytes(void)
Get the current size of all allocated chunks.
Definition: os-memory.h:1405
rtos::memory::memory_resource memory_resource
Definition: memory_resource:55
auto allocate_unique(const A &allocator, Args &&... args)
Function template to allocate a unique pointer.
Definition: os-memory.h:1686
value_type * allocate(std::size_t elements)
Allocate a number of memory blocks of type value_type.
Definition: os-memory.h:1556
constexpr std::size_t max(std::size_t a, std::size_t b)
Definition: os-memory.h:74
std::allocator_traits< A > allocator_traits
Standard allocator traits definition.
Definition: os-memory.h:853
void deallocate(value_type *addr, std::size_t elements) noexcept
Deallocate the number of memory blocks of type value_type.
Definition: os-memory.h:1500
memory_resource * get_resource_typed< mutex >(void) noexcept
Definition: os-memory.h:1215
void operator()(pointer addr) const
Function operator.
Definition: os-memory.h:1656
virtual void * do_allocate(std::size_t bytes, std::size_t alignment)=0
Implementation of the memory allocator.
std::size_t allocated_chunks(void)
Get the current number of allocated chunks.
Definition: os-memory.h:1423
allocator_stateless_polymorphic_synchronized() noexcept
Default constructor. Construct a default allocator object instance.
Definition: os-memory.h:1540
Standard std namespace.
System namespace.
allocator_deleter()
Default constructor.
Definition: os-memory.h:1633
value_type * allocate(std::size_t elements)
Allocate a number of memory blocks of type value_type.
Definition: os-memory.h:1490
memory_resource * set_resource_typed< condition_variable >(memory_resource *res) noexcept
Definition: os-memory.cpp:246
out_of_memory_handler_t out_of_memory_handler(void)
Get the out of memory handler.
Definition: os-memory.h:1393
void trace_print_statistics(void)
Print a long message with usage statistics.
Definition: os-memory.h:1447
bool is_equal(memory_resource const &other) const noexcept
Compare for equality with another memory_resource.
Definition: os-memory.h:1326
void deallocate(value_type *addr, std::size_t elements) noexcept
Deallocate the number of memory blocks of type value_type.
Definition: os-memory.h:1582
memory_resource * get_default_resource(void) noexcept
Get the default RTOS system memory manager.
Definition: os-memory.h:1125
std::size_t max_allocated_bytes(void)
Get the maximum allocated size.
Definition: os-memory.h:1411
Scheduler critical section RAII helper.
Definition: os-sched.h:170
constexpr std::size_t align_size(std::size_t size, std::size_t align) noexcept
Helper function to align size values.
Definition: os-memory.h:86
memory_resource * set_resource_typed< semaphore >(memory_resource *res) noexcept
Definition: os-memory.cpp:336
memory_resource * set_default_resource(memory_resource *res) noexcept
Set the default RTOS system memory manager.
Definition: os-memory.cpp:208
const char * name(void) const
Get object name.
Definition: os-decls.h:760
memory_resource * set_resource_typed< timer >(memory_resource *res) noexcept
Definition: os-memory.cpp:354
memory_resource * set_resource_typed< memory_pool >(memory_resource *res) noexcept
Definition: os-memory.cpp:282
std::size_t max_size(void) const noexcept
Get the largest value that can be passed to allocate().
Definition: os-memory.h:1337
std::unique_ptr< T, allocator_deleter< allocator_typed< T, U > >> unique_ptr
Type of a RTOS unique pointer to objects of type T.
Definition: os-memory.h:1076
memory_resource * set_resource_typed(memory_resource *res) noexcept
Function template to set a memory resource.
virtual bool do_is_equal(memory_resource const &other) const noexcept
Implementation of the equality comparator.
Definition: os-memory.cpp:422
A allocator_type
Standard allocator type definition.
Definition: os-memory.h:848
bool operator!=(thread::id x, thread::id y) noexcept
Definition: thread:263
memory_resource * get_resource_typed< timer >(void) noexcept
Definition: os-memory.h:1245
memory_resource * get_resource_typed< semaphore >(void) noexcept
Definition: os-memory.h:1230
Base class for named objects.
Definition: os-decls.h:350
void init_once_default_resource(void)
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition: trace.cpp:74
bool operator!=(const memory_resource &lhs, const memory_resource &rhs) noexcept
Compare the memory_resource instances for inequality.
Definition: os-memory.h:1472
std::size_t free_bytes(void)
Get the current size of all free chunks.
Definition: os-memory.h:1417
void * allocate(std::size_t bytes, std::size_t alignment=max_align)
Allocate a memory block.
Definition: os-memory.h:1285
typename allocator_traits::pointer pointer
Definition: os-memory.h:855
virtual void do_deallocate(void *addr, std::size_t bytes, std::size_t alignment) noexcept=0
Implementation of the memory deallocator.
bool operator==(const memory_resource &lhs, const memory_resource &rhs) noexcept
Compare the memory_resource instances for equality.
Definition: os-memory.h:1466
bool coalesce(void) noexcept
Coalesce free blocks.
Definition: os-memory.h:1364
T value_type
Type of elements to be allocated.
Definition: os-memory.h:539
allocator_stateless_default_resource() noexcept=default
Default constructor. Construct a default resource allocator object instance.
void __throw_system_error(int ev, const char *what_arg)
memory_resource * set_resource_typed< event_flags >(memory_resource *res) noexcept
Definition: os-memory.cpp:264
memory_resource * get_resource_typed< memory_pool >(void) noexcept
Definition: os-memory.h:1185
std::size_t total_bytes(void)
Get the total size of managed memory.
Definition: os-memory.h:1399
memory_resource * default_resource
memory_resource * malloc_resource(void) noexcept
Get the address of a memory manager based on POSIX malloc().
Definition: os-memory.cpp:191
virtual std::size_t do_max_size(void) const noexcept
Implementation of the function to get max size.
Definition: os-memory.cpp:438
void reset(void) noexcept
Reset the memory manager to the initial state.
Definition: os-memory.h:1348
memory_resource * get_resource_typed< event_flags >(void) noexcept
Definition: os-memory.h:1170
memory_resource * get_resource_typed< message_queue >(void) noexcept
Definition: os-memory.h:1200
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
Standard allocator based on the RTOS system default memory manager.
Definition: os-decls.h:82
std::size_t free_chunks(void)
Get the current number of free chunks.
Definition: os-memory.h:1429
bool operator==(thread::id x, thread::id y) noexcept
Definition: thread:257
memory_resource * get_resource_typed(void) noexcept
Function template to get a memory resource.
void(*)(void) out_of_memory_handler_t
Type of out of memory handler.
Definition: os-memory.h:142
void __throw_bad_alloc(void)
USB switch to High Speed occurred.
Definition: usb-device.h:142
memory_resource * set_resource_typed< thread >(memory_resource *res) noexcept
Definition: os-memory.cpp:228
std::size_t max_size(void) const noexcept
The maximum number of elements that can be passed to allocate().
Definition: os-memory.h:1511
std::size_t deallocations(void)
Get the number of deallocations.
Definition: os-memory.h:1441