zephyr/lib/cmsis_rtos_v1/cmsis_mempool.c

74 lines
1.5 KiB
C

/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel_structs.h>
#include <cmsis_os.h>
#define TIME_OUT 100
/**
* @brief Create and Initialize a memory pool.
*/
osPoolId osPoolCreate(const osPoolDef_t *pool_def)
{
if (_is_in_isr()) {
return NULL;
}
return (osPoolId)pool_def;
}
/**
* @brief Allocate a memory block from a memory pool.
*/
void *osPoolAlloc(osPoolId pool_id)
{
osPoolDef_t *osPool = (osPoolDef_t *)pool_id;
void *ptr;
if (k_mem_slab_alloc((struct k_mem_slab *)(osPool->pool),
&ptr, TIME_OUT) == 0) {
return ptr;
} else {
return NULL;
}
}
/**
* @brief Allocate a memory block from a memory pool and set it to zero.
*/
void *osPoolCAlloc(osPoolId pool_id)
{
osPoolDef_t *osPool = (osPoolDef_t *)pool_id;
void *ptr;
if (k_mem_slab_alloc((struct k_mem_slab *)(osPool->pool),
&ptr, TIME_OUT) == 0) {
memset(ptr, 0, osPool->item_sz);
return ptr;
} else {
return NULL;
}
}
/**
* @brief Return an allocated memory block back to a specific memory pool.
*/
osStatus osPoolFree(osPoolId pool_id, void *block)
{
osPoolDef_t *osPool = (osPoolDef_t *)pool_id;
/* Note: Below 2 error codes are not supported.
* osErrorValue: block does not belong to the memory pool.
* osErrorParameter: a parameter is invalid or outside of a
* permitted range.
*/
k_mem_slab_free((struct k_mem_slab *)(osPool->pool), (void *)&block);
return osOK;
}