1 /* 2 * Copyright (c) 2018 Intel Corporation 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #include <zephyr/kernel.h> 8 #include <cmsis_os.h> 9 #include <string.h> 10 11 #define TIME_OUT K_MSEC(100) 12 13 /** 14 * @brief Create and Initialize a memory pool. 15 */ osPoolCreate(const osPoolDef_t * pool_def)16osPoolId osPoolCreate(const osPoolDef_t *pool_def) 17 { 18 if (k_is_in_isr()) { 19 return NULL; 20 } 21 22 return (osPoolId)pool_def; 23 } 24 25 /** 26 * @brief Allocate a memory block from a memory pool. 27 */ osPoolAlloc(osPoolId pool_id)28void *osPoolAlloc(osPoolId pool_id) 29 { 30 osPoolDef_t *osPool = (osPoolDef_t *)pool_id; 31 void *ptr; 32 33 if (k_mem_slab_alloc((struct k_mem_slab *)(osPool->pool), 34 &ptr, TIME_OUT) == 0) { 35 return ptr; 36 } else { 37 return NULL; 38 } 39 } 40 41 /** 42 * @brief Allocate a memory block from a memory pool and set it to zero. 43 */ osPoolCAlloc(osPoolId pool_id)44void *osPoolCAlloc(osPoolId pool_id) 45 { 46 osPoolDef_t *osPool = (osPoolDef_t *)pool_id; 47 void *ptr; 48 49 if (k_mem_slab_alloc((struct k_mem_slab *)(osPool->pool), 50 &ptr, TIME_OUT) == 0) { 51 (void)memset(ptr, 0, osPool->item_sz); 52 return ptr; 53 } else { 54 return NULL; 55 } 56 } 57 58 /** 59 * @brief Return an allocated memory block back to a specific memory pool. 60 */ osPoolFree(osPoolId pool_id,void * block)61osStatus osPoolFree(osPoolId pool_id, void *block) 62 { 63 osPoolDef_t *osPool = (osPoolDef_t *)pool_id; 64 65 /* Note: Below 2 error codes are not supported. 66 * osErrorValue: block does not belong to the memory pool. 67 * osErrorParameter: a parameter is invalid or outside of a 68 * permitted range. 69 */ 70 71 k_mem_slab_free((struct k_mem_slab *)(osPool->pool), (void *)block); 72 73 return osOK; 74 } 75