/* * qosal allocator backend on Zephyr's kernel heap. * * Replaces qosal/src/zephyr/qmalloc.c, which implements these on libc malloc. That * pulls a libc malloc provider onto the link line, and in a picolibc build two of them * offer one — picolibc's own nano-malloc and Zephyr's lib/libc/common — which collide. * Disabling Zephyr's instead leaves picolibc's wanting __heap_start/__heap_end, symbols * Zephyr only emits for its own allocator. * * Going to k_malloc sidesteps the whole question and puts UWB allocations in the * kernel heap (CONFIG_HEAP_MEM_POOL_SIZE), where they are sized and accounted with * everything else rather than in a second, invisible heap. */ #include "qmalloc.h" #include #include /* * Thread stacks come out of here (see qmalloc_internal), and Zephyr assumes a stack buffer * meets ARCH_STACK_PTR_ALIGN. 32 bytes covers that on Cortex-M and is also the MPU region * granularity the stack guard wants. */ #define QMALLOC_ALIGN 32 /* * Per-quota budgets consumed by qosal's wrappers in qosal/src/qmalloc.c. Index 0 is * MEM_QUOTA_ID_INFINITE. The optional CONFIG_MEM_QUOTA_ID* knobs are not set here, so * the stack runs with a single unbounded quota and the kernel heap is the real limit. */ uint32_t allocation_quotas[] = { ~0U, #ifdef CONFIG_MEM_QUOTA_ID1 CONFIG_MEM_QUOTA_ID1, #endif #ifdef CONFIG_MEM_QUOTA_ID2 CONFIG_MEM_QUOTA_ID2, #endif #ifdef CONFIG_MEM_QUOTA_ID3 CONFIG_MEM_QUOTA_ID3, #endif #ifdef CONFIG_MEM_QUOTA_ID4 CONFIG_MEM_QUOTA_ID4, #endif }; void *qmalloc_internal(size_t size) { /* * Aligned, not plain k_malloc: qthread_create() requires its caller to supply the * thread stack, and the prebuilt uwbmac allocates those through here. Zephyr builds * a thread's initial stack frame assuming the buffer meets ARCH_STACK_PTR_ALIGN, and * k_malloc only guarantees pointer alignment — so a MAC thread came up with a * garbage return address and faulted with "Illegal load of EXC_RETURN into PC" the * moment it was scheduled. */ return k_aligned_alloc(QMALLOC_ALIGN, ROUND_UP(size, QMALLOC_ALIGN)); } void *qrealloc_internal(void *ptr, size_t new_size) { /* * Plain k_realloc, which drops back to pointer alignment. That is fine: the * alignment above matters only for thread stacks, and those are allocated once by * qthread_create's caller and never resized. Growing an aligned block by hand here * would mean tracking the old size ourselves just to copy it. */ return k_realloc(ptr, new_size); } void qfree_internal(void *ptr) { k_free(ptr); }