2021-09-22 21:53:42 +08:00
|
|
|
/* Copyright (c) 2021 Intel Corporation
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2022-05-06 17:23:05 +08:00
|
|
|
#include <zephyr/sys/__assert.h>
|
|
|
|
#include <zephyr/sys/util.h>
|
|
|
|
#include <zephyr/sys/sys_heap.h>
|
|
|
|
#include <zephyr/sys/multi_heap.h>
|
2021-09-22 21:53:42 +08:00
|
|
|
|
|
|
|
void sys_multi_heap_init(struct sys_multi_heap *heap, sys_multi_heap_fn_t choice_fn)
|
|
|
|
{
|
|
|
|
heap->nheaps = 0;
|
|
|
|
heap->choice = choice_fn;
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:13:48 +08:00
|
|
|
void sys_multi_heap_add_heap(struct sys_multi_heap *mheap,
|
|
|
|
struct sys_heap *heap, void *user_data)
|
2021-09-22 21:53:42 +08:00
|
|
|
{
|
|
|
|
__ASSERT_NO_MSG(mheap->nheaps < ARRAY_SIZE(mheap->heaps));
|
|
|
|
|
2022-03-10 16:13:48 +08:00
|
|
|
mheap->heaps[mheap->nheaps].heap = heap;
|
|
|
|
mheap->heaps[mheap->nheaps++].user_data = user_data;
|
2021-09-22 21:53:42 +08:00
|
|
|
|
|
|
|
/* Now sort them in memory order, simple extraction sort */
|
|
|
|
for (int i = 0; i < mheap->nheaps; i++) {
|
2022-03-10 16:13:48 +08:00
|
|
|
struct sys_multi_heap_rec swap;
|
2021-09-22 21:53:42 +08:00
|
|
|
int lowest = -1;
|
|
|
|
uintptr_t lowest_addr = UINTPTR_MAX;
|
|
|
|
|
|
|
|
for (int j = i; j < mheap->nheaps; j++) {
|
2022-03-10 16:13:48 +08:00
|
|
|
uintptr_t haddr = (uintptr_t)mheap->heaps[j].heap->heap;
|
2021-09-22 21:53:42 +08:00
|
|
|
|
|
|
|
if (haddr < lowest_addr) {
|
|
|
|
lowest = j;
|
|
|
|
lowest_addr = haddr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
swap = mheap->heaps[i];
|
|
|
|
mheap->heaps[i] = mheap->heaps[lowest];
|
|
|
|
mheap->heaps[lowest] = swap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *sys_multi_heap_alloc(struct sys_multi_heap *mheap, void *cfg, size_t bytes)
|
|
|
|
{
|
|
|
|
return mheap->choice(mheap, cfg, 0, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *sys_multi_heap_aligned_alloc(struct sys_multi_heap *mheap,
|
|
|
|
void *cfg, size_t align, size_t bytes)
|
|
|
|
{
|
|
|
|
return mheap->choice(mheap, cfg, align, bytes);
|
|
|
|
}
|
|
|
|
|
2022-03-10 16:30:31 +08:00
|
|
|
const struct sys_multi_heap_rec *sys_multi_heap_get_heap(const struct sys_multi_heap *mheap,
|
|
|
|
void *addr)
|
2021-09-22 21:53:42 +08:00
|
|
|
{
|
2022-03-10 16:30:31 +08:00
|
|
|
uintptr_t haddr, baddr = (uintptr_t) addr;
|
2021-09-22 21:53:42 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Search the heaps array to find the correct heap
|
|
|
|
*
|
|
|
|
* FIXME: just a linear search currently, as the list is
|
|
|
|
* always short for reasonable apps and this code is very
|
|
|
|
* quick. The array is stored in sorted order though, so a
|
|
|
|
* binary search based on the block address is the design
|
|
|
|
* goal.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < mheap->nheaps; i++) {
|
2022-03-10 16:13:48 +08:00
|
|
|
haddr = (uintptr_t)mheap->heaps[i].heap->heap;
|
2021-09-22 21:53:42 +08:00
|
|
|
if (baddr < haddr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now i stores the index of the heap after our target (even
|
|
|
|
* if it's invalid and our target is the last!)
|
2022-03-10 16:30:31 +08:00
|
|
|
* FIXME: return -ENOENT when a proper heap is not found
|
2021-09-22 21:53:42 +08:00
|
|
|
*/
|
2022-03-10 16:30:31 +08:00
|
|
|
return &mheap->heaps[i-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void sys_multi_heap_free(struct sys_multi_heap *mheap, void *block)
|
|
|
|
{
|
|
|
|
const struct sys_multi_heap_rec *heap;
|
|
|
|
|
|
|
|
heap = sys_multi_heap_get_heap(mheap, block);
|
|
|
|
|
2022-07-06 19:40:01 +08:00
|
|
|
if (heap != NULL) {
|
2022-03-10 16:30:31 +08:00
|
|
|
sys_heap_free(heap->heap, block);
|
2022-07-06 19:40:01 +08:00
|
|
|
}
|
2021-09-22 21:53:42 +08:00
|
|
|
}
|