diff --git a/kernel/include/kernel_internal.h b/kernel/include/kernel_internal.h index 7e7a34c4e8e..a05925d7f3c 100644 --- a/kernel/include/kernel_internal.h +++ b/kernel/include/kernel_internal.h @@ -46,6 +46,22 @@ extern char *z_setup_new_thread(struct k_thread *new_thread, void *p1, void *p2, void *p3, int prio, uint32_t options, const char *name); +/** + * @brief Allocate aligned memory from the current thread's resource pool + * + * Threads may be assigned a resource pool, which will be used to allocate + * memory on behalf of certain kernel and driver APIs. Memory reserved + * in this way should be freed with k_free(). + * + * If called from an ISR, the k_malloc() system heap will be used if it exists. + * + * @param align Required memory alignment + * @param size Memory allocation size + * @return A pointer to the allocated memory, or NULL if there is insufficient + * RAM in the pool or there is no pool to draw memory from + */ +void *z_thread_aligned_alloc(size_t align, size_t size); + /** * @brief Allocate some memory from the current thread's resource pool * @@ -59,7 +75,10 @@ extern char *z_setup_new_thread(struct k_thread *new_thread, * @return A pointer to the allocated memory, or NULL if there is insufficient * RAM in the pool or there is no pool to draw memory from */ -void *z_thread_malloc(size_t size); +static inline void *z_thread_malloc(size_t size) +{ + return z_thread_aligned_alloc(0, size); +} /* set and clear essential thread flag */ diff --git a/kernel/mempool.c b/kernel/mempool.c index f67fde3585f..b2de4596abd 100644 --- a/kernel/mempool.c +++ b/kernel/mempool.c @@ -37,11 +37,6 @@ static void *z_heap_aligned_alloc(struct k_heap *heap, size_t align, size_t size return mem + excess; } -static void *z_heap_malloc(struct k_heap *heap, size_t size) -{ - return z_heap_aligned_alloc(heap, sizeof(void *), size); -} - void k_free(void *ptr) { struct k_heap **heap_ref; @@ -98,7 +93,7 @@ void k_thread_system_pool_assign(struct k_thread *thread) #define _SYSTEM_HEAP NULL #endif -void *z_thread_malloc(size_t size) +void *z_thread_aligned_alloc(size_t align, size_t size) { void *ret; struct k_heap *heap; @@ -110,7 +105,7 @@ void *z_thread_malloc(size_t size) } if (heap) { - ret = z_heap_malloc(heap, size); + ret = z_heap_aligned_alloc(heap, align, size); } else { ret = NULL; }