Skip to content

Commit 34d7c78

Browse files
maxbachmannnashif
authored andcommitted
kernel: add k_heap_aligned_alloc
k_heap did not have an aligned alloc function, even though this is supported by the internal sys_heap. Signed-off-by: Maximilian Bachmann <[email protected]>
1 parent 44751a1 commit 34d7c78

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

include/kernel.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4450,6 +4450,25 @@ struct k_heap {
44504450
*/
44514451
void k_heap_init(struct k_heap *h, void *mem, size_t bytes);
44524452

4453+
/** @brief Allocate aligned memory from a k_heap
4454+
*
4455+
* Behaves in all ways like k_heap_alloc(), except that the returned
4456+
* memory (if available) will have a starting address in memory which
4457+
* is a multiple of the specified power-of-two alignment value in
4458+
* bytes. The resulting memory can be returned to the heap using
4459+
* k_heap_free().
4460+
*
4461+
* @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
4462+
*
4463+
* @param h Heap from which to allocate
4464+
* @param align Alignment in bytes, must be a power of two
4465+
* @param bytes Number of bytes requested
4466+
* @param timeout How long to wait, or K_NO_WAIT
4467+
* @return Pointer to memory the caller can now use
4468+
*/
4469+
void *k_heap_aligned_alloc(struct k_heap *h, size_t align, size_t bytes,
4470+
k_timeout_t timeout);
4471+
44534472
/**
44544473
* @brief Allocate memory from a k_heap
44554474
*
@@ -4467,7 +4486,11 @@ void k_heap_init(struct k_heap *h, void *mem, size_t bytes);
44674486
* @param timeout How long to wait, or K_NO_WAIT
44684487
* @return A pointer to valid heap memory, or NULL
44694488
*/
4470-
void *k_heap_alloc(struct k_heap *h, size_t bytes, k_timeout_t timeout);
4489+
static inline void *k_heap_alloc(struct k_heap *h, size_t bytes,
4490+
k_timeout_t timeout)
4491+
{
4492+
return k_heap_aligned_alloc(h, sizeof(void *), bytes, timeout);
4493+
}
44714494

44724495
/**
44734496
* @brief Free memory allocated by k_heap_alloc()

kernel/kheap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ static int statics_init(const struct device *unused)
2626

2727
SYS_INIT(statics_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
2828

29-
void *k_heap_alloc(struct k_heap *h, size_t bytes, k_timeout_t timeout)
29+
void *k_heap_aligned_alloc(struct k_heap *h, size_t align, size_t bytes,
30+
k_timeout_t timeout)
3031
{
3132
int64_t now, end = z_timeout_end_calc(timeout);
3233
void *ret = NULL;
@@ -35,7 +36,7 @@ void *k_heap_alloc(struct k_heap *h, size_t bytes, k_timeout_t timeout)
3536
__ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), "");
3637

3738
while (ret == NULL) {
38-
ret = sys_heap_alloc(&h->heap, bytes);
39+
ret = sys_heap_aligned_alloc(&h->heap, align, bytes);
3940

4041
now = z_tick_get();
4142
if ((ret != NULL) || ((end - now) <= 0)) {

0 commit comments

Comments
 (0)