kernel: mempool: add z_thread_aligned_alloc

This adds a new z_thread_aligned_alloc() to do memory allocation
with required alignment.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2020-12-15 13:37:11 -08:00 committed by Andrew Boie
commit 0c9f9691c4
2 changed files with 22 additions and 8 deletions

View file

@ -46,6 +46,22 @@ extern char *z_setup_new_thread(struct k_thread *new_thread,
void *p1, void *p2, void *p3, void *p1, void *p2, void *p3,
int prio, uint32_t options, const char *name); 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 * @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 * @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 * 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 */ /* set and clear essential thread flag */

View file

@ -37,11 +37,6 @@ static void *z_heap_aligned_alloc(struct k_heap *heap, size_t align, size_t size
return mem + excess; 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) void k_free(void *ptr)
{ {
struct k_heap **heap_ref; struct k_heap **heap_ref;
@ -98,7 +93,7 @@ void k_thread_system_pool_assign(struct k_thread *thread)
#define _SYSTEM_HEAP NULL #define _SYSTEM_HEAP NULL
#endif #endif
void *z_thread_malloc(size_t size) void *z_thread_aligned_alloc(size_t align, size_t size)
{ {
void *ret; void *ret;
struct k_heap *heap; struct k_heap *heap;
@ -110,7 +105,7 @@ void *z_thread_malloc(size_t size)
} }
if (heap) { if (heap) {
ret = z_heap_malloc(heap, size); ret = z_heap_aligned_alloc(heap, align, size);
} else { } else {
ret = NULL; ret = NULL;
} }