Skip to content

Commit 0c9f969

Browse files
dcpleungandrewboie
authored andcommitted
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 <[email protected]>
1 parent 041c807 commit 0c9f969

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

kernel/include/kernel_internal.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ extern char *z_setup_new_thread(struct k_thread *new_thread,
4646
void *p1, void *p2, void *p3,
4747
int prio, uint32_t options, const char *name);
4848

49+
/**
50+
* @brief Allocate aligned memory from the current thread's resource pool
51+
*
52+
* Threads may be assigned a resource pool, which will be used to allocate
53+
* memory on behalf of certain kernel and driver APIs. Memory reserved
54+
* in this way should be freed with k_free().
55+
*
56+
* If called from an ISR, the k_malloc() system heap will be used if it exists.
57+
*
58+
* @param align Required memory alignment
59+
* @param size Memory allocation size
60+
* @return A pointer to the allocated memory, or NULL if there is insufficient
61+
* RAM in the pool or there is no pool to draw memory from
62+
*/
63+
void *z_thread_aligned_alloc(size_t align, size_t size);
64+
4965
/**
5066
* @brief Allocate some memory from the current thread's resource pool
5167
*
@@ -59,7 +75,10 @@ extern char *z_setup_new_thread(struct k_thread *new_thread,
5975
* @return A pointer to the allocated memory, or NULL if there is insufficient
6076
* RAM in the pool or there is no pool to draw memory from
6177
*/
62-
void *z_thread_malloc(size_t size);
78+
static inline void *z_thread_malloc(size_t size)
79+
{
80+
return z_thread_aligned_alloc(0, size);
81+
}
6382

6483
/* set and clear essential thread flag */
6584

kernel/mempool.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ static void *z_heap_aligned_alloc(struct k_heap *heap, size_t align, size_t size
3737
return mem + excess;
3838
}
3939

40-
static void *z_heap_malloc(struct k_heap *heap, size_t size)
41-
{
42-
return z_heap_aligned_alloc(heap, sizeof(void *), size);
43-
}
44-
4540
void k_free(void *ptr)
4641
{
4742
struct k_heap **heap_ref;
@@ -98,7 +93,7 @@ void k_thread_system_pool_assign(struct k_thread *thread)
9893
#define _SYSTEM_HEAP NULL
9994
#endif
10095

101-
void *z_thread_malloc(size_t size)
96+
void *z_thread_aligned_alloc(size_t align, size_t size)
10297
{
10398
void *ret;
10499
struct k_heap *heap;
@@ -110,7 +105,7 @@ void *z_thread_malloc(size_t size)
110105
}
111106

112107
if (heap) {
113-
ret = z_heap_malloc(heap, size);
108+
ret = z_heap_aligned_alloc(heap, align, size);
114109
} else {
115110
ret = NULL;
116111
}

0 commit comments

Comments
 (0)