29#include "rtos/mbed_rtos_types.h"
30#include "rtos/internal/mbed_rtos1_types.h"
31#include "rtos/internal/mbed_rtos_storage.h"
32#include "platform/NonCopyable.h"
33#include "platform/mbed_assert.h"
34#include "rtos/Kernel.h"
37#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
58template<
typename T, u
int32_t pool_sz>
60 static_assert(pool_sz > 0,
"Invalid memory pool size. Must be greater than 0.");
68 memset(_pool_mem, 0,
sizeof(_pool_mem));
69 osMemoryPoolAttr_t attr = { 0 };
70 attr.mp_mem = _pool_mem;
71 attr.mp_size =
sizeof(_pool_mem);
72 attr.cb_mem = &_obj_mem;
73 attr.cb_size =
sizeof(_obj_mem);
74 _id = osMemoryPoolNew(pool_sz,
sizeof(T), &attr);
84 osMemoryPoolDelete(_id);
93 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Replaced with try_alloc. In future alloc() will be an untimed blocking call.")
119 return (T *)osMemoryPoolAlloc(_id, 0);
129 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Replaced with `try_alloc_for`. For example use `try_alloc_for(5s)` rather than `alloc_for(5000)`.")
132 return try_alloc_for(std::chrono::duration<uint32_t, std::milli>(millisec));
144 return (T *)osMemoryPoolAlloc(_id, rel_time.count());
159 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Replaced with `try_alloc_until`. For example use `try_alloc_until(Kernel::Clock::now() + 5s)` rather than `alloc_until(Kernel::get_ms_count() + 5000)`.")
162 return try_alloc_until(Kernel::Clock::time_point(std::chrono::duration<uint64_t, std::milli>(millisec)));
178 Kernel::Clock::time_point now = Kernel::Clock::now();
179 Kernel::Clock::duration_u32 rel_time;
180 if (now >= abs_time) {
181 rel_time = rel_time.zero();
185 rel_time = abs_time - now;
196 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Replaced with try_calloc. In future calloc() will be an untimed blocking call.")
210 if (item !=
nullptr) {
211 memset(item, 0,
sizeof(T));
223 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Replaced with `try_calloc_for`. For example use `try_calloc_for(5s)` rather than `calloc_for(5000)`.")
226 return try_calloc_for(std::chrono::duration<uint32_t, std::milli>(millisec));
238 if (item !=
nullptr) {
239 memset(item, 0,
sizeof(T));
256 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Replaced with `try_calloc_until`. For example use `try_calloc_until(Kernel::Clock::now() + 5s)` rather than `calloc_until(Kernel::get_ms_count() + 5000)`.")
259 return try_calloc_until(Kernel::Clock::time_point(std::chrono::duration<uint64_t, std::milli>(millisec)));
275 if (item !=
nullptr) {
276 memset(item, 0,
sizeof(T));
302 return osMemoryPoolFree(_id, block);
306 osMemoryPoolId_t _id;
307 char _pool_mem[MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(pool_sz,
sizeof(T))];
308 mbed_rtos_storage_mem_pool_t _obj_mem;
Prevents generation of copy constructor and copy assignment operator in derived classes.
Define and manage fixed-size memory pools of objects of a given type.
T * calloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero.
~MemoryPool()
Destroy a memory pool.
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.
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.
T * try_alloc_until(Kernel::Clock::time_point abs_time)
Allocate a memory block from a memory pool, blocking.
T * alloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking.
osStatus free(T *block)
Free a memory block.
T * alloc()
Allocate a memory block from a memory pool, without blocking.
T * try_alloc()
Allocate a memory block from a memory pool, without blocking.
T * calloc()
Allocate a memory block from a memory pool, without blocking, and set memory block to zero.
T * try_alloc_for(Kernel::Clock::duration_u32 rel_time)
Allocate a memory block from a memory pool, optionally blocking.
T * calloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking, and set memory block to zero.
MemoryPool()
Create and Initialize a memory pool.
T * try_calloc()
Allocate a memory block from a memory pool, without blocking, and set memory block to zero.
T * alloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking.
constexpr Clock::duration_u32 wait_for_u32_max
Maximum duration for Kernel::Clock::duration_u32-based APIs.