µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os::rtos::memory::memory_resource Class Referenceabstract

Memory resource manager (abstract class). More...

#include <cmsis-plus/rtos/os.h>

+ Inheritance diagram for os::rtos::memory::memory_resource:

Public Member Functions

Constructors & Destructor
 memory_resource ()=default
 Default constructor. Construct a memory resource object instance.
 
 memory_resource (const char *name)
 Construct a named memory resource object instance.
 
virtual ~memory_resource ()
 Destruct the memory resource object instance.
 
Public Member Functions
void * allocate (std::size_t bytes, std::size_t alignment=max_align)
 Allocate a memory block.
 
void deallocate (void *addr, std::size_t bytes, std::size_t alignment=max_align) noexcept
 Deallocate the previously allocated memory block.
 
bool is_equal (memory_resource const &other) const noexcept
 Compare for equality with another memory_resource.
 
void reset (void) noexcept
 Reset the memory manager to the initial state.
 
bool coalesce (void) noexcept
 Coalesce free blocks.
 
std::size_t max_size (void) const noexcept
 Get the largest value that can be passed to allocate().
 
out_of_memory_handler_t out_of_memory_handler (out_of_memory_handler_t handler)
 Set the out of memory handler.
 
out_of_memory_handler_t out_of_memory_handler (void)
 Get the out of memory handler.
 
std::size_t total_bytes (void)
 Get the total size of managed memory.
 
std::size_t allocated_bytes (void)
 Get the current size of all allocated chunks.
 
std::size_t max_allocated_bytes (void)
 Get the maximum allocated size.
 
std::size_t free_bytes (void)
 Get the current size of all free chunks.
 
std::size_t allocated_chunks (void)
 Get the current number of allocated chunks.
 
std::size_t free_chunks (void)
 Get the current number of free chunks.
 
std::size_t allocations (void)
 Get the number of allocations.
 
std::size_t deallocations (void)
 Get the number of deallocations.
 
void trace_print_statistics (void)
 Print a long message with usage statistics.
 
Public Member Functions
const char * name (void) const
 Get object name.
 

Static Public Attributes

static constexpr std::size_t max_align = alignof (std::max_align_t)
 The largest alignment for the platform. Also default when supplied alignment is not supported.
 

Protected Member Functions

Private Member Functions
virtual void * do_allocate (std::size_t bytes, std::size_t alignment)=0
 Implementation of the memory allocator.
 
virtual void do_deallocate (void *addr, std::size_t bytes, std::size_t alignment) noexcept=0
 Implementation of the memory deallocator.
 
virtual bool do_is_equal (memory_resource const &other) const noexcept
 Implementation of the equality comparator.
 
virtual std::size_t do_max_size (void) const noexcept
 Implementation of the function to get max size.
 
virtual void do_reset (void) noexcept
 Implementation of the function to reset the memory manager.
 
virtual bool do_coalesce (void) noexcept
 Implementation of the function to coalesce free blocks.
 
void internal_increase_allocated_statistics (std::size_t bytes) noexcept
 Update statistics after allocation.
 
void internal_decrease_allocated_statistics (std::size_t bytes) noexcept
 Update statistics after deallocation.
 

Detailed Description

This class is based on the standard C++17 memory manager, with several extensions, to control the throw behaviour and to add statistics.

Definition at line 158 of file os-memory.h.

Constructor & Destructor Documentation

◆ memory_resource() [1/2]

os::rtos::memory::memory_resource::memory_resource ( )
default

◆ memory_resource() [2/2]

os::rtos::memory::memory_resource::memory_resource ( const char *  name)
inline
Parameters
namePointer to name.

Definition at line 1264 of file os-memory.h.

1265 : object_named{ name }
1266 {
1267 }
object_named()
Construct a named object instance.
Definition os-core.cpp:620
const char * name(void) const
Get object name.
Definition os-decls.h:753

◆ ~memory_resource()

os::rtos::memory::memory_resource::~memory_resource ( )
virtual

Definition at line 365 of file os-memory.cpp.

366 {
367 }

Member Function Documentation

◆ allocate()

void * os::rtos::memory::memory_resource::allocate ( std::size_t  bytes,
std::size_t  alignment = max_align 
)
inline
Parameters
bytesNumber of bytes to allocate.
alignmentAlignment constraint (power of 2).
Returns
Pointer to newly allocated block, or nullptr.

Allocate storage with a size of at least bytes bytes. The returned storage is aligned to the specified alignment if such alignment is supported, and to alignof(std::max_align_t) otherwise.

If the storage of the requested size and alignment cannot be obtained:

  • if the out of memory handler is not set, return nullptr;
  • if the out of memory handler is set, call it and retry.

Equivalent to return do_allocate(bytes, alignment);.

Exceptions
The code itself throws nothing, but if the out of memory handler is set, it may throw a bad_alloc() exception.
See also
do_allocate();

Definition at line 1290 of file os-memory.h.

1291 {
1292 ++allocations_;
1293 return do_allocate (bytes, alignment);
1294 }
virtual void * do_allocate(std::size_t bytes, std::size_t alignment)=0
Implementation of the memory allocator.

References do_allocate().

Referenced by os::estd::pmr::polymorphic_allocator< T >::allocate(), calloc(), malloc(), operator new(), and realloc().

◆ allocated_bytes()

std::size_t os::rtos::memory::memory_resource::allocated_bytes ( void  )
inline
Parameters
None.
Returns
Number of bytes.

Definition at line 1402 of file os-memory.h.

1403 {
1404 return allocated_bytes_;
1405 }

Referenced by trace_print_statistics().

◆ allocated_chunks()

std::size_t os::rtos::memory::memory_resource::allocated_chunks ( void  )
inline
Parameters
None.
Returns
Number of chunks.

Definition at line 1420 of file os-memory.h.

1421 {
1422 return allocated_chunks_;
1423 }

Referenced by trace_print_statistics().

◆ allocations()

std::size_t os::rtos::memory::memory_resource::allocations ( void  )
inline
Parameters
None.
Returns
Number of allocations.

Definition at line 1432 of file os-memory.h.

1433 {
1434 return allocations_;
1435 }

Referenced by trace_print_statistics().

◆ coalesce()

bool os::rtos::memory::memory_resource::coalesce ( void  )
inlinenoexcept
Parameters
None.
Return values
trueif the operation freed more memory.
falseif the operation was ineffective.

In case the memory manager does not coalesce during deallocation, traverse the list of free blocks and coalesce.

Return true if the operation was successful and at least one larger block resulted.

See also
do_coalesce();

Definition at line 1365 of file os-memory.h.

1366 {
1367 return do_coalesce ();
1368 }
virtual bool do_coalesce(void) noexcept
Implementation of the function to coalesce free blocks.

◆ deallocate()

void os::rtos::memory::memory_resource::deallocate ( void *  addr,
std::size_t  bytes,
std::size_t  alignment = max_align 
)
inlinenoexcept
Parameters
addrAddress of the block to free.
bytesNumber of bytes to deallocate (may be 0 if unknown).
alignmentAlignment constraint (power of 2).
Returns
Nothing.

Deallocate the storage pointed to by addr. The address shall have been returned by a prior call to allocate() on a memory_resource that compares equal to *this, and the storage it points to shall not yet have been deallocated.

Equivalent to return do_deallocate(p, bytes, alignment);.

Exceptions
Throws nothing.
See also
do_deallocate();

Definition at line 1312 of file os-memory.h.

1314 {
1315 ++deallocations_;
1316 do_deallocate (addr, bytes, alignment);
1317 }
virtual void do_deallocate(void *addr, std::size_t bytes, std::size_t alignment) noexcept=0
Implementation of the memory deallocator.

Referenced by os::estd::pmr::polymorphic_allocator< T >::deallocate(), free(), and realloc().

◆ deallocations()

std::size_t os::rtos::memory::memory_resource::deallocations ( void  )
inline
Parameters
None.
Returns
Number of deallocations

Definition at line 1438 of file os-memory.h.

1439 {
1440 return deallocations_;
1441 }

Referenced by trace_print_statistics().

◆ do_allocate()

os::rtos::memory::memory_resource::do_allocate ( std::size_t  bytes,
std::size_t  alignment 
)
protectedpure virtual
Parameters
bytesNumber of bytes to allocate.
alignmentAlignment constraint (power of 2).
Returns
Pointer to newly allocated block, or nullptr.

Allocates storage with a size of at least bytes bytes. The returned storage is aligned to the specified alignment if such alignment is supported, and to alignof(std::max_align_t) otherwise.

The function should return a nullptr when no more memory is available.

Exceptions
The code itself should throw nothing, but if the out of memory handler is set, it may throw a bad_alloc() exception.

Implemented in os::memory::block_pool, os::memory::first_fit_top, os::memory::lifo, os::memory::malloc_memory_resource, and os::memory::null_memory_resource.

Referenced by allocate().

◆ do_coalesce()

bool os::rtos::memory::memory_resource::do_coalesce ( void  )
protectedvirtualnoexcept
Parameters
None.
Return values
trueif the operation resulted in larger blocks.
falseif the operation was ineffective.

The default implementation of this virtual function returns false, meaning the operation was ineffective.

Override this function to perform the action.

Standard compliance
Extension to standard.

Definition at line 468 of file os-memory.cpp.

469 {
470 return false;
471 }

◆ do_deallocate()

os::rtos::memory::memory_resource::do_deallocate ( void *  addr,
std::size_t  bytes,
std::size_t  alignment 
)
protectedpure virtualnoexcept
Parameters
addrAddress of a previously allocated block to free.
bytesNumber of bytes to deallocate (may be 0 if unknown).
alignmentAlignment constraint (power of 2).
Returns
Nothing.

Deallocates the storage pointed to by addr. The address must have been returned by a prior call to allocate(bytes, alignment) on a memory_resource that compares equal to *this, and the storage it points to must not yet have been deallocated, otherwise the behaviour is undefined.

Exceptions
Should throw nothing itself.

Implemented in os::memory::block_pool, os::memory::first_fit_top, os::memory::malloc_memory_resource, and os::memory::null_memory_resource.

◆ do_is_equal()

bool os::rtos::memory::memory_resource::do_is_equal ( memory_resource const &  other) const
protectedvirtualnoexcept
Parameters
otherReference to another memory_resource.
Return values
trueThe memory_resource objects are equal.
falseThe memory_resource objects are not equal.

Compares *this for equality with other. Two memory_resources compare equal if and only if memory allocated from one memory_resource can be deallocated from the other and vice versa.

The most-derived type of other may not match the most derived type of *this. A derived class implementation therefore must typically check whether the most derived types of *this and other match using dynamic_cast, and immediately return false if the cast fails.

Exceptions
Throws nothing.

Definition at line 419 of file os-memory.cpp.

421 {
422 return &other == this;
423 }

◆ do_max_size()

std::size_t os::rtos::memory::memory_resource::do_max_size ( void  ) const
protectedvirtualnoexcept
Parameters
None.
Returns
Integer with size in bytes, or 0 if unknown.

The default implementation of this virtual function returns zero, meaning the size is not known.

Override this function to return the actual size.

Standard compliance
Extension to standard.

Reimplemented in os::memory::block_pool, and os::memory::first_fit_top.

Definition at line 436 of file os-memory.cpp.

437 {
438 return 0;
439 }

◆ do_reset()

void os::rtos::memory::memory_resource::do_reset ( void  )
protectedvirtualnoexcept
Parameters
None.
Returns
Nothing.

The default implementation of this virtual function does nothing.

Override this function to perform the action.

Standard compliance
Extension to standard.

Reimplemented in os::memory::block_pool, and os::memory::first_fit_top.

Definition at line 452 of file os-memory.cpp.

453 {
454 return;
455 }

◆ free_bytes()

std::size_t os::rtos::memory::memory_resource::free_bytes ( void  )
inline
Parameters
None.
Returns
Number of bytes.

Definition at line 1414 of file os-memory.h.

1415 {
1416 return free_bytes_;
1417 }

Referenced by trace_print_statistics().

◆ free_chunks()

std::size_t os::rtos::memory::memory_resource::free_chunks ( void  )
inline
Parameters
None.
Returns
Number of chunks.

Definition at line 1426 of file os-memory.h.

1427 {
1428 return free_chunks_;
1429 }

Referenced by trace_print_statistics().

◆ internal_decrease_allocated_statistics()

void os::rtos::memory::memory_resource::internal_decrease_allocated_statistics ( std::size_t  bytes)
protectednoexcept
Parameters
[in]bytesNumber of deallocated bytes.
Returns
Nothing.

Definition at line 490 of file os-memory.cpp.

492 {
493 // Update statistics.
494 // What is subtracted from allocated is added to free.
495 allocated_bytes_ -= bytes;
496 free_bytes_ += bytes;
497 --allocated_chunks_;
498 ++free_chunks_;
499 }

◆ internal_increase_allocated_statistics()

void os::rtos::memory::memory_resource::internal_increase_allocated_statistics ( std::size_t  bytes)
protectednoexcept
Parameters
[in]bytesNumber of allocated bytes.
Returns
Nothing.

Definition at line 474 of file os-memory.cpp.

476 {
477 // Update statistics.
478 // What is subtracted from free is added to allocated.
479 allocated_bytes_ += bytes;
480 if (allocated_bytes_ > max_allocated_bytes_)
481 {
482 max_allocated_bytes_ = allocated_bytes_;
483 }
484 free_bytes_ -= bytes;
485 ++allocated_chunks_;
486 --free_chunks_;
487 }

Referenced by os::memory::block_pool::do_allocate(), and os::memory::first_fit_top::internal_align_().

◆ is_equal()

bool os::rtos::memory::memory_resource::is_equal ( memory_resource const &  other) const
inlinenoexcept
Parameters
otherReference to another memory_resource.
Return values
trueThe memory_resource objects are equal.
falseThe memory_resource objects are not equal.

Compare *this for equality with other. Two memory_resources compare equal if and only if memory allocated from one memory_resource can be deallocated from the other and vice versa.

Exceptions
Throws nothing.
See also
do_is_equal();

Definition at line 1331 of file os-memory.h.

1332 {
1333 return do_is_equal (other);
1334 }
virtual bool do_is_equal(memory_resource const &other) const noexcept
Implementation of the equality comparator.

◆ max_allocated_bytes()

std::size_t os::rtos::memory::memory_resource::max_allocated_bytes ( void  )
inline
Parameters
None.
Returns
Number of bytes.

Definition at line 1408 of file os-memory.h.

1409 {
1410 return max_allocated_bytes_;
1411 }

Referenced by trace_print_statistics().

◆ max_size()

std::size_t os::rtos::memory::memory_resource::max_size ( void  ) const
inlinenoexcept
Parameters
None.
Returns
Number of bytes or 0 if unknown.
See also
do_max_size();

Definition at line 1340 of file os-memory.h.

1341 {
1342 return do_max_size ();
1343 }
virtual std::size_t do_max_size(void) const noexcept
Implementation of the function to get max size.

◆ name()

const char * os::rtos::internal::object_named::name ( void  ) const
inlineinherited
Parameters
None.
Returns
A null terminated string.

All objects return a non-null string; anonymous objects return "-".

Note
Can be invoked from Interrupt Service Routines.

Definition at line 753 of file os-decls.h.

754 {
755 return name_;
756 }

Referenced by os::memory::lifo::lifo(), os::memory::malloc_memory_resource::malloc_memory_resource(), os::rtos::message_queue_typed< T, Allocator >::message_queue_typed(), os::memory::block_pool::~block_pool(), os::rtos::event_flags::~event_flags(), os::memory::first_fit_top::~first_fit_top(), os::memory::lifo::~lifo(), os::memory::malloc_memory_resource::~malloc_memory_resource(), os::rtos::memory_pool::~memory_pool(), os::rtos::message_queue::~message_queue(), os::rtos::mutex::~mutex(), os::rtos::semaphore::~semaphore(), os::rtos::thread::~thread(), os::rtos::timer::~timer(), os::rtos::memory_pool::alloc(), os::rtos::thread::cancel(), os::rtos::event_flags::clear(), os::rtos::mutex::consistent(), os::rtos::thread::detach(), os::memory::new_delete_memory_resource::do_allocate(), os::memory::block_pool::do_allocate(), os::memory::first_fit_top::do_allocate(), os::memory::lifo::do_allocate(), os::memory::malloc_memory_resource::do_allocate(), os::rtos::thread::flags_raise(), os::rtos::memory_pool::free(), os::rtos::event_flags::get(), os::rtos::thread::interrupt(), os::rtos::thread::join(), os::rtos::thread::kill(), os::rtos::internal::terminated_threads_list::link(), os::rtos::mutex::lock(), out_of_memory_handler(), os::rtos::semaphore::post(), os::rtos::mutex::prio_ceiling(), os::rtos::mutex::prio_ceiling(), os::rtos::thread::priority(), os::rtos::thread::priority_inherited(), os::rtos::event_flags::raise(), os::rtos::message_queue::receive(), os::rtos::memory_pool::reset(), os::rtos::message_queue::reset(), os::rtos::mutex::reset(), os::rtos::semaphore::reset(), os::rtos::thread::resume(), os::rtos::message_queue::send(), os::rtos::clock::sleep_for(), os::rtos::timer::start(), os::rtos::timer::stop(), os::rtos::memory_pool::timed_alloc(), os::rtos::mutex::timed_lock(), os::rtos::message_queue::timed_receive(), os::rtos::message_queue::timed_send(), os::rtos::semaphore::timed_wait(), os::rtos::event_flags::timed_wait(), trace_print_statistics(), os::rtos::memory_pool::try_alloc(), os::rtos::mutex::try_lock(), os::rtos::message_queue::try_receive(), os::rtos::message_queue::try_send(), os::rtos::event_flags::try_wait(), os::rtos::semaphore::try_wait(), os::rtos::internal::ready_threads_list::unlink_head(), os::rtos::mutex::unlock(), os::rtos::event_flags::wait(), os::rtos::semaphore::wait(), and os::rtos::event_flags::waiting().

◆ out_of_memory_handler() [1/2]

out_of_memory_handler_t os::rtos::memory::memory_resource::out_of_memory_handler ( out_of_memory_handler_t  handler)
inline
Parameters
handlerPointer to new handler.
Returns
Pointer to old handler.
Standard compliance
Extension to standard.

Definition at line 1375 of file os-memory.h.

1376 {
1377 trace::printf ("%s(%p) @%p %s\n", __func__, handler, this, name ());
1378
1379 out_of_memory_handler_t tmp = out_of_memory_handler_;
1380 out_of_memory_handler_ = handler;
1381
1382 return tmp;
1383 }
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
void(*)(void) out_of_memory_handler_t
Type of out of memory handler.
Definition os-memory.h:141

References os::rtos::internal::object_named::name(), and os::trace::printf().

Referenced by os_startup_initialize_free_store().

◆ out_of_memory_handler() [2/2]

out_of_memory_handler_t os::rtos::memory::memory_resource::out_of_memory_handler ( void  )
inline
Parameters
None.
Returns
Pointer to existing handler.
Standard compliance
Extension to standard.

Definition at line 1390 of file os-memory.h.

1391 {
1392 return out_of_memory_handler_;
1393 }

◆ reset()

void os::rtos::memory::memory_resource::reset ( void  )
inlinenoexcept
Parameters
None.
Returns
Nothing.
See also
do_reset();

Definition at line 1349 of file os-memory.h.

1350 {
1351 do_reset ();
1352 }
virtual void do_reset(void) noexcept
Implementation of the function to reset the memory manager.

◆ total_bytes()

std::size_t os::rtos::memory::memory_resource::total_bytes ( void  )
inline
Returns
Number of bytes.

Definition at line 1396 of file os-memory.h.

1397 {
1398 return total_bytes_;
1399 }

Referenced by trace_print_statistics().

◆ trace_print_statistics()

void os::rtos::memory::memory_resource::trace_print_statistics ( void  )
inline
Parameters
None.
Returns
Nothing.

Definition at line 1444 of file os-memory.h.

1445 {
1446#if defined(TRACE)
1447 trace::printf ("Memory '%s' @%p: \n"
1448 "\ttotal: %u bytes, \n"
1449 "\tallocated: %u bytes in %u chunk(s), \n"
1450 "\tfree: %u bytes in %u chunk(s), \n"
1451 "\tmax: %u bytes, \n"
1452 "\tcalls: %u allocs, %u deallocs\n",
1453 name (), this, total_bytes (), allocated_bytes (),
1456 deallocations ());
1457#endif /* defined(TRACE) */
1458 }
std::size_t allocated_chunks(void)
Get the current number of allocated chunks.
Definition os-memory.h:1420
std::size_t allocated_bytes(void)
Get the current size of all allocated chunks.
Definition os-memory.h:1402
std::size_t max_allocated_bytes(void)
Get the maximum allocated size.
Definition os-memory.h:1408
std::size_t allocations(void)
Get the number of allocations.
Definition os-memory.h:1432
std::size_t total_bytes(void)
Get the total size of managed memory.
Definition os-memory.h:1396
std::size_t deallocations(void)
Get the number of deallocations.
Definition os-memory.h:1438
std::size_t free_chunks(void)
Get the current number of free chunks.
Definition os-memory.h:1426
std::size_t free_bytes(void)
Get the current size of all free chunks.
Definition os-memory.h:1414

References allocated_bytes(), allocated_chunks(), allocations(), deallocations(), free_bytes(), free_chunks(), max_allocated_bytes(), os::rtos::internal::object_named::name(), os::trace::printf(), and total_bytes().

Referenced by os_terminate_goodbye().

Member Data Documentation

◆ max_align

constexpr std::size_t os::rtos::memory::memory_resource::max_align = alignof (std::max_align_t)
staticconstexpr

Definition at line 166 of file os-memory.h.


The documentation for this class was generated from the following files: