2020-04-04 01:01:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <ksched.h>
|
|
|
|
#include <wait_q.h>
|
|
|
|
#include <init.h>
|
|
|
|
|
|
|
|
void k_heap_init(struct k_heap *h, void *mem, size_t bytes)
|
|
|
|
{
|
|
|
|
z_waitq_init(&h->wait_q);
|
|
|
|
sys_heap_init(&h->heap, mem, bytes);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int statics_init(const struct device *unused)
|
2020-04-04 01:01:03 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
Z_STRUCT_SECTION_FOREACH(k_heap, h) {
|
|
|
|
k_heap_init(h, h->heap.init_mem, h->heap.init_bytes);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(statics_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
|
|
|
|
|
2020-11-13 22:12:31 +08:00
|
|
|
void *k_heap_aligned_alloc(struct k_heap *h, size_t align, size_t bytes,
|
|
|
|
k_timeout_t timeout)
|
2020-04-04 01:01:03 +08:00
|
|
|
{
|
2021-03-13 21:22:38 +08:00
|
|
|
int64_t now, end = sys_clock_timeout_end_calc(timeout);
|
2020-04-04 01:01:03 +08:00
|
|
|
void *ret = NULL;
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&h->lock);
|
|
|
|
|
|
|
|
__ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), "");
|
|
|
|
|
|
|
|
while (ret == NULL) {
|
2020-11-13 22:12:31 +08:00
|
|
|
ret = sys_heap_aligned_alloc(&h->heap, align, bytes);
|
2020-04-04 01:01:03 +08:00
|
|
|
|
2021-03-13 21:21:21 +08:00
|
|
|
now = sys_clock_tick_get();
|
2021-04-19 16:52:34 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_MULTITHREADING) ||
|
|
|
|
(ret != NULL) || ((end - now) <= 0)) {
|
2020-04-04 01:01:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) z_pend_curr(&h->lock, key, &h->wait_q,
|
|
|
|
K_TICKS(end - now));
|
|
|
|
key = k_spin_lock(&h->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
k_spin_unlock(&h->lock, key);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void k_heap_free(struct k_heap *h, void *mem)
|
|
|
|
{
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&h->lock);
|
|
|
|
|
|
|
|
sys_heap_free(&h->heap, mem);
|
2020-04-04 06:39:25 +08:00
|
|
|
|
2021-04-19 16:52:34 +08:00
|
|
|
if (IS_ENABLED(CONFIG_MULTITHREADING) && z_unpend_all(&h->wait_q) != 0) {
|
2020-04-04 06:39:25 +08:00
|
|
|
z_reschedule(&h->lock, key);
|
|
|
|
} else {
|
|
|
|
k_spin_unlock(&h->lock, key);
|
|
|
|
}
|
|
|
|
}
|