µ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::rtos::memory::memory_resource Class Referenceabstract

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

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

Inherits os::rtos::internal::object_named.

Inherited by os::memory::block_pool, os::memory::first_fit_top, os::memory::malloc_memory_resource, os::memory::new_delete_memory_resource, and os::memory::null_memory_resource.

Public Member Functions

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

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. More...
 

Protected Member Functions

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

Detailed Description

Memory resource manager (abstract class).

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 153 of file os-memory.h.

Constructor & Destructor Documentation

◆ memory_resource() [1/2]

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

Default constructor. Construct a memory resource object instance.

◆ memory_resource() [2/2]

os::rtos::memory::memory_resource::memory_resource ( const char *  name)
inline

Construct a named memory resource object instance.

Parameters
namePointer to name.

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

◆ ~memory_resource()

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

Destruct the memory resource object instance.

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

Member Function Documentation

◆ allocate()

void * os::rtos::memory::memory_resource::allocate ( std::size_t  bytes,
std::size_t  alignment = max_align 
)
inline

Allocate a memory block.

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 1285 of file os-memory.h.

◆ allocated_bytes()

std::size_t os::rtos::memory::memory_resource::allocated_bytes ( void  )
inline

Get the current size of all allocated chunks.

Parameters
None.
Returns
Number of bytes.

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

◆ allocated_chunks()

std::size_t os::rtos::memory::memory_resource::allocated_chunks ( void  )
inline

Get the current number of allocated chunks.

Parameters
None.
Returns
Number of chunks.

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

◆ allocations()

std::size_t os::rtos::memory::memory_resource::allocations ( void  )
inline

Get the number of allocations.

Parameters
None.
Returns
Number of allocations.

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

◆ coalesce()

bool os::rtos::memory::memory_resource::coalesce ( void  )
inlinenoexcept

Coalesce free blocks.

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 1364 of file os-memory.h.

◆ deallocate()

void os::rtos::memory::memory_resource::deallocate ( void *  addr,
std::size_t  bytes,
std::size_t  alignment = max_align 
)
inlinenoexcept

Deallocate the previously allocated memory block.

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 1307 of file os-memory.h.

◆ deallocations()

std::size_t os::rtos::memory::memory_resource::deallocations ( void  )
inline

Get the number of deallocations.

Parameters
None.
Returns
Number of deallocations

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

◆ do_allocate()

os::rtos::memory::memory_resource::do_allocate ( std::size_t  bytes,
std::size_t  alignment 
)
protectedpure virtual

Implementation of the memory allocator.

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::first_fit_top, os::memory::block_pool, os::memory::lifo, os::memory::malloc_memory_resource, and os::memory::null_memory_resource.

◆ do_coalesce()

bool os::rtos::memory::memory_resource::do_coalesce ( void  )
protectedvirtualnoexcept

Implementation of the function to coalesce free blocks.

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 470 of file os-memory.cpp.

◆ do_deallocate()

os::rtos::memory::memory_resource::do_deallocate ( void *  addr,
std::size_t  bytes,
std::size_t  alignment 
)
protectedpure virtualnoexcept

Implementation of the memory deallocator.

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::first_fit_top, os::memory::block_pool, 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

Implementation of the equality comparator.

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 422 of file os-memory.cpp.

◆ do_max_size()

std::size_t os::rtos::memory::memory_resource::do_max_size ( void  ) const
protectedvirtualnoexcept

Implementation of the function to get max size.

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::first_fit_top, and os::memory::block_pool.

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

◆ do_reset()

void os::rtos::memory::memory_resource::do_reset ( void  )
protectedvirtualnoexcept

Implementation of the function to reset the memory manager.

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::first_fit_top, and os::memory::block_pool.

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

◆ free_bytes()

std::size_t os::rtos::memory::memory_resource::free_bytes ( void  )
inline

Get the current size of all free chunks.

Parameters
None.
Returns
Number of bytes.

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

◆ free_chunks()

std::size_t os::rtos::memory::memory_resource::free_chunks ( void  )
inline

Get the current number of free chunks.

Parameters
None.
Returns
Number of chunks.

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

◆ internal_decrease_allocated_statistics()

void os::rtos::memory::memory_resource::internal_decrease_allocated_statistics ( std::size_t  bytes)
protectednoexcept

Update statistics after deallocation.

Parameters
[in]bytesNumber of deallocated bytes.
Returns
Nothing.

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

◆ internal_increase_allocated_statistics()

void os::rtos::memory::memory_resource::internal_increase_allocated_statistics ( std::size_t  bytes)
protectednoexcept

Update statistics after allocation.

Parameters
[in]bytesNumber of allocated bytes.
Returns
Nothing.

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

◆ is_equal()

bool os::rtos::memory::memory_resource::is_equal ( memory_resource const &  other) const
inlinenoexcept

Compare for equality with another memory_resource.

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 1326 of file os-memory.h.

◆ max_allocated_bytes()

std::size_t os::rtos::memory::memory_resource::max_allocated_bytes ( void  )
inline

Get the maximum allocated size.

Parameters
None.
Returns
Number of bytes.

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

◆ max_size()

std::size_t os::rtos::memory::memory_resource::max_size ( void  ) const
inlinenoexcept

Get the largest value that can be passed to allocate().

Parameters
None.
Returns
Number of bytes or 0 if unknown.
See also
do_max_size();

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

◆ name()

const char * os::rtos::internal::object_named::name ( void  ) const
inlineinherited

Get object name.

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 760 of file os-decls.h.

◆ 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

Set the out of memory handler.

Parameters
handlerPointer to new handler.
Returns
Pointer to old handler.
Standard compliance
Extension to standard.

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

◆ out_of_memory_handler() [2/2]

out_of_memory_handler_t os::rtos::memory::memory_resource::out_of_memory_handler ( void  )
inline

Get the out of memory handler.

Parameters
None.
Returns
Pointer to existing handler.
Standard compliance
Extension to standard.

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

◆ reset()

void os::rtos::memory::memory_resource::reset ( void  )
inlinenoexcept

Reset the memory manager to the initial state.

Parameters
None.
Returns
Nothing.
See also
do_reset();

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

◆ total_bytes()

std::size_t os::rtos::memory::memory_resource::total_bytes ( void  )
inline

Get the total size of managed memory.

Returns
Number of bytes.

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

◆ trace_print_statistics()

void os::rtos::memory::memory_resource::trace_print_statistics ( void  )
inline

Print a long message with usage statistics.

Parameters
None.
Returns
Nothing.

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

Member Data Documentation

◆ max_align

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

The largest alignment for the platform. Also default when supplied alignment is not supported.

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


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