lib/cmsis_rtos_v1: Implement support for mempool APIs
These APIs allow creating, allocating and freeing of mempools. Note: "Mempool" in CMSIS actually means memslabs in Zephyr. Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
This commit is contained in:
parent
c996d0581f
commit
aff8c51128
3 changed files with 76 additions and 1 deletions
|
@ -535,8 +535,9 @@ osStatus osSemaphoreDelete (osSemaphoreId semaphore_id);
|
||||||
extern const osPoolDef_t os_pool_def_##name
|
extern const osPoolDef_t os_pool_def_##name
|
||||||
#else // define the object
|
#else // define the object
|
||||||
#define osPoolDef(name, no, type) \
|
#define osPoolDef(name, no, type) \
|
||||||
|
K_MEM_SLAB_DEFINE(os_mem_##name, sizeof(type), no, 4); \
|
||||||
const osPoolDef_t os_pool_def_##name = \
|
const osPoolDef_t os_pool_def_##name = \
|
||||||
{ (no), sizeof(type), NULL }
|
{ (no), sizeof(type), &os_mem_##name }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// \brief Access a Memory Pool definition.
|
/// \brief Access a Memory Pool definition.
|
||||||
|
|
|
@ -12,6 +12,7 @@ zephyr_library_sources_ifdef(
|
||||||
cmsis_timer.c
|
cmsis_timer.c
|
||||||
cmsis_mutex.c
|
cmsis_mutex.c
|
||||||
cmsis_semaphore.c
|
cmsis_semaphore.c
|
||||||
|
cmsis_mempool.c
|
||||||
)
|
)
|
||||||
|
|
||||||
zephyr_library_link_libraries(CMSIS)
|
zephyr_library_link_libraries(CMSIS)
|
||||||
|
|
73
lib/cmsis_rtos_v1/cmsis_mempool.c
Normal file
73
lib/cmsis_rtos_v1/cmsis_mempool.c
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue