79e6b0e0f6
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>. This patch proposes to then include <zephyr/kernel.h> instead of <zephyr/zephyr.h> since it is more clear that you are including the Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a catch-all header that may be confusing. Most applications need to include a bunch of other things to compile, e.g. driver headers or subsystem headers like BT, logging, etc. The idea of a catch-all header in Zephyr is probably not feasible anyway. Reason is that Zephyr is not a library, like it could be for example `libpython`. Zephyr provides many utilities nowadays: a kernel, drivers, subsystems, etc and things will likely grow. A catch-all header would be massive, difficult to keep up-to-date. It is also likely that an application will only build a small subset. Note that subsystem-level headers may use a catch-all approach to make things easier, though. NOTE: This patch is **NOT** removing the header, just removing its usage in-tree. I'd advocate for its deprecation (add a #warning on it), but I understand many people will have concerns. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
114 lines
2.3 KiB
C
114 lines
2.3 KiB
C
/*
|
|
* Copyright (c) 2021 Carlo Caione, <ccaione@baylibre.com>
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/device.h>
|
|
#include <zephyr/sys/sys_heap.h>
|
|
#include <zephyr/sys/multi_heap.h>
|
|
|
|
#include <zephyr/multi_heap/shared_multi_heap.h>
|
|
|
|
static struct sys_multi_heap shared_multi_heap;
|
|
static struct sys_heap heap_pool[MAX_SHARED_MULTI_HEAP_ATTR][MAX_MULTI_HEAPS];
|
|
|
|
static unsigned int attr_cnt[MAX_SHARED_MULTI_HEAP_ATTR];
|
|
|
|
static void *smh_choice(struct sys_multi_heap *mheap, void *cfg, size_t align, size_t size)
|
|
{
|
|
struct sys_heap *h;
|
|
unsigned int attr;
|
|
void *block;
|
|
|
|
attr = (unsigned int)(long) cfg;
|
|
|
|
if (attr >= MAX_SHARED_MULTI_HEAP_ATTR || size == 0) {
|
|
return NULL;
|
|
}
|
|
|
|
/* Set in case the user requested a non-existing attr */
|
|
block = NULL;
|
|
|
|
for (size_t hdx = 0; hdx < attr_cnt[attr]; hdx++) {
|
|
h = &heap_pool[attr][hdx];
|
|
|
|
if (h->heap == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
block = sys_heap_aligned_alloc(h, align, size);
|
|
if (block != NULL) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return block;
|
|
}
|
|
|
|
int shared_multi_heap_add(struct shared_multi_heap_region *region, void *user_data)
|
|
{
|
|
static int n_heaps;
|
|
struct sys_heap *h;
|
|
unsigned int slot;
|
|
|
|
if (region->attr >= MAX_SHARED_MULTI_HEAP_ATTR) {
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* No more heaps available */
|
|
if (n_heaps++ >= MAX_MULTI_HEAPS) {
|
|
return -ENOMEM;
|
|
}
|
|
|
|
slot = attr_cnt[region->attr];
|
|
h = &heap_pool[region->attr][slot];
|
|
|
|
sys_heap_init(h, (void *) region->addr, region->size);
|
|
sys_multi_heap_add_heap(&shared_multi_heap, h, user_data);
|
|
|
|
attr_cnt[region->attr]++;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void shared_multi_heap_free(void *block)
|
|
{
|
|
sys_multi_heap_free(&shared_multi_heap, block);
|
|
}
|
|
|
|
void *shared_multi_heap_alloc(unsigned int attr, size_t bytes)
|
|
{
|
|
if (attr >= MAX_SHARED_MULTI_HEAP_ATTR) {
|
|
return NULL;
|
|
}
|
|
|
|
return sys_multi_heap_alloc(&shared_multi_heap, (void *)(long) attr, bytes);
|
|
}
|
|
|
|
void *shared_multi_heap_aligned_alloc(unsigned int attr, size_t align, size_t bytes)
|
|
{
|
|
if (attr >= MAX_SHARED_MULTI_HEAP_ATTR) {
|
|
return NULL;
|
|
}
|
|
|
|
return sys_multi_heap_aligned_alloc(&shared_multi_heap, (void *)(long) attr,
|
|
align, bytes);
|
|
}
|
|
|
|
int shared_multi_heap_pool_init(void)
|
|
{
|
|
static atomic_t state;
|
|
|
|
if (!atomic_cas(&state, 0, 1)) {
|
|
return -EALREADY;
|
|
}
|
|
|
|
sys_multi_heap_init(&shared_multi_heap, smh_choice);
|
|
|
|
atomic_set(&state, 1);
|
|
|
|
return 0;
|
|
}
|