mempool: use k_malloc heap for ISR allocations

Fixes an issue where calling z_thread_malloc() would
borrow the resource pool of whatever thread happened
to be interrupted at the time.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2019-05-22 10:38:43 -07:00 committed by Andrew Boie
commit 6f654bbafd
2 changed files with 14 additions and 3 deletions

View file

@ -67,9 +67,11 @@ extern void z_app_shmem_bss_zero(void);
* memory on behalf of certain kernel and driver APIs. Memory reserved
* in this way should be freed with k_free().
*
* If called from an ISR, the k_malloc() system heap will be used if it exists.
*
* @param size Memory allocation size
* @return A pointer to the allocated memory, or NULL if there is insufficient
* RAM in the pool or the thread has no resource pool assigned
* RAM in the pool or there is no pool to draw memory from
*/
void *z_thread_malloc(size_t size);

View file

@ -188,14 +188,23 @@ void k_thread_system_pool_assign(struct k_thread *thread)
{
thread->resource_pool = _HEAP_MEM_POOL;
}
#else
#define _HEAP_MEM_POOL NULL
#endif
void *z_thread_malloc(size_t size)
{
void *ret;
struct k_mem_pool *pool;
if (_current->resource_pool != NULL) {
ret = k_mem_pool_malloc(_current->resource_pool, size);
if (k_is_in_isr()) {
pool = _HEAP_MEM_POOL;
} else {
pool = _current->resource_pool;
}
if (pool) {
ret = k_mem_pool_malloc(pool, size);
} else {
ret = NULL;
}