2018-07-12 17:45:57 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-10-24 23:08:21 +08:00
|
|
|
#include <kernel.h>
|
2018-07-12 17:45:57 +08:00
|
|
|
#include <cmsis_os.h>
|
2019-10-24 23:08:21 +08:00
|
|
|
#include <string.h>
|
2018-07-12 17:45:57 +08:00
|
|
|
|
|
|
|
#define TIME_OUT 100
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create and Initialize a memory pool.
|
|
|
|
*/
|
|
|
|
osPoolId osPoolCreate(const osPoolDef_t *pool_def)
|
|
|
|
{
|
2018-11-15 17:57:13 +08:00
|
|
|
if (k_is_in_isr()) {
|
2018-07-12 17:45:57 +08:00
|
|
|
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) {
|
2018-09-12 10:09:03 +08:00
|
|
|
(void)memset(ptr, 0, osPool->item_sz);
|
2018-07-12 17:45:57 +08:00
|
|
|
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;
|
|
|
|
}
|