3fbf12487c
There are several subsystems and boards which require a relatively large system heap (used by k_malloc()) to function properly. This became even more notable with the recent introduction of the ACPICA library, which causes ACPI-using boards to require a system heap of up to several megabytes in size. Until now, subsystems and boards have tried to solve this by having Kconfig overlays which modify the default value of HEAP_MEM_POOL_SIZE. This works ok, except when applications start explicitly setting values in their prj.conf files: $ git grep CONFIG_HEAP_MEM_POOL_SIZE= tests samples|wc -l 157 The vast majority of values set by current sample or test applications is much too small for subsystems like ACPI, which results in the application not being able to run on such boards. To solve this situation, we introduce support for subsystems to specify their own custom system heap size requirement. Subsystems do this by defining Kconfig options with the prefix HEAP_MEM_POOL_ADD_SIZE_. The final value of the system heap is the sum of the custom minimum requirements, or the value existing HEAP_MEM_POOL_SIZE option, whichever is greater. We also introduce a new HEAP_MEM_POOL_IGNORE_MIN Kconfig option which applications can use to force a lower value than what subsystems have specficied, however this behavior is disabled by default. Whenever the minimum is greater than the requested value a CMake warning will be issued in the build output. This patch ends up modifying several places outside of kernel code, since the presence of the system heap is no longer detected using a non-zero CONFIG_HEAP_MEM_POOL_SIZE value, rather it's now detected using a new K_HEAP_MEM_POOL_SIZE value that's evaluated at build. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
143 lines
3 KiB
C
143 lines
3 KiB
C
/*
|
|
* Copyright (c) 2017 Intel Corporation
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <zephyr/kernel.h>
|
|
#include <string.h>
|
|
#include <zephyr/sys/math_extras.h>
|
|
#include <zephyr/sys/util.h>
|
|
|
|
static void *z_heap_aligned_alloc(struct k_heap *heap, size_t align, size_t size)
|
|
{
|
|
void *mem;
|
|
struct k_heap **heap_ref;
|
|
size_t __align;
|
|
|
|
/*
|
|
* Adjust the size to make room for our heap reference.
|
|
* Merge a rewind bit with align value (see sys_heap_aligned_alloc()).
|
|
* This allows for storing the heap pointer right below the aligned
|
|
* boundary without wasting any memory.
|
|
*/
|
|
if (size_add_overflow(size, sizeof(heap_ref), &size)) {
|
|
return NULL;
|
|
}
|
|
__align = align | sizeof(heap_ref);
|
|
|
|
mem = k_heap_aligned_alloc(heap, __align, size, K_NO_WAIT);
|
|
if (mem == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
heap_ref = mem;
|
|
*heap_ref = heap;
|
|
mem = ++heap_ref;
|
|
__ASSERT(align == 0 || ((uintptr_t)mem & (align - 1)) == 0,
|
|
"misaligned memory at %p (align = %zu)", mem, align);
|
|
|
|
return mem;
|
|
}
|
|
|
|
void k_free(void *ptr)
|
|
{
|
|
struct k_heap **heap_ref;
|
|
|
|
if (ptr != NULL) {
|
|
heap_ref = ptr;
|
|
ptr = --heap_ref;
|
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap_sys, k_free, *heap_ref, heap_ref);
|
|
|
|
k_heap_free(*heap_ref, ptr);
|
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap_sys, k_free, *heap_ref, heap_ref);
|
|
}
|
|
}
|
|
|
|
#if (K_HEAP_MEM_POOL_SIZE > 0)
|
|
|
|
K_HEAP_DEFINE(_system_heap, K_HEAP_MEM_POOL_SIZE);
|
|
#define _SYSTEM_HEAP (&_system_heap)
|
|
|
|
void *k_aligned_alloc(size_t align, size_t size)
|
|
{
|
|
__ASSERT(align / sizeof(void *) >= 1
|
|
&& (align % sizeof(void *)) == 0,
|
|
"align must be a multiple of sizeof(void *)");
|
|
|
|
__ASSERT((align & (align - 1)) == 0,
|
|
"align must be a power of 2");
|
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap_sys, k_aligned_alloc, _SYSTEM_HEAP);
|
|
|
|
void *ret = z_heap_aligned_alloc(_SYSTEM_HEAP, align, size);
|
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap_sys, k_aligned_alloc, _SYSTEM_HEAP, ret);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void *k_malloc(size_t size)
|
|
{
|
|
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap_sys, k_malloc, _SYSTEM_HEAP);
|
|
|
|
void *ret = k_aligned_alloc(sizeof(void *), size);
|
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap_sys, k_malloc, _SYSTEM_HEAP, ret);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void *k_calloc(size_t nmemb, size_t size)
|
|
{
|
|
void *ret;
|
|
size_t bounds;
|
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap_sys, k_calloc, _SYSTEM_HEAP);
|
|
|
|
if (size_mul_overflow(nmemb, size, &bounds)) {
|
|
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap_sys, k_calloc, _SYSTEM_HEAP, NULL);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
ret = k_malloc(bounds);
|
|
if (ret != NULL) {
|
|
(void)memset(ret, 0, bounds);
|
|
}
|
|
|
|
SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap_sys, k_calloc, _SYSTEM_HEAP, ret);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void k_thread_system_pool_assign(struct k_thread *thread)
|
|
{
|
|
thread->resource_pool = _SYSTEM_HEAP;
|
|
}
|
|
#else
|
|
#define _SYSTEM_HEAP NULL
|
|
#endif
|
|
|
|
void *z_thread_aligned_alloc(size_t align, size_t size)
|
|
{
|
|
void *ret;
|
|
struct k_heap *heap;
|
|
|
|
if (k_is_in_isr()) {
|
|
heap = _SYSTEM_HEAP;
|
|
} else {
|
|
heap = _current->resource_pool;
|
|
}
|
|
|
|
if (heap != NULL) {
|
|
ret = z_heap_aligned_alloc(heap, align, size);
|
|
} else {
|
|
ret = NULL;
|
|
}
|
|
|
|
return ret;
|
|
}
|