Mbed OS Reference
Loading...
Searching...
No Matches
MemoryPool< T, pool_sz > Class Template Reference

Define and manage fixed-size memory pools of objects of a given type. More...

#include <MemoryPool.h>

Inheritance diagram for MemoryPool< T, pool_sz >:
NonCopyable< MemoryPool< T, pool_sz > >

Public Member Functions

 MemoryPool ()
 Create and Initialize a memory pool. More...
 
 ~MemoryPool ()
 Destroy a memory pool. More...
 
T * alloc ()
 Allocate a memory block from a memory pool, without blocking. More...
 
T * try_alloc ()
 Allocate a memory block from a memory pool, without blocking. More...
 
T * alloc_for (uint32_t millisec)
 Allocate a memory block from a memory pool, optionally blocking. More...
 
T * try_alloc_for (Kernel::Clock::duration_u32 rel_time)
 Allocate a memory block from a memory pool, optionally blocking. More...
 
T * alloc_until (uint64_t millisec)
 Allocate a memory block from a memory pool, blocking. More...
 
T * try_alloc_until (Kernel::Clock::time_point abs_time)
 Allocate a memory block from a memory pool, blocking. More...
 
T * calloc ()
 Allocate a memory block from a memory pool, without blocking, and set memory block to zero. More...
 
T * try_calloc ()
 Allocate a memory block from a memory pool, without blocking, and set memory block to zero. More...
 
T * calloc_for (uint32_t millisec)
 Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero. More...
 
T * try_calloc_for (Kernel::Clock::duration_u32 rel_time)
 Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero. More...
 
T * calloc_until (uint64_t millisec)
 Allocate a memory block from a memory pool, blocking, and set memory block to zero. More...
 
T * try_calloc_until (Kernel::Clock::time_point abs_time)
 Allocate a memory block from a memory pool, blocking, and set memory block to zero. More...
 
osStatus free (T *block)
 Free a memory block. More...
 

Detailed Description

template<typename T, uint32_t pool_sz>
class rtos::MemoryPool< T, pool_sz >

Define and manage fixed-size memory pools of objects of a given type.

Template Parameters
Tdata type of a single object (element).
queue_szmaximum number of objects (elements) in the memory pool.
Note
Memory considerations: The memory pool data store and control structures will be created on current thread's stack, both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
Bare metal profile: This class is not supported.

Definition at line 59 of file MemoryPool.h.

Constructor & Destructor Documentation

◆ MemoryPool()

Create and Initialize a memory pool.

Note
You cannot call this function from ISR context.

Definition at line 66 of file MemoryPool.h.

◆ ~MemoryPool()

~MemoryPool ( )

Destroy a memory pool.

Note
You cannot call this function from ISR context.

Definition at line 82 of file MemoryPool.h.

Member Function Documentation

◆ alloc()

T * alloc ( )

Allocate a memory block from a memory pool, without blocking.

Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context.
Deprecated:
Replaced with try_alloc. In future alloc() will be an untimed blocking call.

Definition at line 94 of file MemoryPool.h.

◆ try_alloc()

T * try_alloc ( )

Allocate a memory block from a memory pool, without blocking.

This method works like std::malloc or std::allocator<T>::allocate in that the returned memory block is not initialized. For types with a non-trivial constructor placement new must be used to construct an object in the returned storage.

Example:

MyObject *obj = pool.alloc();
if (obj) {
new (obj) MyObject(1, 2);
}
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context.

Definition at line 117 of file MemoryPool.h.

◆ alloc_for()

T * alloc_for ( uint32_t  millisec)

Allocate a memory block from a memory pool, optionally blocking.

Parameters
millisectimeout value (osWaitForever to wait forever)
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context if the millisec parameter is set to 0.
Deprecated:
Replaced with try_alloc_for. For example use try_alloc_for(5s) rather than alloc_for(5000).

Definition at line 130 of file MemoryPool.h.

◆ try_alloc_for()

T * try_alloc_for ( Kernel::Clock::duration_u32  rel_time)

Allocate a memory block from a memory pool, optionally blocking.

See also
MemoryPool::try_alloc
Parameters
rel_timetimeout value (Kernel::wait_for_u32_forever to wait forever)
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context if the rel_time parameter is set to 0.

Definition at line 142 of file MemoryPool.h.

◆ alloc_until()

T * alloc_until ( uint64_t  millisec)

Allocate a memory block from a memory pool, blocking.

Parameters
millisecabsolute timeout time, referenced to Kernel::get_ms_count().
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You cannot call this function from ISR context.
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the wait will time out earlier than specified.
Deprecated:
Replaced with try_alloc_until. For example use try_alloc_until(Kernel::Clock::now() + 5s) rather than alloc_until(Kernel::get_ms_count() + 5000).

Definition at line 160 of file MemoryPool.h.

◆ try_alloc_until()

T * try_alloc_until ( Kernel::Clock::time_point  abs_time)

Allocate a memory block from a memory pool, blocking.

See also
MemoryPool::try_alloc
Parameters
abs_timeabsolute timeout time, referenced to Kernel::Clock.
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You cannot call this function from ISR context.
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the wait will time out earlier than specified.

Definition at line 176 of file MemoryPool.h.

◆ calloc()

T * calloc ( )

Allocate a memory block from a memory pool, without blocking, and set memory block to zero.

Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context.
Deprecated:
Replaced with try_calloc. In future calloc() will be an untimed blocking call.

Definition at line 197 of file MemoryPool.h.

◆ try_calloc()

T * try_calloc ( )

Allocate a memory block from a memory pool, without blocking, and set memory block to zero.

Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context.

Definition at line 207 of file MemoryPool.h.

◆ calloc_for()

T * calloc_for ( uint32_t  millisec)

Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.

Parameters
millisectimeout value (osWaitForever to wait forever)
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context if the millisec parameter is set to 0.
Deprecated:
Replaced with try_calloc_for. For example use try_calloc_for(5s) rather than calloc_for(5000).

Definition at line 224 of file MemoryPool.h.

◆ try_calloc_for()

T * try_calloc_for ( Kernel::Clock::duration_u32  rel_time)

Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.

Parameters
rel_timetimeout value (Kernel::wait_for_u32_forever to wait forever)
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You may call this function from ISR context if the rel_time parameter is set to 0.

Definition at line 235 of file MemoryPool.h.

◆ calloc_until()

T * calloc_until ( uint64_t  millisec)

Allocate a memory block from a memory pool, blocking, and set memory block to zero.

Parameters
millisecabsolute timeout time, referenced to Kernel::get_ms_count().
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You cannot call this function from ISR context.
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the wait will time out earlier than specified.
Deprecated:
Replaced with try_calloc_until. For example use try_calloc_until(Kernel::Clock::now() + 5s) rather than calloc_until(Kernel::get_ms_count() + 5000).

Definition at line 257 of file MemoryPool.h.

◆ try_calloc_until()

T * try_calloc_until ( Kernel::Clock::time_point  abs_time)

Allocate a memory block from a memory pool, blocking, and set memory block to zero.

Parameters
abs_timeabsolute timeout time, referenced to Kernel::Clock.
Returns
address of the allocated memory block or nullptr in case of no memory available.
Note
You cannot call this function from ISR context.
the underlying RTOS may have a limit to the maximum wait time due to internal 32-bit computations, but this is guaranteed to work if the wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, the wait will time out earlier than specified.

Definition at line 272 of file MemoryPool.h.

◆ free()

osStatus free ( T *  block)

Free a memory block.

This method works like std::free or std::allocator<T>::deallocate in that any object in the memory is not destroyed. For types with a non-trivial destructor that destructor must be called manually before freeing the memory.

Example:

obj->~MyObject();
pool.free(obj);
Parameters
blockaddress of the allocated memory block to be freed.
Returns
osOK on successful deallocation, osErrorParameter if given memory block id is nullptr or invalid, or osErrorResource if given memory block is in an invalid memory pool state.
Note
You may call this function from ISR context.

Definition at line 300 of file MemoryPool.h.