From 7f95e83361283fc1e06cc463588d331003f0f7ee Mon Sep 17 00:00:00 2001 From: Andrew Boie Date: Wed, 8 Nov 2017 14:40:01 -0800 Subject: [PATCH] mempool: add k_calloc() This uses the kernel heap to implement traditional calloc() semantics. Signed-off-by: Andrew Boie --- include/kernel.h | 13 +++++++++++++ kernel/mempool.c | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/kernel.h b/include/kernel.h index 1ec102afc4e..b8de1b97463 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -3704,6 +3704,19 @@ extern void *k_malloc(size_t size); */ extern void k_free(void *ptr); +/** + * @brief Allocate memory from heap, array style + * + * This routine provides traditional calloc() semantics. Memory is + * allocated from the heap memory pool and zeroed. + * + * @param nmemb Number of elements in the requested array + * @param size Size of each array element (in bytes). + * + * @return Address of the allocated memory if successful; otherwise NULL. + */ +extern void *k_calloc(size_t nmemb, size_t size); + /** * @} end defgroup heap_apis */ diff --git a/kernel/mempool.c b/kernel/mempool.c index 4d80615f49c..49a9ca75257 100644 --- a/kernel/mempool.c +++ b/kernel/mempool.c @@ -405,4 +405,17 @@ void k_free(void *ptr) k_mem_pool_free(ptr); } } + +void *k_calloc(size_t nmemb, size_t size) +{ + void *ret; + size_t bounds; + + bounds = nmemb * size; + ret = k_malloc(bounds); + if (ret) { + memset(ret, 0, bounds); + } + return ret; +} #endif