Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / qorvo-uwb / src / qmalloc_zephyr.c
qmalloc_zephyr.c74 lines · 2.5 KiBRaw
| 1 | /* |
| 2 | * qosal allocator backend on Zephyr's kernel heap. |
| 3 | * |
| 4 | * Replaces qosal/src/zephyr/qmalloc.c, which implements these on libc malloc. That |
| 5 | * pulls a libc malloc provider onto the link line, and in a picolibc build two of them |
| 6 | * offer one — picolibc's own nano-malloc and Zephyr's lib/libc/common — which collide. |
| 7 | * Disabling Zephyr's instead leaves picolibc's wanting __heap_start/__heap_end, symbols |
| 8 | * Zephyr only emits for its own allocator. |
| 9 | * |
| 10 | * Going to k_malloc sidesteps the whole question and puts UWB allocations in the |
| 11 | * kernel heap (CONFIG_HEAP_MEM_POOL_SIZE), where they are sized and accounted with |
| 12 | * everything else rather than in a second, invisible heap. |
| 13 | */ |
| 14 | #include "qmalloc.h" |
| 15 | |
| 16 | #include <zephyr/kernel.h> |
| 17 | #include <zephyr/sys/util.h> |
| 18 | |
| 19 | /* |
| 20 | * Thread stacks come out of here (see qmalloc_internal), and Zephyr assumes a stack buffer |
| 21 | * meets ARCH_STACK_PTR_ALIGN. 32 bytes covers that on Cortex-M and is also the MPU region |
| 22 | * granularity the stack guard wants. |
| 23 | */ |
| 24 | #define QMALLOC_ALIGN 32 |
| 25 | |
| 26 | /* |
| 27 | * Per-quota budgets consumed by qosal's wrappers in qosal/src/qmalloc.c. Index 0 is |
| 28 | * MEM_QUOTA_ID_INFINITE. The optional CONFIG_MEM_QUOTA_ID* knobs are not set here, so |
| 29 | * the stack runs with a single unbounded quota and the kernel heap is the real limit. |
| 30 | */ |
| 31 | uint32_t allocation_quotas[] = { |
| 32 | ~0U, |
| 33 | #ifdef CONFIG_MEM_QUOTA_ID1 |
| 34 | CONFIG_MEM_QUOTA_ID1, |
| 35 | #endif |
| 36 | #ifdef CONFIG_MEM_QUOTA_ID2 |
| 37 | CONFIG_MEM_QUOTA_ID2, |
| 38 | #endif |
| 39 | #ifdef CONFIG_MEM_QUOTA_ID3 |
| 40 | CONFIG_MEM_QUOTA_ID3, |
| 41 | #endif |
| 42 | #ifdef CONFIG_MEM_QUOTA_ID4 |
| 43 | CONFIG_MEM_QUOTA_ID4, |
| 44 | #endif |
| 45 | }; |
| 46 | |
| 47 | void *qmalloc_internal(size_t size) |
| 48 | { |
| 49 | /* |
| 50 | * Aligned, not plain k_malloc: qthread_create() requires its caller to supply the |
| 51 | * thread stack, and the prebuilt uwbmac allocates those through here. Zephyr builds |
| 52 | * a thread's initial stack frame assuming the buffer meets ARCH_STACK_PTR_ALIGN, and |
| 53 | * k_malloc only guarantees pointer alignment — so a MAC thread came up with a |
| 54 | * garbage return address and faulted with "Illegal load of EXC_RETURN into PC" the |
| 55 | * moment it was scheduled. |
| 56 | */ |
| 57 | return k_aligned_alloc(QMALLOC_ALIGN, ROUND_UP(size, QMALLOC_ALIGN)); |
| 58 | } |
| 59 | |
| 60 | void *qrealloc_internal(void *ptr, size_t new_size) |
| 61 | { |
| 62 | /* |
| 63 | * Plain k_realloc, which drops back to pointer alignment. That is fine: the |
| 64 | * alignment above matters only for thread stacks, and those are allocated once by |
| 65 | * qthread_create's caller and never resized. Growing an aligned block by hand here |
| 66 | * would mean tracking the old size ourselves just to copy it. |
| 67 | */ |
| 68 | return k_realloc(ptr, new_size); |
| 69 | } |
| 70 | |
| 71 | void qfree_internal(void *ptr) |
| 72 | { |
| 73 | k_free(ptr); |
| 74 | } |