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) 58 template<
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.")
106 return (T *)osMemoryPoolAlloc(_id, 0);
116 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)`.")
119 return try_alloc_for(std::chrono::duration<uint32_t, std::milli>(millisec));
130 return (T *)osMemoryPoolAlloc(_id, rel_time.count());
145 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)`.")
148 return try_alloc_until(Kernel::Clock::time_point(std::chrono::duration<uint64_t, std::milli>(millisec)));
163 Kernel::Clock::time_point now = Kernel::Clock::now();
164 Kernel::Clock::duration_u32 rel_time;
165 if (now >= abs_time) {
166 rel_time = rel_time.zero();
170 rel_time = abs_time - now;
181 MBED_DEPRECATED_SINCE(
"mbed-os-6.0.0",
"Replaced with try_calloc. In future calloc() will be an untimed blocking call.")
195 if (item !=
nullptr) {
196 memset(item, 0,
sizeof(T));
208 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)`.")
211 return try_calloc_for(std::chrono::duration<uint32_t, std::milli>(millisec));
223 if (item !=
nullptr) {
224 memset(item, 0,
sizeof(T));
241 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)`.")
244 return try_calloc_until(Kernel::Clock::time_point(std::chrono::duration<uint64_t, std::milli>(millisec)));
260 if (item !=
nullptr) {
261 memset(item, 0,
sizeof(T));
276 return osMemoryPoolFree(_id, block);
280 osMemoryPoolId_t _id;
281 char _pool_mem[MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(pool_sz,
sizeof(T))];
282 mbed_rtos_storage_mem_pool_t _obj_mem;
~MemoryPool()
Destroy a memory pool.
Define and manage fixed-size memory pools of objects of a given type.
T * calloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking, and set memory block to zero.
T * alloc_until(uint64_t millisec)
Allocate a memory block from a memory pool, blocking.
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 * alloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking.
T * try_alloc_for(Kernel::Clock::duration_u32 rel_time)
Allocate a memory block from a memory pool, optionally blocking.
Prevents generation of copy constructor and copy assignment operator in derived classes.
constexpr Clock::duration_u32 wait_for_u32_max
Maximum duration for Kernel::Clock::duration_u32-based APIs.
osStatus free(T *block)
Free a memory block.
T * try_alloc_until(Kernel::Clock::time_point abs_time)
Allocate a memory block from a memory pool, blocking.
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...
MemoryPool()
Create and Initialize a memory pool.
T * calloc_for(uint32_t millisec)
Allocate a memory block from a memory pool, optionally blocking, and set memory block to zero...
T * try_calloc()
Allocate a memory block from a memory pool, without 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.